/var/www/enzatesting.onesolution.hk/inc/ord_cn_func.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
<?php 

function cn_markCusDtlDispatch($ordtxpako_refid){
    
$ord_txpako getDB("ord_txpako"$ordtxpako_refid);
    
//insert a new txpako
    
    //mark cn_cus_dtl as dispatched
    
}

function 
cn_markCusDtlDispatch_SN($ordtxpako_refid){
    
$ord_txpako getDB("ord_txpako"$ordtxpako_refid);
    
//insert a new txpako
    
    //mark cn_cus_dtl as dispatched
    
}

function 
cn_markCusDtlDispatch_PN($product_refid){
    
//mark cn_cus_dtl as dispatched
}

function 
cn_intmain_updateDtl($cnintmain_refid$prod_item){    
    
    global 
$dbh;
    
    
$sql_param[':cnintmain_refid'] = $cnintmain_refid;
    
$sql_param[':refid']         = (int)filter_var($prod_item['refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':txstatus']        = empty($prod_item['received']) ? 'LOSS' 'RECEIVED' ;
    
$sql_param[':desc']         = filter_var($prod_item['desc'], FILTER_SANITIZE_STRING);
    
$sql_param[':qty']             = (float)filter_var($prod_item['qty'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':wt']             = (float)filter_var($prod_item['wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':unitprice']     = (float)filter_var($prod_item['unitprice'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':remarks']         = filter_var($prod_item['remarks'], FILTER_SANITIZE_STRING);
    
$sql_param[':setused']         = 1;
    
$sql_param[':lastupby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    
    if( 
$sql_param[':txstatus']=='RECEIVED' ){    
        
$org_dtl getDB("cn_intmain_dtl"$prod_item['refid']);
        if(
$org_dtl['txstatus']!='RECEIVED' ){
            
$sql_update "received_time = GETDATE(),";        
            
            if(
$org_dtl['ordtxpako_refid']){
                
//product with serial number
                
cn_markCusDtlDispatch_SN($org_dtl['ordtxpako_refid']);
            }elseif(
$org_dtl['product_refid']){
                
//product without serial number
                
cn_markCusDtlDispatch_PN($org_dtl['product_refid']);
            }
        }
    }else{
        
$sql_update "received_time = NULL,";    
    }
    
    
$sql "UPDATE cn_intmain_dtl SET
                [desc] = :desc,
                qty = :qty, 
                wt = :wt, 
                unitprice = :unitprice,  
                remarks = :remarks, 
                setused = :setused,
                txstatus = :txstatus,
                
$sql_update
                lastupby = :lastupby,                 
                lastupdate = GETDATE()
            WHERE refid = :refid AND
                cnintmain_refid = :cnintmain_refid"
;
                
    
$sth $dbh->prepare($sql);    
    
$q $sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q$sql_param);
    
}

function 
cn_cusmain_updateDtl($cncusmain_refid$prod_item){
    
//vdump($prod_item);    
    
global $dbh;

    
$sql_param[':cncusmain_refid'] = $cncusmain_refid;
    
$sql_param[':refid']         = (int)filter_var($prod_item['refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':txstatus']        = empty($prod_item['received']) ? 'LOSS' 'RECEIVED' ;
    
$sql_param[':desc']         = filter_var($prod_item['desc'], FILTER_SANITIZE_STRING);
    
$sql_param[':qty']             = (float)filter_var($prod_item['qty'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':wt']             = (float)filter_var($prod_item['wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':unitprice']     = (float)filter_var($prod_item['unitprice'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':remarks']         = filter_var($prod_item['remarks'], FILTER_SANITIZE_STRING);
    
$sql_param[':setused']         = 1;
    
$sql_param[':lastupby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    if( 
$sql_param[':txstatus']=='RECEIVED' ){    
        
$org_dtl getDB("cn_intmain_dtl"$prod_item['refid']);
        if(
$org_dtl['txstatus']!='RECEIVED' ){
            
$sql_update "received_time = GETDATE(),";        
        }
    }else{
        
$sql_update "received_time = NULL,";    
    }
    
        
$sql "UPDATE cn_cusmain_dtl SET
                [desc] = :desc,
                qty = :qty, 
                wt = :wt, 
                unitprice = :unitprice,  
                remarks = :remarks, 
                setused = :setused,
                txstatus = :txstatus,
                
$sql_update
                lastupby = :lastupby, 
                lastupdate = GETDATE()
            WHERE refid = :refid AND
                cncusmain_refid = :cncusmain_refid"
;
                
    
$sth $dbh->prepare($sql);
    
    
$q $sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q$sql_param);    
}

function 
cn_intmain_insertDtl($cnintmain_refid$prod_item){
    
//vdump($prod_item);    
    
global $dbh;
    
    
$sql_param[':cnintmain_refid'] = $cnintmain_refid;
    
$sql_param[':product_refid']     = (int)filter_var($prod_item['product_refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':ordtxpako_refid']     = (int)filter_var($prod_item['ordtxpako_refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':txstatus']        = empty($prod_item['received']) ? 'LOSS' 'RECEIVED' ;
    
$sql_param[':desc']         = filter_var($prod_item['desc'], FILTER_SANITIZE_STRING);
    
$sql_param[':qty']             = (float)filter_var($prod_item['qty'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':wt']             = (float)filter_var($prod_item['wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':uom_wt']         = filter_var("gr"FILTER_SANITIZE_STRING);
    
$sql_param[':unitprice']     = (float)filter_var($prod_item['unitprice'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':uom_cy']         = filter_var("USD"FILTER_SANITIZE_STRING);
    
$sql_param[':remarks']         = filter_var($prod_item['remarks'], FILTER_SANITIZE_STRING);
    
$sql_param[':setused']         = 1;
    
$sql_param[':createby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
$sql_param[':lastupby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
        
    if( 
$sql_param[':txstatus']=='RECEIVED' ){    
        
$sql_insfield "received_time,";    
        
$sql_insvalue "GETDATE(),";        
    }

    
$sql "INSERT into cn_intmain_dtl (
                cnintmain_refid, product_refid, ordtxpako_refid, [desc],
                qty, wt, uom_wt, unitprice, uom_cy, 
                remarks, setused, txstatus, 
$sql_insfield
                createby, createdate, lastupby, lastupdate
            ) VALUES (
                :cnintmain_refid, :product_refid, :ordtxpako_refid, :desc,
                :qty, :wt, :uom_wt, :unitprice, :uom_cy, 
                :remarks, :setused, :txstatus, 
$sql_insvalue
                :createby, GETDATE(), :lastupby, GETDATE()
            )"
;
                
    
$sth $dbh->prepare($sql);
    

    
$q $sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q$sql_param);    
    

}

function 
cn_cusmain_insertDtl($cncusmain_refid$prod_item){
    
//vdump($prod_item);    
    
global $dbh;

    
$sql_param[':cncusmain_refid'] = $cncusmain_refid;
    
$sql_param[':product_refid']     = (int)filter_var($prod_item['product_refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':ordtxpako_refid']     = (int)filter_var($prod_item['ordtxpako_refid'], FILTER_SANITIZE_STRING);
    
$sql_param[':txstatus']        = empty($prod_item['received']) ? 'LOSS' 'RECEIVED' ;
    
$sql_param[':desc']         = filter_var($prod_item['desc'], FILTER_SANITIZE_STRING);
    
$sql_param[':qty']             = (float)filter_var($prod_item['qty'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':wt']             = (float)filter_var($prod_item['wt'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':uom_wt']         = filter_var("gr"FILTER_SANITIZE_STRING);
    
$sql_param[':unitprice']     = (float)filter_var($prod_item['unitprice'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    
$sql_param[':uom_cy']         = filter_var("USD"FILTER_SANITIZE_STRING);
    
$sql_param[':remarks']         = filter_var($prod_item['remarks'], FILTER_SANITIZE_STRING);
    
$sql_param[':setused']         = 1;
    
$sql_param[':createby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
$sql_param[':lastupby']     = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    if( 
$sql_param[':txstatus']=='RECEIVED' ){    
        
$sql_insfield "received_time,";    
        
$sql_insvalue "GETDATE(),";        
    }
    
    
$sql "INSERT into cn_cusmain_dtl (
                cncusmain_refid, product_refid, ordtxpako_refid, [desc],
                qty, wt, uom_wt, unitprice, uom_cy, 
                remarks, setused, txstatus, 
$sql_insfield
                createby, createdate, lastupby, lastupdate
            ) VALUES (
                :cncusmain_refid, :product_refid, :ordtxpako_refid, :desc,
                :qty, :wt, :uom_wt, :unitprice, :uom_cy, 
                :remarks, :setused, :txstatus, 
$sql_insvalue
                :createby, GETDATE(), :lastupby, GETDATE()
            )"
;
                
    
$sth $dbh->prepare($sql);
    
    
$q $sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q$sql_param);    
}

function 
cn_intmain_deleteUnusedDtlbyMainRefid($refid){
    global 
$dbh;
    
$sql "DELETE FROM cn_intmain_dtl
            WHERE cnintmain_refid=:refid
            AND setused=0"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid) );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q);        
}

function 
cn_cusmain_deleteUnusedDtlbyMainRefid($refid){
    global 
$dbh;
    
$sql "DELETE FROM cn_cusmain_dtl 
            WHERE cncusmain_refid=:refid
            AND setused=0"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid) );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q);        
}


function 
cn_intmain_getSummary($refid){
    global 
$dbh;
    
$sql "SELECT
            sum(dbo.cn_intmain_dtl.qty * dbo.cn_intmain_dtl.unitprice) as ttl
            FROM
            dbo.cn_intmain_dtl
            WHERE
            dbo.cn_intmain_dtl.cnintmain_refid = :refid"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid) );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q);
    
$row $sth->fetch();
    return 
$row;

}

function 
cn_intmain_updateSummary($refid){
    
$row cn_intmain_getSummary($refid);
    global 
$dbh$lastupby;
    
$sql "UPDATE cn_int_main SET
            ttl_amt = :ttl_amt,
            lastupby = :lastupby,
            lastupdate = GETDATE()
            WHERE refid = :refid"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid
                                
':ttl_amt'=>$row['ttl'],
                                
':lastupby'=>$lastupby) );
//    echo $sth->getSQL( array(':refid'=>$refid, 
//                                ':ttl_amt'=>$row['ttl'],
//                                ':lastupby'=>$lastupby) ) . HTML_EOL;    
    
pdo_showerror($sth$q);

}

function 
cn_cusmain_getSummary($refid){
    global 
$dbh;
    
$sql "SELECT
            sum(dbo.cn_cusmain_dtl.qty * dbo.cn_cusmain_dtl.unitprice) as ttl            
            FROM
            dbo.cn_cusmain_dtl
            WHERE
            dbo.cn_cusmain_dtl.cncusmain_refid = :refid"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid) );
    
//echo $sth->getSQL( $sql_param ) . HTML_EOL;    
    
pdo_showerror($sth$q);
    
$row $sth->fetch();
    return 
$row;
}

function 
cn_cusmain_updateSummary($refid){
    
$row cn_cusmain_getSummary($refid);
    global 
$dbh$lastupby;
    
$sql "UPDATE cn_cus_main SET
            ttl_amt = :ttl_amt,
            lastupby = :lastupby,
            lastupdate = GETDATE()
            WHERE refid = :refid"
;                
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid'=>$refid
                                
':ttl_amt'=>$row['ttl'],
                                
':lastupby'=>$lastupby) );
//    echo $sth->getSQL( array(':refid'=>$refid, 
//                                ':ttl_amt'=>$row['ttl'],
//                                ':lastupby'=>$lastupby) ) . HTML_EOL;    
    
pdo_showerror($sth$q);
}


function 
cn_fty_getSNivcftynbr($txpako_refid){
    
$txpako getDB("ord_txpako"$txpako_refid);
    
//vdump($txpako_refid, $txpako);
    
if($txpako['ivcfty_refid']){
        
$ivc_fty getDB("ivc_fty"$txpako['ivcfty_refid']);
        return 
$ivc_fty['ivc_nbr'];
    }
}

function 
cn_cus_getSNivccusnbr($txpako_refid){
    
$txpako getDB("ord_txpako"$txpako_refid);
    
//vdump($txpako_refid, $txpako);
    
if($txpako['ivccus_refid']){
        
$ivc_fty getDB("ivc_cus"$txpako['ivccus_refid']);
        return 
$ivc_fty['ivc_nbr'];
    }
}

function 
cn_fty_markallDtl($main_refid$setused='0'){
    global 
$dbh;
    
    
$sql "UPDATE cn_intmain_dtl 
            SET setused=:setused 
            WHERE cnintmain_refid=:main_refid"
;
    
$sth $dbh->prepare($sql);
    
$q$sth->execute( array(':main_refid'=> $main_refid':setused'=>$setused) );
    
//echo $sth->getSQL( array(':refid'=> $refid, ':flag'=>$flag) ).HTML_EOL;    
    
pdo_showerror($sth$q);    
}

function 
cn_cus_markallDtl($main_refid$setused='0'){
    global 
$dbh;
    
    
$sql "UPDATE cn_cusmain_dtl 
            SET setused=:setused 
            WHERE cncusmain_refid=:main_refid"
;
    
$sth $dbh->prepare($sql);
    
$q$sth->execute( array(':main_refid'=> $main_refid':setused'=>$setused) );
    
//echo $sth->getSQL( array(':refid'=> $refid, ':flag'=>$flag) ).HTML_EOL;    
    
pdo_showerror($sth$q);    
}


?>