<?php
/**
 * Email devolution 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 17/11/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 FrmDevolution extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $busOperationLoan;
    public $busLibraryUnit;


    public function __construct()
    {
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->busOperationLoan = $this->MIOLO->getBusiness($this->module, 'BusOperationLoan');
        $this->busLibraryUnit   = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        parent::__construct(_M('Communicate devolution', $this->module));
        $this->eventHandler();
    }


    public function createFields()
    {
        $lblInfo = new MLabel(_M('Communicates by email users who have materials, whose date of the return forecast is in X days defined by user', $this->module));
        $lblInfo->setBold(true);
        $fields[] = $lblInfo;

        $this->busLibraryUnit->filterOperator = TRUE;
        $fields[] = new GnutecaSelection('libraryUnitId', null, _M('Library unit', $this->module), $this->busLibraryUnit->listLibraryUnit(), NULL, NULL, NULL, TRUE);
        $fields[] = new MButton('btnOk', _M('Send', $this->module), ':doAction');
        $fields[] = new MDiv('divGrid');

        $this->setFields($fields);
    }


    public function doAction()
    {
        $data = $this->getData();
        $ok     = $this->busOperationLoan->communicateReturn($data->libraryUnitId);
        $goto   = $this->MIOLO->getActionURL($this->module, $this->_action);

        if (($data = $this->busOperationLoan->getGridData()) && (count($data) > 0))
        {
            $grid = $this->MIOLO->getUI()->getGrid($this->module, 'GrdOperationLoan');
            $grid->setData($data);
            $this->setResponse($grid, 'divGrid');
        }
        else if (count($this->busOperationLoan->getMessages()) > 0)
        {
        	$this->injectContent($this->busOperationLoan->getMessagesTableRaw(), true);
        }
        else
        {
            $this->information(_M('There are no returns to be reported', $this->module), $goto);
        }
    }
}
?>
