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

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

$usr=$_SESSION['uname'];



 ?>

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

  <title>GLAKB  | Carton Purchases</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">

<style>




        .input-box {
            width: 100%;
            padding: 1px;
            margin-top: 1px;
            margin-bottom: 1px;
            border: 1px solid gainsboro;
            border-radius: 4px;
            font-size: 16px;

        }
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 98%;
}

#customers td, #customers th {
  border: 1px solid #4444;
  padding: 6px;
}

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


#customers th {
  padding-top: 8px;
  padding-bottom: 8px;
  text-align: center;
background-color:dodgerblue;
    color: white;
}
</style>   
<link rel="stylesheet" href="css/style.css">
<?


if(isset($_POST['add']))
{
$prod = $_POST['item3'];
$q = $_POST['qty'];
$r = $_POST['rate'];
$sku = $_POST['sku'];
$size = $_POST['size'];
$des = $_POST['desc'];

if(empty($_POST['item3']) || empty($_POST['qty']) ){
echo "<script language='javascript'>";
echo "alert('All Fields are Necessary.')";
echo "</script>";
goto donot1;
}    


$tot=$r*$q;
mysqli_query($link, "insert into temp_pur values ('','$prod','Carton','$sku','$size','$q','$r','$tot','$usr','$des')");
donot1:
                                              
 }




if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_pur where Id='$lid'");
if($query){
//unlink($ppicpath);
//echo "<script type='text/javascript'> document.location = 'sales.php'; </script>";
}
}

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

date_default_timezone_set ("Asia/Karachi");
$the_time = date('d-m-Y (h:i:s a)', time());


$date = strtotime($_POST['strDate']);
$date = date("Y-m-d",$date);       
$gst = $_POST['gst'];
$supp = $_POST['item'];
$comp = $_POST['comp'];

$rems = $_POST['rem'];

if(empty($_POST['item'])){
echo "<script language='javascript'>";
echo "alert('Kindly Select Supplier Name.')";
echo "</script>";
goto donot;
}
else
{

$result = mysqli_query($link, "select MAX(Inv_No) as inv from r_details");
while($row = mysqli_fetch_object($result))
{
$inv=$row->inv+1;
}        

$prawInv = str_pad($inv, 4, "0", STR_PAD_LEFT);
$prawInv = "RC".$prawInv;



$strQuery1 = "select count(*) as cnt1 from temp_pur where addby='$usr' and Category='Carton'";
$result121 = mysqli_query($link, $strQuery1);
$rstRow = mysqli_fetch_array($result121);
$counter= $rstRow["cnt1"];
if($counter==0)
{
echo "<script language='javascript'>";
echo "alert('First Enter Carton Details.')";
echo "</script>";
goto donot;
}



$gtot=0;
$result = mysqli_query($link, "select * from temp_pur where addby='$usr' and Category='Carton' order by Id");
while($row = mysqli_fetch_object($result))
{
mysqli_query($link, "insert into raw_purchases (Inv_Date, Inv_No, Inv_Det, Category, Name, Size, SKU, Inv_Type, Qty, Rate, Total, addby, Supplier, addon, Status, Inv_N, Company) values ('$date','$inv','$prawInv','Carton','$row->Name','$row->Size','$row->SKU','Dir Pur','$row->Qty','$row->Rate','$row->Total','$usr','$supp','$the_time','1', 'POR (CTN)','$comp')");

$gtot=$row->Total+$gtot;
}
$gst_amt=($gtot/100)*$gst;
$ntot=$gtot+$gst_amt;
 
 mysqli_query($link, "insert into r_details (Category, Inv_Date, Inv_No, Inv_Det, Supplier, Inv_Type, Total, Gst, G_Amt, G_Total, addby, addon, Remarks, Status, Inv_N, Company) values ('Carton','$date','$inv','$prawInv','$supp','Dir Pur','$gtot','$gst','$gst_amt','$ntot','$usr','$the_time','$rems','1','POR (CTN)','$comp')");

$rst12 = mysqli_query($link,"select A_Id from acc_heads where Name = '$supp' ");
while($row12 = mysqli_fetch_array($rst12))
{
    $s_id=$row12['A_Id'];
}

$dets='Carton Purchasing Inv# '.$prawInv;
mysqli_query($link, "insert into ledgers (A_Id, D_Date, M_Head, Code, Details, Credit, usr, L_Type, Inv_No2, Inv_Det2, V_Time) values ('$s_id','$date','Supplier','$supp','$dets','$ntot','$usr','Purchase-Raw','$inv','$prawInv','$the_time')");

$rst13 = mysqli_query($link,"select A_Id from acc_heads where Name = 'Carton-Purchases' ");
while($row13 = mysqli_fetch_array($rst13))
{
    $cr_id=$row13['A_Id'];
}


$dets='Carton Purchases via Inv# '.$prawInv." from ".$supp;
mysqli_query($link, "insert into ledgers (A_Id, D_Date, M_Head, Code, Details, Debit, usr, L_Type, Inv_No2, Inv_Det2, V_Time) values ('$cr_id','$date','Raw Material Purchases','Carton-Purchases','$dets','$gtot','$usr','Purchase-Raw','$inv','$prawInv','$the_time')");


if($gst!=0)
{

$rst14 = mysqli_query($link,"select A_Id from acc_heads where Name = 'Raw Sales Tax' ");
while($row14 = mysqli_fetch_array($rst14))
{
    $st_id=$row14['A_Id'];
}

$dets='GST (Cartons) Inv# '.$prawInv." to ".$supp;
mysqli_query($link, "insert into ledgers (A_Id, D_Date, M_Head, Code, Details, Debit, usr, L_Type, Inv_No2, Inv_Det2, V_Time) values ('$st_id','$date','General Sales Tax','Raw Sales Tax','$dets','$gst_amt','$usr','Purchase-Raw','$inv','$prawInv','$the_time')");

}





$result = mysqli_query($link, "delete from temp_pur where addby = '$usr' and Category='Carton' ");
 

saveent:
echo "<script language='javascript'>";
 echo "alert('Invoice Saved Successfully.')";
                                                                echo "</script>";
}
 donot:
   }
    
        
        





                       
          

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

<script language="javascript">

window.onload= function show()
                        {
                                if(document.stock.qty.value=="" || document.stock.qty.value==0 )
                                        {

                                                document.stock.qty.focus()
                                        }
                                        }
 function formconfirm() {
return confirm("You Clicked the Button. Continue?<? echo "$coursename";?>");
}
                
function openAndPrintPDF() {
            // Open the PDF in a new tab
            var pdfWindow = window.open('open.php', '_self');

          
          
        }

        function show_cbo1()
                {
                        var val = document.getElementById('item3').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "inv1.php?Code="+val;
                }
                

                
               function show_cbo2()
                {
                        var val2= document.getElementById('item33').value;
                         var val = document.getElementById('item3').value;
                      
                        location.href = "inv1.php?Code="+val+"&Code2="+val2;
                }
       

function myFunction() 
{
$x = document.getElementById("rate").value;
$y = document.getElementById("qty").value;
$c= 0;
$c=$x*$y;

document.getElementById("tot").value=$c;


}
function processkey(e)



       

         {
                        var kc = (window.Event?e.which:e.keyCode)
                        if (kc >=48 && kc<= 57 || kc ==8 || kc == 46)
                                return true
                        else
                                return false

                }
                
     
</script>

<form name="stock" action="" method="POST" onSubmit='return formconfirm()'>

<style type="text/css">
#left                 {width:50%;height:auto;float:left;padding-top:0%;padding-left:0.5%}
#right                {width:50%;height:auto;float:right;padding-top:0%;padding-right:0.5%}

</style>
<div id="left">




 <table align="center" id="customers">
       
<tr>
    <th colspan="4"><font size="5">CARTON PURCHASING FORM</th>

  </tr>      
                       
 

                       
<TR>
<td><b><font size=3 color="black">Select Product Name:</b></td>
                        <td colspan="3">
                                <select class="form-control" name="item3" id="item3" style=font-size="10" onchange="show_cbo1()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Code']) && !isset($_POST['reset']))
                                        {
                                                $Code = $_GET['Code'];
                                                      }
                                else                        
                                       $Code = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Distinct Name from raw_details where Type='Carton' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>

                             

                         <?
$prod=$Code;
$result32 = mysqli_query($link, "select Rate from raw_details where Name='$prod' and Type='Carton' ");
$row32 = mysqli_fetch_object($result32);                              




$result = mysqli_query($link, "select Size, Unit_Liter from products where Full_Name='$prod'");
$row = mysqli_fetch_object($result);                              
?> 



<tr>
<td><b><font size=3 color="black">SKU:</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='sku' size='5' style='color: black; font-family: Times-Roman; font-size:2' id='sku' value='$row->Unit_Liter' onkeypress='return processkey(event)' onfocus='this.select()' readonly >";

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

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='size' size='5' style='color: black; font-family: Times-Roman; font-size:2' id='size' value='$row->Size' onkeypress='return processkey(event)' onfocus='this.select()' readonly >";

?>          

<tr>

<td><b><font size=3 color="black">Rate / Carton:</b></td>
<td>
<?
echo "<b><input type='text' name='rate' class='input-box' size='5' style='color: black; font-family: Times-Roman; font-size:2' id='rate' value='$row32->Rate' onkeypress='return processkey(event)' >";
?> 
<td colspan="2">           
<tr>
<td width="200"><b><font size=3 color="black">Quantity:</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='qty' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='qty' value='0' onkeyup='myFunction()' onfocus='this.select()' required >";

?>

<td colspan="2">  
  <tr>
<td width="200"><b><font size=3 color="black">Total:</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='tot' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='tot' value='0' required readonly >";

?>  
<td colspan="2">  
  <tr>
<td width="200"><b><font size=3 color="black">Description (If any):</b></td>

    
<?

echo "<td align=right><b><input type='text' class='input-box' name='desc' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='desc' value='' onfocus='this.select()' >";

?>

<td colspan="2">      
<tr>
<td colspan="4" align="center">


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

</table>

<table align="center" id="customers">
<br> 
<tr>
    <th colspan="4"><font size="5">INVOICING DETAILS</th>

  </tr> 


             
<TR>
<td><b><font size=3 color="black">Select Supplier:</b></td>
                        <td>
                                <select class="form-control" name="item" id="item" style=font-size="16" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Code3']) && !isset($_POST['reset']))
                                        {
                                                $Code3 = $_GET['Code3'];
                                                      }
                                else                        
                                       $Code3 = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Name from acc_heads where M_Head='Supplier' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code3 == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>

<tr>

<td><b><font size=3 color="black">Select Target Company:</b></td>
                        <td>
                                <select class="form-control" name="comp" id="comp" style=font-size="16" >
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Code4']) && !isset($_POST['reset']))
                                        {
                                                $Code4 = $_GET['Code4'];
                                                      }
                                else                        
                                       $Code4 = "";
                                        
                                        //include('config.php');
    $result = mysqli_query($link, "select Name from acc_heads where M_Head='Company' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code4 == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>                            </select>


<tr>


<?
$result13 = mysqli_query($link, "select MAX(Inv_No) as inv from r_details " );
while($row13  = mysqli_fetch_object($result13))
{
$inv=$row13->inv+1;
}

 $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);         
                                           }

echo " Inv-No. <input type='text' id='strinv' name='strinv' class='widerHeight' size='5' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:20px;' value='$inv' readonly   />";


$result12 = mysqli_query($link, "select SUM(Total) as bal from temp_pur where addby='$usr' and Category='Carton' ");
$row = mysqli_fetch_object($result12);

 ?>
 <tr>
<td width="200"><b><font size=3 color="black">Total Amount:</b></td>
<td>
<?
echo "<b><input type='text' name='amtp' class='input-box' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='amtp' value='$row->bal' onkeypress='return processkey(event)' readonly >";
?>

<tr>
<td width="200"><b><font size=3 color="black">GST (In %) :</b></td>
<td>
<?
echo "<b><input type='text' name='gst' class='input-box' size='15' style='color: black; font-family: Times-Roman; font-size:2' id='gst' value='0' onkeypress='return processkey(event)'>";
?>

<tr>
<td width="220"><b><font size=3 color="black">Reference Invoice / Remarks:</b></td>
<td>
<?
echo "<b><input type='text' name='rem' class='input-box' size='15' style='color: black; font-family: Times-Roman; font-size:2' id='rem' value=''>";
?>

<tr>            
<td 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 align="center"> 
<input type="submit" name="save1" onclick="openAndPrintPDF()" value="Back to Main Form" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">



 </table>
</div>



 


<div id="right">  

<div class="table-container">


<table align="center" id="customers">



<tr>
    <th colspan="9"><font size="5">CARTON DETAILS</th>

  </tr>
<?

 echo "<center>";
      
                $result = mysqli_query($link, "select * from temp_pur where addby='$usr' and Category='Carton' order by Id");
                                  
 
           
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Product Name</b>";
        echo "        </td>";
           echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>SKU</b>";
        echo "        </td>";
        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Size</b>";
        echo "        </td>";
         echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Description</b>";
        echo "        </td>";
         echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Rate</b>";
        echo "        </td>";
       
echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Quantity</b>";
        echo "        </td>";
         
 
                echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Total</b>";
        echo "        </td>";

        echo "        <td align=center>";
        echo "                <b><font size=3 color=darkblue>Deletion</b>";
        echo "        </td>";

 echo"  </td>";

        echo "</tr>";

       






$gtot=0;
$count=0;
$one=1;
while($row = mysqli_fetch_object($result))
{
$count=$one+$count;
$gtot=$row->Total+$gtot;
echo "<td align=left><font size=3 color=black>$row->Name</td>";
echo "<td align=left><font size=3 color=black>$row->SKU</td>";

echo "<td align=left><font size=3 color=black>$row->Size</td>";
echo "<td align=left><font size=3 color=black>$row->Desc</td>";

echo "<td align=right><font size=3 color=black>$row->Qty</td>";
echo "<td align=right><font size=3 color=black>$row->Rate</td>";
echo "<td align=right><font size=3 color=black>$row->Total</td>";

?>
<td align=center>
       
 <a href="inv1.php?action=delete&&tid=<?php echo $row->Id; ?>" style="color:red;" title="Delete Record" onclick="return confirm('Do you really want to delete this record?');"><i class="fa fa-trash" aria-hidden="true"></i> </a>

<?
echo "</tr>";
}
echo "        <td align=center colspan=6>";
echo "<b><font size=3 color=darkblue>Grand Totals</b>";
echo "<td align=right><font size=4 color=black><b>$gtot</b></td>";
echo "        <td align=center>";


?>

</table>
</div>

       



<?

           




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