<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 13-9-10
 * Time: 下午2:51
 * To change this template use File | Settings | File Templates.
 */
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_ReceivingDetail();
    }

    /**
     * @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
     * @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);
    }

    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);

        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['order_code'])&&$condition['order_code']!=""){
            $select->where(" order_code = ?",$condition['order_code']);
        }

        /*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) {
    	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('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_detail as rd', "sum(rd.rd_receiving_qty*if(hu.pu_code_law='035', hum.hum_quantity_law, hum.hum_quantity_second)) as law_weight");
        $select->joinLeft('product as p', 'rd.product_id=p.product_id', '');
        $select->joinLeft('hs_uom_map as hum', 'rd.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("rd.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);
    }
    
}
