<?php
/**
 * gtcSupplier business
 *
 * @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 R. Weisheimer [sandrow@solis.coop.br] 
 *
 * @since
 * Class created on 01/12/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 BusinessGnuteca3BusSupplier extends GnutecaBusiness
{
    public $supplierId;
    public $name;
    public $companyName;

    public  $supplierIdS,
            $nameS,
            $companyNameS,
            $cnpjS,
            $locationS,
            $neighborhoodS,
            $beginDateS,
            $endDateS,
            $observationS,
            $bankDepositS,
            $contactS,
            $tab;

    public $table;
    public $busSupplierTypeAndLocation;


    public function __construct()
    {
        $this->table = 'gtcSupplier';
        $pkeys = 'supplierId';
        $cols  = 'name,
                  companyName';

        parent::__construct($this->table, $pkeys, $cols);

        $this->busSupplierTypeAndLocation = $this->MIOLO->getBusiness($this->module, "BusSupplierTypeAndLocation");
    }

    /**
     *
     */
    public function getSupplier($supplierId)
    {
    	return $this->autoGet($supplierId);
    }


    public function getSupplierCompleteData($supplierId)
    {
        $obj->supplier = $this->getSupplier($supplierId);
        $obj->TypeLocation = $this->busSupplierTypeAndLocation->getSupplierTypeAndLocationValueForm($supplierId);

        return $obj;
    }

    /**
     * Procura um fornecedor
     *
     */
    public function searchSupplier()
    {
        $busSupplierTypeAndLocation = $this->MIOLO->getBusiness($this->module, "BusSupplierTypeAndLocation");

        parent::clear();
        parent::setColumns("A.supplierid,
                            A.name,
                            B.type,
                            B.name,
                            B.companyname,
                            B.cnpj,
                            B.location,
                            B.neighborhood,
                            B.city,
                            B.zipCode,
                            B.phone,
                            B.fax,
                            B.alternativePhone,
                            B.email,
                            B.alternativeEmail,
                            B.contact,
                            B.site,
                            B.observation,
                            B.bankDeposit,
                            B.date");

        parent::setTables("$this->table A INNER JOIN $busSupplierTypeAndLocation->tables B USING (supplierid)");
        parent::setOrderBy("supplierId");

        $args = null;
        if($supplierId = MIOLO::_REQUEST('supplierIdS'))
        {
            parent::setWhere("A.supplierid = ?");
            $args[] = $supplierId;
        }
        if($v = $this->nameS)
        {
            parent::setWhere("(lower(A.name) like lower(?) OR lower(B.name) like lower(?))");
            $args[] = "%$v%";
            $args[] = "%$v%";
        }
        if($v = $this->companyNameS)
        {
            parent::setWhere("(lower(A.companyName) like lower(?) OR lower(B.companyName) like lower(?))");
            $args[] = "%$v%";
            $args[] = "%$v%";
        }
        if($v = $this->cnpjS)
        {
            parent::setWhere("lower(B.cnpj) like lower(?)");
            $args[] = "%$v%";
        }
        if($v = $this->locationS)
        {
            parent::setWhere("lower(B.location) like lower(?)");
            $args[] = "%$v%";
        }
        if($v = $this->neighborhoodS)
        {
            parent::setWhere("lower(B.neighborhood) like lower(?)");
            $args[] = "%$v%";
        }
        if($v = $this->contactS)
        {
            parent::setWhere("lower(B.contact) like lower(?)");
            $args[] = "%$v%";
        }
        if($v = $this->observationS)
        {
            parent::setWhere("lower(B.observation) like lower(?)");
            $args[] = "%$v%";
        }
        if($v = $this->bankDepositS)
        {
            parent::setWhere("lower(B.bankDeposit) like lower(?)");
            $args[] = "%$v%";
        }
        if ($v = $this->beginDateS)
        {
            parent::setWhere("date(B.date) >= ?");
            $args[] = "%$v%";
        }
        if ($v = $this->endDateS)
        {
            parent::setWhere("date(B.date) <= ?");
            $args[] = "%$v%";
        }

        $sql = parent::select($args);
        $res = parent::query($sql);

        if(!$res)
        {
            return false;
        }

        $result = array();
        foreach ($res as $i => $v)
        {
            $result[$v[0]][0] = $v[0];
            $result[$v[0]][1] = $v[1];
            $index = array_search($v[2], array(2 => 'c', 3 => 'p', 4 => 'd'));
            $result[$v[0]][$index] = implode("||", $v);
        }

        return $result;
    }


    /**
     * Insert supplier
     *
     * @return boolean
     */
    public function insertSupplier()
    {
        $this->supplierId = $this->getNextSupplierId();
        $ok = parent::autoInsert();
        if ($ok)
        {
            foreach ($this->tab as $type => $fields)
            {
                if(!is_array($fields))
                {
                    continue;
                }
                $this->busSupplierTypeAndLocation->clean();
                foreach ($fields as $fieldsName => $value)
                {
                    $this->busSupplierTypeAndLocation->supplierId   = $this->supplierId;
                    $this->busSupplierTypeAndLocation->type         = strtolower($type);
                    $this->busSupplierTypeAndLocation->$fieldsName  = $value;
                }
                $this->busSupplierTypeAndLocation->insertSupplierTypeAndLocation();
            }
            return true;
        }
        return false;
    }



    /**
     * UPDATE supplier
     *
     * @return boolean
     */
    public function updateSupplier()
    {
        $ok = parent::autoUpdate();
        if ($ok)
        {
            foreach ($this->tab as $type => $fields)
            {
                if(!is_array($fields))
                {
                    continue;
                }
                $this->busSupplierTypeAndLocation->clean();
                foreach ($fields as $fieldsName => $value)
                {
                    $this->busSupplierTypeAndLocation->supplierId   = $this->supplierId;
                    $this->busSupplierTypeAndLocation->type         = strtolower($type);
                    $this->busSupplierTypeAndLocation->$fieldsName  = $value;
                }
                $this->busSupplierTypeAndLocation->updateSupplierTypeAndLocation();
            }
            return true;
        }
        return false;
    }




    public function deleteSupplier($supplierId)
    {
        $data = array($supplierId);

        $this->busSupplierTypeAndLocation->deleteSupplierTypeAndLocation($supplierId);

        $this->clear();
        $this->setTables($this->tables);
        $this->setWhere('supplierId = ?');
        $sql = $this->delete($data);
        $rs  = $this->execute($sql);
        return $rs;        
    }


    /**
     * Enter description here...
     *
     * @return unknown
     */
    public function getCurrentId()
    {
        parent::clear();

        $query = parent::query("SELECT currval('seq_supplierid')");

        if(!$query)
        {
            return 0;
        }

        $this->supplierId = $query[0][0];
        return $query[0][0];
    }


    /**
     * Limpa os atributos da classe
     */
    public function clean()
    {
        $this->supplierId=
        $this->name=
        $this->companyName=
        $this->supplierIdS=
        $this->nameS=
        $this->companyNameS= null;
    }

    /**
    * Return the next value to be inserted.
    * If you want a cross Database function you need treat this in other way.
    *
    */
    public function getNextSupplierId()
    {
        $sql = "SELECT NEXTVAL('seq_reserveid');";
        $rs  = $this->query($sql);
        return $rs[0][0];
    }
}
?>
