<?
session_start();
include('includes/config.php');
include('defs.php');
include ('phpqrcode/qrlib.php'); 

if(strlen($_SESSION['aid'])==0)
  { header('location:index.php');
}

$usr=$_SESSION['uname'];

$inv_no=$_GET['inv'];


 ?>


 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>GLAKB  | Invoice Booking Form</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!--Function Email Availabilty---->

</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
<?php include_once("includes/navbar.php");?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
 <?php include_once("includes/sidebar.php");?>

 
  <div class="content-wrapper">

<?
$result12 = mysqli_query($link, "select Role from user_details where Name='$usr' ");
$row12=  mysqli_fetch_object($result12);
$role = $row12->Role;
if($role=='Operator')
{
$result = mysqli_query($link, "select Role from user_priv where Name='$usr' and Form='Order' ");
$row=  mysqli_fetch_object($result);
$role = $row->Role;
if($role=='0')
{
{
echo"<center>";
echo"<br><br><br><br><br>";
echo"<img src='users.png' width='160' height='160'><br>";
echo "<font size=6 color=red><b>ACCESS DENIED!</b><br>";
echo "<font size=4 color=darkblue><b>You Do Not Have Permission to View This Page.</b><br>";
goto a;
}
}
}


echo "                <form name='m_head' method=POST onSubmit='return validateForm()'>";


              ?>
              <html>
<head>
<style>
    
#customers1{
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 60%;
}

#customers1 td, #customers1 th {
  border: 1px solid #4444;
  padding: 8px;
}

#customers1 tr:nth-child(even){background-color:gainsboro ;}


#customers1 th {
  padding-top: 8px;
  padding-bottom: 8px;
  text-align: center;
  background-color:dodgerblue;
  color: white;

}
 
#results {
    max-height: 600px;   /* adjust as needed */
    overflow-y: auto;    /* vertical scrollbar */
    border: 1px solid #ccc;
}

 #recordModal {
            display: none;
            position: fixed;
            top: 0; left: 0;
            width: 100%; height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 1000;
        }
        #modalContent {
            background: #fff;
            margin: 5% auto;
            padding: 20px;
            width: 1000px;
            border-radius: 8px;
        }
        #searchBox {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            font-size: 25px;
        }
        table {
            border-collapse: collapse;
            width: 100%;
        }
        
        tr:hover {
            background: #f5f5f5;
            cursor: pointer;
        }
   
        #suggestions {
            border: 2px solid #ccc;
            display: none;
            position: absolute;
            background-color: white;
            width: 300px;
            max-height: 250px;
            overflow-y: auto;
            z-index: 1000;
        }
        #suggestions div {
            padding: 3px;
            cursor: pointer;
        }
        #suggestions div:hover,
        #suggestions .selected {
            background-color: #f0f0f0;
        }

    </style>
<link rel="stylesheet" href="css/style.css">

<script language="javascript" type="text/javascript">

   window.onload= function show()
                        {
                                if(document.m_head.itemBox.value =="")
                                        {

                                                document.m_head.itemBox.focus()
                                        }
                                        }
                                        
function formconfirm() {
return confirm("Do You want to Continue?<? echo "$coursename";?>");
}

 
function validateForm()
{

if((document.m_head.name.value) == '')
{
alert("Please Search Valid Customer!");
document.m_head.itemBox.focus();
return false;
}
if((document.m_head.cid.value) == '')
{
alert("Please Search Valid Customer!");
document.m_head.itemBox.focus();
return false;
}


return true;
}







             







  </script>





 <table align="center" id="customers1">
       
<tr>
    <th colspan="6"><font size="5">INVOICE BOOKING FORM</th>

  </tr>               
       
 <?


$result = mysqli_query($link, "SELECT A_Id, Inv_Date, addby, Warehouse, Salesman, Inv_No, Customer, City, Sum(Total) as tot, Sum(G_Total) as tot1, Sum(N_Total) as tot2, Sum(R_Dis) as tdis, Status  FROM porder where Inv_No='$inv_no'  Group by Inv_No");


while($row = mysqli_fetch_object($result))
{
$name = $row->Customer;
$cid = $row->A_Id;
$cty = $row->City;
$ware = $row->Warehouse;
$sman = $row->Salesman;
$tots = $row->tot2;

}


 ?>



<TR>




<td colspan="1"><b><font size=4 color="black">Order No.:</b></td>

<?
echo "<td align=left><b><input type='text' name='ord' size='30' style='color: black; font-family: Times-Roman; font-size:2' id='ord' value='$inv_no' readonly >";


?>
<tr>

<td colspan="1"><b><font size=4 color="black">Customer Name:</b></td>

<?
echo "<td align=left><b><input type='text' name='name' size='45' style='color: black; font-family: Times-Roman; font-size:2' id='name' value='$name' readonly >";


?>
<tr>




<td colspan="1"><b><font size=4 color="black">City:</b></td>

<?
echo "<td align=left><b><input type='text' name='city' size='45' style='color: black; font-family: Times-Roman; font-size:2' id='city' value='$cty' readonly>";

?>


<TR>
<td colspan="1"><b><font size=4 color="black">Target Warehouse:</b></td>

<?
echo "<td align=left><b><input type='text' name='ware' size='30' style='color: black; font-family: Times-Roman; font-size:2' id='ware' value=$ware readonly>";


?>


<tr>
<td><b><font size=4 color=black>Salesman:</b></td>
<?

echo "<td align=left><b><input type='text' class='input-box' name='sman' size='30' style='color: black; font-family: Times-Roman; font-size:2' id='sman' value='$sman' readonly>";




?>    

<tr>

<td><b><font size=4 color=black>Invoice Amount:</b></td>


<?

echo "<td align=left><b><input type='text' class='input-box' name='amt' size='30' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='amt'  value='$tots' readonly >";

echo "<tr>";


$strvoucher= Date("Y-m-d");
if(isset($_POST['strDate']) && !isset($_POST['reset']) )
{
$strvoucher=$_POST['strDate'];
AddDate("<b><font size=3 color=black>Invoice Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}
else
{
AddDate("<b><font size=3 color=black>Invoice Date: </b> &nbsp;", "strDate", $strvoucher, 10, 10, 0);         
}


?>

<tr>

<td><b><font size=3 color=black>Select Payment Mode:</b></td>

<td>
<select class="form-control" name=pmode>
<option value='Credit'>Credit</option>
<option value='Cash'>Cash</option>
</select>
<tr>



 <td><b><font size=3 color=black>Remarks:</b></td>
<?

echo "<td align=left colspan=3><b><input type='text' class='input-box' name='rems' size='45' style='color: black; font-family: Times-Roman; font-size:2' id='rems' value='' >";

?> 


<tr>
           <td colspan="6" align="center">

<input type="submit" name="save" value="Save Invoice" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">
</td>

</form>
</table>

<form name="print" method="post">

 <table align="center" id="customers1">
       
<tr>
    <th colspan="6"><font size="4">PRINT LAST ORDER</th>

  </tr>      



<?

echo "<td align=left><b><input type='text' class='input-box' name='dued' size='35' style='color: black; font-family: Times-Roman;  background-color: lightblue; font-size:44' id='dued' value='$vend' readonly >";

echo "<b><input type='text' class='input-box' name='dued' size='35' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='dued' value='Purchase Order# $inv_no Dated: $idte' readonly >";

?>


<input type="submit" name="print" value="View and Print" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">




</form>
</table>

 <?


if(isset($_POST['print']))
{

$link1 = "<script>window.open('rep_order.php?usr=$usr','_blank')</script>";
echo $link1;
}




if(isset($_POST['save']))
{
/*
$result421 = mysqli_query($link, "select * from cust_details where addby='$usr'");
while($row  = mysqli_fetch_object($result421))
{
$city=$row->City;
$cust=$row->Customer;
$ware=$row->Warehouse;
$itype=$row->Ctype;
$dued=$row->Dued;
$adr=$row->Adress;
$sman=$row->Salesman;
$provi=$row->Province;
}
*/
date_default_timezone_set ("Asia/Karachi");
$date = date('d-m-Y (h:i:s a)', time());
$adates= Date("Y-m-d");

$invdate = strtotime($_POST['strDate']);
$invdate = date("Y-m-d",$invdate);


//$adis = $_POST['adis'];
$rem = $_POST['rems'];
$pmode = $_POST['pmode'];
$cust = $_POST['name'];
$cty = $_POST['city'];
$sman = $_POST['sman'];

/*
$sql2 = "SELECT * FROM temp_sinv where addby='$usr' ";
$q = $link->query($sql2);
if ($q->num_rows == 0) 
{
echo "<script language='javascript'>";
echo "alert('The Invoice is Empty!')";
echo "</script>";
goto donot;
}
*/
$result = mysqli_query($link, "select max(Inv_No) as v_no2 from s_details ");
$row =  mysqli_fetch_object($result);
$v_no2 = $row->v_no2 + 1;

$salesInv = str_pad($v_no2, 6, "0", STR_PAD_LEFT);
$salesInv = "SIB".$salesInv;
$ttot = 0;
$gtot = 0;
$ntot = 0;
$dis = 0;
$stot = 0;
$result = mysqli_query($link, "select * from porder where Inv_No='$inv_no' order by Id");
while($row = mysqli_fetch_array($result))
{

$itypee = $row['Inv_Type'];
$ttot = $row['Total']+$ttot;
$ntot = $row['N_Total']+$ntot;
$gtot = $row['G_Total']+$gtot;
$stot = $row['S_Amt']+$stot;
$dis = $row['R_Dis']+$dis;

$item=$row['Name'];
$cart=$row['Carton'];
$rte=$row['Rate'];
$dismt=$row['R_Dis'];
$provi = $row['Province'];
$cust1 = $row['Customer'];

//$pack = mysqli_real_escape_string($link, $pack);
//$cname = mysqli_real_escape_string($link, $cname);
mysqli_query($link, "insert into cust_sales (Customer, Inv_No, Inv_Det, Inv_Date, Inv_Type, Name, Unit_Liter, Carton_Qty, Qty, Tunit, Carton, Rate, N_Rate, R_Dis, Stax, S_Amt, Total, G_Total, N_Total, addby, addon, Warehouse, Inv_N, City, Province, Salesman) values ('$row[Customer]','$v_no2','$salesInv','$invdate','$row[Inv_Type]','$row[Name]','$row[Unit_Liter]','$row[Carton_Qty]','$row[Qty]','$row[Tunit]','$row[Carton]','$row[Rate]','$row[N_Rate]','$row[R_Dis]','$row[Stax]','$row[S_Amt]','$row[Total]','$row[G_Total]','$row[N_Total]','$usr','$date','$row[Warehouse]','SIB','$row[City]','$row[Province]','$row[Salesman]')");


}
$vbal = 0;
$result420 = mysqli_query($link, "select Sum(Debit-Credit) as bal from ledgers where M_Head='Customer' and Code='$cust1' group by Code  ");
$row420 =  mysqli_fetch_object($result420);
$vbal = $row420->bal;


//$atax1=($ntot/100)*$atax;
//$atax2=($ntot/100)*$aast;
//$ntotal=$ntot+$atax1;
//$ntot=$ntot-$adis;

mysqli_query($link, "insert into s_details (Customer, Inv_No, Inv_Det, Inv_Date, Inv_Type, Total, N_Total, G_Total, addby, addon, S_Amt, Warehouse, Inv_N, Salesman, Pmode, City, Remarks, Dis, Status, Province, P_Bal) values ('$cust','$v_no2','$salesInv','$invdate','$itypee','$ttot','$ntot','$gtot','$usr','$date','$stot','$ware', 'SIB','$sman','$pmode','$cty','$rem','$dis','Pending','$provi','$vbal')");


//$rems=$item." (".$cart." Cartons"." @ ".$rte."/Liter)";
//mysqli_query($link, "insert into ledgers (M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No2, Salesman) values ('Customer','$cust','$rems','$row[Total]','$invdate','$usr','Finished Sales','$date','$v_no2','$sman')");

$ftot = $ttot + $stot;

$dets="Purchasing via Sales Invoice.";
mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No2, Inv_Det2, Salesman) values ('$cid', 'Customer','$cust','$dets','$ftot','$invdate','$usr','Finished Sales','$date','$v_no2','$salesInv','$sman')");

if($dis>0)
{
mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Credit, D_Date, usr, L_Type, V_Time, Inv_No2, Inv_Det2, Salesman) values ('$cid','Customer','$cust','Discount Awarded.','$dis','$invdate','$usr','Finished Sales','$date','$v_no2','$salesInv','$sman')");

}


$rst4 = mysqli_query($link, "select A_Id from acc_heads where Name='General Sales'");
while($row4 = mysqli_fetch_array($rst4))
{
$o_id = $row4['A_Id'];
}

$dets="Sales via SIB".$salesInv." to ".$cust;

mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Credit, D_Date, usr, L_Type, V_Time, Inv_No2, Inv_Det2, Salesman) values ('$o_id','Sales','General Sales','$dets','$ntot','$invdate','$usr','Finished Sales','$date','$v_no2','$salesInv','$sman')");




if($pmode=='Cash')
{
$dets="Cash Received - Sales Inv# ".$salesInv;
 mysqli_query($link, "insert into ledgers (M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No2, Inv_Det2, Salesman) values ('Assets','Cash','$dets','$ntot','$invdate','$usr','Finished Sales','$date','$v_no2','$salesInv','$sman')");


$dets="Cash Deposited for Inv# ".$salesInv;

mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Credit, D_Date, usr, L_Type, V_Time, Inv_No2, Inv_Det2, Salesman) values ('$cid','Customer','$cust','$dets','$ntot','$invdate','$usr','Finished Sales','$date','$v_no2','$salesInv','$sman')");


}



$text = 'http://www.glakb.com/admin/check.php?user_id='.$v_no2;
$path = 'qrcodes/'; 
$file = $path.$v_no2.".jpg"; 
$ecc = 'L'; 
$pixel_Size = 10; 
$frame_Size = 10; 
QRcode::png($text, $file, $ecc, $pixel_Size, $frame_size); 
$qrcode=$v_no2.".jpg";
$image = imagecreatefrompng('./qrcodes/'.$qrcode);
$Newqr=$v_no2.".jpg";
imagejpeg($image, './qrcodes/'.$Newqr, 50); // 0 = worst / smaller file, 100 = better / bigger file 
imagedestroy($image);

mysqli_query($link, "update porder set Status='D', S_Date='$invdate' where Inv_No='$inv_no' ");



echo "<script language='javascript'>";
echo "alert('Sales Invoice Saved Successfully!')";
echo "</script>";
$link = "<script>window.open('view_orders.php','_self')</script>";
echo $link;


       
         }


a:


            include "footer.php";
?>

</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- bs-custom-file-input -->
<script src="../plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
$(function () {
  bsCustomFileInput.init();
});
</script>
</body>
</html>
<?php  ?>