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

$userid = $_SESSION['rm_id'];
$user_name = $_SESSION['rm_name'];

if (isset($_GET['inv_no'])) {
  $inv_no = trim($_GET['inv_no']);
}
if (isset($_POST['inv_no'])) {
  $inv_no = trim($_POST['inv_no']);
}

// Company Info
$company = "select * from company_master";
$querycomp = mysqli_query($con, $company) or die(mysqli_error($con));
while ($rowcomp = mysqli_fetch_array($querycomp)) {
  $cost_name = $rowcomp['cost_name'];
  $add1 = $rowcomp['address'];
  $email = $rowcomp['email'];
  $web = $rowcomp['web'];
  $vatno = $rowcomp['vatno'];
}

// SR Header
$query = mysqli_query($con, "SELECT inv_header.*,customer_master.cus_name,customer_master.add1,customer_master.cus_code,customer_master.vat_number,customer_master.sup_code,stock_locations.loc_name,invoice_types.des,employee_master.emp_name,
customer_master_delivery.delivery_name,customer_master_delivery.delivery_address
FROM inv_header
LEFT JOIN customer_master ON inv_header.cus_id=customer_master.cus_id
LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id
LEFT JOIN invoice_types ON inv_header.invtype_id=invoice_types.invtype_id
LEFT JOIN employee_master ON inv_header.emp_id=employee_master.emp_id
LEFT JOIN customer_master_delivery ON customer_master_delivery.delivery_id=inv_header.delivery_id
WHERE inv_id='$inv_no'") or die(mysqli_error($con));

if (mysqli_num_rows($query) > 0) {
  while ($rowsp = mysqli_fetch_array($query)) {
    $cus_name = $rowsp['cus_name'];
    $po = $rowsp['po'];
    $edate1 = $rowsp['edate'];
    $discount = $rowsp['discount'];
    $address = $rowsp['add1'];
    $vat_inv = $rowsp['vat_inv'];
    $vat_number = $rowsp['vat_number'];
    $sup_code = $rowsp['sup_code'];
    $delivery_name = $rowsp['delivery_name'];
    $delivery_address = $rowsp['delivery_address'];
     $inv_ref = $rowsp['inv_ref'];
      $inv_date = $rowsp['inv_date'];
  }

  $vat_des = $vat_inv == 1 ? "TAX" : "";
}
?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>
body {
  background-color: #EFF8FF;
}
.main-section {
  background-color: #FFF;
}
.header {
  background-color: #8D43AC;
  padding: 10px 15px;
  color: #fff;
  margin-bottom: 5px;
}
th {
  background-color: #8D43AC;
  color: #fff;
  text-align: center;
}
.table {
  border: 1px solid #8D43AC;
  border-collapse: collapse;
  margin-bottom: 0;
}
.table th, .table td {
  border: 1px solid #8D43AC;
  padding: 4px;
}
.content {
  padding: 5px 15px;
}
#invoiceSection .row {
  margin-bottom: 3px;
}
#invoiceSection h4, #invoiceSection p {
  margin: 0;
  padding: 0;
}
tfoot {
  display: table-row-group;
}
</style>
</head>
<body>
<div id="invoiceSection">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="row main-section">
<div class="col-md-12 header">
  <div class="row"><div class="col-md-6"><h4><?php echo $cost_name; ?></h4></div></div>
  <div class="row"><div class="col-md-6"><h4><?php echo $add1; ?></h4></div></div>
  <div class="row"><div class="col-md-6"><h4>Email : <?php echo $email; ?></h4></div></div>
  <div class="row"><div class="col-md-6"><h4>Web : <?php echo $web; ?></h4></div></div>
  <div class="row"><div class="col-md-6"><h4>VAT Reg No : <?php echo $vatno; ?></h4></div></div>
  <div class="row"><div class="col-md-6 col-md-offset-3 text-center">
    <h4 style="text-decoration: underline;"><?php echo $vat_des; ?> SALES RETURN</h4></div></div>
</div>

<div class="col-md-12 content">
<table style="width: 100%; border-collapse: collapse; margin-bottom: 5px;">
<tr>
<td width="10%">Customer</td><td width="1%">:</td><td width="50%"><?php echo $cus_name; ?></td>
<td width="15%">SR No</td><td width="2%">:</td><td><?php echo $inv_no; ?></td></tr>
<tr>
<td>Address</td><td>:</td><td><?php echo $address; ?></td>
<td>SR Date</td><td>:</td><td><?php echo  $inv_date; ?></td></tr>
<tr>
<td>VAT No</td><td>:</td><td><?php echo $vat_number; ?></td>
<td>PO</td><td>:</td><td><?php echo $po; ?></td></tr>
<tr>
<td>Invoice</td><td>:</td><td><?php echo "$inv_ref"; ?></td>
<td>Supplier Code</td><td>:</td><td><?php echo $sup_code; ?></td></tr>
</table>
</div>

<div class="col-md-12 text-right">
<table class="table">
<thead>
<tr>
<th>#</th>
<th class="text-left">Item</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$sub_total = 0;
$total_qty = 0;
$ncount = 0;

$queryd = mysqli_query($con, "SELECT inv_detail.*, item_master.item_name 
FROM inv_detail
LEFT JOIN item_master ON item_master.item_id = inv_detail.item_id
WHERE inv_id='$inv_no'") or die(mysqli_error($con));

$vat = 0;
while ($row = mysqli_fetch_array($queryd)) {
  $item_name = $row['item_name'];
  $qty = $row['qty'];
  $sales_price = $row['sales_price'];
  $item_dis = $row['item_dis'];
  $vat = $row['vat']; // Assuming all items have same VAT
  $sales_price_ex_vat = $sales_price / (1 + ($vat / 100));
  $tot_sales = ($sales_price_ex_vat * $qty) - ($sales_price_ex_vat * $qty * $item_dis / 100);
  $sub_total += $tot_sales;
  $total_qty += $qty;
  $ncount++;
  echo "<tr>
  <td>$ncount</td>
  <td class='text-left'>$item_name</td>
  <td>$qty</td>
  <td>".number_format($sales_price_ex_vat,3)."</td>
  <td>".number_format($tot_sales,3)."</td>
  </tr>";
}
$inv_discount = ($discount <> 0) ? ($sub_total * $discount / 100) : 0;
$nsub_total = $sub_total - $inv_discount;
$vat_amount = $nsub_total * ($vat / 100);
$net_total = $nsub_total + $vat_amount;
?>
</tbody>
<tfoot>
<tr>
  <th colspan="2"></th>
  <th class="text-right"><?php echo number_format($total_qty, 0); ?></th>
  <th class="text-right">Sub Total</th>
  <th class="text-right"><?php echo number_format($sub_total, 3); ?></th>
</tr>
<tr>
  <th colspan="3"></th>
  <th class="text-right">Net</th>
  <th class="text-right"><?php echo number_format($nsub_total, 3); ?></th>
</tr>
<tr>
  <th colspan="3"></th>
  <th class="text-right">VAT <?php echo $vat; ?>%</th>
  <th class="text-right"><?php echo number_format($vat_amount, 3); ?></th>
</tr>
<tr>
  <th colspan="3"></th>
  <th class="text-right">Net Total</th>
  <th class="text-right"><?php echo number_format($net_total, 3); ?></th>
</tr>
</tfoot>
</table>
</div>

<div class="col-md-12 content" style="padding-top:10px;">
<table width="100%" border="0">
<tr>
<td class="text-center">Customer Signature</td>
<td class="text-center">Authorized Signature</td>
</tr>
<tr><td><br><br></td><td></td></tr>
<tr>
<td class="text-center">....................................</td>
<td class="text-center">....................................</td>
</tr>
</table>
</div>

</div></div></div></div></div>
</body>
</html>
