<?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 --->
 * 
 * @author Lucas Gerhardt [lucas_gerhardt@solis.com.br]
 *
 * @version $Id$
 *
 * \b Maintainers \n
 * Jamiel Spezia [jamiel@solis.coop.br]
 *
 * @since
 * Class created on 14/04/2014
 *
 * */

class BusinessGnuteca3BusIntegrationClient extends GBusiness
{

    public $colsNoId;
    public $fullColumns;
    public $tables;
    public $MIOLO;
    public $module;
    
    public $integrationClientId;
    public $hostClient;
    public $nameClient;
    public $initialAmountClientMaterials;
    public $initialAmountClientExemplarys;
    public $countMaterials1;
    public $countMaterials2;
    public $countExemplaries1;
    public $countExemplaries2;
    public $emailClient;
    public $periodicity;
    public $serverWorkflowInstanceId;
    public $clientWorkflowInstanceId;
    
    public $status;
    
    public $busWorkflowInstance;
    public $busWorkflowStatus;
    
    public function __construct()
    {
        parent::__construct();
        $this->MIOLO = MIOLO::getInstance();
        $this->tables = 'gtcIntegrationClient';
        $this->colsNoId = 'hostClient,
                           nameClient,
                           emailClient,
                           initialAmountClientMaterials,
                           initialAmountClientExemplarys,
                           periodicity,
                           serverWorkflowInstanceId,
                           clientWorkflowInstanceId';

        $this->fullColumns = 'integrationClientId, ' . $this->colsNoId;
        
    }
    
    public function getIntegrationClient($integrationClientId)
    {
        $data = array($integrationClientId);
        
        $this->clear();
        $this->setColumns('integrationClientId,
                           nameClient,
                           hostClient,
                           initialAmountClientMaterials,
                           initialAmountClientExemplarys,
                           emailClient,
                           periodicity');
        $this->setTables($this->tables);
        $this->setWhere('integrationClientId = ?');
        $sql = $this->select($data);
        $rs = $this->query($sql, TRUE);
        
        //Atribui periodicidade para formatar campo
        $getPeriod = $rs[0]->periodicity;
        
        $rs[0]->periodicity = $this->periodValue($getPeriod);
        
        $this->setData($rs[0]);
        
        return $this;
    }
    
    public function periodValue($period)
    {
        switch($period)
        {
            case 1:
                $resp = "Sem ciclo";
                break;
            case 2:
                $resp = "Anual";
                break;
            case 3:
                $resp = "Mensal";
                break;
            case 4:
                $resp = "Semanal";
                break;
            case 5:
                $resp = "Diário";
                break;
        }
        return $resp;
    }
    
    public function insertIntegrationClient()
    {
        //Instancia objeto data, com os dados do formulário
        $data = array($this->hostClient,
                      $this->nameClient,
                      $this->emailClient,
                      $this->initialAmountClientMaterials,
                      $this->initialAmountClientExemplarys,
                      $this->periodicity,
                      $this->serverWorkflowInstanceId
                      );

        $this->clear();
        
        $this->setColumns('hostClient, 
                           nameClient,
                           emailClient,
                           initialAmountClientMaterials, 
                           initialAmountClientExemplarys,
                           periodicity,
                           serverWorkflowInstanceId
                           ');
        
        $this->setTables($this->tables);  
        
        $sql = $this->insert($data) . ' RETURNING *';
        
        $rs  = $this->query($sql);
        
        return $rs;
    }
    
    
    /*
     * Atualiza o parametro clientWorkflowInstanceId
     * Criado por Tcharles Silva em 04/2014
     */
    public function atualizaClientWorkflowInstanceId($cliInstanceId)
    {
        $data = array(
                        $this->clientWorkflowInstanceId,
                        $this->integrationClientId = $cliInstanceId
                      );
        

        $this->clear();
        
        $this->setColumns('clientWorkflowInstanceId');
        
        $this->setTables('gtcIntegrationClient');
        
        $data[] = $this->integrationClientId;
        $this->setWhere('integrationClientId = ?');

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

        return $rs;
    }


    /**
     * Do a search on the database table handled by the class
     *
     * @param $filters (object): Search filters
     *
     * @return (array): An array containing the search results
     * */
    public function searchIntegrationClient($returnAsObject = false)
    {
        $busWorkflowInstance = $this->MIOLO->getBusiness($this->module, 'BusWorkflowInstance');
        $busWorkflowStatus = $this->MIOLO->getBusiness($this->module, 'BusWorkflowStatus');
        
        $this->clear();
        
        if ($v = $this->integrationClientId)
        {
            $this->setWhere('integrationClientId = ?');
            $data[] = $v;
        }

        if ($v = $this->hostClient)
        {
            $this->setWhere('lower(hostClient) LIKE lower(?)');
            $data[] = $v;
        }

        if ($v = $this->nameClient)
        {
            $this->setWhere('lower(unaccent(nameClient)) LIKE lower(?)');
            $data[] = $v;
        }
        
        if ($v = $this->emailClient)
        {
            $this->setWhere('lower(emailClient) LIKE lower(?)');
            $data[] = $v;
        }
        

        if ($v = $this->countMaterials1)
        {
            $this->setWhere('initialAmountClientMaterials >= ?');
            $data[] = $v;
        }
        
        if ($v = $this->countMaterials2)
        {
            $this->setWhere('initialAmountClientMaterials <= ?');
            $data[] = $v;
        }
        
        if ($v = $this->countExemplaries1)
        {
            $this->setWhere('initialAmountClientExemplarys >= ?');
            $data[] = $v;
        }
        
        if ($v = $this->countExemplaries2)
        {
            $this->setWhere('initialAmountClientExemplarys <= ?');
            $data[] = $v;
        }

        if ($v = $this->periodicity)
        {
            $this->setWhere('periodicity = ?');
            $data[] = $v;
        }

        if ($v = $this->serverWorkflowInstanceId)
        {
            $this->setWhere('serverWorkflowInstanceId = ?');
            $data[] = $v;
        }

        if ($v = $this->clientWorkflowInstanceId)
        {
            $this->setWhere('clientWorkflowInstanceId = ?');
            $data[] = $v;
        }
        
        if ($v = $this->status)
        {
            $data[] = $v;
        }
        
        $this->setColumns($this->fullColumns);
        $this->setTables($this->tables);
        $this->setOrderBy('integrationClientId');
        $sql = $this->select($data);
        
        $rs =  $this->query($sql, $returnAsObject);
        
        foreach($rs as $rr)
        {
            $vartt = $this->periodValue($rr[6]);
            $rr[6] = $vartt;
            $retorno[] = $rr;
        }
        
        foreach ($retorno as $ret)
        {
            $busWorkflowInstance->workflowInstanceId = $ret[8];
            $wInstance = $busWorkflowInstance->searchWorkflowInstance(TRUE);
            
            $busWorkflowStatus->workflowStatusId = $wInstance[0]->workflowStatusId;
            $wStatus = $busWorkflowStatus->searchWorkflowStatus(TRUE);
            
            $ret[] = $wStatus[0]->name;
            
            $retObj[] = $ret;
        }
        
        return $retObj;
    }
    
    /**
     * Update data from a specific record
     *
     * @param $data (object): Data which will replace the old record data
     *
     * @return (boolean): True if succeed, otherwise False
     *
     * */
    public function updateIntegrationClient()
    {
        $columns = 'nameClient,
                    hostClient,
                    initialAmountClientMaterials,
                    initialAmountClientExemplarys,
                    emailClient';
        $this->clear();
        $this->setColumns($columns);
        $this->setTables($this->tables);
        $this->setWhere('integrationClientId = ?');
        
        $sql = $this->update($this->associateData($columns . ', integrationClientId'));
        $rs = $this->execute($sql);
        
        return $rs;
    }
    
    public function getStatusWorkflow($status)
    {
        $this->clear();
        $this->setColumns('*');
        $this->setTables('gtcWorkflowStatus');
        $this->setWhere('workflowId = ?');
        
        $sql = $this->select($status);
        $rs = $this->query($sql);
        
        foreach ($rs as $i => $result)
        {
            $rs[$i] = array($result[0], $result[2]);
        }
        return $rs;
    }
    
    public function getSynchronizations()
    {
        $integrationClientId = MIOLO::_REQUEST('integrationClientId');
        $this->clear();
        $this->setColumns('integrationClientLogId,
                           date,
                           materialsSyncronized,
                           exemplariesSinchronized');
        $this->setTables('gtcIntegrationClientLog');
        $this->setWhere('integrationClientId = ?');
        
        $sql = $this->select($integrationClientId);
        $rs = $this->query($sql);
        
        return $rs;
    }
    
    public function getInitialAmountClientMaterials($integrationClientId)
    {
        $this->clear();
        $this->setColumns('initialAmountClientMaterials');
        $this->setTables('gtcIntegrationClient');
        $this->setWhere('integrationClientId = ?');
        
        $sql = $this->select($integrationClientId);
        $rs = $this->query($sql);

        return $rs[0][0];
    }    
    
    public function getInitialAmountClientExemplarys($integrationClientId)
    {
        $this->clear();
        $this->setColumns('initialAmountClientExemplarys');
        $this->setTables('gtcIntegrationClient');
        $this->setWhere('integrationClientId = ?');
        
        $sql = $this->select($integrationClientId);
        $rs = $this->query($sql);
        
        return $rs[0][0];
    }
}
?>
