<?php

require_once './connection.php';


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

    $comp_name = $_POST['price'];

    $query1 = " insert into tbl_milk_rate (price) "
            . " values ('$comp_name') ";
//    echo 'query ' . $query1;
    try {
        if ($con) {

            if (mysqli_query($con, $query1)) {
                $sql = "SELECT * FROM tbl_milk_rate 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["price"] . '</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['company_id']) == "") {
    $company_id = $_POST['company_id'];
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

//    echo 'broker id ' . $_POST['broker_id'];
    $query = 'SELECT * FROM tbl_milk_rate where flag="1" and id="' . $company_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-4">
                                            <label for="comp_name" class="">કંપનીનું નામ</label>
                                            <input name="comp_name" id="comp_name" placeholder="કંપનીનું નામ" tabindex="1" value="' . $row['price'] . '" type="text" class="form-control">
                                            <div id="msg_comp_name"></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_company">Update</button>
                                        </div>
                                    </div>';

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


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

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $company_id_update = $_POST['company_id_update'];
    $company_name_update = $_POST['company_name_update'];
    $sql1 = "UPDATE tbl_milk_rate SET price='" . $company_name_update . "' WHERE id=" . $company_id_update . "";



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

                $query = "SELECT * FROM tbl_milk_rate 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["price"] . '</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['company_id_delete']) == "") {

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

    $company_id_delete = $_POST['company_id_delete'];

    $sql1 = "UPDATE tbl_company SET flag='0' WHERE id=" . $company_id_delete . "";

//    echo 'query ' . $sql1;

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

                $query = "SELECT * FROM tbl_company 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["comp_name"] . '</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_company_id = $_POST['select_company_id'];
    if ($select_company_id == 'Select' || $select_company_id == '') {
        $sql = 'select p.*,tbl_company.comp_name from tbl_party as p LEFT JOIN tbl_company ON p.company_id=tbl_company.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_company.comp_name from tbl_party as p LEFT JOIN tbl_company ON p.company_id=tbl_company.id where p.company_id="' . $select_company_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }
//    echo 'Query '.$sql;

    $select_company_name = $_POST['select_company_name'];
    if ($select_company_name == 'Select' || $select_company_name == '') {
        $select_company_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> કંપનીનું નામ -  ' . $select_company_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;
                $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"];

                    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["comp_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>';
            } 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_company_id = $_POST['select_company_id'];
    if ($select_company_id == 'Select' || $select_company_id == '') {
        $sql = 'select p.*,tbl_company.comp_name from tbl_party as p LEFT JOIN tbl_company ON p.company_id=tbl_company.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_company.comp_name from tbl_party as p LEFT JOIN tbl_company ON p.company_id=tbl_company.id where p.company_id="' . $select_company_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }
//    echo 'Query '.$sql;

    $select_company_name = $_POST['select_company_name'];
    if ($select_company_name == 'Select' || $select_company_name == '') {
        $select_company_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> કંપનીનું નામ -  ' . $select_company_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;
                $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"];
                    $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["comp_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>';
            } else {
                echo "0 results";
            }
        } else {
            echo 'Connection Problem';
        }
    } catch (Exception $ex) {
        echo $e->getMessage();
    }
    $response1 = $response1 . '</table>';
    echo $response1;
}
?>