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

class ApInvoiceClone 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("apinvoiceClone",$this->session->userdata('language'));
		$this->load->model('purchase/invoiceClone');
	}

	//Index page to show all invoices from supplier 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){
			$data['datatbls'] = $this->invoiceClone->invoiceList($queryCal[0]->startDate, (string)$queryCal[0]->endDate);
		}else{
			$data['datatbls'] = null;
		}
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('ap/transactions/invoiceClone', array('latest' => 'sidebar/latest'), $data);
	} 
		
	//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 editPurchaseInvoice($id){
		if(isset($_POST['ID'])){
			$_POST['invoiceDate'] = date('Y-m-d',strtotime($_POST['invoiceDate']));
			$this->invoiceClone->updateInvoice($_POST);
		}
		$this->load->model('company/formSetup_Model');
        $filter['companyID'] = element('compID', $this->session->userdata('logged_in'));
        $this->lang->load("invoices", $this->session->userdata('language'));
        $this->load->model('companyChartAcct');

        $this->load->model('master/masterCode_model');
        $data['shipping'] = $this->masterCode_model->get_all(array('masterID'=>2));
        
        $this->load->model('Usermaster');
        $data['records'] = $this->Usermaster->usermaster_all();

        $this->load->model('Supplier/supplier_model');
        $data['supplier'] = $this->supplier_model->suppliermaster_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['purchaser'] = $this->employee_model->employeemaster_allopt();

        $this->load->model('master/masterCode_model');
        $data['shipmethod'] = $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('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();


        $this->load->model('taxmaster');
        $data['taxmaster'] = $this->taxmaster->taxmaster_all();
        
        $this->load->model('item/itemSetup_model');
        //$default = $this->itemSetup_model->get_byCode($this->input->post('itemCode'));
        $data['items'] = $this->itemSetup_model->get_all();
        
//			$this->load->model('company/formSetup_Model');
        $filter['companyID'] = element('compID', $this->session->userdata('logged_in'));
        $filter['formID'] = 41;
        $data['formNo'] = $this->formSetup_Model->getFormNo($filter);
		
		//Gather data from database
		$this->load->model('accountPayable/accountPayable_model');        
        $data['itemList'] = $this->accountPayable_model->get_Detail_byID($id);
        $data['invoice'] = $this->accountPayable_model->get_byID("comp_supplier_invoice", $id);
        $data['charges'] = $this->accountPayable_model->getChargeByid($id);
		//print_r($data);
		//exit();
		$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('ap/transactions/editPurchaseInvoice', '', $data);
	}
	
/* Editor Sayem */
} //big end
