<?php
class Table_BilCustomerBill
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BilCustomerBill();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BilCustomerBill();
    }

    /**
     * @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 = "bcb_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 = "bcb_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 = 'bcb_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 getJoinInnerSurplusByField($value, $field = 'bcb_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->joinInner('bil_customer_bill_surplus', "bil_customer_bill_surplus.bcb_id=bil_customer_bill.bcb_id");
        $select->where("bil_customer_bill.{$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["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["bcb_status"]) && $condition["bcb_status"] != ""){
            $select->where("bcb_status = ?",$condition["bcb_status"]);
        }
        if(isset($condition["bcb_code"]) && $condition["bcb_code"] != ""){
            $select->where("bcb_code = ?",$condition["bcb_code"]);
        }
        if(isset($condition["bcb_reference_no"]) && $condition["bcb_reference_no"] != ""){
            $select->where("bcb_reference_no = ?",$condition["bcb_reference_no"]);
        }

        if(isset($condition["bcb_writeoff_sign"]) && $condition["bcb_writeoff_sign"] != ""){
            $select->where("bcb_writeoff_sign = ?",$condition["bcb_writeoff_sign"]);
        }
        //不等于
        if(isset($condition["bcb_writeoff_sign_neq"]) && $condition["bcb_writeoff_sign_neq"] != ""){
            $select->where("bcb_writeoff_sign != ?",$condition["bcb_writeoff_sign_neq"]);
        }
        if(isset($condition["sm_code"]) && $condition["sm_code"] != ""){
            $select->where("sm_code = ?",$condition["sm_code"]);
        }
        if(isset($condition["bcb_create_type"]) && $condition["bcb_create_type"] != ""){
            $select->where("bcb_create_type = ?",$condition["bcb_create_type"]);
        }
        if(isset($condition["bcb_invoice_sign"]) && $condition["bcb_invoice_sign"] != ""){
            $select->where("bcb_invoice_sign = ?",$condition["bcb_invoice_sign"]);
        }
        if(isset($condition["bcb_review_sign"]) && $condition["bcb_review_sign"] != ""){
            $select->where("bcb_review_sign = ?",$condition["bcb_review_sign"]);
        }
        /*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 getJoinLeftByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft('bil_customer_bill_surplus', "bil_customer_bill_surplus.bcb_id=bil_customer_bill.bcb_id");
        $select->joinLeft('customer', "customer.customer_code=bil_customer_bill.customer_code",array("customer.customer_saler_user_id",'customer_lastname','customer_firstname','trade_name'));
        $select->joinLeft('user as u1', "u1.user_id=bil_customer_bill.bcb_creator_id",array('u1.user_name as creatorName'));
        $select->joinLeft('user as u2', "u2.user_id=bil_customer_bill.bcb_confirm_id",array('u2.user_name as confirmName'));
        $select->joinLeft('user as u3', "u3.user_id=bil_customer_bill.bcb_verifier_id",array('u3.user_name as verifierName'));
        $select->joinLeft('user as u4', "u4.user_id=bil_customer_bill.bcb_delete_id",array('u4.user_name as deleteName'));
        $select->joinLeft('user as u5', "u5.user_id=customer.customer_saler_user_id",array('u5.user_name as saleName'));
        $select->where("1 = ?", 1);
        /*CONDITION_START*/

        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("bil_customer_bill.customer_code = ?",$condition["customer_code"]);
        }
        //业务员
        if(isset($condition["user_id"]) && $condition["user_id"] !== ""){
            $select->where("customer.customer_saler_user_id = ?",$condition["user_id"]);
        }
        if(isset($condition["bcb_status"]) && $condition["bcb_status"] != ""){
            $select->where("bcb_status = ?",$condition["bcb_status"]);
        }
        if(isset($condition["bcb_code"]) && $condition["bcb_code"] != ""){
            $select->where("bcb_code = ?",$condition["bcb_code"]);
        }
        if(isset($condition["bcb_reference_no"]) && $condition["bcb_reference_no"] != ""){
            $select->where("bcb_reference_no = ?",$condition["bcb_reference_no"]);
        }
        if(isset($condition["bcb_amount"]) && $condition["bcb_amount"] != ""){
            $select->where("bcb_amount = ?",$condition["bcb_amount"]);
        }
        if(isset($condition["bcb_writeoff_sign"]) && $condition["bcb_writeoff_sign"] != ""){
            $select->where("bcb_writeoff_sign = ?",$condition["bcb_writeoff_sign"]);
        }
        if(isset($condition["sm_code"]) && $condition["sm_code"] != ""){
            $select->where("sm_code = ?",$condition["sm_code"]);
        }
        if(isset($condition["bcb_downloads"]) && $condition["bcb_downloads"] != ""){
            $select->where("bcb_downloads = ?",$condition["bcb_downloads"]);
        }
        if(isset($condition["bcb_note"]) && $condition["bcb_note"] != ""){
            $select->where("bcb_note = ?",$condition["bcb_note"]);
        }
        if(isset($condition["bcb_creator_id"]) && $condition["bcb_creator_id"] != ""){
            $select->where("bcb_creator_id = ?",$condition["bcb_creator_id"]);
        }
        if(isset($condition["bcb_create_type"]) && $condition["bcb_create_type"] != ""){
            $select->where("bcb_create_type = ?",$condition["bcb_create_type"]);
        }
        if(isset($condition["bcb_invoice_sign"]) && $condition["bcb_invoice_sign"] != ""){
            $select->where("bcb_invoice_sign = ?",$condition["bcb_invoice_sign"]);
        }
        if(isset($condition["bcb_invoice_amount"]) && $condition["bcb_invoice_amount"] != ""){
            $select->where("bcb_invoice_amount = ?",$condition["bcb_invoice_amount"]);
        }
        if(isset($condition["bcb_review_sign"]) && $condition["bcb_review_sign"] != ""){
            $select->where("bcb_review_sign = ?",$condition["bcb_review_sign"]);
        }
        if(isset($condition["bcb_delete_id"]) && $condition["bcb_delete_id"] != ""){
            $select->where("bcb_delete_id = ?",$condition["bcb_delete_id"]);
        }
        if(isset($condition["bcb_confirm_id"]) && $condition["bcb_confirm_id"] != ""){
            $select->where("bcb_confirm_id = ?",$condition["bcb_confirm_id"]);
        }
        if(isset($condition["bcb_verifier_id"]) && $condition["bcb_verifier_id"] != ""){
            $select->where("bcb_verifier_id = ?",$condition["bcb_verifier_id"]);
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("bcb_add_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("bcb_add_time <= ?",$condition["dateTo"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchOne($sql);
        } 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 getBillByCondition($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["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["bcb_status"]) && $condition["bcb_status"] != "") {
            $select->where("bcb_status = ?", $condition["bcb_status"]);
        }
        if (isset($condition["customer_code_arr"]) && !empty($condition["customer_code_arr"])) {
            $select->where("customer_code in(?)", $condition["customer_code_arr"]);
        }
        if (isset($condition["bcb_status_arr"]) && !empty($condition["bcb_status_arr"])) {
            $select->where("bcb_status in(?)", $condition["bcb_status_arr"]);
        }
        if (isset($condition["bcb_writeoff_sign"]) && $condition["bcb_writeoff_sign"] != "") {
            $select->where("bcb_writeoff_sign = ?", $condition["bcb_writeoff_sign"]);
        }
        //不等于
        if (isset($condition["bcb_writeoff_sign_neq"]) && $condition["bcb_writeoff_sign_neq"] != "") {
            $select->where("bcb_writeoff_sign != ?", $condition["bcb_writeoff_sign_neq"]);
        }
        /*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);
        }
    }


}