/var/www/hkosl.com/innoutstorage2019/webadmin/invoice_addform.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
    
require_once('check_login.php');

    
$invoice_type_info  get_master_type_code("INVOICE_TYPE");
    
$order_info get_order();
    
$customer_info get_customer();
    
$product_info get_product();
    
$invoice_info get_invoice();

    
$default_duedate = new DateTime();
    
$default_duedate->modify("+7 day");

?>
<!DOCTYPE html>
<html>
<head>
    <?php require_once("html_head.php"); ?>

    <script type="text/javascript" src="js/chosen_v1.4.1/chosen.jquery.min.js"></script>
    <link rel="stylesheet" href="js/chosen_v1.4.1/chosen.min.css">

    <script type="text/javascript">
        $(function () {
            $("#duedate").datepicker({dateFormat: 'yy-mm-dd'});
        });
    </script>

    <script type="text/javascript">

        function get_order_rent_info(order_id) {

            if(parseInt(order_id) > 0){
                var xmlhttp;

                if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                        var result = $.trim(xmlhttp.responseText);

                        //console.log(1);
                        if (result == "" || result == null) {
                            $("#ajax_order_rent_info").hide();
                        } else {
                            $("#ajax_order_rent_info").show().html(result);
                            extra_rent_invoice();

                            var original_rent_price = $("#_original_rent_price").val();
                            var actual_rent_price = $("#_actual_rent_price").val();
                            $("#original_rent_price").text(original_rent_price);
                            $("#actual_rent_price").text(actual_rent_price);
                            $("#extra_rent_invoice_amount").val(actual_rent_price);
                        }

                    }
                }
                xmlhttp.open("POST", "_ajax.php?for=get_order_rent_info", true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.send("order_id=" + order_id);
            }


        }

        function extra_rent_invoice(){
            //check if all rent invoice created
            if($(".non_active_invoice").length <= 0 && $("#add_invoice_table").length > 0){
                $("#extra_rent_invoice").show();
            }else{
                $("#extra_rent_invoice").hide();
            }
        }

        function ajax_add_extra_rent_invoice(){

            var extra_rent_invoice_amount = $("#extra_rent_invoice_amount").val();

            if(!isNaN(extra_rent_invoice_amount) && parseFloat(extra_rent_invoice_amount) > 0){

                var order_id = $("#order_id").val();

                var xmlhttp;

                if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                        var result = $.trim(xmlhttp.responseText);

                        if(!isNaN(result) && parseInt(result) > 0){
                            alert("成功新增非合約期月份。");
                            location.href='invoice_modifyform.php?id='+result;
                        }else{
                            alert(result);
                        }
                    }
                }
                xmlhttp.open("POST", "_ajax.php?for=add_extra_rent_invoice", true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.send("order_id=" + order_id + "&extra_rent_invoice_amount=" +extra_rent_invoice_amount);
            }else{
                alert("請輸入正確的非合約期月份租金。");
            }
        }

        function invoice_type(type_name){


            if(type_name == "RENT"){
                $(".RENT").show();
                //$(".RENT input, .RENT select, .RENT textarea").removeAttr("disabled");
                $(".non_RENT").hide();
                //$(".non_RENT input, .non_RENT select, .non_RENT textarea ").attr("disabled", "disabled");

                $("#duedate").val("");

                //check if all rent invoice created
                extra_rent_invoice();
            }else{
                $(".RENT").hide();
                $(".non_RENT").show();

                $("#duedate").val("<?=$default_duedate->format("Y-m-d")?>");

                if(type_name == "ORDER_PRODUCT"){
                    $(".PENALTY").hide();
                    $(".ORDER_PRODUCT").show();
                }else if(type_name == "PENALTY"){  //PENALTY

                    $(".ORDER_PRODUCT").hide();
                    $(".PENALTY").show();
                    $("#customer_tr").show();
                }else{
                    $(".RENT").hide();
                    $(".non_RENT").hide();
                    $("#duedate").val("");
                }

                //$(".invoice_belong").hide();
            }

        }

        function isInt(value) {
            if (isNaN(value)) {
                return false;
            }
            var x = parseFloat(value);
            return (x | 0) === x;
        }

        function product_total_price(){
            var total = 0;
            $(".qty").each(function () {

                var qty = $(this).val();

                if(isFinite(qty) && isInt(qty) && parseInt(qty) > 0){
                    var price = $(this).parent().find(".price").val();
                    total += parseFloat(price) * parseInt(qty);
                }else{
                    /*if(qty != "0"){
                     alert("請輸入正確的數量。");
                     return;
                     }*/

                    total += 0;
                }
            });

            $("#total_product_amount").text("$"+total.toFixed(1));
            return parseFloat(total);
        }

        $(function () {
            $(".RENT").hide();
            $(".non_RENT").hide();

            $("select[name='invoice_type']").on("change", function () {
                invoice_type($(this).val())
            });

            $(".qty").on("change keyup", function () {
                product_total_price();
            });


            if($('#invoice_type').val() != "0"){
                invoice_type($('#invoice_type').val());
            }

            if($("#order_id").val() != "0"){
                get_order_rent_info($("#order_id").val());
            }



            <?php
                
/*if(isset($_SESSION["invoice_type"])){
                    echo "$('#invoice_type').val('".$_SESSION["invoice_type"]."').change();";
                }

                if(isset($_SESSION["order_id"])){
                    echo "$('#order_id').val('".$_SESSION["order_id"]."').change();";
                }*/
            
?>


            $("#invoice_type").chosen({width: "150px", search_contains: true});
            $("#order_id").chosen({width: "150px", search_contains: true});
            $("#customer_id").chosen({width: "200px", search_contains: true});
            $("#invoice_id").chosen({width: "150px", search_contains: true});

            $("input[name='penalty_amount']").on("change keyup", function () {
                if(parseFloat($(this).val()) < 0 || parseFloat($(this).val()) != $(this).val()){
                    alert("請輸入正確的金額。");
                }
            });

            //console.log(parseInt($("#order_id").val()));

            //check if all rent invoice created
            extra_rent_invoice();

        });
    </script>
</head>
<body>

<form action="invoice_add.php" method="post" name="modifyform" enctype="multipart/form-data" class="form_field">

    <input type="hidden" name="invoice_belong" value="NEW">

    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td height="70" align="right" valign="middle" class="icontxt">
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="50" align="center" style="font-size: 12px;">
                            <input type="image" alt="submit" src="images/iconSave.png" width="32" height="32" border="0"><br>
                            &nbsp;儲存&nbsp;&nbsp;</a></td>
                        <td width="50" align="center">
                            <a href="invoice_index.php?<?php if(isset($_SESSION["index_url_parameter"])) echo $_SESSION["index_url_parameter"];?>&msg=取消"><img src="images/iconCancel.png" alt="取消" width="32" height="32" border="0"><br>
                                &nbsp;取消&nbsp;&nbsp;</a></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <span style="float:left;" class="pagetitletxt">
                    &nbsp;&nbsp;<b><img src="images/iconList.jpg" width="48" height="48" align="absmiddle"/> 新增發票
                    </b></span>
            </td>
        </tr>
        <tr>
            <td align="left" valign="middle"><!-- Content -->

                <table style="width: 1200px; padding-left: 15px;">

                    <tr>
                        <td style="width: 120px;">
                            <p class="required">發票類型: </p>
                        </td>

                        <td>
                            <select name="invoice_type" id="invoice_type">
                                <option value="0">---請選擇---</option>
                                <?php
                                    
foreach ($invoice_type_info as $invoice_type) {
                                        echo 
'<option value="' $invoice_type["code"] . '">' $invoice_type["name_tc"] . '</option>';
                                    }
                                
?>
                            </select>
                        </td>
                    </tr>

                    <tr class="RENT" style="display: none;">
                        <td>
                            <p class="required">合約編號: </p>
                        </td>

                        <td>
                            <select name="order_id" id="order_id" onchange="get_order_rent_info(this.value)">
                                <option value="0">---請選擇---</option>
                                <?php
                                    
foreach ($order_info as $order) {
                                        echo 
'<option value="' $order["id"] . '">' $order["code"] . '</option>';
                                    }
                                
?>
                            </select>
                        </td>
                    </tr>

                    <!--<tr class="PENALTY non_RENT" style="display: none;">
                        <td>
                            <p class="required">發票方式 :</p>
                        </td>

                        <td>
                            <input type="radio" name="invoice_belong" value="NEW" onclick="$('#customer_tr').show();$('#invoice_tr').hide();"> 全新發票
                            <input type="radio" name="invoice_belong" value="OLD" onclick="$('#customer_tr').hide();$('#invoice_tr').show();"> 加入到現有發票
                        </td>
                    </tr>-->

                    <tr class="non_RENT invoice_belong" id="customer_tr" style="display: none;">
                        <td>
                            <p class="required">客戶: </p>
                        </td>

                        <td>
                            <select name="customer_id" id="customer_id">
                                <option value="0">---請選擇---</option>
                                <?php
                                    
foreach ($customer_info as $customer) {
                                        if(
$customer["deleted"] == 1)
                                            continue;

                                        echo 
'<option value="' $customer["id"] . '">' $customer["customer_name"] . ' ('.$customer["code"].')</option>';
                                    }
                                
?>
                            </select>
                        </td>
                    </tr>

                    <!--<tr class="PENALTY non_RENT invoice_belong" id="invoice_tr" style="display: none;">
                        <td>
                            <p class="required">租金發票: </p>
                        </td>

                        <td>
                            <select name="invoice_id" id="invoice_id">
                                <option value="0">---請選擇---</option>
                                <?php
                        
/*                                    foreach ($invoice_info as $invoice) {
                                                                if($invoice["status"] == "SETTLED"){
                                                                    continue;
                                                                }

                                                                echo '<option value="' . $invoice["invoice_id"] . '">' . $invoice["invoice_code"] . '</option>';
                                                            }
                                                        */
?>
                            </select>
                        </td>
                    </tr>-->

                    <tr class="RENT">
                        <td>
                            <p class="required">租金月份</p>
                        </td>

                        <td>
                            <div id="ajax_order_rent_info"></div>
                            <br>
                        </td>
                    </tr>

                    <tr class="RENT" id="extra_rent_invoice">
                        <td>
                            <p>非合約期月份</p>
                        </td>

                        <td>
                            租金 $<input name="extra_rent_invoice_amount" id="extra_rent_invoice_amount" value="0" style="width: 70px;"><button id="add_extra_rent_invoice" type="button" style="margin-left: 20px;" onclick="ajax_add_extra_rent_invoice();">+ 新增非合約期月份</button>
                            <br><br>
                            (每月原本租金 : $<span id="original_rent_price"></span>, 每月實際租金 : $<span id="actual_rent_price"></span>)
                        </td>
                    </tr>

                    <tr class="ORDER_PRODUCT non_RENT">
                        <td>
                            <p class="required">可選購物品 : </p>
                        </td>

                        <td>
                            <table cellpadding="5" id="product_table">
                                <tr>
                                    <td>名稱</td>
                                    <td>數量</td>
                                    <td>價格</td>
                                </tr>

                                <?php
                                    
if(!empty($product_info)){
                                        foreach(
$product_info as $product){
                                            echo 
"<tr>
                                                            <td>"
.$product["name_tc"]."</td>
                                                            <td>
                                                            <input type='hidden' name='product_id["
.$product["id"]."]' value='".$product["id"]."' class='product_id'>
                                                            <input type='hidden' name='product_name["
.$product["id"]."]' value='".$product["name_tc"]."' class='product_name'>
                                                            <input type='text' name='qty["
.$product["id"]."]' value='0' style='width: 40px;' class='qty'>
                                                            <input type='hidden' name='price["
.$product["id"]."]' value='".$product["price"]."' class='price'>
                                                            </td>
                                                            <td>$"
.numberformat($product["price"])."</td>
                                                            </tr>"
;
                                        }
                                    }
                                
?>

                                <tr>
                                    <td colspan="3" style="border: 0;">總物品金額: <span id="total_product_amount">$0</span></td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <tr class="PENALTY non_RENT">
                        <td>
                            <p>類型 : </p>
                        </td>

                        <td>
                            <input type="text" name="remark" style="width: 200px;">
                            <br>
                            <span style="font-size: 12px;">eg: 行政費</span>

                        </td>
                    </tr>

                    <tr class="PENALTY non_RENT">
                        <td>
                            <p class="required">金額: </p>
                        </td>

                        <td>
                            $<input type="text" name="penalty_amount" style="width: 138px;">
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <p class="required">繳費日期 : </p>
                        </td>

                        <td>
                            <input type="text" name="duedate" id="duedate" style="width: 145px;">
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <p>發票備註 : </p>
                        </td>

                        <td>
                            <textarea name="invoice_remark" style="width: 500px; height: 100px;"></textarea>
                        </td>
                    </tr>

                    <tr>
                        <td></td>

                        <td>
                            <p class="red">* 必須填寫</p>
                        </td>

                    </tr>

                </table>

                <br><br><br><br>
                <!-- End Content -->
            </td>
        </tr>
    </table>
</form>

</body>
</html>