<?php

/* 
 * Author       : Govind M
 * Purpose      : Model for Billing Temple Shop Sales
 * Created On   : 2017-01-28
 */

class Temple_shop_sales_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service 			= TBL_SERVICE;
        $this->templeshop 		= TBL_BILL_TEMPLESHOP_SALES;
        $this->templeshop_item 	= TBL_BILL_TEMPLESHOP_ITEM;
        $this->payment_type 	= TBL_PAY_TYPE;
        $this->payment 			= TBL_PAY;
        $this->god 				= TBL_GOD_NAME;
        $this->stars 			= TBL_STAR;
        $this->prasadam 		= TBL_PRASADAM;
        $this->refund 			= TBL_REFUND;
		$this->item 			= TBL_INVENTORY_ITEM;
		$this->inv_stock_issue	= TBL_INVENTORY_STOCK_ISSUE;
    }
    
    public function list_all() {
        $select = 'f.id, f.receipt_no, f.devotee_name1, f.date, f.contact_no, s.name as service, s.prefix, f.amount as f_amount, f.status';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = f.service_id');
        #$this->db->where('f.status !=',2);
        $this->db->order_by('f.id','desc');
        return $this->db->get($this->templeshop . ' as f')->result();
    }
    
    public function get_data_by_service_type($type_id){
        $this->db->where('service_id',$type_id);
        $this->db->where('status !=',2);
        return $this->db->get($this->templeshop)->result();
    }
    
    public function get_contact_by_service_type($type_id){
        $this->db->where('service_id',$type_id);
        $this->db->where('status !=',2);
        $this->db->group_by('contact_no');
        return $this->db->get($this->templeshop)->result();
    }
    
    public function get( $tbl_name, $where = array(), $args = array() ) {
        if (!is_array($where)) { $where = array(); }
        $where['status'] = 1;
        if($tbl_name == 'thiru_payment_types') :
            $where['menu_id'] = 0;
        endif;
        
        if( !empty( $args['order_by'] ) ) :
            $this->db->order_by( $args['order_by'] );
        endif;
        
        $q = $this->db->get_where( $tbl_name, $where );
        return $q->result_array();
            
    }
	
	public function get_item_details_by_id($itemID) {
        $this->db->select('item_name, code',FALSE);
		$this->db->where('id', $itemID);
        return $this->db->get($this->item)->row();
    }
	
	public function get_item_quatity_by_id($itemID) {
        $this->db->select('total_quantity',FALSE);
		$this->db->where('item_id', $itemID);
        return $this->db->get(TBL_SB_ITEM)->row();
    }
	
	public function get_item_details($itemcode) {
        $this->db->select('ISI.*, IT.item_name, IT.selling_price',FALSE);
        $this->db->join($this->inv_stock_issue . ' as ISI','ISI.item_id = IT.id');
		$this->db->where('IT.code', $itemcode);
		$this->db->where_in('IT.category_id', array('4','5'));
        return $this->db->get($this->item . ' as IT')->row();
    }
    
    public function get_sb_item() {
        $this->db->select('p.*, pt.item_name, pt.selling_price',FALSE);
        $this->db->join(TBL_INV_ITEM . ' as pt','pt.id = p.item_id AND pt.is_statue_item = 1');
        $this->db->join(TBL_INV_CATEGORY . ' as c','c.id = pt.category_id AND c.is_statue_cat = 1');
        return $this->db->get(TBL_SB_ITEM . ' as p')->result_array();
    }
    
	public function update_issue_item_qty($sb_id, $update_data, $quantity, $op = '+') {
        $this->db->set('available_qty', 'available_qty ' . $op . $quantity, FALSE);
        $this->db->where('item_id', $sb_id);
		$this->db->where_in('category_id', array('4','5'));
        if ($op == '-') {
            $this->db->where('available_qty >= ', $quantity);
        }
        $this->db->update($this->inv_stock_issue, $update_data);
        return $this->db->affected_rows();
    }
	
    public function update_sb_item_qty($sb_id, $update_data, $quantity, $op = '+') {
        $this->db->set('total_quantity', 'total_quantity ' . $op . $quantity, FALSE);
        $this->db->where('item_id', $sb_id);
        if ($op == '-') {
            $this->db->where('total_quantity >= ', $quantity);
        }
        $this->db->update(TBL_SB_ITEM, $update_data);
        return $this->db->affected_rows();
    }
    
    public function get_service_type($type= ''){
        $this->db->where('s.menu_name',$type);
        $this->db->where('s.status',1);
        return $this->db->get($this->service . ' as s')->result();
    }
    
    public function service_type_exists($type, $bk_status = '') {
        $this->db->where('service_id',$type);
        $this->db->where('status !=',2);
        $this->db->order_by('receipt_no','desc');
        return $this->db->get($this->templeshop);
    }
    
    public function insert($data) {
        $this->db->insert($this->templeshop,$data);
        return $this->db->insert_id();
    }
    
    public function insert_prasadam($data) {
        $this->db->insert($this->templeshop_item,$data);
        return $this->db->insert_id();
    }
    
    public function insert_payment($data){
        $this->db->insert($this->payment,$data);
        return $this->db->insert_id();
    }
    
    public function update($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->templeshop,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam($data,$id) {
        $this->db->where('temple_shop_id',$id);
        $this->db->update($this->templeshop_item,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam_by_id($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->templeshop_item,$data);
        return $this->db->affected_rows();
    }
    
    public function festival_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->templeshop)->num_rows();
    }
    
    public function get_by_id($id) {
        $select = 'f.*, CASE f.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str';
        $select .= ', s.name as service, s.prefix, f.amount as f_amount, s.amount as ser_amount';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = f.service_id');
        $this->db->where('f.id',$id);
        return $this->db->get($this->templeshop . ' as f')->row();
    }
    
    public function get_paid_type($menu,$type,$id) {
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('pay_type_id',$id);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function update_refund($data,$type_id,$id) {
        $this->db->where('menu_id',$type_id);
        $this->db->where('auto_id',$id);
        $this->db->update($this->refund,$data);
        return $this->db->affected_rows();
    }
    
    public function get_paid_type_by_id($menu,$type) {
        $this->db->select('p.pay_amount, pt.img_url, pt.name',FALSE);
        $this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id');
        $this->db->where('p.pay_ref_type',$menu);
        $this->db->where('p.pay_ref_id',$type);
        $this->db->where('p.status',1);
        return $this->db->get($this->payment . ' as p')->result();
    }
    
    public function total_paid_amount($menu,$type) {
        $this->db->select('SUM(pay_amount) as paidAmount');
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function get_prasadam_by_itemid($id){
        $this->db->select('fp.*, p.item_name, p.selling_price',FALSE);
        $this->db->join(TBL_SB_ITEM . ' sbt','sbt.item_id = fp.inventory_item_id','left');
        $this->db->join(TBL_INV_ITEM . ' p','p.id = sbt.item_id','left');
        $this->db->where('fp.id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->templeshop_item . ' as fp')->result_array();
    }
    
	/*
    public function get_prasadam_by_id($id){
        $this->db->select('fp.*, p.item_name, p.selling_price',FALSE);
        $this->db->join(TBL_SB_ITEM . ' sbt','sbt.item_id = fp.inventory_item_id','left');
        $this->db->join(TBL_INV_ITEM . ' p','p.id = sbt.item_id','left');
        $this->db->where('fp.temple_shop_id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->templeshop_item . ' as fp')->result();
    }
	*/
	
	public function get_prasadam_by_id($id){
        $this->db->select('fp.*, p.item_name, p.selling_price',FALSE);
        $this->db->join(TBL_INVENTORY_ITEM . ' p','p.id = fp.inventory_item_id','left');
        $this->db->where('fp.temple_shop_id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->templeshop_item . ' as fp')->result();
    }
    
    public function update_paid_type($data,$type_id,$id) {
        $this->db->where('pay_ref_type',$type_id);
        $this->db->where('pay_ref_id',$id);
        $this->db->update($this->payment,$data);
        return $this->db->affected_rows();
    }
    
    public function get_prasadam_json($id){
        $this->db->select('fp.*',FALSE);
//        $this->db->select('fp.*, p.name as prasadam_name',FALSE);
//        $this->db->join($this->prasadam . ' p','p.id = fp.prasadam','left');
        $this->db->where('fp.temple_shop_id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->templeshop_item . ' as fp')->result_array();
    }
    
    public function delete_paid_type($type_id,$id) {
        $this->db->where('pay_ref_type',$type_id);
        $this->db->where('pay_ref_id',$id);
        $this->db->delete($this->payment);
        return $this->db->affected_rows();
    }
    
    public function get_all( $id, $where = array() ) {
        if( !empty( $id ) ) :
            $where['self.id'] = $id;
        endif;
        
        $where['self.status'] = 1;
        
        
        $this->db->select( 'self.*, self.status AS status, serv.name AS service_name, serv.prefix' );
        
        $this->db->join( TBL_SERVICE . ' AS serv', 'serv.id = self.service_id', 'left' );
        $this->db->order_by('self.id','desc');
        $q = $this->db->get_where( TBL_BILL_TEMPLESHOP_SALES . ' AS self', $where );
        return $q->result_array();
    }
    
    public function ajax() {
        $post = $this->input->post();
       
        $where['self.service_id'] = $post['service_type'];
        $where['self.status'] = 1;
        
        //if( !empty( $post['search_by'] ) ) :
            if($post['search_by'] == 1) {
                $where['self.devotee_name1'] = $post['devotee_name']; 
            } else if($post['search_by'] == 0) {
                $where['self.contact_no'] = $post['contact_no'];
            }
       //endif;

        $results = $this->get_all( '', $where );
        
        
        return $results;
    }
    
    public function get_prasadam_price($id) {
        $this->db->where('id',$id);
        return $this->db->get($this->prasadam)->row();        
    }
    
    public function get_paidID($menu,$type,$id) {
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('pay_type_id',$id);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->result();
    }
    
    public function delete_payment_by_paid_id($id) {
        $this->db->where('id',$id);
        $this->db->delete($this->payment);
        return $this->db->affected_rows();
    }
    
    public function get_deity_name($id) {
        $this->db->where('id',$id);
        return $this->db->get(TBL_GOD_NAME)->row();        
    }
	
	public function get_payment_date( $bt_id ) {
        $where['self.pay_ref_type'] = 19; // Billing Kattalai Archanai
        $where['self.pay_ref_id'] = $bt_id;
        $where['self.status'] = 1;        
        $q = $this->db->get_where( TBL_PAY . ' AS self', $where );
        return $q->result_array();
    }
}
?>