<?php
class Table_ReceivingOrderDetail
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ReceivingOrderDetail();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ReceivingOrderDetail();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "rod_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "rod_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'rod_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["receiving_id"]) && $condition["receiving_id"] != ""){
            $select->where("receiving_id = ?",$condition["receiving_id"]);
        }
        if(isset($condition["receiving_code"]) && $condition["receiving_code"] != ""){
            $select->where("receiving_code = ?",$condition["receiving_code"]);
        }
        if(isset($condition["rd_status"]) && $condition["rd_status"] != ""){
            $select->where("rd_status = ?",$condition["rd_status"]);
        }
        if(isset($condition["is_qc"]) && $condition["is_qc"] != ""){
            $select->where("is_qc = ?",$condition["is_qc"]);
        }
        if(isset($condition["is_priority"]) && $condition["is_priority"] != ""){
            $select->where("is_priority = ?",$condition["is_priority"]);
        }
        if(isset($condition["rd_note"]) && $condition["rd_note"] != ""){
            $select->where("rd_note = ?",$condition["rd_note"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
    
    public function getOrderCodes($receiving_code) {
    	$orderCodes = array();
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, 'order_code');
    	$select->where('receiving_code=?', $receiving_code);
    	$list = $this->_table->getAdapter()->fetchAll($select);
    	foreach($list as $row) $orderCodes[] = $row['order_code'];
    	return $orderCodes;
    }
    
    public function getXmlProducts($orderCodes, $asnCode) {
    	if(empty($orderCodes)) return array();
    	$select = $this->_table->getAdapter()->select();
    	$select->from('receiving_order_detail as rod', '');
    	$field1 = array('product_id','sum(op_quantity) as quantity','sum(op_declared_value*op_quantity) as declared_value');
    	$select->joinLeft('order_product as op', 'rod.order_code=op.order_code', $field1);
    	$field2 = array('goods_id', 'hs_goods_name', 'product_title', 'product_title_en', 'currency_code', 'hs_code', 'pu_code', 'product_declared_value');
    	$select->joinLeft('product as p', 'op.product_id=p.product_id', $field2);
    	$field3 = array('hum_quantity_law','hum_quantity_second');
    	$select->joinLeft('hs_uom_map as hum', 'op.product_id=hum.product_id', $field3);
    	$select->joinLeft('hs_uom as hu', 'p.hs_code=hu.hs_code', array('pu_code_law', 'pu_code_second'));
    	$select->where('receiving_code=?', $asnCode);
    	$select->where('rod.order_code IN(?)', $orderCodes);    	
    	$select->group('op.product_id');
    	return $this->_table->getAdapter()->fetchAll($select);
    }
    
    public function sumNetWeight($receiving_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from($this->_table->info('name').' as rod', 'sum(p.product_weight*op.op_quantity) as net_weight');
    	$select->joinLeft('order_product as op', 'rod.order_code=op.order_code', '');
    	$select->joinLeft('product as p', 'op.product_id=p.product_id', '');
    	$select->where('rod.receiving_code=?', $receiving_code);
    	$row = $this->_table->getAdapter()->fetchRow($select);
    	return empty($row) ? 0.00 : $row['net_weight'];
    }
    
    public function sumLawWeight($receiving_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from('receiving_order_detail as rod', "sum(op.op_quantity*if(hu.pu_code_law='035', hum.hum_quantity_law, hum.hum_quantity_second)) as law_weight");
    	$select->joinLeft('order_product as op', 'rod.order_code=op.order_code', '');
    	$select->joinLeft('product as p', 'op.product_id=p.product_id', '');
    	$select->joinLeft('hs_uom_map as hum', 'op.product_id=hum.product_id', '');
    	$select->joinLeft('hs_uom as hu', 'p.hs_code=hu.hs_code', '');
    	//$select->where('hu.pu_code_law=?', '035');
    	$select->where("rod.receiving_code=? and (hu.pu_code_law='035' or hu.pu_code_second='035')", $receiving_code);
    	$row = $this->_table->getAdapter()->fetchRow($select);
    	return empty($row) ? 0.00 : floatval($row['law_weight']);
    }

    public function getTradeCountry($receiving_code,$goods_id){
        $sql = "SELECT country.trade_country FROM receiving_order_detail JOIN order_product ON receiving_order_detail.order_code=order_product.order_code JOIN order_address_book ON order_address_book.order_code=receiving_order_detail.order_code JOIN product ON product.product_id=order_product.product_id JOIN country ON country.country_id=order_address_book.oab_country_id WHERE receiving_order_detail.receiving_code='".$receiving_code."' AND (trade_country='502' or trade_country='303') AND product.goods_id='".$goods_id."' order by receiving_order_detail.order_code asc";
        return $this->_table->getAdapter()->fetchAll($sql);
    }
       
}