<?
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'];



 ?>


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

  <title>GLAKB  | Sales Return 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">

<?

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: 1px;

}

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


#customers th {
  padding-top: 1px;
  padding-bottom: 1px;
  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: 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;

}
 body {
      margin: 0;
      padding-bottom: 60px; /* Give space for bottom bar */
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    .bottom-bar {
      position: fixed;
      bottom: 0;
      width: 100%;
      background: linear-gradient(to right, royalblue, royalblue);
      color: white;
      text-align: center;
      padding: 5px 10px;
      font-size: 16px;
      box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
    }

    .bottom-bar span {
      margin: 0 10px;
    }

    @media (max-width: 600px) {
      .bottom-bar {
        font-size: 14px;
        flex-direction: column;
      }
    }

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

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

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

                                                document.m_head.qty.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 openAndPrintPDF() {
      var pdfWindow = window.open('inv_sales.php?usr=<?php echo $usr ?>', '_blank');
    }

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

if((document.m_head1.city.value) == '')
{
alert("Please Select City First!");
document.m_head1.city.focus();
return false;
}
if((document.m_head1.ctype.value) == '')
{
alert("Please Select Customer Name First!");
document.m_head1.ctype.focus();
return false;
}
if((document.m_head1.salesman.value) == '')
{
alert("Please Select Salesman!");
document.m_head1.salesman.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.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;
location.href = "attach_prod1.php?Code="+val;
}






function myFunction() 
{
$x = document.getElementById("adis").value;
$y = document.getElementById("abal").value;
$c= 0;
$c=$y-$x;

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


}



  </script>





 <table align="center" id="customers1">
       
<tr>
    <th colspan="6"><font size="5">SALES RETURN INVOICE</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 s_details" );
while($row12  = mysqli_fetch_object($result12))
{
$inv=$row12->inv+1;
}

echo "<td colspan=6 align=right>Invoice#  SIR-<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>
<tr>


</td>

</table>
 

 

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

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

  </tr>               


<tr>
<?
$result420 = mysqli_query($link, "select * from cust_details where addby='$usr'");
while($row  = mysqli_fetch_object($result420))
{
$city=$row->City;
$cust=$row->Customer;
$ware=$row->Warehouse;
$itype=$row->Ctype;
$dued=$row->Dued;
$adr=$row->Adress;
$sman=$row->Salesman;
}

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



?>


<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 Full_Name from products order by Article");
                                        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>




<?






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

}
}

if($itype=='Whole Sale')
{
$result1 = mysqli_query($link, "select Unit_Liter, Carton_Qty, W_Price from products where Full_Name='$Code'");
while($row  = mysqli_fetch_object($result1))
{
$numbs=$row->Unit_Liter;
$ucart=$row->Carton_Qty;
$price=$row->W_Price;

}
}

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

}
}

      
?>
<tr>
<td colspan="1"><b><font size=3 color="black">Litres / 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=3><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>Quantity Unit:</b></td>

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

</select>
<tr>
<td><b><font size=3 color=black>Issued Quantity:</b></td>


<?

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

?>

<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><b><font size=3 color=black>Sales Tax (in %):</b></td>
<td ><input type="text" name="stax" size="15" onkeypress="return processkey(event)" value="0" /></td>

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

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

</td>
<tr>

</table>


</fieldset>
</div>


   <div id="right">

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

  </tr>
              


<tr>
                        <td width="200"><b><font size=3 color="black">City:</b></td>
                        <td colspan="3"><input type="text" name="cty" size="30" value="<?php echo $city ?>" readonly /></td>
                </tr>


              
                        <td><b><font size=3 color="black">Customer Name:</b></td>
                        <td colspan="3"><input type="text" name="cust1" value="<?php echo $cust ?>" size="30" readonly/></td>
                </tr>
 <tr>
                        <td width="200"><b><font size=3 color="black">Customer Address:</b></td>
                        <td colspan="3"><input type="text" name="adr1" size="30" value="<?php echo $adr ?>" readonly /></td>
                </tr>

                <tr>
                        <td><b><font size=3 color="black">Warehouse:</b></td>
                        <td colspan="3"><input type="text" name="ware1" size="30" value="<?php echo $ware ?>" readonly /></td>
                </tr>

<tr>
                        <td width="200"><b><font size=3 color="black">Invoice Type:</b></td>
                        <td colspan="3"><input type="text" name="itype" size="30" value="<?php echo $itype ?>" readonly /></td>
                </tr>

 
<tr>
                        <td width="200"><b><font size=3 color="black">Salesman:</b></td>
                        <td colspan="3"><input type="text" name="sman1" size="30" value="<?php echo $sman ?>" readonly /></td>
                </tr>              
<tr>
                        <td width="220"><b><font size=3 color="black">Customer Previous Balance:</b></td>
                        <td colspan="3"><input type="text" name="strtot" size="30" value="<?php echo $vbal ?>" readonly /></td>
                </tr>


            <tr>

   
               </table>
     </div>           


  <div class="table-container">
<table align="center" id="customers">



<?
if($_GET['action']=='delete'){
$lid=intval($_GET['tid']);
//$profilepic=$_GET['profilepic'];
//$ppicpath="teacherspic"."/".$profilepic;
$query=mysqli_query($link,"delete from temp_sinv 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());

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

$cname=$_POST['item3'];
$unit=$_POST['unit'];
$ucart=$_POST['ucart'];
$qty=$_POST['qty'];
$iqty=$_POST['iqty'];

$stax=$_POST['stax'];
$rate=$_POST['dp'];
$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;


$stamt=($newtot/100)*$stax;

$ntot=$newtot+$stamt;
/*
$sql1 = "SELECT * FROM temp_sinv 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_sinv (Name, Warehouse, Inv_Type, Unit_Liter, Carton_Qty, Qty, Tunit, Carton, Rate, N_Rate, Stax, S_Amt, Total, G_Total, N_Total, addby, D_Amt, R_Dis) values ('$cname','$ware','$itype','$unit','$ucart','$tlit','$tunit','$tcart','$rate','$nrate','$stax','$stamt','$acttot','$newtot','$ntot','$usr','$newdis','$dis')");
  
//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" name="m_head1" onSubmit="return validateForm1()">
  <?                     




$result = mysqli_query($link, "select * from temp_sinv 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;">Warehouse</td>
<td style="background-color: dodgerblue; color: white;">Liter/Unit</td>
<td style="background-color: dodgerblue; color: white;">Unit/Carton</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;">Price</td>
<td style="background-color: dodgerblue; color: white;">Total Amount</td>
<td style="background-color: dodgerblue; color: white;">Discount</td>
<td style="background-color: dodgerblue; color: white;">Gross Amount</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 Amount</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;
$dtot = $row->G_Total+$dtot;

$gtot1=$row->N_Total+$gtot1;
$stax=$row->S_Amt+$stax;
$tqty=$row->Qty+$tqty;
$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 align=center><font size=3 color=black>$row->Warehouse</td>";
echo "<td align=center><font size=3 color=black>$row->Unit_Liter</td>";
echo "<td align=center><font size=3 color=black>$row->Carton_Qty</td>";
echo "<td align=center><font size=3 color=black>$row->Carton</td>";
echo "<td align=center><font size=3 color=black>$row->Tunit</td>";
echo "<td align=center><font size=3 color=black>$row->Qty</td>";

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

echo "<td align=center><font size=3 color=black>$row->Stax</td>";
echo "<td align=center><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="attach_prod1.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=5>";
echo "<b><font size=3 color=darkblue>Grand Totals</b>";
echo "<td align=right><font size=3 color=black><b>$tqty</b></td>";
echo "<td align=center></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=center><font size=3 color=black><b>$tdis</b></td>";
echo "<td align=center><font size=3 color=black><b>$dtot</b></td>";

echo "<td align=center></td>";
echo "<td align=center><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>




<form method="POST" name='m_head2'>

   <div id="left">

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

  </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><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>Net Amount:</b></td>
<?

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

?>    






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

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

?>  


 <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='30' style='color: black; font-family: Times-Roman; font-size:2' id='rems' value='' >";

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

         
  

<?


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

$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;
$cid=$row->A_Id;

}
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'];

$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 = "SIR".$salesInv;


$ttot = 0;
$gtot = 0;
$ntot = 0;
$dis = 0;
$stot = 0;
$result = mysqli_query($link, "select * from temp_sinv where addby='$usr' order by Id");
while($row = mysqli_fetch_array($result))
{
$warehousee = $row['Warehouse'];
$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['D_Amt']+$dis;


$ncart=$row['Carton'] * -1;
$nqty=$row['Qty'] * -1;
$ntunit=$row['Tunit'] * -1;
$ndamt=$row['D_Amt'] * -1;
$nsamt=$row['S_Amt'] * -1;
$newtot=$row['Total'] * -1;
$nntot=$row['N_Total'] * -1;
$ngtot=$row['G_Total'] * -1;




//$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 ('$cust','$v_no2','$salesInv','$invdate','$row[Inv_Type]','$row[Name]','$row[Unit_Liter]','$row[Carton_Qty]','$nqty','$ntunit','$ncart','$row[Rate]','$row[N_Rate]','$ndamt','$row[Stax]','$nsamt','$newtot','$ngtot','$nntot','$usr','$date','$row[Warehouse]','SIR','$city','$provi','$sman')");




}


$ttot1=$ttot * -1;
$ntot1=$ntot * -1;
$ngtot1=$gtot * -1;

$stot1=$stot * -1;
$dis1=$dis * -1;

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, O_Dis, Status, Province) values ('$cust','$v_no2','$salesInv','$invdate','$itypee','$ttot1','$ntot1','$ngtot1','$usr','$date','$stot1','$ware', 'SIR','$sman','$pmode','$city','$rem','$dis1','$adis','Sales Return','$provi')");



//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) values ('$cust','$v_no2','$salesInv','$invdate','$itypee','$ttot','$ntot','$gtot','$usr','$date','$stot','$ware', 'SIB','$sman','$pmode','$cty','$rem','$dis','Pending','$provi')");


$ftot = $ttot + $stot;



$dets="Sales Returns Invoice.";
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','$ftot','$invdate','$usr','Sales Return','$date','$v_no2','$salesInv','$sman')");

if($dis>0)
{
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','Discount Reverted due to Sales Return.','$dis','$invdate','$usr','Sales Return','$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 Return via Inv# ".$salesInv." from ".$cust;

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 ('$o_id','Sales','General Sales','$dets','$ntot','$invdate','$usr','Sales Return','$date','$v_no2','$salesInv','$sman')");









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


$dets="Cash Deposited for Inv# SIB-".$v_no2;

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


}



mysqli_query($link, "delete from temp_sinv where addby='$usr' ");


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




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

donot:
}



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