<?php
class Table_ShipBatchMessageHeader
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipBatchMessageHeader();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipBatchMessageBillHead();
    }

    /**
     * @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 = "sbmbh_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 : $row['gross_weight'];
    }
    
    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 : $row['net_weight'];
    }
    
    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 : floatval($row['law_weight']);
    }
    
    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);
    }

    /**
     * @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);
        }
    }
    
}