<?php
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
if(isset($_REQUEST['actp'])){
$states=0;
$dates=$_REQUEST['rddates'];
$old_date_timestamp = strtotime($dates);
$dfmt = date('Y-m-d', $old_date_timestamp); 
$agents=$_REQUEST['rdagents'];
$rws=$_REQUEST['rws'];
$wsdsvalues=$_REQUEST['wsdcvalues']*-1/$rws;
$routes=$_REQUEST['slroute'];
$bvgl=$_REQUEST['bilgross']/$rws;
$bvnl=$_REQUEST['bilnetvl']/$rws;
$scall=$_REQUEST['shedulecl']/$rws;
$mcall=$_REQUEST['madecl']/$rws;
$pcall=$_REQUEST['prodcal']/$rws;
$chqcollect=$_REQUEST['cheqcollect']/$rws;
$cashcollect=$_REQUEST['cashcollect']/$rws;
$srtnvl=$_REQUEST['invsrtn']*-1/$rws;
$mrtnvl=$_REQUEST['invmrtn']*-1;
$srtnd=$_REQUEST['srtndis']/$rws;
$mrtnd=$_REQUEST['mrtndis']/$rws;
$cash=$_REQUEST['cash']/$rws;
$credit=$_REQUEST['credit']/$rws;
$cheq=$_REQUEST['cheq']/$rws;


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$sqlst="select max(invoice_number) from invoice_counter";
$qry=mysqli_query($conn,$sqlst);
$invcc=mysqli_fetch_row($qry);
if(mysqli_num_rows($qry)==0){
$invc=date('y')."0001";}
else{
if(intval($invcc[0],10)==0){
$invc=date('y')."0001";}
else{
$invc=intval($invcc[0],10)+1;}
}


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$sqlst="select max(invoice_number) from dbrdsales";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

if(mysqli_num_rows($qry)==0){
$invoicenumbers=date('Y')."0001";}
else{
if(intval($rst[0],10)==0){
$invoicenumbers=date('y')."0001";}
else{
$invoicenumbers=intval($rst[0],10)+1;}
}

$sqlst="select distinct agent_name,chanelname,zone,region,rsm_name,executive_name,territory_name,zone_order,agent_type from agentsmrd where agent_name='".$agents."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$agnm=$rst[0];
$chnl=$rst[1];
$zns=$rst[2];
$rgns=$rst[3];
$rsm=$rst[4];
$exnm=$rst[5];
$trnm=$rst[6];
$znod=$rst[7];
$agtyp=$rst[8];


for($cnt=0;$cnt<$rws;$cnt++){
$items=$_REQUEST['items'.$cnt];
$prices=$_REQUEST['prices'.$cnt];
$qtys=$_REQUEST['qtys'.$cnt];
$frees=$_REQUEST['frees'.$cnt];
$srtns=$_REQUEST['srtns'.$cnt];
$mrtns=$_REQUEST['mrtns'.$cnt];
$ssrtns=$srtns*-1;
$mrrtns=$mrtns*-1;
$vll=$_REQUEST['prices'.$cnt]*$_REQUEST['qtys'.$cnt];
$srvlrw=$_REQUEST['prices'.$cnt]*$_REQUEST['srtns'.$cnt];
$mrtnwl=$_REQUEST['prices'.$cnt]*$_REQUEST['mrtns'.$cnt];
$wsrate=($wsdsvalues/$bvgl)*100;
$srrate=($srtnd/$srvlrw)*100;
$mrrate=($mrtnd/$mrtnwl)*100;
$rtnpric=($srtnvl-$srtnd)/$srtns;


$sqlst="select distinct item_ranges,sub_ranges,rd_uom,packet_weight,item_name,whole_sale_price from itemsmrd where item_name_mrt='".$items."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$ittm=$rst[4];
$ranges=$rst[0];
$subranges=$rst[1];
$packetsperbox=$rst[2];
$packetweights=$rst[3];
$whslp=$rst[5];

$resultcv=mysqli_query($conn,"insert into invoice_counter (invoice_number)values('".$invc."')");


if($qtys>0){
$sqlst = mysqli_query($conn,"insert into dbrdsales  (dates,invoice_number,invoice_types,agent_names,chanell,zones,regions,rsm_names,executive_names,territory_names,dis_values,item_names,item_ranges,item_subranges,pcs_qty,pcs_price,free_qtys,packets_per_box,packets_weight,box_price,grand_values,manu_bill,dis_rates,slroutes,scalls,mcalls,pcalls,cashs,credits,cheques,chqcollect,cashcollect,zone_orders,agent_types) values ('".$dfmt."','".$invoicenumbers."','RD-Sales','".$agnm."','".$chnl."','".$zns."','".$rgns."','".$rsm."','".$exnm."','".$trnm."','".$wsdsvalues."','".$ittm."','".$ranges."','".$subranges."','".$qtys."','".$prices."','".$frees."','".$packetsperbox."','".$packetweights."','".$whslp."','".$bvgl."','".$invc."','".$wsrate."','".$routes."','".$scall."','".$mcall."','".$pcall."','".$cash."','".$credit."','".$cheq."','".$chqcollect."','".$cashcollect."','".$znod."','".$agtyp."')");}

if($srtns>0){
$sqlst = mysqli_query($conn,"insert into dbrdsales  (dates,invoice_number,invoice_types,agent_names,chanell,zones,regions,rsm_names,executive_names,territory_names,item_names,item_ranges,item_subranges,packets_per_box,packets_weight,box_price,manu_bill,slroutes,zone_orders,agent_types,rtn_pcs,rtn_value,rtn_dis,rtn_price) values ('".$dfmt."','".$invoicenumbers."','Sales-Rtns','".$agnm."','".$chnl."','".$zns."','".$rgns."','".$rsm."','".$exnm."','".$trnm."','".$items."','".$ranges."','".$subranges."','".$packetsperbox."','".$packetweights."','".$whslp."','".$invc."','".$routes."','".$znod."','".$agtyp."','".$srtns."','".$srtnvl."','".$srtnd."','".$prices."')");}

if($mrtns>0){
$sqlst = mysqli_query($conn,"insert into dbrdsales  (dates,invoice_number,invoice_types,agent_names,chanell,zones,regions,rsm_names,executive_names,territory_names,item_names,item_ranges,item_subranges,packets_per_box,packets_weight,box_price,manu_bill,slroutes,zone_orders,agent_types,mrtn_pcs,mrtn_value,mrtn_dis,mrtn_price) values ('".$dfmt."','".$invoicenumbers."','Market-Rtns','".$agnm."','".$chnl."','".$zns."','".$rgns."','".$rsm."','".$exnm."','".$trnm."','".$items."','".$ranges."','".$subranges."','".$packetsperbox."','".$packetweights."','".$whslp."','".$invc."','".$routes."','".$znod."','".$agtyp."','".$mrtns."','".$mrtnvl."','".$mrtnd."','".$prices."')");}

if(mysqli_query($conn,$sqlst)==true){
$states++;}
}
echo "oks####".$invoicenumbers."####".$dates."####".$agnm."####".$bvnl."####".$invc."####";}
else{
echo "errors<td>";}
	


