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

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

		$sql1 = "SELECT comp_sales_debitnote.*, if (comp_sales_debitnote.updateAble=1, 'Yes', 'No' ) as confirm,    customer.customerName as customerName, comp_sales_debitnote.debitNoteDate";
		$sql2 = " FROM (comp_sales_debitnote JOIN customer ON customer.ID = comp_sales_debitnote.customerID)";						        $sql3 = " WHERE comp_sales_debitnote.companyID = '1' AND comp_sales_debitnote.debitNoteDate between '".$dateFrom."' and '".$dateTo."'";
		$sql4 = " ORDER BY debitNoteDate 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_debitnote', $updatepayment); 
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}
		
	}
	
	public function salesdebitnote_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_sales_debitnote', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	 public function salesdebitNotepost($id, $data){
		$this->db->where('ID', $id);
//		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_sales_debitnote', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_sales_debitnote');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
		public function salesdebitnote_unpost($dateFrom, $dateTo){
		$this->db->select('comp_sales_debitnote.*');
		$this->db->select('comp_sales_debitnote.updateAble as Yes');
		$this->db->select('customer.customerName');
		$this->db->where('comp_sales_debitnote.companyID', element('compID',	$this->session->userdata('logged_in')));
		$this->db->where('debitNoteDate >=', $dateFrom);
		$this->db->where('debitNoteDate <=', $dateTo);
		$this->db->where('comp_sales_debitnote.updateAble', 1);
		$this->db->order_by('debitNoteDate', 'desc');
		$this->db->from('comp_sales_debitnote');
		$this->db->join('customer', 'customer.ID = comp_sales_debitnote.customerID');
//		$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 salesdebitnote_post($id, $data){
		$this->db->where('ID', $id);
		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_sales_debitnote', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_sales_debitnote');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	 public function salesdebitNoteDetails_ID($ID){
		$this->db->where('debitnoteID', $ID);
		$this->db->from('view_unpost_ar_debitnote');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 public function salesdebitNoteDetails_debitnoteID($journalID, $header, $detailsData, $year, $period){
		if($detailsData){
			$seq = 0;
			$journalAmount = 0;
			foreach($detailsData as $row):
				if($row->amountExcludedtax != 0){
				$seq++;
				$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' => $row->amountExcludedtax * $header->exchangeRate,
					'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' => $row->amountExcludedtax * $header->exchangeRate,
					'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 + ($row->amountExcludedtax * $header->exchangeRate);
				}
				if($row->taxamount != 0){
				$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' => $row->taxamount * $header->exchangeRate,
					'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' => $row->taxamount * $header->exchangeRate,
					'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 + ($row->taxamount * $header->exchangeRate);
				}
			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;
		}
	 }
}
	