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

function 
casd_checkout($orddtl_refid$opr_param$txcasd=''){
    
//write tx details to $param['subtable']
    //vdump($txcasd);
    
global $dbh;        
    
$sql "UPDATE ord_txcasd SET
                act_returndate = :act_returndate,
                act_qty = :act_qty,
                pure_act_qty = :pure_act_qty,
                status = :status,
                lastupby = :lastupby,
                lastupdate = GETDATE()
            WHERE refid = :casd_refid AND ordsubroute_refid = :ordsubroute_refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':act_returndate' => $opr_param['act_returndate'],
                            
':act_qty' => (float)$txcasd['act_qty'],
                            
':pure_act_qty' => (float)$txcasd['pure_act_qty'],
                            
':lastupby' => $opr_param['lastupby'], 
                            
':status' => $opr_param['status'],
                            
':casd_refid' => (int)$txcasd['casd_refid'],
                            
':ordsubroute_refid' => (int)$txcasd['subroute_refid']) );    
/*    echo $sth->getSQL(  array(':act_returndate' => $opr_param['act_returndate'],
                            ':act_qty' => (float)$txcasd['act_qty'],
                            ':pure_act_qty' => (float)$txcasd['pure_act_qty'],
                            ':lastupby' => $opr_param['lastupby'], 
                            ':status' => $opr_param['status'],
                            ':casd_refid' => (int)$txcasd['casd_refid'],
                            ':ordsubroute_refid' => (int)$txcasd['subroute_refid']) ).HTML_EOL;*/        
    
pdo_showerror($sth$q);                                                
    
    
$subroute getDB('ord_subroute'$txcasd['subroute_refid']);
    
    
//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 = :ordsubroute_refid"
;    
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':act_opr_svc' => (float)$txcasd['act_qty'],    
                            
':uom_svc' => $txcasd['uom_wt'],
                            
':lastupby' => $opr_param['lastupby'],
                            
':ordsubroute_refid' => (int)$txcasd['subroute_refid']) );    
/*    echo $sth->getSQL( array(':act_opr_svc' => (float)$txcasd['act_qty'],    
                            ':uom_svc' => $txcasd['uom_wt'],
                            ':lastupby' => $opr_param['lastupby'],
                            ':ordsubroute_refid' => (int)$txcasd['subroute_refid'])  ).HTML_EOL;        */
    
pdo_showerror($sth$q);    
    
    
$inv_material=getDB_where('inv_material''itemno=:itemno', array(':itemno'=>$subroute['itemno']));
    
    
//write the actual weight to BOM
    
$sql "UPDATE ord_item_bom SET 
            actual_qty = actual_qty + :actual_qty, 
            lastupby = :lastupby,
            lastupdate = GETDATE()
            WHERE orddtl_refid = :orddtl_refid
                AND bomcategy = 'material'
                AND itemnoid = :itemnoid"
;    
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':actual_qty' => (float)$txcasd['act_qty'],
                            
':lastupby' => $opr_param['lastupby'],
                            
':orddtl_refid' => $orddtl_refid,
                            
':itemnoid' => (int)$inv_material['refid']) );    
/*    echo $sth->getSQL(  array(':actual_qty' => (float)$txcasd['act_qty'],
                            ':lastupby' => $opr_param['lastupby'],
                            ':orddtl_refid' => $orddtl_refid,
                            ':itemnoid' => (int)$inv_material['refid'])).HTML_EOL;    */    
    
pdo_showerror($sth$q);                                                   
}



function 
updateFirstCastingRateDate($ordmain_refid$mattype='gold'){
    global 
$dbh;        
    if(
$mattype=='gold'){
        
$sql "UPDATE ord_main SET 
                firstgoldcast_checkoutdate = GETDATE()
                WHERE refid = :refid"
;
    }elseif(
$mattype=='silver'){
        
$sql "UPDATE ord_main SET 
                firstsilvercast_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);    
}


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

if( 
$_REQUEST['action']=="ord_checkout_submit" && !empty($refid) ){
    
    
$curropr ord_currentOprDept($refid);
    if(
$curropr['opr_code']!='CASO'){
        
$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"; //manually run casd_checkout
    
$opr_param['status']        = "finished";
    
$opr_param['txmain_refid']    = $curropr['ordtxmain_refid'];
    
$txmain getDB('ord_txmain'$opr_param['txmain_refid']);
    
    
$opr_param['route_refid']    = $curropr['refid'];
    
$opr_param['ttl_wt']        = (float)($txmain['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']);
    
$ordmain getDB("ord_main"$orddtl['main_refid']);
    
//vdump($_REQUEST, $refid, $opr_param);
    
    
$dbh->beginTransaction();
    
    foreach((array)
$_POST['mat'] as $txcasd){        
        
$org_ord_txcasd getDB('ord_txcasd'$txcasd['casd_refid']);
        
$txcasd['pure_act_qty'] = convToPureMatWt($org_ord_txcasd['mattype'], $txcasd['act_returnwt']);
        
$txcasd['act_qty'] = $txcasd['act_returnwt'];
        
casd_checkout($refid$opr_param$txcasd);
    }
    
    
//check all MELT are finished
    
$MELT_ops casd_build_subroute_array($refid$curropr['refid'], 'MELT');
    
    
$finished_MELT true;
    foreach(
$MELT_ops as $op){
        if(!
$op['act_opr_svc']){
            
$finished_MELT false;    
        }
        
//total weight to be stored in txmain
        
$opr_param['ttl_wt'] += $op['act_opr_svc'];
    }
    
    if(
$finished_MELT || $dept_checkout){ //or all MELT subop are finished
        
$opr_param['uom_wt'] = 'gr';
        
$checkout_time ord_checkout($refid$opr_param);
    }
    
    if( 
$org_ord_txcasd['pure_mattype']=='A'){
        
//echo "update casting gold rate";        
        
        
if( empty($ordmain['firstgoldcast_checkoutdate']) && empty($ordmain['goldrate'])){
            
$goldPerOunce ordmain_updategoldRate($orddtl['main_refid']); //with today gold rate per ounce
            
updateFirstCastingRateDate($orddtl['main_refid'], 'gold');
            
            
// update bom's material unitprice with specified pure metal rate (fot the whole order)
            
updateOrderBOMMaterialPrice($orddtl['main_refid'], $goldPerOunce);    
        }else{
            
//$goldPerOunce = (float)$ordmain['goldrate'];
        
}        
            
    } elseif( 
$org_ord_txcasd['pure_mattype']=='@' && empty($ordmain['firstsilvercast_checkoutdate']) ){
        
//echo "update casting silver rate";        
        
updateFirstCastingRateDate($orddtl['main_refid'], 'silver');
    }
    
    
//exit;
    
$dbh->commit();    
    
    
//form_dest($_REQUEST['godest'], $_REQUEST['formdest']);
    
header("Location: ord_castcheckout_submitform.php?refid=$refid&msg=Saved.");
    print 
"Saved.";
    exit;
    
    
}
print 
"Invalid Request";


?>