<?php
/**
 *
 * Renew 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 28/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
 */


/**
 * Form to manipulate a preference
 **/
class FrmRenew extends GnutecaForm
{
	public $MIOLO;
	public $module;
	public $busLoan;
    public $busRenewType;


    function __construct()
    {
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->busLoan      = $this->MIOLO->getBusiness($this->module, 'BusLoan');
        $this->busRenewType = $this->MIOLO->getBusiness($this->module, 'BusRenewType');
        $this->setAllFunctions('Renew', null, array('renewId'), array('loanId', 'renewTypeId', 'operator'));
        parent::__construct();
      	$this->setFocus('loanId');
    }


    public function createFields()
    {
        if ($this->function != "insert")
        {
            $renewId = new MTextField('renewId', $this->renewId->value, _M('Code', $this->module), FIELD_ID_SIZE);
            $renewId->setReadOnly(TRUE);
            $validators[] = new MIntegerValidator('renewId');
            $validators[] = new MRequiredValidator('renewId');
            $fields[] = $renewId;
            //$this->page->onLoad('document.' . $this->name . '.renewId.focus();');
            $fields[] = new MHiddenField('renewDate', $this->renewDate->value);
        }
        else
        {
            //$this->page->onLoad('document.' . $this->name . '.loanId.focus();');
            $fields[] = new MHiddenField('renewDate', GnutecaDate::now( GnutecaDate::getDbMask('DATE') ));
        }

        $loanIdLabel = new MLabel(_M('Loan code', $this->module) . ':');
        $loanIdLabel->setWidth(FIELD_LABEL_SIZE);
        $loanId = new MLookupTextField('loanId', '', '', FIELD_LOOKUPFIELD_SIZE, NULL, NULL, array('loanIdDescription', 'returnForecastDate'), $this->module, 'Loan');
        $loanId->setContext($this->module, $this->module, 'Loan', 'filler', 'loanId,loanIdDescription,returnForecastDate');
        $loanIdDescription = new MTextField('loanIdDescription', $this->loanIdDescription, NULL, FIELD_DESCRIPTION_LOOKUP_SIZE);
        $loanIdDescription->setReadOnly(true);
        $validators[] = new MIntegerValidator('loanId', _M('Loan code', $this->module), 'required');
        $fields[] = new MHcontainer('loanIdContainer', array($loanIdLabel, $loanId, $loanIdDescription));

        $renewTypeId = new GnutecaSelection('renewTypeId', $this->renewTypeId->value, _M('Renew type', $this->module), $this->busRenewType->listRenewType());
        $fields[] = $renewTypeId;

        $returnForecastDate = new MCalendarField('returnForecastDate', $this->returnForecastDate->value, _M('Return forecast date', $this->module), FIELD_DATE_SIZE, null);
        $fields[] = $returnForecastDate;
        $validators[] = new MDateDMYValidator('returnForecastDate');
        $validators[] = new MRequiredValidator('returnForecastDate');

        $operator = new MTextField('operator', GnutecaOperator::getOperatorId(), _M('Operator', $this->module));
        $operator->setReadOnly(true);
        $validators[] = new MRequiredValidator('operator');
        $fields[] = $operator;

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


    public function loadFields()
    {
        $data = $this->business->getRenew( MIOLO::_REQUEST('renewId') );
        $data->returnForecastDate = GnutecaDate::explodeDate($data->returnForecastDate);
        $data->returnForecastDate = $data->returnForecastDate[0];
        $this->setData($data);
    }


    public function tbBtnSave_click($sender)
    {
        if (($sender->loanId) && (!$this->busLoan->checkAccessLoan($sender->loanId)))
        {
        	$errors[] = _M('You don\'t have access for this loan.', $this->module);
        }
    	parent::tbBtnSave_click($sender, null, $errors);
    }
}
?>
