<?php
/**
 *
 * Fine 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 01/08/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 FrmFine extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $function;
    public $busFineStatus;
    public $busLoan;


    public function __construct()
    {
        $this->MIOLO         = MIOLO::getInstance();
        $this->module        = MIOLO::getCurrentModule();
        $this->function      = MIOLO::_REQUEST('function');
        $this->busFineStatus = $this->MIOLO->getBusiness($this->module, 'BusFineStatus');
        $this->busLoan       = $this->MIOLO->getBusiness($this->module, 'BusLoan');
        $this->setAllFunctions('Fine', null, 'fineId', array('loanId', 'fineStatusId'));
        parent::__construct();
      	$this->setFocus('loanId');

      	$this->page->addScript('dFilter.js', $this->module);

        $currentDate = new GnutecaDate2();
        $dbDate = $currentDate->getUserDate('date');
        $this->addJsCode("
        checkFineStatus = function() {
            var eq = document.getElementById('fineStatusId').selectedIndex == document.getElementById('fineStatusId_current').value;
            document.getElementById('divFineStatusHistory').style.display = (eq) ? 'none' : 'block';

            var endDate = document.getElementById('endDate').value;
            if(document.getElementById('fineStatusId').selectedIndex > 0 && !endDate.length)
            {
                document.getElementById('endDate').value = '$dbDate';
            }
            else if(document.getElementById('fineStatusId').selectedIndex == 0)
            {
                document.getElementById('endDate').value = '';
            }

        }");
        $this->page->onload("
            document.getElementById('fineStatusId_current').value = document.getElementById('fineStatusId').selectedIndex;
            checkFineStatus();
        ");
    }


    public function createFields()
    {
        if ($this->function != 'insert')
        {
            $fineId = new MTextField('fineId', $this->fineId->value, _M('Code', $this->module), FIELD_ID_SIZE);
            $fineId->setReadOnly(TRUE);
            $validators[] = new MIntegerValidator('fineId');
            $fields[] = $fineId;

            $fields[] = new MHiddenField('fineId', $this->fineId->value);
        }

        $loanIdLabel = new MLabel(_M('Loan code', $this->module) . ':');
        $loanIdLabel->setWidth(FIELD_LABEL_SIZE);
        $loanId = new MLookupTextField('loanId', '', '', FIELD_LOOKUPFIELD_SIZE);
        $loanId->setContext($this->module, $this->module, 'Loan', 'filler', 'loanId,loanIdDescription');
        $loanId->baseModule = $this->module;
        $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));

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

        $lblValue     = new MLabel(_M('Value', $this->module) . ':');
        $lblValue->setWidth(FIELD_LABEL_SIZE);
        $value        = new MTextField('value', $this->value->value, _M('Value', $this->module), FIELD_ID_SIZE);
        $value->addAttribute('onkeypress', "return maskMoney(event, this, 'pt_BR')");
        //$validators[] = new MFloatValidator('value', null, '.', null, 'required');
        //$lblAjuda     = new MLabel(_M('Utilize ponto para separar casas decimais', $this->module) );
        $fields[]     = $value;// new MHContainer('hctValue', array($lblValue, $value, $lblAjuda));

        $fields[] = new MHiddenField('fineStatusId_current');
        $fineStatusId = new GnutecaSelection('fineStatusId', $this->fineStatusId->value, _M('Fine status', $this->module), $this->busFineStatus->listFineStatus());
        $fineStatusId->addAttribute('onchange', "checkFineStatus()");
        $fineStatusId->addAttribute('onkeyup', "checkFineStatus()");
        $fineStatusId->addAttribute('onkeydown', "checkFineStatus()");
        $fields[] = $fineStatusId;
        $validators[] = new MRequiredValidator('fineStatusId');
        $fields[] = new MSeparator();

        //gtcFineStatusHistory
        $operator = new MTextField('operator', GnutecaOperator::getOperatorId(), _M('Operator', $this->module));
        $operator->setReadOnly(true);
        $flds[] = $operator;
        $flds[] = new MSeparator();
        $flds[] = new MMultiLIneField ('observationHistoric', NULL, _M('Observation', $this->module), NULL, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);
        $hctObs = new MBaseGroup('bgrObs', _M('Fine status history observation', $this->module), $flds);
        //$hctObs->setFormMode(MControl::FORM_MODE_SHOW_SIDE);
        $divFSH = new MDiv('divFineStatusHistory', array($hctObs));
        $divFSH->addBoxStyle('display', 'none');
        $fields[] = $divFSH;



        $endDate      = new MCalendarField('endDate', $this->endDate->value, _M('End date', $this->module), FIELD_DATE_SIZE, null);
        $validators[] = new MDateDMYValidator('endDate');
        $fields[]     = $endDate;

        $fields[] = new MMultiLineField('observation', null, _M('Observation', $this->module), NULL, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);

        if ($this->function == 'update')
        {
            $data->fineId = $this->getFormValue('fineId');
            $fields[] = new Separator('');
            $fields[] = $this->MIOLO->getUI()->getForm($this->module, 'FrmFineStatusHistory', $data);
        }

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


    public function loadFields()
    {
        $data = $this->business->getFine( MIOLO::_REQUEST('fineId') );
        $data->value     = GnutecaUtils::floatToMoney($data->value);
        $data->beginDate = GnutecaDate::explodeDate($data->beginDate);
        $data->beginDate = $data->beginDate[0];
        $data->endDate   = GnutecaDate::explodeDate($data->endDate);
        $data->endDate   = $data->endDate[0];
        $this->setData($data);
    }


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

        $data = $this->getData();
        $data->value = GnutecaUtils::moneyToFloat($data->value);
    	parent::tbBtnSave_click($sender, $data, $errors);
    }
}
?>
