<?php

class Table_BilBusiness
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BilBusiness();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BilBusiness();
    }

    /**
     * @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 = "bb_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 = "bb_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 = 'bb_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 getAttrByRefCode($code = '')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, '*');
        $select->joinInner('bil_business_attach', "bil_business_attach.bb_id=bil_business.bb_id");
        $select->where("bil_business_attach.bb_refer_code = ?", $code);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAttrByField($value, $field = 'bb_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->joinInner('bil_business_attach', "bil_business_attach.bb_id=bil_business.bb_id");
        $select->where("bil_business.{$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["bt_code"]) && $condition["bt_code"] != "") {
            $select->where("bt_code = ?", $condition["bt_code"]);
        }
        if (isset($condition["ds_code"]) && $condition["ds_code"] != "") {
            $select->where("ds_code = ?", $condition["ds_code"]);
        }
        if (isset($condition["customer_code"]) && $condition["customer_code"] != "") {
            $select->where("customer_code = ?", $condition["customer_code"]);
        }
        if (isset($condition["bb_status"]) && $condition["bb_status"] != "") {
            $select->where("bb_status = ?", $condition["bb_status"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["bb_volume_weight"]) && $condition["bb_volume_weight"] != "") {
            $select->where("bb_volume_weight = ?", $condition["bb_volume_weight"]);
        }
        if (isset($condition["bb_charge_weight"]) && $condition["bb_charge_weight"] != "") {
            $select->where("bb_charge_weight = ?", $condition["bb_charge_weight"]);
        }
        if (isset($condition["bb_delivered_weight"]) && $condition["bb_delivered_weight"] != "") {
            $select->where("bb_delivered_weight = ?", $condition["bb_delivered_weight"]);
        }
        if (isset($condition["bb_service_provider_weight"]) && $condition["bb_service_provider_weight"] != "") {
            $select->where("bb_service_provider_weight = ?", $condition["bb_service_provider_weight"]);
        }
        if (isset($condition["bb_volume"]) && $condition["bb_volume"] != "") {
            $select->where("bb_volume = ?", $condition["bb_volume"]);
        }
        if (isset($condition["bb_pieces"]) && $condition["bb_pieces"] != "") {
            $select->where("bb_pieces = ?", $condition["bb_pieces"]);
        }
        if (isset($condition["bb_declared_value"]) && $condition["bb_declared_value"] != "") {
            $select->where("bb_declared_value = ?", $condition["bb_declared_value"]);
        }
        if (isset($condition["bb_declared_currency_code"]) && $condition["bb_declared_currency_code"] != "") {
            $select->where("bb_declared_currency_code = ?", $condition["bb_declared_currency_code"]);
        }
        if (isset($condition["country_code"]) && $condition["country_code"] != "") {
            $select->where("country_code = ?", $condition["country_code"]);
        }
        if (isset($condition["bb_postcode"]) && $condition["bb_postcode"] != "") {
            $select->where("bb_postcode = ?", $condition["bb_postcode"]);
        }
        if (isset($condition["bb_sales_id"]) && $condition["bb_sales_id"] != "") {
            $select->where("bb_sales_id = ?", $condition["bb_sales_id"]);
        }
        if (isset($condition["bb_charge_weight_sign"]) && $condition["bb_charge_weight_sign"] != "") {
            $select->where("bb_charge_weight_sign = ?", $condition["bb_charge_weight_sign"]);
        }
        if (isset($condition["bb_bill_compare_sign"]) && $condition["bb_bill_compare_sign"] != "") {
            $select->where("bb_bill_compare_sign = ?", $condition["bb_bill_compare_sign"]);
        }
        if (isset($condition["bb_charge_type"]) && $condition["bb_charge_type"] != "") {
            $select->where("bb_charge_type = ?", $condition["bb_charge_type"]);
        }
        if (isset($condition["bb_charge_sign"]) && $condition["bb_charge_sign"] != "") {
            $select->where("bb_charge_sign = ?", $condition["bb_charge_sign"]);
        }
        if (isset($condition["bb_balance_sign"]) && $condition["bb_balance_sign"] != "") {
            $select->where("bb_balance_sign = ?", $condition["bb_balance_sign"]);
        }
        if (isset($condition["bb_writeoff_sign"]) && $condition["bb_writeoff_sign"] != "") {
            $select->where("bb_writeoff_sign = ?", $condition["bb_writeoff_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);
        }
    }

}