<?php
/**
 *
 * Library Unit search form
 *
 * @author Moises Heberle [moises@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 *
 * @since
 * Class created on 29/07/2008
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2008 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 *
 **/

class FrmReserveSearch extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $busRS;
    public $busReserveComposition;
    public $busRT;
    public $busLU;


    public function __construct()
    {
        $this->MIOLO        = MIOLO::getInstance();
        $this->module       = MIOLO::getCurrentModule();
        $this->busRS        = $this->MIOLO->getBusiness($this->module, 'BusReserveStatus');
        $this->busReserveComposition = $this->MIOLO->getBusiness($this->module, 'BusReserveComposition');
        $this->busRT        = $this->MIOLO->getBusiness($this->module, 'BusReserveType');
        $this->busLU        = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        $this->setAllFunctions('Reserve', 'libraryUnitIdS');
        parent::__construct();
        if ( $this->primeiroAcessoAoForm() && strlen(MIOLO::_REQUEST('event')) == 0 )
        {
            $this->setFocus('reserveIdS');
        }
    }


    public function createFields()
    {
        $fields[]       = new MTextField('reserveIdS', null, _M('Code',$this->module));
        $personLabel    = new MLabel(_M('Person', $this->module));
        $personLabel    ->setWidth(FIELD_LABEL_SIZE);
        $personId       = new MLookupTextField('personIdS', '', '', FIELD_LOOKUPFIELD_SIZE);
        $personId->setContext($this->module, $this->module, 'activeperson', 'filler', 'personIdS,personIdDescriptionS');
        $personIdDesc   = new MTextField('personIdDescriptionS', $this->loanIdDescription, NULL, FIELD_DESCRIPTION_LOOKUP_SIZE);
        $personIdDesc   ->setReadOnly(true);
        $fields[]       = new MHContainer('pContainer', array($personLabel, $personId, $personIdDesc) );

        $fields[] = new MHiddenField('requestedDateS');
        $lblRequestedDate = new MLabel(_M('Requested date', $this->module) . ':');
        $lblRequestedDate->setWidth(FIELD_LABEL_SIZE);
        $lblRequestedDate_DATE = new MLabel(_M('Date', $this->module) . ':');
        $beginRequestedDateS_DATE    = new MCalendarField('beginRequestedDateS_DATE', $this->beginRequestedDateS_DATE->value, '', FIELD_DATE_SIZE);
        $lblRequestedDate_TIME  = new MLabel(_M('Hour', $this->module) . ':');
        $beginRequestedDateS_TIME     = new MTextField('beginRequestedDateS_TIME', $this->beginRequestedDateS_TIME->value, '', FIELD_TIME_SIZE);
        $endRequestedDateS_DATE    = new MCalendarField('endRequestedDateS_DATE', $this->endRequestedDateS_DATE->value, '', FIELD_DATE_SIZE);
        $endRequestedDateS_TIME     = new MTextField('endRequestedDateS_TIME', $this->endRequestedDateS_TIME->value, '', FIELD_TIME_SIZE);
        $fields[] = new MHContainer('hctRequestedDate', array($lblRequestedDate, $beginRequestedDateS_DATE, $lblRequestedDate_TIME, $beginRequestedDateS_TIME, $endRequestedDateS_DATE, $endRequestedDateS_TIME));
        $validators[] = new MDateDMYValidator('requestedDateS_DATE', _M('Requested date', $this->module) . ' (' . _M('Date', $this->module) . ')');

        $fields[] = new MTextField('itemNumberS', null, _M('Item number', $this->module), FIELD_ID_SIZE);

        $this->busLU->filterOperator = TRUE;
        $this->busLU->labelAllLibrary = TRUE;
        $fields[] = new GnutecaSelection('libraryUnitIdS',   $this->libraryUnitId->value, _M('Library unit', $this->module), $this->busLU->listLibraryUnit(), null, null, null, TRUE);
 //       $fields[] = new MCalendarField('limitDateS', null, _M('Limit date',$this->module) );
        $lblDate             = new MLabel(_M('Limit Date', $this->module) . ':');
        $lblDate->setWidth(FIELD_LABEL_SIZE);
        $beginLimitDateS     = new MCalendarField('beginLimitDateS', $this->beginLimitDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $endLimitDateS       = new MCalendarField('endLimitDateS', $this->endLimitDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $fields[] = new MHContainer('hctDates', array($lblDate, $beginLimitDateS, $endLimitDateS));
        $validators[] = new MDateDMYValidator('beginLimitDateS');

        $fields[] = new GnutecaSelection('reserveStatusIdS', null, _M('Reserve status', $this->module), $this->busRS->listReserveStatus());
        $fields[] = new GnutecaSelection('reserveTypeIdS', null, _M('Reserve type', $this->module), $this->busRT->listReserveType());

        $this->setFields( $fields );
        $this->setValidators( $validators );
    }

    public function getGrid()
    {
        $this->business->isFormSearch = TRUE;
        $data = $this->getData();
        if (($data->beginRequestedDateS_DATE) && ($data->beginRequestedDateS_TIME))
        {
            $data->beginRequestedDateS_DATE = $data->beginRequestedDateS_DATE . ' ' . $data->beginRequestedDateS_TIME . ':00';
        }
        if (($data->endRequestedDateS_DATE) && ($data->endRequestedDateS_TIME))
        {
            $data->endRequestedDateS_DATE = $data->endRequestedDateS_DATE . ' ' . $data->endRequestedDateS_TIME . ':00';
        }

        return parent::getGrid($data);
    }
     
    public function showDetail()
    {
        $frm = $this->MIOLO->getUI()->getForm($this->module, 'FrmReserveStatusHistory');
        parent::_showDetail($frm);
    }


    public function showReserveComposition()
    {
    	$reserveId = MIOLO::_REQUEST('reserveId');

    	//Search data...
    	$this->busReserveComposition->reserveIdS = $reserveId;
    	$data = $this->busReserveComposition->searchReserveComposition(TRUE);

    	if ($data)
    	{
            $cols = array(
               _M('Item number', $this->module),
               _M('Is confirmed', $this->module)
            );
            $tbData = array();
            foreach ($data as $v)
            {
            	$tbData[] = array(
            	   $v->itemNumber,
            	   GnutecaUtils::getYesNo($v->isConfirmed)
            	);
            }
            $tb = new MTableRaw(null, $tbData, $cols);
            $fields[] = $this->getDefaultDetailDiv($tb);
    	}
        else
        {
            $fields[] = new MLabel(_M('No composition for reserve code: @1', $this->module, $reserveId));
        }

    	$this->setTitle( _M('Reserve composition', $this->module) );
    	$this->setFields($fields, FALSE);
    	parent::_showDetail($this);
    }
}
?>
