beginTransaction();
if(!isset($_POST["paymethod"])){
$_POST["paymethod"] = "CASH";
}
// create withdrawal invoice
if(!$order_info["withdraw_date"]){
$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 = (int)$_POST["penalty_amount"];
$duedate = $_POST["withdraw_date"];
// $status = "SENT";
$data = array(
"order_id" => $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);
}
// get all deposit & invoice detail
if($order_info['renew']) {
$deposit_info = get_order_deposit($order_info['first_order_id']);
if ($deposit_info['deposit_status'] !== "PAID") {
$deposit_info = NULL;
}
} else {
$deposit_info = get_order_deposit($order_id);
}
$invoice_info = get_order_invoice($order_id);
//$amount = numberformat($order_info["total_discounted_price"] / 30 * 14);
$original_deposit_amount = $deposit_info["deposit_amount"];
//update deposit balance
$sql = "update deposit set balance=?, status=?, actual_returnamount= ?, lastupby=?, lastupdate=? where id=?";
$parameters = array(0, "RETURNED", $_POST["return_deposit"], $_SESSION["cmsloginid"], $nowdate, $deposit_info["deposit_id"]);
bind_pdo($sql, $parameters);
//update invoice balance
if (!$deposit_info["deposit_id"]) { // assume client clear the debt if no deposit record
foreach ($invoice_info as $invoice) {
$pay_by_deposit = $original_deposit_amount;
$this_invoice_balance = 0;
$original_deposit_amount = 0;
$sql = "update invoice set balance=?, status=?, lastupby=?, lastupdate=?, deduct_by_deposit_id=?, deduct_by_deposit_amount=? where id=? and status != ?";
$parameters = array($this_invoice_balance, "SETTLED", $_SESSION["cmsloginid"], $nowdate, 0, 0, $invoice["invoice_id"], "SETTLED");
bind_pdo($sql, $parameters);
}
} else {
foreach ($invoice_info as $invoice) {
if($original_deposit_amount > 0){
//deposit can full pay this invoice
if($original_deposit_amount >= $invoice["invoice_balance"]){
$pay_by_deposit = $invoice["invoice_balance"];
$this_invoice_balance = 0;
$original_deposit_amount -= $invoice["invoice_balance"];
} else {
$pay_by_deposit = $original_deposit_amount;
$this_invoice_balance = $invoice["invoice_balance"] - $original_deposit_amount;
$original_deposit_amount = 0;
}
$sql = "update invoice set balance=?, status=?, lastupby=?, lastupdate=?, deduct_by_deposit_id=?, deduct_by_deposit_amount=? where id=? and status != ?";
$parameters = array($this_invoice_balance, "SETTLED", $_SESSION["cmsloginid"], $nowdate, $deposit_info["deposit_id"], $pay_by_deposit, $invoice["invoice_id"], "SETTLED");
bind_pdo($sql, $parameters);
}
}
}
//check the total remain amount
if ((int)$_POST["real_int_total_remain_amount"] > 0) {
$pay_by_deposit = 0;
if (isset($_POST["pay_by_deposit"]) && $_POST["pay_by_deposit"] > 0) {
$pay_by_deposit = $_POST["pay_by_deposit"];
}
$total_payment_amount = ((int)$_POST["real_int_total_remain_amount"] + $pay_by_deposit);
if ($_POST["paymethod"] == "CHEQUE") {
$bank_name = $_POST["bank_name"];
$cheque_num = $_POST["cheque_num"];
} else {
$bank_name = "";
$cheque_num = "";
}
if ($_POST["paymethod"] == "OTHER") {
$paymethod_other = $_POST["paymethod_other"];
} else {
$paymethod_other = "";
}
//create a payment
//PAYMENT for this invoice
if(isset($_POST["pay_by_deposit"]) && $_POST["pay_by_deposit"] > 0){
$remark = "使用按金($".numberformat($_POST["pay_by_deposit"]).")支付部份款項。";
}else{
$remark = "退倉付款。";
}
$sql = "select max(id) as max_id from payment";
$new_payment_code_num = bind_pdo($sql, NULL, "selectone");
$payment_code = "P" . date("y") . str_pad($new_payment_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);
$data = array(
"customer_id" => $order_info["customer_id"],
"paymethod" => $_POST["paymethod"],
"bank_name" => $bank_name,
"cheque_num" => $cheque_num,
"paymethod_other" => $paymethod_other,
"status" => "PAID",
"code" => $payment_code,
"docdate" => $nowdate,
"amount" => (int)$_POST["real_int_total_remain_amount"],
"remark" => $remark,
"createby" => $_SESSION['cmsloginid'],
"createdate" => $nowdate,
"lastupby" => $_SESSION['cmsloginid'],
"lastupdate" => $nowdate,
);
$result = insert_record("payment", $data);
$payment_id = $dbh->lastInsertId();
//payment_dtl
//pay deposit
$total_payment_amount -= $deposit_info["deposit_balance"];
if ($deposit_info["deposit_balance"] > 0) {
$data = array(
"payment_id" => $payment_id,
"payable_type" => "DEPOSIT",
"payable_id" => $deposit_info["deposit_id"],
"status" => "PAID",
"docdate" => $nowdate,
"amount" => $deposit_info["deposit_balance"],
"createby" => $_SESSION['cmsloginid'],
"createdate" => $nowdate,
"lastupby" => $_SESSION['cmsloginid'],
"lastupdate" => $nowdate,
);
$result = insert_record("payment_dtl", $data);
$deposit_id = $deposit_info["deposit_id"];
}
//pay invoice
foreach ($invoice_info as $invoice) {
if($invoice["invoice_balance"] > 0){
$total_payment_amount -= $invoice["invoice_balance"];
$data = array(
"payment_id" => $payment_id,
"payable_type" => "INVOICE",
"payable_id" => $invoice["invoice_id"],
"status" => "PAID",
"docdate" => $nowdate,
"amount" => $invoice["invoice_balance"],
"createby" => $_SESSION['cmsloginid'],
"createdate" => $nowdate,
"lastupby" => $_SESSION['cmsloginid'],
"lastupdate" => $nowdate,
);
$result = insert_record("payment_dtl", $data);
}
}
if ($total_payment_amount != 0) {
echo "";
exit;
// header("Location: terminate_order.php?order_id=" . $order_id);
// exit;
} else {
$_datetime = new DateTime();
$order_enddate = new DateTime($order_info["enddate"]);
if($_datetime->format("Y-m-d") >= $order_enddate->format("Y-m-d")) {
$order_status = "COMPLETED";
}else{
$order_status = "TERMINATION";
}
$sql = "update `order` set status = ?, withdraw_date = ?, withdraw_remuneration = ?, withdraw_process_date = ? where id = ?";
// $parameters = array($order_status, $nowdate, $order_id);
$parameters = array($order_status, $_POST[withdraw_date], $_POST[penalty_amount], date("Y-m-d"), $order_id);
$result = bind_pdo($sql, $parameters);
}
} else {
//no need to created payment
$_datetime = new DateTime();
$order_enddate = new DateTime($order_info["enddate"]);
if($_datetime->format("Y-m-d") >= $order_enddate->format("Y-m-d")) {
$order_status = "COMPLETED";
}else{
$order_status = "TERMINATION";
}
$sql = "update `order` set status = ?, withdraw_date = ?, withdraw_remuneration = ?, withdraw_process_date = ? where id = ?";
// $parameters = array($order_status, $nowdate, $order_id);
$parameters = array($order_status, $_POST[withdraw_date], $_POST[penalty_amount], date("Y-m-d"), $order_id);
$result = bind_pdo($sql, $parameters);
}
$dbh->commit();
if ($deposit_info["deposit_id"]) {
header("Location: deposit_modifyform.php?id=" . $deposit_info["deposit_id"]);
} else {
header("Location: deposit_modifyform_no_deposit.php?id=" . $order_id);
}
//header("Location: deposit_modifyform.php?id=" . (int)$deposit_id);
//header("Location: order_modifyform.php?order_id=" . $order_id);
} catch (Exception $e) {
$dbh->rollBack();
echo $e->getMessage();
echo var_dump($e);
// echo "";
// header("Location: order_modifyform.php?id=" . $order_id);
}
} else {
echo "";
exit;
}