<?php
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
else
{
$actp=$_REQUEST['actp'];
if($actp=='dvnotes'){
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$ddates=$_REQUEST['ddates'];
$invdates=$_REQUEST['invdates'];
$invnumbers=$_REQUEST['invnumbers'];
$dagents=$_REQUEST['invagents'];
$dvehicles=$_REQUEST['dvvehicles'];
$ddrivers=$_REQUEST['dvdrivers'];
$dcn=$_REQUEST['rcnt'];
$dvstates=0;

$sqlst="select zone_orders,zones,regions,agent_types,territory_names from rc_freeinvoices where invoice_number='".$invnumbers."' and dates='".
$invdates."' and agent_names='".$dagents."' ";

$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];
$mxn=0;

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

for($cnt=0;$cnt<$dcn;$cnt++){
$items=$_REQUEST['items'.$cnt];
$itemcodes=$_REQUEST['itemcodes'.$cnt];
$storelocations=$_REQUEST['storelocations'.$cnt];
//$frees=$_REQUEST['frees'.$cnt];
$sqlstm="select distinct item_ranges,sub_ranges,packets_per_box,packet_weight,location,com_tec,whole_sale_price from itemsmrd where itemcode='".$itemcodes."'";
$qry=mysqli_query($conn,$sqlstm);
$rstu=mysqli_fetch_row($qry);
$rangesm=$rstu[0];
$subrangesm=$rstu[1];
$packetsperboxsm=$rstu[2];
$packetweightsm=$rstu[3];
$locationtsm=$rstu[4];
$comtecc=$rstu[5];
$boxprice=$rstu[6];
$stkpcs=$_REQUEST[$cnt.'_qtys']*-1;
$stkbox=$stkpcs/$packetsperboxsm;
if($_REQUEST[$cnt.'_qtys']>0)
{

$sqlst="INSERT INTO st_notes(dates,note_number,ref_dates,ref_numbers,ref_types,sq_number,note_types,zone_orders,zones,regions,agent_types,agent_names,territory_names,locations,stores_names,itemcodes,item_names,pcs_price,box_price,ref_box_qty,free_pcs,stock_box_qty,item_ranges,sub_ranges,packets_per_box,packet_weights,vehicle_names,driver_names,com_tecs,user_names,box_mrp,user_dates) VALUES('".$ddates."','FISU:".($mxn+19000)."','".$invdates."','".$invnumbers."','Invoice',".$mxn.",'Fisue note',".$zoneorders.",'".$zones."','".$regions."','".$agenttypes."','".$dagents."','".$terrys."','".$locationtsm."','".$storelocations."','".$itemcodes."','".$items."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).
",".$boxprice.",".$_REQUEST[$cnt.'_refqtys'].",".$_REQUEST[$cnt.'_qtys'].",".$stkbox.",'".$rangesm."','".$subrangesm."',".$packetsperboxsm.",".$packetweightsm.",'".
$dvehicles."','".$ddrivers."','".$comtecc."','".$_SESSION['usernames']."',".$_REQUEST[$cnt.'_mrprates'].",now())";
}
if(mysqli_query($conn,$sqlst)==true){
$dvstates++;}
}
}
$vsqlst="update rc_freeinvoices set dv_pcs_qty=(select sum(free_pcs) from st_notes where ref_numbers='".$invnumbers."' and rc_freeinvoices.item_names=st_notes.item_names and rc_freeinvoices.pcs_qty=ref_box_qty and note_types='Fisue note' group by item_names,ref_box_qty) where invoice_number='".$invnumbers."'";
mysqli_query($conn,$vsqlst);
}

if($dvstates>0){
echo "success***".$dvstates."***".$dcn."***".$ddates."***DV:".(19000+$mxn);}
else{
echo "errors***".$dvstates."***".$dcn."***".$ddates."***".(19000+$mxn);}

?>
