403Webshell
Server IP : 210.245.233.93  /  Your IP : 216.73.216.226
Web Server : Apache/2.2.15 (CentOS)
System : Linux webserver2.onesolution.com.hk 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64
User : apache ( 48)
PHP Version : 5.3.3
Disable Function : exec, shell_exec, system, passthru, popen, proc_open, pcntl_exec
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/hkosl.com/innoutstorage/webadmin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/hkosl.com/innoutstorage/webadmin/_ajax.php
<?php
require_once('check_login.php');

global $dbh;

if ($_GET["for"] == "search_matched_customer") {
    $identity_id = $_POST["identity_id"];

    if (empty($identity_id))
        exit;

    $sql = "select * from customer where deleted = 0 ";
    $customer_info = bind_pdo($sql, NULL, "selectall");

    $exist = 0;

    //looping customer info to find if a matched customer exist
    foreach ($customer_info as $customer) {
        if ($customer["deleted"] == 1)
            continue;

        $decrypt_hkid = rsa_crypt($customer["hkid"], 2);
        $decrypt_hkbr = rsa_crypt($customer["hkbr"], 2);
        $decrypt_tel = rsa_crypt($customer["tel"], 2);

        if ((!empty($decrypt_hkbr) && strpos($decrypt_hkbr, $identity_id) !== FALSE) || (!empty($decrypt_hkid) && strpos($decrypt_hkid, $identity_id) !== FALSE) || (!empty($decrypt_tel) && strpos($decrypt_tel, $identity_id) !== FALSE) || (!empty($customer["code"]) && strpos($customer["code"], $identity_id) !== FALSE)) {

            if (!empty($decrypt_hkbr)) {
                $identity = $decrypt_hkbr;
            }

            if (!empty($decrypt_hkid)) {
                $identity = $decrypt_hkid;
            }

            $exist++;

            if ($exist == 1) {
                echo "<table cellpadding='5' border='1px solid #000' style='border-collapse: collapse;width: 100%;'><caption class='tl'>客戶資料</caption>";

                echo "<tr>
						<td></td>
						<td style='width: 5%;'>編號</td>
						<td style='width: 5%;'>稱謂</td>
						<td style='width: 5%;'>姓氏</td>
						<td style='width: 10%;'>名字</td>
						<td style='width: 10%;'>證明文件</td>
						<td style='width: 10%;'>文件號碼</td>
						<td style='width: 10%;'>聯絡電話</td>
						<td style='width: 20%;'>聯絡電郵</td>
						<td style='width: 30%;'>聯絡地址</td>
					</tr>";
            }

            //$customer_name = get_customer_name($customer["firstname"], $customer["lastname"]);

            /*if($customer["title"] == "Mr"){
                $title = "先生";
            }else if($customer["title"] == "Mrs"){
                $title = "女士";
            }else if($customer["title"] == "Ms"){
                $title = "小姐";
            }else{

            }*/

            echo "<tr class='customer_info' id='" . $customer["id"] . "'>
					<td>
						<input type='hidden' class='identity_type' name='identity_type2' value='" . $customer["typeid"] . "'>
						<input type='hidden' name='companyname2' class='companyname' value='" . $customer["companyname"] . "'>
						<input type='radio' name='customer' value='" . $customer["id"] . "'>
					</td>
					<td class='code'>" . ($customer["code"]) . "</td>
					<td class='title'>" . ($customer["title"]) . "</td>
					<td class='lastname'>" . rsa_crypt($customer["lastname"], 2) . "</td>
					<td class='firstname'>" . rsa_crypt($customer["firstname"], 2) . "</td>";

            if ($customer["typeid"] == "PERSONAL") {
                echo "<td>身分證(個人)</td>";
            }

            if ($customer["typeid"] == "BUSINESS") {
                echo "<td>商業登記證(公司)</td>";
            }

            echo "
					<td class='identity_id'>" . $identity . "</td>
					<td class='tel'>" . rsa_crypt($customer["tel"], 2) . "</td>
					<td class='email'>" . rsa_crypt($customer["email"], 2) . "</td>
					<td class='address'>" . rsa_crypt($customer["address"], 2) . "</td>
				</tr>";
        }
    }


    if ($exist > 0) {
        echo "</table>";
    }
}

if ($_GET["for"] == "search_available_room") {
    $location_id = $_POST["location_id"];
    $typeid = $_POST["typeid"];
    $master_room_id = $_POST["master_room_id"];

    if (empty($typeid) || empty($master_room_id) || empty($location_id))
        exit;

    $sql = "select *, room.id as room_id, room.code as room_code from room room
						INNER JOIN location location ON room.location_id = location.id
						INNER JOIN master_room_price room_price ON room.master_room_id = room_price.master_room_id
						where room.deleted = 0 and room.status = 'OPEN' and room.typeid = ? and room.master_room_id = ? and room.location_id = ?";
    $parameters = array($typeid, $master_room_id, $location_id);

    $room_info = bind_pdo($sql, $parameters, "selectall");

    if (!empty($_SESSION["switch"])) {
        //allow to select customer rented room when do switching
        $extra_room_info = array();
        foreach ($_SESSION["switch"]["ordered_room_id"] as $ordered_room_id) {
            $sql = "select *, room.id as room_id, room.code as room_code from room room
						INNER JOIN location location ON room.location_id = location.id
						INNER JOIN master_room_price room_price ON room.master_room_id = room_price.master_room_id
						where room.deleted = 0 and room.typeid = ? and room.master_room_id = ? and room.location_id = ? and room.id = ?";
            $parameters = array($typeid, $master_room_id, $location_id, $ordered_room_id);

            //_log(basename($_SERVER['PHP_SELF']), "64091519", $sql, $parameters);

            $extra_room_info[] = bind_pdo($sql, $parameters, "selectone");

        }

        if (!empty($extra_room_info)) {
            foreach ($extra_room_info as $this_room_info) {
                $room_info[] = $this_room_info;
            }
        }

    }


    $all_room_id = array();
    $valid_room_info = array();

    foreach ($room_info as $room) {
        if (!in_array($room["room_id"], $all_room_id)) {
            $all_room_id[] = $room["room_id"];
            $valid_room_info[] = $room;
        }

    }

    $exist = 0;
    if (!empty($room_info) && $room_info[0]["room_id"] > 0) {
        foreach ($valid_room_info as $room) {
            if (empty($room))
                break;

            $exist++;

            $master_room_info = get_master_room($room["master_room_id"]);

            if ($exist == 1) {
                echo "<table cellpadding='5' border='1px solid #000' style='border-collapse: collapse;'><caption class='tl'>可選擇倉庫</caption>";

                echo "<tr>
						<td>倉庫編號</td>
						<td>尺寸</td>
						<td>原價(月)</td>
						<td>優惠價(月)</td>
						<td>預繳 / 特價優惠</td>
						<td>價格</td>
						<td>價格(折扣後)</td>
						<td></td>
					</tr>";
            }

            //check if this room selected
            $background = "background-color: #ffffff";
            $available_room_selected_class = "";
            $select_room_text = "選取";
            if (!empty($_SESSION["selected_room_tr"])) {

                if (strpos($_SESSION["selected_room_tr"], $room["room_code"]) !== FALSE) {
                    $background = "background-color: #7EBF3D";
                    $select_room_text = "已選取";
                    $available_room_selected_class = "available_room_selected";
                }
            }

            echo "<tr style='" . $background . "' id='" . $room["room_id"] . "' class='room_id_flag location_" . $location_id . " " . $available_room_selected_class . "'>
					<td>" . $room["room_code"] . " <input type='hidden' name='room_id[]' value='" . $room["room_id"] . "'><input type='hidden' name='room_code[]' value='" . $room["room_code"] . "'><input type='hidden' name='location_id[]' value='" . $location_id . "'></td>
					<td>" . $master_room_info["length"] . "X" . $master_room_info["width"] . "X" . $master_room_info["height"] . " <input type='hidden' name='room_size[]' value='" . ($master_room_info["length"] * $master_room_info["width"]) . "'></td>
					<td>$" . numberformat($room["retail_price"], 2, "") . " <input type='hidden' name='retail_price[]' value='" . $room["retail_price"] . "'></td>
				";

            //need to check if master room discount exist, if exist -> discounted price -> master room discount
            if ($nowdate >= $room["effectivedate_from"] && $nowdate <= $room["effectivedate_to"]) {
                //discounted price valid
                echo "<td>$" . ($room["discounted_price"]) . " </td>";

                $set_discounted_price = $room["discounted_price"];
            } else {
                echo "<td>- </td>";

                $set_discounted_price = $room["retail_price"];
            }

            $discounted_price = $set_discounted_price;
            //check if this kind of room has prepaid / specific discount
            //$sql                       = "select *, discount.id as master_room_discount_id from master_room_discount discount INNER JOIN master_type_code mtcode ON discount.payment_term = mtcode.code where discount.master_room_id = ? and discount.deleted = ? and mtcode.typeid = ? and ? >= discount.effectivedate_from and ? <= discount.effectivedate_to order by discount.payment_term ASC";
            $sql = "select *, discount.id as master_room_discount_id from master_room_discount discount 
				INNER JOIN master_type_code mtcode ON discount.payment_term = mtcode.code 
				where discount.master_room_id = ? and discount.deleted = ? and discount.status = ? and mtcode.typeid = ? and ? >= discount.effectivedate_from and ? <= discount.effectivedate_to 
				order by mtcode.sort ASC ";
            $parameters = array($_POST["master_room_id"], 0, 1, "PAYMENT_TERM", date("Y-m-d"), date("Y-m-d"));
            $master_room_discount_info = bind_pdo($sql, $parameters, "selectall");
            $discount_message = "";
            if (!empty($master_room_discount_info)) {
                foreach ($master_room_discount_info as $master_room_discount) {
                    //$discount_message .= $master_room_discount["name_tc"] . " -" . $master_room_discount["discount"] . $master_room_discount["discount_type"] . " / ";
                    if ($master_room_discount["payment_term"] == "SPECIFIC_SIZE") {
                        $class = "SPECIFIC_SIZE_DISCOUNT _DISCOUNT DISCOUNT_ID_" . $master_room_discount["master_room_discount_id"];

                        $discounted_price = $set_discounted_price * (100 - $master_room_discount["discount"]) / 100;
                    } else {
                        $class = "PREPAID_DISCOUNT _DISCOUNT DISCOUNT_ID_" . $master_room_discount["master_room_discount_id"];

                        if ($master_room_discount["is_final_discount"] == 1) {
                            $class .= " IS_FINAL_DISCOUNT";
                        }

                        $discounted_price = $set_discounted_price;
                    }

                    //$discount_message .= $master_room_discount["name_tc"] . " <input type='text' id='MONTH_" . $master_room_discount["config_value"] . "' value='" . $master_room_discount["discount"] . "' style='width: 40px;' class='" . $class . "' > % 折扣 / </br>";


                    $discount_message .= $master_room_discount["name_tc"] . " <input type='text' id='MONTH_" . $master_room_discount["config_value"] . "' value='" . ($master_room_discount["discount"]) . "' style='width: 40px;' class='" . $class . " available_room_list_input' onkeyup=\"update_discounted_price($(this).closest('tr').attr('id'));data_calculation($(this).attr('class'));\"> % 折扣 / </br>";


                }
                $discount_message = substr_replace($discount_message, "", -7);
            }

            /*echo "<td>" . $discount_message . "
                <input type='hidden' name='prepaid_id[]' class='prepaid_id' value='' />
                <input type='hidden' name='specific_size_id[]' class='specific_size_id' value='' />
                </td>";*/

            echo "<td>" . $discount_message . "</td>";

            /*echo "<td>
                    <input type='text' name='set_discounted_price[]' class='set_discounted_price' value='" . numberformat($set_discounted_price) . "' style='width: 70px;'>
                </td>";*/

            echo "<td>
						<input type='text' name='set_discounted_price[]' class='set_discounted_price available_room_list_input' value='" . numberformat($set_discounted_price, 2, "") . "' style='width: 70px;' onkeyup=\"update_discounted_price($(this).closest('tr').attr('id'));data_calculation($(this).attr('class'));\">
					</td>";

            echo "<td>
						<input type='text' name='discounted_price[]' class='discounted_price available_room_list_input' value='" . numberformat($discounted_price, 2, "") . "' style='width: 70px;' readonly>
					</td>";

            echo "<td class='select_room_td'>
						<button type='button' class='select_room'>" . $select_room_text . "</button>
					</td>";

            echo "</tr>";

        }

        if ($exist > 0) {
            echo "</table>";
        }
    } else {
        echo "沒有空置的倉庫可供租出。";
    }
}

if ($_GET["for"] == "selected_room_tr") {
    $_SESSION["selected_room_tr"] = ($_POST["selected_room_tr"]);
}

if ($_GET["for"] == "prepaid_discount") {

    //var_dump($_POST);
    $prepaid_deduct = 0;

    if (!empty($_POST["order_total"]) && !empty($_POST["prepaid_month"]) && !empty($_POST["selected_room_id"])) {
        $order_total = $_POST["order_total"];
        $prepaid_month = $_POST["prepaid_month"];
        $selected_room_id_list = $_POST["selected_room_id"];
        $selected_room_id = explode(",", $selected_room_id_list);

        foreach ($selected_room_id as $room_id) {
            $config_value_list = array();

            $sql = "select * from master_room_discount mrdiscount INNER JOIN master_type_code mtcode ON mrdiscount.payment_term = mtcode.code where mtcode.typeid = 'PAYMENT_TERM' and mrdiscount.status = 1 and mrdiscount.deleted = 0 and mrdiscount.effectivedate_from <= ? and mrdiscount.effectivedate_to >= ? and mrdiscount.master_room_id = ? and (";

            $room_info = get_room($room_id);

            $parameters = array(date("Y-m-d"), date("Y-m-d"), $room_info["master_room_id"]);

            /*for ($i = $prepaid_month; $i > 0; $i--) {
                $sql .= "mtcode.config_value = ? or ";
                $parameters[] = $i;
            }

            $sql = substr_replace($sql, "", -3);*/

            $sql .= "mtcode.config_value = ? ";
            $parameters[] = $prepaid_month;

            $sql .= ") order by mtcode.config_value DESC, mrdiscount.lastupdate DESC";

            /*var_dump($sql);
            var_dump($parameters);*/
            $prepaid_discount_info = bind_pdo($sql, $parameters, "selectone");

            //calculate prepaid deduct
            if ($prepaid_discount_info["discount_type"] == "%") {
                $prepaid_deduct += $order_total * $prepaid_discount_info["discount"] / 100;
            } else {
                $prepaid_deduct += $prepaid_discount_info["discount"];
            }
        }
    }

    echo round($prepaid_deduct, 1);
}


if ($_GET["for"] == "ajax_calculate_monthly_rent") {

    $total_discounted_price = 0;

    $selected_room_id_list = $_REQUEST["selected_room_id_list"];

    $selected_room_id_list = explode(",", $selected_room_id_list);

    $prepaid_month = $_REQUEST["prepaid_month"];

    $selected_room_discount_id_list = $_REQUEST["selected_room_discount_id_list"];

    $selected_room_discount_id_list = explode(",", $selected_room_discount_id_list);

    $selected_room_discount_id_value_list = $_REQUEST["selected_room_discount_id_value_list"];

    $selected_room_discount_id_value_list = explode(",", $selected_room_discount_id_value_list);

    //var_dump($_REQUEST);

    $j = 0;
    $discount_info = array();
    //$biggest_retail_price = 0;

    foreach ($selected_room_id_list as $key => $room_id) {
        //get the discounted price of each selected room
        $sql = "select *, room.id as room_id, room.code as room_code from room room
						INNER JOIN master_room_price room_price ON room.master_room_id = room_price.master_room_id
						where room.deleted = 0 and room.id = ? ";
        $parameters = array($room_id);

        $room_info = bind_pdo($sql, $parameters, "selectone");

        //compare retail price and record the biggest one
        /*if($room_info["retail_price"] >= $biggest_retail_price){
            $biggest_retail_price = $room_info["retail_price"];
        }*/

        if ($nowdate >= $room_info["effectivedate_from"] && $nowdate <= $room_info["effectivedate_to"]) {
            //discounted price valid
            $discounted_price = $room_info["discounted_price"];
        } else {
            $discounted_price = $room_info["retail_price"];
        }

        //do discount, start from prepaid
        if (!empty($prepaid_month)) {

            //get prepaid discount first
            $sql = "select *,mrdiscount.id as master_room_discount_id from master_room_discount mrdiscount
						INNER JOIN master_type_code mtcode ON mrdiscount.payment_term = mtcode.code
						where mrdiscount.deleted = ? and mrdiscount.status = ? and mrdiscount.payment_term IS NOT NULL and mrdiscount.payment_term != ? and mrdiscount.master_room_id = ? and mrdiscount.effectivedate_from <= ? and ? <= mrdiscount.effectivedate_to group by mrdiscount.payment_term order by config_value DESC";

            $parameters = array(0, 1, "SPECIFIC_SIZE", $room_info["master_room_id"], date("Y-m-d"), date("Y-m-d"));

            $master_room_discount_info = bind_pdo($sql, $parameters, "selectall");


            if (!empty($master_room_discount_info)) {
                foreach ($master_room_discount_info as $master_room_discount) {
                    //if ($master_room_discount["discount_type"] == "%" && $master_room_discount["config_value"] <= $prepaid_month) {
                    if ($master_room_discount["discount_type"] == "%" && $master_room_discount["config_value"] == $prepaid_month) {
                        $latest_discount_value = $master_room_discount["discount"];
                        //discount id list
                        foreach ($selected_room_discount_id_list as $list_key => $list) {
                            $token = explode("DISCOUNT_ID_", $list);
                            $this_discount_id = $token[1];

                            if ($this_discount_id == $master_room_discount["master_room_discount_id"]) {
                                $latest_discount_value = $selected_room_discount_id_value_list[$list_key];
                                break;
                            }
                        }

                        //$deduct = $discounted_price * $master_room_discount["discount"] / 100;
                        $deduct = $discounted_price * $latest_discount_value / 100;

                        $discount_info[$j]["master_room_discount_id"] = $master_room_discount["master_room_discount_id"];
                        $discount_info[$j]["master_room_id"] = $master_room_discount["master_room_id"];
                        $discount_info[$j]["discount_type"] = $master_room_discount["discount_type"];
                        //$discount_info[$j]["discount"]                = $master_room_discount["discount"];
                        $discount_info[$j]["discount"] = $latest_discount_value;
                        $discount_info[$j]["monthly_deduct"] = $deduct;
                        $discount_info[$j]["room_id"] = $room_id;

                        $discounted_price -= $deduct;

                        //check if this is the final discount or not
                        if ($master_room_discount["is_final_discount"] == 1 || $deduct > 0) {
                            break;
                        }
                    }
                }

            }

        }

        //specific discount record

        $sql = "select * from master_room_discount mrdiscount
						where mrdiscount.deleted = ? and mrdiscount.payment_term = ? and mrdiscount.master_room_id = ? and mrdiscount.effectivedate_from <= ? and ? <= mrdiscount.effectivedate_to";
        $parameters = array(0, "SPECIFIC_SIZE", $room_info["master_room_id"], date("Y-m-d"), date("Y-m-d"));
        $master_room_discount_info = bind_pdo($sql, $parameters, "selectone");


        if (!empty($master_room_discount_info) && $master_room_discount_info["discount_type"] == "%") {

            $latest_discount_value = $master_room_discount_info["discount"];
            //discount id list
            foreach ($selected_room_discount_id_list as $list_key => $list) {
                $token = explode("DISCOUNT_ID_", $list);
                $this_discount_id = $token[1];

                if ($this_discount_id == $master_room_discount_info["master_room_discount_id"]) {
                    $latest_discount_value = $selected_room_discount_id_value_list[$list_key];
                    break;
                }
            }

            //$deduct = $discounted_price * $master_room_discount_info["discount"] / 100;
            $deduct = $discounted_price * $latest_discount_value / 100;


            $discount_info[$j]["master_room_discount_id"] = $master_room_discount_info["id"];
            $discount_info[$j]["master_room_id"] = $master_room_discount_info["master_room_id"];
            $discount_info[$j]["discount_type"] = $master_room_discount_info["discount_type"];
            //$discount_info[$j]["discount"]                = $master_room_discount_info["discount"];
            $discount_info[$j]["discount"] = $latest_discount_value;
            $discount_info[$j]["monthly_deduct"] = $deduct;
            $discount_info[$j]["room_id"] = $room_id;

            $discounted_price -= $deduct;
        }

        //check if this is the final discount or not
        if ($master_room_discount_info["is_final_discount"] == 1) {
            break;
        }

        $j++;
        $total_discounted_price += $discounted_price;
    }

    //record biggest price
    //$_SESSION["warehousing"][2]["biggest_retail_price"] = $biggest_retail_price;

    //record discount info into session
    $_SESSION["warehousing"][2]["order_discount"] = $discount_info;

    //output the total_discounted_price
    echo round($total_discounted_price, 1);
}


if ($_GET["for"] == "switch_order") {
    if (!empty($_GET["order_id"])) {
        $order_id = (int)$_GET["order_id"];
        $order_info = get_order($order_id);

        if ($order_info["status"] == "VOID" || $order_info["status"] == "COMPLETED") {
            $order_status = get_master_type_code("ORDER_STATUS", $order_info["status"]);
            echo "<script>alert('這張合約的狀態為 " . $order_status["name_tc"] . " 。不可以進行轉倉程序。如需為這張合約進行轉倉,請先更改合約的狀態。'); window.location.href='order_index.php'; </script>";
            exit;
        }

        $_SESSION["switch"]["order_info"] = $order_info;

        $_SESSION["switch"]["ordered_room_code"] = array();
        $order_room_info = get_order_room($order_id);
        foreach ($order_room_info as $order_room) {
            $room_info = get_room($order_room["room_id"]);
            $_SESSION["switch"]["ordered_room_id"][] = $room_info["id"];
        }

        $_SESSION["warehousing"][1]["customer_id"] = $order_info["customer_id"];
        $customer_info = get_customer($order_info["customer_id"]);

        //record warehousing order in session
        $_SESSION["warehousing"][1]["new_customer"] = 0;

        $_SESSION["warehousing"][1]["customer_code"] = $customer_info["code"];
        $_SESSION["warehousing"][1]["title"] = $customer_info["title"];

        $_SESSION["warehousing"][1]["identity_type"] = $customer_info["typeid"];
        if ($customer_info["typeid"] == "PERSONAL") {
            $_SESSION["warehousing"][1]["identity_id"] = rsa_crypt($customer_info["hkid"], 2);
        } else {
            $_SESSION["warehousing"][1]["identity_id"] = rsa_crypt($customer_info["hkbr"], 2);
        }
        $_SESSION["warehousing"][1]["lastname"] = rsa_crypt($customer_info["lastname"], 2);
        $_SESSION["warehousing"][1]["firstname"] = rsa_crypt($customer_info["firstname"], 2);
        $_SESSION["warehousing"][1]["companyname"] = $customer_info["companyname"];
        $_SESSION["warehousing"][1]["email"] = rsa_crypt($customer_info["email"], 2);
        $_SESSION["warehousing"][1]["tel"] = rsa_crypt($customer_info["tel"], 2);
        $_SESSION["warehousing"][1]["address"] = rsa_crypt($customer_info["address"], 2);
        $_SESSION["warehousing"][1]["pass"] = TRUE;
        $_SESSION["warehousing"][1]["customer_first_order"] = FALSE;

        //calculate the remain money of last contract
        $total_return_amount = 0;
        $datetime1 = date_create($order_info["startdate"]);
        $datetime2 = date_create($nowdate);
        $interval = date_diff($datetime1, $datetime2);

        $used_month_num = (int)$interval->format("%m") + 1;
        $used_money_total = $used_month_num * $order_info["total_discounted_price"];

        //get contract paid amount
        $sql = "select * from invoice where order_id = ? and status != ?";
        $parameters = array($order_id, "VOID");
        $order_invoice_info = bind_pdo($sql, $parameters, "selectall");

        $total_order_product = 0;
        $total_rent = 0;
        $total_penalty = 0;
        $total_deposit = 0;
        $user_paid_amount_for_invoice = 0;
        $user_paid_amount_for_deposit = 0;

        //check invoice
        foreach ($order_invoice_info as $order_invoice) {

            $user_paid_amount_for_invoice += $order_invoice["amount"] - $order_invoice["balance"];


            $invoice_dtl_info = get_invoice_detail($order_invoice["id"]);
            foreach ($invoice_dtl_info as $invoice_dtl) {

                if ($invoice_dtl["type"] == "ORDER_PRODUCT") {
                    $total_order_product += $invoice_dtl["amount"];
                }

                if ($invoice_dtl["type"] == "RENT") {
                    $total_rent += $invoice_dtl["amount"];
                }

                if ($invoice_dtl["type"] == "PENALTY") {
                    $total_penalty += $invoice_dtl["amount"];
                }
            }

            if (round($order_info["free_first_month_deduct"], 1) == round(($order_invoice["amount"] - $order_invoice["balance"]), 1)) {
                $total_order_product = 0;
                $total_penalty = 0;
            }
        }

        //$user_paid_amount_for_invoice_for_rent = $user_paid_amount_for_invoice - $total_order_product;

        $user_paid_amount_for_invoice_for_rent = $user_paid_amount_for_invoice - $total_order_product - $total_penalty;

        //check deposit
        $sql = "select * from deposit where order_id = ? and status != ?";
        $parameters = array($order_id, "VOID");
        $deposit_info = bind_pdo($sql, $parameters, "selectall");
        foreach ($deposit_info as $deposit) {
            $user_paid_amount_for_deposit += $deposit["amount"] - $deposit["balance"];

            $total_deposit += $deposit["amount"];
        }

        $total_return_amount = $total_return_amount + (float)$user_paid_amount_for_invoice_for_rent;

        $total_return_amount = $total_return_amount - (float)$used_money_total;

        //var_dump(round($total_return_amount, 2));
        $_SESSION["switch"]["total_return_amount"] = round($total_return_amount, 1);

        if ($_SESSION["switch"]["total_return_amount"] <= 0) {
            $_SESSION["switch"]["total_return_amount"] = 0;
        }
        echo "<script>window.location.href='warehousing_step2.php';</script>";
        exit;
    }
}


if ($_GET["for"] == "cancel_switch") {
    unset($_SESSION["switch"]);
    unset($_SESSION["warehousing"]);
    unset($_SESSION["selected_room_tr"]);

    header("Location: order_index.php");
}

if ($_GET["for"] == "clear_selected_room") {
    unset($_SESSION["selected_room_tr"]);
    unset($_SESSION["floor_plan_rent_room"]);

    header("Location: warehousing_step2.php");
}

if ($_GET["for"] == "get_customer_unpaid") {
    if (!empty($_POST["customer_id"])) {
        $unpaid_payment = array();
        $i = 0;

        /*if($_SESSION["_payment_type"] == "DEPOSIT"){
            $sql          = "select * from deposit where status = 'NEW' and deleted = 0 and balance != 0";
            $deposit_info = bind_pdo($sql, NULL, "selectall");

            foreach ($deposit_info as $deposit) {
                $order_info = get_order($deposit["order_id"]);

                if ($order_info["customer_id"] != $_POST["customer_id"])
                    continue;

                $unpaid_payment[$i]["payable_type"]        = "DEPOSIT";
                $unpaid_payment[$i]["payable_type_detail"] = "";
                $unpaid_payment[$i]["payable_id"]          = $deposit["id"];
                $unpaid_payment[$i]["payable_code"]        = $deposit["code"];
                $unpaid_payment[$i]["ori_balance"]         = $deposit["balance"];
                $unpaid_payment[$i]["balance"]             = $deposit["balance"];
                $unpaid_payment[$i]["amount"]              = $deposit["amount"];
                $unpaid_payment[$i]["docdate"]             = $deposit["docdate"];
                $i++;
            }
        }elseif($_SESSION["_payment_type"] == "INVOICE"){
            $sql          = "select * from invoice where (status = 'NEW' or status = 'SENT') and deleted = 0 and balance != 0";
            $invoice_info = bind_pdo($sql, NULL, "selectall");

            foreach ($invoice_info as $invoice) {
                //$order_info = get_order($invoice["order_id"]);

                if ($invoice["customer_id"] != $_POST["customer_id"])
                    continue;

                $unpaid_payment[$i]["payable_type"] = "INVOICE";
                $unpaid_payment[$i]["payable_id"]   = $invoice["id"];
                $unpaid_payment[$i]["payable_code"] = $invoice["code"];
                $unpaid_payment[$i]["ori_balance"]  = $invoice["balance"];
                $unpaid_payment[$i]["balance"]      = $invoice["balance"];
                $unpaid_payment[$i]["amount"]       = $invoice["amount"];
                $unpaid_payment[$i]["docdate"]      = $invoice["docdate"];
                $i++;
            }
        }*/

        $sql = "select * from deposit where status = 'NEW' and deleted = 0 and balance != 0 and customer_id = ?";
        $deposit_info = bind_pdo($sql, array($_POST["customer_id"]), "selectall");

        foreach ($deposit_info as $deposit) {
            /*$order_info = get_order($deposit["order_id"]);

            if ($order_info["customer_id"] != $_POST["customer_id"])
                continue;*/

            $unpaid_payment[$i]["payable_type"] = "DEPOSIT";
            $unpaid_payment[$i]["payable_type_detail"] = "";
            $unpaid_payment[$i]["payable_id"] = $deposit["id"];
            $unpaid_payment[$i]["payable_code"] = $deposit["code"];
            $unpaid_payment[$i]["ori_balance"] = $deposit["balance"];
            $unpaid_payment[$i]["balance"] = $deposit["balance"];
            $unpaid_payment[$i]["amount"] = $deposit["amount"];
            $unpaid_payment[$i]["docdate"] = $deposit["docdate"];
            $i++;
        }

        $sql = "select * from invoice where (status = 'NEW' or status = 'SENT') and deleted = 0 and balance != 0 and customer_id = ?";
        $invoice_info = bind_pdo($sql, array($_POST["customer_id"]), "selectall");

        foreach ($invoice_info as $invoice) {
            //$order_info = get_order($invoice["order_id"]);

            /*if ($invoice["customer_id"] != $_POST["customer_id"])
                continue;*/

            $unpaid_payment[$i]["payable_type"] = "INVOICE";
            $unpaid_payment[$i]["payable_id"] = $invoice["id"];
            $unpaid_payment[$i]["payable_code"] = $invoice["code"];
            $unpaid_payment[$i]["ori_balance"] = $invoice["balance"];
            $unpaid_payment[$i]["balance"] = $invoice["balance"];
            $unpaid_payment[$i]["amount"] = $invoice["amount"];
            $unpaid_payment[$i]["docdate"] = $invoice["docdate"];
            $i++;
        }

        if (!empty($unpaid_payment)) {
            echo "<div class='red'>* 已選擇的應付款項將加入到這個付款當中</div><br>";

            echo "<table id='unpaid_payment_table'>";
            echo "<tr>";
            echo "<td></td>";
            echo "<td>類別</td>";
            echo "<td>發票編號</td>";
            echo "<td>發票日期</td>";
            echo "<td>金額</td>";
            echo "<td>未付金額</td>";
            echo "<td>是次付款金額</td>";
            echo "</tr>";

            $i = 0;
            foreach ($unpaid_payment as $payment) {

                echo "<tr>";

                $payment_type = get_master_type_code("PAYMENT_TYPE", $payment["payable_type"]);

                echo "<td><input type='checkbox' class='payable_id' name='payable_id[" . $i . "]' value='" . $payment["payable_id"] . "'> <input type='hidden' name='payable_type[" . $i . "]' value='" . $payment["payable_type"] . "'><input type='hidden' name='ori_balance[" . $i . "]' value='" . $payment["ori_balance"] . "' class='ori_balance'> </td>";
                echo "<td>" . $payment_type["name_tc"] . "</td>";

                echo "<td><a href='" . mb_strtolower($payment["payable_type"]) . "_modifyform.php?id=" . $payment["payable_id"] . "' style='text-decoration: underline;' target='_blank'> " . $payment["payable_code"] . "</a></td>";
                echo "<td>" . $payment["docdate"] . "</td>";
                echo "<td>$" . numberformat($payment["amount"]) . "</td>";
                echo "<td>$" . numberformat($payment["balance"]) . "<input type='hidden' class='unpaid_amount' value='" . $payment["balance"] . "' ></td>";

                echo "<td>$<input type='text' class='paid_amount' name='paid_amount[" . $i . "]' style='width:100px;'></td>";
                echo "</tr>";

                $i++;
            }

            echo "<tr>";
            echo "<td colspan='8' style='text-align: right'>已付金額總數: <span id='paid_payment_total'>$0</span></td>";
            echo "</tr>";

            echo "</table>";

        } else {
            echo "沒有應付款項。";
        }
    }
}


if ($_GET["for"] == "get_order_rent_info") {
    $order_id = (int)$_POST["order_id"];

    $order_info = get_order($order_id);

    $rent_start_date = $order_info["warehousing_date"];
    $rent_month = $order_info["rent_month"];

    $sql = "select *, inv_dtl.amount as inv_dtl_amount, inv.code as inv_code from invoice_dtl inv_dtl
							INNER JOIN invoice inv ON inv_dtl.invoice_id = inv.id
							where inv_dtl.type = ? and inv.order_id = ? and inv.status != ? order by month ASC ";
    $parameters = array("RENT", $order_id, "VOID");
    $invoice_info = bind_pdo($sql, $parameters, "selectall");


    if (!empty($invoice_info)) {
        $master_type_code = get_master_type_code("INVOICE_TYPE", "RENT");

        echo "<input type='hidden' id='_original_rent_price' value='" . $order_info["total_retail_price"] . "' >";
        echo "<input type='hidden' id='_actual_rent_price' value='" . $order_info["total_discounted_price"] . "' >";
        echo "<table id='add_invoice_table'>";
        echo "<tr>";
        echo "<td></td>";
        echo "<td>類型</td>";
        echo "<td>月份</td>";
        echo "<td>發票編號</td>";
        echo "<td>租用月份</td>";
        echo "<td>金額</td>";
        //echo "<td>未繳款項</td>";
        echo "<td>繳費日期</td>";
        echo "<td>狀態</td>";
        echo "<td>備註</td>";
        echo "</tr>";

        $num_month = 0;

        for ($i = 1; $i <= $rent_month; $i++) {
            $exist = FALSE;
            //created invoice
            foreach ($invoice_info as $invoice) {

                if ($invoice["month"] == $i) {
                    $exist = TRUE;
                    $num_month++;
                    $invoice_status = get_master_type_code("INVOICE_STATUS", $invoice["status"]);

                    echo "<tr>";
                    echo "<td></td>";
                    echo "<td>" . $master_type_code["name_tc"] . "</td>";
                    echo "<td>第" . $i . "個月</td>";
                    echo "<td><a href='invoice_modifyform.php?id=" . $invoice["invoice_id"] . "' target='_blank' >" . $invoice["inv_code"] . "</a></td>";

                    $start_date = new DateTime($order_info["warehousing_date"]);
                    $start_date->modify('+' . ($i - 1) . ' month');
                    $end_date = new DateTime($order_info["warehousing_date"]);
                    if ($i == $rent_month) {
                        $end_date->modify('+' . $i . ' month - 1 day');
                    } else {
                        $end_date->modify('+' . $i . ' month');
                    }


                    echo "<td>" . $start_date->format('Y-m-d') . " - " . $end_date->format('Y-m-d') . "</td>";
                    echo "<td>$" . numberformat($invoice["inv_dtl_amount"]) . "</td>";
                    //echo "<td>$" . $invoice["balance"] . "</td>";
                    echo "<td>" . $invoice["duedate"] . "</td>";
                    echo "<td>" . $invoice_status["name_tc"] . "</td>";
                    echo "<td>" . nl2br($invoice["remark"]) . "</td>";
                    echo "</tr>";
                }

            }

            if (!$exist) {
                //non-created invoice

                $rent = $order_info["total_discounted_price"];

                echo "<tr>";
                echo "<td><input type='checkbox' class='non_active_invoice' name='rent_month[" . $i . "]' value='" . $i . "'> <input type='hidden' name='rent_amount[" . $i . "]' value='" . $rent . "' > <input type='hidden' name='rent_duedate[" . $i . "]' value='" . Date('Y-m-d', strtotime($rent_start_date . "+" . ($i - 1) . "month")) . "' ></td>";
                echo "<td>" . $master_type_code["name_tc"] . "</td>";
                echo "<td>第" . $i . "個月</td>";
                echo "<td>-</td>";
                $start_date = new DateTime($order_info["warehousing_date"]);
                $start_date->modify('+' . ($i - 1) . ' month');
                $end_date = new DateTime($order_info["warehousing_date"]);
                if ($i == $rent_month) {
                    $end_date->modify('+' . $i . ' month - 1 day');
                } else {
                    $end_date->modify('+' . $i . ' month');
                }

                echo "<td>" . $start_date->format('Y-m-d') . " - " . $end_date->format('Y-m-d') . "</td>";
                echo "<td>$" . numberformat($rent) . "</td>";
                //echo "<td>$" . $rent . "</td>";
                echo "<td>" . Date('Y-m-d', strtotime($rent_start_date . "+" . ($i - 1) . " month")) . "</td>";
                echo "<td>未建立</td>";
                echo "<td><textarea name='rent_remark[" . $i . "]' style='width:200px; height: 50px;'></textarea></td>";

                echo "</tr>";

            }
        }

        //extra rent invoice, non contract
        $sql = "select *, inv_dtl.amount as inv_dtl_amount, inv.code as inv_code from invoice_dtl inv_dtl
							INNER JOIN invoice inv ON inv_dtl.invoice_id = inv.id
							where inv_dtl.type = ? and inv.order_id = ? and inv.status != ? and inv_dtl.month > ? order by month ASC ";
        $parameters = array("RENT", $order_id, "VOID", $rent_month);
        $extra_rent_invoice_info = bind_pdo($sql, $parameters, "selectall");


        if (!empty($extra_rent_invoice_info)) {
            foreach ($extra_rent_invoice_info as $extra_rent_invoice) {
                $invoice_status = get_master_type_code("INVOICE_STATUS", $extra_rent_invoice["status"]);

                $i = $extra_rent_invoice["month"];
                echo "<tr>";
                echo "<td></td>";
                echo "<td>" . $master_type_code["name_tc"] . "</td>";
                echo "<td>第" . $i . "個月</td>";
                echo "<td><a href='invoice_modifyform.php?id=" . $extra_rent_invoice["invoice_id"] . "' target='_blank' >" . $extra_rent_invoice["inv_code"] . "</a></td>";

                $start_date = new DateTime($order_info["warehousing_date"]);
                $start_date->modify('+' . ($i - 1) . ' month');
                $end_date = new DateTime($order_info["warehousing_date"]);
                $end_date->modify('+' . $i . ' month - 1 day');


                echo "<td>" . $start_date->format('Y-m-d') . " - " . $end_date->format('Y-m-d') . "</td>";
                echo "<td>$" . numberformat($extra_rent_invoice["inv_dtl_amount"]) . "</td>";
                //echo "<td>$" . $invoice["balance"] . "</td>";
                echo "<td>" . $extra_rent_invoice["duedate"] . "</td>";
                echo "<td>" . $invoice_status["name_tc"] . "</td>";
                echo "<td>" . nl2br($extra_rent_invoice["remark"]) . "</td>";
                echo "</tr>";
            }
        }

        echo "</table>";

        echo "<br>選擇要新建的租金月份,然後儲存。";
    }
}


if ($_GET["for"] == "floor_plan_rent_room") {

    if ($_POST["go_to_warehousing"] == 1) {

        if (!empty($_SESSION["floor_plan_rent_room"])) {

            $_SESSION["selected_room_tr"] = '<!--<tr id="selected_room_tr">-->
											<td style="vertical-align: middle;">選擇迷你倉</td>

											<td>
												<hr>

												<table border="1" cellpadding="5" style="border-collapse: collapse;">
													<caption class="tl required">已選取倉庫 <button type="button" onclick="window.location.href=\'_ajax.php?for=clear_selected_room\';" style="margin: 0 0 5px 30px;">清空已選取倉庫</button></caption>
													<tbody><tr id="selected_room">
														<td>倉庫編號</td>
														<td>尺寸</td>
														<td>原價(月)</td>
														<td>優惠價(月)</td>
														<td>預繳 / 指定倉庫優惠</td>
														<td>價格</td>
														<td>價格(折扣後)</td>
														<td></td>
													</tr>';


            foreach ($_SESSION["floor_plan_rent_room"] as $selected_room) {
                $room = get_room($selected_room["room_id"]);
                $master_room_info = get_master_room($room["master_room_id"]);
                $master_room_price = get_master_room_price($room["master_room_id"]);

                $location_id = $master_room_info["location_id"];

                //check if this room selected
                $background = "background-color: #ffffff";
                if (!empty($_SESSION["selected_room_tr"])) {

                    if (strpos($_SESSION["selected_room_tr"], $room["room_code"]) !== FALSE) {
                        $background = "background-color: #7EBF3D";
                    }
                }

                $_SESSION["selected_room_tr"] .= "<tr style='" . $background . "' id='" . $room["id"] . "' class='room_id_flag location_" . $location_id . " " . $available_room_selected_class . "'>
					<td>" . $room["code"] . " <input type='hidden' name='room_id[]' value='" . $room["id"] . "'><input type='hidden' name='room_code[]' value='" . $room["code"] . "'><input type='hidden' name='location_id[]' value='" . $location_id . "'></td>
					<td>" . $master_room_info["length"] . "X" . $master_room_info["width"] . "X" . $master_room_info["height"] . " <input type='hidden' name='room_size[]' value='" . ($master_room_info["length"] * $master_room_info["width"]) . "'></td>
					<td>$" . numberformat($master_room_price["retail_price"], 2, "") . " <input type='hidden' name='retail_price[]' value='" . $master_room_price["retail_price"] . "'></td>
				";

                //need to check if master room discount exist, if exist -> discounted price -> master room discount
                if ($nowdate >= $master_room_price["effectivedate_from"] && $nowdate <= $master_room_price["effectivedate_to"]) {
                    //discounted price valid
                    $_SESSION["selected_room_tr"] .= "<td>$" . $master_room_price["discounted_price"] . " </td>";
                    $set_discounted_price = $master_room_price["discounted_price"];
                } else {
                    $_SESSION["selected_room_tr"] .= "<td>N/A </td>";
                    $set_discounted_price = $master_room_price["retail_price"];
                }

                $discounted_price = $set_discounted_price;
                //check if this kind of room has prepaid / specific discount
                $sql = "select *, discount.id as master_room_discount_id from master_room_discount discount INNER JOIN master_type_code mtcode ON discount.payment_term = mtcode.code where discount.master_room_id = ? and discount.deleted = ? and mtcode.typeid = ? and ? >= effectivedate_from and ? <= effectivedate_to";
                $parameters = array($_POST["master_room_id"], 0, "PAYMENT_TERM", date("Y-m-d"), date("Y-m-d"));
                $master_room_discount_info = bind_pdo($sql, $parameters, "selectall");
                $discount_message = "";
                foreach ($master_room_discount_info as $master_room_discount) {
                    //$discount_message .= $master_room_discount["name_tc"] . " -" . $master_room_discount["discount"] . $master_room_discount["discount_type"] . " / ";
                    if ($master_room_discount["payment_term"] == "SPECIFIC_SIZE") {
                        $class = "SPECIFIC_SIZE_DISCOUNT _DISCOUNT DISCOUNT_ID_" . $master_room_discount["master_room_discount_id"];
                        $discounted_price = $set_discounted_price * (100 - $master_room_discount["discount"]) / 100;
                    } else {
                        $class = "PREPAID_DISCOUNT _DISCOUNT DISCOUNT_ID_" . $master_room_discount["master_room_discount_id"];
                        $discounted_price = $set_discounted_price;
                    }

                    $discount_message .= $master_room_discount["name_tc"] . " <input type='text' id='MONTH_" . $master_room_discount["config_value"] . "' value='" . $master_room_discount["discount"] . "' style='width: 40px;' class='" . $class . "' onkeyup=\"update_discounted_price($(this).closest('tr').attr('id'));data_calculation($(this).attr('class'));\"> % 折扣 / </br>";

                }
                $discount_message = substr_replace($discount_message, "", -7);

                $_SESSION["selected_room_tr"] .= "<td>" . $discount_message . "</td>";

                $_SESSION["selected_room_tr"] .= "<td>
						<input type='text' name='set_discounted_price[]' class='set_discounted_price' value='" . numberformat($set_discounted_price, 2, "") . "' style='width: 70px;' onkeyup=\"update_discounted_price($(this).closest('tr').attr('id'));data_calculation($(this).attr('class'));\">
					</td>";

                $_SESSION["selected_room_tr"] .= "<td>
						<input type='text' name='discounted_price[]' class='discounted_price' value='" . numberformat($discounted_price, 2, "") . "' style='width: 70px;' readonly>
					</td>";

                $_SESSION["selected_room_tr"] .= "<td class='select_room_td'>
						<button type='button' class='delete_room' onclick='delete_room(this," . $room["id"] . ")'>刪除</button>
					</td>";

                $_SESSION["selected_room_tr"] .= "</tr>";
            }


            $_SESSION["selected_room_tr"] .= '<!-- display selected room here -->
												</tbody></table>

											</td>
										<!--</tr>-->';
        }

    } else {

        if (!isset($_SESSION["floor_plan_rent_room"])) {
            $_SESSION["floor_plan_rent_room"] = array();
        }

        $found = FALSE;
        foreach ($_SESSION["floor_plan_rent_room"] as $selected_room) {
            if ($selected_room["room_id"] == $_POST["room_id"]) {
                $found = TRUE;
            }
        }

        if (!$found) {
            $_SESSION["floor_plan_rent_room"][] = array("room_id" => $_POST["room_id"], "master_room_id" => $_POST["master_room_id"], "room_code" => $_POST["room_code"]);
        }

        echo "success";
    }
}

if ($_GET["for"] == "floor_plan_delete_room") {
    $room_id = $_POST["room_id"];

    if (isset($_SESSION["floor_plan_rent_room"])) {
        foreach ($_SESSION["floor_plan_rent_room"] as $key => $selected_room) {
            if ($selected_room["room_id"] == $_POST["room_id"]) {
                unset($_SESSION["floor_plan_rent_room"][$key]);
            }
        }
    }
}

if ($_GET["for"] == "update_room") {
    if (!empty($_POST["room_id"])) {
        //update record
        $sql = "update room set status = ?, master_room_id = ?, lastupby = ?, lastupdate = ? where id = ?";
        $parameters = array($_POST["room_status"], $_POST["master_room_id"], $_SESSION['cmsloginid'], $nowdate, $_POST["room_id"]);
        bind_pdo($sql, $parameters);

        activity_log('_ajax.php', 'update_room', $input = array('post' => $_POST, 'session' => $_SESSION, 'sql' => $sql, 'parameters' => $parameters), $data_before = array(), $data_after = array(), $step = 1);

        update_master_room_total_number();

        echo "倉庫單位: " . $_POST["room_code"] . " 已更新。";
    } else {
        echo "倉庫單位: " . $_POST["room_code"] . " 未能更新。";
    }
}

if ($_GET["for"] == "check_room_contract") {
    if (!empty($_POST["room_id"])) {

        $sql = "select `order`.status, `order`.enddate, `order`.code from `room` 
            inner join `order_room` on `order_room`.room_id = `room`.id 
            inner join `order` on `order`.id = `order_room`.order_id 
            where `order_room`.room_id = ? and `order`.enddate > ? and `order`.status = ? and `room`.status = ?
            order by `order_room`.id DESC limit 1";
        $parameters = array($_POST["room_id"], date('Y-m-d'), 'NEW', 'RENTED');
        $result = bind_pdo($sql, $parameters, 'selectone');

        if (!empty($result)) {
            echo "倉庫單位: " . $_POST["room_code"] . " 已被合約 " . $result['code'] . " 佔用,請小心操作此單位。";
        }
    } else {
        echo "找不到倉庫單位。";
    }
}

if ($_GET["for"] == "order_discount") {

    //debug_log(123, $_POST);
    $prepaid_month = $_POST["prepaid_month"];

    $selected_room_id_list = $_POST["selected_room_id_list"];
    $selected_room_id_list = explode(",", $selected_room_id_list);

    $selected_room_discount_id_list = $_POST["selected_room_discount_id_list"];
    $selected_room_discount_id_list = explode(",", $selected_room_discount_id_list);

    $selected_room_discount_id_value_list = $_POST["selected_room_discount_id_value_list"];
    $selected_room_discount_id_value_list = explode(",", $selected_room_discount_id_value_list);

    $selected_room_discounted_price_list = $_POST["selected_room_discounted_price_list"];
    $selected_room_discounted_price_list = explode(",", $selected_room_discounted_price_list);

    $selected_room_price_list = $_POST["selected_room_price_list"];
    $selected_room_price_list = explode(",", $selected_room_price_list);

    $selected_room_prepaid_month_list = $_POST["selected_room_prepaid_month_list"];
    $selected_room_prepaid_month_list = explode(",", $selected_room_prepaid_month_list);


    $j = 0;
    $discount_info = array();


    arsort($selected_room_prepaid_month_list);

    foreach ($selected_room_prepaid_month_list as $key => $prepaid_month_list) {
        $selected_room_id_list_ordered[] = $selected_room_id_list[$key];
        $selected_room_discount_id_list_ordered[] = $selected_room_discount_id_list[$key];
        $selected_room_discount_id_value_list_ordered[] = $selected_room_discount_id_value_list[$key];
        $selected_room_discounted_price_list_ordered[] = $selected_room_discounted_price_list[$key];
        $selected_room_price_list_ordered[] = $selected_room_price_list[$key];
        $selected_room_prepaid_month_list_ordered[] = $selected_room_prepaid_month_list[$key];
    }

    if (!empty($selected_room_discount_id_list)) {
        foreach ($selected_room_discount_id_list as $key => $discount_id) {

            $valid_discount = FALSE;

            $token = explode("DISCOUNT_ID_", $selected_room_discount_id_list_ordered[$key]);
            if (empty($token[1]))
                continue;

            $this_discount_id = $token[1];

            $room_info = get_room($selected_room_id_list_ordered[$key]);
            $master_room_discount_info = get_master_room_discount($this_discount_id);


            foreach ($discount_info as $discount) {
                if ($discount["payment_term"] != "SPECIFIC_SIZE" && $selected_room_id_list_ordered[$key] == $discount["room_id"]) {
                    continue 2;
                }

                if ($discount["payment_term"] == "SPECIFIC_SIZE" && $selected_room_id_list_ordered[$key] == $discount["room_id"] && $discount["is_final_discount"] == 1) {
                    continue 2;
                }
            }


            if ($master_room_discount_info["payment_term"] != "SPECIFIC_SIZE") {
                //prepaid discount
                $token2 = explode("MONTH_", $selected_room_prepaid_month_list_ordered[$key]);
                $this_prepaid_month = $token2[1];
                //TODO:: must match month?
                //if ($prepaid_month >= $this_prepaid_month && !empty($prepaid_month)) {
                if ($prepaid_month == $this_prepaid_month && !empty($prepaid_month)) {
                    $valid_discount = TRUE;
                }

            } else {
                //specific size discount
                $valid_discount = TRUE;
            }

            if ($valid_discount) {
                $discount_info[$j]["master_room_discount_id"] = $this_discount_id;
                $discount_info[$j]["master_room_id"] = $master_room_discount_info["master_room_id"];
                $discount_info[$j]["discount_type"] = $master_room_discount_info["discount_type"];

                $discount_info[$j]["price"] = $selected_room_price_list_ordered[$key];
                $discount_info[$j]["disocunted_price"] = $selected_room_discounted_price_list_ordered[$key];
                $discount_info[$j]["discount"] = $selected_room_discount_id_value_list_ordered[$key];

                $deduct = $selected_room_price_list_ordered[$key] * $selected_room_discount_id_value_list_ordered[$key] / 100;
                $discount_info[$j]["monthly_deduct"] = $deduct;

                $discount_info[$j]["room_id"] = $selected_room_id_list_ordered[$key];
                $discount_info[$j]["is_final_discount"] = $master_room_discount_info["is_final_discount"];
                $discount_info[$j]["payment_term"] = $master_room_discount_info["payment_term"];

                $j++;
            }

        }

        //record discount info into session
        $_SESSION["warehousing"][2]["order_discount"] = $discount_info;
    } else {
        $_SESSION["warehousing"][2]["order_discount"] = "";
    }

    //var_dump($_SESSION["warehousing"][2]["order_discount"]);
}

if ($_GET["for"] == "overdue_email") {
    echo overdue_email($_POST["order_id"], $_POST["type_code"], $_POST["invoice_id"]);
}

if ($_GET["for"] == "invoice_email") {
    invoice_email($_GET["type"], $_GET["invoice_id"]);
}

/* -- done in terminate_order_modify 29/09/2022
if ($_GET["for"] == "terminate_order_penalty") { //create a penalty invoice of less than 14 announcement

    $order_info = get_order((int)$_GET["order_id"]);

    // check if withdraw_remuneration exist
    if (is_numeric($order_info["withdraw_remuneration"])){
        echo "<script>alert('WARNING: 已存在退倉賠償紀錄。'); window.close()</script>";
        exit;
    }

    //generate a invoice, need to update when sum the total
    $sql = "select max(id) as max_id from invoice";
    $new_invoice_code_num = bind_pdo($sql, NULL, "selectone");
    $location_info = get_location($order_info["location_id"]);
    $invoice_code = $location_info["location_code"] . "-I" . date("y") . str_pad($new_invoice_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);

    $amount = $_GET["penalty_amount"];    
    $duedate = $_GET["duedate"];
    //$amount = numberformat($order_info["total_discounted_price"] / 30 * 14);
    $data = array(
        "order_id" => (int)$_GET["order_id"],
        "customer_id" => $order_info["customer_id"],
        "status" => "SENT",
        "code" => $invoice_code,
        "amount" => $amount,
        "balance" => $amount,
        "docdate" => $nowdate,
        "duedate" => $duedate,
        "createby" => $_SESSION['cmsloginid'],
        "createdate" => $nowdate,
        "lastupby" => $_SESSION['cmsloginid'],
        "lastupdate" => $nowdate
    );
    $result = insert_record("invoice", $data);
    $invoice_id = $dbh->lastInsertId();

    $data = array(
        "invoice_id" => $invoice_id,
        "type" => "PENALTY",
        "price" => $amount,
        "uom_price" => "PCS",
        "qty" => "1",
        "amount" => $amount,
        "remark" => "退倉通知賠償",
        "createby" => $_SESSION['cmsloginid'],
        "createdate" => $nowdate,
        "lastupby" => $_SESSION['cmsloginid'],
        "lastupdate" => $nowdate
    );
    $result = insert_record("invoice_dtl", $data);


    
    if ($result && $invoice_id > 0) {
        // echo "<script>alert('退倉賠償賬單已建立。'); history.back();</>";
        echo "<script>alert('賬單已建立。'); window.close();</>";
        exit;
    } else {
        // echo "<script>alert('退倉賠償賬單未能建立。'); history.back();</script>";
        echo "<script>alert('賬單未能建立。'); window.close();</script>";
        exit;
    }

}
*/

if ($_GET["for"] == "add_extra_rent_invoice") {

    $message = "";
    if (empty($_POST["order_id"])) {
        $message .= "缺少必須的資料,請重新嘗試。\\n\\n";
    }

    if (!is_numeric($_POST["extra_rent_invoice_amount"]) || (float)$_POST["extra_rent_invoice_amount"] <= 0) {
        $message .= "請輸入正確的非合約期月份租金。\\n\\n";
    }

    if (!empty($message)) {
        /*echo "<script>alert('" . $message . "'); history.back();</script>";
        exit;*/

        echo $message;
    } else {
        $order_id = (int)$_POST["order_id"];
        $order_info = get_order($order_id);

        $sql = "select * from invoice inv INNER JOIN invoice_dtl dtl ON inv.id = dtl.invoice_id where inv.order_id = ? order by month DESC limit 1";
        $parameters = array($order_id);
        $invoice_info = bind_pdo($sql, $parameters, "selectone");

        if (!empty($invoice_info)) {
            //if many location active, need to update this
            $location_info = get_location(1);

            $sql = "select max(id) as max_id from invoice";
            $new_invoice_code_num = bind_pdo($sql, NULL, "selectone");
            $invoice_code = $location_info["location_code"] . "-I" . date("y") . str_pad($new_invoice_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);

            $date = new DateTime($order_info["warehousing_date"]);
            $date->modify('+' . ((int)$invoice_info["month"]) . ' month');

            $data = array(
                "code" => $invoice_code,
                "order_id" => $order_id,
                "customer_id" => $order_info["customer_id"],
                "status" => "SENT",
                "amount" => $_POST["extra_rent_invoice_amount"],
                "balance" => $_POST["extra_rent_invoice_amount"],
                "docdate" => $nowdate,
                "duedate" => $date->format('Y-m-d'),
                "createby" => $_SESSION['cmsloginid'],
                "createdate" => $nowdate,
                "lastupby" => $_SESSION['cmsloginid'],
                "lastupdate" => $nowdate
            );

            $result = insert_record("invoice", $data);
            $rent_invoice_id = $dbh->lastInsertId();

            //insert rent invoice detail
            $data = array(
                "invoice_id" => $rent_invoice_id,
                "type" => "RENT",
                "remark" => "非合約期月份",
                "month" => ($invoice_info["month"] + 1),
                "price" => $_POST["extra_rent_invoice_amount"],
                "uom_price" => "MO",
                "qty" => "1",
                "amount" => $_POST["extra_rent_invoice_amount"] * 1,
                "createby" => $_SESSION['cmsloginid'],
                "createdate" => $nowdate,
                "lastupby" => $_SESSION['cmsloginid'],
                "lastupdate" => $nowdate
            );

            $result = insert_record("invoice_dtl", $data);

            /*echo "<script>alert('成功新增非合約期月份。'); location.href='invoice_modifyform.php?id=" . $rent_invoice_id . "';</script>";
            exit;*/
            //echo "success";
            echo $rent_invoice_id;
        } else {
            //error
            $message = "這張合約出現問題,請先檢查合約是否有效。\\n\\n";

            /*echo "<script>alert('" . $message . "'); history.back();</script>";
            exit;*/
            echo $message;
        }
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit