<?php
date_default_timezone_set("Asia/Colombo");
require "showshp.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}


if(isset($_REQUEST['actp'])){
$states=0;
$rws=$_REQUEST['rws'];
$dates=$_REQUEST['rddates'];
$agents=$_REQUEST['rdagents'];
$userdates=date("Y-m-d");


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
$sqlst="select max(invoice_number) from open_stks ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

if(mysqli_num_rows($qry)==0){
$invoicenumbers=date('Y')."0001";}
else{
if(intval($rst[0],10)==0){
$invoicenumbers=date('y')."0001";}
else{
$invoicenumbers=intval($rst[0],10)+1;}
}

$sqlst="select distinct stores from warehouse where stores='".$agents."'";
#echo $sqlst;

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

$agtname=$rst[0];


for($cnt=0;$cnt<$rws;$cnt++){
$items=$_REQUEST['items'.$cnt];
$prices=$_REQUEST['prices'.$cnt];
$qtys=$_REQUEST['qtys'.$cnt];
$vll=$_REQUEST['prices'.$cnt]*$_REQUEST['qtys'.$cnt];


$sqlst="select distinct item_ranges,sub_ranges,rd_uom,packet_weight,whole_sale_price from itemsmrd where item_name='".$items."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

$ranges=$rst[0];
$subranges=$rst[1];
$packetsperbox=$rst[2];
$packetweights=$rst[3];
$bxprice=$rst[4];


$sqlst="insert into open_stks(dates,invoice_number,chanell,item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,range_types,box_price,box_qty,user_names,user_dates) values ('".$dates."','".$invoicenumbers."','".$agtname."','".$items."','".$ranges."','".$subranges."',".$packetsperbox.",".$packetweights.
",'".$rangetypes."',".$bxprice.",".$qtys.",'".$_SESSION['usernames']."','".$userdates."')";

}

if(($qry=mysqli_query($conn,$sqlst))==false){
echo "errors";}
else{
echo "oks<td>".$invoicenumbers."<td>".$dates."<td>";}
}


	


