<?php
class Table_OutboundBag
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_OutboundBag();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_OutboundBag();
    }

    /**
     * @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 = "ob_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 = "ob_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 = 'ob_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["ob_no"]) && $condition["ob_no"] != "") {
            $select->where("ob_no = ?", $condition["ob_no"]);
        }
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != "") {
            $select->where("warehouse_id = ?", $condition["warehouse_id"]);
        }
        if (isset($condition["ob_status"]) && $condition["ob_status"] != "") {
            $select->where("ob_status = ?", $condition["ob_status"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["ob_weight"]) && $condition["ob_weight"] != "") {
            $select->where("ob_weight = ?", $condition["ob_weight"]);
        }
        if (isset($condition["creater_id"]) && $condition["creater_id"] != "") {
            $select->where("creater_id = ?", $condition["creater_id"]);
        }
        if (isset($condition["dateFor"]) && $condition["dateFor"] != "") {
            $select->where("ob_add_time >= ?", $condition["dateFor"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("ob_add_time <= ?", $condition["dateTo"]);
        }
        /*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 getLeftJoinUserByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('user', 'user.user_id=' . $table . '.creater_id', array('user_name'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/

        if (isset($condition["ob_no"]) && $condition["ob_no"] != "") {
            $select->where("ob_no = ?", $condition["ob_no"]);
        }
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != "") {
            $select->where("warehouse_id = ?", $condition["warehouse_id"]);
        }
        if (isset($condition["ob_status"]) && $condition["ob_status"] != "") {
            $select->where("ob_status = ?", $condition["ob_status"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["ob_weight"]) && $condition["ob_weight"] != "") {
            $select->where("ob_weight = ?", $condition["ob_weight"]);
        }
        if (isset($condition["creater_id"]) && $condition["creater_id"] != "") {
            $select->where("creater_id = ?", $condition["creater_id"]);
        }
        if (isset($condition["dateFor"]) && $condition["dateFor"] != "") {
            $select->where("ob_add_time >= ?", $condition["dateFor"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where("ob_add_time <= ?", $condition["dateTo"]);
        }
        /*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 getJoinOrderByCondition($condition = array(),$type="*",$pageSize=0,$page=1,$orderBy=""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        if($type=="count(*)"){
            $select->from($table, "COUNT(DISTINCT outbound_bag.ob_no)");
        }else{
            $select->from($table,$type);
        }
        //$select->from($table, $type);
        $select->joinLeft('outbound_bag_detail', 'outbound_bag_detail.ob_no=' . $table . '.ob_no', array());
        $select->joinLeft("orders",'orders.order_code=outbound_bag_detail.order_code',array());
        $select->where("1 =?", 1);

        if (isset($condition["ob_no"]) && $condition["ob_no"] != "") {
            $select->where($table.".ob_no = ?", $condition["ob_no"]);
        }
        if (isset($condition["warehouse_id"]) && $condition["warehouse_id"] != "") {
            $select->where($table.".warehouse_id = ?", $condition["warehouse_id"]);
        }
        if (isset($condition["ob_status"]) && $condition["ob_status"] != "") {
            $select->where($table.".ob_status = ?", $condition["ob_status"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where($table.".sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["ob_weight"]) && $condition["ob_weight"] != "") {
            $select->where($table.".ob_weight = ?", $condition["ob_weight"]);
        }
        if (isset($condition["creater_id"]) && $condition["creater_id"] != "") {
            $select->where($table.".creater_id = ?", $condition["creater_id"]);
        }
        if (isset($condition["dateFor"]) && $condition["dateFor"] != "") {
            $select->where($table.".ob_add_time >= ?", $condition["dateFor"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where($table.".ob_add_time <= ?", $condition["dateTo"]);
        }
        if(isset($condition['order_code'])&&$condition['order_code']!=""){
            $select->where(" orders.order_code = ?",$condition['order_code']);
        }
        if(isset($condition['reference_no'])&&$condition['reference_no']!=""){
            $select->where(" orders.reference_no = ?",$condition['reference_no']);
        }

        if ('count(*)' == $type) {
            //$select->group($table.'.ob_no');
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->group($table.'.ob_no');
            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 getExportFileJoinByCondition($condition = array(),$type="*",$pageSize=0,$page=1,$orderBy=""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        switch ($condition['select']) {
            case 'ship':
                $select->joinLeft('outbound_bag_detail', 'outbound_bag_detail.ob_no=' . $table . '.ob_no', array('so_weight','order_code as obd_order_code','obd_add_time'));
                $select->joinLeft("orders",'orders.order_code=outbound_bag_detail.order_code',array('ref_tracking_number as orders_ref_tracking_number','order_code as orders_order_code'));
                $select->joinLeft("ship_order",'ship_order.order_code=outbound_bag_detail.order_code',array('tracking_number','order_code as so_order_code','so_ship_time'));
                $select->joinLeft("order_product",'order_product.order_code=ship_order.order_code',array('op_quantity','order_product.product_id as order_product_product_id','product_title_en as op_product_title_en','op_purpose_declared_value'));
                $select->joinLeft("order_address_book",'order_address_book.order_code=order_product.order_code',array('CONCAT(oab_firstname,oab_lastname) as add_name','oab_phone','oab_postcode','oab_street_address1','oab_country_id','oab_firstname','oab_lastname'));
                $select->joinLeft("country",'country.country_id=order_address_book.oab_country_id',array('country_code'));
                $select->joinLeft("product",'product.product_id=order_product.product_id',array('product_id','product_sku','product_title','product_title_en','product_weight','pu_code','product_declared_value'));
                $select->joinLeft("product_uom",'product.pu_code=product_uom.pu_code',array('pu_name'));
                break;
            case 'air':
                $select->joinLeft('outbound_bag_detail', 'outbound_bag_detail.ob_no=' . $table . '.ob_no', array('so_weight','order_code as obd_order_code'));
                $select->joinLeft("orders",'orders.order_code=outbound_bag_detail.order_code',array('ref_tracking_number as orders_ref_tracking_number','order_code as orders_order_code'));
                $select->joinLeft("ship_order",'ship_order.order_code=outbound_bag_detail.order_code',array('tracking_number','order_code as so_order_code','so_ship_time'));
                $select->joinLeft("order_address_book",'order_address_book.order_code=ship_order.order_code',array('oab_postcode','oab_country_id'));
                break;
            case 'sea':
                # code...
                break;
            
            default:
                # code...
                break;
        }
        $select->where("1 =?", 1);
        if (isset($condition["ob_status"]) && $condition["ob_status"] != "") {
            $select->where($table.'.ob_status IN(?)', $condition["ob_status"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where($table.'.sm_code IN(?)', $condition["sm_code"]);
        }
        if (isset($condition["ob_no"]) && $condition["ob_no"] != "") {
            $select->where($table.'.ob_no IN(?)', $condition["ob_no"]);
        }
        if(isset($condition["order_code"]) && $condition["order_code"] != ""){
            $select->where("ship_order.order_code = ?",$condition["order_code"]);
        }
        if(isset($condition["reference_no"]) && $condition["reference_no"] != ""){
            $select->where("orders.reference_no = ?",$condition["reference_no"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("warehouse_id = ?",$condition["warehouse_id"]);
        }
        if (isset($condition["dateFor"]) && $condition["dateFor"] != "") {
            $select->where($table.".ob_add_time >= ?", $condition["dateFor"]);
        }
        if (isset($condition["dateTo"]) && $condition["dateTo"] != "") {
            $select->where($table.".ob_add_time <= ?", $condition["dateTo"]);
        }

        // if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            // $select->where('op_quantity > 0');
        // }

        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);
        }
    }

}