<?php
/**
 *
 * Library Unit search form
 *
 * @author Eduardo Bonfandini [eduardo@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]
 * Sandro Roberto Weisheimer [sandrow@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 FrmLoanSearch extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $busLoanType;
    public $busRenew;
    public $businessLibraryUnit;


    public function __construct()
    {
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->busLoanType         = $this->MIOLO->getBusiness($this->module, 'BusLoanType');
        $this->busRenew            = $this->MIOLO->getBusiness($this->module, 'BusRenew');
        $this->businessLibraryUnit = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        $this->MIOLO->getClass('gnuteca3', 'GnutecaIntegerField');
        $this->setAllFunctions('Loan', 'itemNumber');
        parent::__construct();
        if ( $this->primeiroAcessoAoForm() && strlen(MIOLO::_REQUEST('event')) == 0 )
        {
            $this->setFocus('loanIdS');
        }
    }


    public function createFields()
    {
        $fields[]       = new MTextField('loanIdS', null, _M('Code', $this->module));
        $loanTypeData   = $this->busLoanType->listLoanType();
        $fields[]       = new GnutecaSelection('loanTypeIdS', null, _M('Loan type',$this->module), $loanTypeData);

        $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,linkIdS,linkDescS');

        $personIdDesc   = new MTextField('personIdDescriptionS', $this->loanIdDescription, NULL, FIELD_DESCRIPTION_LOOKUP_SIZE);
        $personIdDesc   ->setReadOnly(true);
        $linkId         = new MHiddenField('linkIdS');
        $linkDesc       = new MTextField('linkDescS');
        $linkDesc       ->setReadOnly(true);
        $fields[]       = new MHContainer('pContainer', array($personLabel, $personId, $personIdDesc, $linkId, $linkDesc) );

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

        $this->businessLibraryUnit->filterOperator = TRUE;
        $this->businessLibraryUnit->labelAllLibrary = TRUE;
        $listLibraryUnit = $this->businessLibraryUnit->listLibraryUnit();
       // $hideSelect      = !$this->businessLibraryUnit->operatorAllLibraries;
        $fields[]        = new GnutecaSelection('libraryUnitIdS',   $this->libraryUnitId->value, _M('Library unit', $this->module), $listLibraryUnit, null, null, null, TRUE);

        $lblDate        = new MLabel(_M('Date', $this->module) . ':');
        $lblDate->setWidth(FIELD_LABEL_SIZE);
        $beginLoanDateS          = new MCalendarField('beginLoanDateS', $this->beginLoanDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $endLoanDateS          = new MCalendarField('endLoanDateS', $this->endLoanDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $fields[] = new MHContainer('hctDates', array($lblDate, $beginLoanDateS, $endLoanDateS));

        $fields[]       = new MTextField('loanOperatorS', null, _M('Operator', $this->module));

        $lblDate        = new MLabel(_M('Return forecast date', $this->module) . ':');
        $lblDate->setWidth(FIELD_LABEL_SIZE);
        $beginReturnForecastDateS          = new MCalendarField('beginReturnForecastDateS', $this->beginReturnDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $endReturnForecastDateS          = new MCalendarField('endReturnForecastDateS', $this->endReturnForecastDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $fields[] = new MHContainer('hctDates', array($lblDate, $beginReturnForecastDateS, $endReturnForecastDateS));

        $lblDate        = new MLabel(_M('Return date', $this->module) . ':');
        $lblDate->setWidth(FIELD_LABEL_SIZE);
        $beginReturnDateS          = new MCalendarField('beginReturnDateS', $this->beginReturnDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $endReturnDateS          = new MCalendarField('endReturnDateS', $this->endReturnDateS->value, null, FIELD_DATE_SIZE, null, 'calendar-win2k-1');
        $fields[] = new MHContainer('hctDates', array($lblDate, $beginReturnDateS, $endReturnDateS));

        $fields[]       = new MTextField('returnOperatorS', null, _M('Return operator', $this->module));
        $fields[]       = new MTextField('renewalAmountS', null, _M('Renewal amount', $this->module));
        $fields[]       = new MTextField('renewalWebAmountS', null, _M('Renewal web amount', $this->module));
        $renewalWebBonusOpt = array('t' =>_M('Yes', $this->module), 'f'=>_M('No', $this->module));
        $fields[]       = new GnutecaSelection('renewalWebBonusS', null, _M('Renewal web bonus', $this->module), $renewalWebBonusOpt);
        $opts = array(
			0 => _M("--Select--", $this->module),
			1 => _M("Pending", $this->module),
			2 => _M("Delayed", $this->module)
        );
        $fields[]       = new GnutecaSelection('status', null, _M('Status', $this->module), $opts, null, null, null, true);
        $this->setFields( $fields );
    }


    public function showRenew()
    {
        $data->loanIdS = MIOLO::_REQUEST('loanId');
        $this->busRenew->setData($data);
        $search = $this->busRenew->searchRenew(TRUE);
        if ($search)
        {
            foreach ($search as $val)
            {
                $timestamp = explode(' ', $val->renewDate);
                $data = substr($timestamp[1], 0, 5);

                $tbData[] = array(
                    $val->renewType,
                    GnutecaDate::toUser($val->renewDate, 'DATE') ." ". $data,
                    GnutecaDate::toUser($val->returnForecastDate, 'DATE'),
                    $val->operator
                );
            }

            $fields[] = new MLabel(_M('Loan code', $this->module) . ': '. $data->loanIdS);
            $tbColumns = array(
                _M('Renew type', $this->module),
                _M('Renew date', $this->module),
                _M('Return forecast date', $this->module),
                _M('Operator', $this->module)
            );
            $tb = new MTableRaw('', $tbData, $tbColumns);
            $tb->zebra = TRUE;
            $fields[] = $this->getDefaultDetailDiv($tb);
        }
        else
        {
            $fields[] = new MLabel (_M('No renew found for this loan', $this->module));
        }
        $this->setTitle(_M('Renew historic', $this->module));
        $this->setFields($fields, false);
        parent::_showDetail($this);
    }
}
?>
