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

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

	function getGoodReceivingList($date, $cust, $grnCode=''){

        $sql = "SELECT * FROM " . GoodsReceivingTable::C_TABLE . " ";
        $filter = "WHERE " . GoodsReceivingTable::C_CODE . " like '%" . $grnCode . "%' ";

       	if($cust != 0){
            if($filter != "") $filter .= "AND ";
            else $filter .= "WHERE ";
            
            $filter .= " " . GoodsReceivingTable::C_CUST_ID . " = " . $cust . " ";
        }

        if($date==1) {
           	if($filter != "") $filter .= "AND ";
           	else $filter .= "WHERE ";

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

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

        	$filter .= " " . GoodsReceivingTable::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 .= " " . GoodsReceivingTable::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;
    }

}
?>