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

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

		$sql1 = "SELECT comp_sales_creditnote.*, if (comp_sales_creditnote.updateAble=1, 'Yes', 'No' ) as confirm,    customer.customerName as customerName, comp_sales_creditnote.creditNoteDate";
		$sql2 = " FROM (comp_sales_creditnote JOIN customer ON customer.ID = comp_sales_creditnote.customerID)";						        $sql3 = " WHERE comp_sales_creditnote.companyID = '1' AND comp_sales_creditnote.creditNoteDate between '".$dateFrom."' and '".$dateTo."'";
		$sql4 = " ORDER BY creditNoteDate 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_creditnote', $updatepayment); 
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}
		
	}
	
		public function salescreditnote_unpost($dateFrom, $dateTo){
		$this->db->select('comp_sales_creditnote.*');
		$this->db->select('comp_sales_creditnote.updateAble as Yes');
		$this->db->select('customer.customerName');
		$this->db->where('comp_sales_creditnote.companyID', element('compID',	$this->session->userdata('logged_in')));
		$this->db->where('creditNoteDate >=', $dateFrom);
		$this->db->where('creditNoteDate <=', $dateTo);
		$this->db->where('comp_sales_creditnote.updateAble', 1);
		$this->db->order_by('creditNoteDate', 'desc');
		$this->db->from('comp_sales_creditnote');
		$this->db->join('customer', 'customer.ID = comp_sales_creditnote.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 salescreditnote_post($id, $data){
		$this->db->where('ID', $id);
		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_sales_creditnote', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_sales_creditnote');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	public function salescreditnote_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_sales_creditnote', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	 
// credit note details	 
	 public function salescreditnoteDetails_ID($ID){
		$this->db->where('invoiceID', $ID);
		$this->db->from('view_unpost_ar_creditnote');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 public function salescreditnoteDetails_creditnoteID($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;
		}
	 }
}
	