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

//echo 'milk_type_id ' . $_POST['milk_type_id'];
//echo 'customer_id ' . $_POST['customer_id'];

$month = date('m');
$day = date('d');
$year = date('Y');
$today_date = date("Y-m-d");

if (!isset($_POST['milk_type_id']) == "" && !isset($_POST['customer_id']) == "") {
    $milk_type_id = $_POST['milk_type_id'];
    $customer_id = $_POST['customer_id'];
    $milk_time_entry = $_POST['milk_time_entry'];
    $milk_sale_date = $_POST['milk_sale_date'];
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

//    echo 'broker id ' . $_POST['broker_id'];
    $query = 'SELECT * FROM `tbl_daily_sell_milk` WHERE flag="1" and milk_type_id="' . $milk_type_id . '" and cust_id="' . $customer_id . '" ORDER by id DESC LIMIT 1';
//    echo 'query ' . $query;
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $i = 1;
        $response = '';
        while ($row = mysqli_fetch_assoc($result)) {
//            print_r($row);
            $quantity = $row['quntity'];
            $milk_price = 0;
            ?>

            <div class="col-md-1">
                <label for="quantity" class="">કેટલા લિટર </label>
                <input name="quantity" id="quantity" step="any" tabindex="4" value="<?php echo $row['quntity']; ?>" placeholder="quantity" type="number" class="form-control">
                <div id="msg_quantity"></div>
            </div>

            <div class="col-md-2">
                <label for="price" class="">દુધનો ભાવ ૧ લિટર </label>
                <?php
                $query = 'select * from  tbl_milk_rate where flag="1" and id="' . $milk_type_id . '"';
                $result1 = mysqli_query($con, $query);
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_assoc($result1)) {
                        $milk_price = $row1["price"];
                        ?>
                        <input name="price" id="price" placeholder="લિટર" value="<?php echo $row1["price"]; ?>" name="<?php echo $row1["id"]; ?>" disabled="true" tabindex="5" type="number" class="form-control">
                        <?php
                    }
                }
                ?>
            </div>
            <div class="col-md-3">
                <label for="total_amt" class="">કુલ રકમ રુ. </label>
                <?php
                $total_amount = $quantity * $milk_price;
//                echo '$total_amount ' . $total_amount;
                ?>
                <input name="total_amt" id="total_amt" disabled="" value="<?php echo $total_amount; ?>" placeholder="Total Amount" tabindex="6" type="number" class="form-control">
                <div id="msg_total_amt"></div>
            </div>
            <div class="col-md-3">
                <label for="companay_name" class="col1">Payment Pay</label>
                <select name="select" id="payment_pay" tabindex="7" class="form-control">
                    <option name="UNPAID" selected="" id="UNPAID"> UNPAID </option>
                    <option name="PAID" id="PAID"> PAID </option>
                </select>
                <div id="msg_payment_pay"></div>
            </div>

<!--            <div class="position-relative form-group col-md-3">                    
                <label class="">દુધ વેચાણ તારીખ</label>
                <input name="milk_date" id="milk_sale_date" tabindex="8" placeholder="દુધ વેચાણ તારીખ" value="<?php // echo $today_date; ?>" type="date" class="form-control">
                <div id="msg_milk_sale_date"></div>
            </div>-->

            <div class="col-md-3">
                <label for="companay_name" class="col1">દુધનો સમય</label>
                <select name="select" id="milk_time" tabindex="9" class="form-control">

                    <?php
                    if ($milk_time_entry == "MORNING") {
                        ?>
                        <option name="MORNING" selected="" id="MORNING"> MORNING </option>
                        <option name="EVENING" id="EVENING"> EVENING </option>
                        <?php
                    } else if ($milk_time_entry == "EVENING") {
                        ?>
                        <option name="MORNING" id="MORNING"> MORNING </option>
                        <option name="EVENING" selected="" id="EVENING"> EVENING </option>
                        <?php
                    } else {
                        ?>
                        <option name="MORNING" selected="" id="MORNING"> MORNING </option>
                        <option name="EVENING" id="EVENING"> EVENING </option>
                        <?php
                    }
                    ?>
                </select>
                <div id="msg_milk_time"></div>
            </div>

            <?php
            $query1 = 'SELECT * FROM `tbl_daily_sell_milk` WHERE flag="1" and milk_time="' . $milk_time_entry . '" and milk_type_id="' . $milk_type_id . '" and cust_id="' . $customer_id . '" and created_date="' . $milk_sale_date . '" ORDER by id DESC LIMIT 1';
//            echo 'query1 ' . $query1;
            $result1 = mysqli_query($con, $query1);
            if (mysqli_num_rows($result1) > 0) {
//                echo 'query1 ' . $query1;
                ?>
                <div class="form-row" style="margin-top: 10px">
                    <div class="col-md-12" style="font-size: 18px;color: green"> * Selected Date Milk Entry Already Added.</div>
                </div>
                <?php
            } else {
//                echo 'query1 ' . $query1;
                ?>
                <div class="form-row" style="margin-top: 10px">
                    <div class="col-md-12">
                        <button type="button" class="mt-1 btn btn-primary form-control font-size-lg" tabindex="10" style="width: auto!important" id="btn_save_broker">Save</button>
                        <div id="msg_insert_data" > </div>
                    </div>
                </div>
                <?php
            }
        }
//        echo $response;
    } else {
        ?>
        <div class="col-md-1">
            <label for="quantity" class="">કેટલા લિટર </label>
            <input name="quantity" id="quantity" step="any" tabindex="4" value="0" placeholder="quantity" type="number" class="form-control">
            <div id="msg_quantity"></div>
        </div>

        <div class="col-md-2">
            <label for="price" class="">દુધનો ભાવ ૧ લિટર </label>
            <?php
            $query = 'SELECT * FROM `tbl_milk_rate` where flag=1 and id="' . $milk_type_id . '" ORDER by id DESC limit 1';
            $result = mysqli_query($con, $query);
            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    ?>
                    <input name="price" id="price" placeholder="લિટર" value="<?php echo $row["price"]; ?>" disabled="true" tabindex="5" type="number" class="form-control">
                    <?php
                }
            }
            ?>
            <div id="msg_price"></div>
        </div>

        <div class="col-md-3">
            <label for="total_amt" class="">કુલ રકમ રુ. </label>
            <input name="total_amt" id="total_amt" disabled="" placeholder="Total Amount" tabindex="6" type="number" class="form-control">
            <div id="msg_total_amt"></div>
        </div>
        <div class="col-md-3">
            <label for="companay_name" class="col1">Payment Pay</label>
            <select name="select" id="payment_pay" tabindex="7" class="form-control">
                <option name="UNPAID" selected="" id="UNPAID"> UNPAID </option>
                <option name="PAID" id="PAID"> PAID </option>
            </select>
            <div id="msg_payment_pay"></div>
        </div>

<!--        <div class="position-relative form-group col-md-3">                    
            <label class="">દુધ વેચાણ તારીખ</label>
            <input name="milk_date" id="milk_sale_date" tabindex="8" placeholder="દુધ વેચાણ તારીખ" value="<?php // echo $today_date; ?>" type="date" class="form-control">
            <div id="msg_milk_sale_date"></div>
        </div>-->

        <div class="col-md-3">
            <label for="companay_name" class="col1">દુધનો સમય</label>
            <select name="select" id="milk_time" tabindex="9" class="form-control">
                <?php
                if ($milk_time_entry == "MORNING") {
                    ?>
                    <option name="MORNING" selected="" id="MORNING"> MORNING </option>
                    <option name="EVENING" id="EVENING"> EVENING </option>
                    <?php
                } else if ($milk_time_entry == "EVENING") {
                    ?>
                    <option name="MORNING" id="MORNING"> MORNING </option>
                    <option name="EVENING" selected="" id="EVENING"> EVENING </option>
                    <?php
                } else {
                    ?>
                    <option name="MORNING" selected="" id="MORNING"> MORNING </option>
                    <option name="EVENING" id="EVENING"> EVENING </option>
                    <?php
                }
                ?>
            </select>
            <div id="msg_milk_time"></div>
        </div>

        <?php
        $query1 = 'SELECT * FROM `tbl_daily_sell_milk` WHERE flag="1" and milk_time="' . $milk_time_entry . '" and milk_type_id="' . $milk_type_id . '" and cust_id="' . $customer_id . '" and created_date="' . $milk_sale_date . '" ORDER by id DESC LIMIT 1';
//            echo 'query1 ' . $query1;
        $result1 = mysqli_query($con, $query1);
        if (mysqli_num_rows($result1) > 0) {
//            echo 'query1 ' . $query1;
            ?>
            <div class="form-row" style="margin-top: 10px">
                <div class="col-md-12" style="font-size: 18px;color: green"> * Selected Date Milk Entry Already Added.</div>
            </div>
            <?php
        } else {
//            echo 'query1 ' . $query1;
            ?>
            <div class="form-row" style="margin-top: 10px">
                <div class="col-md-12">
                    <button type="button" class="mt-1 btn btn-primary form-control font-size-lg" tabindex="10" style="width: auto!important" id="btn_save_broker">Save</button>
                    <div id="msg_insert_data" > </div>
                </div>
            </div>
            <?php
        }
    }
} else {
    echo 'else';
}


if (!isset($_POST['milk_type_id_search']) == "" && !isset($_POST['animal_type']) == "") {
    $milk_type_id = $_POST['milk_type_id_search'];
    $animal_type = $_POST['animal_type'];

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

    $query = '';
    if ($animal_type == "BUFFALO") {
        $query = 'select DISTINCT buffelo_milk from  tbl_customer where flag="1"';
    } else if ($animal_type == "COW") {
        $query = 'select DISTINCT cow_milk from  tbl_customer where flag="1"';
    }
//    echo '$query '.$query;

    $result1 = mysqli_query($con, $query);
    ?>
    <option name="Select" id="Select"> Select </option>
    <?php
    if (mysqli_num_rows($result1) > 0) {
        while ($row = mysqli_fetch_assoc($result1)) {
//            $milk_price = $row1["price"];

            if ($animal_type == "BUFFALO") {
                ?>
                <option id="<?php echo $row["buffelo_milk"]; ?>"><?php echo $row["buffelo_milk"]; ?></option>
                <?php
            } else if ($animal_type == "COW") {
                ?>
                <option id="<?php echo $row["cow_milk"]; ?>"><?php echo $row["cow_milk"]; ?></option>
                <?php
            }
        }
    }
}


if (!isset($_POST['search_milk_liter']) == "" && !isset($_POST['animal_type_milk']) == "") {
    $search_milk_liter = $_POST['search_milk_liter'];
    $animal_type_milk = $_POST['animal_type_milk'];
    $search_milk_time = $_POST['search_milk_time'];

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

    $query = '';
    if ($animal_type_milk == "BUFFALO") {
        $query = 'select * from  tbl_customer where flag="1" and milk_take_time="' . $search_milk_time . '" and buffelo_milk="' . $search_milk_liter . '"';
    } else if ($animal_type_milk == "COW") {
        $query = 'select * from  tbl_customer where flag="1" and milk_take_time="' . $search_milk_time . '" and cow_milk="' . $search_milk_liter . '"';
    }
    echo '$query ' . $query;

    $result1 = mysqli_query($con, $query);
    if (mysqli_num_rows($result1) > 0) {
        ?>
        <option name="Select" id="Select"> Select </option>
        <?php
        while ($row = mysqli_fetch_assoc($result1)) {
//            $milk_price = $row1["price"];

            if ($animal_type_milk == "BUFFALO") {
                ?>
                <option id="<?php echo $row["id"]; ?>"><?php echo $row["cust_name"]; ?></option>
                <?php
            } else if ($animal_type_milk == "COW") {
                ?>
                <option id="<?php echo $row["id"]; ?>"><?php echo $row["cust_name"]; ?></option>
                <?php
            }
        }
    }
}


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

    $milk_take_time_search = $_POST['milk_take_time_search'];

    try {
        if ($con) {
            $sql = "SELECT * FROM tbl_customer where flag='1' and milk_take_time='" . $milk_take_time_search . "'  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:02";
        }
    } catch (Exception $ex) {
        echo "Error:03" . $ex;
    }
}



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

    $start_date_bill = $_POST['start_date_bill'];
    $date = $_POST['start_date_bill'];

    $sql = 'SELECT td.*,tbl_customer.cust_name,tbl_customer.address,tmr.milk_type FROM `tbl_daily_sell_milk` as td left join tbl_customer on td.cust_id=tbl_customer.id left join tbl_milk_rate as tmr on tmr.id=td.milk_type_id WHERE td.flag=1 and td.created_date="' . $date . '" ORDER by td.id ASC';
    echo '$sql ' . $sql;
    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)) {
                    $datetime = new DateTime($row["created_date"]);
                    $response = $response . '
                                                    <tr>
                                                        <td scope="row">' . $i . '</td>
                                                         <td>' . $datetime->format('d-m-Y') . '</td>
                                                            <td>' . $row["milk_time"] . '</td>
                                                                <td>' . $row["milk_type"] . '</td>
                                                        <td>' . $row["cust_name"] . '</td>
                                                        <td>' . $row["quntity"] . '</td>
                                                        <td>' . $row["price"] . '</td>
                                                        <td>' . $row["total_amt"] . '</td>
                                                            <td>' . $row["paid"] . '</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) {
        echo $e->getMessage();
    }
}


if (!isset($_POST['start_date_time_milk']) == "" && !isset($_POST['milk_time_check']) == "") {

    $start_date_bill = $_POST['start_date_time_milk'];
    $date = $_POST['start_date_time_milk'];
    $milk_time_check = $_POST['milk_time_check'];

    $sql = 'SELECT td.*,tbl_customer.cust_name,tbl_customer.address,tmr.milk_type FROM `tbl_daily_sell_milk` as td left join tbl_customer on td.cust_id=tbl_customer.id left join tbl_milk_rate as tmr on tmr.id=td.milk_type_id WHERE td.flag=1 and td.created_date="' . $date . '" and milk_time="' . $milk_time_check . '" ORDER by td.id ASC';
    echo '$sql ' . $sql;
    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)) {
                    $datetime = new DateTime($row["created_date"]);
                    $response = $response . '
                                                    <tr>
                                                        <td scope="row">' . $i . '</td>
                                                         <td>' . $datetime->format('d-m-Y') . '</td>
                                                            <td>' . $row["milk_time"] . '</td>
                                                                <td>' . $row["milk_type"] . '</td>
                                                        <td>' . $row["cust_name"] . '</td>
                                                        <td>' . $row["quntity"] . '</td>
                                                        <td>' . $row["price"] . '</td>
                                                        <td>' . $row["total_amt"] . '</td>
                                                            <td>' . $row["paid"] . '</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) {
        echo $e->getMessage();
    }
}
?>