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

class CreditNote_model extends CI_Model{
	
	
	public function creditnote_all(){
		$this->db->where('comp_supplier_creditnote.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->select('comp_supplier_creditnote.*');
		$this->db->select('supplier.supplierName as supplierName');
		$this->db->from('comp_supplier_creditnote');
		$this->db->join('supplier', 'supplier.ID = comp_supplier_creditnote.supplierID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function creditnote_id($ID){
		$this->db->select('a.*');
		$this->db->select('b.*');
		$this->db->select('concat(a.formNo,"[",lpad(a.ID,8,"0"),"]") as creditNoteNo', false);
		$this->db->where('a.ID', $ID);
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('comp_supplier_creditnote a');
		$this->db->join('supplier b', 'b.ID = a.supplierID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function creditnote_Date($dateFrom, $dateTo){
		$sql1 = "SELECT comp_supplier_creditnote.*, if (comp_supplier_creditnote.updateAble=1, 'Yes', 'No' ) as confirm, supplier.supplierName as supplierName, comp_supplier_invoice.supplierInvoiceNo";
		$sql1 .= " , concat(comp_supplier_creditnote.formNo, '[',Lpad(comp_supplier_creditnote.ID, 8, '0'),']' ) as creditNoteNo";
		$sql2 = " FROM (comp_supplier_creditnote JOIN supplier ON supplier.ID = comp_supplier_creditnote.supplierID) JOIN comp_supplier_invoice ON comp_supplier_invoice.ID = comp_supplier_creditnote.supplierInvoiceID";
		$sql3 = " WHERE comp_supplier_creditnote.companyID = '".element('compID',$this->session->userdata('logged_in'))."' AND comp_supplier_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 creditnote_unpost($dateFrom, $dateTo){
		$this->db->select('a.*');
		$this->db->select('a.updateAble as Yes');
		$this->db->select('b.supplierName');
		$this->db->select('c.formNo as supplierInvoiceNo');
		$this->db->where('a.companyID', element('compID',$this->session->userdata('logged_in')));
		$this->db->where('creditNoteDate >=', $dateFrom);
		$this->db->where('creditNoteDate <=', $dateTo);
		$this->db->where('a.updateAble', 1);
		$this->db->order_by('creditNoteDate', 'desc');
		$this->db->from('comp_supplier_creditnote a');
		$this->db->join('supplier b', 'b.ID = a.supplierID');
		$this->db->join('comp_supplier_invoice c','c.ID = a.supplierInvoiceID', 'left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
     
	 public function creditNotepost($id, $data){
		$this->db->where('ID', $id);
		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_supplier_creditnote', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_supplier_creditnote');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	 
	 public function creditnotedetails_creditnoteID($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->supplier,
					'amount_cr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountExcludedtax,
					'period' => $period,
					'year' => $year,
					'line' => $row->ID,
					'formNo' => $header->formNo . '[' . sprintf('%08d', $header->ID) . ']',
					'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->supplier,
					'amount_dr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->amountExcludedtax,
					'period' => $period,
					'year' => $year,
					'formNo' => $header->formNo . '[' . sprintf('%08d', $header->ID) . ']',
					'line' => $row->ID,
					'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->supplier,
					'amount_dr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->taxamount,
					'period' => $period,
					'year' => $year,
					'formNo' => $header->formNo . '[' . sprintf('%08d', $header->ID) . ']',
					'line' => $row->ID,
					'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->supplier,
					'amount_cr' => $lineAmount,
					'currencyID' => ($header->currencyID == 0)? 1: $header->currencyID,
					'exchange_rate' => $header->exchangeRate,
					'currency_amount' => $row->taxamount,
					'period' => $period, 
					'year' => $year,
					'formNo' => $header->formNo . '[' . sprintf('%08d', $header->ID) . ']',
					'line' => $row->ID,
					'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;
		}
	 }
	 
	 public function creditNotePrintDetails_ID($ID){
		$this->db->select("scDet.*,CONCAT('[',i.itemCode,'] ',i.name) as itemNamCode",false)
				->where('creditnoteID', $ID)
				->from('comp_supplier_creditnote_detail as scDet')
				->join('item as i','i.ID = scDet.itemID','left');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 
	 public function creditNoteDetails_ID($ID){
		$this->db->where('creditnoteID', $ID);
		$this->db->from('view_unpost_ap_creditnote');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	 public function journalCreateHead($data, $module){
			$this->load->model('Glseq');
			$query = $this->Glseq->glSequence_jl();
			$newData = array(
				'companyID' => element('compID', $this->session->userdata('logged_in')),
				'journalID' => $query['seqNumber'],
				'description' => $this->input->post('description'),
				'module' => '1',
				'effective_date' => date("Y-m-d", strtotime($this->input->post('effdate'))),
				'year' => $query['financialYear'],
				'period' => $query['period'],
				'createdBy' => element('userid', $this->session->userdata('logged_in'))
			);
			$query2 = $this->Journal->journal_insert($newData);
	 }
}
	