<?php
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
else
{

$actp=$_REQUEST['actp'];
if($actp=='grns'){
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$ddates=$_REQUEST['ddates'];
$invdates="0";
$invnumbers=$_REQUEST['refernumbers'];
$dagents=$_REQUEST['dvfroms'];
$dstores=$_REQUEST['dvstores'];
$dvehicles=$_REQUEST['dvvehicles'];
$ddrivers=$_REQUEST['dvdrivers'];
$dcn=$_REQUEST['rcnt'];

$dvstates=0;

//$sqlst="select zone_order,zone,region,agent_type,territory_name from agentsmrd where stores_name='".$dstores."' ";
//$qry=mysqli_query($conn,$sqlst);
//$rst=mysqli_fetch_row($qry);
//$zoneorders=$rst[0];
//$zones=$rst[1];
//$regions=$rst[2];
//$agenttypes=$rst[3];
//$terrys=$rst[4];

$arritems=array();
$arrranges=array();
$arrsubranges=array();
$arrpacketsperbox=array();
$arrpacketweights=array();
$mxn=0;

$sqlst="select max(sq_number) from st_notes where note_types='FG GRN'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
$mxn=$rst[0]+1;

for($cnt=0;$cnt<$dcn;$cnt++){
$items=$_REQUEST['items'.$cnt];
$sqlstm="select distinct item_ranges,sub_ranges,packets_per_box,packet_weight,location from itemsmrd where item_name='".$items."'";
$qry=mysqli_query($conn,$sqlstm);
$rstu=mysqli_fetch_row($qry);
$rangesm=$rstu[0];
$subrangem=$rstu[1];
$packetsperboxm=$rstu[2];
$packetweightsm=$rstu[3];
$locationsm=$rstu[4];
}

$sqlst="select item_name,item_ranges,sub_ranges,packets_per_box,packet_weight from itemsmrd  where item_name='".$_REQUEST[$cnt.'_items']." '";
$qry=mysqli_query($conn,$sqlst);

$cnt=0;
while($trst=mysqli_fetch_row($qry)){
$arritems[$cnt]=$trst[0];
$arrranges[$cnt]=$trst[1];
$arrsubranges[$cnt]=$trst[2];
$arrpacketsperbox[$cnt]=$trst[3];
$arrpacketweights[$cnt]=$trst[4];
}
	
$sqlst="INSERT INTO `st_notes`(`dates`, `note_number`, `ref_dates`, `ref_numbers`, `ref_types`, `sq_number`, `note_types`,`locations`, `item_names`,
 `box_price`,`box_qty`,`ref_box_qty`,`balance_box_qty`,`stock_box_qty`,`item_ranges`, `sub_ranges`, `packets_per_box`, `packet_weights`,`user_names`,`user_dates`) VALUES ";

for($cnt=0;$cnt<$dcn;$cnt++)
{

if($_REQUEST[$cnt.'_qtys']>0)
{

$ranges=0;
$subranges=0;
$packetsperbox=0;
$packetweights=0;

for($cn=0;$cn<count($arritems);$cn++){
if($_REQUEST[$cnt.'_items']==$arritems[$cn]){
$ranges=$arrranges[$cn];
$subranges=$arrsubranges[$cn];
$packetsperbox=$arrpacketsperbox[$cn];
$packetweights=$arrpacketweights[$cn];
}}	


$sqlst=$sqlst."('".$ddates."','FG-GRN:".($mxn+19000)."','".$invdates."','".$invnumbers."','None',".$mxn.",'FG GRN','".$dstores."','".$_REQUEST['items'.$cnt]."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).
",".$_REQUEST[$cnt.'_qtys'].",0,0,".$_REQUEST[$cnt.'_qtys'].",'".$rangesm."','".$subrangem."','".$packetsperboxm."','".$packetweightsm."','".$_SESSION['usernames']."',now())";

if($cnt<($dcn-1)){
$sqlst=$sqlst.",";}	
}

}

#echo $sqlst;

if(mysqli_query($conn,$sqlst)==true){
$dvstates++;	


$vsqlst="update rc_invoices set dv_box_qty=(select sum(box_qty) from st_notes where ref_numbers='".$invnumbers."' and
 rc_invoices.item_names=st_notes.item_names and rc_invoices.box_qty=ref_box_qty and note_types='Delivery note' group by
 item_names,ref_box_qty) where invoice_number='".$invnumbers."'";


#$vsqlst="update rc_invoices set dv_box_qty=dv_box_qty+(select sum(box_qty) as qtys from st_notes where rc_invoices.invoice_number=ref_numbers and rc_invoices.item_names=
#st_notes.item_names and rc_invoices.agent_names=st_notes.agent_names and ref_numbers='".$invnumbers."' and note_numberFG-GRN:='".($mxn+16000)."' group by ref_numbers,agent_names,item_names) #where invoice_number='".$invnumbers."' and  item_names=(select distinct item_names from st_notes where note_number='FG-GRN:".($mxn+16000)."'";

//mysql_query($vsqlst,$conn);


#$vsqlst="update rc_invoices set dv_box_qty=dv_box_qty+(select distinct box_qty from st_notes where dates='".$ddates."' and 
#agent_names='".$dagents."' and note_number='FG-GRN:".($mxn+16000)."' and item_names=rc_invoices.item_names and ref_numbers='".$invnumbers."') 
#where invoice_number='".$invnumbers."' and dates='".$invdates."' and agent_names='".$dagents."' ";

}

if($dvstates>0){
echo "success***".$dvstates."***".$dcn."***".$ddates."***FG-GRN:".(16000+$mxn);}
else{
echo "errors***".$dvstates."***".$dcn."***".$ddates."***".(16000+$mxn);}
	
}
}



?>
