<?php

require_once './connection.php';

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

    $broker_name = $_POST['cust_name'];
    $address = $_POST['address'];
    $mobileno = $_POST['mobileno'];
    $buffalo_litre = $_POST['buffalo_litre'];
    $cow_litre = $_POST['cow_litre'];
    $milk_take_time = $_POST['milk_take_time'];

    $query1 = " insert into tbl_customer (cust_name,mobile_no,address,buffelo_milk,cow_milk,milk_take_time) "
            . " values ('$broker_name','$mobileno','$address','$buffalo_litre','$cow_litre','$milk_take_time')";
//    echo 'query ' . $query1;
    try {
        if ($con) {
            if (mysqli_query($con, $query1)) {
                $sql = "SELECT * FROM tbl_customer where flag='1' ORDER BY id ASC";
                try {
                    if ($con) {
                        mysqli_set_charset($con, 'utf8');
                        $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["cust_name"] . '</td>
                                                        <td>' . $row["mobile_no"] . '</td>
                                                        <td>' . $row["address"] . '</td>
                                                            <td>' . $row["buffelo_milk"] . '</td>
                                                                <td>' . $row["cow_milk"] . '</td>
                                                                    <td>' . $row["milk_take_time"] . '</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:02";
        }
    } catch (Exception $ex) {
        echo "Error:03" . $ex;
    }
}


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

//    echo 'broker id ' . $_POST['broker_id'];
    $query = 'SELECT * FROM tbl_customer where flag="1" and id="' . $cust_id . '"';
    mysqli_set_charset($con, 'utf8');
    $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="broker_name" class="">ગ્રાહકનું નામ</label>
                                            <input name="broker_name" id="broker_name" placeholder="ગ્રાહકનું નામ" tabindex="1" value="' . $row['cust_name'] . '" type="text" class="form-control">
                                            <div id="msg_broker_name"></div>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="mobile_no" class="">મોબાઇલ નંબર</label>
                                            <input name="mobile_no" id="mobile_no" placeholder="મોબાઇલ નંબર" tabindex="2" value="' . $row['mobile_no'] . '" type="text" class="form-control">
                                            <div id="msg_mobile_no"></div>
                                        </div>
                                            
                                        <div class="col-md-4">
                                            <label for="Address" class="">સરનામું</label>
                                            <textarea name="address" id="address" placeholder="સરનામું" tabindex="3" value="' . $row['address'] . '" class="form-control">' . $row['address'] . '</textarea>
                                            <div id="msg_address"></div>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="buffalo_litre" class="">Buffalo Milk Litre</label>
                                            <input name="buffalo_litre" id="buffalo_litre" placeholder="Buffalo Milk Litre" value="' . $row['buffelo_milk'] . '" tabindex="4" type="text" class="form-control">
                                            <div id="msg_buffalo_litre"></div>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="cow_litre" class="">Cow Milk Litre</label>
                                            <input name="cow_litre" id="cow_litre" placeholder="Cow Milk Litre" value="' . $row['cow_milk'] . '" tabindex="5" type="text" class="form-control">
                                            <div id="msg_cow_litre"></div>
                                        </div>
                                        
                                        <div class="col-md-4">
                                            <label for="companay_name" class="col1">દુધ લેવાનો સમય</label>
                                            <select name="select" id="milk_time" tabindex="6" class="form-control">';

            if ($row['milk_take_time'] == "MORNING") {

                $response = $response . '<option name="MORNING" selected="" id="MORNING"> MORNING </option>
                <option name="EVENING" id="EVENING"> EVENING </option>
                <option name="MORNING & EVENING" id="MORNING/EVENING"> MORNING & EVENING </option>';
            } else if ($row['milk_take_time'] == "EVENING") {

                $response = $response . '<option name="MORNING" id="MORNING"> MORNING </option>
                <option name="EVENING"  selected="" id="EVENING"> EVENING </option>
                <option name="MORNING & EVENING" id="MORNING/EVENING"> MORNING & EVENING </option>';
            } else if ($row['milk_take_time'] == "MORNING & EVENING") {

                $response = $response . '<option name="MORNING" id="MORNING"> MORNING </option>
                <option name="EVENING" id="EVENING"> EVENING </option>
                <option name="MORNING & EVENING"  selected="" id="MORNING/EVENING"> MORNING & EVENING </option>';
            } else {

                $response = $response . '<option name="MORNING" id="MORNING"> MORNING </option>
                <option name="EVENING" id="EVENING"> EVENING </option>
                <option name="MORNING & EVENING" id="MORNING/EVENING"> MORNING & EVENING </option>';
            }

            $response = $response . '</select>
<div id="msg_milk_time"></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="7" name="' . $row['id'] . '" id="btn_update_customer">Update</button>
    </div>
</div>';

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


if (!isset($_POST['cust_id_update']) == "" && !isset($_POST['cust_name_update']) == "") {

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

    $broker_id_update = $_POST['cust_id_update'];
    $broker_name = $_POST['cust_name_update'];
    $address = $_POST['address'];
    $mobileno = $_POST['mobileno'];
    $buffalo_litre = $_POST['buffalo_litre'];
    $cow_litre = $_POST['cow_litre'];
    $milk_take_time = $_POST['milk_take_time'];

    $sql1 = "UPDATE tbl_customer SET cust_name='" . $broker_name . "',address='" . $address . "',mobile_no='" . $mobileno . "',buffelo_milk='" . $buffalo_litre . "',cow_milk='" . $cow_litre . "',milk_take_time='" . $milk_take_time . "' WHERE id=" . $broker_id_update . "";

//    echo 'query ' . $sql1;

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

                $query = "SELECT * FROM tbl_customer where flag='1' ORDER BY id ASC";
                mysqli_set_charset($con, 'utf8');
                $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["cust_name"] . '</td>
    <td>' . $row["mobile_no"] . '</td>
    <td>' . $row["address"] . '</td>
    <td>' . $row["buffelo_milk"] . '</td>
    <td>' . $row["cow_milk"] . '</td>
    <td>' . $row["milk_take_time"] . '</td>
    <td><a href="JavaScript:Void(0);" class="btn_edit" tabindex="8" 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="9" 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['cust_id_delete']) == "") {

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

    $broker_id_delete = $_POST['cust_id_delete'];

    $sql1 = "UPDATE tbl_customer SET flag='0' WHERE id=" . $broker_id_delete . "";

//    echo 'query ' . $sql1;

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

                $query = "SELECT * FROM tbl_customer where flag='1' ORDER BY id ASC";
                mysqli_set_charset($con, 'utf8');
                $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["cust_name"] . '</td>
    <td>' . $row["mobile_no"] . '</td>
    <td>' . $row["address"] . '</td>
    <td>' . $row["buffelo_milk"] . '</td>
    <td>' . $row["cow_milk"] . '</td>
    <td>' . $row["milk_take_time"] . '</td>
    <td><a href="JavaScript:Void(0);" class="btn_edit" tabindex="8" 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="9" 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_broker_id = $_POST['select_broker_id'];
    if ($select_broker_id == 'Select' || $select_broker_id == '') {
        $sql = 'select p.*,tbl_broker.broker_name from tbl_party as p 
LEFT JOIN tbl_broker ON p.broker_id=tbl_broker.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_broker.broker_name from tbl_party as p 
LEFT JOIN tbl_broker ON p.broker_id=tbl_broker.id where p.broker_id="' . $select_broker_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }
//    echo 'Query '.$sql;

    $select_broker_name = $_POST['select_broker_name'];
    if ($select_broker_name == 'Select' || $select_broker_name == '') {
        $select_broker_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_broker_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);
            mysqli_set_charset($con, 'utf8');
            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["broker_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['1_start_bill_date_full']) == "" && !isset($_POST['1_end_bill_date_full']) == "") {
    $start_bill_date = $_POST['1_start_bill_date_full'];
    $start_bill_date = $start_bill_date . ' 00:00:05';
    $end_bill_date = $_POST['1_end_bill_date_full'];
    $end_bill_date = $end_bill_date . ' 23:50:05';

    $select_broker_id = $_POST['select_broker_id'];
    if ($select_broker_id == 'Select' || $select_broker_id == '') {
        $sql = 'select p.*,tbl_broker.broker_name from tbl_party as p 
LEFT JOIN tbl_broker ON p.broker_id=tbl_broker.id where p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    } else {
        $sql = 'select p.*,tbl_broker.broker_name from tbl_party as p 
LEFT JOIN tbl_broker ON p.broker_id=tbl_broker.id where p.broker_id="' . $select_broker_id . '" and p.bill_date BETWEEN "' . $start_bill_date . '" and "' . $end_bill_date . '" and p.flag="1"';
    }
//    echo 'Query '.$sql;

    $select_broker_name = $_POST['select_broker_name'];
    if ($select_broker_name == 'Select' || $select_broker_name == '') {
        $select_broker_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['1_start_bill_date_full'])) . '  થી  ' . date("d-M-Y", strtotime($_POST['1_end_bill_date_full'])) . ' સુધી. </b></label>
                <label style="margin-left: 20px"><b> બ્રોકરનું નામ -  ' . $select_broker_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) {
            mysqli_set_charset($con, 'utf8');
            $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["broker_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;
}
?>