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

class Soreport extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('5', 'reports');
		if(!$query){
			echo '<script>alert("Not authorised..");
				window.location.replace("'.base_url().'Error");
				</script>';
		}
		$this->load->library('layouts');
		$this->load->model('menu');
	}

	public function index()
	{
		
	}
	public function SalesPriceList(){
	  	$this->lang->load("salesprice",$this->session->userdata('language'));
      	$this->load->model('SalesPrice');
		$data['datatbls'] = $this->SalesPrice->SalesPrice_all();
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/priceList', array('latest' => 'sidebar/latest'), $data);
	}
	public function SalesByCustomer(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/soByCustomer', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function SalesByCustomer_print(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->view('so/reports/soByCustomer_print', $data);
	}
	
	public function SalesByCustomer_excel(){
		$this->load->library('parser');
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		
		$myFile = "Sales_By_Customer_List.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('so/reports/soByCustomer_excel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
	}
	
	public function SalesByCustomerYears(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		if(!$add_year){
			$add_year = date("Y");
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}else{
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
	  $this->layouts->view('so/reports/soByCustomerYear', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function SalesByCustomerYear_print(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		if(!$add_year){
			$add_year = date("Y");
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}else{
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->view('so/reports/soByCustomerYear_print', $data);
	}
	
	public function SalesByCustomerYear_excel(){
		$this->load->library('parser');
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		if(!$add_year){
			$add_year = date("Y");
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}else{
			$query2 = $this->Compfinancialyear->financialYear_year_start($add_year);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
				$query3 = $this->Compfinancialyear->financialYear_year_end($add_year);
				if($query3){
					$endDate = $query3[0]->endDate;
				}
			}else{
			}
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['selyear'] = $add_year;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByCustomer_all_date($startDate, $endDate);
		
		$myFile = "Yerly_Sales_By_Customer.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('so/reports/soByCustomerYear_excel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
	}
	public function SalesOrderByCustomer(){
		$this->lang->load("salesorderByCustomer",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesOrder->SalesOrder_all_date_byCustomer($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/salesOrderByCustomer', array('latest' => 'sidebar/latest'), $data);
	}
	public function SalesOrderByItem(){
		$this->lang->load("salesorderByCustomer",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesOrder->SalesOrder_all_date_byItem($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/salesOrderByItem', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function SalesOrderOutstandingByItem(){
		$this->lang->load("salesOrderoutstanding",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Compfinancialyear');
		$data['datatbls'] = $this->SalesOrder->SalesOrder_all_outstanding_byItem();
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/salesOrdeOutstandingrByItem', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function SalesByItem(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByItems_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/soByItem', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function SalesByItem_print(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByItems_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->view('so/reports/soByItem_print', $data);
	}
	
	public function SalesByItem_excel(){
		$this->load->library('parser');
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		if(!$add_year){
			$add_year = date("Y");
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}else{
			$query2 = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
			if($query2){
				$startDate = $query2[0]->startDate;
				$endDate = $query2[0]->endDate;
			}else{
				$startDate = date("Y-m-01");
				$endDate = date("Y-m-t");
			}
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['yrs'] = range(date('Y', strtotime('+7 year')), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesByItems_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->view('so/reports/soByItem_print', $data);
		
		$myFile = "Sales_By_Item_List.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('so/reports/soByItem_excel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
	}
	
	/* download created excel file */
    function downloadExcel($myFile) {
        header("Content-Length: " . filesize($myFile));
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename='.$myFile);
 
        readfile($myFile);
    }

		
}

	
/* End of file artransaction.php */
/* Location: ./application/controller/artransaction.php */
