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

class ArInvoiceClone extends CI_Controller {
	
/* Editor Sayem */
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('2', 'transaction');
		if(!$query){
			echo '<script>alert("Not authorised..");
				window.location.replace("'.base_url().'Error");
				</script>';
		}
		$this->load->library('layouts');
		$this->load->model('menu');
		
		//adding model
		$this->lang->load("invoiceClone",$this->session->userdata('language'));
		$this->load->model('ar/invoiceClone');
	}

	//Index page to show all invoices from sales invoice table
	public function index($add_year = '', $add_period = ''){
		//Financial year detection
		$this->load->model('company/FormSetup_model');
		$this->load->model('Compfinancialyear');
		
		if(!$add_year){
			$add_year = date("Y");
		}
		if(!$add_period){
			$add_period = date("m");
		}
		$data['yrs'] = range(date('Y'), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$queryCal = $this->Compfinancialyear->financialYear_yearperiod($add_year, $add_period);
				
		//Retrive invoice list 
		if($queryCal){//Start and ending date
			$data['startDate'] = $queryCal[0]->startDate;
			$data['endDate'] = $queryCal[0]->endDate;
			$data['datatbls'] = $this->invoiceClone->invoiceList($queryCal[0]->startDate, (string)$queryCal[0]->endDate);
		}else{
			$data['datatbls'] = $this->invoiceClone->invoiceList(date("Y-m-1"), date("Y-m-d",time()));
		}
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('ar/transaction/invoiceClone', array('latest' => 'sidebar/latest'), $data);
	} 
	
	
	
	//Gather customer currency & exchange rate
	public function loadCustomerDefaultValue() {
		echo $this->invoiceClone->getCustomerInfo($this->input->post('customer'));
	}
	
	//Clone rows of Sales Invoice table
	public function cloneit(){
		$invID = explode(',',$this->input->post('fldid'));
		foreach($invID as $v){
			$this->invoiceClone->cloneIt($v);
		}
		echo json_encode("Success");
		exit();	
	}
	
	//Edit clone invoice information
	public function editSaleInvoice($id){
		if(isset($_POST['ID'])){
			$_POST['salesInvoiceDate'] = date('Y-m-d',strtotime($_POST['salesInvoiceDate']));
			$this->invoiceClone->updateInvoice($_POST);
		}
		$this->load->model('company/formSetup_Model');
		$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$this->load->model('companyChartAcct');
	
		$this->load->model('Usermaster');
		$data['records'] = $this->Usermaster->usermaster_all();
	
		$this->load->model('Project/project_model');
		$data['project'] = $this->project_model->project_all();
			
		$this->load->model('address_model');
		$data['shipto'] = $this->address_model->getCompanyAdresses();
			
		$this->load->model('employee/employee_model');
		$data['saler'] = $this->employee_model->employeemaster_allopt();
			
		$this->load->model('master/masterCode_model');
		$data['shipping'] = $this->masterCode_model->get_all(array('masterID'=>2));
				
		$this->load->model('Terms/terms_model');
		$data['terms'] = $this->terms_model->terms_all();
			
		$this->load->model('location');
		$data['location'] = $this->location->location_all();
			
		$this->load->model('customer/Customer_model');
		$data['customer'] = $this->Customer_model->customermaster_all();
			
		$this->load->model('SaleAgent/Agent_model');
		$data['agent'] = $this->Agent_model->agent_all();
			
		$this->load->model('companyChartAcct');
		$data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
			
			
		$this->load->model('taxmaster');
		$data['taxmaster'] = $this->taxmaster->taxmaster_all();
		
		$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$filter['formID']=48;
		$data['formNo']= $this->formSetup_Model->getFormNo($filter);
	
		$this->load->model('Usermaster');
		$data['records'] = $this->Usermaster->usermaster_all();

		$this->load->model('item/itemSetup_model');
		$data['items'] = $this->itemSetup_model->get_all();
		//Gather data from database
		$this->load->model('ar/saleInvoice_model');
		
		$data['invoice']=$this->saleInvoice_model->getDataByTname("comp_sales_invoice",$id);
		$data['itemList']=$this->saleInvoice_model->getDataByTname("comp_sales_invoice_detail",$id);
		$data['charges']=$this->saleInvoice_model->getDataByTname("comp_sales_invoice_charges",$id);
		
		
		
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/accounting.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
		$this->layouts->view('ar/transaction/editSalesInvoice', '', $data);
	}
	
/* Editor Sayem */
} //big end
