<?php
/**
 * Policy 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 04/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 FrmPolicy extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $session;

    public $business;
    public $businessPrivilegeGroup;
    public $businessMaterialGender;
    public $businessLink;
    public $busMaterialGender;
    public $busUserGroup;


    function __construct()
    {
        $this->MIOLO    = MIOLO::getInstance();
        $this->module   = MIOLO::getCurrentModule();
        $this->session  = new MSession('policy');
        $this->setAllFunctions('Policy', 'privilegeGropId', array('privilegeGroupId', 'linkId', 'materialGenderId'), array('privilegeGroupId', 'linkId', 'materialGenderId'));
        $this->businessPrivilegeGroup = $this->MIOLO->getBusiness($this->module, 'BusPrivilegeGroup');
        $this->businessMaterialGender = $this->MIOLO->getBusiness($this->module, 'BusMaterialGender');
        $this->businessLink           = $this->MIOLO->getBusiness($this->module, 'BusBond');
        $this->busMaterialGender      = $this->MIOLO->getBusiness($this->module, 'BusMaterialGender');
        $this->busUserGroup           = $this->MIOLO->getBusiness($this->module, 'BusUserGroup');
        parent::__construct();
    }


    public function createFields()
    {
        if ( $this->function != 'update' )
        {
            $privilegeGroupId = new GnutecaSelection('privilegeGroupId', $this->privilegeGroupId->value, _M('Privilege group',$this->module), $this->businessPrivilegeGroup->listPrivilegeGroup());
            $this->setFocus('privilegeGroupId');
            $fields[] = $privilegeGroupId;
            $validators[] = new MRequiredValidator('privilegeGroupId');
        }
        else
        {
            $privilegeGroupId = new MTextField('privilegeGroupId', $this->privilegeGroupId->value, _M('Privilege group code',$this->module), FIELD_ID_SIZE);
            $privilegeGroupId->setReadOnly(true);
            $fields[] = $privilegeGroupId;
        }

        if ( $this->function != 'update' )
        {
            $linkList = new MMultiSelection('linkList', array(null), _M('Link',$this->module), $this->businessLink->listBond(), null, null, 5);
            $fields[] = $linkList;
            $validators[] = new MRequiredValidator('linkList');
        }
        else
        {
            $linkId = new MTextField('linkId', $this->linkId->value, _M('Link code',$this->module), FIELD_ID_SIZE);
            $linkId->setReadOnly(true);
            $fields[] = $linkId;
        }

        if ( $this->function != 'update' )
        {
            $materialGenderList = new MMultiSelection('materialGenderList', array(null), _M('Material gender',$this->module), $this->businessMaterialGender->listMaterialGender(), null, null, 5);
            $fields[] = $materialGenderList;
            $validators[] = new MRequiredValidator('materialGenderList');
        }
        else
        {
            $this->setFocus('loanDate');
            $materialGenderId = new MTextField('materialGenderId', $this->materialGenderId->value, _M('Material gender code',$this->module), FIELD_ID_SIZE);
            $materialGenderId->setReadOnly(true);
            $fields[] = $materialGenderId;
        }

        $loanDate = new MCalendarField('loanDate', $this->loanDate->value, _M('Date of loan',$this->module), FIELD_DATE_SIZE , null, 'calendar-win2k-1');
        $fields[] = $loanDate;
        $validators[] = new MDateDMYValidator('loanDate');

        $loanDays = new MTextField('loanDays', $this->loanDays->value, _M('Days of loan',$this->module), FIELD_ID_SIZE);
        $fields[] = $loanDays;
        $validators[] = new MIntegerValidator('loanDays','','');

        $loanLimit = new MTextField('loanLimit', $this->loanLimit->value, _M('Limit of loan',$this->module), FIELD_ID_SIZE);
        $fields[] = $loanLimit;
        $validators[] = new MIntegerValidator('loanLimit','','');

        $renewalLimit = new MTextField('renewalLimit', $this->renewalLimit->value, _M('Limit of renewal',$this->module), FIELD_ID_SIZE);
        $fields[] = $renewalLimit;
        $validators[] = new MIntegerValidator('renewalLimit','','');

        $reserveLimit = new MTextField('reserveLimit', $this->reserveLimit->value, _M('Limit of reserve',$this->module), FIELD_ID_SIZE);
        $fields[] = $reserveLimit;
        $validators[] = new MIntegerValidator('reserveLimit','','');

        $fineValue = new MTextField('fineValue', $this->fineValue->value, _M('Value of the fine',$this->module), FIELD_ID_SIZE);
        $fields[] = $fineValue;
        //$validators[] = new MIntegerValidator('fineValue','','');

        $daysOfWaitForReserve = new MTextField('daysOfWaitForReserve', $this->daysOfWaitForReserve->value, _M('Days of wait for reserve',$this->module), FIELD_ID_SIZE);
        $fields[] = $daysOfWaitForReserve;
        $validators[] = new MIntegerValidator('daysOfWaitForReserve','','');

        $reserveLimitInInitialLevel = new MTextField('reserveLimitInInitialLevel', $this->reserveLimitInInitialLevel->value, _M('Reserve limit in initial level', $this->module), FIELD_ID_SIZE);
        $fields[] = $reserveLimitInInitialLevel;
        $validators[] = new MIntegerValidator('reserveLimitInInitialLevel');

        $daysOfWaitForReserveInInitialLevel = new MTextField('daysOfWaitForReserveInInitialLevel', $this->daysOfWaitForReserveInInitialLevel->value, _M('Days of wait for reserve in initial level', $this->module), FIELD_ID_SIZE);
        $fields[] = $daysOfWaitForReserveInInitialLevel;
        $validators[] = new MIntegerValidator('daysOfWaitForReserveInInitialLevel');

        $renewalWebLimit = new MTextField('renewalWebLimit', $this->renewalWebLimit->value, _M('Limit of web renewal',$this->module), FIELD_ID_SIZE);
        $fields[] = $renewalWebLimit;
        $validators[] = new MIntegerValidator('renewalWebLimit','','');

        $lbl = new MLabel(_M('Bonus of web renewal', $this->module) . ':');
        $lbl->setWidth(FIELD_LABEL_SIZE);
        $renewalWebBonus = new MRadioButtonGroup('renewalWebBonus', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctRenewalWebBonus', array($lbl, $renewalWebBonus));
        //$validators[] = new MRequiredValidator('renewalWebBonus');

        $additionalDaysForHolidays = new MTextField('additionalDaysForHolidays', $this->additionalDaysForHolidays->value, _M('Additional of days for holiday',$this->module), FIELD_ID_SIZE);
        $fields[] = $additionalDaysForHolidays;
        $validators[] = new MIntegerValidator('additionalDaysForHolidays','','');

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


    public function tbBtnSave_click($sender=null)
    {
    	$data = $this->getData();
    	$data->loanDate = GnutecaDate::identifyDateMask($data->loanDate, GnutecaDate::getDBMask('DATE'));

    	//Policies
    	if ( MIOLO::_REQUEST('update_repeat') )
    	{
            $data = $this->session->get('data');
            $data->update_repeat = MIOLO::_REQUEST('update_repeat');
            $this->session->destroy();
    	}
    	else
    	{
            $policies = array();
            foreach ( (array)$data->materialGenderList as $materialGenderId )
            {
                foreach ( (array)$data->linkList as $linkId )
                {
                    $policy = $this->business->getPolicy($data->privilegeGroupId, $linkId, $materialGenderId, TRUE);
                    if ( $policy )
                    {
                    	$link = $this->busUserGroup->getUserGroup($policy->linkId)->description;
                    	$materialGender = $this->busMaterialGender->getMaterialGender($policy->materialGenderId)->description;
                        $policies[] = "{$link} - {$materialGender}";
                    }
                }
            }
            if ($policies)
            {
            	$policies = implode(', ', $policies);

            	//Save form data on session
                $this->session->set('data', $data);

                $args['event']         = __FUNCTION__;
                $args['function']      = $this->function;
                $args['update_repeat'] = DB_TRUE;
                $gotoYes = $this->MIOLO->getActionURL($this->module, $this->_action, NULL, $args);
                $gotoNo  = 'gnuteca.closeAction()';
                $this->question(_M("The following policies have already been recorded: <br>@1<br> You want them to be updated?", $this->module, $policies), $gotoYes, $gotoNo);
                return;
            }
    	}

    	parent::tbBtnSave_click($sender, $data);
    }


    public function loadFields()
    {
        try
        {
            $data = $this->business->getPolicy( 
                    MIOLO::_REQUEST('privilegeGroupId'),
                    MIOLO::_REQUEST('linkId'),
                    MIOLO::_REQUEST('materialGenderId')
            );
            $data->loanDate = GnutecaDate::explodeDate($data->loanDate);
            $data->loanDate = $data->loanDate[0];
            $this->setData($data);
        }
        catch( EDatabaseException $e )
        {
            $this->error( $e->getMessage() );
        }
    }

}
?>
