<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reports extends CI_Controller {
    public $start = '';
    public $end = '';
    public $state_array = '';
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) && ! $this->session->userdata ( 'user_logged_in' )) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) );

        $this->start = date('Y-m').'-01';
        $this->end = date('Y-m-t');

        $this->load->model('User_model');
        $this->load->model('Item_model');
        $this->load->model('Reports_model');

        $this->state_array = array('-','Jammu & Kashmir','Himachal Pradesh','Punjab','Chandigarh','Uttarakhand','Haryana','Delhi','Rajasthan','Uttar Pradesh','Bihar','Sikkim','Arunachal Pradesh','Nagaland','Manipur','Mizoram','Tripura','Meghalaya','Assam','West Bengal','Jharkhand','Odisha','Chhattisgarh','Madhya Pradesh','Gujarat','Daman & Diu','Dadra & Nagar Haveli','Maharashtra','Karnataka','Goa','Lakshdweep','Kerala','Tamil Nadu','Pondicherry','Andaman & Nicobar Islands','Telengana','Andhra Pradesh','Other Territory');
    }

    public function b2b(){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['report'] = $this->Reports_model->generate_b2b(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))));
        //echo $this->db->last_query();die();
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['GSTIN/UIN of Recipient'] = $value->gstn;
                        $csv_arr[$key]['Invoice Number'] = $value->invoice_no;
                        $csv_arr[$key]['Invoice date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice Value'] = $value->total;
                        $csv_arr[$key]['Place Of Supply'] = $value->shipping_state.'-'.$this->state_array[$value->shipping_state];
                        $csv_arr[$key]['Reverse Charge'] = 0.00;
                        $csv_arr[$key]['Invoice Type'] = 'Rgular';
                        $csv_arr[$key]['E-Commerce GSTIN'] = 'NA';
                        $csv_arr[$key]['Rate'] = $value->gst_rate;
                        $csv_arr[$key]['Taxable Value'] = $value->taxable_amount;
                        $csv_arr[$key]['Cess Amount'] = 0.00;
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('b2b_report',$data);
        }
    }

    public function b2cl(){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['report'] = $this->Reports_model->generate_b2cl(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))));
        //echo $this->db->last_query();die();
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        //$csv_arr[$key]['GSTIN/UIN of Recipient'] = $value->gstn;
                        $csv_arr[$key]['Invoice Number'] = $value->invoice_no;
                        $csv_arr[$key]['Invoice date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice Value'] = $value->total;
                        $csv_arr[$key]['Place Of Supply'] = $value->shipping_state.'-'.$this->state_array[$value->shipping_state];
                        //$csv_arr[$key]['Reverse Charge'] = 0.00;
                        //$csv_arr[$key]['Invoice Type'] = 'Rgular';
                        $csv_arr[$key]['Rate'] = $value->gst_rate;
                        $csv_arr[$key]['Taxable Value'] = ($value->taxable_amount);
                        $csv_arr[$key]['Cess Amount'] = 0.00;
                        $csv_arr[$key]['E-Commerce GSTIN'] = '';
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('b2cl_report',$data);
        }
    }

    public function b2cs(){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['report'] = $this->Reports_model->generate_b2cs(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))));
        //echo $this->db->last_query();die();
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        //$csv_arr[$key]['GSTIN/UIN of Recipient'] = $value->gstn;
                        $csv_arr[$key]['Invoice Number'] = $value->invoice_no;
                        $csv_arr[$key]['Invoice date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice Value'] = $value->total;
                        $csv_arr[$key]['Place Of Supply'] = $value->shipping_state.'-'.$this->state_array[$value->shipping_state];
                        //$csv_arr[$key]['Reverse Charge'] = 0.00;
                        //$csv_arr[$key]['Invoice Type'] = 'Rgular';
                        $csv_arr[$key]['Rate'] = $value->gst_rate;
                        $csv_arr[$key]['Taxable Value'] = ($value->taxable_amount);
                        $csv_arr[$key]['Cess Amount'] = 0.00;
                        $csv_arr[$key]['E-Commerce GSTIN'] = '';
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('b2cs_report',$data);
        }
    }

    public function purchase(){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['report'] = $this->Reports_model->generate_purchase(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))));
        // echo $this->db->last_query();die();
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['GSTIN/UIN of Recipient'] = $value->gstn;
                        $csv_arr[$key]['Bill Number'] = $value->bill_no;
                        $csv_arr[$key]['Vendor Name'] = $value->contact_display_name;
                        $csv_arr[$key]['Bill date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->bill_date)));
                        $csv_arr[$key]['Bill Value'] = $value->total;
                        $csv_arr[$key]['Place Of Supply'] = $value->shipping_state.'-'.$this->state_array[$value->shipping_state];
                        //$csv_arr[$key]['Reverse Charge'] = 0.00;
                        //$csv_arr[$key]['Bill Type'] = 'Rgular';
                        $csv_arr[$key]['Rate'] = 3;
                        $csv_arr[$key]['Taxable Value'] = ($value->total - $value->sub_total);
                        $csv_arr[$key]['Cess Amount'] = 0.00;
                        $csv_arr[$key]['E-Commerce GSTIN'] = '';
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('purchase_report',$data);
        }
    }

    public function customer_sale(){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['customer'] = $this->input->post('customer'); 

        $where = array('contact_type' => 1);
        $data['customer_list'] = $this->User_model->user_list_where($where);

        $data['report'] = $this->Reports_model->generate_customer_sales(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))),$data['customer']);

        if (count($data['report']) > 0) {
            foreach ($data['report'] as $key => $value) {
                 if ($value->status == 1) {
                    if (strtotime($value->due_date) < strtotime(date('Y-m-d'))) {
                        if (($value->total - $value->payment) == 0) {
                            $status = "Paid";
                        }else{
                            $status = "Over Due";
                        }
                    } else {
                        if (($value->total - $value->payment) == 0) {
                            $status = "Paid";
                        }else{
                            $status = "Due";
                        }
                    }
                } else {
                    $status = "Draft";
                }
                $data['report'][$key]->status_text = $status;
                $data['report'][$key]->payment = !empty($value->payment) ? $value->payment : 0;
            }
        }

        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice#'] = $value->invoice_no;
                        $csv_arr[$key]['Order No.'] = $value->order_no;
                        $csv_arr[$key]['Customer'] = $value->name;
                        $csv_arr[$key]['Status'] = $status_text;
                        $csv_arr[$key]['Due Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->due_date)));
                        $csv_arr[$key]['Amount'] = $value->total;
                        $csv_arr[$key]['Balance Due'] = ($value->total - $value->payment);
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('customer_sales',$data);
        }
    }

    public function item_sales (){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['item'] = $this->input->post('item'); 

        $data['item_list'] = $this->Item_model->item_list();

        $data['report'] = $this->Reports_model->generate_item_sales(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))),$data['item']);
        
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice#'] = $value->invoice_no;
                        $csv_arr[$key]['Order No.'] = $value->order_no;
                        $csv_arr[$key]['Customer'] = $value->name;
                        $csv_arr[$key]['Status'] = $status_text;
                        $csv_arr[$key]['Due Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->due_date)));
                        $csv_arr[$key]['Amount'] = $value->total;
                        $csv_arr[$key]['Balance Due'] = ($value->total - $value->payment);
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('item_sales',$data);
        }
    }

    public function customer_balance (){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['customer'] = $this->input->post('customer'); 

        $where = array('contact_type' => 1);
        $data['customer_list'] = $this->User_model->user_list_where($where);

        $data['invoice_report'] = $this->Reports_model->customer_invoice(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))),$data['customer']);
        
        $data['payment_report'] = $this->Reports_model->customer_payment(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))),$data['customer']);
        // echo "<pre>";
        // print_r($data['invoice_report']);
        // print_r($data['payment_report']);
        // die();
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice#'] = $value->invoice_no;
                        $csv_arr[$key]['Order No.'] = $value->order_no;
                        $csv_arr[$key]['Customer'] = $value->name;
                        $csv_arr[$key]['Status'] = $status_text;
                        $csv_arr[$key]['Due Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->due_date)));
                        $csv_arr[$key]['Amount'] = $value->total;
                        $csv_arr[$key]['Balance Due'] = ($value->total - $value->payment);
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data);
            }
        } else {
            $this->load->view('customer_balance',$data);
        }
    }

    public function customer_balance_old (){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 
        $data['customer'] = $this->input->post('customer'); 

        $where = array('contact_type' => 1);
        $data['customer_list'] = $this->User_model->user_list_where($where);

        $data['report'] = $this->Reports_model->generate_customer_balance(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))),$data['customer']);
        // echo $this->db->last_query();die();
        
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice#'] = $value->invoice_no;
                        $csv_arr[$key]['Order No.'] = $value->order_no;
                        $csv_arr[$key]['Customer'] = $value->name;
                        $csv_arr[$key]['Status'] = $status_text;
                        $csv_arr[$key]['Due Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->due_date)));
                        $csv_arr[$key]['Amount'] = $value->total;
                        $csv_arr[$key]['Balance Due'] = ($value->total - $value->payment);
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('customer_balance',$data);
        }
    }

    public function invoice_details (){
        $data['start_date'] = !empty($this->input->post('from')) ? $this->input->post('from') : $this->start; 
        $data['end_date'] = !empty($this->input->post('to')) ? $this->input->post('to') : $this->end; 
        $data['data'] = $this->input->post('data'); 
        $data['excel'] = $this->input->post('excel'); 

        $data['report'] = $this->Reports_model->invoice_details(date('Y-m-d',strtotime(str_replace('/', '-', $data['start_date']))),date('Y-m-d',strtotime(str_replace('/', '-', $data['end_date']))));
        // echo $this->db->last_query();die();
        
        if ($data['data'] == 'json') {
            if (!empty($data['excel']) && ($data['excel'] ==1)) {
                $csv_arr = array();
                if (count($data['report']) > 0) {
                    foreach ($data['report'] as $key => $value) {
                        $csv_arr[$key]['Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->invoice_date)));
                        $csv_arr[$key]['Invoice#'] = $value->invoice_no;
                        $csv_arr[$key]['Order No.'] = $value->order_no;
                        $csv_arr[$key]['Customer'] = $value->name;
                        $csv_arr[$key]['Status'] = $status_text;
                        $csv_arr[$key]['Due Date'] = date('d/m/Y',strtotime(str_replace('-', '/', $value->due_date)));
                        $csv_arr[$key]['Amount'] = $value->total;
                        $csv_arr[$key]['Balance Due'] = ($value->total - $value->payment);
                    }
                }
                echo json_encode($csv_arr);
            } else {
                echo json_encode($data['report']);
            }
        } else {
            $this->load->view('invoice_details',$data);
        }
    }
}?>