<?php
class Table_OrderProduct
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_OrderProduct();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_OrderProduct();
    }

    /**
     * @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 = "op_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 = "op_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * 获取最大行
     * @return [type] [description]
     */
    public function getMaxCountPorduct()
    {
        $sql = "SELECT order_code , count(*) as cou , order_id FROM order_product WHERE 1=1 GROUP BY order_code ORDER BY cou DESC LIMIT 1";
        $result = $this->_table->getAdapter()->fetchRow($sql);
        return intval($result['cou']);
    }
    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'op_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 getInvoiceSkuData($order_code)
    {
        $select = $this->_table->getAdapter()->select();
        $select->from('order_product as op', array('op_quantity','op_purpose_declared_value as declared_value','product_title_en'));
        $select->joinLeft('product as p', 'op.product_id=p.product_id', array('product_weight'));
        $select->joinLeft('ship_order_detail as sod', 'op.op_id=sod.op_id', array('op_description'));
        $select->where('op.order_code=?', $order_code);
        $select->group('op.op_id');
        return $this->_table->getAdapter()->fetchAll($select);
    }
    
    public function sumPurposeDeclaredValue($order_code) {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, array('sum(op_purpose_declared_value*op_quantity) as purpose_declared_value'));
    	$select->where('order_code=?', $order_code);
    	return $this->_table->getAdapter()->fetchOne($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["order_id"]) && $condition["order_id"] != ""){
            $select->where("order_id = ?",$condition["order_id"]);
        }
        if(isset($condition["order_code"]) && $condition["order_code"] != ""){
            $select->where("order_code = ?",$condition["order_code"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["product_barcode"]) && $condition["product_barcode"] != ""){
            $select->where("product_barcode = ?",$condition["product_barcode"]);
        }
        if(isset($condition["op_quantity"]) && $condition["op_quantity"] != ""){
            $select->where("op_quantity = ?",$condition["op_quantity"]);
        }
        if(isset($condition["op_category"]) && $condition["op_category"] != ""){
            $select->where("op_category = ?",$condition["op_category"]);
        }
        if(isset($condition["op_ref_tnx"]) && $condition["op_ref_tnx"] != ""){
            $select->where("op_ref_tnx = ?",$condition["op_ref_tnx"]);
        }
        if(isset($condition["op_ref_item_id"]) && $condition["op_ref_item_id"] != ""){
            $select->where("op_ref_item_id = ?",$condition["op_ref_item_id"]);
        }
        if(isset($condition["op_ref_buyer_id"]) && $condition["op_ref_buyer_id"] != ""){
            $select->where("op_ref_buyer_id = ?",$condition["op_ref_buyer_id"]);
        }
        /*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);
        }
    }
    
    /**
     * 出库单报文表体产品行
     * @author solar
     * @param array $aOrderCode
     * @return array
     */
    public function getOutBillProducts($aOrderCode) {
    	if(empty($aOrderCode)) return array();
    	$select = $this->_table->getAdapter()->select();
    	$field1 = array('product_id','sum(op_quantity) as quantity','sum(op_declared_value*op_quantity) as declared_value');
    	$select->from('order_product as op', $field1);
    	$field2 = array ('customer_id','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_id','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('op.order_code IN (?)', $aOrderCode);
    	$select->group('op.product_id');
    	return $this->_table->getAdapter()->fetchAll($select);
    }

    public function countOrderHscodeNum($aOrderCode) {
        $select = $this->_table->getAdapter()->select();
        $select->from($this->_table->info('name').' as op', 'count(DISTINCT p.hs_code)');
        $select->joinInner('product as p', 'p.product_id=op.product_id','');
        $select->where('op.order_code=?', $aOrderCode);
        $count = $this->_table->getAdapter()->fetchOne($select);
        return intval($count);
    }

    public function getOrderProducts($orderCode) {
        if(empty($orderCode)) return array();
        $select = $this->_table->getAdapter()->select();
        $select->from('order_product as op', array('product_id','op_quantity','product_barcode','op_add_time','op_purpose_declared_value','op_declared_value','product_title_en','op_id'));
        $field = array('product_sku','product_title', 'currency_code','product_weight', 'hs_code', 'pu_code', 'product_declared_value');
        $select->joinLeft('product as p', 'op.product_id=p.product_id', $field);
        $select->where('op.order_code=?', $orderCode);
        return $this->_table->getAdapter()->fetchAll($select);
    }

    public function getGoodsId($order_code){
        $select = "SELECT DISTINCT p.goods_id FROM order_product op JOIN product p on op.product_id=p.product_id WHERE op.order_code='".$order_code."'";
        return $this->_table->getAdapter()->fetchCol($select);
    }

    public function getJoinProductByCondtion($condition){
        $sql = "select op.*,p.hs_code,p.hs_goods_name,p.currency_code,p.product_declared_value,p.product_weight,p.goods_id from order_product op join receiving_order_detail rod on  rod.order_code=op.order_code join product p on p.product_id=op.product_id where 1";
        if(isset($condition['goods_id'])&&$condition['goods_id']!=""){
            $sql.= " and p.goods_id= '".$condition['goods_id']."'";
        }
        if(isset($condition['receiving_code'])&&$condition['receiving_code']!=""){
            $sql.= " and rod.receiving_code = '".$condition['receiving_code']."'";
        }
        return $this->_table->getAdapter()->fetchAll($sql);
    }

    public function calNetWeightByCondition($condition){
        $sql = " select SUM(op.op_quantity * p.product_weight) from order_product op join receiving_order_detail rod on  rod.order_code=op.order_code join product p on p.product_id=op.product_id where 1 ";
        if(isset($condition['goods_id'])&&$condition['goods_id']!=""){
            $sql.= " and p.goods_id= '".$condition['goods_id']."'";
        }
        if(isset($condition['receiving_code'])&&$condition['receiving_code']!=""){
            $sql.= " and rod.receiving_code = '".$condition['receiving_code']."'";
        }
        return $this->_table->getAdapter()->fetchOne($sql);
    }

    public function checkOrder($product_id){
        $sql = "select op.* from order_product op join orders o on op.order_code=o.order_code where o.order_status in(4,5,6,7,8,9,10,14) and op.product_id=".$product_id;
        return $this->_table->getAdapter()->fetchAll($sql);
    }

    public function changeWeightCondition($product_id){
        $sql = "select count(op.op_id) from order_product op join orders o on op.order_code=o.order_code where o.order_status in(2,3,4,5,6,7,8,9,10,14) and op.product_id=".$product_id;
        return $this->_table->getAdapter()->fetchOne($sql);
    }

    public function getShippingCount($shippingCountDate) {
        $sql = "
        SELECT
            `p`.`customer_code`,
            `op`.`product_id`,
            SUM(`op`.`op_quantity`) AS `op_quantity_total`,
            SUM(
                `op`.`op_quantity` * `op`.`op_declared_value`
            ) AS `op_declared_value_total`
        FROM
            `order_product` AS `op`
        LEFT JOIN `order_operation_time` AS `oot` ON `op`.`order_id` = `oot`.`order_id`
        LEFT JOIN `product` AS `p` ON `op`.`product_id` = `p`.`product_id`
        WHERE
            `oot`.`finishbag_time` LIKE '" . $shippingCountDate . "%'
        GROUP BY
            `p`.`customer_code`,
            `op`.`product_id`;";
        return $this->_table->getAdapter()->fetchAll($sql);
    }

    public function getOrderNetWeight($order_code)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table.' as op', 'sum(product_weight*op_quantity) as net_weight');
        $select->joinInner('product as p', 'op.product_id=p.product_id', '');
        $select->where('op.order_code=?', $order_code);
        $row = $this->_table->getAdapter()->fetchRow($select);
        return empty($row) ? 0.00 : $row['net_weight'];
    }

}