<?php

class Superuser_model extends CI_Model {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }
    
    function getsupervisordayinvoices($today){
        
        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
        
        $this->db->select("*", False);
        $this->db->where('invoice.business_id', $this->session->userdata('businessid'));
        $this->db->where('date_format(invoice_date , "%Y-%m-%d") =', $today);
        $query = $this->db->get('invoice');
              
        return $query->result_array();
    }
    
    function get_invoice_for_edit($id){
        
        $this->db->select("*");
        $this->db->where('invoice.id_invoice', $id);
        $query = $this->db->get('invoice');
             
        return $query->row();
    }
    
    function update_super_invoice(){
       
        $this->db->where('id_invoice', $this->input->post('id_invoice'));
       
        $data = array(
            'invoice_date' => $this->input->post('invoice_date', TRUE),
            'invoice_number' => $this->input->post('invoice_number', TRUE),
            'invoice_type' => $this->input->post('invoice_type', TRUE),
            'invoice_status' => $this->input->post('invoice_status', TRUE),
            'customer_id' => $this->input->post('customer_id', TRUE),
            'customer_name' => $this->input->post('customer_name', TRUE),
            'customer_cell' => $this->input->post('customer_cell', TRUE),
            'reference_invoice_number' => $this->input->post('reference_invoice_number', TRUE),
            'visit_id' => $this->input->post('visit_id', TRUE),
            'visit_time' => $this->input->post('visit_time', TRUE),
            'sub_total' => $this->input->post('sub_total', TRUE),
            'gross_amount' => $this->input->post('gross_amount', TRUE),
            'tax_total' => $this->input->post('tax_total', TRUE),
            'cc_charge' => $this->input->post('cc_charge', TRUE),
            'discount' => $this->input->post('discount'),
            'cctip' => $this->input->post('cctip', TRUE),
            'other_charges' => $this->input->post('other_charges', TRUE),
            'cc_charge' => $this->input->post('cc_charge', TRUE),
            'net_amount' => $this->input->post('net_amount', TRUE),
            'advance_amount' => $this->input->post('advance_amount', TRUE),            
            'advance_inst' => $this->input->post('advance_inst', TRUE),
            'total_payable' => $this->input->post('total_payable', TRUE),
            'retained_used' => $this->input->post('retained_used'),
            'retained_amount_used' => $this->input->post('retained_amount_used', TRUE),
            'loyalty_used' => $this->input->post('loyalty_used', TRUE),
            'payment_mode' => $this->input->post('payment_mode', TRUE),
            'paid_amount' => $this->input->post('paid_amount', TRUE),
            'paid_cash' => $this->input->post('paid_cash', TRUE),
            'paid_card' => $this->input->post('paid_card', TRUE),
            'paid_check' => $this->input->post('paid_check', TRUE),
            'paid_voucher' => $this->input->post('paid_voucher', TRUE),
            'balance' => $this->input->post('balance'),
            'returnamount' => $this->input->post('returnamount', TRUE),
            'loyalty_earned' => $this->input->post('loyalty_earned', TRUE),
            'retained_amount' => $this->input->post('retained_amount', TRUE)
        );
        $this->db->update('invoice', $data);
        $affected =  $this->db->affected_rows();
        
        //update visit status
        if($this->input->post('invoice_status', TRUE)=='valid'){
            if($this->input->post('invoice_type', TRUE)=='service'){
                $this->db->where('id_customer_visits',$this->input->post('visit_id', TRUE));
                $this->db->set('visit_status','invoiced');
                $this->db->update('customer_visits');
                //change account voucher status
                $this->db->where('invoice_id', $this->input->post('id_invoice'));
                $this->db->update('account_vouchers', array('voucher_status' => 'Active'));
            } else {
                $this->db->where('id_customer_order',$this->input->post('visit_id', TRUE));
                $this->db->set('order_status','invoiced');
                $this->db->update('customer_orders');
                
                //change account voucher status
                $this->db->where('invoice_id', $this->input->post('id_invoice'));
                $this->db->where('business_partner_id', $this->input->post('customer_id', TRUE));
                $this->db->update('account_vouchers', array('voucher_status' => 'Active'));
                
            }
            
        }else{
            if($this->input->post('invoice_type', TRUE)=='service'){
                if($this->input->post('reference_invoice_number')==''){
                    $this->db->where('id_customer_visits',$this->input->post('visit_id', TRUE));
                    $this->db->set('visit_status','open');
                    $this->db->update('customer_visits');
                } else {
                    $r=explode('-',$this->input->post('reference_invoice_number'));
                    $this->db->where('id_invoice',$r[2]);
                    $this->db->set('is_recovery','yes');
                    $this->db->update('invoice');
                }
            } else {
                if($this->input->post('reference_invoice_number')==''){
                    $this->db->where('id_customer_order',$this->input->post('visit_id', TRUE));
                    $this->db->set('order_status','open');
                    $this->db->update('customer_orders');
                } else {
                    $r=explode('-',$this->input->post('reference_invoice_number'));
                    $this->db->where('id_invoice',$r[2]);
                    $this->db->set('is_recovery','yes');
                    $this->db->update('invoice');
                }
            }
            //change account voucher status
            $this->db->where('invoice_id', $this->input->post('id_invoice'));
            $this->db->where('business_partner_id', $this->input->post('customer_id', TRUE));
            $this->db->update('account_vouchers', array('voucher_status' => 'cancelled', 'cancelled_by' => $this->session->userdata('username'), 'cancelled_on' => date('Y-m-d H:i:s')));
             
        }
        
        $this->db->where('invoice_id',$this->input->post('id_invoice', TRUE));
        $this->db->where('business_partner_id', $this->input->post('customer_id', TRUE));
        $this->db->set('voucher_date', $this->input->post('invoice_date', TRUE));
        $this->db->update('account_vouchers');
        
        
        ///update invoice product date
        $this->db->where('invoice_id',$this->input->post('id_invoice', TRUE));
        $this->db->set('invoice_product_date', $this->input->post('invoice_date', TRUE));
        $this->db->update('invoice_products');
        
        
        if($this->input->post('invoice_status', TRUE)!=='valid'){
                   
        }
        
        return $affected;
    }

    
     function getsupervisordayvisits($today){
        
        $this->db->select("*, sum(visit_advance.advance_amount) advance_amount, date_format(visit_services.visit_service_start, '%Y-%m-%d %H:%i:%s') as 'visitdate'", False);
        $this->db->join("visit_services", "visit_services.customer_visit_id = customer_visits.id_customer_visits and visit_service_status='Active'");
        $this->db->join("customers", "customer_visits.customer_id=customers.id_customers");
        $this->db->join("visit_advance", "visit_advance.customer_visit_id=customer_visits.id_customer_visits","left");
        $this->db->where('customer_visits.business_id', $this->session->userdata('businessid'));
        $this->db->where('date_format(visit_services.visit_service_start, "%Y-%m-%d") =', $today);
        $this->db->group_by("id_customer_visits");
        $query = $this->db->get('customer_visits');
              
        return $query->result_array();
    }
    
    
    function get_visit_for_edit($id){
        
        $this->db->select("*");
        $this->db->join("customers", "customer_visits.customer_id=customers.id_customers");
        $this->db->where('customer_visits.id_customer_visits', $id);
        $query = $this->db->get('customer_visits');
             
        return $query->row();
    }
    
    function update_super_visit(){
      
        $this->db->where('id_customer_visits', $this->input->post('id_customer_visits'));
        $data = array(
            'customer_id'  => $this->input->post('customer_id', TRUE),
            
            'visit_status' => $this->input->post('visit_status', TRUE),
            'advance_amount' => $this->input->post('advance_amount', TRUE),
            'advance_date' => $this->input->post('advance_date', TRUE),
            'advance_mode' => $this->input->post('advance_mode', TRUE),
            'advance_inst' => $this->input->post('advance_inst', TRUE)
        );
        $this->db->update('customer_visits', $data);
        return $this->db->affected_rows();                
    }
    
    function update_super_advance(){
        $this->db->where('id_visit_advance', $this->input->post('id_visit_advance'));
        $data = array(
            
            'advance_amount' => $this->input->post('advance_amount', TRUE),
            'advance_date' => $this->input->post('advance_date', TRUE),
            'advance_mode' => $this->input->post('advance_mode', TRUE),
            'advance_inst' => $this->input->post('advance_inst', TRUE),
            'advance_remarks' => $this->input->post('advance_remarks', TRUE),
            'advance_cc_charge' => $this->input->post('advance_cc_charge', TRUE),
        );
        $this->db->update('visit_advance', $data);
        $affected =  $this->db->affected_rows();
        
        //change account voucher status
        $this->db->where('visit_advance_id',  $this->input->post('id_visit_advance'));
        $this->db->update('account_vouchers', array('voucher_date' => $this->input->post('advance_date', TRUE)));
        
        return $affected;
    }
    
    function update_event_mapping(){
       
        $this->db->where('id_account_event_mapping', $this->input->post('txtideventmapping'));
        
        $data = array(
            'account_head_id'=>$this->input->post('txtidaccounthead'),
            'description'=>$this->input->post('txtdescription'),
            'transaction_type'=>$this->input->post('txttransactiontype')
            
        );
        $this->db->update('account_event_mapping', $data);
        return $this->db->affected_rows();
    }
     function insert_event_mapping(){
       
        $data = array(
            'account_event_id'=>$this->input->post('id_account_events'),
            'account_head_id'=>$this->input->post('account_head'),
            'description'=>$this->input->post('desctiption'),
            'transaction_type'=>$this->input->post('transaction_type'),
            'entity_name'=>$this->input->post('entity_name'),
            'business_id'=>$this->session->userdata('businessid'),
            'mapped_by'=>$this->session->userdata('userid')
            
        );
        $this->db->insert('account_event_mapping', $data);
        return $this->db->insert_id();
    }
    
    function insert_account_event(){
         $data = array(
            
            'account_event'=>$this->input->post('account_event')
            
        );
        $this->db->insert('account_events', $data);
        return $this->db->insert_id();
    }
    
    function update_account_event(){
        
    }
    function download_customers($businessid=null){
        
        $this->db->select('customer_cell, replace(replace(replace(customer_name,"%",""),"@",""),",","") as customer_name, customer_email');
        $this->db->where('length(customer_cell)',11);
        $this->db->where('left(customer_cell,2)','03',false);
        
        if(null !== $businessid){
            
            $this->db->where('business_id', $businessid);
            
        }
        
        $this->db->group_by('customer_cell');
        $query = $this->db->get('customers');
        
        return $query->result_array();
        
        
    }
    
    function reopencashregister($idcashregister){
        if($idcashregister>0){
            //change account voucher status
           $this->db->where('cashregister_id', $idcashregister);
           $this->db->update('account_vouchers', array('voucher_status' => 'cancelled', 'cancelled_by' => $this->session->userdata('username'), 'cancelled_on' => date('Y-m-d H:i:s')));

           $this->db->set('register_status', 'open');
           $this->db->where('id_cash_register', $idcashregister);
           $this->db->update('cash_register');
        }
        return $this->db->affected_rows();
        
    }
    
    function update_business_details(){
        
      //exit();
        if ($this->db->field_exists('business_name', 'business')) {$this->db->set('business_name', $this->input->post('business_name'));}
        if ($this->db->field_exists('loyalty_enable', 'business')) {$this->db->set('loyalty_enable', $this->input->post('loyalty_enable'));}
        if ($this->db->field_exists('s_loyalty', 'business')) {$this->db->set('s_loyalty', $this->input->post('s_loyalty'));}
        if ($this->db->field_exists('r_loyalty_enable', 'business')) {$this->db->set('r_loyalty_enable', $this->input->post('r_loyalty_enable'));}
        if ($this->db->field_exists('l_point_value', 'business')) {$this->db->set('l_point_value' , $this->input->post('l_point_value'));}
        if ($this->db->field_exists('l_point_discount', 'business')) {$this->db->set('l_point_discount', $this->input->post('l_point_discount'));}
        if ($this->db->field_exists('loyalty_sms', 'business')) {$this->db->set('loyalty_sms', $this->input->post('loyalty_sms'));}
        if ($this->db->field_exists('show_amount', 'business')) {$this->db->set('show_amount', $this->input->post('show_amount'));}
        if ($this->db->field_exists('show_points', 'business')) {$this->db->set('show_points', $this->input->post('show_points'));}
        if ($this->db->field_exists('admin_notifications', 'business')) {$this->db->set('admin_notifications', $this->input->post('admin_notifications'));}
        if ($this->db->field_exists('show_staff', 'business')) {$this->db->set('show_staff', $this->input->post('show_staff'));}
        if ($this->db->field_exists('invoicenumber', 'business')) {$this->db->set('invoicenumber', $this->input->post('invoicenumber'));}
        if ($this->db->field_exists('invoice_terms', 'business')) {$this->db->set('invoice_terms', $this->input->post('invoice_terms'));}
        if ($this->db->field_exists('payment_terms', 'business')) {$this->db->set('payment_terms', $this->input->post('payment_terms'));}
        if ($this->db->field_exists('fineprint', 'business')) {$this->db->set('fineprint', $this->input->post('fineprint'));}
        
        if ($this->db->field_exists('till_closing', 'business')) {$this->db->set('till_closing', $this->input->post('till_closing'));}
        if ($this->db->field_exists('b_switch', 'business')) {$this->db->set('b_switch', $this->input->post('b_switch'));}
        if ($this->db->field_exists('gym', 'business')) {$this->db->set('gym', $this->input->post('gym'));}
        if ($this->db->field_exists('programs', 'business')) {$this->db->set('programs', $this->input->post('programs'));}
        if ($this->db->field_exists('training', 'business')) {$this->db->set('training', $this->input->post('training'));}
        if ($this->db->field_exists('ho', 'business')) {$this->db->set('ho', $this->input->post('ho'));}
        if ($this->db->field_exists('common_products', 'business')) {$this->db->set('common_products', $this->input->post('common_products'));}
        if ($this->db->field_exists('auto_vouchers', 'business')) {$this->db->set('auto_vouchers',$this->input->post('auto_vouchers'));}
        if ($this->db->field_exists('tax_mode', 'business')) {$this->db->set('tax_mode', $this->input->post('tax_mode'));}
        if ($this->db->field_exists('hide_cell', 'business')) {$this->db->set('hide_cell', $this->input->post('hide_cell'));}
        if ($this->db->field_exists('loyalty_mode', 'business')) {$this->db->set('loyalty_mode', $this->input->post('loyalty_mode'));}
        if ($this->db->field_exists('discount_pw', 'business')) {$this->db->set('discount_pw', $this->input->post('discount_pw'));}
        
        if ($this->db->field_exists('allow_invoice_cancel', 'business')) {$this->db->set('allow_invoice_cancel', $this->input->post('allow_invoice_cancel'));}
        if ($this->db->field_exists('allow_balance', 'business')) {$this->db->set('allow_balance', $this->input->post('allow_balance'));}
        
        $this->db->where('id_business', $this->input->post('id_business'));
        $this->db->update('business');
        
        return $this->db->affected_rows();
    }
    
}
