<?php
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
if(isset($_POST["save"])){
$agnt=$_POST['agentname'];
$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'];

$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($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')");
}

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



	


