/var/www/enzatesting.onesolution.hk/inc/enza_master_func - 20240522.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
<?php

function inv_product_option$custcode$default=""$showdesc=false$custcode2=false ) {
    global 
$dbh;
    
//$sql_param = array(':custcode'=> $custcode);
    
$sql_param = array( ':custgroup'=> substr$custcode0).'_' );

    if ( !empty( 
$custcode2 ) ) {
        
$sql_clause " OR custcode = :cust2";
        
$sql_param['cust2'] = $custcode2;
    }

    
//$sql = "SELECT * FROM inv_product where custcode=:custcode $sql_clause order by itemno";
    
$sql "SELECT * FROM inv_product where verified='1' AND custcode like :custgroup $sql_clause order by itemno";
    
$sth $dbh->prepare$sql );
    
$sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" '.$selected.'>'.$row['itemno'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['itemname'].' - '.$row['name_'.SYS_LANG];
        }
        
$s .= '</option>';
    }
    return 
$s;
}

function 
inv_rm_option$bomcategy$default=""$showdesc=false ) {
    global 
$dbh;
    
$table inv_getBomcategyTablename$bomcategy );

    
//$sql = "SELECT * FROM inv_product where custcode=:custcode $sql_clause order by itemno";
    
$sql "SELECT * FROM $table order by itemno";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
//echo $sth->getSQL( $sql_param );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" '.$selected.'>'.$row['itemno'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['name_'.SYS_LANG];
        }
        
$s .= '</option>';
    }
    return 
$s;
}

function 
inv_allproduct_option$default=""$showdesc=false$custcode2=false ) {
    return 
inv_product_option'___'$default$showdesc$custcode2 );
}

function 
inv_po_item_option$supplier_refid$bomcategy$default=""$showdesc=false$byfactory="" ) {
    global 
$dbh;

    
$sql_param = array( ':supplier'=> $supplier_refid':bomcategy'=> $bomcategy );

    if ( 
$byfactory==="" ) {}
    else {
        
$wherecl " AND dbo.master_supplier_item.byfactory = :byfactory ";
        
$sql_param[':byfactory'] = $byfactory;
    }

    
$sql "SELECT
            *
            FROM
            dbo.master_supplier_item
            WHERE
            dbo.master_supplier_item.bomcategy = :bomcategy AND
            dbo.master_supplier_item.mastersupplier_refid = :supplier
            
$wherecl
            ORDER by itemno"
;
    
$sth $dbh->prepare$sql );
    
$sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param )."--------------";
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['itemnoid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['itemnoid'].'" '.$selected.'>'.$row['itemno'];
        if ( 
$showdesc ) {
            
$detail getDB_inv$row['bomcategy'], $row['itemnoid'] );
            
$s .= ' - '.$detail['name_'.SYS_LANG];
        }
        
$s .= '</option>';
    }
    if ( empty( 
$s ) ) {
        
$s .= '<option value="" selected="selected">'.ITEM.WS.NOT_FOUND.'</option>';
    }
    return 
$s;
}


function 
master_type_option$default=""$default0=""$showdesc=false ) {
    global 
$dbh;

    
$sql "SELECT * FROM master_type WHERE showcode=1 ORDER BY typeid";
    
//echo $sql;
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['typeid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['typeid'].'" '.$selected.'>'.$row['typeid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['typedesc_'.SYS_LANG];
        }
        
$s .= '</option>';
    }
    return 
$s;
}

function 
master_setting_option$default=""$default0=""$showdesc=false ) {
    global 
$dbh;

    
$sql "SELECT * FROM master_setting order by `name`";
    if ( 
defined"MSSQL" ) ) {
        
$sql "SELECT * FROM master_setting order by name";
    }
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['name']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['name'].'" '.$selected.'>'.$row['name'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['desc_'.SYS_LANG];
        }
        
$s .= '</option>';
    }
    return 
$s;
}

function 
master_option$typeid$default=""$showdesc=false$showdeleted=false$clasue=""$settings=array() ) {
    global 
$dbh;
    if ( 
$showdeleted===false ) {
        
$del_clause 'AND deleted=0';
    }
    
// $sql = "SELECT * FROM master_type_code WHERE typeid = :typeid $del_clause $clasue ORDER BY CASE WHEN seq IS NULL THEN 1 ELSE 0 END, seq ASC, id ASC";
    
$sql "SELECT * FROM master_type_code WHERE typeid = :typeid $del_clause $clasue ORDER BY CASE WHEN seq IS NULL THEN 1 ELSE 0 END, seq ASC, codeid ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':typeid'=> $typeid ) );
    
//echo $sth->getSQL( array(':typeid'=> $typeid) );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['codeid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['codeid'].'" '.$selected.' >'.$row['codeid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['codedesc_'.SYS_LANG];
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}


function 
master_supplier$default=""$showdesc=false$whereclause=false ) {
    global 
$dbh;
    if ( 
$whereclause ) {
        
$sql_wc "WHERE $whereclause";
    }
    
$sql "SELECT * FROM master_supplier $sql_wc";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
//echo $sth->getSQL(  );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['code']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['code'].'" '.$selected.' >'.$row['code'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['companyname_'.SYS_LANG];
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
byfactory_option$default=""$showdesc=false ) {
    global 
$system_var;
    
$data = array( 1=>$system_var['COMPANY_FACTORY'], 0=>$system_var['COMPANY_OVERSEA'] );

    
$s '';

    foreach ( 
$data as $key=>$row ) {
        
$selected = ( $key==$default )? 'selected' '';
        
$s .= '<option value="'.$key.'" '.$selected.' >'.resolveCode_masterCustomer$row );
        if ( 
$showdesc ) {
            
$s .= ' - '.$row;
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
master_company_option$default=""$showdesc=false ) {
    global 
$system_var;
    
// global $dbh;
    // $sql = "SELECT * FROM master_type_code WHERE typeid = :typeid AND deleted=0 ORDER BY seq ASC, id ASC";
    // $sth = $dbh->prepare($sql);
    // $sth->execute( array(':typeid'=> $typeid) );
    
$data = array( $system_var['COMPANY_FACTORY'], $system_var['COMPANY_OVERSEA'] );

    
$s '';

    foreach ( 
$data as $row ) {
        
$selected = ( $row==$default )? 'selected' '';
        
$s .= '<option value="'.$row.'" '.$selected.' >'.$row;
        if ( 
$showdesc ) {
            
$s .= ' - '.resolveCode_masterCustomer$row );
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}


function 
master_user_status$default=""$showdesc=false ) {
    
// global $dbh;
    // $sql = "SELECT * FROM master_type_code WHERE typeid = :typeid AND deleted=0 ORDER BY seq ASC, id ASC";
    // $sth = $dbh->prepare($sql);
    // $sth->execute( array(':typeid'=> $typeid) );
    
$data = array( 'ACTIVE''INACTIVE' );

    
$s '';

    foreach ( 
$data as $row ) {
        
$selected = ( $row==$default )? 'selected' '';
        
$s .= '<option value="'.$row.'" '.$selected.' >'.$row;
        if ( 
$showdesc ) {
            
$s .= ' - '.$row;
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
master_table$typeid$default=""$showedit=false ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_type_code
    WHERE
    master_type_code.typeid = :typeid
    ORDER BY
    master_type_code.codeid ASC
    "
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':typeid'=> $typeid ) );
    
$s '<table class="tablelist" width="100%"><tr><th>'.CODE.'</th><th>'.DESC.'</th><th>'.DESCSC.'</th><th>'.DELETED.'</th>';
    if ( 
$showedit ) {
        
$s .= '<th>'.MODIFY.'</th>';
    }
    
$s .= '</tr>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['codeid']==$default )? 'class="current"' '';
        
$s .= "<tr $selected ><td class='code'>".$row['codeid'].'</td><td>'.$row['codedesc_en'].'</td><td>'.$row['codedesc_sc'].'</td><td>';
        
$s .= ( $row['deleted']==)?DELETED:'';
        
$s .= '</td>';
        if ( 
$showedit ) {
            
$s .= '<td><a href="master_modifyform.php?id='.$row['id'].'&typeid='.$typeid.'" class="uibutton">'.EDIT.'</a></td>';
        }
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}

function 
master_setting_table$name$default=""$showedit=false ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_setting
    ORDER BY
    `name` ASC
    "
;
    if ( 
defined"MSSQL" ) ) {
        
$sql "SELECT
        *
        FROM
        master_setting
        ORDER BY
        name ASC
        "
;
    }

    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '<table class="tablelist" width="100%"><tr><th>'.CODE.'</th><th>'.DESC.'</th><th>'.DESCSC.'</th><th>'.VALUE.'</th>';
    if ( 
$showedit ) {
        
$s .= '<th>'.MODIFY.'</th>';
    }
    
$s .= '</tr>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['name']==$default )? 'class="current"' '';
        
$s .= "<tr $selected ><td>".$row['name'].'</td><td>'.$row['desc_en'].'</td><td>'.$row['desc_sc'].'</td><td>'.$row['val'].'</td>';
        if ( 
$showedit ) {
            
$s .= '<td><a href="master_setting_modifyform.php?name='.$row['name'].'" class="uibutton">'.EDIT.'</a></td>';
        }
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}

function 
master_svc_table$default=""$showedit=false ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_svc_cost
    ORDER BY
    code ASC
    "
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '<table class="tablelist" width="100%"><tr><th>'.CODE.'</th><th>'.UNITPRICE.'</th>';
    if ( 
$showedit ) {
        
$s .= '<th>'.MODIFY.'</th>';
    }
    
$s .= '</tr>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'class="current"' '';
        
$s .= "<tr $selected ><td>".$row['code'].'</td><td>'.unmf$row['unit_price'] ).'&nbsp;'.$row['uom_cy'].'/'.$row['uom_svc'].'</td>';
        if ( 
$showedit ) {
            
$s .= '<td><a href="master_svc_modifyform.php?id='.$row['refid'].'&typeid='.$typeid.'" class="uibutton">'.EDIT.'</a></td>';
        }
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}

function 
master_user_table$default=""$showedit=false ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_user
    WHERE canlogin = 1
    ORDER BY department ASC, userid ASC
    "
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '<table class="tablelist" width="100%"><thead><tr><th>'.LOGIN.'</th><th>'.USER.'</th><th>'.DEPT.'</th><th>'.COMPANY.'</th><th>'.STATUS.'</th>';
    if ( 
$showedit ) {
        
$s .= '<th>'.MODIFY.'</th>';
    }
    
$s .= '</tr></thead>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'class="current"' '';
        
$s .= "<tr $selected ><td class='code'>".$row['userid'].'</td><td>'.$row['username'].'</td><td class="code">'.$row['department'].'</td><td class="code">'.$row['companyid'].'</td><td class="code">'.$row['status'].'</td>';
        if ( 
$showedit ) {
            
$s .= '<td><a href="master_user_modifyform.php?refid='.$row['refid'].'" class="uibutton">'.EDIT.'</a></td>';
        }
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}

function 
master_user_option$default=""$showdesc=true$where_clause="" ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_user
    
$where_clause
    ORDER BY
    userid ASC
    "
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    echo 
$sth->getSQL( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['userid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['userid'].'" '.$selected.'>'.$row['userid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['username'];
        }
        
$s .= '</option>';
    }
    return 
$s;
}

function 
master_worker_table$default=""$showedit=false ) {
    global 
$dbh;
    
$sql "SELECT
    *
    FROM
    master_user
    WHERE canlogin = 0
    ORDER BY department ASC, userid ASC
    "
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '<table class="tablelist" width="100%"><thead><tr><th>'.LOGIN.'</th><th>'.NAME.'</th><th>'.CHINESE.WS.NAME.'</th><th>'.DEPT.'</th><th>'.COMPANY.'</th><th>'.STATUS.'</th>';
    if ( 
$showedit ) {
        
$s .= '<th>'.MODIFY.'</th>';
    }
    
$s .= '</tr></thead>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'class="current"' '';
        
$s .= "<tr $selected ><td class='code'>".$row['userid'].'</td><td>'.$row['username_en'].'</td><td>'.$row['username_sc'].'</td><td class="code">'.$row['department'].'</td><td class="code">'.$row['companyid'].'</td><td class="code">'.$row['status'].'</td>';
        if ( 
$showedit ) {
            
$s .= '<td><a href="master_worker_modifyform.php?refid='.$row['refid'].'" class="uibutton">'.EDIT.'</a></td>';
        }
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}

function 
master_materialpricelist_table$numrow=50$from=""$to=""$showedit=false ) {
    global 
$dbh;

    
$numrow = (int)$numrow;

    if ( 
$from ) {
        
$sql_condition .= 'AND createdate = :from ';
        
$sql_param[':from'] = $from;
    }

    if ( 
$to ) {
        
$sql_condition .= 'AND createdate <= :to ';
        
$sql_param[':to'] = $to;
    }

    
/*    if($sql_condition){
    $sql_condition = " WHERE " . $sql_condition;
    }*/

    
$sql "SELECT * FROM master_mat_pricelist WHERE refid IN
    ( SELECT max(refid) FROM master_mat_pricelist WHERE TRUE 
$sql_condition GROUP BY date(createdate) )
    ORDER BY createdate DESC"
;
    if ( 
defined"MSSQL" ) ) {
        
$sql "SELECT TOP(60) * FROM master_mat_pricelist WHERE refid IN
            ( SELECT max(refid) FROM master_mat_pricelist WHERE 1=1 
$sql_condition GROUP BY CONVERT(date, createdate) )
            ORDER BY createdate DESC"
;
    }
    
$sth $dbh->prepare$sql );
    
$sth->execute$sql_param );
    
//echo $sth->getSQL( $sql_param );
    
$s '<table class="tablelist" width="100%">
    <th>'
.DATE.'</th>
    <th align="right">'
.GOLD.'</th>
    <th align="right">'
.SILVER.'</th>
    <th align="right">'
.PLATINUM.'</th>
    <th align="right">'
.COPPER.'</th>
    <th align="right">'
.ALUMINUM.'</th>
    <th align="right">USD/CNY</th>
    <th align="right">USD/EUR</th>'
;
    
/*    if($showedit){
    $s .= '<th>Modify</th>';
    }    */
    
$s .= '</tr>';

    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
//$selected = ($row['codeid']==$default)? 'class="current"' : '';

        
$s .= "<tr $selected >
        <td>"
.datef$row['createdate'] ).'</td>
        <td align="right">'
.numfs$row['gold'] ).'</td>
        <td align="right">'
.numfs$row['silver'] ).'</td>
        <td align="right">'
.numfs$row['platinum'] ).'</td>
        <td align="right">'
.numfs$row['copper'] ).'</td>
        <td align="right">'
.numfs$row['aluminum'] ).'</td>
        <td align="right">'
.numfs$row['usd-cny'], ).'</td>
        <td align="right">'
.numfs$row['usd-eur'], 4).'</td>';

        
/*        if($showedit){
        $s .= '<td><a href="master_mat_pricelist_modifyform.php?id='.$row['id'].'&typeid='.$typeid.'" class="uibutton">Edit</a></td>';
        }*/
        
$s .= '</tr>';
    }

    
$s .= '</table>';
    return 
$s;
}
/*function itemgp_option($table, $default=""){
global $dbh;
//var_dump($dbh);
$sql = "select distinct(itemgp) from $table ORDER BY itemgp ASC";
echo $sql;
$sth = $dbh->prepare($sql);
//echo"1";
$sth->execute();
$s = '';
//echo"2";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$selected = ($row['itemgp']==$default)? 'selected' : '';
$s .= '<option value="'.$row['itemgp'].'" '.$selected.' >'.$row['itemgp'].'</option>'.PHP_EOL;
}
//echo"3";
return $s;
}*/

function defaultsupplier_option$bomcategy$itemnoid$default="" ) {
    global 
$dbh;
    
$sql "SELECT
dbo.master_supplier.code,
dbo.master_supplier.suppliername_en,
dbo.master_supplier.suppliername_sc,
dbo.master_supplier_item.refid,
dbo.master_supplier_item.mastersupplier_refid

FROM
dbo.master_supplier_item
INNER JOIN dbo.master_supplier ON dbo.master_supplier_item.mastersupplier_refid = dbo.master_supplier.refid
WHERE
dbo.master_supplier_item.byfactory = 1 AND
dbo.master_supplier_item.itemnoid = :itemnoid AND
dbo.master_supplier_item.bomcategy = :bomcategy"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':bomcategy'=>$bomcategy':itemnoid'=>$itemnoid ) );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {

        
$selected = ( $row['mastersupplier_refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['mastersupplier_refid'].'" '.$selected.' >'.$row['code'] .' - '$row['suppliername_'.SYS_LANG].'</option>'.PHP_EOL;
    }
    
//echo"3";
    
return $s;
}
function 
po_getdefaultsupplier$companyid$bomcategy$itemnoid ) {
    
$table inv_getBomcategyTablename$bomcategy );

    global 
$dbh$system_var;
    
//vdump($table, $companyid, $bomcategy, $itemnoid);
    
$sql "SELECT * FROM $table WHERE refid=:itemnoid";

    
$sth $dbh->prepare$sql );
    
//echo $sth->getSQL(array(':itemnoid'=>$itemnoid));
    
$sth->execute( array( ':itemnoid'=>$itemnoid ) );
    if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        if ( 
$companyid != $system_var['COMPANY_FACTORY'] ) {
            return 
$row['mastervendor_refid'];
        }else {
            return 
$row['mastersupplier_refid'];
        }
    }
    return 
false;
}

function 
po_getSupplierPriceAndRef$companyid$supplier_refid$bomcategy$itemnoid ) {
    global 
$dbh$system_var;
    if ( 
$companyid != $system_var['COMPANY_FACTORY'] ) {
        
$sqlw '(dbo.master_supplier_item.byfactory <> 1 OR dbo.master_supplier_item.byfactory IS NULL)';
    }else {
        
$sqlw '(dbo.master_supplier_item.byfactory = 1 )';
    }

    
$sql "SELECT
            *
            FROM
            dbo.master_supplier_item
            WHERE
            
$sqlw AND
            dbo.master_supplier_item.itemnoid = :itemnoid AND
            dbo.master_supplier_item.bomcategy = :bomcategy AND
            dbo.master_supplier_item.mastersupplier_refid = :supplier_refid"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':bomcategy'=>$bomcategy':itemnoid'=>$itemnoid':supplier_refid'=>$supplier_refid ) );
    
//echo $sth->getSQL(array(':bomcategy'=>$bomcategy, ':itemnoid'=>$itemnoid, ':supplier_refid'=>$supplier_refid));
    
if ( $row $sth->fetchPDO::FETCH_ASSOC ) ) {
        return 
$row;
    }
    return 
false;
}


function 
vendor_defaultsupplier_option$bomcategy$itemnoid$default="" ) {
    global 
$dbh;
    
$sql "SELECT
dbo.master_supplier.code,
dbo.master_supplier.suppliername_en,
dbo.master_supplier.suppliername_sc,
dbo.master_supplier_item.refid,
dbo.master_supplier_item.mastersupplier_refid

FROM
dbo.master_supplier_item
INNER JOIN dbo.master_supplier ON dbo.master_supplier_item.mastersupplier_refid = dbo.master_supplier.refid
WHERE
(dbo.master_supplier_item.byfactory <> 1 OR dbo.master_supplier_item.byfactory IS NULL) AND
dbo.master_supplier_item.itemnoid = :itemnoid AND
dbo.master_supplier_item.bomcategy = :bomcategy"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':bomcategy'=>$bomcategy':itemnoid'=>$itemnoid ) );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {

        
$selected = ( $row['mastersupplier_refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['mastersupplier_refid'].'" '.$selected.' >'.$row['code'] .' - '$row['suppliername_'.SYS_LANG].'</option>'.PHP_EOL;
    }
    
//echo"3";
    
return $s;
}

function 
suppliercode_option$default=""$includeDataAttr=false ) {
    global 
$dbh;
    
$sql "SELECT
dbo.master_supplier.*
FROM
dbo.master_supplier
ORDER BY
code ASC"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute();
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {

        
$selected = ( $row['code']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['code'].'" ';
        if ( 
$includeDataAttr ) {
            
$s .= 'data-'.$includeDataAttr.'="'.$row[$includeDataAttr].'"';
        }
        
$s .= ' '.$selected.' >'.$row['code'] .' - '$row['suppliername_'.SYS_LANG].'</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
supplier_option$default=""$includeDataAttr=false ) {
    global 
$dbh;
    
$sql "SELECT
dbo.master_supplier.*
FROM
dbo.master_supplier
ORDER BY
code ASC"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute();
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {

        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" ';
        if ( 
$includeDataAttr ) {
            
$s .= 'data-'.$includeDataAttr.'="'.$row[$includeDataAttr].'"';
        }
        
$s .= $selected.' >'.$row['code'] .' - '$row['suppliername_'.SYS_LANG].'</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
vendor_option$default="" ) {
    global 
$dbh;
    
$sql "SELECT
            *
            FROM
            dbo.master_customer
            WHERE
            dbo.master_customer.isvendor = 1"
;

    
$sth $dbh->prepare$sql );
    
$sth->execute();
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {

        
$selected = ( $row['custcode']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['custcode'].'" '.$selected.' >'.$row['custcode'] .' - '$row['custname_'.SYS_LANG].'</option>'.PHP_EOL;
    }
    
//echo"3";
    
return $s;
}

function 
ivcrpt_template_option$default=""$showdesc=true ) {
    global 
$dbh;
    
$sql "SELECT * FROM ivc_rpt_template ORDER BY refid ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" '.$selected.' >'.$row['refid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['ivc_type_en'];//SYS_LANG
        
}
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
getCusIvcTemplate_refid$custcode ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_customer WHERE custcode = :custcode";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':custcode' => $custcode ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['ivcrpttemplate_refid'];
}
//echo getCusIvcTemplate_refid('A2F');

function getCusIvcTemplateSrc$ivcrpttemplate_refid ) {
    global 
$dbh;
    
$sql "SELECT * FROM ivc_rpt_template WHERE refid = :refid";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':refid' => $ivcrpttemplate_refid ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['template_path'].$row['template_src'];
}
//echo getCusIvcTemplateSrc(2);

function cncus_template_option$default=""$showdesc=true ) {
    global 
$dbh;
    
$sql "SELECT * FROM cn_cus_template ORDER BY refid ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" '.$selected.' >'.$row['refid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['cuscn_type_en'];//SYS_LANG
        
}
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
bal_cus_template_option$default=""$showdesc=true ) {
    global 
$dbh;
    
$sql "SELECT * FROM bal_cus_template ORDER BY refid ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['refid']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['refid'].'" '.$selected.' >'.$row['refid'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['cusbal_type_en'];//SYS_LANG
        
}
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
getCusCnTemplate_refid$custcode ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_customer WHERE custcode = :custcode";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':custcode' => $custcode ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['cncustemplate_refid'];
}

function 
getCusCnTemplateSrc$cuscntemplate_refid ) {
    global 
$dbh;
    
$sql "SELECT * FROM cn_cus_template WHERE refid = :refid";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':refid' => $cuscntemplate_refid ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['template_path'].$row['template_src'];
}

function 
customer_option$default=""$showdesc=false$where_clause=false$includeDataAttr=false ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_customer $where_clause ORDER BY custcode ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
$s '';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['custcode']==$default )? 'selected' '';
        
$s .= '<option value="'.$row['custcode'].'" ';
        if ( 
$includeDataAttr ) {
            
$s .= ' data-'.$includeDataAttr.'="'.$row[$includeDataAttr].'" ';
        }
        
$s .= $selected.' >'.$row['custcode'];
        if ( 
$showdesc ) {
            
$s .= ' - '.$row['custname_'.SYS_LANG];
        }
        
$s .= '</option>'.PHP_EOL;
    }
    return 
$s;
}

function 
getCustomerByCode$custcode ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_customer WHERE custcode=:custcode";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':custcode' => $custcode ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row;
}


/**
 * get the customer material loss from the $customer Array to the loss_* variables
 * if the customer does not have a customized loss settings, we assign system default LOSS to the empty variables
 */
function getCustomerMetalLoss$customer ) {
    
//function getCustomerMetalLoss( $customer, &$loss_18k=null, &$loss_14k=null, &$loss_9_10k=null, &$loss_silver=null ) {
    
global $system_var;
    
$matloss_array = array( 'loss_18k''loss_14k''loss_9_10k''loss_silver' );

    foreach ( 
$matloss_array as $key => $value ) {
        if ( 
$customer[$value]>) {
            $
$value $customer[$value];
        }elseif ( empty( $
$value ) ) {
            $
$value $system_var['LOSS']; //fill system default only if the variable is empty
        
}
        
$result[$value] = $$value;
    }
    return 
$result;
}
// $cus = getDB_where('master_customer', 'custcode=:custcode', array(':custcode'=>'11N'));
// $r = getCustomerMetalLoss($cus, $a, $b, $c, $d);
// vdump($a, $b, $c, $d, $r);

/**
 * get the order material loss from the $ordermain Array to the loss_* variables
 * if the customer does not have a customized loss settings, we assign system default LOSS to the empty variables
 */
function getOrderMainMetalLoss$ordermain ) {
    
//function getOrderMainMetalLoss( $ordermain, &$loss_18k, &$loss_14k, &$loss_9_10k, &$loss_silver ) {
    
global $system_var;
    
$matloss_array = array( 'loss_18k''loss_14k''loss_9_10k''loss_silver' );

    foreach ( 
$matloss_array as $key => $value ) {
        if ( 
$ordermain[$value]>) {
            $
$value $ordermain[$value];
        }elseif ( empty( $
$value ) ) {
            $
$value $system_var['LOSS']; //fill system default only if the variable is empty
        
}
        
$result[$value] = $$value;
    }
    return 
$result;
}

/**
* return proper matloss in $matloss_array based on the $mattype provided
*/
function mattypeLoss$mattype$matloss_array ){
    global 
$system_var;
    
//master type code (MATTYPE) to loss field mapping
    
$matloss_map = array(
        
'loss_18k' => array('E'),
        
'loss_14k' => array('F'),
        
'loss_9_10k' => array('N','T'),
        
'loss_silver' => array('S','@'),
    );
    
//vdump($matloss_map);
    
foreach($matloss_map as $k => $mat_list){
        if( 
in_array($mattype$mat_list)){
            
//vdump('found', $k, $matloss_array);
            
return $matloss_array[$k];
        }
    }
    return 
$system_var['LOSS'];
}
// vdump( mattypeLoss('N', $r) );

function getSupplierByCode$code ) {
    global 
$dbh$master_alldata;
    if(
$master_alldata['getSupplierByCode'][$code]){ //from cache
        
return $master_alldata['getSupplierByCode'][$code]; 
    }
    
$sql "SELECT * FROM master_supplier WHERE [code]=:code";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':code' => $code ) );
    
// echo $sth->getSQL(  );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$master_alldata['getSupplierByCode'][$code] = $row;
}
// getSupplierByCode("AGE");
// getSupplierByCode("AGE");
// getSupplierByCode("AGE");
// vdump($master_alldata);

function resolveCode_master_type_code$typeid$code$showcode=false$lang=SYS_LANG ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_type_code WHERE codeid=:code AND typeid=:typeid ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':typeid' => $typeid':code' => $code ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    if ( 
$showcode ) {
        
$s .= $code " - ";
    }
    
$s .= evl$row['codedesc_'.$lang], $row['codedesc_en'] );
    return 
$s;
}

function 
resolveCode_masterCustomer$code$lang=SYS_LANG ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_customer WHERE custcode=:code ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':code' => $code ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
evl$row['custname_'.$lang], $row['custname_en'] );
}

function 
resolveCode_masterUser$code$lang=SYS_LANG ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_user WHERE userid=:code ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':code' => $code ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['userid'];
}

function 
resolveCode_masterSupplier$code$lang=SYS_LANG ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_supplier WHERE [code]=:code ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':code' => $code ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
evl$row['companyname_'.$lang], $row['companyname_en'] );
}

function 
resolveCode_master_operation$code$showcode=false$lang=SYS_LANG ) {
    global 
$dbh;
    
$sql "SELECT * FROM master_operation WHERE code=:code ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':code' => $code ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    if ( 
$showcode ) {
        
$s .= $code " - ";
    }
    
$s .= $row['codedesc_'.$lang];
    return 
$s;
}

function 
resolveItemno$itemno$langin='' ) {
    global 
$dbh;
    
$lang = empty( $langin ) ? 'name_en' 'name_'.$langin;
    
$sql "SELECT name_en, name_sc FROM inv_product WHERE itemno = :itemno0 UNION
            SELECT name_en, name_sc FROM inv_diamond WHERE itemno = :itemno1 UNION
            SELECT name_en, name_sc FROM inv_stone WHERE itemno = :itemno2 UNION
            SELECT name_en, name_sc FROM inv_accessory WHERE itemno = :itemno3 UNION
            SELECT name_en, name_sc FROM inv_material WHERE itemno = :itemno4"
;
    
$sth $dbh->prepare$sql );
    
$q $sth->execute( array( ':itemno0'=>$itemno':itemno1'=>$itemno':itemno2'=>$itemno':itemno3'=>$itemno':itemno4'=>$itemno ) );
    
//echo $sth->getSQL( );
    
pdo_showerror$sth$q );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return empty( 
$row[$lang] ) ? $itemno $row[$lang]; //return itemno if itemno not found
}
//echo resolveItemno('SANTO3RD00165', 'sc');

function user_profile_form$user_refid ) {
    global 
$dbh;

    
$sql "SELECT * from dbo.sec_profile WHERE status=1";
    
$sth $dbh->prepare$sql );
    
$q $sth->execute( );
    
//echo $sth->getSQL( );
    
pdo_showerror$sth$q );
    
$s '<table>';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$sql2 "SELECT
                    dbo.sec_user_profile.refid
                    FROM
                    dbo.master_user
                    INNER JOIN dbo.sec_user_profile ON dbo.sec_user_profile.userid = dbo.master_user.userid
                    WHERE
                    dbo.master_user.refid = :user_refid AND
                    dbo.sec_user_profile.profile_name = :profile_name "
;
        
$sth2 $dbh->prepare$sql2 );
        
$q $sth2->execute( array( ':user_refid' => $user_refid':profile_name' => $row['profile_name'] ) );
        
//echo $sth2->getSQL( array(':user_refid' => $user_refid, ':profile_name' => $row['profile_name']) ).HTML_EOL;
        
pdo_showerror$sth2$q );

        if ( 
$row2 $sth2->fetchPDO::FETCH_ASSOC ) ) {
            
$checked 'checked="checked"';
        }
        else {
            
$checked '';
        }
        
$s .= '<tr><td>'.$row['profile_name'].'</td>';
        
$s .= '<td><input type="checkbox" name="p['.$row['profile_name'].']" class="profile"  '.$checked.' /></td></tr>';
    }
    
$s .= '</table>';
    return 
$s;

}
//echo user_profile_form(2);
//input_masterWorkerBarcode('name="worker['.$r.']"', 1);

function input_masterWorkerBarcode$attr=""$seqno=false$default=""$spry=true ) {
    if ( 
is_numeric$seqno ) === false ) {
        
$seq rand1000000 9000000 );
    }else {
        
$seq $seqno;
    }

    
$s '<span id="workerspan'.$seq.'">N/A</span>&nbsp;'
        
.'<span id="sprytextworkerfield'.$seq.'">'
        
.'<input type="hidden" id="worker'.$seq.'" value="'.$default.'" '.$attr.' />'
        
.'<span class="textfieldRequiredMsg">'.INVALID.WS.WORKER.'</span></span><br/>'
        
.'<input type="password" name="barcodepwd'.$seq.'" id="barcodepwd'.$seq.'" value="'.$default.'" '.$attr.' autocomplete="off"  />';

    
$s .= '<script type="text/javascript">';

    if ( 
$spry ) {
        
$s .= 'var sprytextworkerfield'.$seq.' = new Spry.Widget.ValidationTextField("sprytextworkerfield'.$seq.'", "none", {validateOn:["blur", "change"]});';
    }

    
$s .= '$(document).ready(function() {
                    $("#barcodepwd'
.$seq.'").on("change", function(e) {
                        $.post("ajax_json_workerbarcode.php",{
                                      formid: "'
.$GLOBALS['formid'].'",
                                      barcodepwd: $("#barcodepwd'
.$seq.'").val(),
                                      format: "json"
                            },
                            function(data){
                                if(data != null && typeof(data.userid) !== "undefined" && data.userid != null) {
                                    $("#worker'
.$seq.'").val(data.userid);
                                    $("#worker'
.$seq.'").blur();
                                    $("#workerspan'
.$seq.'").html(data.username_en+" "+data.username_sc);
                                    //alert(data.order_nbr);
                                }else{
                                    $("#worker'
.$seq.'").val("");
                                    $("#worker'
.$seq.'").blur();
                                    $("#workerspan'
.$seq.'").html("N/A");
                                    alert("'
.INVALID.WS.WORKER.'");
                                }
                            },
                            "json"
                        );
                    });
                });
            </script>'
;
    return 
$s;
}

//temp function
function input_masterWorker$default=""$attr="" ) {
    return 
'<input type="text" name="worker" value="'.$default.'" '.$attr.' />';
}

function 
input_masterWorker_option$default=""$showname=ture$department=''$status=''$deleted='' ) {
    global 
$dbh;
    if ( 
$department!=='' ) {
        
$where_clause .= "AND department='$department' ";
    }
    if ( 
$status!='ALL' ) {
        if ( 
$status=='' ) {
            
$where_clause .= "AND status='ACTIVE' ";
        }else {
            
$where_clause .= "AND status='$status' ";
        }
    }
    if ( 
$deleted!=='' ) {
        
$where_clause .= "AND deleted='$deleted' ";
    }
    
$sql "SELECT * FROM master_user WHERE canlogin='0' $where_clause ORDER BY userid ASC";
    
$sth $dbh->prepare$sql );
    
$sth->execute( );
    
//echo $sth->getSQL( );

    
$s='';
    while ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
        
$selected = ( $row['userid']==$default )? 'selected="selected"' '';

        
$s .= "<option value='".$row['userid']."' $selected >".$row['userid'];
        if ( 
$showname==true ) {
            
$s .= " - ".$row['username'];
        }
        
$s .="</option>";
    }

    return 
$s;
}

function 
validate_workerbarcode$userid$barcodepwd ) {
    global 
$dbh;
    
$sql "SELECT count(*) as cnt FROM master_user WHERE canlogin='0' AND userid = :userid AND barcodepwd = :barcodepwd";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':userid'=>$userid ':barcodepwd'=>$barcodepwd ) );
    
//echo $sth->getSQL( array(':userid'=>$userid , ':barcodepwd'=>$barcodepwd ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['cnt']==1;

}
//echo validate_workerbarcode('2222', 'WK12121212');

function barcodepwdToWorker$barcodepwd ) {
    global 
$dbh;
    
$sql "SELECT
            dbo.master_user.refid,
            dbo.master_user.userid,
            dbo.master_user.username,
            dbo.master_user.username_en,
            dbo.master_user.username_sc

            FROM master_user
            WHERE
            dbo.master_user.status = 'ACTIVE' AND
            dbo.master_user.canlogin = '0'AND
            dbo.master_user.barcodepwd = :barcodepwd"
;
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':barcodepwd'=>$barcodepwd ) );
    
//echo $sth->getSQL( array(':barcodepwd'=>$barcodepwd ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row;
}
//vdump(barcodepwdToWorker('WK12341234'));


//echo"<textarea>".input_masterWorker_option('', true, 'QCDT', 'ALL')."</textarea>";
/*
function getNextMoldnoByPdt($producttype){
    global $dbh;
    $sql = "SELECT last_seq_no FROM dgn_mold_seq WITH (TABLOCKX) WHERE producttype=:producttype ";
    $sth = $dbh->prepare($sql);
    $sth->execute( array(':producttype' => $producttype) );
    $row = $sth->fetch(PDO::FETCH_ASSOC);
    incNextMoldnoByPdt($producttype);
    return (int)$row['last_seq_no']+1;
}
//echo getNextMoldnoByPdt("R");

function incNextMoldnoByPdt($producttype){
    global $dbh;
    $sql = "UPDATE dgn_mold_seq SET last_seq_no=last_seq_no+1, lastupdate=GETDATE() WHERE producttype=:producttype ";
    $sth = $dbh->prepare($sql);
    $sth->execute( array(':producttype' => $producttype) );
}
//incNextMoldnoByPdt("R");
 */
function getCurrMoldnoByPdt$producttype ) {
    global 
$dbh;
    
$sql "SELECT last_seq_no FROM dgn_mold_seq WHERE producttype=:producttype ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':producttype' => $producttype ) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    
//echo $newno = (int)$row['last_seq_no']+1;
    
return $row['last_seq_no'];
}
//echo getCurrMoldnoByPdt("B");

function getNextMoldnoByPdt$producttype ) {
    global 
$dbh;
    
//$dbh->beginTransaction();
    
$sql "SELECT last_seq_no FROM dgn_mold_seq WITH (TABLOCKX) WHERE producttype=:producttype ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':producttype' => $producttype ) );
    
//echo $sth->getSQL( array(':producttype' => $producttype) );
    
$row $sth->fetchPDO::FETCH_ASSOC );
    
$newno = (int)$row['last_seq_no']+1;
    while ( 
moldnoIsUsed$producttype$newno ) ) {
        
$newno++;
    }
    
incNextMoldnoByPdt$producttype$newno );
    
//exit;
    //$dbh->commit();
    
return (string)str_pad$newno4'0'STR_PAD_LEFT );
}
//echo getNextMoldnoByPdt("R");

function incNextMoldnoByPdt$producttype$newno ) {
    global 
$dbh;
    
$sql "UPDATE dgn_mold_seq SET last_seq_no=:newno, lastupdate=GETDATE() WHERE producttype=:producttype ";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':producttype' => $producttype':newno' => $newno ) );
    
//echo $sth->getSQL( array(':producttype' => $producttype, ':newno' => $newno) );
}
//incNextMoldnoByPdt("R");

function moldnoIsUsed$producttype$newno ) {
    
//echo"moldnoIsUsed($producttype, $newno) ";
    
global $dbh;
    
$sql "SELECT count(*) as cnt FROM dgn_master_mold WHERE producttype=:producttype AND moldno=:moldno";
    
$sth $dbh->prepare$sql );
    
$sth->execute( array( ':producttype' => $producttype':moldno' => $newno ) );
    echo 
$sth->getSQL( array( ':producttype' => $producttype':moldno' => $newno ) ).HTML_EOL;
    
$row $sth->fetchPDO::FETCH_ASSOC );
    return 
$row['cnt']>0;
}

function 
moldnoAndSizeIsValid$producttype$moldno$size='' ) {
    global 
$dbh;


    if ( empty( 
$size ) ) { //find same product type and mold (req: without size)
        /*         $sql = "SELECT count(*) as cnt FROM dgn_master_mold WHERE producttype=:producttype AND moldno=:moldno AND (size<>'' AND size IS NOT NULL)";
        $sth = $dbh->prepare($sql);
        $sth->execute( array(':producttype' => $producttype, ':moldno' => $moldno) );
        //echo $sth->getSQL( array(':producttype' => $producttype, ':moldno' => $moldno) ).HTML_EOL;
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        if($row['cnt']>0){
            return false;
        } */
        
$sql "SELECT count(*) as cnt FROM dgn_master_mold WHERE producttype=:producttype AND moldno=:moldno";
        
$sth $dbh->prepare$sql );
        
$sth->execute( array( ':producttype' => $producttype':moldno' => $moldno ) );
        
//echo $sth->getSQL( array(':producttype' => $producttype, ':moldno' => $moldno) ).HTML_EOL;
        
$row $sth->fetchPDO::FETCH_ASSOC );
        if ( 
$row['cnt']>) {
            return 
false;
        }
    }else {
        
$sql "SELECT count(*) as cnt FROM dgn_master_mold WHERE producttype=:producttype AND moldno=:moldno AND size=:size";
        
$sth $dbh->prepare$sql );
        
$sth->execute( array( ':producttype' => $producttype':moldno' => $moldno':size' => $size ) );
        
//echo $sth->getSQL( array(':producttype' => $producttype, ':moldno' => $moldno, ':size' => $size) ).HTML_EOL;
        
$row $sth->fetchPDO::FETCH_ASSOC );
        if ( 
$row['cnt']>) {
            return 
false;
        }
        
/*         $sql = "SELECT count(*) as cnt FROM dgn_master_mold WHERE producttype=:producttype AND moldno=:moldno AND (size<>'' OR size<>'XX' OR size IS NOT NULL)";
        $sth = $dbh->prepare($sql);
        $sth->execute( array(':producttype' => $producttype, ':moldno' => $moldno) );
        //echo $sth->getSQL( array(':producttype' => $producttype, ':moldno' => $moldno) ).HTML_EOL;
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        if($row['cnt']>0){
            return false;
        }     */
    
}
    return 
true;
}
//vdump( moldnoAndSizeIsValid('R', '2455', '53') );

//echo getNextMoldnoByPdt("B");

function my_crypt$data$keyin='' ) {
    
$key = empty( $keyin ) ? "4vH2gMonJENZXD7x" $keyin;
    
//$td = mcrypt_module_open('tripledes', '', 'ecb', '');
    
$td mcrypt_module_open'rijndael-256''''ecb''' );
    
$iv mcrypt_create_ivmcrypt_enc_get_iv_size$td ), MCRYPT_RAND );
    
mcrypt_generic_init$td$key$iv );
    
$outstr mcrypt_generic$td$data );
    
mcrypt_generic_deinit$td );
    
mcrypt_module_close$td );
    return 
base64_encode$outstr );
}

function 
my_decrypt$data$keyin='' ) {
    
$key = empty( $keyin ) ? "4vH2gMonJENZXD7x" $keyin;
    
//$td = mcrypt_module_open('tripledes', '', 'ecb', '');
    
$td mcrypt_module_open'rijndael-256''''ecb''' );
    
$iv mcrypt_create_ivmcrypt_enc_get_iv_size$td ), MCRYPT_RAND );
    
mcrypt_generic_init$td$key$iv );
    
$outstr mdecrypt_generic$tdbase64_decode$data ) );
    
mcrypt_generic_deinit$td );
    
mcrypt_module_close$td );
    return 
$outstr;
}

//$d = my_crypt('this is a test');
//echo my_decrypt($d);

class master {
    private static 
$OprName;
    public static function 
resolveOprName$code$lang="" ) {
        global 
$dbh;
        if ( empty( 
$lang ) ) {
            
$language SYS_LANG;
        } else {
            
$language $lang;
        }

        if ( empty( 
self::$OprName ) ) {
            
$sql "SELECT
            dbo.master_operation.code,
            dbo.master_operation.codedesc_en,
            dbo.master_operation.codedesc_sc
            FROM dbo.master_operation"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( );
            
pdo_showerror$sth$q );
            
self::$OprName $sth->fetchAllPDO::FETCH_ASSOC );
            
//vdump(self::$OprName);
        
}
        foreach ( 
self::$OprName as $key=>$record ) {
            if ( 
$record['code']==$code ) {
                return 
$record['codedesc_'.$language];
            }
        }
        return 
false;
    }

    private static 
$OprPlace;
    public static function 
resolveOprPlace$code$lang="" ) {
        global 
$dbh;
        if ( empty( 
$lang ) ) {
            
$language SYS_LANG;
        } else {
            
$language $lang;
        }

        if ( empty( 
self::$OprPlace ) ) {
            
$sql "SELECT
            dbo.master_operation.code,
            dbo.master_operation.workplace
            FROM dbo.master_operation"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( );
            
pdo_showerror$sth$q );
            
self::$OprPlace $sth->fetchAllPDO::FETCH_ASSOC );
            
//vdump(self::$OprPlace);
        
}
        foreach ( 
self::$OprPlace as $key=>$record ) {
            if ( 
$record['code']==$code ) {
                return 
$record['workplace'];
            }
        }
        return 
false;
    }

    private static 
$Workplace;
    public static function 
resolveWorkplace$code$lang="" ) {
        global 
$dbh;
        if ( empty( 
$lang ) ) {
            
$language SYS_LANG;
        } else {
            
$language $lang;
        }

        if ( empty( 
self::$Workplace ) ) {
            
$sql "SELECT
                    dbo.master_type_code.codeid,
                    dbo.master_type_code.codedesc_en,
                    dbo.master_type_code.codedesc_sc
                    FROM
                    dbo.master_type_code
                    WHERE
                    dbo.master_type_code.typeid = 'WORKPLACE'"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( );
            
pdo_showerror$sth$q );
            
self::$Workplace $sth->fetchAllPDO::FETCH_ASSOC );
        }
        foreach ( 
self::$Workplace as $key=>$record ) {
            
//vdump(self::$Workplace);
            
if ( $record['codeid']==$code ) {
                return 
$record['codedesc_'.$language];
            }
        }
        return 
false;
    }

    private static 
$mattype;
    public static function 
resolveMattype$code$lang="" ) {
        global 
$dbh;
        if ( empty( 
$lang ) ) {
            
$language SYS_LANG;
        } else {
            
$language $lang;
        }

        if ( empty( 
self::$mattype ) ) {
            
$sql "SELECT
                    dbo.master_type_code.codeid,
                    dbo.master_type_code.codedesc_en,
                    dbo.master_type_code.codedesc_sc
                    FROM
                    dbo.master_type_code
                    WHERE
                    dbo.master_type_code.typeid = 'MATTYPE'"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( );
            
pdo_showerror$sth$q );
            
self::$mattype $sth->fetchAllPDO::FETCH_ASSOC );
        }
        foreach ( 
self::$mattype as $key=>$record ) {
            
//vdump(self::$mattype);
            
if ( $record['codeid']==$code ) {
                return 
$record['codedesc_'.$language];
            }
        }
        return 
false;
    }

}
//echo master::resolveMattype("E");
//echo master::resolveWorkplace("WAXD", "sc");
// echo master::resolveOprName("CASO");
// echo master::resolveOprName($code);

function parseBarcodeType$searchType$barcode, &$rtnRefid ) {
    global 
$dbh$system_var;

    if ( empty( 
$searchType ) || $searchType=='ALL' || $searchType=='ORDER' ) {
        if ( 
preg_match'/^(JC|MC|EC)\/\d+$/i'$barcode$str ) ) {
            
$result preg_replace'/^(JC|MC|EC)\//i'''$str );
            
$rtnRefid = (int)$result[0];
            
$result preg_replace'/\/\d+$/i'''$str );
            return 
strtoupper$result[0] );
        }

        if ( 
preg_match'/^(TCI|TFI|TCC|TFC|TMI|TMR|TMC|STX)-\d+$/i'$barcode$str ) ) {
            
$result preg_replace'/^(TCI|TFI|TCC|TFC|TMI|TMR|TMC|STX)-/i'''$str );
            
$rtnRefid = (int)$result[0];
            
$result preg_replace'/-\d+$/i'''$str );
            return 
strtoupper$result[0] );
        }

        if ( 
preg_match'/^(PA|PM|PE)\d+$/i'$barcode$str ) ) {
            
$result preg_replace'/^(PA|PM|PE)/i'''$str );
            
$rtnRefid = (int)$result[0];
            
$result preg_replace'/\d+$/i'''$str );
            return 
strtoupper$result[0] );
        }

        if ( 
preg_match'/^(P|O|X)-[0-9A-Z]{3}(D|A|S|M)\d{8}$/i'$barcode$str ) ) {
            
$sql "SELECT
                    *
                    FROM
                    dbo.ord_pomain
                    WHERE
                    dbo.ord_pomain.pono = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];

                if ( 
$_SESSION['companyid']==$row['buyer'] ) {
                    return 
'PO';
                } else {
                    return 
'SO';
                }
            }
        }

        if ( 
preg_match'/^(P|O|X)-[0-9A-Z]{3}(D|A|S|M)\d{8}(CN)$/i'$barcode$str ) ) {
            
$sql "SELECT
                    *
                    FROM
                    dbo.cn_rm_main
                    WHERE
                    dbo.cn_rm_main.cnrq_nbr = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];

                if ( 
$_SESSION['companyid']==$row['cn_customer'] ) {
                    return 
'CNRMRQ';
                } else {
                    return 
'CNRM';
                }
            }
        }


        if ( 
preg_match'/^(ECH)\d{2}-\d{2}-\d{3}$/i'$barcode$str ) ) { //IMI HK
            
$sql "SELECT
                    *
                    FROM
                    dbo.ivc_rawmat_main
                    WHERE
                    dbo.ivc_rawmat_main.ivc_nbr = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'IMI';
            }
        }

        if ( 
preg_match'/^(ECH)\d{2}-\d{2}-\d{3}(CN)$/i'$barcode$str ) ) { //IMCN HK
            
$sql "SELECT
                    *
                    FROM
                    dbo.cn_rm_main
                    WHERE
                    dbo.cn_rm_main.cn_nbr = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'IMCN';
            }
        }

        if ( 
preg_match'/^(E)\d{2}-\d{5}$/i'$barcode$str ) ) { //IVCCUS HK
            
$sql "SELECT
                    *
                    FROM
                    dbo.ivc_cus
                    WHERE
                    dbo.ivc_cus.ivc_nbr = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( array(':barcode'=>$barcode)  );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'IVCCUS';
            }
        }

        if ( 
preg_match'/^(E)\d{2}-\d{5}(CN)$/i'$barcode$str ) ) { //CNCUS HK
            
$sql "SELECT
                    *
                    FROM
                    dbo.cn_cus_main
                    WHERE
                    dbo.cn_cus_main.cn_nbr = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( array(':barcode'=>$barcode)  );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'CNCUS';
            }
        }

        if ( 
preg_match'/^(EZ)\d{2}-\d{2}-\d{3}$/i'$barcode$str ) ) { // IMI/IVCFTY/IVCCUS
            
$sql "
            SELECT 'IVCFTY' AS type, dbo.ivc_fty.refid FROM dbo.ivc_fty
                WHERE dbo.ivc_fty.ivc_nbr = :barcode1 UNION
            SELECT 'IVCCUS' as type, dbo.ivc_cus.refid FROM dbo.ivc_cus
                WHERE dbo.ivc_cus.ivc_nbr = :barcode2 UNION
            SELECT 'IMI' AS type, dbo.ivc_rawmat_main.refid FROM dbo.ivc_rawmat_main
                WHERE dbo.ivc_rawmat_main.ivc_nbr = :barcode3    "
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode1'=>$barcode':barcode2'=>$barcode':barcode3'=>$barcode ) );
            
//echo $sth->getSQL(  );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
$row['type'];
            }
        }

        if ( 
preg_match'/^(EZ)\d{2}-\d{2}-\d{3}(CN)$/i'$barcode$str ) ) {// IMCN/CNFTY/CNCUS
            
$sql "
            SELECT 'CNFTY' AS type, dbo.cn_int_main.refid FROM dbo.cn_int_main
                WHERE dbo.cn_int_main.cn_nbr = :barcode1 UNION
            SELECT 'CNCUS' as type, dbo.cn_cus_main.refid FROM dbo.cn_cus_main
                WHERE dbo.cn_cus_main.cn_nbr = :barcode2 UNION
            SELECT 'IMCN' AS type, dbo.cn_rm_main.refid FROM dbo.cn_rm_main
                WHERE dbo.cn_rm_main.cn_nbr = :barcode3    "
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode1'=>$barcode':barcode2'=>$barcode':barcode3'=>$barcode ) );
            
//echo $sth->getSQL( array(':barcode1'=>$barcode, ':barcode2'=>$barcode, ':barcode3'=>$barcode) );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
$row['type'];
            }
        }

        if ( 
preg_match'/^(R-)?[0-9A-Z]{3}\d{5}$/i'$barcode$str ) ) {
            
$sql "SELECT refid
                    FROM
                    dbo.ord_main
                    WHERE
                    dbo.ord_main.order_nbr = :order_nbr"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':order_nbr'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'ORDER';
            }
        }

        if ( 
preg_match'/^(WK)/i'$barcode$str ) ) {
            
$sql "SELECT
                    *
                    FROM
                    dbo.master_user
                    WHERE
                    dbo.master_user.barcodepwd = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $row['refid'];
                return 
'WORKER';
            }
        }

        if ( 
preg_match'/^CDN-\d+$/i'$barcode$str ) ) {
            
$rtnRefid $barcode;
            return 
'CDN';
            
$sql "SELECT
                    *
                    FROM
                    dbo.master_user
                    WHERE
                    dbo.master_user.barcodepwd = :barcode"
;
            
$sth $dbh->prepare$sql );
            
$q $sth->execute( array( ':barcode'=>$barcode ) );
            
//echo $sth->getSQL( );
            
pdo_showerror$sth$q );
            if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
                
$rtnRefid $barcode;
                return 
'WORKER';
            }
        }
    }

    if ( empty( 
$searchType ) || $searchType=='ALL' || $searchType=='ITEMNO' ) {
        
$sql "SELECT 'PRODUCT' as type, refid FROM inv_product WHERE itemno = :itemno0 UNION
                SELECT 'DIAMOND' as type, refid FROM inv_diamond WHERE itemno = :itemno1 UNION
                SELECT 'STONE' as type, refid FROM inv_stone WHERE itemno = :itemno2 UNION
                SELECT 'ACCESSORY' as type, refid FROM inv_accessory WHERE itemno = :itemno3 UNION
                SELECT 'MATERIAL' as type, refid FROM inv_material WHERE itemno = :itemno4"
;
        
$sth $dbh->prepare$sql );
        
$q $sth->execute( array( ':itemno0'=>$barcode':itemno1'=>$barcode':itemno2'=>$barcode':itemno3'=>$barcode':itemno4'=>$barcode ) );
        
//echo $sth->getSQL( array(':itemno0'=>$barcode, ':itemno1'=>$barcode, ':itemno2'=>$barcode, ':itemno3'=>$barcode, ':itemno4'=>$barcode) );
        
pdo_showerror$sth$q );
        if ( 
$row $sth->fetchPDO::FETCH_ASSOC ) ) {
            
$rtnRefid $row['refid'];
            return 
$row['type'];
        }
    }

    
$rtnRefid 0;
    
//return '404';
    
return false;
}

function 
getBarcodeDir$barcodeType$refid=false ) {
    global 
$dbh$system_var;

    
$barcodeType strtoupper$barcodeType );
    switch ( 
$barcodeType ) {
    case 
'JC':
        
$info['desc'] = JOB_CARD;
        
$info['path'] = 'ord_dtl_product_modifyform.php?refid=$refid';
        break;
    case 
'MC':
        
$info['desc'] = MATERIAL.WS.JOB_CARD;
        
$info['path'] = '';
        break;
    case 
'EC':
        
$info['desc'] = EXTRA.WS.JOB_CARD;
        
$info['path'] = '';
        break;

    case 
'PA':
        
$info['desc'] = PRODUCT;
        
$info['path'] = '';
        break;
    case 
'PM':
        
$info['desc'] = MATERIAL.WS.PRODUCT;
        
$info['path'] = '';
        break;
    case 
'PE':
        
$info['desc'] = EXTRA.WS.PRODUCT;
        
$info['path'] = '';
        break;

    case 
'ORDER':
        
$info['desc'] = ORDER;
        
$info['path'] = 'ord_dtl_modifyform.php?refid=$refid';
        break;
    case 
'PO':
        
$info['desc'] = PO;
        
$info['path'] = 'ord_po_modifyform.php?refid=$refid';
        break;
    case 
'SO':
        
$info['desc'] = SO;
        
$info['path'] = 'ord_so_modifyform.php?refid=$refid';
        break;

    case 
'TMI':
    case 
'IMI':
        
$info['desc'] = MATERIAL.WS.INVOICE;
        
$info['path'] = 'ivc_rawmat_modifyform.php?refid=$refid';
        break;
    case 
'TMR':
    case 
'CNRMRQ':
        
$info['desc'] = MATERIAL.WS.CREDIT_NOTE.WS.REQUEST;
        
$info['path'] = 'cnr_rm_modifyform.php?refid=$refid';
        break;
    case 
'TMC':
    case 
'CNRM':
    case 
'IMCN':
        
$info['desc'] = MATERIAL.WS.CREDIT_NOTE;
        
$info['path'] = 'cn_rm_modifyform.php?refid=$refid';
        break;
    case 
'TCI':
    case 
'IVCCUS':
        
$info['desc'] = CUSTOMER.WS.INVOICE;
        
$info['path'] = 'ivc_cus_modifyform.php?refid=$refid';
        break;
    case 
'TCC':
    case 
'CNCUS':
        
$info['desc'] = CUSTOMER.WS.CREDIT_NOTE;
        
$info['path'] = 'cn_cus_modifyform.php?refid=$refid';
        break;
    case 
'TFI':
    case 
'IVCFTY':
        
$info['desc'] = FACTORY.WS.INVOICE;
        
$info['path'] = 'ivc_fty_modifyform.php?refid=$refid';
        break;
    case 
'TFC':
    case 
'CNFTY':
        
$info['desc'] = FACTORY.WS.CREDIT_NOTE;
        
$info['path'] = 'cn_fty_modifyform.php?refid=$refid';
        break;

    case 
'STX':
        
$info['desc'] = STOCK.WS.TRANSFER;
        
$info['path'] = 'inv_xfer_modifyform.php?refid=$refid';
        break;

    case 
'PRODUCT':
        
$info['desc'] = PRODUCT;
        
$info['path'] = 'inv_product_modifyform.php?refid=$refid';
        break;
    case 
'STONE':
        
$info['desc'] = STONE;
        
$info['path'] = 'inv_stone_modifyform.php?refid=$refid';
        break;
    case 
'DIAMOND':
        
$info['desc'] = DIAMOND;
        
$info['path'] = 'inv_diamond_modifyform.php?refid=$refid';
        break;
    case 
'ACCESSORY':
        
$info['desc'] = ACCESSORY;
        
$info['path'] = 'inv_accessory_modifyform.php?refid=$refid';
        break;
    case 
'MATERIAL':
        
$info['desc'] = MATERIAL;
        
$info['path'] = 'inv_material_modifyform.php?refid=$refid';
        break;
    case 
'WORKER':
        
$info['desc'] = WORKER;
        
$info['path'] = 'master_user_modifyform.php?refid=$refid';
        break;
    case 
'CDN':
        
$info['desc'] = CASTING_DEAL;
        
$info['path'] = 'ord_castdeal_modifyform.php?delnote_nbr=$refid';
        break;
    default:
        return 
false;
    }

    if ( 
$refid ) {
        
$info['path'] = preg_replace'/\$refid/'$refid$info['path'] );
    }


    return 
$info;
}

class 
USD_CNY {

    public static 
$rate;

    static function 
init$formCurrency$rate ) {
        
$rate = (float)$rate;
        if ( 
abs$rate )>0.0001 ) {
            if ( 
$fromCurrency=='USD' ) {
                
self::$rate $rate;
            }else {
                
self::$rate 1.0/$rate;
            }
        }
        
self::checkInit();
    }

    
//use specified date exchange rate
    
static function initByDate$date ) {
        
self::$rate getDB_mat_pricelist'usd-cny'$date );
        
self::checkInit();
    }

    private static function 
checkInit() {
        if ( !
self::$rate ) {
            
self::$rate getDB_mat_pricelist'usd-cny' );
        }
    }

    static function 
getOppCurrency$fromCurrecny$option='code' ) {
        
//vdump($option);
        
if ( $option=='name' ) {
            if ( 
$fromCurrecny=='USD' ) {
                return 
constant'CNY' );
            }else {
                return 
constant'USD' );
            }
        }else {
            if ( 
$fromCurrecny=='USD' ) {
                return 
'CNY';
            }else {
                return 
'USD';
            }
        }
    }

    static function 
getCurrencyRateFrom$fromCurrecny='USD'$toCurrency=null ) {
        
self::checkInit();
        if ( 
$fromCurrecny == $toCurrency ) {
            return 
1.0;
        }
        elseif ( 
$fromCurrecny=='USD' ) {
            return 
self::$rate;
        }
        else {
            return 
1.0/self::$rate;
        }
    }

    static function 
getCurrencyRateTo$toCurrecny='USD' ) {
        return 
self::getCurrencyRateFromself::getOppCurrency$toCurrecny ) );
    }

    static function 
getAmount$fromCurrecny='USD'$amount=1$toCurrency='USD' ) {
        if ( 
$toCurrency=='USD' ) {
            return 
self::getUSDAmount$fromCurrecny$amount );
        }else {
            
//20140313 changed from getUSDAmount to getCNYAmount
            
return self::getCNYAmount$fromCurrecny$amount );
        }
    }

    static function 
getUSDAmount$fromCurrecny='USD'$amount=) {
        if ( 
$fromCurrecny=='USD' ) {
            return 
$amount;
        }else {
            return 
$amount self::getCurrencyRateFrom$fromCurrecny );
        }
    }

    static function 
getCNYAmount$fromCurrecny='CNY'$amount=) {
        if ( 
$fromCurrecny=='CNY' ) {
            return 
$amount;
        }else {
            return 
$amount self::getCurrencyRateFrom$fromCurrecny );
        }
    }

    static function 
getOppAmt$fromCurrecny='CNY'$amount=) {
        if ( 
$fromCurrecny=='CNY' ) {
            return 
self::getUSDAmount$fromCurrecny$amount );
        }else {
            return 
self::getCNYAmount$fromCurrecny$amount );
        }
    }
}

/*
USD_CNY::initByDate('2013-12-31 09:40:25');
vdump(USD_CNY::getUSDAmount('CNY', 100) );
USD_CNY::initByDate('2013-11-05 09:51:00');
vdump(USD_CNY::getUSDAmount('CNY', 100) );
USD_CNY::initByDate('2013-10-16 08:52:00');
vdump(USD_CNY::getUSDAmount('CNY', 100) );

vdump( USD_CNY::getAmount('USD', 100, 'USD') );
vdump( USD_CNY::getAmount('USD', 100, 'CNY') );
vdump( USD_CNY::getAmount('CNY', 100, 'USD') );
vdump( USD_CNY::getAmount('CNY', 100, 'CNY') );
*/
//vdump( USD_CNY::getCurrencyRateFrom('CNY') );
//var_dump( USD_CNY::getUSDAmount('CNY', '100') );

class USD_EUR {

    public static 
$rate;

    static function 
init$formCurrency$rate ) {
        
$rate = (float)$rate;
        if ( 
abs$rate )>0.0001 ) {
            if ( 
$fromCurrency=='USD' ) {
                
self::$rate $rate;
            }else {
                
self::$rate 1.0/$rate;
            }
        }
        
self::checkInit();
    }

    
//use specified date exchange rate
    
static function initByDate$date ) {
        
self::$rate getDB_mat_pricelist'usd-eur'$date );
        
self::checkInit();
    }

    private static function 
checkInit() {
        if ( !
self::$rate ) {
            
self::$rate getDB_mat_pricelist'usd-eur' );
        }
    }

    static function 
getOppCurrency$fromCurreeur$option='code' ) {
        
//vdump($option);
        
if ( $option=='name' ) {
            if ( 
$fromCurreeur=='USD' ) {
                return 
constant'EUR' );
            }else {
                return 
constant'USD' );
            }
        }else {
            if ( 
$fromCurreeur=='USD' ) {
                return 
'EUR';
            }else {
                return 
'USD';
            }
        }
    }

    static function 
getCurrencyRateFrom$fromCurreeur='USD'$toCurrency=null ) {
        
self::checkInit();
        if ( 
$fromCurreeur == $toCurrency ) {
            return 
1.0;
        }
        elseif ( 
$fromCurreeur=='USD' ) {
            return 
self::$rate;
        }
        else {
            return 
1.0/self::$rate;
        }
    }

    static function 
getCurrencyRateTo$toCurreeur='USD' ) {
        return 
self::getCurrencyRateFromself::getOppCurrency$toCurreeur ) );
    }

    static function 
getAmount$fromCurreeur='USD'$amount=1$toCurrency='USD' ) {
        if ( 
$toCurrency=='USD' ) {
            return 
self::getUSDAmount$fromCurreeur$amount );
        }else {
            
//20140313 changed from getUSDAmount to getCNYAmount
            
return self::getEURAmount$fromCurreeur$amount );
        }
    }

    static function 
getUSDAmount$fromCurreeur='USD'$amount=) {
        if ( 
$fromCurreeur=='USD' ) {
            return 
$amount;
        }else {
            return 
$amount self::getCurrencyRateFrom$fromCurreeur );
        }
    }

    static function 
getEURAmount$fromCurreeur='EUR'$amount=) {
        if ( 
$fromCurreeur=='EUR' ) {
            return 
$amount;
        }else {
            return 
$amount self::getCurrencyRateFrom$fromCurreeur );
        }
    }

    static function 
getOppAmt$fromCurreeur='EUR'$amount=) {
        if ( 
$fromCurreeur=='EUR' ) {
            return 
self::getUSDAmount$fromCurreeur$amount );
        }else {
            return 
self::getEURAmount$fromCurreeur$amount );
        }
    }
}



class 
sys_tmp {

    static function 
save$ent$attr$val ) {
        global 
$dbh;
        
$sql "INSERT INTO sys_tmp (entity, attr, v) VALUES (:e, :a, :v)";
        
$sth $dbh->prepare$sql );
        
$q $sth->execute(  array( ':e' => $ent,
                
':a' => $attr,
                
':v' => $val ) );
        
// echo $sth->getSQL(  array(':e' => $ent,
        // ':a' => $attr,
        // ':v' => $val) ).HTML_EOL;
        
pdo_showerror$sth$q );
        return 
$dbh->lastInsertId();
    }

}
//vdump( sys_tmp::save(111,222,333) );