<?php
class Table_ShipBatchDetail
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipBatchDetail();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipBatchDetail();
    }

    /**
     * @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 = "sbd_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 = "sbd_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 = 'sbd_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 stat($sb_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from($this->_table->info('name'), array('count(sbd_id) as num','sum(ob_weight) as weight'));
    	$select->where('sb_code=?', $sb_code);
    	return $this->_table->getAdapter()->fetchRow($select);
    }
    
    public function sumGrossWeight($sb_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from($this->_table->info('name'), 'sum(ob_weight) as gross_weight');
    	$select->where('sb_code=?', $sb_code);
    	$row = $this->_table->getAdapter()->fetchRow($select);
    	return empty($row) ? 0.00 : round($row['gross_weight'],2);
    }
    
    public function sumNetWeight($sb_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from($this->_table->info('name').' as sbd', 'sum(product_weight*op_quantity) as net_weight');
    	$select->joinLeft('outbound_bag_detail as obd', 'sbd.ob_no=obd.ob_no', '');
    	$select->joinLeft('order_product as op', 'obd.order_code=op.order_code', '');
    	$select->joinLeft('product as p', 'op.product_id=p.product_id', '');
    	$select->where('sbd.sb_code=?', $sb_code);
    	$row = $this->_table->getAdapter()->fetchRow($select);
    	return empty($row) ? 0.00 : round($row['net_weight'],2);
    }
    
    public function sumLawWeight($sb_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from('ship_batch_detail AS sbd', "sum( op.op_quantity * if(hu.pu_code_law = '035',hum.hum_quantity_law,hum.hum_quantity_second )) as law_weight");
    	$select->joinLeft('outbound_bag_detail AS obd', 'sbd.ob_no=obd.ob_no', '');
    	$select->joinLeft('order_product AS op', 'obd.order_code=op.order_code', '');
    	$select->joinLeft('product AS p', 'op.product_id=p.product_id', '');
    	$select->joinLeft('hs_uom AS hu', 'p.hs_code = hu.hs_code', '');
    	$select->joinLeft('hs_uom_map AS hum', 'op.product_id = hum.product_id', '');
    	$select->where("sbd.sb_code=? AND (hu.pu_code_law='035' OR hu.pu_code_second='035')", $sb_code);
    	$row = $this->_table->getAdapter()->fetchRow($select);
    	return empty($row) ? 0.00 : round(floatval($row['law_weight']),2);
    }
    
    public function countHscodeNum($sb_code) {
    	$select = $this->_table->getAdapter()->select();
    	$select->from($this->_table->info('name').' as sbd', 'count(DISTINCT p.hs_code)');
    	$select->joinLeft('outbound_bag_detail as obd', 'sbd.ob_no=obd.ob_no', '');
    	$select->joinLeft('order_product as op', 'obd.order_code=op.order_code', '');
    	$select->joinLeft('product as p', 'op.product_id=p.product_id', '');
    	$select->where('sbd.sb_code=?', $sb_code);
    	$count = $this->_table->getAdapter()->fetchOne($select);
    	return intval($count);
    }

    public function getHscode($sb_code){
        $sql = "select DISTINCT p.hs_code from ship_batch_detail as sbd join outbound_bag_detail as obd on sbd.ob_no=obd.ob_no join order_product as op on obd.order_code=op.order_code join product as p on op.product_id=p.product_id where sbd.sb_code='".$sb_code."'";
        return $this->_table->getAdapter()->fetchCol($sql);
    }

    /**
     * @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["sb_code"]) && $condition["sb_code"] != ""){
            $select->where("sb_code = ?",$condition["sb_code"]);
        }
        if(isset($condition["ob_no"]) && $condition["ob_no"] != ""){
            $select->where("ob_no = ?",$condition["ob_no"]);
        }
        if(isset($condition["creater_id"]) && $condition["creater_id"] != ""){
            $select->where("creater_id = ?",$condition["creater_id"]);
        }
        if(isset($condition["ob_weight"]) && $condition["ob_weight"] != ""){
            $select->where("ob_weight = ?",$condition["ob_weight"]);
        }
        /*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);
        }
    }


    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByJoinProductGroupCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type)
            ->joinLeft('outbound_bag_detail', $table.'.ob_no = outbound_bag_detail.ob_no','order_code')
            ->joinLeft('orders', 'outbound_bag_detail.order_code = orders.order_code',array('order_code','order_status'))
            ->joinLeft('order_operation_time', 'orders.order_id = order_operation_time.order_id','shipment_time')
            ->joinLeft('order_product', 'orders.order_id = order_product.order_id',null)
            ->joinLeft('product', 'order_product.product_id = product.product_id',array('product_barcode','product_id','product_sku','product_title','goods_id','hs_code'))
            ->joinLeft('customer', 'product.customer_id = customer.customer_id','customer_company_name');
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["sb_code"]) && $condition["sb_code"] != ""){
            $select->where("sb_code = ?",$condition["sb_code"]);
        }
        if(isset($condition["ob_no"]) && $condition["ob_no"] != ""){
            $select->where("ob_no = ?",$condition["ob_no"]);
        }
        if(isset($condition["order_mode_type"]) && $condition["order_mode_type"] != ""){
            $select->where("orders.order_mode_type = ?",$condition["order_mode_type"]);
        }
        if(isset($condition["order_status"]) && $condition["order_status"] != ""){
            $select->where("orders.order_status in(?)",$condition["order_status"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            // $select->group(array("product.product_sku","customer.customer_company_name"));
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            // echo $sql;exit;
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    public function getTotalPrice($sb_code){
        $select = $this->_table->getAdapter()->select();
        $select->from($this->_table->info('name').' as sbd', '');
        $select->joinLeft('outbound_bag_detail as obd', 'sbd.ob_no=obd.ob_no', '');
        $select->joinLeft('order_product as op', 'obd.order_code=op.order_code', 'sum(op_declared_value*op_quantity) as declared_value');
        $select->where('sbd.sb_code=?', $sb_code);
        $row = $this->_table->getAdapter()->fetchRow($select);
        return empty($row) ? 0.00 : $row['declared_value'];
    }

    public function getOrderCode($sb_code){
        $select = $this->_table->getAdapter()->select();
        $select->from($this->_table->info('name').' as sbd', '');
        $select->joinLeft('outbound_bag_detail as obd', 'sbd.ob_no=obd.ob_no', 'order_code');
        $select->where('sbd.sb_code=?', $sb_code);
        return $this->_table->getAdapter()->fetchCol($select);
    }
    
}