<?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_diana");

$ddates=$_REQUEST['ddates'];
$invdates=$_REQUEST['invdates'];
$invnumbers=$_REQUEST['invnumbers'];
$dagents=$_REQUEST['invagents'];
//$dstores=$_REQUEST['dvstores'];
$dvehicles=$_REQUEST['dvvehicles'];
$ddrivers=$_REQUEST['dvdrivers'];
$rmks=$_REQUEST['w3review'];
$dcn=$_REQUEST['rcnt'];
$dvstates=0;

$sqlst="select zone_orders,zones,regions,agent_types,territory_names from rc_invoices 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='Undelivery 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['itemcode'.$cnt];
$storelocations=$_REQUEST['storelocation'.$cnt];
//$rmks=$_REQUEST['rmkss'.$cnt];
$sq="select distinct item_ranges,sub_ranges,rd_uom,packet_weight,location,com_tec from itemsmrd where itemcode='".$itemcodes."'";
$qry=mysqli_query($conn,$sq);
$rstn=mysqli_fetch_row($qry);
$rangesm=$rstn[0];
$subrangesm=$rstn[1];
$packetsperboxm=$rstn[2];
$packetweightsm=$rstn[3];
$locationsm=$rstn[4];
$comtec=$rstn[5];
$uom=$rstn[2];

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,com_tecs,locations,stores_names,itemcodes,item_names,box_price,box_mrp,remark_naration,box_qty,ref_box_qty,balance_box_qty,stock_box_qty,item_ranges,sub_ranges,packets_per_box, packet_weights,vehicle_names,driver_names,pcs_qty,user_names,user_dates)VALUES('".$ddates."','UNDV:".($mxn+19000)."','".$invdates."','".$invnumbers."','Invoice',".$mxn.",'Undelivery note',".$zoneorders.",'".$zones."','".$regions.
"','".$agenttypes."','".$dagents."','".$terrys."','".$comtec."','".$locationsm."','".$storelocations."','".$itemcodes."','".$items."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).
",".str_replace(',','',$_REQUEST[$cnt.'_mrp']).
",'".$rmks."',".$_REQUEST[$cnt.'_qtys'].",".$_REQUEST[$cnt.'_refqtys'].",".$_REQUEST[$cnt.'_qbalances'].",'0','".$rangesm."','".$subrangesm."',".$packetsperboxm.",".$packetweightsm.",'".
$dvehicles."','".$ddrivers."',".$_REQUEST[$cnt.'_qtys']*$uom.",'".$_SESSION['usernames']."',now())";
}
if(mysqli_query($conn,$sqlst)==true){
$dvstates++;
}
}
}
$vsqlst="update rc_invoices set undv_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='Undelivery note' group by item_names,ref_box_qty) where invoice_number='".$invnumbers."'";
mysqli_query($conn,$vsqlst);  
}
if($dvstates>0){
echo "success***".$dvstates."***".$dcn."***".$ddates."***UNDV:".(19000+$mxn);}
else{
echo "errors***".$dvstates."***".$dcn."***".$ddates."***".(19000+$mxn);}
?>
