<?php
/**
 * ExemplaryFutureStatusDefined 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 21/10/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 FrmExemplaryFutureStatusDefined extends GnutecaForm
{
	public $busExemplaryControl;
    public $busExemplaryStatus;
    public $busLibraryUnit;


    public function __construct()
    {
        $MIOLO  = MIOLO::getInstance();
        $module = MIOLO::getCurrentModule();
        $this->busExemplaryControl = $MIOLO->getBusiness($module, 'BusExemplaryControl');
        $this->busExemplaryStatus  = $MIOLO->getBusiness($module, 'BusExemplaryStatus');
        $this->busLibraryUnit      = $MIOLO->getBusiness($module, 'BusLibraryUnit');
        $save_args = array('exemplaryStatusId', 'itemNumber', 'operator');
        $this->setAllFunctions('ExemplaryFutureStatusDefined', null, array('exemplaryFutureStatusDefinedId'), $save_args);
        parent::__construct();
        $this->setFocus('exemplaryStatusId');
    }


    public function createFields()
    {
        if ($this->function != 'insert')
        {
            $exemplaryFutureStatusDefinedId = new MTextField('exemplaryFutureStatusDefinedId', $this->exemplaryFutureStatusDefinedId->value, _M('Code', $this->module), FIELD_ID_SIZE);
            $exemplaryFutureStatusDefinedId->setReadOnly(true);
            $fields[] = $exemplaryFutureStatusDefinedId;
        }
        $statusList            = $this->busExemplaryStatus->listExemplaryStatus(null, null, null, null, true);
        if (GnutecaPerms::checkAccess('gtcMaterialMovementChangeStatusInitial', null, false))
        {
            $levelStatus['level0'] = _M('Estado Anterior', $this->module);
            $statusList            = array_merge($levelStatus, $statusList);
        }            


        $fields[] = new GnutecaSelection('exemplaryStatusId', $this->exemplaryStatusId->value, _M('Exemplary status', $this->module), $statusList, false, false, false,false);

        $itemNumber = new MTextField('itemNumber', $this->itemNumber->value, _M('Item number', $this->module), FIELD_ID_SIZE);
        //$itemNumber->addAttribute('onblur', "miolo.doAjax('checkPermission', '', 'frm__mainForm')");
        $fields[] = $itemNumber;

        $lblApplied = new MLabel(_M('Applied', $this->module) . ':');
        $lblApplied->setWidth(FIELD_LABEL_SIZE);
        $applied = new MRadioButtonGroup('applied', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctApplied', array($lblApplied, $applied));
        $date = new MCalendarField('date', GnutecaDate::now(GnutecaDate::getDbMask('DATE')), _M('Date', $this->module), FIELD_DATE_SIZE, null);
        $validators[] = new MDateDMYValidator('date');
        $fields[] = $date;

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

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

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


    public function loadFields()
    {
    	$this->business->getExemplaryFutureStatusDefined( MIOLO::_REQUEST('exemplaryFutureStatusDefinedId') );
    	$this->business->date = GnutecaDate::explodeDate($this->business->date);
    	$this->business->date = $this->business->date[0];
    	$this->setData($this->business);
    }


    public function tbBtnSave_click($sender)
    {
        $ec = $this->busExemplaryControl->getExemplaryControl($sender->itemNumber);
        $this->busLibraryUnit->filterOperator = TRUE;
        $libraries = $this->busLibraryUnit->listLibraryUnit();
        $hasAccess = FALSE;
        foreach ((array)$libraries as $v)
        {
            $libraryUnitId = $v[0];
            if ($ec->libraryUnitId == $libraryUnitId)
            {
                $hasAccess = TRUE;
            }
        }
        if (!$hasAccess)
        {
            $errors[] = _M('You don\'t have permission for this item number', $this->module);
        }

    	parent::tbBtnSave_click($sender, null, $errors);
    }
}
?>
