<?php

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

class Purchasetransaction extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Useraccess');
        $query = $this->Useraccess->useraccess_id_module2('8', '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 purchaseInvoiceList() {
        $this->lang->load("purchaseInvoice", $this->session->userdata('language'));
        $this->load->model('purchaseInvoice_list');
        $this->load->model('company/FormSetup_model');

//		$dateFrom = $this->input->post('selDateFrom');
//		$dateTo = $this->input->post('selDateTo');
        $dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		$totalPayable = $this->uri->segment(5);
		
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['totalPayable'] = $totalPayable;
        //if (($dateFrom == "") || ($dateTo == "")) {
            //$data['datatbls'] = $this->purchaseInvoice_list->purchaseInvoice_all();
        //} else {
		
		$data['datatbls'] = $this->purchaseInvoice_list->purchaseInvoiceDate_totalPayable($dateFrom, $dateTo, $totalPayable);
		
        //}
//		echo $this->db->last_query();
        $this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
        $this->layouts->view('purchase/transactions/purchaseInvoiceList', array('latest' => 'sidebar/latest'), $data);
    }
	
	public function purchaseInvoiceListprint() {
        $this->lang->load("purchaseInvoice", $this->session->userdata('language'));
        $this->load->model('purchaseInvoice_list');
        $this->load->model('company/FormSetup_model');
		$this->load->model('company/company_info_model');

//		$dateFrom = $this->input->post('selDateFrom');
//		$dateTo = $this->input->post('selDateTo');

        $dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		$totalPayable = $this->uri->segment(5);
		$status = "";
		
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['totalPayable'] = $totalPayable;
        //if (($dateFrom == "") || ($dateTo == "")) {
            //$data['datatbls'] = $this->purchaseInvoice_list->purchaseInvoice_all();
        //} else {
		
		$data['datatbls'] = $this->purchaseInvoice_list->purchaseInvoiceDate_totalPayable($dateFrom, $dateTo, $totalPayable);
		
        
		
		if ($totalPayable == ""){
			$headerData['rightContentHeader'] = "Name supplier/ ot";
        	$headerData['rptTitle'] = "Purchase Invoice";
        	$headerData['rightContent'] = array($this->lang->line('dateFrom')." ".date('d-m-Y',strtotime($dateFrom))." ".$this->lang->line('dateTo')." ".date('d-m-Y',strtotime($dateTo)));
			//$headerData['leftContent'] = array("Left : value", "Left: value");
			$headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $this->purchaseInvoice_list->purchaseInvoiceDate_totalPayableStatus($dateFrom, $dateTo, $status);
        	$bodyData['tableHeader'] = array($this->lang->line('invoiceno'), $this->lang->line('supplierName'), $this->lang->line('formNo'), "Status", $this->lang->line('date')); 
        	$bodyData['selectFields'] = array(
				"supplierInvoiceNo",
				"supplierName", 
				"formNo",
				"formNo", 
				"InvDate");
        	//$bodyData ['tdAmount'] = array("invoiceDate");
        	//$bodyData['tableFooter'] = array ("footer : value");
		}
		else if( $totalPayable == 1){
			$headerData['rightContentHeader'] = "Name supplier/ ot";
        	$headerData['rptTitle'] = "Purchase Invoice";
			$headerData['rightContent'] = array($this->lang->line('dateFrom')." ".date('d-m-Y',strtotime($dateFrom))." ".$this->lang->line('dateTo')." ".date('d-m-Y',strtotime($dateTo)), $this->lang->line('statOpen'));
			//$headerData['leftContent'] = array("Left : value", "Left: value");
			$headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $this->purchaseInvoice_list->purchaseInvoiceDate_totalPayable($dateFrom, $dateTo, $totalPayable);
        	$bodyData['tableHeader'] = array($this->lang->line('invoiceno'), $this->lang->line('supplierName'), $this->lang->line('formNo'), $this->lang->line('date')); 
        	$bodyData['selectFields'] = array(
				"supplierInvoiceNo",
				"supplierName", 
				"formNo", 
				"InvDate");
        	//$bodyData ['tdAmount'] = array("invoiceDate");
        	//$bodyData['tableFooter'] = array ("footer : value");
		}
		else if( $totalPayable == 2){
			$headerData['rightContentHeader'] = "Name supplier/ ot";
        	$headerData['rptTitle'] = "Purchase Invoice";
			$headerData['rightContent'] = array($this->lang->line('dateFrom')." ".date('d-m-Y',strtotime($dateFrom))." ".$this->lang->line('dateTo')." ".date('d-m-Y',strtotime($dateTo)), $this->lang->line('statClose'));
			//$headerData['leftContent'] = array("Left : value", "Left: value");
			$headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $this->purchaseInvoice_list->purchaseInvoiceDate_totalPayable($dateFrom, $dateTo, $totalPayable);
        	$bodyData['tableHeader'] = array($this->lang->line('invoiceno'), $this->lang->line('supplierName'), $this->lang->line('formNo'), $this->lang->line('date')); 
        	$bodyData['selectFields'] = array(
				"supplierInvoiceNo",
				"supplierName", 
				"formNo", 
				"InvDate");
        	//$bodyData ['tdAmount'] = array("invoiceDate");
        	//$bodyData['tableFooter'] = array ("footer : value");
		}
            
        $this->load->view('layouts/report/header', $headerData);
        $this->load->view('layouts/report/body', $bodyData);
        $this->load->view('layouts/report/footer','');
    }

// purchase invoices	
// created by Eisa
// 20th may 2014

    public function invoices() {
        $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('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['shippingmethod'] = $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);

        $this->load->model('Usermaster');
        $data['records'] = $this->Usermaster->usermaster_all();
        $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('purchase/transactions/newdirectpurchaseinvoice', '', $data);
    }

// purchase invoices confirm 
// created by Sayem
// 24th june 2014
	public function invoicesConfirm(){
		$this->lang->load("purchaseinvoiceconfirm", $this->session->userdata('language'));
		$this->load->model('debitnote_model');
		$this->load->model('company/FormSetup_model');
		$dateFrom = date("Y-m-d", strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d", strtotime($this->uri->segment(4)));
		if ($this->uri->segment(3) == "") {
			$dateFrom = date("Y-m-01");
		}
		if ($this->uri->segment(4) == "") {
			$dateTo = date("Y-m-t");
		}
		if ($dateFrom > $dateTo) {
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		
		$this->load->model('accountPayable/accountPayable_model');
		$data['datatbls'] = $this->accountPayable_model->getSupplierInvoiceAll($data['dateFrom'],$data['dateTo']);
		
		$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')
					->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('purchase/transactions/purchaseinvoiceconfirm', '', $data);
	}

    public function loadSupplierDefaultValue() {
        $this->load->model('accountPayable/accountPayable_model');

        $JsonRecords = '{"records":[';

        $i = 0;
        $default = $this->accountPayable_model->loadSupplierDefaultValue($this->input->post('supplier'));
        $n = sizeof($default);
        foreach ($default as $row) {
            $JsonRecords .= '{';
            $JsonRecords .= '"currency":"' . $row->currencyWord . '",' . '"exchangeRate":"' . $row->exchangeRate . '","currencyID":"' . $row->currencyID . '"';
            $JsonRecords .= '}';
            if ($i < $n - 1)
                $JsonRecords .= ',';
            $i++;
        }
        $JsonRecords .= ']}';
        echo $JsonRecords;
    }

    public function refreshSupplier() {
        //for ajax call 
        $this->load->model('Supplier/supplier_model');
        $supllier = $this->supplier_model->suppliermaster_all();
        $JsonRecords = '{"records":[';

        $n = sizeof($supllier);
        $i = 0;
        if ($supllier)
            foreach ($supllier as $row) {
                $JsonRecords .= '{';
                $JsonRecords .= '"supplierName":"' . $row->supplierName . '","id":"' . $row->ID . '"';
                $JsonRecords .= '}';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
        $JsonRecords .= ']}';

        //$resul= '{"supplier":['. json_encode($this->supplier_model->suppliermaster_all()).']}';
        echo $JsonRecords;
    }

    public function getItemCodes() {
        $JsonRecords = '[';
        $i = 0;
        $this->load->model('item/itemSetup_model');
        //$default = $this->itemSetup_model->get_byCode($this->input->post('itemCode'));
        $default = $this->itemSetup_model->get_all();
        $n = sizeof($default);
        if ($default)
            foreach ($default as $row) {
                $JsonRecords .= '&quot;' . $row->itemCode . '&quot;';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
        $JsonRecords .= ']';
        return $JsonRecords;
    }

    public function refreshTerms() {
        //for ajax call 
        $this->load->model('Terms/terms_model');
        $terms = $this->terms_model->terms_all();

        //json_encode ($this->project_model->project_all());

        $JsonRecords = '{"records":[';
        $n = sizeof($terms);
        $i = 0;
        if ($terms)
            foreach ($terms as $row) {
                $JsonRecords .= '{';
                $JsonRecords .= '"termName":"' . $row->termName . '","id":"' . $row->ID . '"';
                $JsonRecords .= '}';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
        $JsonRecords .= ']}';

        //$resul= '{"supplier":['. json_encode($this->supplier_model->suppliermaster_all()).']}';
        echo $JsonRecords;
    }

    public function refreshProject() {
        //for ajax call 
        $this->load->model('Project/project_model');
        $project = $this->project_model->project_all();

        //json_encode ($this->project_model->project_all());

        $JsonRecords = '{"records":[';
        $n = sizeof($project);
        $i = 0;
        if ($project)
            foreach ($project as $row) {
                $JsonRecords .= '{';
                $JsonRecords .= '"id":"' . $row->ID . '","projectName":"' . $row->project_name . '"';
                $JsonRecords .= '}';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
        $JsonRecords .= ']}';

        //$resul= '{"supplier":['. json_encode($this->supplier_model->suppliermaster_all()).']}';
        echo $JsonRecords;
    }

    function getTermDescription() {
        $this->load->model('terms/terms_model');
        $JsonRecords = '';
        $default = $this->terms_model->terms_id($this->input->post('term'));
        if ($default != false) {
            foreach ($default as $row)
                $JsonRecords = $row->termDescription;
            echo $JsonRecords;
        } else
            echo "Term not found";
    }

    function savePurchaseRow() {
        $this->load->model('accountPayable/accountPayable_model');
        $ID = $this->input->post('ID');
        $companyID = element('compID', $this->session->userdata('logged_in'));
        $datinsert = array(
            'shippingMethodID' => $this->input->post('shippingMethodID'),
            'purchaserID' => $this->input->post('employeeID'),
            'memo' => $this->input->post('memo'),
            'locationID' => $this->input->post('locationID'),
            'currencyID' => $this->input->post('currencyID'),
            'companyID' => $companyID,
            'createdBy' => element('role', $this->session->userdata('logged_in')),
            'purchaseInvoiceCodeID' => 2,
            'supplierID' => $this->input->post('supplierID'),
            'shipToID' => $this->input->post('shipToID'),
            'formNo' => $this->input->post('formNo'),
            'invoiceDate' => date('Y-m-d', strtotime($this->input->post('invoiceDate'))),
            'supplierInvoiceNo' => $this->input->post('supplierInvoiceNo'),
            'projectID' => $this->input->post('projectID'),
            'termsID' => $this->input->post('termsID'),
            'exchangeRate' => $this->input->post('exchangeRate')
        );


        $queryRes = $this->accountPayable_model->insert("comp_supplier_invoice", $datinsert);


        if ($queryRes>0) {
            
            $datinsertDetails = array(
                'invoiceID' => $queryRes,
                'createdBy' => element('role', $this->session->userdata('logged_in')),
                'itemID' => $this->input->post('itemID'),
                'lineNo' => $this->input->post('lineNo'),
                'itemdescription' => $this->input->post('itemdescription'),
                'quantity' => $this->input->post('quantity'),
                'unitPrice' => $this->input->post('unitPrice'),
                'amountExcludedTax' => $this->input->post('amountExcludedTax'),
                'taxID' => $this->input->post('taxID'),
                'taxRate' => $this->input->post('taxRate'),
                'taxAmount' => $this->input->post('taxAmount'),
                'amountIncludedTax' => $this->input->post('amountIncludeTax')
            );
            $query = $this->accountPayable_model->insert('comp_supplier_invoice_detail', $datinsertDetails);
            echo '{"status":"success","invoiceID":"' . $queryRes . '","detailID":"' . $query . '"}';
        } else {
            echo "false";
        }
    }

    public function updateInvoice() {
        $this->load->model('accountPayable/accountPayable_model');
        $invoiceUpdate = array(
            'updatedBy' => element('role', $this->session->userdata('logged_in')),
            'totalTaxAmount' => $this->input->post('totalamount'),
            'totalPayable' => $this->input->post('totalPayable'),
        );
        $this->accountPayable_model->updateInvoice($this->input->post('invoiceId'), $invoiceUpdate);
    }

    function saveParchaseDetail() {
       
        $this->load->model('accountPayable/accountPayable_model');
        $datinsertDetails = array(
            'invoiceID' => $this->input->post('invoiceId'),
            'createdBy' => element('role', $this->session->userdata('logged_in')),
            'itemID' => $this->input->post('itemID'),
            'lineNo' => $this->input->post('lineNo'),
            'itemdescription' => $this->input->post('itemdescription'),
            'quantity' => $this->input->post('quantity'),
            'unitPrice' => $this->input->post('unitPrice'),
            'amountExcludedTax' => $this->input->post('amountExcludedTax'),
            'taxID' => $this->input->post('taxID'),
            'taxRate' => $this->input->post('taxRate'),
            'taxAmount' => $this->input->post('taxAmount'),
            'amountIncludedTax' => $this->input->post('amountIncludeTax')
        );
        $query = $this->accountPayable_model->insert('comp_supplier_invoice_detail', $datinsertDetails);
        $detail = $this->accountPayable_model->getLasIdInsert("comp_supplier_invoice_detail");


        echo '{"status":"success","detailID":"' . $detail . '"}';
    }

    public function deleteCompanyDetails() {
        $this->load->model('accountPayable/accountPayable_model');
        $this->accountPayable_model->delete($this->input->post('invDetailsId'), 'comp_supplier_invoice_detail');
        echo "ok";
    }

    function checkformNumber() {
        $this->load->model('accountPayable/accountPayable_model');
        echo $this->accountPayable_model->checkformNumber_model("comp_supplier_invoice", $this->input->post('code'));
    }

    public function updateRowDetail() {
        $this->load->model('accountPayable/accountPayable_model');
        $data = array(
            'updatedBy' => element('role', $this->session->userdata('logged_in')),
            'itemID' => $this->input->post('itemID'),
            'itemdescription' => $this->input->post('itemdescription'),
            'quantity' => $this->input->post('quantity'),
            'unitPrice' => $this->input->post('unitPrice'),
            'amountExcludedTax' => $this->input->post('amountExcludedTax'),
            'taxRate' => $this->input->post('taxRate'),
            'taxID' => $this->input->post('taxID'),
            'taxAmount' => $this->input->post('taxAmount'),
            'amountIncludedTax' => $this->input->post('amountIncludedTax')
        );
        echo $this->accountPayable_model->update_byID($this->input->post('invDetailsId'), $data);
    }

    public function saveCharge() {
        $this->load->model('accountPayable/accountPayable_model');
        $ID = $this->input->post('ID');
        $companyID = element('compID', $this->session->userdata('logged_in'));
        $datinsert = array(
            'companyID' => $companyID,
            'createdBy' => element('role', $this->session->userdata('logged_in')),
            'currencyID' => $this->input->post('currencyID'),
            'purchaseInvoiceCodeID' => 2,
            'supplierID' => $this->input->post('supplierID'),
            'formNo' => $this->input->post('formNo'),
            'shipToID' => $this->input->post('shipToID'),
            'invoiceDate' => date('Y-m-d', strtotime($this->input->post('invoiceDate'))),
            'supplierInvoiceNo'=> $this->input->post('supplierInvoiceNo'),
            'projectID' => $this->input->post('projectID'),
            'termsID' => $this->input->post('termsID'),
            'exchangeRate' => $this->input->post('exchangeRate'),
            'shippingMethodID' => $this->input->post('shippingMethodID'),
            'purchaserID' => $this->input->post('employeeID'),
            'memo' => $this->input->post('memo')
        );


        $query = $this->accountPayable_model->insert("comp_supplier_invoice", $datinsert);


        if ($query>0) {

            $datinsertDetails = array(
                'invoiceID' => $query,
                'createdBy' => element('role', $this->session->userdata('logged_in')),
                'accountNoID' => $this->input->post('accountNoID'),
                'lineNo' => $this->input->post('lineNo'),
                'itemdescription' => $this->input->post('itemdescription'),
                'amountExcludedTax' => $this->input->post('amountExcludedTax'),
                'taxID' => $this->input->post('taxID'),
                'taxAmount' => $this->input->post('taxAmount'),
                'amountIncludedTax' => $this->input->post('amountIncludedTax'),
            );
            $detail = $this->accountPayable_model->insert('comp_supplier_invoice_charges', $datinsertDetails);
           // $detail = $this->accountPayable_model->getLasIdInsert("comp_supplier_invoice_charges");
            echo '{"status":"success","invoiceID":"' . $invID . '","chargeId":"' . $detail . '"}';
        } else {
            echo "false";
        }
    }

    function deleteCharge() {
        $this->load->model('accountPayable/accountPayable_model');
        echo $this->accountPayable_model->delete($this->input->post('chargeID'), "comp_supplier_invoice_charges");
    }

    public function saveChargeDetailOnly() {

        $this->load->model('accountPayable/accountPayable_model');
        $datinsertDetails = array(
            'invoiceID' => $this->input->post('invoiceId'),
            'lineNo' => $this->input->post('lineNo'),
            'createdBy' => element('role', $this->session->userdata('logged_in')),
            'accountNoID' => $this->input->post('accountNoID'),
            'itemdescription' => $this->input->post('itemdescription'),
            'amountExcludedTax' => $this->input->post('amountExcludedTax'),
            'taxID' => $this->input->post('taxID'),
            'taxAmount' => $this->input->post('taxAmount'),
            'amountIncludedTax' => $this->input->post('amountIncludedTax'),
        );
        $detail = $this->accountPayable_model->insert('comp_supplier_invoice_charges', $datinsertDetails);
       
        echo '{"status":"success","chargeId":"' . $detail . '"}';
    }

    public function refreshLocation() {
        //for ajax call 
        $this->load->model('location');
        $location = $this->location->location_all();

        //json_encode ($this->project_model->project_all());

        $JsonRecords = '{"records":[';
        $n = sizeof($location);
        $i = 0;
        if ($location)
            foreach ($location as $row) {
                $JsonRecords .= '{';
                $JsonRecords .= '"id":"' . $row->fldid . '","location":"' . $row->code . ' | ' . $row->address . ' | ' . $row->city . '"';
                $JsonRecords .= '}';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
        $JsonRecords .= ']}';

        //$resul= '{"supplier":['. json_encode($this->supplier_model->suppliermaster_all()).']}';
        echo $JsonRecords;
    }

    public function updateCharge() {
        $update = array(
            'updatedBy' => element('role', $this->session->userdata('logged_in')),
            'accountNoID' => $this->input->post('accountNoID'),
            'itemdescription' => $this->input->post('itemdescription'),
            'amountExcludedTax' => $this->input->post('amountExcludedTax'),
            'taxID' => $this->input->post('taxID'),
            'taxAmount' => $this->input->post('taxAmount'),
            'amountIncludedTax' => $this->input->post('amountIncludedTax'),
        );
        $this->load->model('accountPayable/accountPayable_model');
        echo $this->accountPayable_model->update_charge($this->input->post('chargeId'), $update);
    }

    function getItemDetails() {
        $JsonRecords = '{';
        $i = 0;
        $this->load->model('item/itemSetup_model');
        $default = $this->itemSetup_model->get_byCode($this->input->post('code'));
        //$default = $this->itemSetup_model->get_all();
        $n = sizeof($default);
        //print json_encode(array("status"=>"success", "message"=>$query));
        if ($default) {
            foreach ($default as $row) {

                $tax = "0";
                $taxcode = "...";
                $this->load->model('taxmaster');
                $taxObj = $this->taxmaster->taxmaster_id($row->inputTaxID);
                if ($taxObj) {
                    foreach ($taxObj as $taxrow) {
                        $tax = $taxrow->taxPercentage;
                        $taxcode = $taxrow->code;
                    }
                }
                $JsonRecords .= '"taxcode":"' . $taxcode . '","name":"' . $row->itemCode . '","description":"' . $row->description . '", "tax":"' . $tax . '", "taxID":"' . $row->inputTaxID . '"';
                if ($i < $n - 1)
                    $JsonRecords .= ',';
                $i++;
            }
            $JsonRecords .= '}';
            echo $JsonRecords;
        }else {
            echo "item Not found";
        }
    }

    public function __reset() {
        $data = array('ID' => 0, 'itemName' => '');
    }

    //// Edit purchase invoice

    function editInvoices($id) {

        $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);
        $this->lang->load("invoices", $this->session->userdata('language'));
        $this->load->model('companyChartAcct');

        $this->load->model('Usermaster');
        $data['records'] = $this->Usermaster->usermaster_all();
        $this->load->library('layouts');
        $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->load->model('Usermaster');
        $data['records'] = $this->Usermaster->usermaster_all();
        
        $this->load->model('item/itemSetup_model');
        $data['items'] = $this->itemSetup_model->get_all();

        $this->load->model('companyChartAcct');
        $data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();

        $this->load->model('taxmaster');
        $data['taxmaster'] = $this->taxmaster->taxmaster_all();

        $this->load->model('accountPayable/accountPayable_model');
        $data['pdetails'] = $this->accountPayable_model->get_Detail_byID($id);
        $data['pInvoice'] = $this->accountPayable_model->get_byID("comp_supplier_invoice", $id);

        $data['charge'] = $this->accountPayable_model->getChargeByid($id);

        $this->layouts->view('purchase/transactions/editdirectpurchaseinvoice', '', $data);
    }

// purchase invoice end
}

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