<?php

function settleinvoice($postdata,$xcrud){

			$invoicenumber = $postdata->get('invoice_number');
	
$id = $postdata->get('id');
	

	
	$db43 = Xcrud_db::get_instance();
	    $query = "UPDATE debtortrans set settled = '1'
		               
					  
		         
				 
				 WHERE transno=  '" . $invoicenumber . "'
				 ";
        
        $db43->query($query);
	
	
	
	$db431 = Xcrud_db::get_instance();
	    $query1 = "UPDATE debtortrans set settled = '1'
		               
					  
		         
				 
				 WHERE id =  '" . $id. "'
				 ";
        
        $db431->query($query1);
	
	
	
}
function addreceipt($postdata,$xcrud,$ebusiness){

			$p = $postdata->get('payment_method');
	$bank= $postdata->get('electronic_bank_details');
	$bankdate = $postdata->get('electronic_bank_date');
	$clientaccount = $postdata->get('account_number');
	$ref = $postdata->get('reference');
	$amount = $postdata->get('ovamount');
	$transno = $postdata->get('transno');
	$trandate = $postdata->get('trandate');
	
	
	$yeardate = date('Y',strtotime($trandate));

$monthdate =date('m',strtotime($trandate));

		
$dbp = Xcrud_db::get_instance();
	 $query22p = "SELECT 

						periodno
		                 

					  

		          FROM periods

				    WHERE YEAR(lastdate_in_period) = '" . $yeardate. "'

					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
	$dbp->query($query22p);
        $out = $dbp->result();
        foreach($out as $output){
			
			$period = $output['periodno'];
			
        } 
	
	
$db = Xcrud_db::get_instance();
	    $query = "SELECT 
		            typeno
					  
		          FROM systypes
				    
				   WHERE typeid = 12";
        
        $db->query($query);
        $out = $db->result();
        foreach($out as $output){
			
			$current = $output['typeno'];
			
        } 
	
	

	$newreceiptno = ($current + 1);
	
	if ($newreceiptno == 1){
		
		$xcrud->set_exception('transno', 'Receipt No Track!-'.$current, 'error');
	 //$ebusiness->highlight('transno', '=', $newreceiptno, 'red');
		
	}else {
	$date = date('Y-m-d H:i:s');
$postdata->set('transno', $newreceiptno);
	$postdata->set('inputdate', $date);
	$postdata->set('entered_date', $date);
	
	//now systypes
	
	
	$db43 = Xcrud_db::get_instance();
	    $query = "UPDATE systypes
		               
					  
		         set  typeno = '" . $newreceiptno . "'
				 
				 WHERE typeid = '12'";
        
        $db43->query($query);
	
	
	
       	if (($p =="EFPOS") || ($p == "Direct Deposit") || ($p == "Credit Card")) {
		
		
		$postdata->set('receipt_banked', '1');
		
		
		
			
			
			
	$db431= Xcrud_db::get_instance();
	    $query11 = "INSERT INTO banktrans (type,
								transno,
								client_account_number,
								bankact,
								ref,
								exrate,
								
								transdate,
								banktranstype,
								amount,
								currcode
								
								)
		VALUES (
			12,
			'" . $newreceiptno. "',
			'" . $clientaccount. "',
			'" . $bank. "',
			'" . $ref. "',
			'1',
			
			'" .$bankdate . "',
			'" .$p. "',
			'" . -$amount . "',
			'FJD'
		)";
        
        $db431->query($query11);
		
			
			//gl entries
			
			//debit bank
				$db4311= Xcrud_db::get_instance();
	    $query111 = "INSERT INTO gltrans (type,
								typeno,
								trandate,
								periodno,
								account,
								narrative,
								
								amount,
								accountnumber
								
								)
		VALUES (
			12,
			'" . $newreceiptno. "',
			'" . $trandate. "',
			'" . $period. "',
			'" . $bank. "',
			'" . $ref. "',
		
			'" . $amount . "',
				'" . $clientaccount. "'
		)";
        
        $db4311->query($query111);
			
			//credit debtor account
			$db5311= Xcrud_db::get_instance();
	    $query511 = "INSERT INTO gltrans (type,
								typeno,
								trandate,
								periodno,
								account,
								narrative,
								
								amount,
								accountnumber
								
								)
		VALUES (
			12,
			'" . $newreceiptno. "',
			'" . $trandate. "',
			'" . $period. "',
			'G601',
			'" . $ref. "',
		
			'" . -$amount . "',
				'" . $clientaccount. "'
		)";
        
        $db5311->query($query511);
			
			
			
		}
			
			
			
			
		
		
		
		
	}
	
	
	
}
function updatereceipt($postdata,$xcrud,$ebusiness){
	
		$p = $postdata->get('payment_method');
	
		$bank= $postdata->get('electronic_bank_details');
	$bankdate = $postdata->get('electronic_bank_date');
	$clientaccount = $postdata->get('account_number');
	$ref = $postdata->get('reference');
	$amount = $postdata->get('ovamount');
	$newreceiptno = $postdata->get('transno');
	
	if (($p =="EFPOS") || ($p == "Direct Deposit") || ($p == "Credit Card")) {
		
		
		$postdata->set('receipt_banked', '1');
		
		
		
			
			//update 
			
	$db431= Xcrud_db::get_instance();
	    $query11 = "UPDATE banktrans set client_account_number = '" . $clientaccount. "',
								         bankact = '" . $bank. "',
							     ref ='" . $ref. "',
								exrate='1',
								
								transdate='" .$bankdate . "',
								banktranstype='" .$p. "',
								amount='" . -$amount . "',
								currcode='FJD'
								
								WHERE type = 12 and transno = '" . $newreceiptno. "'
			";
        
        $db431->query($query11);
			
			
			
				
			//gl entries
			
			//debit bank
				$db4311= Xcrud_db::get_instance();
	    $query111 = "UPDATE gltrans set trandate = '" . $trandate. "',
							
								account = '" . $bank. "',
								narrative = '" . $ref. "',
								
								amount = '" . $amount . "',
								accountnumber ='" . $clientaccount. "'
								
								WHERE type = 12 and typeno = '" . $newreceiptno. "' AND amount > 0
								
								";
        
        $db4311->query($query111);
			
			
			//credit debtor
				$db43111= Xcrud_db::get_instance();
	    $query1111 = "UPDATE gltrans set trandate = '" . $trandate. "',
								
								account = '" . $bank. "',
								narrative = '" . $ref. "',
								
								amount = '" . -$amount . "',
								accountnumber ='" . $clientaccount. "'
								
								WHERE type = 12 and typeno = '" . $newreceiptno. "' AND amount < 0
								
								";
        
        $db43111->query($query1111);
			
			
			
		
		
	}
	
}
