<?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'];
$dvehicles=$_REQUEST['dvvehicles'];
$ddrivers=$_REQUEST['dvdrivers'];
$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 city_notes where note_types='Delivery 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];
$frees=$_REQUEST['frees'.$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];
$subrangesm=$rstu[1];
$packetsperboxsm=$rstu[2];
$packetweightsm=$rstu[3];
$locationtsm=$rstu[4];
if($_REQUEST[$cnt.'_qtys']>0)
{

$sqlst="INSERT INTO city_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,item_names,box_price,box_qty,ref_box_qty,balance_box_qty,stock_box_qty,item_ranges,sub_ranges,packets_per_box,packet_weights,vehicle_names,driver_names,user_names,free_pcs,user_dates) VALUES('".$ddates."','DV:".($mxn+19000)."','".$invdates."','".$invnumbers."','Invoice',".$mxn.",'Delivery note',".$zoneorders.",'".$zones."','".$regions."','".$agenttypes."','".$dagents."','".$terrys."','City Office','".$items."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).",".$_REQUEST[$cnt.'_qtys']*str_replace(',','',$_REQUEST[$cnt.'_pcsrates'])/str_replace(',','',$_REQUEST[$cnt.'_rates']).",".$_REQUEST[$cnt.'_refqtys']*str_replace(',','',$_REQUEST[$cnt.'_pcsrates'])/str_replace(',','',$_REQUEST[$cnt.'_rates']).",".$_REQUEST[$cnt.'_qbalances']*str_replace(',','',$_REQUEST[$cnt.'_pcsrates'])/str_replace(',','',$_REQUEST[$cnt.'_rates']).",".$_REQUEST[$cnt.'_qtys']*str_replace(',','',$_REQUEST[$cnt.'_pcsrates']*-1)/str_replace(',','',$_REQUEST[$cnt.'_rates']).",'".$rangesm."','".$subrangesm."',".$packetsperboxsm.",".$packetweightsm.",'".
$dvehicles."','".$ddrivers."','".$_SESSION['usernames']."','".$frees."',now())";
}
if(mysqli_query($conn,$sqlst)==true){
$dvstates++;}
}
}
$vsqlst="update rc_invoices set dv_box_qty=(select sum(stock_box_qty)*-1 from city_notes where ref_numbers='".$invnumbers."' and rc_invoices.item_names=city_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."'";

}
mysqli_query($conn,$vsqlst);
$qts="update rc_invoices set dlv_dates='".$ddates."',inv_status='1',paysts='1' where invoice_number='".$invnumbers."'";
mysqli_query($conn,$qts);

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

?>
