<?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);
    }


    /*********************************
			Sayem (17th July, 2014)
    *************Start****************/

	//Sales Quotation listing for report
	public function SalesQuotationList(){
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->load->model('SalesQuote');
		$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->SalesQuote->SalesQuote_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/soquoteList', array('latest' => 'sidebar/latest'), $data);
	}

	//Sales Quotation list print		
	public function SalesQuotationListPrint($add_year,$add_period, $mode = false){
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->load->model('SalesQuote');
		$this->load->model('Compfinancialyear');
		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");
			}
		}
		$data['datatbls'] = $this->SalesQuote->SalesQuote_all_date($startDate, $endDate);


		//Report printing
		$headerData['rptTitle'] = $this->lang->line('salesQuotation');
		$headerData['rightContent'] = array("From: ".date('d-m-Y', strtotime($startDate))." To: ".date('d-m-Y', strtotime($endDate)));
		//$headerData['leftContent'] = array("Left : value", "Left: value");

		//   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

		$bodyData['rptDataDetail'] = $data['datatbls'];
		$bodyData['tableHeader'] = array($this->lang->line("quoteNo"), $this->lang->line("quoteDate"), $this->lang->line("currency"),$this->lang->line("amount"),$this->lang->line("customer")); 
		$bodyData['selectFields'] = array("formNo", "quotationDate", "fldcurr_code","totalTaxAmount","customerName");
		$bodyData ['tdAmount'] = array("totalTaxAmount");
		//$bodyData['tableFooter'] = array ("footer : value");
		$bodyData['excelTitle'] = $this->lang->line('salesQuotation');

		if(!$mode){
		//print
		$this->load->view('layouts/report/header', $headerData);
		$this->load->view('layouts/report/body', $bodyData);
		$this->load->view('layouts/report/footer','');
		}else{
		//download excell
		$this->load->helper('excel');
		excel($bodyData);
		}

	}

	//Sales quotation detail print
	public function SalesQuotationDetail($id, $mode = false){
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->load->model('SalesQuote');
		$this->load->model('Compfinancialyear');
		
		$data['detail'] = $this->SalesQuote->SalesQuoteDetails_all_id($id);
		$data['info']   = $this->SalesQuote->SalesQuote_ID($id);	

		//Report printing
		$headerData['rptTitle'] = $bodyData['excelTitle'] = $this->lang->line('quotationDetail');
		
		$headerData['rightContentHeader'] = $this->lang->line("quoteNo")." :".$data['info'][0]->quoteNo;
		$headerData['rightContent'] = array(
											$this->lang->line("quoteDate")." :".date('d-m-Y',strtotime($data['info'][0]->quotationDate))
										); 


		$headerData['leftContentHeader'] = $data['info'][0]->customerName;
        $headerData['leftContent'] = array(
											$data['info'][0]->customerCompanyNo,
											$data['info'][0]->line1,
											$data['info'][0]->line2,
											$data['info'][0]->line3,
											$data['info'][0]->customerPostCode,
											$data['info'][0]->city
										); 

		$bodyData['rptDataDetail'] = $data['detail'];
		$bodyData['tableHeader'] = array(
										$this->lang->line("itemName"), 
										$this->lang->line("description"), 
										$this->lang->line("quantity"),
										$this->lang->line("unitPrice"),
										$this->lang->line("amountExcludeTax"),
										$this->lang->line("taxRate"),
										$this->lang->line("amountIncludeTax"),
										$this->lang->line("taxAmount"),
									); 
		$bodyData['selectFields'] = array(
										"itemCodeName", 
										"description", 
										"quantityOrder",
										"unitOfPrice",
										"amountExcludeTax",
										"taxRate",
										"amountIncudeTax",
										"taxAmount",
										);
		$bodyData ['tdAmount'] = array(
										"quantityOrder",
										"unitOfPrice",
										"amountExcludeTax",
										"taxRate",
										"amountIncudeTax",
										"taxAmount"
										);
		//$bodyData['tableFooter'] = array ("footer : value");

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}

	//Sales Order Report
	public function SalesOrderList(){
		$this->lang->load("salesorder",$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($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/soList', array('latest' => 'sidebar/latest'), $data);
	}

	//Sales Order List Print
	public function SalesOrderListPrint($add_year,$add_period,$mode = false){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Compfinancialyear');
		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($startDate, $endDate);

		//Report printing
		$headerData['rptTitle'] = $this->lang->line('salesOrder');
		$headerData['rightContent'] = array("From: ".date('d-m-Y', strtotime($startDate))." To: ".date('d-m-Y', strtotime($endDate)));
		//$headerData['leftContent'] = array("Left : value", "Left: value");

		//   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

		$bodyData['rptDataDetail'] = $data['datatbls'];
		$bodyData['tableHeader'] = array($this->lang->line("orderNo"), 
										$this->lang->line("orderDate"), 
										$this->lang->line("memo"),
										$this->lang->line("currency"),
										$this->lang->line("amount"),
										$this->lang->line("customer")
									); 
		$bodyData['selectFields'] = array("OrderNo", "salesOrderDate", "memo","fldcurr_code","totalAmount","customerName");
		$bodyData ['tdAmount'] = array("totalAmount");
		//$bodyData['tableFooter'] = array ("footer : value");
		$bodyData['excelTitle'] = $this->lang->line('salesOrder');

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}

	}

	//Sales Order Detail print
	public function SalesOrderDetail($id, $mode = false){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Compfinancialyear');
		
		$data['detail'] = $this->SalesOrder->SalesOrderDetails_all_ID($id);
		$data['info']   = $this->SalesOrder->SalesOrder_ID($id);	

		//Report printing
		$headerData['rptTitle'] = $bodyData['excelTitle'] = $this->lang->line('orderHeader');
		
		$headerData['rightContentHeader'] = $this->lang->line("orderNo")." :".$data['info'][0]->formNo;
		$headerData['rightContent'] = array(
											$this->lang->line("orderDate")." :".date('d-m-Y',strtotime($data['info'][0]->salesOrderDate))
										); 


		$headerData['leftContentHeader'] = $data['info'][0]->customerName;
        $headerData['leftContent'] = array(
											$data['info'][0]->customerCompanyNo,
											$data['info'][0]->line1,
											$data['info'][0]->line2,
											$data['info'][0]->line3,
											$data['info'][0]->city.", ".$data['info'][0]->customerPostCode,
											"Phone: ".$data['info'][0]->phoneNumber1
										); 

		$bodyData['rptDataDetail'] = $data['detail'];
		$bodyData['tableHeader'] = array(
										$this->lang->line("itemName"), 
										$this->lang->line("description"),
										$this->lang->line("unitPrice"),
										$this->lang->line("code"),
										$this->lang->line("quantityOrder"),
										$this->lang->line("quantityShip"),
										$this->lang->line("amount"),
									); 
		$bodyData['selectFields'] = array(
										"itemCodeName", 
										"description", 
										"unitPrice",
										"code",
										"quantityOrder",
										"quantityShip",
										"amount"
										);
		$bodyData ['tdAmount'] = array(
										"unitPrice",
										"quantityOrder",
										"quantityShip",
										"amount",
										);
		//$bodyData['tableFooter'] = array ("footer : value");

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}

	//Delivery Order report list
	public function SalesDOList(){
		$this->lang->load("salesDO",$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->SalesDeliveryOrder_all_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/deliveryOrderList', array('latest' => 'sidebar/latest'), $data);
	}

	//Delivery Order report list
	public function DeliveryOrderListPrint($add_year,$add_period,$mode = false){
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		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->SalesDeliveryOrder_all_date($startDate, $endDate);
		

		//Report printing
		$headerData['rptTitle'] = $this->lang->line('deliveryOrder');
		$headerData['rightContent'] = array("From: ".date('d-m-Y', strtotime($startDate))." To: ".date('d-m-Y', strtotime($endDate)));
		//$headerData['leftContent'] = array("Left : value", "Left: value");

		//   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

		$bodyData['rptDataDetail'] = $data['datatbls'];
		$bodyData['tableHeader'] = array($this->lang->line("orderNo"), 
										$this->lang->line("effective_date"), 
										$this->lang->line("employeeName"),
										$this->lang->line("amount"),
										$this->lang->line("customer")
									); 
		$bodyData['selectFields'] = array("formNo", 
										"deliveryOrderDate", 
										"employeeName",
										"totalAmount",
										"customerName"
									);
		$bodyData ['tdAmount'] = array("totalAmount");
		//$bodyData['tableFooter'] = array ("footer : value");
		$bodyData['excelTitle'] = $this->lang->line('deliveryOrder');

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}

	//Delivery order detail print
	public function DeliveryOrderDetail($id, $mode = false){
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		
		$data['detail'] = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_ID($id);
		$data['info']   = $this->SalesDeliveryOrder->SalesDeliveryOrder_id($id);	
		
		//Report printing
		$headerData['rptTitle'] = $bodyData['excelTitle'] = $this->lang->line('deliveryDetail');
		
		$headerData['rightContentHeader'] = $this->lang->line("deliveryNo")." :".$data['info'][0]->formNo;
		$headerData['rightContent'] = array(
											$this->lang->line("deliveryDate")." :".date('d-m-Y',strtotime($data['info'][0]->deliveryOrderDate))
										); 


		$headerData['leftContentHeader'] = $data['info'][0]->customerName;
        $headerData['leftContent'] = array(
											$data['info'][0]->desc,
											$data['info'][0]->address,
											$data['info'][0]->city.", ".$data['info'][0]->postcode,
											"Phone: ".$data['info'][0]->phone_no
										); 

		$bodyData['rptDataDetail'] = $data['detail'];
		$bodyData['tableHeader'] = array(
										$this->lang->line("itemName"), 
										$this->lang->line("description"),
										$this->lang->line("unitPrice"),
										$this->lang->line("code"),
										$this->lang->line("quantityOrder"),
										$this->lang->line("quantitySent"),
										$this->lang->line("amount"),
									); 
		$bodyData['selectFields'] = array(
										"itemCodeName", 
										"description", 
										"unitPrice",
										"code",
										"quantityOrdered",
										"quantitySent",
										"amount"
										);
		$bodyData ['tdAmount'] = array(
										"quantityOrdered",
										"unitPrice",
										"quantitySent",
										"amount",
										);
		//$bodyData['tableFooter'] = array ("footer : value");

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}

	//Sales Price report
	public function SalesPrice(){
	  	$this->lang->load("salesprice",$this->session->userdata('language'));
      	$this->load->model('SalesPrice');
		$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->SalesPrice->SalesPriceReport_all($startDate,$endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/reports/salesPriceList', array('latest' => 'sidebar/latest'), $data);
	}

	//Sales Price report print
	public function SalesPricePrint($add_year,$add_period, $mode = false){
	  	$this->lang->load("salesprice",$this->session->userdata('language'));
      	$this->load->model('SalesPrice');
		$this->load->model('Compfinancialyear');
		
		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->SalesPrice->SalesPriceReport_all($startDate,$endDate);
		
		//Report printing
		$headerData['rptTitle'] = $this->lang->line('salesPrice');
		$headerData['rightContent'] = array("As at: ".date('d-m-Y'));
		//$headerData['leftContent'] = array("Left : value", "Left: value");

		//   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

		$bodyData['rptDataDetail'] = $data['datatbls'];
		$bodyData['tableHeader'] = array($this->lang->line("items"), 
										$this->lang->line("description"), 
										$this->lang->line("customer"),
										$this->lang->line("ums"),
										$this->lang->line("currency"),
										$this->lang->line("price")
									); 
		$bodyData['selectFields'] = array("itemCode", 
										"description", 
										"customerName",
										"code",
										"fldcurr_code",
										"price"
									);
		$bodyData ['tdAmount'] = array("price");
		//$bodyData['tableFooter'] = array ("footer : value");
		$bodyData['excelTitle'] = $this->lang->line('salesPrice');

		if(!$mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}
    /*************End****************/
		
}

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