<?php
require_once './connection.php';

if (!isset($_POST['name']) == "" && !isset($_POST['net_amount']) == "") {
    date_default_timezone_set('Asia/Kolkata');

    date_default_timezone_set("Asia/Kolkata");
    $start_bill_date = $_POST['start_bill_date'].' '.date("H:i:s");
//    echo 'start_bill_date ' . $start_bill_date . ' ' . date("h:i:s");

    $date_time = $start_bill_date . ' ' . date("h:i:s");
//    echo 'start_bill_date ' . $date_time;

    $bill_no = $_POST['bill_no'];
    $name = $_POST['name'];
    $vehicle_no = $_POST['vehicle_no'];

    $address = $_POST['address'];
    $select_state_code = $_POST['select_state_code'];
    $gstin = $_POST['gstin'];

    $qty = $_POST['qty'];
    $rate = $_POST['rate'];
    $amount = $_POST['amount'];

    $cgst = $_POST['cgst'];
    $sgst = $_POST['sgst'];
    $igst = $_POST['igst_val'];
    $net_amount = $_POST['net_amount'];

    $query1 = " insert into tbl_party_bill_print (vehicle_no,name, state_code, address,gstin, bill_no, qty, rate,amount, cgst, sgst, igst,net_amount, created_date) "
            . " values ('$vehicle_no','$name','$select_state_code','$address','$gstin','$bill_no','$qty','$rate','$amount','$cgst','$sgst','$igst','$net_amount','$date_time') ";
//    echo 'query ' . $query1;
    try {
        if ($con) {
            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_bill_print where id=" . $last_id . " and flag='1'";
//                echo '$sql ' . $sql;
                try {
                    if ($con) {
                        $result = mysqli_query($con, $sql);
                        if ($result) {
                            $row = mysqli_fetch_assoc($result);
                            ?>

                            <table id="zero_config" class="mb-0 table-hover table table-striped table-bordered table2excel table2excel_with_colors">

                                <tbody id="table_body" style="border: 2px solid black;">
                                    <tr>
                                        <td class="td1" colspan="6"><span style="float: right;padding-right: 20px">Mo. 9879316313,9586009009</span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="6"><span style="text-align: center;display: block;font-size: 24px"><b>PATEL TRADING</b></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="6"><span style="text-align: center;display: block;font-size: 14px">Station Road, Vyara, Tapi Gujarat-394650</span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="4"><span class="form-control" style="text-align: center;display: block;"><b>GST IN : </b>24DOIPP2661M1ZI</span></td>
                                        <td class="td1" colspan="2"><span class="form-control" style="text-align: center;display: block;"><b>Tax Invoice</b></span></td>
                                    </tr>

                                    <tr style="border-top: 2px solid #000">
                                        <td class="td1" colspan="4"><span style="text-align: left;display: block;margin-left: 30px"><b>Vehicle No - </b>&nbsp;<?php echo $row['vehicle_no']; ?></span></td>
                                        <td class="td1" colspan="2"><span style="text-align: center;display: block;"><b>Cass/Credit</b></span></td>
                                    </tr>

                                    <tr>
                                        <td class="td1" colspan="4"><span style="text-align: left;display: block;margin-left: 30px"><b>M/s. - </b>&nbsp;<?php echo $row['name']; ?></span></td>
                                        <td class="td1" colspan="2"><span style="text-align: center;display: block;"><b>Bill No - </b>&nbsp;<?php echo $row['bill_no']; ?></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="4"><span style="text-align: left;display: block;margin-left: 30px"><b>Address - </b>&nbsp;<?php echo $row['address']; ?></span></td>
                                        <td class="td1" colspan="2"><span style="text-align: center;display: block;"><b>Bill Date - </b> &nbsp;<?php echo date("d-M-Y", strtotime($row["created_date"])); ?></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="4"><span style="text-align: left;display: block;margin-left: 30px"><b>State-Code - </b> &nbsp;<?php echo $row['state_code']; ?> , <b>GSTIN - </b> &nbsp;<?php echo $row['gstin']; ?> </span></td>
                                        <td class="td1" colspan="2"><span style="text-align: center;display: block;"></span></td>
                                    </tr>

                                    <tr style="border-top: 2px solid black">
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Sr no.</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Production</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>HSN Code</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Qty</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Rate</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Amount</b></span></td>
                                    </tr>

                                    <tr style="border-top: 2px solid black">
                                        <td class="td1" ><span style="text-align: center;display: block;margin-left: 30px">1</span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">Natural Send (River Send)</span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">2505</span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['qty']; ?></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['rate']; ?></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['amount']; ?></span></td>
                                    </tr>

                                    <tr>
                                        <td class="td1" colspan="6"></td>
                                    </tr>
                                    <tr>
                                        <td class="td1" colspan="6"></td>
                                    </tr>
                                    <tr>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Total</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['amount']; ?></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>CGST(%)</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['cgst']; ?></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>SGST(%)</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['sgst']; ?></span></td>
                                    </tr>
                                    <tr>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>IGST(%)</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px">&nbsp;<?php echo $row['igst']; ?></span></td>
                                    </tr>

                                    <tr style="border-top: 2px solid black">
                                        <td class="td1" colspan="4"><span style="text-align: center;display: block">
                                                <b> In Words : &nbsp;</b>
                                                <?php
                                                $number = round($row['net_amount']);
                                                $no = floor($number);
                                                $point = round($number - $no, 2) * 100;
                                                $hundred = null;
                                                $digits_1 = strlen($no);
                                                $i = 0;
                                                $str = array();
                                                $words = array('0' => '', '1' => 'one', '2' => 'two',
                                                    '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
                                                    '7' => 'seven', '8' => 'eight', '9' => 'nine',
                                                    '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
                                                    '13' => 'thirteen', '14' => 'fourteen',
                                                    '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
                                                    '18' => 'eighteen', '19' => 'nineteen', '20' => 'twenty',
                                                    '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
                                                    '60' => 'sixty', '70' => 'seventy',
                                                    '80' => 'eighty', '90' => 'ninety');
                                                $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
                                                while ($i < $digits_1) {
                                                    $divider = ($i == 2) ? 10 : 100;
                                                    $number = floor($no % $divider);
                                                    $no = floor($no / $divider);
                                                    $i += ($divider == 10) ? 1 : 2;
                                                    if ($number) {
                                                        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
                                                        $hundred = ($counter == 1 && $str[0]) ? ' ' : null;
                                                        $str [] = ($number < 21) ? $words[$number] .
                                                                " " . $digits[$counter] . $plural . " " . $hundred :
                                                                $words[floor($number / 10) * 10]
                                                                . " " . $words[$number % 10] . " "
                                                                . $digits[$counter] . $plural . " " . $hundred;
                                                    } else
                                                        $str[] = null;
                                                }
                                                $str = array_reverse($str);
                                                $result = implode('', $str);
                                                $points = ($point) ?
                                                        "." . $words[$point / 10] . " " .
                                                        $words[$point = $point % 10] : '';
                                                if ($points == '') {
                                                    echo $result . "Rupees  " . " Only";
                                                } else {
                                                    echo $result . "Rupees  " . $points . " Paise Only";
                                                }
                                                ?>

                                            </span>
                                        </td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>Net_Amount</b></span></td>
                                        <td class="td1"><span style="text-align: center;display: block;margin-left: 30px"><b>&nbsp;<?php echo round($row['net_amount']).".00" ?></b></span></td>
                                    </tr>

                                    <tr>
                                        <td colspan="2" class="td1"><span style="text-align: left;display: block;margin-left: 10px"><b>Subject to Surat Jurisdiction</b></span></td>
                                        <td colspan="4" class="td1"><span style="text-align: right;display: block;margin-right: 30px"><b>For,PATEL TRADING</b></span></td>
                                    </tr>

                                    <tr>
                                        <td colspan="6"></td>
                                    </tr>

                                    <tr>
                                        <td colspan="6"><span style="text-align: left;display: block;margin-left: 10px"><b>E. &. O. F.</b></span></td>
                                    </tr>
                                </tbody>
                            </table>

                            <?php
                        }
                    } 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['vehicle_no_search_by_vehicle_no']) == "") {
    $vehicle_no = $_POST['vehicle_no_search_by_vehicle_no'];
    $bill_no=$_POST['bill_no'];

    $sql = 'select * from tbl_party_bill_print where vehicle_no="' . $vehicle_no . '" and flag="1" ORDER by id DESC';
//    echo 'query '.$sql;
    try {
        if ($con) {
            $result = mysqli_query($con, $sql);
            if (mysqli_num_rows($result) > 0) {

                while ($row = mysqli_fetch_assoc($result)) {
                    $response1 = '<div class="form-row">
                        
                                        <div class="col-md-3">
                                                <label for="mobile_no" class="col1">Bill No</label>
                                                <input name="Bill No" step="any" id="bill_no" disabled="true" value="'.$bill_no.'" placeholder="Bill No" type="number" class="form-control">
                                                <div id="msg_bill_no"></div>
                                            </div>

                                            <div class="col-md-3">
                                                <label for="vehicle_no" class="col1">ગાડી નં.</label>
                                                <input name="vehicles" id="vehicle_no" list="vehicles" value="' . $row['vehicle_no'] . '" placeholder="ગાડી નં." tabindex="4" type="text" class="form-control">
                                                <datalist id="vehicles">';

                    $query = 'select DISTINCT vehicle_no from  tbl_party_bill_print where flag="1"';
                    $result = mysqli_query($con, $query);
                    if (mysqli_num_rows($result) > 0) {
                        while ($row_no = mysqli_fetch_assoc($result)) {

                            if ($vehicle_no == $row_no["vehicle_no"]) {
                                $response1 = $response1 . '<option name="' . $row_no["vehicle_no"] . '" selected>' . $row_no["vehicle_no"] . '</option>';
                            } else {
                                $response1 = $response1 . '<option name="' . $row_no["vehicle_no"] . '">' . $row_no["vehicle_no"] . '</option>';
                            }
                        }
                    } else {
                        
                    }
                    $month = date('m');
                $day = date('d');
                $year = date('Y');

                $today = $year . '-' . $month . '-' . $day;
                    $response1 = $response1 . '</datalist>
                    <div id="msg_vehicle_no"></div>
                                            </div>
                                            
                                            
                                            <div class="col-md-3">
                                                <label for="vehicle_Owner_Name" class="col1"> બીલ તા. </label>
                                                <input name="Bill Date" id="bill_date" value="' . $today . '" max="<?php echo $today; ?>" tabindex="1" placeholder="Bill Date" type="date" class="form-control mr-sm-4">
                                                <div id="msg_bill_date"></div>
                                            </div>
                                            
                                            <div class="col-md-3">
                                                <label for="Name" class="col1"> માલ લેનારનું નામ</label>
                                                <input name="Name" id="name" tabindex="2" value="' . $row['name'] . '" placeholder="માલ લેનારનું નામ" type="text" class="form-control">
                                                <div id="msg_name"></div>
                                            </div>
                                            
                                            <div class="col-md-3">
                                                <label for="state-code" class="col1">State-Code</label>
                                                <select name="select" id="select-state-code" tabindex="8" class="form-control">
                                                    <option name="Select"> Select </option>';

                    if ($row['state_code'] == 'GJ-24') {
                        $response1 = $response1 . '<option name="GJ-24" selected> Gujarat (GJ-24) </option>
                                                    <option name="MH-27"> Maharashtra (MH-27) </option>
                                                    <option name="MP-23"> Madhya-Pradesh (MP-23) </option>';
                    } else if ($row['state_code'] == 'MH-27') {
                        $response1 = $response1 . '<option name="GJ-24"> Gujarat (GJ-24) </option>
                                                    <option name="MH-27" selected> Maharashtra (MH-27) </option>
                                                    <option name="MP-23"> Madhya-Pradesh (MP-23) </option>';
                    } else if ($row['state_code'] == 'MP-23') {
                        $response1 = $response1 . '<option name="GJ-24"> Gujarat (GJ-24) </option>
                                                    <option name="MH-27"> Maharashtra (MH-27) </option>
                                                    <option name="MP-23" selected> Madhya-Pradesh (MP-23) </option>';
                    }
                    $response1 = $response1 . '</select>
                                                <div id="msg_select_state_code"></div>
                                            </div> 
                                            
                                            <div class="col-md-3">
                                                <label for="Address" class="col1">સરનામું</label>
                                                <textarea name="address" id="address"  placeholder="સરનામું" tabindex="7" class="form-control">' . $row['address'] . '</textarea>
                                                <div id="msg_address"></div>
                                            </div>
                                            
                                            <div class="col-md-2">
                                                <label for="cust_name" class="col1">GSTIN</label>
                                                <input name="GSTIN" id="gstin" tabindex="5" value="' . $row['gstin'] . '" placeholder="GSTIN" type="text" class="form-control">
                                                <div id="msg_cust_gstin"></div>
                                            </div>
                                            
                                            
                                            
                                            <div class="col-md-2">
                                                <label for="qty" class="col1">Qty</label>
                                                <input name="qty" step="any" id="qty" tabindex="7" value="' . $row['qty'] . '" placeholder="Qty" type="number" class="form-control">
                                                <div id="msg_qty"></div>
                                            </div>

                                            <div class="col-md-2">
                                                <label for="qty" class="col1">Rate</label>
                                                <input name="rate" step="any" id="rate" tabindex="8" value="' . $row['rate'] . '" placeholder="rate" type="number" class="form-control">
                                                <div id="msg_rate"></div>
                                            </div>
                                        </div>
                                        <div class="form-row form-group">
                                                            <div class="col-md-2">
                                                                <label for="qty" class="col1">Amount</label>
                                                                <input name="amount" disabled="" step="any" id="amount" value="' . $row['amount'] . '" tabindex="9" placeholder="Amount" type="number" class="form-control">
                                                                <div id="msg_amount"></div>
                                                            </div>

                                                            <div class="col-md-2">
                                                                <label for="cgst" class="col1">CGST (2.5%)</label>
                                                                <input name="cgst" disabled="" step="any" id="cgst" tabindex="10" value="' . $row['cgst'] . '" placeholder="CGST" type="number" class="form-control">
                                                                <div id="msg_cgst"></div>
                                                            </div>

                                                            <div class="col-md-2">
                                                                <label for="sgst" class="col1">Sgst (2.5%)</label>
                                                                <input name="sgst" disabled="" step="any" id="sgst" tabindex="11" value="' . $row['sgst'] . '" placeholder="Sgst" type="number" class="form-control">
                                                                <div id="msg_sgst"></div>
                                                            </div>

                                                            <div class="col-md-2">
                                                                <label for="igst" class="col1">IGST (5%)</label>
                                                                <input name="igst" step="igst" disabled="" id="igst" tabindex="12" value="' . $row['igst'] . '" placeholder="IGST" type="number" class="form-control">
                                                                <div id="msg_igst"></div>
                                                            </div>

                                                            <div class="col-md-2">
                                                                <label for="igst" class="col1">Net Amount</label>
                                                                <input name="net_amount" disabled="" step="any" id="net_amount" tabindex="13" value="' . $row['net_amount'] . '" placeholder="Net Amount" type="number" class="form-control">
                                                                <div id="msg_net_amount"></div>
                                                            </div>
                                                            
                                                            <div class="col-md-2">
                                                                <br>
                                                                <button type="button" class="mt-2 btn btn-primary form-control font-size-lg" tabindex="11" style="font-size: 20px !important;padding: 0px;" id="btn_generate_bill">Generate Bill</button>
                                                            </div>
                                                        </div>';
                }

                echo $response1;
            } else {
                echo 'No Record Found';
            }
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
}
?>