<?php

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

class Expenses_report extends Admin_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
		
		 if (!$this->rbac->hasPrivilege('class', 'can_view')) {access_denied();}
		 
				$this->session->set_userdata('top_menu', 'Expenses Report');
				$this->session->set_userdata('sub_menu', 'expenses_report/index');

				$this->form_validation->set_rules('gl_account', 'GL Account', 'required');
				$this->form_validation->set_rules('from', 'Start Date', 'required');
				$this->form_validation->set_rules('to', 'End Date', 'required');

				if ($this->form_validation->run() == FALSE) {
					
									//$data['expenses_report']=$this->supplier_model->select(); 
									$data['glledger_accounts']=$this->glledger_accounts_model->select();

									$this->load->view('layout/header', $data);
									$this->load->view('supplier/expenses_report', $data);
									$this->load->view('layout/footer', $data);
					
				} else {			

						$gl_account = $this->input->post('gl_account');
						$from = $this->input->post('from');
						$to = $this->input->post('to');
						$search = $this->input->post('search');
												
					if (isset($search)) {

						$data['from'] = $from;
						$data['to'] = $to;
						$data['gl_name'] = $gl_account;
						
							if($gl_account == 'All'){
								
									$resultlist = $this->supplier_model->supplier_expenses_TO_FROM($from, $to);
									$data['supplier_results'] = $resultlist;
										
										//$data['print'] = "ITS PRINTING IN if statement";
									$data['glledger_accounts']=$this->glledger_accounts_model->select();
									$data['gl_account'] = $gl_account;
									$data['from'] = $from;
									$data['to'] = $to;									
									$this->load->view('layout/header', $data);
									$this->load->view('supplier/expenses_report', $data);
									$this->load->view('layout/footer', $data);
							}							
							else{
									$resultlist = $this->supplier_model->supplier_expenses_search($gl_account, $from, $to);
									$data['supplier_results'] = $resultlist;
										
									$data['glledger_accounts']=$this->glledger_accounts_model->select();
									$data['gl_account'] = $gl_account;
									$data['from'] = $from;
									$data['to'] = $to;
									$this->load->view('layout/header', $data);
									$this->load->view('supplier/expenses_report', $data);
									$this->load->view('layout/footer', $data);
							}


					}
					else{
						  echo "PLEASE NOTHING HAS BEEN SUBMITTED TO  FORM";
					}
				}          
    }    
	function printExpensesReport() {
		
		 if (!$this->rbac->hasPrivilege('class', 'can_view')) {access_denied();}
		        $setting_result = $this->setting_model->get();
                $data['settinglist'] = $setting_result;
				
						$gl_account = $this->input->post('gl_account');
						$from = $this->input->post('from');
						$to = $this->input->post('to');
						
							if($gl_account == 'All'){
								
									$resultlist = $this->supplier_model->supplier_expenses_TO_FROM($from, $to);
									$data['supplier_results'] = $resultlist;

									$data['glledger_accounts']=$this->glledger_accounts_model->select();

									$data['gl_account'] = $gl_account;
									$data['from'] = $from;
									$data['to'] = $to;
									
									$this->load->view('print/printExpensesReport', $data);
							}							
							else{
									$resultlist = $this->supplier_model->supplier_expenses_search($gl_account, $from, $to);
									$data['supplier_results'] = $resultlist;
										
									$data['glledger_accounts']=$this->glledger_accounts_model->select();
									
									$data['gl_account'] = $gl_account;
									$data['from'] = $from;
									$data['to'] = $to;
									
									$this->load->view('print/printExpensesReport', $data);
							}         
    }
    function printSupplierReceipt() {
	  
        $id = $this->input->post('id');

        $setting_result = $this->setting_model->get();
        $data['settinglist'] = $setting_result;
		
		$this->db->select('*');
		$this->db->from('supplier_receipt');
		$this->db->where('id = ', $id);
		$query = $this->db->get();
        $data['supplierReceipt'] = $query;
        $data['supID'] = $id;


        $this->load->view('print/printSupplierReceipt', $data);

    }
   
   function printSupplierInvoice() {
	  
        $id = $this->input->post('id');

        $setting_result = $this->setting_model->get();
        $data['settinglist'] = $setting_result;
		
		$this->db->select('*');
		$this->db->from('supplier_invoice');
		$this->db->where('id = ', $id);
		$query = $this->db->get();
        $data['supplierInvoice'] = $query;
        $data['supID'] = $id;


        $this->load->view('print/printSupplierInvoice', $data);

    }

}

?>