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

class directsalesinv_model extends CI_Model{
	
	
	public function directsalesinv_all(){
		$this->db->where('comp_sales_invoice.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->select('comp_sales_invoice.*');
		$this->db->select('customer.customerName as customerName');
		$this->db->from('comp_sales_invoice');
		$this->db->join('customer', 'customer.ID = comp_sales_invoice.customerID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function directsalesinv_Date($dateFrom, $dateTo){

		$sql1 = "SELECT comp_sales_invoice.*, if (comp_sales_invoice.updateAble=1, 'Yes', 'No' ) as confirm,    customer.customerName as customerName, comp_sales_invoice.salesinvoiceDate";
		$sql2 = " FROM (comp_sales_invoice JOIN customer ON customer.ID = comp_sales_invoice.customerID)";						        $sql3 = " WHERE comp_sales_invoice.companyID = '1' AND comp_sales_invoice.salesinvoiceDate between '".$dateFrom."' and '".$dateTo."'";
		$sql4 = " ORDER BY salesinvoiceDate desc"; 
		$query = $this->db->query($sql1.$sql2.$sql3.$sql4);
		
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function updatePosting($ID){
		$this->db->where('ID', $ID);
		$updatepayment = array('updateAble'=>0);
		$query = $this->db->update('comp_sales_invoice', $updatepayment); 
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}
		
	}
	
	public function directsalesinv_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_sales_invoice', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	
		public function directsalesinv_unpost($dateFrom, $dateTo){
		$this->db->select('a.*');
                $this->db->select('concat(a.formNo,"[",lpad(a.serialNo,8,"0"),"]") as formNo', false);
		$this->db->select('a.updateAble as Yes');
		$this->db->select('a.totalamount * exchangeRate as totalbaseamount');
		$this->db->select('a.totalTaxAmount * exchangeRate as totalbasetaxamount');
		$this->db->select('b.fldcurr_code');
		$this->db->where('a.companyID', element('compID',	$this->session->userdata('logged_in')));
		$this->db->where('a.salesinvoiceDate >=', $dateFrom);
		$this->db->where('a.salesinvoiceDate <=', $dateTo);
		$this->db->where('a.updateAble', 1);
		$this->db->order_by('a.salesinvoiceDate', 'desc');
		$this->db->from('comp_sales_invoice1 a');
		
		$this->db->join('_ref_currency b', 'b.fldid = a.currencyID', 'left');
//		$this->db->join('comp_sales_creditnote','comp_sales_creditnote.ID = comp_sales_creditnote.salesInvoiceID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
     
	 public function directsalesinv_post($id, $data){
		$this->db->where('ID', $id);
		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_sales_invoice', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_sales_invoice');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	 
// directsalesinv details	 
	 public function directsalesinvDetails_ID($ID){
		$this->db->where('invoiceID', $ID);
		$this->db->from('view_unpost_ar_invoice');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 public function directsalesinv_invoiceID($journalID, $header, $detailsData, $year, $period){
		if($detailsData){
			$seq = 0;
			$journalAmount = 0;
			$lineAmount = 0;
			foreach($detailsData as $row):
				if($row->amountExcludedtax != 0){
				$seq++;
				$lineAmount = $row->amountExcludedtax * $header->exchangeRate;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'journalID' => $journalID,
					'sequence' => $seq,
					'description' => $row->itemName,
					'acctCode' => $row->crAccountID,
					'supplier' => $row->customer,
					'amount_cr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountExcludedtax,
					'period' => $period,
					'year' => $year,
					'line' => $row->lineNo,
					'formNo' => $row->formNo,
					'createdBy' => element('role', $this->session->userdata('logged_in'))
				);
				$queryInsert = $this->db->insert('comp_journal_detail', $newData);
				$seq++;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'sequence' => $seq,
					'journalID' => $journalID,
					'description' => $row->itemName,
					'acctCode' => $row->drAccountID,
					'supplier' => $row->customer,
					'amount_dr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountExcludedtax,
					'period' => $period,
					'year' => $year,
					'line' => $row->lineNo,
					'formNo' => $row->formNo,
					'createdBy' => element('role', $this->session->userdata('logged_in'))
				);
				$queryInsert = $this->db->insert('comp_journal_detail', $newData);
				$journalAmount = $journalAmount + $lineAmount;
				}
				if($row->taxamount != 0){
				$lineAmount = $row->taxamount * $header->exchangeRate;
				$seq++;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'sequence' => $seq,
					'journalID' => $journalID,
					'description' => $row->itemName,
					'acctCode' => $row->drTaxAccountID,
					'supplier' => $row->customer,
					'amount_dr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->taxamount,
					'period' => $period,
					'year' => $year,
					'line' => $row->lineNo,
					'formNo' => $row->formNo,
					'createdBy' => element('role', $this->session->userdata('logged_in'))
				);
				$queryInsert = $this->db->insert('comp_journal_detail', $newData);
				$seq++;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'sequence' => $seq,
					'journalID' => $journalID,
					'description' => $row->itemName,
					'acctCode' => $row->crTaxAccountID,
					'supplier' => $row->customer,
					'amount_cr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->taxamount,
					'period' => $period, 
					'year' => $year,
					'line' => $row->lineNo,
					'formNo' => $row->formNo,
					'createdBy' => element('role', $this->session->userdata('logged_in'))
				);
				$queryInsert = $this->db->insert('comp_journal_detail', $newData);
				$journalAmount = $journalAmount + $lineAmount;
				}
			endforeach;
			if($journalAmount != 0){
				$data = array(
					'total_amount_dr'=>$journalAmount,
					'total_amount_cr'=>$journalAmount,
					'total_amount'=>0
				);
				$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
				$this->db->where('journalID', $journalID);
				$queryJournal = $this->db->update('comp_journal', $data); 
			}
		}else{
			return false;
		}
	 }
}
	