<?php

class Table_BillBook
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BillBook();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BillBook();
    }

    /**
     * @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 = "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 = "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 = '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 getByFieldForUpdate($value, $field = 'id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        $select->forUpdate();
        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["id"]) && $condition["id"] != "") {
            $select->where("id = ?", $condition["id"]);
        }
        if (isset($condition["last_id"]) && $condition["last_id"] != "") {
            $select->where("id > ?", $condition["last_id"]);
        }
        if (isset($condition["id_array"]) && !empty($condition["id_array"])) {
            $select->where("id in(?)", $condition["id_array"]);
        }
        if (isset($condition["state"]) && $condition["state"] !== "") {
            $select->where("state = ?", $condition["state"]);
        }
        if (isset($condition["warehous_account_no"]) && $condition["warehous_account_no"] != "") {
            $select->where("warehous_account_no = ?", $condition["warehous_account_no"]);
        }
        if (isset($condition["recorde_no"]) && $condition["recorde_no"] != "") {
            $select->where("recorde_no = ?", $condition["recorde_no"]);
        }
        if (isset($condition["recorde_no_list"]) && !empty($condition["recorde_no_list"])) {
            $select->where("recorde_no in(?)", $condition["recorde_no_list"]);
        }
        if (isset($condition["product_title"]) && $condition["product_title"] != "") {
            $select->where("product_title = ?", $condition["product_title"]);
        }
        if (isset($condition["hs_code"]) && $condition["hs_code"] != "") {
            $select->where("hs_code = ?", $condition["hs_code"]);
        }
        if (isset($condition["specification"]) && $condition["specification"] != "") {
            $select->where("specification = ?", $condition["specification"]);
        }
        if (isset($condition["country_code"]) && $condition["country_code"] != "") {
            $select->where("country_code = ?", $condition["country_code"]);
        }
        if (isset($condition["decalare_unit"]) && $condition["decalare_unit"] != "") {
            $select->where("decalare_unit = ?", $condition["decalare_unit"]);
        }
        if (isset($condition["legal_unit"]) && $condition["legal_unit"] != "") {
            $select->where("legal_unit = ?", $condition["legal_unit"]);
        }
        if (isset($condition["second_legal_unit"]) && $condition["second_legal_unit"] != "") {
            $select->where("second_legal_unit = ?", $condition["second_legal_unit"]);
        }
        if (isset($condition["decalar_price"]) && $condition["decalar_price"] != "") {
            $select->where("decalar_price = ?", $condition["decalar_price"]);
        }
        if (isset($condition["decalar_currency"]) && $condition["decalar_currency"] != "") {
            $select->where("decalar_currency = ?", $condition["decalar_currency"]);
        }
        if (isset($condition["avg_dollar_price"]) && $condition["avg_dollar_price"] != "") {
            $select->where("avg_dollar_price = ?", $condition["avg_dollar_price"]);
        }
        if (isset($condition["total_dollar_price"]) && $condition["total_dollar_price"] != "") {
            $select->where("total_dollar_price = ?", $condition["total_dollar_price"]);
        }
        if (isset($condition["in_quantity"]) && $condition["in_quantity"] != "") {
            $select->where("in_quantity = ?", $condition["in_quantity"]);
        }
        if (isset($condition["in_legal_quantity"]) && $condition["in_legal_quantity"] != "") {
            $select->where("in_legal_quantity = ?", $condition["in_legal_quantity"]);
        }
        if (isset($condition["in_seconde_legal_quantity"]) && $condition["in_seconde_legal_quantity"] != "") {
            $select->where("in_seconde_legal_quantity = ?", $condition["in_seconde_legal_quantity"]);
        }
        if (isset($condition["real_increa_quantity"]) && $condition["real_increa_quantity"] != "") {
            $select->where("real_increa_quantity = ?", $condition["real_increa_quantity"]);
        }
        if (isset($condition["real_reduce_quantity"]) && $condition["real_reduce_quantity"] != "") {
            $select->where("real_reduce_quantity = ?", $condition["real_reduce_quantity"]);
        }
        if (isset($condition["pre_increa_quantity"]) && $condition["pre_increa_quantity"] != "") {
            $select->where("pre_increa_quantity = ?", $condition["pre_increa_quantity"]);
        }
        if (isset($condition["pre_reduce_quantity"]) && $condition["pre_reduce_quantity"] != "") {
            $select->where("pre_reduce_quantity = ?", $condition["pre_reduce_quantity"]);
        }
        if (isset($condition["storage_date"]) && $condition["storage_date"] != "") {
            $select->where("storage_date = ?", $condition["storage_date"]);
        }
        if (isset($condition["bill_no"]) && $condition["bill_no"] != "") {
            $select->where("bill_no = ?", $condition["bill_no"]);
        }
        if (isset($condition["bill_serial"]) && $condition["bill_serial"] != "") {
            $select->where("bill_serial = ?", $condition["bill_serial"]);
        }
        if (isset($condition["stock_quantity_gt"]) && $condition["stock_quantity_gt"] !== "") {
            $select->where("stock_quantity > ?", $condition["stock_quantity_gt"]);
        }
        if (isset($condition["ie_port"]) && $condition["ie_port"] != "") {
            $select->where("ie_port = ?", $condition["ie_port"]);
        }
        if (isset($condition["bill_time_from"]) && $condition["bill_time_from"] != "") {
            $select->where("bill_time >= ?", $condition["bill_time_from"]);
        }
        if (isset($condition["bill_time_to"]) && $condition["bill_time_to"] != "") {
            $select->where("bill_time <= ?", $condition["bill_time_to"]);
        }
        if (isset($condition["goods_code"]) && $condition["goods_code"] != "") {
            $select->where("goods_code = ?", $condition["goods_code"]);
        }
        if (isset($condition["custom_code"]) && $condition["custom_code"] != "") {
            $select->where("custom_code = ?", $condition["custom_code"]);
        }
        if (isset($condition["total_price"]) && $condition["total_price"] != "") {
            $select->where("total_price = ?", $condition["total_price"]);
        }
        if (isset($condition["product_sku"]) && $condition["product_sku"] != "") {
            $select->where("product_sku = ?", $condition["product_sku"]);
        }
        if (isset($condition["product_id"]) && $condition["product_id"] != "") {
            $select->where("product_id = ?", $condition["product_id"]);
        }
        if (isset($condition["add_time_from"]) && $condition["add_time_from"] != "") {
            $select->where("add_time >= ?", $condition["add_time_from"]);
        }
        if (isset($condition["add_time_to"]) && $condition["add_time_to"] != "") {
            $select->where("add_time <= ?", $condition["add_time_to"]);
        }
        if (isset($condition["can_use_quantity_gt"]) && $condition["can_use_quantity_gt"] != "") {
            $select->where("can_use_quantity >= ?", $condition["can_use_quantity_gt"]);
        }
        if (isset($condition["frozen_quantity_gt"]) && $condition["frozen_quantity_gt"] != "") {
            $select->where("frozen_quantity >= ?", $condition["frozen_quantity_gt"]);
        }
        if (isset($condition["lock_quantity_gt"]) && $condition["lock_quantity_gt"] != "") {
            $select->where("lock_quantity >= ?", $condition["lock_quantity_gt"]);
        }
        if (isset($condition["state"]) && $condition["state"] !== "") {
            $select->where("state = ?", $condition["state"]);
        }
        /*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 getJoinLeftDetailBycondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from(['b'=>$table], $type);
        $select->joinLeft(['zb'=>'bill_book_product'],'zb.bill_book_id=b.id',null);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["id"]) && $condition["id"] != "") {
            $select->where("b.id = ?", $condition["id"]);
        }
        if (isset($condition["id_array"]) && !empty($condition["id_array"])) {
            $select->where("b.id in(?)", $condition["id_array"]);
        }
        if (isset($condition["warehous_account_no"]) && $condition["warehous_account_no"] != "") {
            $select->where("b.warehous_account_no = ?", $condition["warehous_account_no"]);
        }
        if (isset($condition["recorde_no"]) && $condition["recorde_no"] != "") {
            $select->where("recorde_no = ?", $condition["recorde_no"]);
        }
        if (isset($condition["hs_code"]) && $condition["hs_code"] != "") {
            $select->where("hs_code = ?", $condition["hs_code"]);
        }

        if (isset($condition["stock_quantity_gt"]) && $condition["stock_quantity_gt"] !== "") {
            $select->where("stock_quantity > ?", $condition["stock_quantity_gt"]);
        }
        if (isset($condition["ie_port"]) && $condition["ie_port"] != "") {
            $select->where("ie_port = ?", $condition["ie_port"]);
        }

        if (isset($condition["product_id"]) && $condition["product_id"] != "") {
            $select->where("zb.product_id = ?", $condition["product_id"]);
        }
        if (isset($condition["add_time_from"]) && $condition["add_time_from"] != "") {
            $select->where("add_time >= ?", $condition["add_time_from"]);
        }
        if (isset($condition["add_time_to"]) && $condition["add_time_to"] != "") {
            $select->where("add_time <= ?", $condition["add_time_to"]);
        }
        if (isset($condition["can_use_quantity_gt"]) && $condition["can_use_quantity_gt"] != "") {
            $select->where("can_use_quantity >= ?", $condition["can_use_quantity_gt"]);
        }
        /*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);
        }
    }
}