<?php
require "showshp.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'];
$agents=$_REQUEST['rdagents'];
$rws=$_REQUEST['rws'];
$dcsvalues=$_REQUEST['rtdcvalues']+$_REQUEST['wsdcvalues'];
$rtdsvalues=$_REQUEST['rtdcvalues'];
$wsdsvalues=$_REQUEST['wsdcvalues'];
$mrks=$_REQUEST['rdmrks'];
$scalls=$_REQUEST['rdscalls'];
$mcalls=$_REQUEST['rdmcalls'];
$pcalls=$_REQUEST['rdpcalls'];
$routes=$_REQUEST['routes'];
$cash=$_REQUEST['cash'];
$cheqs=$_REQUEST['cheqs'];
$credits=$_REQUEST['credits'];
$collection=$_REQUEST['collection'];
$expens=$_REQUEST['expens'];
$wd=$wd+($rws);
$avdistot=$dcsvalues/$wd;
$rtdis=$rtdsvalues/$wd;
$wsdis=$wsdsvalues/$wd;
$pcl=$pcalls/$wd;
$mcl=$mcalls/$wd;
$scl=$scalls/$wd;
$distot=$rtdis+$wsdis;

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$sqlst="select max(invoice_number) from rd_invoices ";
$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 zone_order,zone,region,rsm_name,executive_name,agent_type,territory_name,range_type,rep_name,chanelname,agent_name from agentsmrd where agent_replc='".$agents."'";
$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];
$agtss=$rst[10];

for($cnt=0;$cnt<$rws;$cnt++){
$items=$_REQUEST['items'.$cnt];
$prices=$_REQUEST['prices'.$cnt];
$qtys=$_REQUEST['qtys'.$cnt];
$qtyst=$_REQUEST['qtys'.$cnt]*-1;
$frees=$_REQUEST['frees'.$cnt];
$vll=$_REQUEST['prices'.$cnt]*$_REQUEST['qtys'.$cnt];
$rtrate=$rtdsvalues/$vll;
$wsrate=$wsdsvalues/$vll;
$disrate=$dcsvalues/$vll;

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

$sqlst="insert into rd_invoices  (dates,invoice_number,invoice_types,item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,zone_orders,zones,regions,rsm_names,executive_names,agent_names,agent_types,
territory_names, rep_names,range_types,pcs_price,pcs_qty,free_qtys,discount_values,rtdiscount_values,wsdiscount_values,market_return_values,scalls,mcalls,pcalls,routes,grand_values,rtdiscount_rates,wsdiscount_rates,discount_rates,base_values,chanell,user_names,cash_sales,credit_sales,cheq_sales,collection_sales,expenses,stock,user_dates) values ('".$dates."','".$invoicenumbers."','RD day sales','".$items."','".$ranges."','".$subranges."',".$packetsperbox.",".$packetweights.
",".$zoneorders.",'".$zones."','".$regions."','".$rsmnames."','".$executivenames."','".$agtss."','".$agenttypes."','".$terrys."','".$reps."','".$rangetypes."',".$prices.",".$qtys.
",".$frees.",".$distot.",".$rtdis.",".$wsdis.",".$mrks.",".$scl.",".$mcl.",".$pcl.",'".$routes."','".$vll."','".$rtrate."','".$wsrate."','".$disrate."','".$avdistot."','".$chl."','".$_SESSION['usernames']."','".$cash."','".$cheqs."','".$credits."','".$collection."','".$expens."',".$qtyst.
",now())";

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


	


