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

class ItemClass extends MasterBase {
	private $dbrms;

	function __construct($db, $dbrms = null) {
		$this->db 		= $db ;
		$this->tbl 		= ItemTable::C_TABLE ;
		$this->fldid 	= ItemTable::C_ID ;
		$this->fldorg 	= ItemTable::C_ORG_ID ;
		$this->dbrms    = $dbrms;
	}
	function __destruct() {
	}
	public function getItemList($desc='',$code='',$brand=''){
        $sql = "SELECT top(1000) * FROM Item  " ;
        $sql .= " Where ".ItemTable::C_ORG_ID." = 0 " ;

        if ($desc!='') {
            $sql .= " AND ".ItemTable::C_DESC." like '%".$desc."%' " ;
        }
        if ($code!='') {
            $sql .= " AND ".ItemTable::C_CODE." like '%".$code."%' " ;
        }
        if ($brand!='') {
            $sql .= " AND ".ItemTable::C_BRAND." like '%".$brand."%' " ;
        }
        $sql .= " order by ".ItemTable::C_DESC."  " ;
		try {
			$rows = $this->db->getTable($sql);
			if (!is_null($rows) && count($rows)> 0){
				return $rows;
			}else{
				return null;
			}
		} catch (Exception $ex){
			Log::write("[ListItem] " . $ex->getMessage());
		}
		
    }
    public function getItemListRms($desc='',$code='',$brand=''){
        $sql = "SELECT top(100) d.Name,i.Description,i.ItemLookupCode FROM Item i " ;
        $sql .= " LEFT OUTER JOIN Department d on (d.ID = i.DepartmentID) " ;

        $filter = "";
        if ($desc!='') {
            if($filter != "") $filter = "AND ";
            else $filter = "WHERE ";

            $sql .= $filter." i.Description like '%".$desc."%' " ;
        }
        if ($code!='') {
            if($filter != "") $filter = "AND ";
            else $filter = "WHERE ";

            $sql .= $filter." i.ItemLookupCode like '%".$code."%' " ;
        }
        if ($brand!='') {
            if($filter != "") $filter = "AND ";
            else $filter = "WHERE ";

            $sql .= $filter." d.Name like '%".$brand."%' " ;
        }
        $sql .= " ORDER BY i.Description " ;

		try {
			$rows = $this->dbrms->getTable($sql);
			if (!is_null($rows) && count($rows)> 0){
				return $rows;
			}else{
				return null;
			}
		} catch (Exception $ex){
			Log::write("[ListItem] " . $ex->getMessage());
		}
		
    }
    public function checkItemCode($code){
        try {
            $sql = "SELECT " .ItemTable::C_ID." ". 
                    " FROM " . ItemTable::C_TABLE . " " .
                    " WHERE " . ItemTable::C_CODE . " = '" . trim($code) . "'";
        
            $rows_item = $this->db->getTable($sql);

            if (is_null($rows_item) || count($rows_item) == 0){
                return null;
            }else{
                return $rows_item[0][ItemTable::C_ID];
            }
        } catch (Exception $ex){
            return null;
        }
        
    }

	public function refreshSupplierFromSun(){
        $sql = "SELECT * " .
                
                " FROM Item  " ;
			
        $rows_item_sun = $this->dbrms->getTable($sql);
	
        if (is_null($rows_item_sun) || count($rows_item_sun) == 0){
            return;
        }
        
        foreach($rows_item_sun as $row_item_sun){
            $sql = "SELECT * " . 
                    " FROM " . ItemTable::C_TABLE . " " .
                    " WHERE " . ItemTable::C_CODE . " = '" . trim($row_item_sun['ItemLookupCode']) . "'";
            
            $rows_item = $this->db->getTable($sql);
            
            $ws 		= $_SESSION[SE_REMOTE_IP];
            $orgid 		= $_SESSION[SE_ORGID] ;
            $modifyby 	= $_SESSION[SE_USERID] ;
            
            if (is_null($rows_item) || count($rows_item) == 0){
                $sql        = "INSERT INTO " . ItemTable::C_TABLE . " " . 
                                "(" . ItemTable::C_CODE . 
                                    ", " . ItemTable::C_REF . 
                                    ", " . ItemTable::C_DESC . 
                                    ", " . ItemTable::C_ORG_ID . 
                                    ", " . ItemTable::C_WS_ID . 
                                    ", " . ItemTable::C_MODIFY_BY . 
                                    ", " . ItemTable::C_CREATE_BY . ") " .
                                " VALUES " . 
                                "('" . $this->prepareStringValues($row_item_sun['ItemLookupCode']) . 
                                        "', '" . $this->prepareStringValues($row_item_sun['SubDescription3']) . 
                                        "', '" . $this->prepareStringValues($row_item_sun['Description']) . 
                                        "', " . $orgid . 
                                        ", '" . $ws . 
                                        "', " . $modifyby . 
                                        ", " . $modifyby . ")";
                
                try {
                    $this->db->executeSql($sql);
                } catch (Exception $ex){
                    Log::write("[SupplierUpdate] " . $ex->getMessage());
                }
            } else {
                $sql        = "UPDATE " . ItemTable::C_TABLE . 
                                " SET " . ItemTable::C_REF . " = '" . $this->prepareStringValues($row_item_sun['SubDescription3']) . "', " . 
                                "     " . ItemTable::C_DESC . " = '" . $this->prepareStringValues($row_item_sun['Description']) . "' " . 
                     
                                " WHERE " . ItemTable::C_CODE . " = '" . $this->prepareStringValues($row_item_sun['ItemLookupCode']) . "'";
                
                try {
                    $this->db->executeSql($sql);
                } catch (Exception $ex){
                    Log::write("[SupplierUpdate] " . $ex->getMessage());
                }
            }
        }
    }
}
?>