1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
<?php require_once('check_login.php');
if (!empty($_POST["order_id"])) { global $dbh;
try {
$dbh->beginTransaction();
if(!isset($_POST["paymethod"])){ $_POST["paymethod"] = "CASH"; }
$order_id = (int)$_POST["order_id"];
$order_info = get_order($order_id); $deposit_info = get_order_deposit($order_id); $invoice_info = get_order_invoice($order_id);
$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 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 ($_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 = ($_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" => $_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 "<script>alert('計算出錯,請再嘗試。'); history.back();</script>"; 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 = ? where id = ?"; $parameters = array($order_status, $nowdate, $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 = ? where id = ?"; $parameters = array($order_status, $nowdate, $order_id); $result = bind_pdo($sql, $parameters); }
$dbh->commit(); 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();
header("Location: order_modifyform.php?id=" . $order_id); }
} else { echo "<script>alert('缺少必須填寫的資料,請再嘗試。'); history.back();</script>"; exit; }
|