<?php

include_once("config.php"); 

$orderId = isset($_GET['id']) ? intval($_GET['id']) : 0;

$sqlOrder = "SELECT id, client_name, phone, email, company_name, gst, address, city, state, 
                     product_name, quantity, price, sub_total, discount, total_tax, total_amount, 
                     created_by, updated_by, created_at 
              FROM orders WHERE id = ?";
$stmt = mysqli_prepare($conn, $sqlOrder);
mysqli_stmt_bind_param($stmt, 'i', $orderId);
mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $id, $client_name, $phone, $email, $company_name, $gst, $address, $city, $state, 
                                    $product_name, $quantity, $price, $sub_total, $discount, $total_tax, 
                                    $total_amount, $created_by, $updated_by, $created_at);

$order = [];
if (mysqli_stmt_fetch($stmt)) {
    $created_at = date('d-m-Y', strtotime($created_at));
    $order = [
        'id' => $id,
        'client_name' => $client_name,
        'phone' => $phone,
        'email' => $email,
        'company_name' => $company_name,
        'gst' => $gst,
        'address' => $address,
        'city' => $city,
        'state' => $state,
        'product_name' => $product_name,
        'quantity' => $quantity,
        'price' => $price,
        'sub_total' => $sub_total,
        'discount' => $discount,
        'total_tax' => $total_tax,
        'total_amount' => $total_amount,
        'created_at' => $created_at,
    ];
} else {
    die("Order not found.");
}

mysqli_stmt_close($stmt);


mysqli_close($conn);
ob_end_flush();
?>


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap CSS -->
        <link
            href="https://stackpath.bootstrapcdn.com/bootstrap/5.1.0/css/bootstrap.min.css"
            rel="stylesheet">
        <link
            href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
            rel="stylesheet">
        <!-- Custom styles for the invoice -->

        <style>
        
      body{
            background: url(https://demo.wedigitalindia.com/dcl-erp/assets/images/dcl.png);
            background-size: contain;
            background-position: center;
            background-repeat: repeat;
        }
        
    .col-md-4, .col-md-5, .col-md-3, .col-md-6, .col-md-8, th, td{
        border: 1px solid #000;
    }
    th,td{
        font-size: 13px;
        text-align: center;
    }
    .row{
        padding-left: 12px;
        padding-right: 12px;
    }
    
    @media print {
        .buttons {
            display: none;
        }
    }
    @media screen and (max-width: 760px) {
   .val{
       width: 818px;
   }
}
</style>
    </head>
    <body>

        <div class="container-fluid" style="margin: 30px 0 20px;background-color: #ffffffe0;">
            <div id="buttons" class="buttons">
                <div class="d-flex justify-content-between">
                    <button id="printButton">Print Invoice</button>
                    <button id="goBackButton" onclick="goBack()">Go Back To Website</button>
                </div>
            </div>
                
            <div class="row val" style="margin-top: 20px;">
            
                <h5 class="text-center">TAX INVOICE</h5>
                <div class="col-md-4 col-4"
                    style="border-right: 0; border-bottom: 0;">
                    <h5 style="margin-bottom: 0px;"><b>DHRUV CONTAINER LINE PVT. LTD.</b></h5>
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 3px;">Unit No P4 - 447, Plot No. BGH-A, Paramount Golfmart, SURAJPUR, UPSIDC SITE C, INDUSTRIAL AREA,
                        Greater Noida, Gautam Buddha Nagar, Uttar Pradesh, Pincode - 201306
                         <br></p>
                    <p
                        style="margin-bottom: 3px; font-size: 12px; margin-top: 0;"><b>Email-
                        </b> admin@dhruvcontainer.com &nbsp; Tel: +919457415357</p>
                    <p
                        style="margin-bottom: 3px; font-size: 12px; margin-top: 4px;"><b>GSTIN:
                        </b> 09AAKCD7557L1ZW &emsp; <b>PAN:</b> ACTPYUUE7
                        &emsp; <b>State Code:</b> 06</p>
                </div>
                <div class="col-md-5 col-5"
                    style="border-right: 0; border-bottom: 0;">
         
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>Transport
                            Mode:</b></p>
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>AWB Number:</b></p>
                    <!--<p-->
                    <!--    style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>Date-->
                    <!--        & Time of Supply: </b> 03/07/2024</p>-->
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>Place
                            of Supply: </b>  </p>
                </div>
                <div class="col-md-3 col-3"
                    style="border-right: 0; border-bottom: 0;">
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>Bill
                            No: CSR- </b> &emsp;</p>
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>Order Date:
                             </b> </p>
                </div>
            </div>
            <div class="row val">
                <div class="col-md-4 col-4"
                    style="border-right: 0; border-bottom: 0;">
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 4px;">Details
                        of Receiver (Bill to) &emsp;&emsp;&emsp; </p>
                    <!--<h6 class="text-uppercase" style="margin-bottom: 0;"> Billing Name :  </h6>-->
                    <p  class="text-uppercase"  style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>Billing Name -
                        </b>  <?php  echo $order['client_name']  ?>  &emsp;</p>
                        
                    <p style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>Phone no. -
                    </b>  <?php  echo $order['phone']  ?>&emsp;</p>
                     <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>Company Name -
                        </b>   <?php  echo $order['company_name']  ?>&emsp;</p>
                   
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>GSTIN-
                        </b>  <?php  echo $order['gst']?> &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <b>City:</b> 
                        <?php echo $order['city']?> </p>
                        
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0px;"><b>State:
                        </b> <?php echo $order['state']?>  </p>
                          <p style="margin-bottom: 3px; font-size: 13px; margin-top: 3px;"><b>Full Address : </b><?php echo $address ?>
                        </p>
                </div>
                <div class="col-md-5 col-5"
                    style="border-right: 0; border-bottom: 0;">
                   <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 4px;">Details
                        of Consignee (Ship to) &emsp;&emsp;&emsp;  </p>
                    <!--<h6 class="text-uppercase" style="margin-bottom: 0;"> Billing Name : </h6>-->
                    <p  class="text-uppercase"  style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>Shipping Name -
                        </b>   &emsp;</p>
                    
                    <p style="margin-bottom: 3px; font-size: 13px; margin-top: 0;"><b>Phone no. -
                    </b>   &emsp;</p>
                    
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 3px;"> 
                        </p>
                    
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0px;"><b>State:
                        </b> </p>
                        <p 
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0px;"><b>Pin code:</b>   </p>
                         <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 0px;"></p>
                </div>
                <div class="col-md-3 col-3"
                    style="border-right: 0; border-bottom: 0;">
                   <center>
            <!--<img src="https://demowdi.xyz/lift_up/images/41.png" style="width: 60%; height: 60%;">-->
        </center>
                    
                </div>
            </div>
            
           <table class="table" style="margin-top: 20px;">
        <thead>
            <tr>
                <th>Sr. No.</th>
                <th>Description</th>
                <th>HSN/SAC Code</th>
                <th>Basic Price</th>
                <th>Qty</th>
                <th>Sub Total</th>
                <th>Discount %</th>
                <th>Taxable Value</th>
                <th>GST %</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            
            $product_names_array = explode(',', $order['product_name']);
            $quantities_array = explode(',', $order['quantity']);
            $prices_array = explode(',', $order['price']);
            $sub_totals_array = explode(',', $order['sub_total']);
            $discounts_array = explode(',', $order['discount']);
            $taxes_array = explode(',', $order['total_tax']);
             
            $totalAmount = 0;
            
            ?>
               <?php $srNo = 1; ?>
                <?php for ($i = 0; $i < count($product_names_array); $i++): ?>
                    <tr>
                        <td><?php echo $srNo++; ?></td>
                        <td><?php echo htmlspecialchars($product_names_array[$i]); ?></td>
                        <td><?php echo htmlspecialchars($quantities_array[$i]); ?></td>
                        <td><?php echo number_format($prices_array[$i], 2); ?></td>
                        <td><?php echo htmlspecialchars($quantities_array[$i]); ?></td>
                        <td><?php echo number_format($sub_totals_array[$i], 2); ?></td>
                        <td><?php echo htmlspecialchars($discounts_array[$i]); ?></td>
                        <td><?php echo number_format($sub_totals_array[$i], 2); ?></td>
                        <td><?php echo htmlspecialchars($taxes_array[$i]); ?></td>
                        <td><?php echo number_format($totalAmount += $sub_totals_array[$i], 2); ?></td>
                    </tr>
                <?php endfor; ?>
                <?php $totalAmount += $order['total_amount']; ?>
            <tr>
                <td colspan="9" style="text-align: right;"><strong>Total Amount:</strong></td>
                <td><strong><?php echo number_format($totalAmount, 2); ?></strong></td>
            </tr>
        </tbody>
    </table>

</div>
            <div class="row val">
                <div class="col-md-4 col-4" style="border-right: 0; border-top: 0;">
                    
                </div>
                <div class="col-md-5 col-4" style="border-right: 0; border-top: 0;">
                    <p
                        style="margin-bottom: 3px; font-size: 13px; margin-top: 4px;"><b>Invoice
                            Value (in Words) : </b>  </p>
                            

                    <div class="row" style="padding: 0;">
                        <div class="col-md-6"
                            style="border-left: 0; border-bottom: 0; border-right: 0;">
                            <p
                                style="margin-bottom: 3px; font-size: 13px; margin-top: 4px;"><b>Payment Mode :
                                    </b>  </p>
                            <!--<p-->
                            <!--    style="margin-bottom: 1px; font-size: 13px; margin-top: 4px;">Bank-->
                            <!--    name: </p>-->
                            <!--<p-->
                            <!--    style="margin-bottom: 1px; font-size: 13px; margin-top: 4px;">Address:-->
                            <!--</p>-->
                            <!--<p-->
                            <!--    style="margin-bottom: 1px; font-size: 13px; margin-top: 4px;">Account-->
                            <!--    No: </p>-->
                            <!--<p-->
                            <!--    style="margin-bottom: 1px; font-size: 13px; margin-top: 4px;">IFSC-->
                            <!--    Code: </p>-->
                        </div>
                        <div class="col-md-6"
                            style="border-bottom: 0; border-right: 0;">
                            <!--<p-->
                            <!--    style="margin-bottom: 3px; font-size: 14px; margin-top: 4px;"><b>Freight:-->
                            <!--    </b>&emsp; &emsp; <font-->
                            <!--        style="text-align: right">0.00</font></p>-->
                            <!--<p-->
                            <!--    style="margin-bottom: 3px; font-size: 14px; margin-top: 4px;"><b>Rounded-->
                            <!--        Off: </b>&emsp; &emsp; <font-->
                            <!--        style="text-align: right">0.40</font></p>-->
                            <p
                                style="margin-bottom: 3px; font-size: 14px; margin-top: 4px;"><b>Invoice
                                    Total (Rs.): </b> <font
                                    style="text-align: right; font-weight: bold;"> </font></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-4" style="border-right: 0; border-top: 0;">
                    <p
                        style="margin-bottom: 3px; font-size: 15px; margin-top: 8px;"><b>For
                            Competent System resources</b></p>
                    <p style="height: 60px;"></p>

                    <p
                        style="margin-top: 8px; font-size: 14px; margin-bottom: 3px;"><b>AUTHORISED
                            SIGNATORY</b></p>
                </div>
            </div>
        </div>

        <!-- Bootstrap Bundle with Popper -->
        <script
            src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
            
            <script>
                document.getElementById('printButton').addEventListener('click', function() {
                    window.print();
                });
                
                function goBack() {
                     window.location.href = 'https://demo.wedigitalindia.com/dcl-erp/invoice.php';
                }

            </script>
    </body>
</html>