<?php

class Table_BillBookProduct
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BillBookProduct();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BillBookProduct();
    }

    /**
     * @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 = "bbp_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 = "bbp_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 = 'bbp_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["bbp_id"]) && $condition["bbp_id"] != "") {
            $select->where("bbp_id = ?", $condition["bbp_id"]);
        }
        if (isset($condition["bill_book_id"]) && $condition["bill_book_id"] != "") {
            $select->where("bill_book_id = ?", $condition["bill_book_id"]);
        }
        if (isset($condition["product_sku"]) && $condition["product_sku"] != "") {
            $select->where("product_sku = ?", $condition["product_sku"]);
        }
        if (isset($condition["product_sku_in"]) && !empty($condition["product_sku_in"])) {
            $select->where("product_sku in(?)", $condition["product_sku_in"]);
        }
        if (isset($condition["product_id"]) && $condition["product_id"] != "") {
            $select->where("product_id = ?", $condition["product_id"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["add_time"]) && $condition["add_time"] != "") {
            $select->where("add_time = ?", $condition["add_time"]);
        }
        /*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);
        }
    }
    /**
     * @todo
     */
    public function getInnerByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = ""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->joinInner('bill_book as bb','bb.id='.$table.'.bill_book_id');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["bbp_id"]) && $condition["bbp_id"] != "") {
            $select->where("bbp_id = ?", $condition["bbp_id"]);
        }
        if (isset($condition["product_id"]) && $condition["product_id"] != "") {
            $select->where("bill_book_product.product_id = ?", $condition["product_id"]);
        }
        if (isset($condition["warehous_account_no"]) && $condition["warehous_account_no"] != "") {
            $select->where("warehous_account_no = ?", $condition["warehous_account_no"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("bill_book_product.customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["state"]) && $condition["state"] !== "") {
            $select->where("bb.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);
        }
    }
}