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

class ExportCustomerHistoryClass extends MasterBase
{
    function __construct($db)
    {
        $this->db       = $db;
        $this->tbl      = ExportCustomerHistoryTable::C_TABLE;
        $this->fldid    = ExportCustomerHistoryTable::C_ID;
    }
    function __destruct()
    {
    }

    function insertExportCustomerHistory($destination, $log, $status){
        $user_id    = $_SESSION[SE_USERID];
        $date       = date_create('now')->format('Y-m-d H:i:s');

        $res_log = str_replace(". ", ".\n", $log);

        $sql = "INSERT INTO " . $this->tbl . " (" . ExportCustomerHistoryTable::C_USER_ID . ","
                                                . ExportCustomerHistoryTable::C_DESTINATION . ","
                                                . ExportCustomerHistoryTable::C_DATE . ","
                                                . ExportCustomerHistoryTable::C_LOG . ","
                                                . ExportCustomerHistoryTable::C_STATUS . " ) VALUES ( "
                                                . "'" . $user_id . "',"
                                                . "'" . $destination . "',"
                                                . "'" . $date . "',"
                                                . "'" . $res_log . "',"
                                                . "'" . $status . "')";
        $this->db->executeSql($sql);
    }
    function getExportCustomerHistoryList($orderby){
        $sql = "Select a.*, des." .DestinationTable::C_NAME. ", users." . UserTable::C_FULL_NAME . 
                    " From " . $this->tbl . " a  LEFT JOIN " . DestinationTable::C_TABLE . " des ON a." . ExportCustomerHistoryTable::C_DESTINATION . " = des." . DestinationTable::C_ID .
                    " LEFT JOIN users " . UserTable::C_TABLE . " ON a." . ExportCustomerHistoryTable::C_USER_ID . " = users." . UserTable::C_ID .
                    " ORDER BY " . $orderby;
        
        $rows = $this->db->getTable($sql);
        if (is_null($rows) || count($rows) == 0)
            return null;
        else
            return $rows;
    }

    function getExportCustomerHistoryByID($id){
        $sql = "Select a.*, des." .DestinationTable::C_NAME. ", users." . UserTable::C_FULL_NAME . 
                    " From " . $this->tbl . " a  LEFT JOIN " . DestinationTable::C_TABLE . " des ON a." . ExportCustomerHistoryTable::C_DESTINATION . " = des." . DestinationTable::C_ID .
                    " LEFT JOIN users " . UserTable::C_TABLE . " ON a." . ExportCustomerHistoryTable::C_USER_ID . " = users." . UserTable::C_ID .
                    " WHERE " . ExportCustomerHistoryTable::C_ID . " = " . $id .
                    " ORDER BY " . ExportCustomerHistoryTable::C_DATE;
        
        $rows = $this->db->getTable($sql);
        if (is_null($rows) || count($rows) == 0)
            return 0;
        else
            return $rows[0];
    }
}

?>