<?php
require_once 'includes/database.php';
require_once 'includes/funcs.php';
session_start();
$user = $_SESSION['reusername'];
$db=new MySQLDatabase();
$db->open_connection();
if (intval($_REQUEST['newreceipt']) >0){
			$amount = mysqli_real_escape_string($db->connection, $_REQUEST['am']);
            $pmode = mysqli_real_escape_string($db->connection, $_REQUEST['pmode']); 
            $rmks = mysqli_real_escape_string($db->connection, $_REQUEST['rmks']); 
			$recpno = mysqli_real_escape_string($db->connection, $_REQUEST['newreceipt']);
			$rtype = mysqli_real_escape_string($db->connection, $_REQUEST['rtype']);
            $ts = date("Y-m-d H:i:s");
            $bacc = mysqli_real_escape_string($db->connection, $_REQUEST['bacc']);
            $chqdet = mysqli_real_escape_string($db->connection, $_REQUEST['chqdet']); 
            $chqno = mysqli_real_escape_string($db->connection, $_REQUEST['chqno']);
			$invarr = explode(">", mysqli_real_escape_string($db->connection, $_REQUEST['ina'])); 
			$payarr = explode(">", mysqli_real_escape_string($db->connection, $_REQUEST['pya']));
            $rdate = substr($_REQUEST['rdate'],6,4)."-".substr($_REQUEST['rdate'],3,2)."-".substr($_REQUEST['rdate'] ,0,2);       
            $query="UPDATE recptrans SET pmode='$pmode', rdate='$rdate', amount = '$amount', rmks='$rmks', chqno='$chqno', chqdet='$chqdet', bacc='$bacc', us='$user', ts='$ts' WHERE recpno='$recpno'";
            $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
			$i=0;
            while ($invarr[$i] !==''){
				$ivalu = $payarr[$i];
				if($rtype ==="INSURANCE"){ if(intval($ivalu) > 0) { 
								$query="UPDATE insinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
							   else { $query="UPDATE insinvoices SET recpno = 0, paidamount = 0 WHERE invno = ".$invarr[$i]; 
							       }	
				}
				elseif ($rtype ==="TRACKER") { 
					if(intval($ivalu) > 0){ $query="UPDATE trackinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
					else{ $query="UPDATE trackinvoices SET recpno = 0, paidamount = 0 WHERE invno = ".$invarr[$i]; }
				 }
				elseif ($rtype ==="REPOSESSION") {if(intval($ivalu) > 0) { 
									$query="UPDATE repoinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i];  }
									
							   elseif ($rtype ==="OTHERS") { $query="UPDATE othinvoices SET recpno = 0, paidamount = 0 WHERE invno = ".$invarr[$i]; 
							       }
							   else { $query="UPDATE repoinvoices SET recpno = 0, paidamount = 0 WHERE invno = ".$invarr[$i]; 
							       }
				 }
				$sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				$i++;
			}
}
else {
			$amount = mysqli_real_escape_string($db->connection, $_REQUEST['am']); 
            $pmode = mysqli_real_escape_string($db->connection, $_REQUEST['pmode']); 
            $rmks = mysqli_real_escape_string($db->connection, $_REQUEST['rmks']); 
            $rtype = mysqli_real_escape_string($db->connection, $_REQUEST['rtype']);
			$cname = mysqli_real_escape_string($db->connection, $_REQUEST['cname']);
            $recpno = getnewno('recpno');
            $ts = date("Y-m-d H:i:s");
            $bacc = mysqli_real_escape_string($db->connection, $_REQUEST['bacc']);
            $chqdet = mysqli_real_escape_string($db->connection, $_REQUEST['chqdet']); 
            $chqno = mysqli_real_escape_string($db->connection, $_REQUEST['chqno']); 
			$invarr = explode(">", mysqli_real_escape_string($db->connection, $_REQUEST['ina'])); 
			$payarr = explode(">", mysqli_real_escape_string($db->connection, $_REQUEST['pya']));
            $rdate = substr($_REQUEST['rdate'],6,4)."-".substr($_REQUEST['rdate'],3,2)."-".substr($_REQUEST['rdate'] ,0,2);       
            $query="INSERT INTO recptrans (recpno, cname, pmode, rdate, rtype, amount, rmks, chqno, chqdet, bacc, us, ts) VALUES ('".$recpno."', '".$cname."', '".$pmode."', '".$rdate."', '".$rtype."', ".$amount.", '".$rmks."', '".$chqno."', '".$chqdet."', '".$bacc."', '".$user."', '".$ts."')"; 
            $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
			$i=0;
            while ($invarr[$i] !==''){
				$ivalu = $payarr[$i];
				if($rtype ==="INSURANCE"){ $query="UPDATE insinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
				elseif ($rtype ==="TRACKER") {$query="UPDATE trackinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
				elseif ($rtype ==="REPOSESSION") {$query="UPDATE repoinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
				elseif ($rtype ==="OTHERS") {$query="UPDATE othinvoices SET recpno = '".$recpno."', paidamount = ".$ivalu." WHERE invno = ".$invarr[$i]; }
				$sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				$i++;
			}
			if(!$sql){
                echo 'nothing';  
             }else{   
                echo $recpno;
                   }                                                                                  
    $db->close_connection();
}   
?>	