<?php
class General_Settings_Model extends CI_Model {
    public function __construct(){
        parent::__construct();
        $this->settings = TBL_SETTINGS_GENERAL;
        $this->bill_prasadam = TBL_BP;
        $this->bill_prasadam_items = TBL_BPI;
        $this->bill_payment = TBL_PAY;
        $this->logs = TBL_LOGIN;
        $this->star = TBL_STAR;
        $this->god = TBL_GOD_NAME;
        $this->prasadam = TBL_PRASADAM;
        $this->payment_type = TBL_PAY_TYPE;
        $this->service = TBL_SERVICE;
        $this->service_menu = TBL_SERVICE_MENU;
    }
    public function insert_bill_prasadam($data){
        $this->db->insert($this->bill_prasadam,$data);
        return $this->db->insert_id();
    }
    public function update_bill_prasadam($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->bill_prasadam, $data);
        return $this->db->affected_rows();
    }
    public function insert_bill_prasadam_items($data){
        $this->db->insert($this->bill_prasadam_items,$data);
        return $this->db->insert_id();
    }
    public function set_bill_items_inactive($id, $data){
        $this->db->where('bp_id', $id);
        $this->db->update($this->bill_prasadam_items, $data);
        return $this->db->affected_rows();
    }
    public function insert_bill_payment($data){
        $this->db->insert($this->bill_payment,$data);
        return $this->db->insert_id();
    }
    public function set_bill_payment_inactive($id, $data){
        $this->db->where('pay_ref_id', $id);
        $this->db->update($this->bill_payment, $data);
        return $this->db->affected_rows();
    }
    public function insert_logs($data){
        $this->db->insert($this->logs,$data);
        return $this->db->insert_id();
    }
    public function last_sync_history(){
        $this->db->where('menu_type', 8);
        $this->db->where('page_type_id', 8);
        $this->db->order_by('id', 'desc');
        $this->db->limit('1');
        $resource = $this->db->get($this->logs);
        return $resource->row();
    }   
    public function check_id_exists($id){
        $this->db->select('id,sync_id,service_id,bp_rec_no');
        $this->db->where('sync_id', $id);
        $resource = $this->db->get($this->bill_prasadam);
        return $resource->row();
    }     
    public function get_last_receipt_bill($id){
        $this->db->select('service_id,bp_rec_no');
        $this->db->where('service_id', $id);
        $this->db->order_by('id', 'desc');
        $this->db->limit('1');
        $resource = $this->db->get($this->bill_prasadam);
        return $resource->row();
    }     
    public function get_by_id() {
        $resource = $this->db->get($this->settings);
        return $resource->row();
    }    
    public function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->settings, $data);
        return $this->db->affected_rows();
    }
    public function get_stars($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->star);
        return $resource->result();
    }
    public function get_god_names($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->god);
        return $resource->result();
    }
    public function get_prasadam($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->prasadam);
        return $resource->result();
    }
    public function get_payment_type($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->payment_type);
        return $resource->result();
    }
    public function get_service($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->service);
        return $resource->result();
    }
    public function get_service_menus($last_date=NULL){
        if(!empty($last_date)){
            $this->db->where('updated_on > "'.$last_date.'" AND updated_on !=NULL ');   
        }
        #$this->db->where('status',1);
        $resource = $this->db->get($this->service_menu);
        return $resource->result();
    }
    
    function manipulate(){
        $post = $this->input->post();
        
        //echo '<pre>'; print_r($post); die;
        
        // Check if slug already exists
        $this->db->select('id');
        $this->db->limit(1);
        $q = $this->db->get_where( TBL_BILL_SETT, array('slug' => 'alt_url') );
        $is_new = empty( $q->row_array() ) ? TRUE : FALSE;
        $status = $post['alt_chk'] == 'on' ? '1' : '0';
        
        $args = array(
            'alt_url'   => 'Alt. App URL', 
            'alt_host'  => 'Alt. DB Host',
            'alt_user'  => 'Alt. DB User',
            'alt_pass'  => 'Alt. DB Password',
            'alt_db'    => 'Alt. DB Name',
            
            'server_down'=> 'Main server is Down/Not',
            //'alt_count' => 'Import Count',
        );
        
        foreach( $args as $slug => $name ) :
            
            $a = array(
                'slug'      => $slug,
                'value'     => $post[$slug],
                'status'    => $status, 
            );
        
            if( $is_new ) :
                // Insert
                $a['name']       = $name;
                $a['created_on'] = $date_now;
                $a['created_by'] = $user_data['user_id'];
                
                $this->db->insert( TBL_BILL_SETT, $a );
            else :
                // Update
                $a['updated_on'] = $date_now;
                $a['updated_by'] = $user_data['user_id'];
                
                $this->db->update( TBL_BILL_SETT, $a, array('slug' => $slug) );
            endif;
            
            
        endforeach;
        
        
        
        
    }
    
    public function reset_service_gst() {
        $gst = 1;
        $data = array('gst' => 0);
        $this->db->where('gst',$gst);
        $this->db->update(TBL_SERVICE, $data);
        return $this->db->affected_rows();
    }
    
    public function truncate_billing_table() {       
        /* $query = 'SELECT concat("TRUNCATE TABLE trun_tiru.", ' . TABLE_NAME .', ";") AS trunTable FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = "trun_tiru" AND TABLE_NAME LIKE "thiru_bill_%"'; */
           
        $query = 'SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = "' . $this->db->database . '" AND table_name LIKE "thiru_bill_%"';
        
        $result = $this->db->query($query)->result();
        foreach($result as $value) {
            if($value->table_name != 'thiru_bill_settings' || $value->table_name != 'thiru_bill_devotees') {
                $this->db->query('SET FOREIGN_KEY_CHECKS = 0;');
                $this->db->query('TRUNCATE TABLE ' . $value->table_name .';');
                $this->db->query('SET FOREIGN_KEY_CHECKS = 1;');
            }
        }
        
        $tables = array(
            '0' => 'glb_service',
            '1' => 'tallytransaction',
            '2' => 'thiru_bar_items',
            '3' => 'thiru_ba_abishegam',
            //'4' => 'thiru_bd_items',
            '5' => 'thiru_bm_items',
            '6' => 'thiru_bp_items',
            '7' => 'thiru_inventory_category',
            '8' => 'thiru_inventory_general',
            '9' => 'thiru_inventory_gold_silver',
            '10' => 'thiru_inventory_issued',
            '11' => 'thiru_inventory_item',
            '12' => 'thiru_inventory_log',
            '13' => 'thiru_inventory_sales',
            '14' => 'thiru_inventory_temple',
            '15' => 'thiru_log_details',
            '16' => 'thiru_opening_balance_details',
            '17' => 'thiru_payments',
            '18' => 'thiru_po_items',
            '19' => 'thiru_purchase_entry',
            '20' => 'thiru_sms_types',
            '21' => 'thiru_statue_books',
            '22' => 'thiru_student_form',
            '23' => 'thiru_form_downloads',
        );
        
        foreach($tables as $value) {
            $this->db->query('SET FOREIGN_KEY_CHECKS = 0;');
            $this->db->query('TRUNCATE TABLE ' . $value .';');
            $this->db->query('SET FOREIGN_KEY_CHECKS = 1;');
        }
        return true;
    }
    
}
?>
