<?php
/**
 * <--- Copyright 2005-2011 de Solis - Cooperativa de Soluções Livres Ltda. e
 * Univates - Centro Universitário.
 * 
 * Este arquivo é parte do programa Gnuteca.
 * 
 * O Gnuteca é um software livre; você pode redistribuí-lo e/ou modificá-lo
 * dentro dos termos da Licença Pública Geral GNU como publicada pela Fundação
 * do Software Livre (FSF); na versão 2 da Licença.
 * 
 * Este programa é distribuído na esperança que possa ser útil, mas SEM
 * NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO
 * ou APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/GPL em
 * português para maiores detalhes.
 * 
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU, sob o título
 * "LICENCA.txt", junto com este programa, se não, acesse o Portal do Software
 * Público Brasileiro no endereço www.softwarepublico.gov.br ou escreva para a
 * Fundação do Software Livre (FSF) Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA --->
 * 
 * This file handles the connection and actions for busFine table
 *
 * @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]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 01/08/2008
 *
 **/
$MIOLO = MIOLO::getInstance();
$MIOLO->uses('/classes/receipt/GnutecaReceipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/Receipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/ReceiptMail.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/ReturnReceipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/LoanReceipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/FineReceipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/receipt/FineReserveReceipt.class.php', 'gnuteca3');
$MIOLO->uses('/classes/GPDF.class.php', 'gnuteca3');

class BusinessGnuteca3BusFine extends GBusiness
{
    public $pkeys;
    public $cols;
    public $table;
    public $fullColumns;
    public $busFineStatusHistory;
    public $busLibraryAssociation;
    public $isFormSearch = FALSE;
    public $busLoan;
    public $busReserve;

    public $fineId;
    public $loanId;
    public $reserveId;
    public $beginDate;
    public $value;
    public $fineStatusId;
    public $endDate;
    public $operator;
    public $observation;
    public $observationHistoric;
    public $date;
    public $personId;

    public $fineIdS;
    public $loanIdS;
    public $reserveIdS;
    public $beginDateS;
    public $beginBeginDateS;
    public $endBeginDateS;
    public $startDateS;
    public $valueS;
    public $fineStatusIdS;
    public $beginEndDateS;
    public $endEndDateS;
    public $endDateS;
    public $itemNumberS;
    public $libraryUnitIdS;
    public $personIdS;
    public $observationS;


    /**
     * Class constructor
     **/
    function __construct()
    {
        parent::__construct();
        $this->MIOLO   = MIOLO::getInstance();
        $this->module  = MIOLO::getCurrentModule();
        $this->busFineStatusHistory = $this->MIOLO->getBusiness($this->module, 'BusFineStatusHistory');
        $this->busLibraryAssociation = $this->MIOLO->getBusiness($this->module, 'BusLibraryAssociation');
        $this->busLoan = $this->MIOLO->getBusiness($this->module, 'BusLoan');
        $this->busReserve = $this->MIOLO->getBusiness($this->module, 'BusReserve');
        $this->table = 'gtcFine';
        $this->pkeys = 'fineId';
        $this->cols = 'loanId,
                       beginDate,
                       value,
                       fineStatusId,
                       endDate,
                       observation,
                       reserveId';
        $this->fullColumns = $this->pkeys . ',' . $this->cols;
    }


    /**
     * Insert a new record
     *
     * @return TRUE if succed, otherwise FALSE
     **/
    public function insertFine($return = false)
    {
        $this->fineId = $this->getNextFineId();
        $data = $this->associateData( $this->fullColumns );
        $this->clear();
        $this->setColumns($this->fullColumns);
        $this->setTables($this->table);
        $sql = $this->insert($data);
        
        if ( $return )
        {
            $rs  = $this->query($sql . ' RETURNING fineId, loanId, beginDate, value, fineStatusId, endDate, observation, reserveId', true);
            $this->setData($rs[0]);
        }
        else
        {
            $rs  = $this->execute($sql);
        }

        //Pois o campo observationHistoric do formulário é para ser armazenado no campo observation do histórico
        $this->observation = $this->observationHistoric;
        $this->insertFineStatusHistory();
        
        return $rs;
    }


    /**
     * Update data from a specific record
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     **/
    public function updateFine()
    {
        $data = $this->associateData( $this->cols . ',' . $this->pkeys );
        $this->clear();

        //Na Circulação de material não deve gravar a observação no gtcFine. Mas, sim no gtcStatusHistory
        if (MIOLO::_REQUEST('action') == 'main:materialMovement')
        {
        	$this->observationHistoric = $data[5];
        	$this->setColumns('loanId,beginDate,value,fineStatusId,endDate');
            $this->setWhere('fineId = '. $this->fineId);
        }
        //No Administrativo deve gravar todos os campos
        else
        {
            $this->setColumns($this->cols);
            $this->setWhere('fineId = ?');
        }

        $this->setTables($this->table);
        $sql = $this->update($data);
        $rs  = $this->execute($sql);

        //Pois o campo observationHistoric do formulário é para ser armazenado no campo observation do histórico
        //Na Circulação de material, como só tem o campo observação, deve armazenar este no histórico
        $this->observation = $this->observationHistoric;
        $this->insertFineStatusHistory();

        return $rs;
    }


    /**
     * Delete a record
     *
     * @param $fineId (integer)
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     *
     **/
    public function deleteFine($fineId)
    {
        //Delete all registers with this fineId code on gtcFineStatusHistory
        $delFSH = $this->busFineStatusHistory->deleteByFine($fineId);

        $this->clear();
        $this->setTables($this->table);
        $this->setWhere('fineId = ?');
        $sql = $this->delete(array($fineId));
        $rs  = $this->execute($sql);
        return $rs;
    }
    
    public function insereMultaParaReserva($reserve, $valor, $composition)
    {
        $gnutecaReceipt = new GnutecaReceipt();
        
        GnutecaReceipt::clearReceitpsText();
        ReceiptMail::clearStoredEmails();
        
        $busReserve = $this->MIOLO->getBusiness($this->module, 'BusReserve');
        $busReserveComposition = $this->MIOLO->getBusiness($this->module, 'BusReserveComposition');
        $busFineStatus = $this->MIOLO->getBusiness('gnuteca3', 'BusFineStatus');
        
        // Adiciona uma multa.
        $this->value         = $valor;
        $this->loanId        = null;
        $this->reserveId     = $reserve->reserveId;
        $this->beginDate     = GDate::now()->getDate(GDate::MASK_DATE_DB);
        $this->fineStatusId  = DEFAULT_VALUE_FINE_INITIAL_STATUS;

        $ok = $this->insertFine(true);
        
        if ( $ok )
        {
            $fineStatus = $busFineStatus->getFineStatus($this->fineStatusId);
            $operation = $fineStatus->description . ' ';
            
            // Recibo
            $fineReserveReceipt = $gnutecaReceipt->getItem(get_class(new FineReserveReceipt), $reserve->personId);
            
            if ( !$fineReserveReceipt )
            {
                $fineReserveReceipt = new FineReserveReceipt();
                $fineReserveReceipt->personId = $reserve->personId;
                $fineReserveReceipt->libraryUnitId_ = $reserve->libraryUnitId;

                $fineReserveReceipt->setIsPostable(true);
                $fineReserveReceipt->setIsPrintable(true);
            }
            
            //objeto de recibos
            $fineReceiptWork = new FineReceiptWork();
            $fineReceiptWork->setFineId($this->fineId);
            $fineReceiptWork->setValue($this->value);
            $fineReceiptWork->setItemNumber($composition->itemNumber);
            $fineReceiptWork->setOperation($operation);
            $fineReceiptWork->generate();

            // Adiciona item
            $fineReserveReceipt->addItem($fineReceiptWork);
            
            //Gera e envia o e-mail com recibo
            $fineReserveReceipt->generate();         
        }
    }

    /**
     * Return a specific record from the database
     *
     * @return (Object): Return an object of the type handled by the class
     **/
    public function getFine($fineId, $returnThisObject = true)
    {
        $data[] = $fineId;

        $this->clear();
        $this->setColumns($this->fullColumns);
        $this->setTables($this->table);
        $this->setWhere('fineId = ?');
        $sql = $this->select($data);
        $rs  = $this->query($sql, TRUE);

        if ($returnThisObject)
        {
            $this->setData($rs[0]);
            return $this;
        }
        
        return $rs[0];
    }

    /**
     * Return a specific record from gtcFine and gtcFineHistory
     *
     * @return (Object): Return an object of the type handled by the class
     **/
    public function getFineAndHistory($fineId)
    {
        $data[] = $fineId;

        $this->clear();
        $this->setColumns('A.fineId,A.loanId,A.beginDate,A.value,A.fineStatusId,A.endDate,A.observation,B.observation,B.date, A.reserveId');
        $this->setTables('gtcFine A
                         LEFT JOIN gtcFineStatusHistory B
                         ON A.fineId = B.fineId');
        $this->setWhere('A.fineId = ?');
        $this->setOrderBy('B.date desc');
        $sql = $this->select($data);
        $rs  = $this->query($sql, TRUE);

        return $rs[0];
    }


    /**
     *
     **/
    public function getLoanId($fineId)
    {
        $data[] = $fineId;

        $this->clear();
        $this->setColumns('loanid');
        $this->setTables($this->table);
        $this->setWhere('fineId = ?');
        $sql = $this->select($data);
        $rs  = $this->query($sql, TRUE);

        return ($rs && isset($rs[0]->loanid)) ? $rs[0]->loanid : false;
    }

    /**
     *
     **/
    public function getReserveId($fineId)
    {
        $data[] = $fineId;

        $this->clear();
        $this->setColumns('reserveId');
        $this->setTables($this->table);
        $this->setWhere('fineId = ?');
        $sql = $this->select($data);
        
        $rs  = $this->query($sql, TRUE);

        return ($rs && isset($rs[0]->reserveId)) ? $rs[0]->reserveId : false;
    }


    /**
     * Do a search on the database table handled by the class
     *
     * @return (Array): An array containing the search results
     **/
    public function searchFine($toObject = FALSE , $orderBy='A.fineId DESC', $extraInfo= false )
    {
        $this->clear();

        if ($this->fineIdS)
        {
            $this->setWhere('A.fineId = ?');
            $data[] = $this->fineIdS;
        }

        if ($this->loanIdS)
        {
            $this->setWhere('A.loanId = ?');
            $data[] = $this->loanIdS;
        }

        if ($this->reserveIdS)
        {
            $this->setWhere('A.reserveId = ?');
            $data[] = $this->reserveIdS;
        }

        if ($this->beginBeginDateS)
        {
            $this->setWhere('date(A.beginDate) >= ?');
            $data[] = $this->beginBeginDateS;
        }
        if ($this->endBeginDateS)
        {
            $this->setWhere('date(A.beginDate) <= ?');
            $data[] = $this->endBeginDateS;
        }

        if ($this->startDateS)
        {
            $this->setWhere('A.beginDate >= ?');
            $data[] = $this->startDateS;
        }

        if ($this->valueS)
        {
            $this->setWhere('A.value = ?');
            //converte a , para ponto para evitar erro na base de dados
            $data[] = str_replace(',','.' ,$this->valueS);
        }

        if ($this->fineStatusIdS)
        {
            $this->setWhere('A.fineStatusId = ?');
            $data[] = $this->fineStatusIdS;
        }

        if ($this->beginDateS)
        {
            $this->setWhere('date(A.beginDate) = ?');
            $data[] = $this->beginDateS;
        }
        if ($this->beginEndDateS)
        {
            $this->setWhere('date(A.endDate) >= ?');
            $data[] = $this->beginEndDateS;
        }
        if ($this->endDateS)
        {
            $this->setWhere('date(A.endDate) = ?');
            $data[] = $this->endDateS;
        }
        if ($this->endEndDateS)
        {
            $this->setWhere('date(A.endDate) <= ?');
            $data[] = $this->endEndDateS;
        }

        if ($this->itemNumberS)
        {
        	$this->setWhere('L.itemNumber = ?');
        	$data[] = $this->itemNumberS;
        }

        if ($this->itemNumber)
        {
            $this->setWhere('L.itemNumber = ?');
            $data[] = $this->itemNumber;
        }

        if ($this->libraryUnitIdS)
        {
            $this->setWhere('L.libraryUnitId in (' . $this->libraryUnitIdS . ')' );
        }

        if ($this->personIdS)
        {
            $this->setWhere('BP.personId = ?');
            $data[] = $this->personIdS;
        }
        
        if($this->personId)
        {
            $this->setWhere('BP.personId = ?');
            $data[] = $this->personId;
        }

        if ($this->isFormSearch) //Busca sendo feita a partir do FrmFineSearch
        {
            $columns = "A.fineId,
                        A.loanId,
                        A.reserveId,
                        COALESCE(L.itemNumber, (SELECT itemNumber 
                                                  FROM gtcReserveComposition 
                                                 WHERE reserveId = R.reserveId 
                                                   AND isConfirmed = 't')),
                        NULL AS data,
                        A.beginDate,
                        A.value,
                        B.description,
                        A.endDate,
                        BP.personId,
                        BP.name,
                        L.LibraryUnitId,
                        LU.libraryName,
                        A.fineStatusId";
        }
        else
        {
	        $columns = 'A.fineId,
	                    A.loanId,
                            A.reserveId,
                            NULL AS tomo,
                            NULL AS data,
	                    A.beginDate,
	                    A.value,
	                    B.description,
	                    A.endDate,
                            L.LibraryUnitId,
                            LU.libraryName,
                            A.fineStatusId,
                            BP.personId';
        }

        $tables = '    gtcFine A
             LEFT JOIN gtcFineStatus B
                    ON (A.fineStatusId = B.fineStatusId)
             LEFT JOIN gtcLoan L
                    ON (A.loanId = L.loanId)
             LEFT JOIN gtcReserve R
                    ON (A.reserveId = R.reserveId)
        LEFT JOIN ONLY basPerson BP
                    ON (L.personid = BP.personid OR R.personId = BP.personId)
             LEFT JOIN gtcLibraryUnit LU
                    ON (L.libraryUnitId = LU.libraryUnitId OR R.libraryUnitId = LU.libraryUnitId)';

        // Se for uma pesquisa por pessoa, faz um join com a tabela loan  e basperson
        /*if($this->personId)
        {
            $this->setWhere('P.personId = ?');
            $data[] = $this->personId;

            $tables.= 'INNER JOIN ONLY basPerson P ON (L.personId = P.personId) OR (R.personId = P.personId)';

            $columns.= ", P.personId";
        }*/

        $this->setTables($tables);
        $this->setColumns($columns);
        $this->setOrderBy($orderBy);
        $sql = $this->select($data);
        $rs  = $this->query($sql, $toObject);

        if ($toObject && $extraInfo)
        {
            if (is_array($rs))
            {
        	foreach( $rs as $line => $info)
        	{
                    if ( strlen($info->loanId) > 0 )
                    {
                        $rs[$line]->loan = $this->busLoan->getLoan($info->loanId , true, true);
                    }
                    else if ( strlen($info->reserveId) > 0 )
                    {
                        $rs[$line]->reserve = $this->busReserve->getReserve($info->reserveId, false, true);
                    }
        	}
            }
        }
        return $rs;
    }


    /**
     * List all records from the table handled by the class
     *
     * @param None
     *
     * @return (Array): Return an array with the entire table
     *
     **/
    public function listFine()
    {
        $this->clear();
        $this->setColumns($this->fullColumns);
        $this->setTables($this->table);
        $sql = $this->select();
        $rs  = $this->query($sql);
        return $rs;
    }


    public function getNextFineId()
    {
        $query = $this->query("SELECT NEXTVAL('seq_fineId')");
        return $query[0][0];
    }


    /**
     * Este metodo registra o historico de estado de uma determinada multa
     *
     * @param string $operator
     */
    public function insertFineStatusHistory($operator = null)
    {
    	$operator = is_null($operator) ? GOperator::getOperatorId() : $operator;

        //Insert data on gtcFineStatusHistory
        $this->busFineStatusHistory->fineId = $this->fineId;
        $this->busFineStatusHistory->fineStatusId = $this->fineStatusId;
        $this->busFineStatusHistory->date = GDate::now()->getDate(GDate::MASK_TIMESTAMP_DB);
        $this->busFineStatusHistory->operator = $operator;
        $this->busFineStatusHistory->observation = $this->observation;
        $this->busFineStatusHistory->insertFineStatusHistory();
    }



    /**
     *
     *
     * @param unknown_type $libraryUnitId
     * @param unknown_type $personId
     * @return unknown
     */
    public function getFineOpenOfAssociation($libraryUnitId, $personId)
    {
        $libraries = $this->busLibraryAssociation->getLibrariesAssociationOf($libraryUnitId);

        if ($libraries)
        {
            $this->clear();
            $this->setColumns("gtcloan.libraryUnitId,
                               gtcloan.linkId,
                               (CASE WHEN gtcloan.loanId IS NOT NULL 
                                    THEN 
                                        gtcloan.loanId 
                                    ELSE 
                                        gtcreserve.reserveId 
                                END) as \"loanId\",
                               gtcfine.fineId,
                               gtcfine.value,
                               gtcfine.beginDate,
                               gtcfine.endDate,
                               (CASE WHEN gtcloan.loanId IS NOT NULL
                                    THEN 
                                        'loan'
                                    ELSE
                                        'reserve' 
                                END) as tipo");
            $this->setTables('gtcfine
                 LEFT JOIN    gtcloan
                        ON    (gtcfine.loanId = gtcloan.loanId)
                 LEFT JOIN    gtcreserve
                        ON    (gtcfine.reserveId = gtcreserve.reserveId)');
            
            if ( VERIFY_FINES_AND_PENALTIES_PER_UNIT == DB_TRUE )
            {
                $this->setWhere('(( gtcloan.libraryUnitId IN ('.implode(',', $libraries).')
                                    AND gtcloan.personId = ? )
                               OR ( gtcreserve.libraryUnitId IN ('.implode(',', $libraries).')
                                    AND gtcreserve.personId = ? ))
                              AND gtcfine.fineStatusId = ?');
            }
            else
            {
                $this->setWhere('(gtcloan.personId = ? OR gtcreserve.personId = ? ) AND gtcfine.fineStatusId = ?');
            }
            
            $args[] = $personId;
            $args[] = $personId;
            $args[] = ID_FINESTATUS_OPEN;
            $sql   = $this->select($args);
            $query = $this->query($sql, true);
            return $query;
        }
        else
        {
            return array();
        }
    }

    /**
     * Retorna as multas abertas de uma determinada biblioteca
     *
     * @param integer $libraryUnitId
     * @return array
     */
    public function getFinesOpen($libraryUnitId = null, $personId = null)
    {
        return $this->getFines($libraryUnitId, $personId, ID_FINESTATUS_OPEN);
    }


    /**
     * retorna todas multas com forma do pagamento via boleto.
     *
     * @param int $libraryUnitId
     * @param  array int $personId
     * @return array object
     */
    public function getFinePayRoll($libraryUnitId = null, $personId = null, $period = null, $offSet = null, $limit = null)
    {
        return $this->getFines($libraryUnitId, $personId, ID_FINESTATUS_BILLET, $period, $offSet, $limit);
    }



    /**
     * Retorna multas
     *
     * @param unknown_type $libraryUnitId
     * @param unknown_type $personId
     * @param unknown_type $fineStatus
     * @return unknown
     */
    public function getFines($libraryUnitId, $personId = null, $fineStatus = null, $period = null, $offSet = null, $limit = null)
    {
        $this->clear();

        $this->setColumns
        (
            'A.libraryUnitId,
             A.personId,
             B.fineId,
             B.value,
             A.itemNumber,
             A.loanDate,
             A.returnForecastDate,
             A.returnDate,
             B.fineStatusId,
             C.description as fineStatusDescription,
             D.libraryName'
        );

        $this->setTables('gtcloan A INNER JOIN gtcfine          B USING (loanId)
                                    INNER JOIN gtcFineStatus    C USING (fineStatusId)
                                    INNER JOIN gtcLibraryUnit   D USING (libraryUnitId) ');

        if(!is_null($libraryUnitId))
        {
            $libraryUnitId = !is_array($libraryUnitId) ? array($libraryUnitId) : $libraryUnitId;
            $this->setWhere("A.libraryUnitId IN ('". implode("', '", $libraryUnitId) ."')");
        }
        if(!is_null($personId))
        {
            $personId = !is_array($personId) ? array($personId) : $personId;
            $this->setWhere("A.personId IN ('". implode("', '", $personId) ."')");
        }
        if(!is_null($fineStatus))
        {
            $this->setWhere("B.fineStatusId = ?");
            $args[] = $fineStatus;
        }
        if(!is_null($period))
        {
            if(is_array($period))
            {
                $this->setWhere("A.returnDate BETWEEN '". GDate::construct($period[0])->getDate(GDate::MASK_TIMESTAMP_DB) ."' AND '". GDate::construct($period[1])->getDate(GDate::MASK_TIMESTAMP_DB) ."'");
            }
            else
            {
                $this->setWhere("A.returnDate::date = '". GDate::construct($period)->getDate(GDate::MASK_DATE_DB) ."' ");
            }
        }

        $this->setOrderBy("A.libraryUnitId, A.personId, A.returnDate");
        $sql   = $this->select($args);

        if(!is_null($offSet))
        {
            $sql.= " OFFSET $offSet ";
        }

        if(!is_null($limit))
        {
            $sql.= " LIMIT $limit ";
        }

        $query = parent::query($sql, true);
        return $query;
    }



    /**
     * Enter description here...
     *
     * @return unknown
     */
    public function getData()
    {
        $this->value = str_replace(',', '.', $this->value);
        return parent::getData();
    }


    /**
     * Seta uma determinada multa como paga
     *
     * @param int $fineId
     * @return boolean
     */
    public function setFinePay($fineId, $saveHistory=NULL)
    {
        $this->fineId       = $fineId;
        $this->fineStatusId = ID_FINESTATUS_PAYED;
        $this->endDate      = GDate::now()->getDate(GDate::MASK_TIMESTAMP_DB);

        return $this->changeStatus($saveHistory);
    }

    /**
     * Seta uma determinada multa como aberta
     *
     * @param int $fineId
     * @return boolean
     */
    public function setFineOpen($fineId, $saveHistory=NULL)
    {
        $this->fineId       = $fineId;
        $this->fineStatusId = ID_FINESTATUS_OPEN;

        return $this->changeStatus($saveHistory);
    }



    /**
     * Seta uma determinada multa como paga por boleto
     *
     * @param int $fineId
     * @return boolean
     */
    public function setFinePayRoll($fineId, $saveHistory=NULL)
    {
        $this->fineId       = $fineId;
        $this->fineStatusId = ID_FINESTATUS_BILLET;
        $this->endDate      = GDate::now()->getDate(GDate::MASK_TIMESTAMP_DB);

        return $this->changeStatus($saveHistory);
    }


    /**
     * Seta uma determinada multa como abonada
     *
     * @param int $fineId
     * @return boolean
     */
    public function setFineBonus($fineId, $obs = null, $saveHistory=NULL)
    {

        $this->fineId       = $fineId;
        $this->fineStatusId = ID_FINESTATUS_EXCUSED;
        $this->endDate      = GDate::now()->getDate(GDate::MASK_TIMESTAMP_DB);
        $this->observation  = $obs;

        return $this->changeStatus($saveHistory);
    }


    /**
     * Update data from a specific record
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     **/
    public function changeStatus($saveHistory=TRUE)
    {
        $this->clear();
        $this->setColumns("finestatusid, enddate");
        $this->setTables($this->table);
        $this->setWhere('fineId = ?');
        $sql = $this->update(array($this->fineStatusId, $this->endDate, $this->fineId));

        $rs  = $this->execute($sql);

        if ($saveHistory)
        {
            $operator = is_string($saveHistory) ? $saveHistory : null;
            $this->insertFineStatusHistory($operator);
        }

        return $rs;
    }


    /**
     * limpa todos atributos da classe
     *
     */
    public function clean()
    {
        $this->fineId=
        $this->loanId=
        $this->beginDate=
        $this->value=
        $this->fineStatusId=
        $this->endDate=
        $this->operator=
        $this->observation=
        $this->observationHistoric=
        $this->date=
        $this->personId=
        $this->fineIdS=
        $this->loanIdS=
        $this->beginDateS=
        $this->beginBeginDateS=
        $this->endBeginDateS=
        $this->startDateS=
        $this->valueS=
        $this->fineStatusIdS=
        $this->beginEndDateS=
        $this->endEndDateS=
        $this->endDateS=
        $this->itemNumberS=
        $this->libraryUnitIdS=
        $this->personIdS=
        $this->observationS= 
        $this->reserveId= 
        $this->reserveIdS = null;
    }
}
?>
