<?php
include_once('include.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'];
$dudate=$_REQUEST['dudate'];
$adddays=$dudate;
$old_date_timestamp = strtotime($dates);
$dfprd = date('Y-m-d', $old_date_timestamp); 
$agents=$_REQUEST['rdagents'];
$rws=$_REQUEST['rws'];
$wsdsvalues=$_REQUEST['wsdcvalues']*-1;
$bvgl=$_REQUEST['bilgross'];
$bvnl=$_REQUEST['bilnetvl'];
$userr=$_SESSION['titles'];
$psitn=$_SESSION['positions'];

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$sqlst="select max(ref_number) from online_orders ";
$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,agent_type,zone_order 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];
$rpnm=$rst[7];
$znod=$rst[8];


for($cnt=0;$cnt<$rws;$cnt++){
$items=$_REQUEST['items'.$cnt];
$prices=$_REQUEST['prices'.$cnt];
$qtys=$_REQUEST['qtys'.$cnt];
$frees=$_REQUEST['frees'.$cnt];
$vll=$_REQUEST['prices'.$cnt]*$_REQUEST['qtys'.$cnt]/100;
$wsrate=($wsdsvalues*$vll)/$rws;

$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);
$ranges=$rst[0];
$ittm=$rst[4];
$subranges=$rst[1];
$packetsperbox=$rst[2];
$packetweights=$rst[3];
$whslp=$rst[5];

if($qtys>0 ){
$sqlst = mysqli_query($conn,"insert into online_orders  (dates,ref_number,invoice_types,agent_names,chanell,zones,regions,rsm_names,executive_names,territory_names,agent_types,item_names,item_ranges,sub_ranges,box_qty,packets_per_box,packet_weights,box_price,grand_values,discount_rates,zone_orders,discount_values) values ('".$dfprd."','".$invoicenumbers."','Pr-Order','".$agnm."','".$chnl."','".$zns."','".$rgns."','".$rsm."','".$exnm."','".$trnm."','".$rpnm."','".$ittm."','".$ranges."','".$subranges."','".$qtys."','".$packetsperbox."','".$packetweights."','".$whslp."','".$bvgl."','".$wsdsvalues."','".$znod."','".$wsrate."')");}

if(mysqli_query($conn,$sqlst)==true){
$states++;}
}
echo "oks####".$invoicenumbers."####".$dates."####".$invoicenumbers."####".$invoicenumbers."####".$invoicenumbers."####";}

else{
echo "errors<td>";}



$msgs=$msgs."<p style='font-family:calibri;font-size:12;'>Sales web program-Auto generated email: Order Status Update</p>";

$msgs=$msgs."<table style='font-family:calibri;font-size:12;width:70%%;'><th style='background:silver;' colspan='2'>New Order Uploaded</th>";

$msgs=$msgs."<tr style='background:silver'><td style='border:1px solid silver;width:50%%;' >Order Value</td><td style='border:1px solid silver;width:50%%;' >".$_REQUEST['bilgross']."</td></tr>";

$msgs=$msgs."<tr style='background:silver'><td style='border:1px solid silver;width:50%%;' >Distributor</td><td style='border:1px solid silver;width:50%%;' >".$_REQUEST['rdagents']."</td></tr>";

$msgs=$msgs."<tr style='background:silver'><td style='border:1px solid silver;width:50%%;' >Region</td><td style='border:1px solid silver;width:50%%;' >".$rgns."</td></tr>";

$msgs=$msgs."<tr style='border:1px solid silver;'><td style='border:1px solid silver;' colspan='2'>".$psitn." - ".$userr."</td></tr>";
$msgs=$msgs."</table>";

$msgs=$msgs."<p style='font-family:calibri;font-size:12;'>Contact Sales Coordinator Any Issues Regarding Above Information.</p>";


$hdrs = "Content-type: text/html\r\n";
$hdrs .= 'To:niroz@diana.lk' . "\r\n";
$hdrs .= 'From: Sales web program' . "\r\n";
mail("kosala@diana.lk","Sales web program: New Sales Order",$msgs,$hdrs);
?>	

