/var/www/enzatesting.onesolution.hk/01_20240711_full_backup/ord_castcheckout_submit.new.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
<?php 
$formid 
"Order";
require_once 
"inc/configure.php";

function 
casd_checkout($orddtl_refid$opr_param){
    
//write tx details to $param['subtable']
    
global $dbh;        
    
$sql "UPDATE ".$opr_param['subtable']." SET
                act_returndate=:act_returndate,
                status=:status,
                lastupby = :lastupby,
                lastupdate = GETDATE()
                where act_returndate is NULL AND txmain_refid=:txmain_refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':txmain_refid' => $opr_param['txmain_refid'],                                             
                            
':act_returndate' => $opr_param['act_returndate'],
                            
':lastupby' => $opr_param['lastupby'], 
                            
':status' => $opr_param['status']) );    
/*    echo $sth->getSQL(  array(':txmain_refid' => $opr_param['txmain_refid'],                                             
                            ':act_returndate' => $opr_param['act_returndate'],
                            ':status' => $opr_param['status'])  );*/        
    
pdo_showerror($sth$q);                                                
    
    
    
//write the actual weight to MELT suboperation
    
$sql "update ord_subroute set 
            act_opr_svc = :act_opr_svc, 
            uom_svc = :uom_svc,
            lastupby = :lastupby,
            lastupdate = GETDATE()
            where refid = (SELECT top (1)
                            dbo.ord_subroute.refid                            
                            FROM
                            dbo.ord_subroute
                            INNER JOIN dbo.ord_route ON dbo.ord_route.refid = dbo.ord_subroute.ordroute_refid
                            WHERE
                            dbo.ord_route.ordtxmain_refid = :txmain_refid AND
                            (dbo.ord_subroute.act_opr_svc = 0  OR dbo.ord_subroute.act_opr_svc is null) AND
                            dbo.ord_subroute.opr_code = :opr_code )"
;    
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':act_opr_svc' => $opr_param['ttl_wt'],    
                            
':uom_svc' => $opr_param['uom_wt'],
                            
':lastupby' => $opr_param['lastupby'],     
                            
':opr_code' => 'MELT',    
                            
':txmain_refid' => $opr_param['txmain_refid']) );    
/*    echo $sth->getSQL(  array(':act_opr_svc' => $opr_param['ttl_wt'],    
                            ':uom_svc' => $opr_param['uom_wt'],    
                            ':lastupby' => $opr_param['lastupby'],
                            ':opr_code' => 'MELT',    
                            ':txmain_refid' => $opr_param['txmain_refid'])  );    */    
    
pdo_showerror($sth$q);                                                   
}



function 
updateCastingRate($ordmain_refid){
    global 
$dbh;        
    
$sql "UPDATE ord_main SET 
            firstgoldcast_checkoutdate = GETDATE()
            WHERE refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute( array(':refid' => $ordmain_refid) );    
    echo 
$sth->getSQL( array(':refid' => $ordmain_refid)  );        
    
pdo_showerror($sth$q);    
}

function 
updateOrderBOMMaterialPrice($ordmain_refid){
    return 
false;
    global 
$dbh;    
    
$lastupby    filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    
$ordmain getDB("ord_main"$ordmain_refid);
        
    
// get order bom item where material is gold or silver    
    
$sql "SELECT
            dbo.inv_material.mattype,
            dbo.master_type_code.ext2,
            dbo.ord_item_bom.refid,
            dbo.ord_item_bom.itemnoid            
            FROM
            dbo.ord_dtl
            INNER JOIN dbo.ord_item_bom ON dbo.ord_item_bom.orddtl_refid = dbo.ord_dtl.refid
            INNER JOIN dbo.inv_material ON dbo.ord_item_bom.itemnoid = dbo.inv_material.refid
            INNER JOIN dbo.master_type_code ON dbo.master_type_code.codeid = dbo.inv_material.mattype
            WHERE
            dbo.ord_item_bom.bomcategy = 'material' AND
            dbo.master_type_code.typeid = 'MATTYPE' AND
            dbo.master_type_code.ext1 IN ('gold', 'silver') AND
            dbo.ord_dtl.main_refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':refid' => $ordmain_refid) );    
    echo 
$sth->getSQL( array(':refid' => $ordmain_refid)  ).HTML_EOL;        
    
pdo_showerror($sth$q);    
    while( 
$bom $sth->fetch(PDO::FETCH_ASSOC) ){            
    
        
$unitprice materialUnitPrice($bom['itemnoid']);
        
//update material unitprice with current material weight
        
$sql "UPDATE ord_item_bom SET
                        unitprice = :unitprice,
                        lastupby=:lastupby,
                        lastupdate=GETDATE()
                    where refid = :refid"
;
                    
        
$sth2 $dbh->prepare($sql);
        
$q $sth2->execute( array(':refid'=>$bom['refid'], 
                                    
':unitprice'=>$unitprice,
                                    
':lastupby'=>$lastupby) );
        echo 
$sth2->getSQL( array(':refid'=>$bom['refid'], 
                                    
':unitprice'=>$unitprice,
                                    
':lastupby'=>$lastupby) ).HTML_EOL
                                    
        
updateProductCost($orddtl_refid);                            
    }
    
updateMainValue($ordmain_refid);
}

//vdump($_REQUEST); exit;    
$refid                 = (int)filter_var($_REQUEST['refid'], FILTER_SANITIZE_STRING);

if( 
$_REQUEST['action']=="ord_checkout_submit" && !empty($refid) ){
    
    
$curropr ord_currentOprDept($refid);
    if(
$curropr['opr_code']!='CASO'){
        echo 
$msg "CASO is not in progress";
        
header("Location: ord_castcheckout_submitform.php?refid=$refid&msg=$msg.");
        exit;
    }
    
    
$opr_param['opr_code']        = "CASO";
    
$opr_param['subtable']         = "ord_txcasd";
    
$opr_param['php_function']    = "casd_checkout";
    
$opr_param['status']        = "finished";
    
$opr_param['txmain_refid']    = $curropr['ordtxmain_refid'];
    
$opr_param['route_refid']    = $curropr['refid'];
    
$opr_param['ttl_wt']        = filter_var($_REQUEST['act_returnwt'], FILTER_SANITIZE_STRING);
    
$opr_param['uom_wt']        = filter_var($_REQUEST['uom_wt'], FILTER_SANITIZE_STRING);        
    
$opr_param['worker']         = filter_var($_REQUEST['worker'], FILTER_SANITIZE_STRING);
    
$opr_param['act_returndate']     = filter_var($_REQUEST['act_returndate'], FILTER_SANITIZE_STRING);
    
$opr_param['qc']            = filter_var($_REQUEST['qc'], FILTER_SANITIZE_STRING);    
    
$opr_param['createby']        = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
$opr_param['lastupby']        = filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
    
$txmain getDB("ord_txmain"$curropr['ordtxmain_refid']);
    
$orddtl getDB("ord_dtl"$txmain['orddtl_refid']);
    
//vdump($orddtl, $opr_param);
    
    
$dbh->beginTransaction();
    
$checkout_time ord_checkout($refid$opr_param);
    
    if( empty(
$orddtl['firstgoldcast_checkoutdate']) ){
        echo 
"update casting rate";
        
//vdump($orddtl['refid'], $orddtl['main_refid']);
        
updateCastingRate($orddtl['main_refid']);
        
        
// todo: update bom's material uniitprice with today pure metal rate
        
updateOrderBOMMaterialPrice($orddtl['main_refid']);
    }
    
//exit;
    
$dbh->commit();    

//echo "msg=$msg";            
//exit;
    
    //form_dest($_REQUEST['godest'], $_REQUEST['formdest']);
    //header("Location: ord_castcheckout_submitform.php?refid=$refid&msg=Saved.");
    
print "Saved.";
    exit;
    
    
}
print 
"Invalid Request";


?>