<?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'];
    $add2=$rowcomp['add2'];
    $city=$rowcomp['city'];
    $telephone=$rowcomp['telephone'];
    $email=$rowcomp['email'];
    $web=$rowcomp['web'];
    $vatno=$rowcomp['vatno'];
}

///// Open inv_header
$query=mysqli_query($con,"SELECT inv_header.*,customer_master.cus_name,customer_master.add1,customer_master.city,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));

$count=mysqli_num_rows($query);

if ($count>0) {
  while($rowsp=mysqli_fetch_array($query)){     
    $inv_date=$rowsp['inv_date'];
    $loc_name=$rowsp['loc_name']; 
    $cus_name=$rowsp['cus_name'];
    $po=$rowsp['inv_ref'];
    $note=$rowsp['note'];
    $edate1=$rowsp['edate'];
    $discount=$rowsp['discount'];
    $des=$rowsp['des'];
    $emp_name=$rowsp['emp_name'];
    $address=$rowsp['add1'];
    $cuscity=$rowsp['city'];
    $cus_code=$rowsp['cus_code'];
    $note=$rowsp['note'];
    $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'];
    $po=$rowsp['po'];
  }

  if($vat_inv==1){   
    $vat_des="TAX";
    $vat_sql="select * from company_master";
    $query_vat=mysqli_query($con,$vat_sql)or die(mysqli_error($con));
    while($rowv=mysqli_fetch_array($query_vat)){ 
      $vat_reg=$rowv['br'];
    }
  } else { 
    $vat_des=" "; 
  }
}
?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="custom.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<style>
body {
  background-color:#EFF8FF;
}
h1, p {
  margin:0px;
}

/* Removed border from the main container */
.main-section {
  background-color: #FFF;
  /* border: 1px solid #8D43AC;  <-- Removed this line */
}

.header {
  background-color: #8D43AC;
  padding: 10px 10px 8px 10px; /* Reduced padding */
  color: #fff;
}

.content {
  padding: 15px 10px 15px 10px;
}

th {
  background-color: #8D43AC;
  color: #fff;
  text-align: right;
}

.table td:nth-child(1),
.table th:nth-child(1) {
  text-align: left;
}

/* Add border to the item table only */
.table {
  border: 1px solid #8D43AC;
  border-collapse: collapse;
}

.table th, .table td {
  border: 1px solid #8D43AC;
  padding: 2px 3px; /* Reduced padding for tighter rows */
  line-height: 1.1;
}

/* Last section padding */
.lastSection {
  padding: 15px 10px 20px 10px;
}

/* Reduce vertical spacing in header rows */
.header .row {
  margin-bottom: 3px !important;
}

/* Reduce bottom margin for header text */
.header h4 {
  margin-bottom: 3px !important;
  line-height: 1.1;
}

/* Reduce spacing between rows in invoice info */
#invoiceSection .row {
  margin-bottom: 3px;
}

#invoiceSection h4, #invoiceSection p {
  margin: 0;
  padding: 0;
}
/* Reduce bottom margin below the invoice title row */
.row.invoice-title-row {
  margin-bottom: 1px !important; /* Adjust value as needed */
}
</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 col-sm-12 header">

            <!-- Header -->
            <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-6">
                <h4><?php echo $cost_name; ?></h4>
              </div>    
            </div>   

            <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-6">
                <h4><?php echo $add1; ?></h4>
              </div>    
            </div> 

            <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-6">
                <h4>Email : <?php echo $email; ?></h4>
              </div>    
            </div> 

            <div class="row">
              <div class="col-md-6 col-sm-6 col-xs-6">
                <h4>Web : <?php echo $web; ?></h4>
              </div>    
            </div> 

           <div class="row invoice-title-row">
            <div class="col-md-6 col-md-offset-3 text-center">
                <h4 style="text-decoration: underline;"><?php echo $vat_des; ?> INVOICE</h4>
            </div>    
            </div>

          </div>

          <div class="col-md-12 col-sm-12 content" style="margin-top:2px; padding-top:2px;">
            <div class="row">
              <div class="col-md-6 col-sm-12 col-xs-6">
                <table>
                  <tr><td>Customer </td><td>:</td><td><?php echo $cus_name; ?></td></tr>
                  <tr><td>Address </td><td>:</td><td><?php echo $address . "," . $cuscity; ?></td></tr>
                  <tr><td>Delivery To </td><td>:</td><td><?php echo $delivery_name . "," . $delivery_address; ?></td></tr>
                </table>
              </div>
              <div class="col-md-6 col-sm-6 col-xs-6">
                <table>
                  <tr><td width="30%">Invoice No </td><td width="2%">:</td><td><?php echo $inv_no; ?></td></tr>
                  <tr><td width="30%">Invoice Date </td><td width="2%">:</td><td><?php echo $edate1; ?></td></tr>
                  <tr><td width="30%">PO </td><td width="2%">:</td><td><?php echo $po; ?></td></tr>
                  <tr><td width="30%">Supplier Code </td><td width="2%">:</td><td><?php echo $sup_code; ?></td></tr>
                </table>
              </div>
            </div>
          </div>

          <div class="col-md-12 col-sm-12 text-right">
            <table class="table">
              <thead>
                <tr>
                  <th>#</th>
                  <th class="text-left">Part No</th>
                  <th>Qty</th>
                  <th>Unit Price</th>
                  <th>Amount</th>
                </tr>
              </thead>
              <tbody>
                <?php
                $sub_total=0;
                $inv_discount=0;
                $net_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' order by eseq") or die(mysqli_error($con));

                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'];
                  $eseq=$row['eseq'];

                  $tot_sales=($sales_price * $row['qty'])-($sales_price * $row['qty'] *  $item_dis/100);
                  $sub_total += $tot_sales;
                  $total_qty += $row['qty'];
                  $ncount++;

                  echo "<tr>
                    <td class='text-left'>$ncount</td>
                    <td class='text-left'>$item_name</td>
                    <td>$qty</td>
                    <td>".number_format($sales_price,2)."</td>
                    <td>".number_format($tot_sales,2)."</td>
                  </tr>";
                }

                if ($discount != 0) {
                  $inv_discount= $sub_total * $discount/100;
                  $net_total=$sub_total - $inv_discount;
                } else { 
                  $net_total=$sub_total;
                }

                $taxable_amount = $net_total / (1 + ($vat / 100));
                $tax_amount = $net_total - $taxable_amount;
                ?>
              </tbody>
              <tr class="text-right">
                <td class="text-left"><strong>Net Total</strong></td>
                <td class="text-left"></td>
                <td><span style="font-weight:bold"><u><?php echo number_format($total_qty,0); ?></u></span></td>
                <td></td>
                <td><span style="font-weight:bold"><u><?php echo number_format($net_total,2); ?></u></span></td>
              </tr>
              <?php if($vat != 0) { ?>
              <tr class="text-right">
                <td class="text-left">Vat <?php echo $vat; ?>%</td>
                <td class="text-left"></td>
                <td></td>
                <td></td>
                <td><span style="font-weight:bold"><u><?php echo number_format($tax_amount,2); ?></u></span></td>
              </tr>
              <?php } ?>
            </table>
          </div>

          <div class="col-md-12 col-sm-12 lastSection">
            <table width="80%" border="0">
              <tr>
                <td width="30%" class="text-center">Issued By</td>
                <td width="30%" class="text-center">Received By:</td>
                <td width="30%" class="text-center">Customer Signature:</td>
              </tr>
              <tr>
                <td width="30%" class="text-center"><br><br></td>
                <td width="30%" class="text-center"></td>
                <td width="30%" class="text-center"></td>
              </tr>
              <tr>
                <td width="30%" class="text-center">...................................</td>
                <td width="30%" class="text-center">...................................</td>
                <td width="30%" class="text-center">...................................</td>
              </tr>
            </table>
          </div>

        </div> <!-- main-section -->
      </div> <!-- col-md-8 col-md-offset-2 -->
    </div> <!-- row -->
  </div> <!-- container -->
</div> <!-- invoiceSection -->

</body>
</html>
