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

class Sotransaction extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('5', 'transaction');
		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 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/transactions/soList', array('latest' => 'sidebar/latest'), $data);
	}
	public function salesOrder(){
		$ID = $this->uri->segment(3);
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->model('Sysum');
		$this->load->model('Currency');
		$this->load->model('customer/customer_model');
		$this->load->model('employee/employee_model');
		$this->load->model('company/formSetup_model');
    	$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$filter['formID']=48;
		$data['formNo'] = $this->formSetup_model->getFormNo($filter);
		$data['employees'] = $this->employee_model->employeemaster_all();
		$data['um'] = $this->Sysum->sysum_all();
		$data['currency'] = $this->Currency->currency_all();
		$data['customers'] = $this->customer_model->customermaster_all();
		if($ID != ''){
			$this->load->model('SalesOrder');
			$query = $this->SalesOrder->SalesOrder_ID($ID);
			$data['headerSO'] = $query[0];
			$data['datatbls'] = $this->SalesOrder->SalesOrderDetails_all_ID($ID);
			$this->layouts->view('so/transactions/salesOrderEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$this->layouts->view('so/transactions/salesOrder', array('latest' => 'sidebar/latest'), $data);
		}
	}
	public function shipto(){
		$this->load->model('customer/customer_model');
		$this->load->model('customer/address_model');
		$query = $this->address_model->address_customer_all($this->input->post('customerID'));
		$query1 = $this->customer_model->customermaster_id($this->input->post('customerID'));
		if($query1[0]->currencyID == element('compCurrency', $this->session->userdata('logged_in'))){
			$exchangeRate = 1;
		}else{
			$this->load->model('currency');
			$querycurrency = $this->currency->currencyConv_id($query1[0]->currencyID);
			if($querycurrency){
				$exchangeRate = $querycurrency[0]->rateConv;
			}else{
				$exchangeRate = 1;
			}
		}
		if($query){
			print json_encode(array(
				"status"=>"success", 
				"message"=>$query, 
				"message1"=>$query1[0], 
				'exchangeRate'=>$exchangeRate 
				));
		}else{
			print json_encode(array("status"=>"false", "message"=>'',"message1"=>$query1[0], 'exchangeRate'=>$exchangeRate));
		}
	}
	public function itemSearch(){
		$this->load->model('item/ItemSetup_model');
		$query = $this->ItemSetup_model->get_all();
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function itemSearch2(){
		$this->load->model('item/ItemSetup_model');
        $this->load->model('SalesPrice');
		$query = $this->ItemSetup_model->get_byID($this->input->post('ID'));
		$where = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'customerID' => $this->input->post('customerID'),
			'itemID' => $this->input->post('ID'),
			'currencyID' => $this->input->post('currencyID'),
			'startDate <=' => date("Y-m-d", strtotime($this->input->post('effdate'))),
			'endDate >=' => date("Y-m-d", strtotime($this->input->post('effdate'))),
			'umID' => $query[0]->unitOfMeasureID
		);
		if($query){
			$query2 = $this->SalesPrice->SalesPrice_search($where);
			if($query2){
				$price = $query2[0]->price;
			}else{
				$price = 0;
			}
   			print json_encode(array("status"=>"success", "message"=>$query[0], "price"=>$price));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function itemSearch3(){
		$this->load->model('item/ItemSetup_model');
        $this->load->model('SalesPrice');
		$this->load->model('taxmaster');
		$query = $this->ItemSetup_model->get_byID($this->input->post('ID'));
		$where = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'customerID' => $this->input->post('customerID'),
			'itemID' => $this->input->post('ID'),
			'currencyID' => $this->input->post('currencyID'),
			'startDate <=' => date("Y-m-d", strtotime($this->input->post('effdate'))),
			'endDate >=' => date("Y-m-d", strtotime($this->input->post('effdate'))),
			'umID' => $query[0]->unitOfMeasureID
		);
		if($query){
			$query2 = $this->SalesPrice->SalesPrice_search($where);
			if($query2){
				$price = $query2[0]->price;
			}else{
				$price = 0;
			}
			$query3 = $this->taxmaster->taxmaster_id($query[0]->outputTaxID);
   			print json_encode(array("status"=>"success", "message"=>$query[0], "price"=>$price, "taxRate"=>$query3[0]->taxPercentage));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>''));
		}
	}
	public function umSearch(){
		$this->load->model('Sysum');
		$query = $this->Sysum->sysum_all();
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function customerSearch(){
		$this->load->model('customer/customer_model');
		$query = $this->customer_model->customermaster_all();
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function employeeSearch(){
		$this->load->model('employee/employee_model');
		$query = $this->employee_model->employeemaster_all();
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
		}
	}
	public function sodetails_delete(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$query = $this->SalesOrder->SalesOrderDetails_Delete($this->input->post('ID'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$this->lang->line('message6')));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
	
	public function sodetails_save(){
		$this->lang->load("salesorder",$this->session->userdata('language'));
		$this->load->model('SalesOrder');
		$this->load->model('Sysum');
		$this->load->model('item/ItemSetup_model');
		if($this->input->post('ID') == '' ){
			$salesID = $this->SalesOrder->SalesOrder_Insert();
			
			if($salesID){
//				$query = $this->SalesOrder->seqNumber();
				$OrderNo = $this->input->post('OrderNo');//.'-'.sprintf('%08d', $salesID);
				$dataUpdate =  array(
					'OrderNo' => $OrderNo
				);
				$query = $this->SalesOrder->SalesOrder_Update($salesID, $dataUpdate);
				$query = $this->SalesOrder->SalesOrderDetails_Insert($salesID);
				$queryum = $this->Sysum->sysum_id($this->input->post('unitmeasure'));
				$queryitems = $this->ItemSetup_model->get_byID($this->input->post('itemID'));
   				print json_encode(array(
					"status"=>"success", 
					"message"=>$this->lang->line('message2'), 
					"salesID"=>$salesID, "message1"=>$query, 
					"um"=>$queryum[0]->code, 
					"OrderNo"=>$OrderNo, 
					"items"=>$queryitems[0]->itemCode));
			}else{
   				print json_encode(array("status"=>"failed", "message"=>'Journal details saved..'));
			}
		}else{
			$OrderNo = $this->input->post('OrderNo');
			$updateData = array(
				'OrderNo' => $this->input->post('OrderNo'),
				'currencyID' => $this->input->post('currencyID'),
				'salesOrderDate' => date("Y-m-d", strtotime($this->input->post('effdate'))),
				'exchangeRate' => $this->input->post('exchangeRate'),
				'salesPerson' => $this->input->post('salesPerson'),
				'deliveryToID' => $this->input->post('deliveryToID'),
				'totalAmount' => $this->input->post('totalAmount'),
				'memo' => $this->input->post('memo'),
				'updatedBy' => element('role', $this->session->userdata('logged_in'))
			);
			$salesID = $this->SalesOrder->SalesOrder_update($this->input->post('ID'),$updateData);
			$query = $this->SalesOrder->SalesOrderDetails_Insert($this->input->post('ID'));
			$queryum = $this->Sysum->sysum_id($this->input->post('unitmeasure'));
			$queryitems = $this->ItemSetup_model->get_byID($this->input->post('itemID'));
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$this->lang->line('message2'), 
				"salesID"=>$this->input->post('itemID'), 
				"message1"=>$query, 
				"um"=>$queryum[0]->code, 
				"OrderNo"=>$OrderNo, 
				"items"=>$queryitems[0]->itemCode));
		}
	}
	
// Sales Quotation

	public function SalesQuotation(){
		$ID = $this->uri->segment(3);
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->model('Sysum');
		$this->load->model('Currency');
		$this->load->model('customer/customer_model');
		$this->load->model('employee/employee_model');
		$data['employees'] = $this->employee_model->employeemaster_all();
		$data['um'] = $this->Sysum->sysum_all();
		$data['currency'] = $this->Currency->currency_all();
		$data['customers'] = $this->customer_model->customermaster_all();
		$this->load->model('company/formSetup_model');
    	$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$filter['formID']=47;
		$data['formNo'] = $this->formSetup_model->getFormNo($filter);
		if($ID != ''){
			$this->load->model('customer/address_model');
			$this->load->model('SalesQuote');
			$query = $this->SalesQuote->SalesQuote_ID($ID);
			$data['headerQuote'] = $query[0];
			$data['shipTo'] = $this->address_model->address_customer_all($query[0]->customerID);
			$data['detailsQuote'] = $this->SalesQuote->SalesQuoteDetails_all_id($ID);
			$this->layouts->view('so/transactions/salesQuoteEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$this->layouts->view('so/transactions/salesQuote', array('latest' => 'sidebar/latest'), $data);
		}
	}
	public function taxSearch(){
		$this->load->model('taxmaster');
		$query = $this->taxmaster->taxmaster_saleOutput();
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$query));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>''));
		}
	}
	public function quoteDetails_save(){
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->load->model('SalesQuote');
		$this->load->model('Sysum');
		$this->load->model('item/ItemSetup_model');
		$this->load->model('taxmaster');
		if($this->input->post('ID') == '' ){
			$quoteID = $this->SalesQuote->SalesQuote_Insert();
			if($quoteID){
				$formNo = $this->input->post('formNo');  //Sayem (17th July,2014-instructed by Mr. Akmal).'-'.sprintf('%08d', $quoteID);
				$dataUpdate =  array(
					'formNo' => $formNo
				);
				$query = $this->SalesQuote->SalesQuote_Update($quoteID, $dataUpdate);
				$query = $this->SalesQuote->SalesQuoteDetails_Insert($quoteID);
				$queryum = $this->Sysum->sysum_id($this->input->post('unitmeasure'));
				$queryitems = $this->ItemSetup_model->get_byID($this->input->post('itemID'));
				$querytax = $this->taxmaster->taxmaster_id($this->input->post('taxID'));
   				print json_encode(array(
					"status"=>"success", 
					"message"=>$this->lang->line('message2'), 
					"quoteID"=>$quoteID, 
					"message1"=>$query, 
					"formNo"=>$formNo, 
					"um"=>$queryum[0]->code, 
					"items"=>$queryitems[0]->itemCode,
					"tax"=>$querytax[0]->code.' [ ' .$querytax[0]->code . ' ]')
					);
			}else{
   				print json_encode(array(
					"status"=>"failed", 
					"message"=>''));
			}
		}else{
			$updateData = array(
				'employeeID' => $this->input->post('employeeID'),
				'formNo' => $this->input->post('formNo'),
				'quotationDate' => date("Y-m-d", strtotime($this->input->post('effdate'))),
				'deliverToID' => $this->input->post('deliveryToID'),
				'currencyID' => $this->input->post('currencyID'),
				'memo' => $this->input->post('memo'),
				'exchangeRate' => $this->input->post('exchangeRate'),
				'employeeID' => $this->input->post('salesPerson'),
				'totalTaxAmount' => $this->input->post('totalAmount'),
				'updatedBy' => element('role', $this->session->userdata('logged_in'))
			);
			$quoteID = $this->SalesQuote->SalesQuote_update($this->input->post('ID'),$updateData);
			$query = $this->SalesQuote->SalesQuoteDetails_Insert($this->input->post('ID'));
			$queryum = $this->Sysum->sysum_id($this->input->post('unitmeasure'));
			$queryitems = $this->ItemSetup_model->get_byID($this->input->post('itemID'));
			$querytax = $this->taxmaster->taxmaster_id($this->input->post('taxID'));
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$this->lang->line('message2'), 
				"salesID"=>$this->input->post('itemID'), 
				"message1"=>$query, 
				'formNo' => $this->input->post('formNo'),
				"um"=>$queryum[0]->code, 
				"items"=>$queryitems[0]->itemCode,
				"tax"=>$querytax[0]->code.' [ ' .$querytax[0]->code . ' ]')
				);
		}
	}

	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/transactions/soquoteList', array('latest' => 'sidebar/latest'), $data);
	}
	public function quotation_delete(){
		$this->load->model('SalesQuote');
		$query = $this->SalesQuote->SalesQuote_delete($this->input->post('ID'));
	}
	public function soQuotedetails_delete(){
		$this->lang->load("salesquote",$this->session->userdata('language'));
		$this->load->model('SalesQuote');
		$query = $this->SalesQuote->SalesQuoteDetails_delete($this->input->post('ID'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$this->lang->line('message6')));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
// Delivery Order
// Yahaya Abdollah
// 6th June 2014

	public function SalesDO(){
		$ID = $this->uri->segment(3);
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/accounting.js')
				->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->load->model('Sysum');
		$this->load->model('Currency');
		$this->load->model('customer/customer_model');
		$this->load->model('employee/employee_model');
		$this->load->model('company/formSetup_model');
		$this->load->model('location');
		$this->load->model('SalesOrder');
		$this->load->model('master/MasterCode_model');
		$data['salesorder'] = $this->SalesOrder->SalesOrder_all();
		$data['employees'] = $this->employee_model->employeemaster_all();
		$data['um'] = $this->Sysum->sysum_all();
		$data['currency'] = $this->Currency->currency_all();
		$data['customers'] = $this->customer_model->customermaster_all();
    	$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$filter['formID']=49;
		$data['formNo'] = $this->formSetup_model->getFormNo($filter);
		$data['location'] = $this->location->location_all();
		$data['shippingMethod'] = $this->MasterCode_model->mastercode_all_id('2');
		if($ID != ''){
			$this->load->model('SalesDeliveryOrder');
			$query = $this->SalesDeliveryOrder->SalesDeliveryOrder_id($ID);
			$data['headerDo'] = $query[0];
			$data['datatbls'] = $this->SalesDeliveryOrder->SalesOrderDetails_withoutSO_ID($ID);
			$data['datatbls1'] = $this->SalesDeliveryOrder->SalesOrderDetails_SO_ID($query[0]->salesOrderID);
			$this->layouts->view('so/transactions/deliveryOrderEdit', array('latest' => 'sidebar/latest'), $data);
		}else{
			$this->layouts->view('so/transactions/deliveryOrder', array('latest' => 'sidebar/latest'), $data);
		}
	}
	
	public function SalesDO_print(){
		$ID = $this->uri->segment(3);
		$this->lang->load("salesDO",$this->session->userdata('language'));
	
		$this->load->model('Sysum');
		$this->load->model('Currency');
		$this->load->model('customer/customer_model');
		$this->load->model('employee/employee_model');
		$this->load->model('company/formSetup_model');
		$this->load->model('location');
		$this->load->model('SalesOrder');
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('master/MasterCode_model');
//		$data['salesorder'] = $this->SalesOrder->SalesOrder_all();
		$DOprint = $this->SalesDeliveryOrder->SalesDeliveryOrder_id($ID);
		$data['salesOrder'] = $DOprint[0];
//		echo $this->db->last_query();
		$data['employees'] = $this->employee_model->employeemaster_all();
		$data['um'] = $this->Sysum->sysum_all();
		//$data['currency'] = $this->Currency->currency_all();
		$data['customers'] = $this->customer_model->customermaster_all();
    	$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
		$data['location'] = $this->location->location_all();
		$shipMethod = $this->MasterCode_model->mastercode_all_id('2');
		$data['shippingMethod'] = $shipMethod[0];
		
		$this->load->model('SalesDeliveryOrder');
		$query = $this->SalesDeliveryOrder->SalesDeliveryOrder_id($ID);
		$data['headerDo'] = $query[0];
		$data['datatbls'] = $this->SalesDeliveryOrder->SalesOrderDetails_withoutSO_ID($ID);
		$data['datatbls1'] = $this->SalesDeliveryOrder->SalesOrderDetails_SO_ID($query[0]->salesOrderID);
		$this->load->view('so/transactions/deliveryOrder_print', $data);
	}
	
	public function deliveryOrder_insert(){
		$this->load->model('SalesDeliveryOrder');
		if($this->input->post('ID') == '' ){
			$deliveryOrderID = $this->SalesDeliveryOrder->SalesDeliveryOrder_Insert();
			if($deliveryOrderID){
				$ID = $deliveryOrderID;
				$formNo = $this->input->post('formNo');//.'-'.sprintf('%08d', $deliveryOrderID);
				$dataUpdate =  array(
					'formNo' => $formNo
				);
				$query = $this->SalesDeliveryOrder->SalesDeliveryOrder_Update($deliveryOrderID, $dataUpdate);
				$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_Insert($deliveryOrderID);
			}
   			print json_encode(array(
				"status"=>"success", 
				"ID"=>$ID, 
				));
		}else{
			$ID = $this->input->post('ID');
			$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_Insert($ID);
   			print json_encode(array(
				"status"=>"success", 
				"ID"=>$ID, 
				));
		}
	}
	public function deliveryOrderWo_insert(){
		$this->load->model('SalesDeliveryOrder');
		if($this->input->post('ID') == '' ){
			$deliveryOrderID = $this->SalesDeliveryOrder->SalesDeliveryOrder_Insert();
			if($deliveryOrderID){
				$ID = $deliveryOrderID;
				$formNo = $this->input->post('formNo').'-'.sprintf('%08d', $deliveryOrderID);
				$dataUpdate =  array(
					'formNo' => $formNo
				);
				$query = $this->SalesDeliveryOrder->SalesDeliveryOrder_Update($deliveryOrderID, $dataUpdate);
				$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetailsWo_Insert($deliveryOrderID);
			}
   			print json_encode(array(
				"status"=>"success", 
				"ID"=>$ID, 
				));
		}else{
			$ID = $this->input->post('ID');
			$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetailsWo_Insert($ID);
   			print json_encode(array(
				"status"=>"success", 
				"ID"=>$ID, 
				));
		}
	}
	public function doDetails_save(){
		$this->load->model('SalesDeliveryOrder');
		$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetailsWo_Insert($this->input->post('ID'));
		if($query){
			$query2 = $this->SalesDeliveryOrder->SalesOrderDetails_ID($query);
			if($query2){
   				print json_encode(array(
					"status"=>"success",
					"details"=>$query2[0], 
					"message"=>'Updated'
					));
			}
		}else{
   				print json_encode(array(
					"status"=>"failed",
					"details"=>'', 
					));
		}
	}
	public function doDetails_update(){
		$this->load->model('SalesDeliveryOrder');
//		if($this->input->post('fldid' != '')){
			$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_Update($this->input->post('fldid'));
//			if($query){
   				print json_encode(array(
					"status"=>"success", 
					));
//			}else{
//				print json_encode(array(
//					"status"=>"failed",
//					"details"=>'', 
//				));
//			}
//		}else{
//		}
	}
	public function SalesOrderSearch(){
		$this->load->model('SalesOrder');
		$this->load->model('customer/customer_model');
		$this->load->model('customer/address_model');
		$query = $this->SalesOrder->SalesOrder_all_customerID($this->input->post('customerID'));
		$query2 = $this->address_model->address_customer_all($this->input->post('customerID'));
		$query3 = $this->customer_model->customermaster_id($this->input->post('customerID'));
		if($query3[0]->currencyID == element('compCurrency', $this->session->userdata('logged_in'))){
			$exchangeRate = 1;
		}else{
			$this->load->model('currency');
			$querycurrency = $this->currency->currencyConv_id($query3[0]->currencyID);
			if($querycurrency){
				$exchangeRate = $querycurrency[0]->rateConv;
			}else{
				$exchangeRate = 1;
			}
		}
		if($query){
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$query,
				"message1"=>$query3[0], 
				'exchangeRate'=>$exchangeRate 
				));
		}else{
   			print json_encode(array(
				"status"=>"failed", 
				"message"=>'',
				'exchangeRate'=>$exchangeRate 
				));
		}
	}
	public function SalesOrderSearch1(){
		$this->load->model('Compfinancialyear');
		$this->load->model('SalesOrder');
		$query1 = $this->Compfinancialyear->compfinancialyear_date1(date("Y-m-d"));
		if($query1){
			$startDate = $query1[0]->startDate;
			$endDate = $query1[0]->endDate;
		}else{
			$startDate = date("Y-m-01");
			$endDate = date("Y-m-t");
		}
		$query = $this->SalesOrder->SalesOrder_all_date1($startDate, $endDate );
		if($query){
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$query,
				));
		}else{
   			print json_encode(array(
				"status"=>"failed", 
				"message"=>'',
				));
		}
	}
	public function soDetailsSearch(){
		$this->load->model('SalesOrder');
		$this->load->model('customer/customer_model');
		$this->load->model('customer/address_model');
		$query = $this->SalesOrder->SalesOrderDetails_all_ID($this->input->post('soID'));
		$query1 = $this->SalesOrder->SalesOrder_ID($this->input->post('soID'));
		$query2 = $this->address_model->address_customer_all($query1[0]->customerID);
//		$query3 = $this->customer_model->customermaster_id($query1[0]->customerID);
		if($query1[0]->currencyID == element('compCurrency', $this->session->userdata('logged_in'))){
			$exchangeRate = 1;
		}else{
			$exchangeRate = $query1[0]->exchangeRate;
		}
		if($query){
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$query, 
				"soHeader"=>$query1[0],
				"soShipto"=>$query2[0],
				'exchangeRate'=>$exchangeRate 
				));
		}else{
   			print json_encode(array(
				"status"=>"failed", 
				"message"=>'',
				'exchangeRate'=>$exchangeRate 
				));
		}
	}
	public function soDetailsSearch1(){
		$this->load->model('SalesOrder');
		$this->load->model('customer/customer_model');
		$this->load->model('customer/address_model');
		$query = $this->SalesOrder->SalesOrderDetails_all_ID($this->input->post('soID'));
		$query1 = $this->SalesOrder->SalesOrder_ID($this->input->post('soID'));
		$query2 = $this->address_model->address_customer_all($query1[0]->customerID);
//		$query3 = $this->customer_model->customermaster_id($query1[0]->customerID);
		if($query1[0]->currencyID == element('compCurrency', $this->session->userdata('logged_in'))){
			$exchangeRate = 1;
		}else{
			$exchangeRate = $query1[0]->exchangeRate;
		}
		if($query){
   			print json_encode(array(
				"status"=>"success", 
				"message"=>$query, 
				"soHeader"=>$query1[0],
				"soShipto"=>$query2[0],
				'exchangeRate'=>$exchangeRate 
				));
		}else{
   			print json_encode(array(
				"status"=>"failed", 
				"message"=>'',
				'exchangeRate'=>$exchangeRate 
				));
		}
	}
	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/transactions/delveryOrderList', array('latest' => 'sidebar/latest'), $data);
	}
	public function doDetails_delete(){
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder'); 
		$data = array(
			'totalAmount' => 'totalAmount - '.$this->input->post('lineAmount') 
		);	
		$query = $this->SalesDeliveryOrder->SalesDeliveryOrder_Update($this->input->post('deliveryID'), $data);
		$query = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_Delete($this->input->post('ID'));
		if($query){
   			print json_encode(array("status"=>"success", "message"=>$this->lang->line('message6')));
		}else{
   			print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
	public function salesDOPost(){
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$this->load->model('SalesDeliveryOrder');
		$this->load->model('Compfinancialyear');
		$this->load->model('company/FormSetup_model');
		$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_unpost_date($startDate, $endDate);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('so/transactions/doList', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function doPosting(){
		$this->lang->load("salesDO",$this->session->userdata('language'));
		$ID = $this->input->post('fldid');
		$effdate = date("Y-m-d", strtotime($this->input->post('effdate')));
		$this->load->model('SalesDeliveryOrder');
		$queryDetails = $this->SalesDeliveryOrder->SalesDeliveryOrderDetails_ID($ID);
		$data = array(
			'updateAble'=>'0'
		);
		$query = $this->SalesDeliveryOrder->SalesDeliveryOrderpost($ID, $data);
		if($query){
			$this->load->model('Compfinancialyear');
			$query1 = $this->Compfinancialyear->compfinancialyear_date1($effdate);
			if($query1){
				$year = $query1[0]->financialYear;
				$period = $query1[0]->period;
				$formNo = $query[0]->formNo;
			}else{
   				print json_encode(array("status"=>"failed", "message"=>$query1));
				return FALSE;
			}
 			$this->load->model('Glseq');
			$query2 = $this->Glseq->glSequence_sub('5', 'SO', $year, $period);
			$newData = array(
				'companyID' => element('compID', $this->session->userdata('logged_in')),
				'journalID' => $query2['seqNumber'],
				'formNo' => $formNo,
				'description' => $this->lang->line('message2') . ' [ ' . $formNo . ' ]',
				'module' => '5',
				'effective_date' => $effdate,
				'year' => $year,
				'period' => $period,
				'createdBy' => element('role', $this->session->userdata('logged_in'))
			);
			$data = array(
				'journalNo'=>$query2['seqNumber']
			);
			$query5 = $this->SalesDeliveryOrder->SalesDeliveryOrder_Update($ID, $data);
			$this->load->model('Journal');
			$query3 = $this->Journal->journal_insert($newData); 
			if($query3){
				$query4 = $this->SalesDeliveryOrder->SalesDeliveryOrderdetails_deliveryID($query2['seqNumber'], $query[0], $queryDetails, $year, $period);
//				$query4 = $this->SalesDeliveryOrder->SalesDeliveryOrderdetails_deliveryID($queryDetails);
  				print json_encode(array("status"=>"success", "message"=>$query));
			}else{
  				print json_encode(array("status"=>"failed", "message"=>$query));
			}
		}else{
  				print json_encode(array("status"=>"failed", "message"=>$query));
			return FALSE;
		} 
	}
	
	
}

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