<?php

class Voucher_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function validate_voucher($voucherno) {
        $now = date('Y-m-d H:i:s');
        $this->db->select("*");
        $this->db->join('customers c', 'ov.customer_id = c.id_customers');
        $this->db->where('ov.voucher_number', $voucherno);
        $this->db->where('ov.voucher_status', 'open');
        $this->db->where('ov.valid_until > ', $now);
        $this->db->where('ov.remaining_amount > ', 0);
       // $this->db->where('ov.business_id', $this->session->userdata('businessid'));
       // $this->db->where('c.business_id', $this->session->userdata('businessid'));
        $query = $this->db->get('order_vouchers ov');
        return $query->result();
    }

    function update_remaining_amount($voucherno, $remaining, $remaining_services) {
        $this->db->where('voucher_number', $voucherno);
        $query = $this->db->update('order_vouchers', array(
            'remaining_amount' => $remaining,
            'remaining_service_ids' => $remaining_services,
            'voucher_status' => $remaining > 0 ? 'open' : 'closed'
        ));
        return $query;
    }

    //Voucher List view function.....
    function voucher_list() {
        $this->db->select("c.customer_name,v.id_order_vouchers,v.voucher_number,v.type,v.service_names,v.amount,date_format(v.voucher_date,'%d-%m-%Y') as date, v.voucher_date, date_format(v.valid_until,'%d-%m-%Y') as valid_date,v.voucher_status,v.remaining_amount");
        $this->db->join('customers c', 'c.id_customers = v.customer_id');
        $this->db->where('v.business_id', $this->session->userdata('businessid'));
        $query = $this->db->get('order_vouchers v');
        return $query->result_array();
    }

    
     function gettodayvouchers(){
        
        $today = date('Y-m-d');
        $tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
        $this->db->select("c.customer_name,v.id_order_vouchers,v.voucher_number,v.type,v.service_names,date_format(v.voucher_date,'%d-%m-%Y') as date, v.voucher_date, v.amount,date_format(v.voucher_date,'%d-%m-%Y') as date,date_format(v.valid_until,'%d-%m-%Y') as valid_date,v.voucher_status,v.remaining_amount");
        $this->db->join('customers c', 'c.id_customers = v.customer_id');
        $this->db->where('v.business_id', $this->session->userdata('businessid'));
        $this->db->where('v.voucher_date >=', $today);
        $this->db->where('v.voucher_date <', $tomorrow);
        $query = $this->db->get('order_vouchers v');
        
        return $query->result_array();
    }
    
    
    function getdayvouchers($today){
        
        $tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
        $this->db->select("c.customer_name,v.id_order_vouchers,v.voucher_number,v.type,v.service_names,v.amount,date_format(v.voucher_date,'%d-%m-%Y') as date,date_format(v.valid_until,'%d-%m-%Y') as valid_date,v.voucher_status,v.remaining_amount");
        $this->db->join('customers c', 'c.id_customers = v.customer_id');
        $this->db->where('v.business_id', $this->session->userdata('businessid'));
        $this->db->where('date_format(v.voucher_date,"%Y-%m-%d") =', $today, False);
        
        $query = $this->db->get('order_vouchers v');
        
        return $query->result_array();
    }
    
    
    //Voucher update function....
    function voucher_update() {
        
        $this->db->select('ifnull(remaining_amount,0) as remaining_amount, voucher_status, valid_until, now() as today', false);
        $this->db->where('id_order_vouchers', $this->input->post('id_order_vouchers', TRUE));
        $query1 = $this->db->get('order_vouchers');
        
        $voucher = $query1->row();
        
        $voucher_status = $voucher->voucher_status;
        $valid_until = $voucher->valid_until;
        $remaining_amount = $voucher->remaining_amount;
        $current_date = $voucher->today;
        
        
        if($remaining_amount>0 && date($this->input->post('valid_until', TRUE) . ' 23:59:59') > date($current_date)){            
            $voucher_status = 'open';            
        }else{
            $voucher_status = 'closed';    
        }
        
        $data = array(
            'valid_until' => $this->input->post('valid_until', TRUE) . ' 23:59:59',
            'voucher_status' => $voucher_status
        );
        
        $this->db->where('id_order_vouchers', $this->input->post('id_order_vouchers', TRUE));
        $query = $this->db->update('order_vouchers', $data);
        return $query;
    }

    function voucher_super_update() {
        
        $data = array(
            'voucher_date' => $this->input->post('voucher_date', TRUE),
            'valid_until' => $this->input->post('valid_until', TRUE) . ' 23:59:59',
            'voucher_status' => $this->input->post('voucher_status'),
            'remaining_amount' => $this->input->post('remaining_amount')
        );
        
        $this->db->where('id_order_vouchers', $this->input->post('txtvoucherid', TRUE));
        $query = $this->db->update('order_vouchers', $data);
        return $query;
    }
    
    //Voucher delete view function...
    function delete_voucher($id) {
        $this->db->where('id_order_vouchers', $id);
        $result = $this->db->delete('order_vouchers');
        
        
        if ($result) {
            
            $this->db->where('order_voucher_id', $id);
            $this->db->set('voucher_status','Cancelled');
            $this->db->update('account_vouchers');
            
            return TRUE;
        }
    }

    function voucher_usage($voucher_number){
        
        $sql = "SELECT id_order_vouchers, voucher_number, order_vouchers.amount, order_vouchers.remaining_amount, 
                id_invoice, invoice.customer_name, date_format(invoice_date,'%d-%m-%Y') as invoicedate, invoice.paid_voucher 
                FROM order_vouchers left 
                join invoice on invoice.instrument_number COLLATE utf8_unicode_ci = order_vouchers.voucher_number 
                where order_vouchers.voucher_number='".$voucher_number."'";
        
        $query = $this->db->query($sql);
        
        return $query->result_array();
        
    }
    
}
