
<?php

require_once './connection.php';

if (!isset($_POST['vehicle_owner_name']) == "" && !isset($_POST['total_amount']) == "") {

    $sql = "SELECT bill_no FROM tbl_party ORDER BY id DESC LIMIT 1";
    $bill_no = 1;
    try {
        if ($con) {
            $result = mysqli_query($con, $sql);
            $row = mysqli_fetch_assoc($result);
            $bil_no = $row["bill_no"];
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }

//    echo 'bill_no '.$bill_no;

    $bill_no = $bil_no + $bill_no;

    $vehicle_owner_name = $_POST['vehicle_owner_name'];
    $vehicle_no = $_POST['vehicle_no'];
    $address = $_POST['address'];
    $select_state_code = $_POST['select_state_code'];
    $select_place_of_supply = $_POST['select_place_of_supply'];
    $select_product_name = $_POST['select_product_name'];
    $hsn_code = $_POST['hsn_code'];
    $unit_price = $_POST['unit_price'];
    $quantity = $_POST['quantity'];
    $cgst = $_POST['cgst'];
    $sgst = $_POST['sgst'];
    $igst = $_POST['igst'];

    $total_amount = round($_POST['total_amount']);

    $query1 = " insert into tbl_party (bill_no,vehicle_owner_name,vehicle_no,address,state_code,product_name,hsn_code,quantity,unit_price,CGST,SGST,IGST,amount,place_of_supply) "
            . " values ('$bill_no','$vehicle_owner_name','$vehicle_no','$address','$select_state_code','$select_product_name','$hsn_code','$quantity','$unit_price','$cgst','$sgst','$igst','$total_amount','$select_place_of_supply') ";
//    echo 'query ' . $query1;
    try {
        if ($con) {
//            mysqli_query($con, $query1);
//            echo 'Insert';
//        } else {
//            echo 'Connection Problem';
//        }
            if (mysqli_query($con, $query1)) {
                $last_id = mysqli_insert_id($con);
//                echo "New record created successfully. Last inserted ID is: " . $last_id;
//            $last_id = 1;
                $sql = "SELECT * FROM tbl_party where id=" . $last_id;
//                $sql = "SELECT * FROM tbl_party ORDER BY id DESC LIMIT 1";
                try {
                    if ($con) {
                        $result = mysqli_query($con, $sql);
                        if ($result) {
                            $row = mysqli_fetch_assoc($result);
                            $total_tax = $row["CGST"] + $row["SGST"] + $row["IGST"];
                            $total_amount = round($row["quantity"] * $row["unit_price"] + $total_tax);
//                        $flt=$row["quantity"] * $row["unit_price"] + $total_tax;
//                        $dif=$total_amount-$flt.'0';
//                        echo 'dif '.$dif;

                            $response = '<div class="form-row">
                <table class="mb-0 table" style="line-height: 2px !important">
                    <tbody>
                        <tr>
                            <td style="border-top: 0px"> <b><label>Original for Recipient</b></label></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px;font-size:20px">
                                <b><label>INVOICE &nbsp;' . $row["bill_no"] . '</label></b>
                            </td>

                        </tr>
                        <tr>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"><b><label>Date -&nbsp;' . date("d F Y", strtotime($row["bill_date"])) . '</label></b></td>

                        </tr>
                    </tbody>
                </table>
            </div>
            <hr style="border: 0.5px solid black;">

            <div class="form-row">
                <table class="mb-0 table" style="line-height: 0px !important">
                    <tbody>
                        <tr>
                            <td style="border-top: 0px;white-space: pre"><b><label>BHARATSINH RAMANSINH CHAVDA</label></b></td>
                            <td style="border-top: 0px"><b><label>Bill To :</label></b></td>

                        </tr>
                        <tr>
                            <td style="border-top: 0px"><i class="fa fa-home"></i><label>&nbsp; 43/1/70, Patel faliyu, Bhada, Surat,</label></td>
                            <td style="border-top: 0px"><label> ' . $row["vehicle_owner_name"] . ' </label></td>
                            <td style="border-top: 0px"></td>

                        </tr>
                        <tr>
                            <td style="border-top: 0px"> <label> Gujarat, 395008 , India </label></td>
                            <td style="border-top: 0px"><label><i class="fa fa-truck"></i> ' . $row["vehicle_no"] . ' </label></td>
                        </tr>

                        <tr>
                            <td style="border-top: 0px"> <b><label>GSTIN - 24AHFPC7871D1ZL </label></b></td>
                            <td style="border-top: 0px;white-space: pre"><label><i class="fa fa-home"></i> ' . $row["address"] . ' </label></td>
                        </tr>
                        <tr>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"><label> <label> ' . $row["state_code"] . ' ,' . $row["country"] . '</label></label></td>
                        </tr>
                        <tr>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"><label><i class="fa fa-info-circle"></i> Place of Supply - &nbsp;' . $row["place_of_supply"] . ' </label></td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="form-row">
                <table class="mb-0 table">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Product/Service Name</th>
                            <th>HSN/SAC</th>
                            <th>Qty</th>
                            <th>Unit Price</th>
                            <th>CGST</th>
                            <th>SGST</th>
                            <th>IGST</th>
                            <th>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td scope="row"  style="border-top: 0px">1</td>
                            <td>' . $row["product_name"] . '</td>
                            <td>' . $row["hsn_code"] . '</td>
                            <td>' . $row["quantity"] . '</td>
                            <td>' . $row["unit_price"] . '</td>
                            <td>' . $row["CGST"] . '</td>
                            <td>' . $row["SGST"] . '</td>
                            <td>' . $row["IGST"] . '</td>
                            <td>' . $row["amount"] . '</td>

                        </tr>
                        <tr>
                            <td  style="border-top: 0px"></td>
                        </tr>
                        <tr>
                            <td  style="border-top: 0px"></td>
                        </tr>
                        <tr>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;"></td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;"></td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;"> Total </td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["quantity"] . '</td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["quantity"] * $row["unit_price"] . '</td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["CGST"] . '</td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["SGST"] . '</td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["IGST"] . '</td>
                            <td style="border-bottom: 1px solid black;border-top: 1px solid black;">' . $row["amount"] . '</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td> </td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="form-row form-group">

                <table class="mb-0 table" style="line-height: 0px !important">
                    <tbody>
                        <tr>
                            <td style="border-top: 0px"><b><label>AUTHORISED SIGNATORY</label></b></td>
                            <td style="border-top: 0px"><b><label>Total Before Tax &nbsp;</label></b></td>
                            <td style="border-top: 0px"><b><label><i class="fas fa-rupee-sign" aria-hidden="true"></i>&nbsp; ' . $row["quantity"] * $row["unit_price"] . '</label></b></td>
                        </tr>
                        
                        <tr>
                            <td style="border-top: 0px"></td>
                            <td style="border-top: 0px"><b><label>Total Tax Amount &nbsp;</label></b></td>
                            <td style="border-top: 0px"><b><label><i class="fas fa-rupee-sign" aria-hidden="true"></i>&nbsp; ' . $total_tax . '</label></b></td>
                        </tr>

                        
                        <tr>
                            <td style="border-top: 0px">________________________________</td>
                            <td style="border-top: 0px"><b><label>TOTAL AMOUNT RECEIVED &nbsp; </label></b></td>
                            <td style="border-top: 0px"><b><label><i class="fas fa-rupee-sign" aria-hidden="true"></i>&nbsp; ' . $total_amount . '</label></b></td>
                        </tr>
                    </tbody>
                </table>
            </div>';

                            echo $response . '<hr style="border: 0.5px solid black;margin-bottom: 0px;"><span style="margin-left: 355px;">Second Copy</span>' . $response;
                        }
                    } else {
                        echo 'Connection Problem';
                    }
                } catch (Exception $ex) {
                    echo $e->getMessage();
                }
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($con);
            }
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
}


if (!isset($_POST['start_bill_date']) == "" && !isset($_POST['end_bill_date']) == "") {
    $start_bill_date = $_POST['start_bill_date'];
    $start_bill_date = $start_bill_date . ' 00:00:05';
    $end_bill_date = $_POST['end_bill_date'];
    $end_bill_date = $end_bill_date . ' 23:50:05';

    $sql = 'select * from tbl_party where bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and flag="1"';
    try {
        if ($con) {
            $result = mysqli_query($con, $sql);
//            print_r($result);
//            if ($result) {
//                $row = mysqli_fetch_assoc($result);
//                $total_tax = $row["CGST"] + $row["SGST"] + $row["IGST"];
//                $total_amount = round($row["quantity"] * $row["unit_price"] + $total_tax);
//            }
            if (mysqli_num_rows($result) > 0) {
                $i = 1;
                $response1 = '';
                $unit_price = 0;
                $cgst = 0;
                $sgst = 0;
                $igst = 0;
                $amount = 0;
                while ($row = mysqli_fetch_assoc($result)) {
                    $amount = $amount + $row["amount"];
                    $cgst = $cgst + $row["CGST"];
                    $sgst = $sgst + $row["SGST"];
                    $igst = $igst + $row["IGST"];
                    $unit_price = $unit_price + $row["quantity"] * $row["unit_price"];

                    $response1 = $response1 . '<tr>
                        <th scope="row">' . $i . '</th>
                        <td>' . date("d-m-Y", strtotime($row["bill_date"])) . '</td>
                        <td> INVOICE ' . $row["bill_no"] . '</td>
                        <td>' . $row["vehicle_no"] . '</td>
                        <td>' . $row["state_code"] . '</td>
                        <td>' . $row["quantity"] . '</td>
                        <td>' . $row["unit_price"] . '</td>
                        <td>' . $row["CGST"] . '</td>
                        <td>' . $row["SGST"] . '</td>
                        <td>' . $row["IGST"] . '</td>
                        <td>' . $row["amount"] . '</td>
                        <td><a href="JavaScript:Void(0);" class="btn_print_view" id="' . $row["id"] . '"><i class="fa fa-print"> View</i></a></td>
                            </tr>';


                    $i = $i + 1;
                }
                $response1 = $response1 . '<tr>
                                                        <td colspan="5" style="text-align: right"><b>Total</b></td>
                                                        <td></td>
                                                        <td><b>' . $unit_price . '</b></td>
                                                        <td><b>' . $cgst . '</b></td>
                                                        <td><b>' . $sgst . '</b></td>
                                                        <td><b>' . $igst . '</b></td>
                                                        <td><b>' . $amount . '</b></td>
                                                        <td></td>
                                                    </tr>';
                echo $response1;
            } else {
                echo "0 results";
            }
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
}

if (!isset($_POST['leez_id']) == "") {
    $leez_id = $_POST['leez_id'];
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION)) {
        $_SESSION['leez_id'] = $leez_id;
    } else {
        $_SESSION['leez_id'] = $leez_id;
    }
//    echo 'leez id ' . $_SESSION['leez_id'];
}
?>

