<?php

require_once './connection.php';


if (!isset($_POST['boat_name']) == "" && !isset($_POST['mobileno']) == "") {

    $boat_name = $_POST['boat_name'];
    $address = $_POST['address'];
    $mobileno = $_POST['mobileno'];
    $rate = $_POST['rate'];

    $query1 = " insert into tbl_boat (boat_owner_name,rate,address,mobile_no) "
            . " values ('$boat_name','$rate','$address','$mobileno') ";
//    echo 'query ' . $query1;
    try {
        if ($con) {

            if (mysqli_query($con, $query1)) {
                $sql = "SELECT * FROM tbl_boat where flag='1' ORDER BY id DESC";
                try {
                    if ($con) {
                        $result = mysqli_query($con, $sql);
                        print_r($result);
                        if (mysqli_num_rows($result) > 0) {
                            $i = 1;
                            $response = '';
                            while ($row = mysqli_fetch_assoc($result)) {
                                $response = $response . '<tr>
                                                        <td scope="row">' . $i . '</td>
                                                        <td>' . $row["boat_owner_name"] . '</td>
                                                            <td>' . $row["rate"] . '</td>
                                                        <td>' . $row["address"] . '</td>
                                                        <td>' . $row["mobile_no"] . '</td>
                                                        <td><a href="JavaScript:Void(0);" class="btn_edit" tabindex="5" id="' . $row["id"] . '"><i class="fa fa-edit"> Edit</i></a><a href="JavaScript:Void(0);" data-toggle="modal" data-target="#exampleModal" class="btn_delete ml-4" tabindex="6" id="' . $row["id"] . '"><i class="fa fa-trash"> Delete</i></a></td>
                                                    </tr>';
                                $i = $i + 1;
                            }
                            echo $response;
                        } else {
                            echo "<tr> <td>0 results</td></tr>";
                        }
                    } else {
                        echo 'Connection Problem';
                    }
                } catch (Exception $ex) {
                    echo $e->getMessage();
                }
            } else {
                echo "Error:01";
            }
        } else {
            echo "Error:01";
        }
    } catch (Exception $ex) {
        echo "Error:01";
    }
}



if (!isset($_POST['boat_id']) == "") {
    $boat_id = $_POST['boat_id'];
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

//    echo 'broker id ' . $_POST['broker_id'];
    $query = 'SELECT * FROM tbl_boat where flag="1" and id="' . $boat_id . '"';
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $i = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            $response = '<div class="form-row">
                                        <div class="col-md-3">
                                            <label for="boat_name" class="">હોડી માલિકનું નામ</label>
                                            <input name="boat_name" id="boat_name" placeholder="હોડી માલિકનું નામ" tabindex="1" value="' . $row['boat_owner_name'] . '" type="text" class="form-control">
                                            <div id="msg_boat_name"></div>
                                        </div>
                                    
                                        <div class="col-md-3">
                                            <label for="rate" class="">1  ટન નો ભાવ</label>
                                            <input name="rate" id="rate" step="any" tabindex="2" value="' . $row['rate'] . '" placeholder="1  ટન નો ભાવ" type="number" class="form-control">
                                            <div id="msg_rate"></div>
                                        </div>
                                        
                                         <div class="col-md-3">
                                            <label for="mobile_no" class="">મોબાઇલ નં</label>
                                            <input name="mobile_no" id="mobile_no" placeholder="મોબાઇલ નંબર" tabindex="3" value="' . $row['mobile_no'] . '" type="text" class="form-control">
                                            <div id="msg_mobile_no"></div>
                                        </div>
                                        
                                        <div class="col-md-3">
                                            <label for="Address" class="">સરનામું</label>
                                            <textarea name="address" id="address" placeholder="સરનામું" tabindex="4" value="' . $row['address'] . '" class="form-control">' . $row['address'] . '</textarea>
                                            <div id="msg_address"></div>
                                        </div>
                                    </div>
                                    <div class="form-row form-group">
                                        <div class="col-md-2">
                                            <br>
                                            <button type="button" style="width: auto!important" class="mt-1 btn btn-primary form-control font-size-lg" tabindex="4" name="' . $row['id'] . '" id="btn_update_boat">Update</button>
                                        </div>
                                    </div>';

            echo $response;
        }
    } else {
        echo "<tr> <td>0 results</td></tr>";
    }
}


if (!isset($_POST['boat_id_update']) == "" && !isset($_POST['boat_name_update']) == "") {

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $boat_id_update = $_POST['boat_id_update'];
    $boat_name_update = $_POST['boat_name_update'];
    $address = $_POST['address'];
    $rate = $_POST['rate'];
    $mobileno = $_POST['mobileno'];
    $sql1 = "UPDATE tbl_boat SET boat_owner_name='" . $boat_name_update . "',rate='" . $rate . "',address='" . $address . "',mobile_no='" . $mobileno . "' WHERE id=" . $boat_id_update . "";



    try {
        if ($con) {
            if (mysqli_query($con, $sql1)) {

                $query = "SELECT * FROM tbl_boat where flag='1' ORDER BY id DESC";
                $result = mysqli_query($con, $query);
//                print_r($result);
                if (mysqli_num_rows($result) > 0) {
                    $i = 1;
                    $response = '';
                    while ($row = mysqli_fetch_assoc($result)) {
                        $response = $response . '<tr>
                                                        <td scope="row">' . $i . '</td>
                                                        <td>' . $row["boat_owner_name"] . '</td>
                                                            <td>' . $row["rate"] . '</td>
                                                        <td>' . $row["address"] . '</td>
                                                        <td>' . $row["mobile_no"] . '</td>
                                                        <td><a href="JavaScript:Void(0);" class="btn_edit" tabindex="5" id="' . $row["id"] . '"><i class="fa fa-edit"> Edit</i></a><a href="JavaScript:Void(0);" data-toggle="modal" data-target="#exampleModal" class="btn_delete ml-4" tabindex="6" id="' . $row["id"] . '"><i class="fa fa-trash"> Delete</i></a></td>
                                                    </tr>';
                        $i = $i + 1;
                    }
                    echo $response;
                } else {
                    echo "<tr> <td>0 results</td></tr>";
                }
            } else {
                echo "Error:01";
            }
        } else {
            echo "Error:01";
        }
    } catch (Exception $ex) {
        echo "Error:01";
    }
}


if (!isset($_POST['boat_id_delete']) == "") {

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    $boat_id_delete = $_POST['boat_id_delete'];

    $sql1 = "UPDATE tbl_boat SET flag='0' WHERE id=" . $boat_id_delete . "";

//    echo 'query ' . $sql1;

    try {
        if ($con) {
            if (mysqli_query($con, $sql1)) {

                $query = "SELECT * FROM tbl_boat where flag='1' ORDER BY id DESC";
                $result = mysqli_query($con, $query);
                print_r($result);
                if (mysqli_num_rows($result) > 0) {
                    $i = 1;
                    $response = '';
                    while ($row = mysqli_fetch_assoc($result)) {
                        $response = $response . '<tr>
                                                        <td scope="row">' . $i . '</td>
                                                        <td>' . $row["boat_owner_name"] . '</td>
                                                             <td>' . $row["rate"] . '</td>
                                                        <td>' . $row["address"] . '</td>
                                                        <td>' . $row["mobile_no"] . '</td>
                                                        <td><a href="JavaScript:Void(0);" class="btn_edit" tabindex="5" id="' . $row["id"] . '"><i class="fa fa-edit"> Edit</i></a><a href="JavaScript:Void(0);" data-toggle="modal" data-target="#exampleModal" class="btn_delete ml-4" tabindex="6" id="' . $row["id"] . '"><i class="fa fa-trash"> Delete</i></a></td>
                                                    </tr>';
                        $i = $i + 1;
                    }
                    echo $response;
                } else {
                    echo "<tr> <td>0 results</td></tr>";
                }
            }
        }
    } catch (Exception $ex) {
        
    }
}


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';

    $select_boat_id = $_POST['select_boat_id'];
    if ($select_boat_id == 'Select' || $select_boat_id == '') {
        $sql = 'select p.*,tbl_boat.boat_owner_name,tbl_boat.rate from tbl_party as p
LEFT JOIN tbl_boat ON p.boat_id=tbl_boat.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_boat.boat_owner_name,tbl_boat.rate from tbl_party as p
LEFT JOIN tbl_boat ON p.boat_id=tbl_boat.id where p.boat_id="' . $select_boat_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }

//    echo 'query '.$sql;

    $boat_owner_name = $_POST['boat_owner_name'];
    if ($boat_owner_name == 'Select') {
        $boat_owner_name = '';
    }
    $response1 = '';
    $response1 = $response1 . '<table id="zero_config" class="mb-0 table-hover table table-striped table-bordered table2excel table2excel_with_colors">    
                                <thead>
                                                <tr>
                                                <th colspan="10">
                                                    <label><b> બીલ તા. ' . date("d-M-Y", strtotime($_POST['start_bill_date'])) . '  થી  ' . date("d-M-Y", strtotime($_POST['end_bill_date'])) . ' સુધી. </b></label>
                                                    <label style="margin-left: 20px"><b> હોડી માલિકનું નામ -  ' . $boat_owner_name . '</b></label>
                                                </th>
                                                </tr>
                                                <tr>
                                                    <th>#</th>
                                                    <th style="font-size: 16px"><b>બીલ તા.</b></th>
                                                    <th style="font-size: 16px"><b>ચલન નં.</b></th>
                                                    <th style="font-size: 16px"><b>હોડી માલિકનું નામ</b></th>
                                                    <th style="font-size: 16px"><b>ગાડી નં.</b></th>
                                                    <th style="font-size: 16px"><b> રોયલ્ટી (ટન) </b></th>
                                                    <th style="font-size: 16px"><b> Cass </b></th>
                                                    <th style="font-size: 16px"><b> Credit </b></th>
                                                    <th style="font-size: 16px">Amount</th>
                                                    <th class="hide_action2" style="font-size: 16px">Action</th>
                                                </tr>
                                            </thead>';

    try {
        if ($con) {
            $result = mysqli_query($con, $sql);
            if (mysqli_num_rows($result) > 0) {
                $i = 1;
                $amount = 0;
                $qty = 0;
                $rate = 0;
                $cass = 0;
                $credit = 0;
                while ($row = mysqli_fetch_assoc($result)) {
//                    $boat_owner_name=$row["boat_owner_name"];
                    $amount = $amount + $row["amount"];
                    $qty = $qty + $row["royalti_mujab_vajan"];
                    $rate = $row["rate"];

                    if ($row["payment_type"] == 'Cass') {
                        $cass = $cass + $row["amount"];
                    } else if ($row["payment_type"] == 'Credit') {
                        $credit = $credit + $row["amount"];
                    }

                    $response1 = $response1 . '<tr>
                        <th scope="row">' . $i . '</th>
                        <td title="બીલ તા.">' . date("d-m-Y", strtotime($row["bill_date"])) . '</td>
                        <td title="ચલન નં."> ' . $row["bill_no"] . '</td>
                        <td title="હોડી માલિકનું નામ">' . $row["boat_owner_name"] . '</td>
                        <td title="ગાડી નં.">' . $row["vehicle_no"] . '</td>
                        <td title="રોયલ્ટી વજન (ટન)">' . $row["quantity"] . '</td>';

                    if ($row["payment_type"] == 'Cass') {

                        $response1 = $response1 . '<td title="Cass">' . $row["amount"] . '</td>
                        <td title="Cass">-</td>';
                    } else if ($row["payment_type"] == 'Credit') {

                        $response1 = $response1 . '<td title="Credit">-</td>
                        <td title="Credit">' . $row["amount"] . '</td>';
                    }
                    $response1 = $response1 . '<td title="Amount">' . $row["amount"] . '</td>
                        <td class="action_hide1"><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></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                        <td style="text-align: right"><b>કુલ</b></td>
                                                        <td><b>' . $qty . '</b></td>
                                                            <td><b>' . $cass . '</b></td>
                                                        <td><b>' . $credit . '</b></td>
                                                        <td><b>' . $amount . '</b></td>
                                                        <td class="action_hide"></td>
                                                    </tr>';

//                echo 'boat'.$boat_owner_name.'boat';
                if ($boat_owner_name == 'Select' || $boat_owner_name == '') {
                    
                } else {

                    $response1 = $response1 . '<tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    
                                                    <td><b>1 ટન નો ભાવ - </b></td>
                                                    <td><b>' . $rate . '</b></td>
                                                    <td><b>' . $rate * $qty . '</b></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td class="hide_action"></td>
                                                </tr>';
                }
            } else {
                echo "0 results";
            }
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
    $response1 = $response1 . '</table>';
    echo $response1;
}


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

    $select_boat_id = $_POST['select_boat_id'];
    if ($select_boat_id == 'Select' || $select_boat_id == '') {
        $sql = 'select p.*,tbl_boat.boat_owner_name,tbl_boat.rate from tbl_party as p
LEFT JOIN tbl_boat ON p.boat_id=tbl_boat.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_boat.boat_owner_name,tbl_boat.rate from tbl_party as p
LEFT JOIN tbl_boat ON p.boat_id=tbl_boat.id where p.boat_id="' . $select_boat_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }

//    echo 'query '.$sql;

    $boat_owner_name = $_POST['boat_owner_name'];
    if ($boat_owner_name == 'Select') {
        $boat_owner_name = '';
    }
    $response1 = '';
    $response1 = $response1 . '<table id="zero_config" class="mb-0 table-hover table table-striped table-bordered table2excel table2excel_with_colors">    
                                <thead>
                                                <tr>
                                                <th colspan="11">
                                                    <label><b> બીલ તા. ' . date("d-M-Y", strtotime($_POST['start_bill_date_full'])) . '  થી  ' . date("d-M-Y", strtotime($_POST['end_bill_date_full'])) . ' સુધી. </b></label>
                                                    <label style="margin-left: 20px"><b> હોડી માલિકનું નામ -  ' . $boat_owner_name . '</b></label>
                                                </th>
                                                </tr>
                                                <tr>
                                                    <th>#</th>
                                                    <th style="font-size: 16px"><b>બીલ તા.</b></th>
                                                    <th style="font-size: 16px"><b>ચલન નં.</b></th>
                                                    <th style="font-size: 16px"><b>હોડી માલિકનું નામ</b></th>
                                                    <th style="font-size: 16px"><b>ગાડી નં.</b></th>
                                                    <th style="font-size: 16px"><b> રોયલ્ટી (ટન) </b></th>
                                                    <th style="font-size: 16px"><b> ખરેખર ભરેલ વજન (ટન) </b></th>
                                                    <th style="font-size: 16px"><b> Cass </b></th>
                                                    <th style="font-size: 16px"><b> Credit </b></th>
                                                    <th style="font-size: 16px">Amount</th>
                                                    <th class="hide_action2" style="font-size: 16px">Action</th>
                                                </tr>
                                            </thead>';

    try {
        if ($con) {
            $result = mysqli_query($con, $sql);
            if (mysqli_num_rows($result) > 0) {
                $i = 1;
                $amount = 0;
                $qty = 0;
                $rate = 0;
                $cass = 0;
                $credit = 0;
                $full_qty = 0;
                while ($row = mysqli_fetch_assoc($result)) {
//                    $boat_owner_name=$row["boat_owner_name"];
                    $amount = $amount + $row["amount"];
                    $qty = $qty + $row["royalti_mujab_vajan"];
                    $rate = $row["rate"];
                    $full_qty = $full_qty + $row["karekhar_barayel_vajan"];

                    if ($row["payment_type"] == 'Cass') {
                        $cass = $cass + $row["amount"];
                    } else if ($row["payment_type"] == 'Credit') {
                        $credit = $credit + $row["amount"];
                    }

                    $response1 = $response1 . '<tr>
                        <th scope="row">' . $i . '</th>
                        <td title="બીલ તા.">' . date("d-m-Y", strtotime($row["bill_date"])) . '</td>
                        <td title="ચલન નં."> ' . $row["bill_no"] . '</td>
                        <td title="હોડી માલિકનું નામ">' . $row["boat_owner_name"] . '</td>
                        <td title="ગાડી નં.">' . $row["vehicle_no"] . '</td>
                        <td title="રોયલ્ટી વજન (ટન)">' . $row["quantity"] . '</td>
                            <td title="ખરેખર ભરેલ વજન (ટન)">' . $row["karekhar_barayel_vajan"] . '</td>';
                    if ($row["payment_type"] == 'Cass') {

                        $response1 = $response1 . '<td title="Cass">' . $row["amount"] . '</td>
                        <td title="Cass">-</td>';
                    } else if ($row["payment_type"] == 'Credit') {

                        $response1 = $response1 . '<td title="Credit">-</td>
                        <td title="Credit">' . $row["amount"] . '</td>';
                    }
                    $response1 = $response1 . '<td title="Amount">' . $row["amount"] . '</td>
                        <td class="action_hide1"><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></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                        <td style="text-align: right"><b>કુલ</b></td>
                                                        <td><b>' . $qty . '</b></td>
                                                            <td><b>' . $full_qty . '</b></td>
                                                                <td><b>' . $cass . '</b></td>
                                                        <td><b>' . $credit . '</b></td>
                                                        <td><b>' . $amount . '</b></td>
                                                        <td class="action_hide"></td>
                                                    </tr>';

//                echo 'boat'.$boat_owner_name.'boat';
                if ($boat_owner_name == 'Select' || $boat_owner_name == '') {
                    
                } else {

                    $response1 = $response1 . '<tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td><b>1 ટન નો ભાવ - </b></td>
                                                    <td><b>' . $rate . '</b></td>
                                                    <td><b>' . $rate * $qty . '</b></td>
                                                        <td><b>' . $rate * $full_qty . '</b></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td class="hide_action"></td>
                                                </tr>';
                }
            } else {
                echo "0 results";
            }
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
    $response1 = $response1 . '</table>';
    echo $response1;
}
?>