<?php
require_once (PATH_TABLES . "activity/salesquotation/SalesQuotationItemsTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class SalesQuotationItemsClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= SalesQuotationItemsTable::C_TABLE ;
		$this->fldid 	= SalesQuotationItemsTable::C_ITEMS_ID ;
	}
	function __destruct() {
	}
    function copySalesQuotation($quot_id, $new_quot_id){
        $modifydate = date_create('now')->format('Y-m-d H:i:s') ;
        $modifyby   = $_SESSION[SE_USERID];
        $ws         = $_SESSION[SE_REMOTE_IP];

        $sql = "INSERT INTO ".SalesQuotationItemsTable::C_TABLE." (" .
                SalesQuotationItemsTable::C_ID.",".
                SalesQuotationItemsTable::C_SUBTOTAL.",".
                SalesQuotationItemsTable::C_DISC_TYPE.",".
                SalesQuotationItemsTable::C_DISC_PERCENTAGE.",".
                SalesQuotationItemsTable::C_DISC.",".
                SalesQuotationItemsTable::C_GST_PERCENTAGE.",".
                SalesQuotationItemsTable::C_GST.",".
                SalesQuotationItemsTable::C_GRAND_TOTAL.",".
                SalesQuotationItemsTable::C_QTY_TOTAL.",".
                SalesQuotationItemsTable::C_ORG_ID.",".
                SalesQuotationItemsTable::C_COY_ID.",".
                SalesQuotationItemsTable::C_ACTIVE.",".
                SalesQuotationItemsTable::C_WS_ID.",".
                SalesQuotationItemsTable::C_MODIFY_BY.",".
                SalesQuotationItemsTable::C_MODIFY_DATE.",".
                SalesQuotationItemsTable::C_CREATE_BY.",".
                SalesQuotationItemsTable::C_CREATE_DATE.",".
                SalesQuotationItemsTable::C_REF_ID.
                ") SELECT " .
                $new_quot_id.",".
                SalesQuotationItemsTable::C_SUBTOTAL.",".
                SalesQuotationItemsTable::C_DISC_TYPE.",".
                SalesQuotationItemsTable::C_DISC_PERCENTAGE.",".
                SalesQuotationItemsTable::C_DISC.",".
                SalesQuotationItemsTable::C_GST_PERCENTAGE.",".
                SalesQuotationItemsTable::C_GST.",".
                SalesQuotationItemsTable::C_GRAND_TOTAL.",".
                SalesQuotationItemsTable::C_QTY_TOTAL.",".
                SalesQuotationItemsTable::C_ORG_ID.",".
                SalesQuotationItemsTable::C_COY_ID.",".
                true.",".
                "'".$ws."',".
                $modifyby.",".
                "'".$modifydate."',".
                $modifyby.",".
                "'".$modifydate."',".
                SalesQuotationItemsTable::C_ITEMS_ID.
                " FROM ".SalesQuotationItemsTable::C_TABLE.
                " WHERE " . SalesQuotationItemsTable::C_ID . " = " .$quot_id ;
        // log::write($sql);
		$this->db->executeSql($sql);
		
		$sql = " SELECT TOP 1 * ".
				" FROM " . SalesQuotationItemsTable::C_TABLE . " ".
                " WHERE " . SalesQuotationItemsTable::C_ID . " = " .$new_quot_id.
                " ORDER BY " . SalesQuotationItemsTable::C_ITEMS_ID . " DESC ";
        
		$row = $this->db->getTable($sql);
		
        if(!is_null($row) && count($row) > 0)
            return $row[0][SalesQuotationItemsTable::C_ITEMS_ID];
        else 
            return 0;
    }
	function getRecordByQuotationId($quot_id){
        $sql = "SELECT * FROM " .SalesQuotationItemsTable::C_TABLE.
        " WHERE " . SalesQuotationItemsTable::C_ID . " = " .$quot_id.
        " ORDER BY ". SalesQuotationItemsTable::C_ITEMS_ID ." DESC " ;
        
        $rows = $this->db->getTable($sql);

        if(!is_null($rows) && count($rows) > 0){
            return $rows;
        }
        
        return null;
    }
	function deleteRecordByQuotationId($quot_id){
        $sql = "DELETE FROM " .SalesQuotationItemsTable::C_TABLE.
		" WHERE " . SalesQuotationItemsTable::C_ID . " = " .$quot_id ;
        
        $this->db->executeSql($sql);
    }
}
?>