<?php

class Table_InvtListBody
{
    protected $_table = null;

    public function __construct() {
        $this->_table = new DbTable_InvtListBody();
    }

    public function getAdapter() {
        return $this->_table->getAdapter();
    }

    public static function getInstance() {
        return new Table_InvtListBody();
    }

    /**
     * @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 = "ilb_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 = "ilb_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 = 'ilb_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["ilb_id"]) && $condition["ilb_id"] != "") {
            $select->where("ilb_id = ?", $condition["ilb_id"]);
        }
        if (isset($condition["il_id"]) && $condition["il_id"] != "") {
            $select->where("il_id = ?", $condition["il_id"]);
        }
        if (isset($condition["SeqNo"]) && $condition["SeqNo"] != "") {
            $select->where("SeqNo = ?", $condition["SeqNo"]);
        }
        if (isset($condition["GdsSeqno"]) && $condition["GdsSeqno"] != "") {
            $select->where("GdsSeqno = ?", $condition["GdsSeqno"]);
        }
        if (isset($condition["PutrecSeqno"]) && $condition["PutrecSeqno"] != "") {
            $select->where("PutrecSeqno = ?", $condition["PutrecSeqno"]);
        }
        if (isset($condition["PutrecSeqnoEmpty"]) && $condition["PutrecSeqnoEmpty"] != "") {
            $select->where("PutrecSeqno = ''");
        }
        if (isset($condition["GdsMtno"]) && $condition["GdsMtno"] != "") {
            $select->where("GdsMtno = ?", $condition["GdsMtno"]);
        }
        if (isset($condition["GdsSpcfModelDesc"]) && $condition["GdsSpcfModelDesc"] != "") {
            $select->where("GdsSpcfModelDesc = ?", $condition["GdsSpcfModelDesc"]);
        }
        if (isset($condition["UsdStatTotalAmt"]) && $condition["UsdStatTotalAmt"] != "") {
            $select->where("UsdStatTotalAmt = ?", $condition["UsdStatTotalAmt"]);
        }

        /*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 getGiftProductByCondition($condition = array(),$type,$groupBy) {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from(["ib"=>$table],$type);
        $select->where("1 =?", 1);
        $select->joinLeft(['il'=>"invt_list"],'il.il_id = ib.il_id',null);
        $select->joinLeft(['ilb'=>"invt_list_body"],'il.il_id = ilb.il_id',null);
        $select->joinLeft(['p'=>"product"],'ilb.product_id = p.product_id',null);
        /*CONDITION_START*/
        if (isset($condition["il_id"]) && $condition["il_id"] != "") {
            $select->where("il.il_id = ?", $condition["il_id"]);
        }
        if (isset($condition["etps_inner_invt_no_in"]) && !empty($condition["etps_inner_invt_no_in"])) {
            $select->where("il.EtpsInnerInvtNo in(?)", $condition["etps_inner_invt_no_in"]);
        }
        if(isset($condition['gift_flag']) && !empty($condition['gift_flag'])){
            $select->where("p.gift_flag = ? ",$condition['gift_flag']);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($groupBy)) {
                $select->group($groupBy);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}