<?php
require_once (PATH_TABLES . "activity/goods/GoodsDeliveryTable.php") ;
require_once (PATH_TABLES . "activity/goods/GoodsReceivingTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class GoodsDeliveryClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= GoodsDeliveryTable::C_TABLE ;
		$this->fldid 	= GoodsDeliveryTable::C_ID ;
		$this->fldorg 	= GoodsDeliveryTable::C_ORG_ID ;
	}
	function __destruct() {
	}

	function getGoodDeliveryList($date,$cust=0, $grnCode=''){

		$sql = "SELECT dn.*,rn.* FROM " . GoodsDeliveryTable::C_TABLE . " dn ";
		$sql .= "LEFT JOIN " . GoodsReceivingTable::C_TABLE . " rn ON (rn." . GoodsReceivingTable::C_ID . " = dn." . GoodsDeliveryTable::C_GRN_ID .") ";
		$filter ="";
		
		if ($grnCode!='') {
			if($filter != "") $filter .= "AND ";
			else $filter .= "WHERE ";
			
			$filter = "WHERE rn." . GoodsReceivingTable::C_CODE . " like '%" . $grnCode . "%' ";
		}
		if ($cust!=0) {
			if($filter != "") $filter .= "AND ";
			else $filter .= "WHERE ";
			
			$filter = "WHERE rn." . GoodsReceivingTable::C_CUST_ID . " = " . $cust . " ";
		}
        if($date==1) {
           	if($filter != "") $filter .= "AND ";
           	else $filter .= "WHERE ";

        	$filter .= " cast(dn." . GoodsDeliveryTable::C_DATE . " as Date) = cast(convert(date, getdate()) as Date) ";
        } else if($date==2) {
        	if($filter != "") $filter .= "AND ";
           	else $filter .= "WHERE ";

        	$filter .= " cast(dn." . GoodsDeliveryTable::C_DATE . " as Date) = cast(convert(date, getdate()-1) as Date) ";
        } else if($date==3) {
        	if($filter != "") $filter .= "AND ";
           	else $filter .= "WHERE ";

        	$filter .= " dn." . GoodsDeliveryTable::C_DATE . " BETWEEN DATEADD(WEEK,-1,convert(date, getdate())) AND dateadd(millisecond,-1,cast(cast(getdate()+1 AS date) AS datetime2))"	;
        } else if($date==4) {
        	if($filter != "") $filter .= "AND ";
           	else $filter .= "WHERE ";

        	$filter .= " dn." . GoodsDeliveryTable::C_DATE . " BETWEEN DATEADD(MONTH,-2,convert(date, getdate())) AND dateadd(millisecond,-1,cast(cast(getdate()+1 AS date) AS datetime2))";
        }

        $rows = $this->db->getTable($sql . $filter);
        
        return $rows;
    }

}
?>