<?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;

date_default_timezone_set("Asia/Colombo");
$dates=date('Y-m-d H:i:s');

$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='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];
$itemcodes=$_REQUEST['itemscode'.$cnt];
$storelocation=$_REQUEST['storelocation'.$cnt];
//$frees=$_REQUEST['frees'.$cnt];
$sqlstm="select distinct item_ranges,sub_ranges,packets_per_box,packet_weight,location,com_tec,rd_uom 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];
$comtecs=$rstu[5];
$uom=$rstu[6];
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,box_price,box_mrp,box_qty,ref_box_qty,balance_box_qty,stock_box_qty,item_ranges,sub_ranges,packets_per_box,packet_weights,vehicle_names,driver_names,com_tecs,user_names,free_pcs,pcs_qty,user_dates) VALUES('".$ddates."','DV:".($mxn+19000)."','".$invdates."','".$invnumbers."','Invoice',".$mxn.",'Delivery note',".$zoneorders.",'".$zones."','".$regions."','".$agenttypes."','".$dagents."','".$terrys."','".$locationtsm."','".$storelocation."','".$itemcodes."','".$items."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).
",".str_replace(',','',$_REQUEST[$cnt.'_mrp']).
",".$_REQUEST[$cnt.'_qtys'].",".$_REQUEST[$cnt.'_refqtys'].",".$_REQUEST[$cnt.'_qbalances'].",".(($_REQUEST[$cnt.'_qtys'])*-1).",'".$rangesm."','".$subrangesm."',".$packetsperboxsm.",".$packetweightsm.",'".
$dvehicles."','".$ddrivers."','".$comtecs."','".$_SESSION['usernames']."','0',".(($_REQUEST[$cnt.'_qtys'])*$uom).",'".$dates."')";

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.itemcodes=st_notes.itemcodes 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);}

?>
