<?php
function ptax_calculate($postdata,$xcrud){

	$invoice = $postdata->get('invoice');
   
$db41 = Xcrud_db::get_instance();
	    $query = "SELECT 
		             ovamount,
					 ovgst,
					 ovdiscount
		               
					  
		          FROM debtortrans
				    
				   WHERE transno = '" . $invoice . "'";
        
        $db41->query($query);
        $out41 = $db41->result();
        foreach($out41 as $output41){
			
			$amount=$output41['ovamount'];
			$vat=$output41['ovgst'];
			
            $discount=$output41['ovdiscount'];
        } 

	$total = $amount + $vat + $discount;
	
	
$postdata->set('invoice_amount', $total);
	
	
	
	//now update ptax
	
	$db42 = Xcrud_db::get_instance();
	    $query = "SELECT 
		            rate
		               
					  
		          FROM taxrates
				    
				   WHERE taxid = 2";
        
        $db42->query($query);
        $out42 = $db42->result();
        foreach($out42 as $output42){
			
			$rate=$output42['rate'];
			
        } 
	
	
$totalvep = $total-$vat;
	
	$ptax = $totalvep *($rate/100);
	
	$postdata->set('ptax', $ptax);
	
	//update debtortrans to insert a ptax
	$db43 = Xcrud_db::get_instance();
	    $query = "UPDATE debtortrans
		               
					  
		         set  ptax = '" . $ptax . "'
				 
				 WHERE transno = '" . $invoice . "'";
        
        $db43->query($query);
       
	
	// determine department
	$db48 = Xcrud_db::get_instance();
	    $query = "SELECT 
		            department
		               
					  
		          FROM debtortrans
				    
				   WHERE transno = '" . $invoice . "'";
        
        $db48->query($query);
        $out48 = $db48->result();
        foreach($out48 as $output48){
			
			$department=$output48['department'];
			
        } 
	
	//insert journals
	
	//get periods 
	$debtorno = $postdata->get('name');
	$date = $postdata->get('date');
	$yeardate = date('Y',strtotime($date));

	$monthdate = date('m',strtotime($date));
	$transdate =  date('Y-m-d h:i:s',strtotime($date));
	$db412 = Xcrud_db::get_instance();
	    $query = "SELECT 
						periodno,
		             lastdate_in_period
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate . "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
        
        $db412->query($query);
        $out412 = $db412->result();
        foreach($out412 as $output412){
			
			$PeriodNo=$output412['periodno'];
			
        } 
	
	//determine next transno
	
	$db4122 = Xcrud_db::get_instance();
	    $query = "select transno from debtortrans
					order by transno desc
						limit 1";
        
        $db4122->query($query);
        $out4122 = $db4122->result();
        foreach($out4122 as $output4122){
			
			$lasttransno=$output4122['transno'];
			
        } 
	
	$TransNo = $lasttransno + 1;
	
	//debtortrans
	$db441 = Xcrud_db::get_instance();
	    $query = "INSERT INTO debtortrans (transno,
									type,
									debtorno,
									branchcode,
									trandate,
									inputdate,
									prd,
									settled,
									reference,
									tpe,
									order_,
									rate,
									ovamount,
								
							
									
									department,
									entered_by)
				VALUES (
					'" . $TransNo . "',
					'52',
					'" . $debtorno . "',
					'" . $debtorno . "',
					'" . $date . "',
					'" . $transdate . "',
					
					'" . $PeriodNo . "',
					'1',
					'ptax',
					'DE',
					'" . $TransNo . "',
					'1',
					'" . -$ptax. "',
					
								
					'" . $department . "',
					'admin'
					)";
        
        $db441->query($query);
	
	
	
	//first increase ptax 
	$db44 = Xcrud_db::get_instance();
	    $query = "INSERT INTO gltrans (type,
									typeno,
									trandate,
									periodno,
									account,
									narrative,
									amount,
									posted,
									tag,
									departmentid)
				VALUES ('0',
					'" . $TransNo . "',
					'" . $date . "',
					'" . $PeriodNo . "',
					'12000022',
					'" . $invtxt. "',
					'" . $ptax. "',
					'1',
					'" . $department. "',
					'" . $department. "'
					)";
        
        $db44->query($query);
       
	//decrease in AR 
	
	
		$db49 = Xcrud_db::get_instance();
	    $query = "INSERT INTO gltrans (type,
									typeno,
									trandate,
									periodno,
									account,
									narrative,
									amount,
									posted,
									tag,
									departmentid)
				VALUES ('0',
					'" . $TransNo . "',
					'" . $date . "',
					'" . $PeriodNo . "',
					'1200002',
					'" . $invtxt. "',
					'" . -$ptax. "',
					'1',
					'" . $department. "',
					'" . $department. "'
					)";
        
        $db49->query($query);
	
	
	
}
