<?
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  | Company Invoice</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='Inv-Pur' ");
$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>
    .table-container {
    max-height: 168px;        /* Set max height for vertical scrolling */
    overflow-y: auto;
    overflow-x: auto;
    width: 100%;
    border: 1px solid #ccc;   /* Optional: visible border around container */
  }
#customers {
  font-family: Times-Roman;
  border-collapse: collapse;
  width: 98%;
}

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

}

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


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

}


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

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

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


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

}
 

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

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

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

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

 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

     
           }
           function moveToAmount() {
    let amountInput = document.getElementById("dcn");
    if (amountInput) {
        amountInput.focus();
    }
}

function moveToAmount1() {
    let amountInput = document.getElementById("tqty");
    if (amountInput) {
        amountInput.focus();
    }
}


 function openAndPrintPDF() {
      var pdfWindow = window.open('rep_pur_inv.php?usr=<?php echo $usr ?>', '_blank');
    }
function validateForm1()
{
if((document.m_head1.ctype.value) == '')
{
alert("Please Select Company Name First!");
document.m_head1.ctype.focus();
return false;
}
if((document.m_head1.ware.value) == '')
{
alert("Please Select Warehouse!");
document.m_head1.ware.focus();
return false;
}

if((document.m_head1.dcn.value) == '0' )
{
alert("Please Enter DC-Number!");
document.m_head1.dcn.focus();
return false;
}

return true;
}


function validateForm()
{
if((document.m_head.item3.value) == '')
{
alert("Select Product Name First!");
document.m_head.item3.focus();
return false;
}

if((document.m_head.grade.value) == '')
{
alert("Please Select Product Grade First!");
document.m_head.grade.focus();
return false;
}



if((document.m_head.munit.value) == '')
{
alert("Please Select Measuring Unit First!");
document.m_head.munit.focus();
return false;
}



if((document.m_head.qty.value) == '' || (document.m_head.qty.value) == '0' )
{
alert("Please Enter Quantity First!");
document.m_head.qty.focus();
return false;
}


if((document.m_head.dp.value) == '' || (document.m_head.dp.value) == '0' )
{
alert("Please Enter Price First!");
document.m_head.dp.focus();
return false;
}

return true;
}


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

function show_cbo11()
                {
                        var val = document.getElementById('ctype').value;
                           //var val3 = document.getElementById('strDate').value;
                      
                        location.href = "add_inv.php?M_Headc="+val;
                }
                function show_cbo12()
                {
                        var val = document.getElementById('ctype').value;
                         var val1 = document.getElementById('ware').value;
                      
                        location.href = "add_inv.php?M_Headc="+val+"&Ware="+val1;
                }
function show_cbo3()
{
var val1 = document.getElementById('item3').value;
var val2 = document.getElementById('grade').value;
var val3 = document.getElementById('munit').value;

location.href = "add_inv.php?Code="+val1+"&Code1="+val2+"&Code2="+val3;
}

function show_cbo2()
{
var val1 = document.getElementById('item3').value;
var val2 = document.getElementById('grade').value;

location.href = "add_inv.php?Code="+val1+"&Code1="+val2;
}


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

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


}



  </script>





 <table align="center" id="customers1">
       
<tr>
    <th colspan="6"><font size="5">COMPANY INVOICES FOR REFINE PRODUCTS</th>

  </tr>               
       
                       
<!DOCTYPE html>
<html>
<head>
    
    <style>
        #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>
</head>
<body>
<?
$result12 = mysqli_query($link, "select MAX(Inv_No) as inv from p_details" );
while($row12  = mysqli_fetch_object($result12))
{
$inv=$row12->inv+1;
}

echo "<td colspan=6 align=right>Invoice#  PIB-<input type='text' id='strinv' name='strinv' size='13' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:20px;' value='$inv' readonly   />";


?>
  
<tr>

<td><b><font size=3 color="black">Select Product Name:</b></td>
                        <td colspan="">
                                <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 Full_Name from products order by Full_Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Code == $row->Full_Name)
                                                        {
                                                                echo "<option value=\"$row->Full_Name\" selected=\"selected\">$row->Full_Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Full_Name\">$row->Full_Name</option>";
                                                        }
                                        }
                                ?>                            </select>


<?
$result1 = mysqli_query($link, "select Unit_Liter, Carton_Qty, DP from products where Full_Name='$Code'");
while($row  = mysqli_fetch_object($result1))
{
$numbs=$row->Unit_Liter;
$ucart=$row->Carton_Qty;
$price=$row->DP;

}
?>



<td colspan="1"><b><font size=3 color="black">Liters / Unit:</b></td>

<?
echo "<td align=left><b><input type='text' name='unit' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='unit' value='$numbs' onkeypress='return processkey(event)'>";


?>


<td colspan="1"><b><font size=3 color="black">Carton Units:</b></td>

<?
echo "<td align=left><b><input type='text' name='ucart' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='ucart' value='$ucart' onkeypress='return processkey(event)'>";


?>

<tr>


<td><b><font size=3 color=black>Price/Litre:</b></td>
<?
echo "<td colspan=5><b><input type='text' name='dp' class='widerHeight' size='15' style='color: black; font-family: Times-Roman; font-size:2' id='dp' value='$price' onkeypress='return processkey(event)' >";
?>
<tr>


<td><b><font size=3 color=black>Purchased Unit:</b></td>

<td colspan="1">
<select class="form-control" name=iqty id=iqty onchange="moveToAmount1()">
<option value='CTN'>CTN</option>
<option value='PACKS'>PACKS</option>
<option value='LITERS'>LITERS</option>

</select>
<td colspan="4">
<tr>
<td><b><font size=3 color=black>Purchased Quantity:</b></td>
<?

echo "<td align=left colspan=5><b><input type='text' class='input-box' name='tqty' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='tqty' value='0' onkeypress='return processkey(event)' onfocus='this.select()' autofocus  >";

?>    

<tr>


<td><b><font size=3 color=black>Discount From Price:</b></td>


<?

echo "<td align=left><b><input type='text' class='input-box' name='dis' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='dis' value='0' onfocus='this.select()' onkeypress='return processkey(event)' required >";

?>
<td colspan="4">
<tr>

<td><b><font size=3 color=black>FED (in %):</b></td>
<td ><input type="text" name="fed" size="15" onkeypress="return processkey(event)" value="5" /></td>

<td><b><font size=3 color=black>Sales Tax (in %):</b></td>
<td colspan="3"><input type="text" name="stax" size="15" onkeypress="return processkey(event)" value="18" /></td>

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

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

</td>
</form>
</table>
 

 <?



if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_cinv 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");
$date = date('d-m-Y (h:i:s a)', time());

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

//$cname = mysqli_real_escape_string($link, $_POST['searchBox']);

$cname=$_POST['item3'];

$ucart=$_POST['ucart'];
$unit=$_POST['unit'];

$qty=$_POST['tqty'];
$stax=$_POST['stax'];
$rate=$_POST['dp'];
$fed=$_POST['fed'];
$iqty=$_POST['iqty'];
$dis=$_POST['dis'];
$nrate=$rate-$dis;

if($iqty=='CTN')
{
$tcart=$qty;
$tunit=$ucart*$qty;
$tlit=$tunit*$unit;    
}  

if($iqty=='LITERS')
{
$tunit=$qty/$unit;
$tcart=$tunit/$ucart;
$tlit=$qty;    
}

if($iqty=='PACKS')
{
$tunit=$qty;
$tlit=$qty*$unit;    
$tcart=$qty/$ucart;
}

$newtot=$nrate*$tlit;
$acttot=$rate*$tlit;
$newdis=$acttot-$newtot;



$ftax=($newtot/100)*$fed;
$gtot=$newtot+$ftax;

$stamt=($gtot/100)*$stax;
$ntot=$gtot+$stamt;



$result1 = mysqli_query($link, "select Grade, Sufix, Item_Code from products where Full_Name='$Code'");
while($row  = mysqli_fetch_object($result1))
{
$grade=$row->Grade;
$suff=$row->Sufix;
$icode=$row->Item_Code;

}



$sql1 = "SELECT * FROM temp_cinv where Name='$cname' ";
$q = $link->query($sql1);
if ($q->num_rows == 1) 
{
echo "<script language='javascript'>";
echo "alert('Product Already added!')";
echo "</script>";
goto donothing;
}



mysqli_query($link, "insert into temp_cinv (Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, Rate, R_Dis, D_Amt, FED, F_Amt, Stax, S_Amt, Total, G_Total, N_Total, addby) values ('$cname','$grade','$ucart','$unit','$tlit','$tunit','$tcart','$rate','$dis','$newdis','$fed','$ftax','$stax','$stamt','$acttot','$gtot','$ntot','$usr')");
  
//mysqli_query($link, "insert into temp_pur (Name, Company, Code, C_Name, P_Group, Packing) values ('$name','$comp','$cid','$cname','$pgroup','$pack')");
  

  echo "<script language='javascript'>";
echo "alert('Added Successfully!')";
echo "</script>";

  donothing:         
         }
?>
<form method="POST">
  <div class="table-container">
<table align="center" id="customers">
   
                        <?



 $date2=Date("Y-n-j");
$result = mysqli_query($link, "select * from temp_cinv where addby='$usr' order by Id");
?>
<td style="background-color: dodgerblue; color: white;">S.No</td>
<td style="background-color: dodgerblue; color: white;">Product Name</td>
<td style="background-color: dodgerblue; color: white;">Grade</td>
<td style="background-color: dodgerblue; color: white;">Litres/Unit</td>
<td style="background-color: dodgerblue; color: white;">Quantity (Cartons)</td>
<td style="background-color: dodgerblue; color: white;">Quantity (Units)</td>
<td style="background-color: dodgerblue; color: white;">Quantity (Liters)</td>

<td style="background-color: dodgerblue; color: white;">Rate</td>
<td style="background-color: dodgerblue; color: white;">Dis (in Price)</td>
<td style="background-color: dodgerblue; color: white;">Tot-Amt</td>
<td style="background-color: dodgerblue; color: white;">Tot-Dis</td>

<td style="background-color: dodgerblue; color: white;">FED(%)</td>
<td style="background-color: dodgerblue; color: white;">FED(Amt)</td>
<td style="background-color: dodgerblue; color: white;">Gross-Amt</td>
<td style="background-color: dodgerblue; color: white;">S.T(%)</td>
<td style="background-color: dodgerblue; color: white;">S.T(Amt)</td>
<td style="background-color: dodgerblue; color: white;">Net-Amt</td>
<td style="background-color: dodgerblue; color: white;">Deletion</td>

<?
               
           
       
        echo "</tr>";
        $count=0;
        $one=1;
 $stot=0;
 $stax=0;
 $gtot1=0;
 $ftax=0;
$gtot=0;
$tqty=0;
                                        while($row = mysqli_fetch_object($result))
                                        {
                                        $count=$one+$count;

$stot = $row->Total+$stot;
$gtot1=$row->N_Total+$gtot1;
$ftax=$row->F_Amt+$ftax;
$stax=$row->S_Amt+$stax;
$gtot=$row->G_Total+$gtot;
$qty=$row->Qty+$qty;
$tqty=$row->Tqty+$tqty;
$tcarts=$row->Carton+$tcarts;
$tdis=$row->D_Amt+$tdis;


echo "<td align=center><font size=3 color=black><b>$count</b></td>";
echo "<td align=left><font size=3 color=black><b>$row->Name</b></td>";
echo "<td><font size=3 color=black>$row->Grade</td>";
echo "<td align=center><font size=3 color=black>$row->Unit</td>";
echo "<td align=right><font size=3 color=black>$row->Carton</td>";
echo "<td align=right><font size=3 color=black>$row->Tqty</td>";
echo "<td align=right><font size=3 color=black>$row->Qty</td>";

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

echo "<td align=right><font size=3 color=black>$row->FED</td>";
echo "<td align=right><font size=3 color=black>$row->F_Amt</td>";
echo "<td align=right><font size=3 color=black><b>$row->G_Total</td>";
echo "<td align=right><font size=3 color=black>$row->Stax</td>";
echo "<td align=right><font size=3 color=black>$row->S_Amt</td>";

echo "<td align=right><font size=3 color=black><b>$row->N_Total</td>";
?>
<td align=center>
 <a href="add_inv.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>
</td>
<?
echo "<tr>";
}
echo "        <td align=center colspan=4>";
echo "<b><font size=3 color=darkblue>Grand Totals</b>";

echo "<td align=right><font size=3 color=black><b>$tcarts</b></td>";
echo "<td align=right><font size=3 color=black><b>$tqty</b></td>";
echo "<td align=right><font size=3 color=black><b>$qty</b></td>";

echo "<td align=center></td>";
echo "<td align=center></td>";

echo "<td align=right><font size=3 color=black><b>$stot</b></td>";
echo "<td align=right><font size=3 color=black><b>$tdis</b></td>";
echo "<td align=center></td>";
echo "<td align=right><font size=3 color=black><b>$ftax</b></td>";
echo "<td align=right><font size=3 color=black><b>$gtot</b></td>";
echo "<td align=center></td>";
echo "<td align=right><font size=3 color=black><b>$stax</b></td>";

echo "<td align=right><font size=3 color=black><b>$gtot1</b></td>";
echo "<td align=center></td>";

$ntot1=$stot-$gdis-$gcom;



?>
</form>
</table>
</div>
<style type="text/css">
#left                 {width:52%;height:auto;float:left;padding-top:0%;padding-left:0.5%;font-weight:bold;}
#right                {width:48%;height:auto;float:right;padding-top:0%;padding-right:0.5%;font-weight:bold;}

</style>



<div id="left">

<form method="POST" name="m_head1" onSubmit="return validateForm1()">
 <table align="center" id="customers1">
  
<tr>
    <th colspan="6"><font size="4">COMPANY DETAILS</th>

  </tr>               

<td align=left ><b><font size=3 color=black>Select Company:</b></td>
                        <td align=left colspan="3">
                                <select class="form-control" name="ctype" id="ctype"  onchange="show_cbo11()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['M_Headc']))
                                        {
                                                $M_Headc = $_GET['M_Headc'];
                                                
                                                
                
                                        }
                                else                        
                                        $M_Headc = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from acc_heads where M_Head='Company' order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($M_Headc == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>
<?
$result420 = mysqli_query($link, "select Sum(Debit-Credit) as bal from ledgers where M_Head='Company' and Code='$M_Headc' group by Code  ");
                $row420 =  mysqli_fetch_object($result420);
                        $vbal = $row420->bal;

echo "<td align=right><b><input type='text' name='vbal' size='15' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='vbal' value='$vbal' onkeypress='return processkey(event)' readonly>";
?>
<tr>
<td align=left><b><font size=3 color=black>Select Warehouse:</b></td>
                        <td align=left colspan="4">
                                <select class="form-control" name="ware" id="ware"  onchange="moveToAmount()">
                                <option value="">select</option>
                                <?php 
                                if(isset($_GET['Ware']))
                                        {
                                                $Ware = $_GET['Ware'];
                                                
                                                
                
                                        }
                                else                        
                                        $Ware = "";
                                        
                                        //include('config.php');
                                        $result = mysqli_query($link, "select Distinct Name from warehouse order by Name");
                                        while($row  = mysqli_fetch_object($result))
                                        {
                                                        if($Ware == $row->Name)
                                                        {
                                                                echo "<option value=\"$row->Name\" selected=\"selected\">$row->Name</option>";
                                                        }        
                                                        else
                                                        {
                                                                echo "<option value=\"$row->Name\">$row->Name</option>";
                                                        }
                                        }
                                ?>
                                </select>

                                <?


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


?>
<td colspan="2"><b><font size=3 color=black>A/Tax 236G:</b></td>
<?
echo "<td align=left><b><input type='text' name='atax' size='8' style='color: black; font-family: Times-Roman; background-color: lightblue; font-size:2' id='atax' value='0' onkeypress='return processkey(event)'>";
?>
<tr>
<td><b><font size=3 color=black>D.C Number:</b></td>
<td colspan="2"><input type="text" name="dcn" id="dcn"  size="15" value="0" onfocus="this.select()" /></td>

<td><b><font size=3 color=black>Purchase Order No.:</b></td>
<td colspan="3"><input type="text" name="purn" size="15" value="0" /></td>
 <tr>

<td><b><font size=3 color=black>Vehicle No.:</b></td>
<td colspan="5"><input type="text" name="vehe" size="15" value="" /></td>
 <tr>
<td colspan="5" align="center">
 <input type="submit" name="save1" value="Save Invoice" style="background: none; color:#007BFF; border:2px solid #007BFF; padding:2px 20px; border-radius:5px;">

</tr>

</table>


</fieldset>
</div>


   <div id="right">

<table align="center" id="customers1">
       
<tr>
    <th colspan="4"><font size="4">CALCULATIONS</th>

  </tr>
              <tr>
                        <td width="250"><b><font size=3 color="black">Value Exluding FED and S.Tax:</b></td>
                        <td colspan="3"><input type="text" name="strtot" size="10" value="<?php echo $stot ?>" readonly /></td>
                </tr>

                <tr>
                        <td><b><font size=3 color="black">Amount of FED:</b></td>
                        <td><input type="text" name="strtdis" value="<?php echo $ftax ?>" size="10" readonly /></td>
               <tr>
                         <td><b><font size=3 color="black">Value Including FED:</b></td>
                        <td><input type="text" name="strtcom" value="<?php echo $gtot ?>" size="10" readonly/></td></tr>
 <tr>
                        <td><b><font size=3 color="black">Amount of Sales Tax:</b></td>
                        <td colspan="3"><input type="text" name="strtgst" value="<?php echo $stax ?>" size="10" readonly/></td>
                </tr>
 
                <tr>
                        <td><b><font size=3 color="black">Value with FED and Sales Tax :</b></td>
                        <td colspan="3"><input type="text" name="strnet" size="10" value="<?php echo $gtot1 ?>" readonly /></td>
                </tr>
                
            <tr>

   </form>             
  <td colspan="7" align="right">
    <span class="icon text-white-50">
      <i class="fas fa-book"></i>
    </span>
    <button class="btn btn-info btn-icon-split" onclick="openAndPrintPDF()"><span class="text">View Last Invoice</span></button>
 
               </table>
               

<?

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


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


$vend = $_POST['ctype'];
$dcn = $_POST['dcn'];
$purn = $_POST['purn'];
$vehe = $_POST['vehe'];
$wareh = $_POST['ware'];
$atax = $_POST['atax'];

$sql2 = "SELECT * FROM temp_cinv 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 p_details ");
$row =  mysqli_fetch_object($result);
$v_no2 = $row->v_no2 + 1;

$salesInv = str_pad($v_no2, 6, "0", STR_PAD_LEFT);
$salesInv = "PIB".$salesInv;




$ttot = 0;
$gtot = 0;
$ntot = 0;
$ctot = 0;
$stot = 0;
$atax1 = 0;
$result = mysqli_query($link, "select * from temp_cinv where addby='$usr' order by Id");
while($row = mysqli_fetch_array($result))
{
//$pack = $row['Packing'];
//$cname = $row['C_Name'];
//$vidd = $row['V_Id'];

$dtot = $row['D_Amt']+$dtot;
$ttot = $row['Total']+$ttot;
$gtot = $row['G_Total']+$gtot;
$ntot = $row['N_Total']+$ntot;
$ctot = $row['F_Amt']+$ctot;
$stot = $row['S_Amt']+$stot;
$totpacks = $row['Tqty'];
$totcarts = $row['Carton'];
$fname = $row['Name'];
//$pack = mysqli_real_escape_string($link, $pack);
//$cname = mysqli_real_escape_string($link, $cname);
mysqli_query($link, "insert into purchases (Company, Inv_No, Inv_Det, Inv_Date, Inv_Type, Name, Grade, Carton_Qty, Unit, Qty, Tqty, Carton, Rate, R_Dis, FED, F_Amt, Stax, S_Amt, Total, G_Total, N_Total, addby, addon, DC_No, Pur_No, Vehicle, Warehouse, Inv_N) values ('$vend','$v_no2','$salesInv','$invdate','Dir Purchase','$row[Name]','$row[Grade]','$row[Carton_Qty]','$row[Unit]','$row[Qty]','$row[Tqty]','$row[Carton]','$row[Rate]','$row[D_Amt]','$row[FED]','$row[F_Amt]','$row[Stax]','$row[S_Amt]','$row[Total]','$row[G_Total]','$row[N_Total]','$usr','$date','$dcn','$purn','$vehe','$wareh','PIB')");


$result42 = mysqli_query($link, "select Item_Type from products where Full_Name='$fname'");
while($row42 = mysqli_fetch_array($result42))
{
$itemtpe = $row42['Item_Type'];
}


if($itemtpe=='Pack' && $totpacks>0)
{

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','$itemtpe','$row[Name]','Comp Sale','$row[Tqty]','$usr','$vend','$date','1')");

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','Sticker','$row[Name]','Comp Sale','$row[Tqty]','$usr','$vend','$date','1')");

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','Seal','Seal','Comp Sale','$row[Tqty]','$usr','$vend','$date','1')");

}

if($itemtpe=='Pack' && $totcarts>0)
{

$result420 = mysqli_query($link, "select Carton from products where Full_Name='$fname'");
while($row420 = mysqli_fetch_array($result420))
{
$cartname = $row420['Carton'];
}
    mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','Carton','$cartname','Comp Sale','$row[Carton]','$usr','$vend','$date','1')");
}


if($itemtpe=='Bucket' && $totpacks>0)
{

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','$itemtpe','$row[Name]','Comp Sale','$row[Tqty]','$usr','$vend','$date','1')");

}


if($itemtpe=='Drum' && $totpacks>0)
{

mysqli_query($link, "insert into raw_sales (Inv_Date, Inv2, Inv_Det2, Category, Name, Inv_Type, S_Out, addby, Company, addon, Status) values ('$invdate','$v_no2','$salesInv','$itemtpe','$row[Name]','Comp Sale','$row[Tqty]','$usr','$vend','$date','1')");

}



}



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

mysqli_query($link, "insert into p_details (Company, Inv_No, Inv_Det, Inv_Date, Inv_Type, Total, N_Total, G_Total, addby, addon, F_Amt, S_Amt, DC_No, Pur_No, Vehicle, Warehouse, Inv_N, A_Tax, A_Amt, D_Amt) values ('$vend','$v_no2','$salesInv','$invdate','Dir Purchase','$ttot','$ntotal','$gtot','$usr','$date','$ctot','$stot','$dcn','$purn','$vehe','$wareh', 'PIB','$atax','$atax1','$dtot')");



$rst1 = mysqli_query($link, "select A_Id from acc_heads where Name='$vend'");
while($row1 = mysqli_fetch_array($rst1))
{
$v_id = $row1['A_Id'];
}


$dets="Purchases via Inv# ".$salesInv.". Ref-DC#".$dcn;
 mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details, Credit, D_Date, usr, L_Type, V_Time, Inv_No, Inv_Det) values ('$v_id','Company','$vend','$dets','$ntotal','$invdate','$usr','Finished Purchases','$date','$v_no2','$salesInv')");


$phead="Purchases-".$vend;
$rst2 = mysqli_query($link, "select A_Id from acc_heads where Name='$phead'");
while($row2 = mysqli_fetch_array($rst2))
{
$p_id = $row2['A_Id'];
}

mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details, Debit, D_Date, usr, L_Type, V_Time, Inv_No, Inv_Det) values ('$p_id','Product Purchases','$phead','$dets','$gtot','$invdate','$usr','Finished Purchases','$date','$v_no2','$salesInv')");


if($stot!=0)
{
$shead="Sales Tax-".$vend;

$rst3 = mysqli_query($link, "select A_Id from acc_heads where Name='$shead'");
while($row3 = mysqli_fetch_array($rst3))
{
$s_id = $row3['A_Id'];
}

$dets="Sales Tax for Inv#".$salesInv.". Ref-DC#".$dcn;
 mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No, Inv_Det) values ('$s_id','General Sales Tax','$shead','$dets','$stot','$invdate','$usr','Finished Purchases','$date','$v_no2','$salesInv')");

}

if($atax1!=0)
{
$rst4 = mysqli_query($link, "select A_Id from acc_heads where Name='Other Taxes'");
while($row4 = mysqli_fetch_array($rst4))
{
$o_id = $row4['A_Id'];
}


$dets="A/Tax Inv#".$salesInv.". Ref-DC#".$dcn."Company->".$vend;
 mysqli_query($link, "insert into ledgers (A_Id, M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No, Inv_Det) values ('$o_id','Expense','Other Taxes','$dets','$atax1','$invdate','$usr','Finished Purchases','$date','$v_no2','$salesInv')");

}
/*
if($ctot!=0)
{
$dets="Advance Tax 236G for Inv#".$v_no2.". Ref#".$rem;
 mysqli_query($link, "insert into ledgers (Company, M_Head, Code, Details , Debit, D_Date, usr, L_Type, V_Time, Inv_No) values ('$comp','Expense','Advance Sales Tax','$dets','$atax2','$invdate','$usr','Purchases','$date','$v_no2')");

}
*/
mysqli_query($link, "delete from temp_cinv where addby='$usr' ");





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

donot:
}

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