<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
if(isset($_POST["save"])){
$agnt=$_POST['agentname'];
$invoice=$_POST['invnumber'];
$invdate=$_POST['invdt'];
$undlv=$_POST["undlvsr"];
$invamount=$_POST["invamt"];
$dlvamount=$_POST["dlvamt"];
$dlvdate=$_POST["deliverydate"];
$claim=$_POST["addclaim"];
$claimdate=$_POST["climdate"];
$charges=$_POST["addcharges"];
$srtns=$_POST["dedsrtn"];
$mrtn=$_POST["dedmrtn"];
$cashamt=$_POST["cashpay"];
$cashbankdetail=$_POST["cashbank"];
$cashdepositdate=$_POST["cashbnkdate"];
$chqamount=$_POST["chqpay1"];
$chqrcvdate=$_POST["chq1chqdate"];
$chqbnkdetail=$_POST["chqbank1"];
$chqrelzdate=$_POST["chq1bnkdate"];
$chqbnkdate=$_POST["chq1chqdate"];
$chqnum=$_POST["chqnum"];
$chargcoment=$_POST['chargeremark'];
$srtncoment=$_POST['srtnremark'];
$mrtncoment=$_POST['mrtnremark'];
$undlvcoment=$_POST['undlvremark'];
$paystatus=$_POST['paysts'];

if($paystatus=='PAYMENT NR'){
$psts='1';   
}else{
$psts='0';    
}

if($paystatus=='ALTERNATION'){
$psts='1';   
}else{
$psts='0';    
}

if($paystatus=='UNLD UNDLV ISSUE'){
$psts='1';   
}else{
$psts='0';    
}

if($paystatus=='CASH NOT DEPOSIT'){
$psts='1';   
}else{
$psts='0';    
}

if($paystatus=='INV COPY NR'){
$psts='1';   
}else{
$psts='0';    
}

if($paystatus=='OVER DATE'){
$psts='1';   
}else{
$psts='0';    
}

$sqq="select sum(claim_amount),sum(charges_amount),sum(srtn_amount),sum(undlv_amount),sum(mrtn_amount),sum(inv_amount) as invtots from st_paymode where invoice_number='".$invoice."'  group by invoice_number";
$qrys=mysqli_query($conn,$sqq);
$rstm=mysqli_fetch_row($qrys);
$invtot=$invtot+$rstm[5];

if($claim==number_format($rstm[0],2,'.',',')){
$claimamount='';    
}else{
$claimamount=$claim;
}
if($invamount==number_format($rstm[5],2,'.',',')){
$invs='';    
}else{
$invs=$invamount;
}
if($charges==number_format($rstm[1],2,'.',',')){
$chrgamt='';    
}else{
$chrgamt=$charges;    
}
if($srtns==number_format($rstm[2],2,'.',',')){
$srtvl='';    
}else{
$srtvl=$srtns;    
}
if($undlv==number_format($rstm[3],2,'.',',')){
$undlvr='';    
}else{
$undlvr=$undlv;    
}
if($mrtn==number_format($rstm[4],2,'.',',')){
$mrvll='';    
}else{
$mrvll=$mrtn;    
}

$sqlst="select distinct zone_order,zone,region,rsm_name,executive_name,agent_type,territory_name,range_type,rep_name,chanelname,sub_region,stores_name from agentsmrd where agent_name='".$agnt."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$zoneorders=$rst[0];
$zones=$rst[1];
$regions=$rst[2];
$rsmnames=$rst[3];
$executivenames=$rst[4];
$agenttypes=$rst[5];
$terrys=$rst[6];
$rangetypes=$rst[7];
$reps=$rst[8];
$chl=$rst[9];
$subrgn=$rst[10];
$location=$rst[11];

if($invtot==''){

$resultin = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,inv_amount,coment_area,dlv_dates,tot_amounts) values ('".$invdate."','".$invoice."','Invoice','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$invs."','Invoice','".$dlvdate."','".$invs."')");
$qq=mysqli_query ($conn,"update rc_invoices set paysts='2' where invoice_number='".$invoice."'");
}

if($cashamt>=0 || $chqamount>=0 && $claim>0 && ($charges>=0 || $srtns>=0 || $mrtn>=0 || $undlv>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,claim_amount,coment_area,dlv_dates,claim_month,tot_amounts) values ('".$invdate."','".$invoice."','Claim','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$claimamount."','".$claimdate."','".$dlvdate."','".$claimdate."','".($claimamount*-1)."')");
}

if($cashamt>=0 || $chqamount>=0 && $charges>0 && ($claim>=0 || $srtns>=0 || $mrtn>=0 || $undlv>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,charges_amount,coment_area,dlv_dates,tot_amounts) values ('".$invdate."','".$invoice."','Charges-Rtnchq','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$chrgamt."','".$chargcoment."','".$dlvdate."','".$chrgamt."')");
}

if($cashamt>=0 || $chqamount>=0 && $srtns>0 && ($claim>=0 || $charges>=0 || $mrtn>=0 || $undlv>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,srtn_amount,coment_area,dlv_dates,tot_amounts) values ('".$invdate."','".$invoice."','Srtn','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$srtvl."','".$srtncoment."','".$dlvdate."','".($srtvl*-1)."')");
}

if($cashamt>=0 || $chqamount>=0 && $mrtn>0 && ($claim>=0 || $charges>=0 || $srtns>=0 || $undlv>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,mrtn_amount,coment_area,dlv_dates,tot_amounts) values ('".$invdate."','".$invoice."','Mrtn','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$mrvll."','".$mrtncoment."','".$dlvdate."','".($mrvll*-1)."')");
}

if($cashamt>=0 || $chqamount>=0 && $undlv>0 && ($claim>=0 || $charges>=0 || $srtns>=0 || $mrtn>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,undlv_amount,coment_area,dlv_dates,tot_amounts) values ('".$invdate."','".$invoice."','Undlv','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$undlvr."','".$undlvcoment."','".$dlvdate."','".($undlvr*-1)."')");
}

if($cashamt>0 && $chqamount>=0 && ($claim>=0 || $charges>=0 || $srtns>=0 || $mrtn>=0 || $undlv>=0)){
$result = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,cash_amount,bank_name,payrcv_date,deposit_date,coment_area,dlv_dates,tot_amounts,chq_pass) values ('".$invdate."','".$invoice."','cash','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$cashamt."','".$cashbankdetail."','".$cashdepositdate."','".$cashdepositdate."','Cash','".$dlvdate."','".($cashamt*-1)."','Cash')");
}

if($chqamount>0 && $cashamt>=0 && ($claim>=0 || $charges>=0 || $srtns>=0 || $mrtn>=0 || $undlv>=0)){
$resultn = mysqli_query($conn,"insert into st_paymode (dates,invoice_number,	pay_types,zone_orders,zones,regions,sub_regions,agent_types,agent_names,territory_names,rsm_names,executive_names,rep_names,stores_names,cheq_amount,bank_name,chq_number,payrcv_date,deposit_date,coment_area,dlv_dates,tot_amounts,chq_pass) values ('".$invdate."','".$invoice."','Cheq','".$zoneorders."','".$zones."','".$regions."','".$subrgn."','".$agenttypes."','".$agnt."','".$terrys."','".$rsmnames."','".$executivenames."','".$reps."','".$location."','".$chqamount."','".$chqbnkdetail."','".$chqnum."','".$chqbnkdate."','".$chqrelzdate."','Cheque:".$chqnum."','".$dlvdate."','".($chqamount*-1)."','Cheq to realize')");
}

$qt=mysqli_query ($conn,"update rc_invoices set payments='".$paystatus."' where invoice_number='".$invoice."'");
$qts=mysqli_query ($conn,"update st_paymode set payments='".$paystatus."' where invoice_number='".$invoice."'");
$qtg=mysqli_query ($conn,"update rc_invoices set paysts='".$psts."' where invoice_number='".$invoice."'");

$sqlstn="delete from st_paymode where tot_amounts='0'";
$qry=mysqli_query($conn,$sqlstn);
header("Location: addrecpay.php");
}
?>



	


