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

class SalesPayment extends CI_Model{
	
	
	public function SalesPayment_unpost($dateFrom,$dateTo ){
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('a.receivePaymentDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->where('a.updateAble', 1);
		$this->db->select('a.*');
		$this->db->from('comp_receive_payment a');
		$this->db->select('b.customerName as customerName');
		$this->db->join('customer b', 'b.ID = a.customerID');
		$this->db->order_by('receivePaymentDate desc');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
// payment details	
	 public function SalesPaymentDetails_ID($ID){
		$this->db->where('paymentID', $ID);
		$this->db->from('view_unpost_ar_payment');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 public function SalesPaymentDetailsPost($id, $data){
		$this->db->where('ID', $id);
//		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_receive_payment', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_receive_payment');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	public function SalesPayment_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_receive_payment', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	 public function SalesPaymentDetails_paymentID($journalID, $header, $detailsData, $year, $period){
		if($detailsData){
			$seq = 0;
			echo var_dump($detailsData);
			foreach($detailsData as $row):
				if($row->amountApplied != 0){
				$seq++;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'journalID' => $journalID,
					'sequence' => $seq,
					'description' => $row->itemName,
					'acctCode' => $row->crAccountID,
					'amount_cr' => $row->amountApplied * $header->exchangeRate,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountApplied,
					'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,
					'amount_dr' => $row->amountApplied * $header->exchangeRate,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountApplied,
					'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);
				}
				if($row->discount_bankCharges != 0){
				$seq++;
				$newData = array(
					'companyID' => element('compID', $this->session->userdata('logged_in')),
					'sequence' => $seq,
					'journalID' => $journalID,
					'description' => $row->itemName,
					'acctCode' => $row->drDiscountAccountID,
					'amount_dr' => $row->discount_bankCharges * $header->exchangeRate,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->discount_bankCharges,
					'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->crDiscountAccountID,
					'amount_cr' => $row->discount_bankCharges * $header->exchangeRate,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->discount_bankCharges,
					'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);
				}
			endforeach;
		}else{
			return false;
		}
	 }
}

// Yahaya Abdollah


