/var/www/hkosl.com/innoutstorage/webadmin/payment_add.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
<?php
    
require_once("check_login.php");

    
$message "";

    if (empty(
$_POST["customer_id"])) {
        
$message .= "請選擇客戶。\\n\\n";
    }

    if (empty(
$_POST["paymethod"])) {
        
$message .= "請選擇付款方式。\\n\\n";
    } else {
        if (
$_POST["paymethod"] == "CHEQUE") {
            if (empty(
$_POST["bank_name"])) {
                
$message .= "請輸入銀行名稱 。\\n\\n";
            }

            if (empty(
$_POST["cheque_num"])) {
                
$message .= "請輸入支票號碼 。\\n\\n";
            }
        }

        if (
$_POST["paymethod"] == "OTHER") {
            if (empty(
$_POST["paymethod_other"])) {
                
$message .= "請輸入付款方式-其他 。\\n\\n";
            }
        }
    }

    if (empty(
$_POST["amount"])) {
        
$message .= "請輸入金額。\\n\\n";
    } else {
        if (!
is_numeric($_POST["amount"]) || $_POST["amount"] <= 0) {
            
$message .= "金額應是正數數字。\\n\\n";
        }
    }

    if (empty(
$_POST["docdate"])) {
        
$message .= "請選擇付款日期。\\n\\n";
    }

    
$total_payment_amount 0;
    if (empty(
$_POST["payable_id"])) {
        
$message .= "請最少選擇一個應付款項。\\n\\n";
    } else {
        foreach (
$_POST["payable_id"] as $key => $payable_id) {
            if (!
is_numeric($_POST["paid_amount"][$key]) || (float)$_POST["paid_amount"][$key] < 0) {
                
$message .= "未付金額應輸入大於或等於零的數字。\\n\\n";
            }

            
/*var_dump($payable_id);
            var_dump( $_POST["ori_balance"][$key]);
            var_dump( $_POST["balance"][$key]);*/
            //$total_payment_amount += $_POST["ori_balance"][$key]-$_POST["balance"][$key];
            
$total_payment_amount += $_POST["paid_amount"][$key];
        }
    }

    
/*var_dump($_POST["amount"]);
    var_dump($total_payment_amount);*/

    /*if(!empty($_POST["amount"]) && $total_payment_amount != (float)$_POST["amount"]){
        $message .= "付款金額應與己選擇未付金額的總和應相同。\\n\\n";
    }*/

    
if(!empty($_POST["amount"]) && $total_payment_amount != (float)$_POST["amount"]){
        
$message .= "付款金額應與己選擇未付金額的總和應相同。\\n\\n";
    }

    if (!empty(
$message)) {
        echo 
"<script>alert('" $message "'); history.back();</script>";
        
//echo "<script>alert('" . $message . "'); </script>";
        
exit;
    }

    
$bank_name       "";
    
$cheque_num      "";
    
$paymethod_other "";

    if (
$_POST["paymethod"] == "CHEQUE") {
        
$bank_name  $_POST["bank_name"];
        
$cheque_num $_POST["cheque_num"];
    }

    if (
$_POST["paymethod"] == "OTHER") {
        
$paymethod_other $_POST["paymethod_other"];
    }

    if (
$_POST["paymethod"] == "BANKIN") {
        
$paymethod_other $_POST["paymethod_other2"];
    }

    
//insert payment
    
$has_deposit false;
    
$has_invoice false;
    foreach(
$_POST["payable_id"] as $key => $payable_id){
        if(
$_POST["payable_type"][$key] == "DEPOSIT"){
            
$has_deposit true;
        }

        if(
$_POST["payable_type"][$key] == "INVOICE"){
            
$has_invoice true;
        }
    }

    if(
$has_deposit && $has_invoice){
        
$payfor "ALL";
    }else if(
$has_deposit){
        
$payfor "DEPOSIT";
    }else if(
$has_invoice){
        
$payfor "INVOICE";
    }

    
$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"     => $_POST["customer_id"],
        
"paymethod"       => $_POST["paymethod"],
        
"bank_name"       => $bank_name,
        
"cheque_num"      => $cheque_num,
        
"paymethod_other" => $paymethod_other,
        
"code"            => $payment_code,
        
"amount"          => $_POST["amount"],
        
"status"          => "PAID",
        
"docdate"         => $_POST["docdate"],
        
"remark"          => $_POST["remark"],
        
"createdate"      => $nowdate,
        
"createby"        => $_SESSION["cmsloginid"],
        
"lastupdate"      => $nowdate,
        
"lastupby"        => $_SESSION["cmsloginid"],
        
"payfor"          => $payfor,
    );

    
$result insert_record("payment"$data);
    
$payment_id  $dbh->lastInsertId();

    
//insert payment dtl, update invoice or deposit balance
    
foreach($_POST["payable_id"] as $key => $payable_id){
        
//insert payment dtl
        //$payment_amount = $_POST["ori_balance"][$key]-$_POST["balance"][$key];
        
$payment_amount $_POST["paid_amount"][$key];
        
$data = array(
            
"payment_id"       => $payment_id,
            
"payable_type"       => $_POST["payable_type"][$key],
            
"payable_id"       => $_POST["payable_id"][$key],
            
"amount"          => $payment_amount,
            
"status"          => "PAID",
            
"docdate"         => $_POST["docdate"],
            
"createdate"      => $nowdate,
            
"createby"        => $_SESSION["cmsloginid"],
            
"lastupdate"      => $nowdate,
            
"lastupby"        => $_SESSION["cmsloginid"],
        );

        
$result insert_record("payment_dtl"$data);

        
//update invoice or deposit balance
        
if($_POST["payable_type"][$key] == "DEPOSIT"){
            
$table_name "deposit";
            
$result2 get_deposit($payable_id);

            
$status $result2["deposit_status"];
            if(
$result2["deposit_balance"] - $payment_amount == 0){
                
$status "PAID";
            }
        }

        if(
$_POST["payable_type"][$key] == "INVOICE"){
            
$table_name "invoice";
            
$result2 get_invoice($payable_id);

            
$status $result2["invoice_status"];
            if(
$result2["invoice_balance"] - $payment_amount == 0){
                
$status "SETTLED";
            }
        }

        if( (
$table_name == "deposit") || ($table_name == "invoice") ){
            
$sql "update ".$table_name." set balance=balance-?, status=?, lastupby=?, lastupdate=? where id=?";
            
$parameters = array($payment_amount$status$_SESSION["cmsloginid"], $nowdate$payable_id);
            
bind_pdo($sql$parameters);
        }
    }

    
$dbh null;

    
header("Location: payment_index.php?type=".$_POST["type"]."&msg=新增成功");