<?php
require_once(PATH_TABLES . "prospect/ProspectDeliveryTable.php");
require_once(PATH_MODELS . "base/MasterBase.php");

class ProspectDeliveryClass extends MasterBase
{
    function __construct($db)
    {
        $this->db       = $db;
        $this->tbl      = ProspectDeliveryTable::C_TABLE;
        $this->fldid2   = ProspectDeliveryTable::C_ID;
        $this->fldid    = ProspectDeliveryTable::C_DELIVERY_ID;
        $this->fldcode  = ProspectDeliveryTable::C_CODE;
    }
    function __destruct()
    {
    }

    function getProspectDelivery($id, $orderby = '', $api = false)
    {
        if ($api) {
            $sql = "SELECT " . $this->fldid2 . "," . $this->fldcode . " FROM ";
        } else {
            $sql = "SELECT * FROM ";
        }

        $sql .= ProspectDeliveryTable::C_TABLE . " WHERE " . ProspectDeliveryTable::C_ID . " = " . $id;
        if ($orderby != "")
            $sql .= " ORDER BY " . $orderby;

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

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

        return null;
    }

    function deleteProspectDeliveryByDeliveryId($id, $contactId)
    {
        $sql = "DELETE FROM " . ProspectDeliveryTable::C_TABLE .
            " WHERE " . ProspectDeliveryTable::C_ID . " = " . $id .
            " AND " . ProspectDeliveryTable::C_DELIVERY_ID . " = " . $contactId;

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

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

    function getRecordByContactId($cust_id, $contact_id)
    {
        $sql = "SELECT * ";
        $sql .= "FROM " . ProspectDeliveryTable::C_TABLE . " ";
        $sql .= "WHERE " . ProspectDeliveryTable::C_ID . " = " . $cust_id;
        $sql .= " AND " . ProspectDeliveryTable::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, $delivery_code)
    {
        $sql  = "SELECT * FROM " . ProspectDeliveryTable::C_TABLE .
            " WHERE " . ProspectDeliveryTable::C_ID . " = '" . $cust_id . "' " .
            " AND " . ProspectDeliveryTable::C_CODE . " = '" . $delivery_code . "' ";

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

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