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
|
<?php require_once("check_login.php");
$message = ""; if (empty($_POST["invoice_type"])) { $message .= "請選擇發票類型。\\n\\n"; }
if (empty($_POST["duedate"])) { $message .= "請輸入繳費日期。\\n\\n"; }else{ if(!validateDate($_POST["duedate"], "Y-m-d")){ $message .= "請輸入正確的繳費日期。\\n\\n"; } }
if(!empty($message)){ echo "<script>alert('".$message."'); history.back();</script>"; exit; }
if (!empty($_POST["invoice_type"])) { //$_SESSION["invoice_type"] = $_POST["invoice_type"];
//if many location active, need to update this $location_info = get_location(1);
$new_invoice_id = "";
if ($_POST["invoice_type"] == "RENT") {
if (!empty($_POST["rent_month"])) { //$_SESSION["order_id"] = $_POST["order_id"];
$order_info = get_order((int)$_POST["order_id"]);
$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);
$data = array( "code" => $invoice_code, "order_id" => (int)$_POST["order_id"], "customer_id" => $order_info["customer_id"], "status" => "SENT", "remark" => $_POST["invoice_remark"], "docdate" => $nowdate, "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice", $data); $rent_invoice_id = $dbh->lastInsertId(); $new_invoice_id = $rent_invoice_id;
$total_invoice_amount = 0;
$i = 0; foreach ($_POST["rent_month"] as $month) { if ($i == 0) { $invoice_duedate = $_POST["rent_duedate"][$month]; $i++; }
$total_invoice_amount += $_POST["rent_amount"][$month];
//insert rent invoice detail $data = array( "invoice_id" => $rent_invoice_id, "type" => $_POST["invoice_type"], "remark" => $_POST["rent_remark"][$month], "month" => $month, "price" => $_POST["rent_amount"][$month], "uom_price" => "MO", "qty" => "1", "amount" => $_POST["rent_amount"][$month] * 1, "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice_dtl", $data);
/*unset($_SESSION["invoice_type"]); unset($_SESSION["order_id"]);*/ }
//update invoice amount $sql = "update invoice set amount = ?, balance = ?, duedate = ? where id = ?"; //$parameters = array($total_invoice_amount, $total_invoice_amount, $invoice_duedate, $new_invoice_id); $parameters = array($total_invoice_amount, $total_invoice_amount, $_POST["duedate"], $new_invoice_id); $result = bind_pdo($sql, $parameters);
} else { echo "<script>alert('請選擇要建立的租金月份發票。'); history.back();</script>"; exit; } }
if ($_POST["invoice_type"] == "ORDER_PRODUCT") { if (!empty($_POST["product_id"]) && !empty($_POST["customer_id"])) { $order_product_total = 0;
foreach ($_POST["product_id"] as $product_id) { if (is_numeric($_POST["qty"][$product_id]) && (int)$_POST["qty"][$product_id] > 0) {
$order_product_total += $_POST["price"][$product_id] * $_POST["qty"][$product_id]; } }
if ($order_product_total == 0) { echo "<script>alert('請選擇要購買的物品。'); history.back();</script>"; exit; } //insert order product invoice $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); $data = array( "order_id" => "", "customer_id" => (int)$_POST["customer_id"], "status" => "SENT", "code" => $invoice_code, "amount" => $order_product_total, "balance" => $order_product_total, "docdate" => $nowdate, "duedate" => $_POST["duedate"], "remark" => $_POST["invoice_remark"], "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice", $data); $order_product_invoice_id = $dbh->lastInsertId(); $new_invoice_id = $order_product_invoice_id;
foreach ($_POST["product_id"] as $product_id) { if (is_numeric($_POST["qty"][$product_id]) && (int)$_POST["qty"][$product_id] > 0) {
$data = array( "customer_id" => (int)$_POST["customer_id"], "product_id" => $product_id, "qty" => $_POST["qty"][$product_id], "price" => $_POST["price"][$product_id], "amount" => $_POST["price"][$product_id] * $_POST["qty"][$product_id], "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate, );
$result = insert_record("order_product", $data);
//insert order product invoice detail $data = array( "invoice_id" => $order_product_invoice_id, "type" => $_POST["invoice_type"], //"remark" => $_POST["remark"], "price" => $_POST["price"][$product_id], "uom_price" => "PCS", "qty" => $_POST["qty"][$product_id], "amount" => $_POST["price"][$product_id] * $_POST["qty"][$product_id], "order_dtl_id" => $product_id, "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice_dtl", $data);
} } } else { echo "<script>alert('請選擇客戶及選購物品。'); history.back();</script>"; exit; } }
if ($_POST["invoice_type"] == "PENALTY") {
$message = ""; if (!isset($_POST["invoice_belong"])) { $message .= "請選擇發票方式。\\n\\n"; } else { if ($_POST["invoice_belong"] == "NEW") { if (empty($_POST["customer_id"])) { $message .= "請選擇客戶\\n\\n"; } }
if ($_POST["invoice_belong"] == "OLD") { if (empty($_POST["invoice_id"])) { $message .= "請選擇租金發票\\n\\n"; } }
}
if (empty($_POST["penalty_amount"]) || !is_numeric($_POST["penalty_amount"]) || $_POST["penalty_amount"] < 0) { $message .= "請輸入正確的金額。\\n\\n"; }
if ($message != "") { echo "<script>alert('" . $message . "'); history.back();</script>"; exit; } else { if ($_POST["invoice_belong"] == "NEW") { //insert penalty invoice $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); $data = array( "order_id" => "", "customer_id" => (int)$_POST["customer_id"], "status" => "SENT", "code" => $invoice_code, "amount" => $_POST["penalty_amount"], "balance" => $_POST["penalty_amount"], "docdate" => $nowdate, "duedate" => $_POST["duedate"], "remark" => $_POST["invoice_remark"], "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice", $data); $penalty_invoice_id = $dbh->lastInsertId(); $new_invoice_id = $penalty_invoice_id;
//insert order product invoice detail $data = array( "invoice_id" => $penalty_invoice_id, "type" => $_POST["invoice_type"], "remark" => $_POST["remark"], "price" => $_POST["penalty_amount"], "uom_price" => "PCS", "qty" => 1, "amount" => $_POST["penalty_amount"] * 1, "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice_dtl", $data); }
if ($_POST["invoice_belong"] == "OLD") { //insert order product invoice detail $data = array( "invoice_id" => (int)$_POST["invoice_id"], "type" => $_POST["invoice_type"], "remark" => $_POST["remark"], "price" => $_POST["penalty_amount"], "uom_price" => "PCS", "qty" => 1, "amount" => $_POST["penalty_amount"] * 1, "createby" => $_SESSION['cmsloginid'], "createdate" => $nowdate, "lastupby" => $_SESSION['cmsloginid'], "lastupdate" => $nowdate );
$result = insert_record("invoice_dtl", $data);
//update exist invoice $sql = "update invoice set amount = amount + ?, balance = balance + ?, lastupby = ?, lastupdate = ? where id = ?"; $parameters = array($_POST["penalty_amount"], $_POST["penalty_amount"], $_SESSION["cmsloginid"], $nowdate, (int)$_POST["invoice_id"]); $result = bind_pdo($sql, $parameters);
$new_invoice_id = (int)$_POST["invoice_id"]; } } }
header("Location: invoice_modifyform.php?id=" . $new_invoice_id); } else { echo "<script>alert('請選擇發票類型。'); history.back();</script>"; exit; }
|