<?php

session_start();

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

$actp=$_REQUEST['actp'];

if($actp=='dvnotes'){
	
$conn=mysql_connect("localhost","primarysales","S5L7lF01EQZvQrGK");
mysql_select_db("primarysales_diana",$conn);

	
$ddates=$_REQUEST['ddates'];
$invdates=$_REQUEST['invdates'];
$invnumbers=$_REQUEST['invnumbers'];
$notedates=$_REQUEST['snotedates'];
$notenumbers=$_REQUEST['snotenumbers'];
$dagents=$_REQUEST['invagents'];
$dstores=$_REQUEST['dvstores'];
$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=mysql_query($sqlst,$conn);
$rst=mysql_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='Unload note'";
$qry=mysql_query($sqlst,$conn);
$rst=mysql_fetch_row($qry);
$mxn=$rst[0]+1;


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

$cnt=0;
while($trst=mysql_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`, `ret_dates`, `ret_numbers`, `ret_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`, `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."','UNLD:".($mxn+16000)."','".$invdates."','".$invnumbers."','Invoice','".$notedates."','".$notenumbers."','Delivery note',"
.$mxn.",'Unload note',".$zoneorders.",'".$zones."','".$regions."','".$agenttypes."','".$dagents."','".$terrys."','".$dstores."','".
$_REQUEST[$cnt.'_items']."',".str_replace(',','',$_REQUEST[$cnt.'_rates']).",".$_REQUEST[$cnt.'_qtys'].",".$_REQUEST[$cnt.'_refqtys'].
",".$_REQUEST[$cnt.'_qbalances'].",".$_REQUEST[$cnt.'_qtys'].",'".$ranges."','".$subranges."',".$packetsperbox.",".$packetweights.",'".$dvehicles."','".$ddrivers.
"','".$_SESSION['usernames']."',now())";

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

}

#echo $sqlst;

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


$vsqlst="update rc_invoices set unld_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='Unload note' 
group by item_names,ref_box_qty) where invoice_number='".$invnumbers."'";

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='".($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."***UNLD:".(16000+$mxn);}
else{
echo "errors***".$dvstates."***".$dcn."***".$ddates."***".(16000+$mxn);}
	
}
}



?>
