<?php
require_once(PATH_TABLES . "customer/CustomerBranchTable.php");
require_once(PATH_MODELS . "base/MasterBase.php");

class CustomerBranchClass extends MasterBase
{
    function __construct($db)
    {
        $this->db       = $db;
        $this->tbl      = CustomerBranchTable::C_TABLE;
        $this->fldid2   = CustomerBranchTable::C_ID;
        $this->fldid    = CustomerBranchTable::C_BRANCH_ID;
    }
    function __destruct()
    {
    }

    function getCustomerBranch($id)
    {
        $sql = "SELECT * FROM " . CustomerBranchTable::C_TABLE . " WHERE " . CustomerBranchTable::C_ID . " = " . $id;
        
        $rows = $this->db->getTable($sql);

        if (!is_null($rows) && count($rows) > 0) {
            return $rows;
        }

        return null;
    }

    function deleteCustomerBranchByBranchId($id, $contactId)
    {
        $sql = "DELETE FROM " . CustomerBranchTable::C_TABLE .
            " WHERE " . CustomerBranchTable::C_ID . " = " . $id;

        return $this->db->executeSql($sql);
    }
    function deleteCustomerBranch($id)
    {
        $sql = "DELETE FROM " . CustomerBranchTable::C_TABLE .
            " WHERE " . CustomerBranchTable::C_ID . " = " . $id;

        return $this->db->executeSql($sql);
    }

    function getRecordByContactId($cust_id, $contact_id)
    {
        $sql = "SELECT * ";
        $sql .= "FROM " . CustomerBranchTable::C_TABLE . " ";
        $sql .= "WHERE " . CustomerBranchTable::C_ID . " = " . $cust_id;
        $sql .= " AND " . CustomerBranchTable::C_CONTACT . " = " . $contact_id;

        $rows = $this->db->getTable($sql);

        if (!is_null($rows) && count($rows) > 0) {
            return count($rows);
        } else {
            return 0;
        }
    }

    public function getExistingRecords($cust_id, $code)
    {
        $sql  = "SELECT * FROM " . CustomerBranchTable::C_TABLE .
            " WHERE " . CustomerBranchTable::C_ID . " = '" . $cust_id . "' " .
            " AND " . CustomerBranchTable::C_CODE . " = '" . $code . "' ";

        $rows = $this->db->getTable($sql);

        if (is_null($rows) || count($rows) == 0) {
            return null;
        } else {
            return $rows;
        }
    }
}
