/var/www/hkosl.com/innoutstorage/webadmin/terminate_order_modify.php


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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
    
    
require_once('check_login.php');

    if (!empty(
$_POST["order_id"])) {
        global 
$dbh;

        
$order_id = (int)$_POST["order_id"];
        
// get order detail
        
$order_info   get_order($order_id);

        try {

            
$dbh->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($sqlNULL"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"] + 14"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"], $nowdate00$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($sqlNULL"selectone");

                
$payment_code "P" date("y") . str_pad($new_payment_code_num["max_id"] + 14"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 
"<script>
                                alert('計算出錯,請再嘗試。'"
.$total_payment_amount."); 
                                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 = ?, 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 "<script>alert('Error');</script>";

            // header("Location: order_modifyform.php?id=" . $order_id);
        
}


    } else {
        echo 
"<script>alert('缺少必須填寫的資料,請再嘗試。'); history.back();</script>";
        exit;
    }