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

function 
getIvcSummary($refid){
    global 
$dbh;
    
$sql "SELECT
            sum(dbo.ivc_rawmat_dtl.unit_price * dbo.ivc_rawmat_dtl.qty) as ttl_price
            FROM
            dbo.ivc_rawmat_dtl
            WHERE
            dbo.ivc_rawmat_dtl.ivcrawmatmain_refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':refid' => $refid) );    
    
//echo $sth->getSQL(  array(':refid' => $refid) );    
    
$row $sth->fetch();
    
pdo_showerror($sth$q);    

    return 
$row;
    
}
//getIvcSummary(5);
//exit;

function updateIvcSummary($refid){
    
    global 
$dbh;
    
$summary getIvcSummary($refid);
    
    
$sql "UPDATE ivc_rawmat_main SET 
                ttl_price = :ttl_price,
                uom_cy = :uom_cy,
                lastupby = :lastupby, 
                lastupdate = GETDATE()
            WHERE refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':ttl_price'     => (float)filter_var($summary['ttl_price'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),                             
                            
':uom_cy'             => filter_var('USD'FILTER_SANITIZE_STRING),
                            
':lastupby'         => filter_var($_SESSION['user'], FILTER_SANITIZE_STRING),
                            
':refid'             => $refid) );    
    
pdo_showerror($sth$q);        
}


$refid                 = (int) filter_var($_REQUEST['refid'], FILTER_SANITIZE_NUMBER_INT);

if( 
$_POST['action']=="ivc_rawmat_modifyform" && !empty($refid)  && $ivc=getDB("ivc_rawmat_main"$refid)){

//vdump($_REQUEST); //exit;    
    
$issue_ivcnbr        filter_var($_REQUEST['issue_ivcnbr'], FILTER_SANITIZE_STRING);
    
$status                filter_var($_REQUEST['status'], FILTER_SANITIZE_STRING);
    
$launch_date        filter_var($_REQUEST['launch_date'], FILTER_SANITIZE_STRING);    
    
$due_date            filter_var($_REQUEST['due_date'], FILTER_SANITIZE_STRING);
    
$companyid            filter_var($_REQUEST['companyid'], FILTER_SANITIZE_STRING);
    
$createby            filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);
    
$lastupby            filter_var($_SESSION['user'], FILTER_SANITIZE_STRING);

    if(
$status=="FINISHED") {
        
$sql_update "finished=1, ";
    }else{
        
$sql_update "finished=NULL, ";
    }
        
$dbh->beginTransaction();
//modify invoice
$sql "UPDATE ivc_rawmat_main SET
                due_date = :due_date,
                status = :status,
                
$sql_update
                lastupby = :lastupby, 
                lastupdate =GETDATE()
            WHERE refid = :refid"
;
$sth $dbh->prepare($sql);
$q $sth->execute(  array(    ':refid' => $refid,
                        
':due_date' => $due_date
                        
':status' => $status
                        
':lastupby' => $lastupby) );    
/*echo $sth->getSQL( );*/    
pdo_showerror($sth$q);

if(
$issue_ivcnbr){
  list(
$ivc_nbr$seq) = cusivc_getNewIvcNbrSeq("ivc_rawmat"$ivc['companyid']);
  
  
$sql "UPDATE ivc_rawmat_main SET
                ivc_nbr = :ivc_nbr, 
                seq = :seq,
                lastupby = :lastupby, 
                lastupdate =GETDATE()
            WHERE refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(':ivc_nbr' => $ivc_nbr
                            
':seq' => $seq
                            
':refid' => $refid
                            
':lastupby' => $lastupby) );    
/*    echo $sth->getSQL(   array(':due_date' => $due_date, 
                            ':status' => $status, 
                            ':refid' => $refid, 
                            ':lastupby' => $lastupby) );*/    
    
pdo_showerror($sth$q);
    
//vdump($seq, $ivc_nbr);
}

if(
$ivc['status']=="INPROGRESS"){
    
    
//echo"modify detail"; exit;
    //remove rawmat_dtl for this rawmat_main
/*    $sql = "DELETE FROM ivc_rawmat_dtl
            WHERE ivcrawmatmain_refid = :refid";*/
    
$sql "UPDATE ivc_rawmat_dtl SET
                setused = 0
            WHERE ivcrawmatmain_refid = :refid"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(    ':refid' => $refid ) );
    
//echo $sth->getSQL(  array(    ':refid' => $refid ) );
    
    
foreach ($_REQUEST['invoice_check'] as $key => $value){
        if(
$key>&& !empty($value) && $value['checked']){
            
            
//vdump($value); //continue;
            
$po_dtl getDB("ord_podtl"$value['refid']);
            
$po_main getDB("ord_pomain"$po_dtl['pomain_refid']); 
            
//vdump($po_main['pono']);
            
$table inv_getBomcategyTablename($po_dtl['bomcategy']);
            
$item getDB($table$po_dtl['itemnoid']);
            
    
/*        if($po_dtl['uom_qty']==$item['uom_wt']){
                $received = poset_getReceivedPOWt($po_dtl['refid']);
            } else {
                $received = poset_getReceivedPOQty($po_dtl['refid']); 
            }*/
            
            
$qty = (float)filter_var($value['qty'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
            
$unit_price = (float)filter_var($value['unit_price'], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
            
            
            if(
$value['ivcrawmat_refid']){
            
//update dtl    
            
$sql "UPDATE ivc_rawmat_dtl SET
                        qty = :qty, 
                        unit_price = :unit_price,
                        lastupby = :lastupby,
                        lastupdate = GETDATE(),
                        setused = 1
                    WHERE refid = :refid"
;
            
$sth $dbh->prepare($sql);
            
$q $sth->execute(  array(    ':qty' => $qty,
                                    
':unit_price' => $unit_price
                                    
':lastupby' => $lastupby,                            
                                    
':refid' => $value['ivcrawmat_refid']) );    
/*            echo $sth->getSQL(  array(    ':qty' => $qty,
                                    ':unit_price' => $unit_price, 
                                    ':lastupby' => $lastupby,                            
                                    ':refid' => $value['ivcrawmat_refid']) ).HTML_EOL;    */                
                
            
}else{
                
//insert dtl    
                
$sql "INSERT INTO ivc_rawmat_dtl(
                        ivcrawmatmain_refid, ordpodtl_refid, itemno, bomcategy, itemnoid,
                        qty, uom_qty, wt, uom_wt, status, unit_price, uom_cy, uom_unitprice,
                        buyer_reference, setused,
                        createby, createdate, lastupby, lastupdate, companyid
                    ) VALUES (
                        :ivcrawmatmain_refid, :ordpodtl_refid, :itemno, :bomcategy, :itemnoid,
                        :qty, :uom_qty, :wt, :uom_wt, :status, :unit_price, :uom_cy, :uom_unitprice,
                        :buyer_reference, 1,
                        :createby, GETDATE(), :lastupby, GETDATE(), :companyid            
                    )"
;
                
$sth $dbh->prepare($sql);
                
$q $sth->execute(  array(':ivcrawmatmain_refid' => $refid
                                        
':ordpodtl_refid' => $po_dtl['refid'], 
                                        
':itemnoid' => $item['refid'],                                 
                                        
':itemno' => $item['itemno'],
                                        
':bomcategy' => $po_dtl['bomcategy'],                                                        
                                        
':qty' => $qty,
                                        
':uom_qty' => $po_dtl['uom_qty'], 
                                        
':wt' => $po_dtl['wt'],                             
                                        
':uom_wt' => $po_dtl['uom_wt'],
                                        
':status' => 'NEW'
                                        
':unit_price' => $unit_price,                             
                                        
':uom_cy' => $po_dtl['uom_cy'],
                                        
':uom_unitprice' => $po_dtl['uom_unitprice'], 
                                        
':buyer_reference' => $po_main['pono'],                             
                                        
':createby' => $createby,
                                        
':lastupby' => $lastupby,                             
                                        
':companyid' => $companyid) );    
/*                echo $sth->getSQL(  array(':ivcrawmatmain_refid' => $refid, 
                                        ':ordpodtl_refid' => $po_dtl['refid'], 
                                        ':itemnoid' => $item['refid'],                                 
                                        ':itemno' => $item['itemno'],
                                        ':bomcategy' => $po_dtl['bomcategy'],                                                        
                                        ':qty' => $po_dtl['qty'],
                                        ':uom_qty' => $po_dtl['uom_qty'], 
                                        ':wt' => $po_dtl['wt'],                             
                                        ':uom_wt' => $po_dtl['uom_wt'],
                                        ':status' => 'NEW', 
                                        ':unit_price' => $unit_price,                             
                                        ':uom_cy' => $po_dtl['uom_cy'],
                                        ':uom_unitprice' => $po_dtl['uom_unitprice'], 
                                        ':buyer_reference' => $po_main['pono'],                             
                                        ':createby' => $createby,
                                        ':lastupby' => $lastupby,                             
                                        ':companyid' => $companyid) ).HTML_EOL;    */
                
pdo_showerror($sth$q);                
            }        
        }    
    }
    
    
//prevent delete received item
    
$sql "UPDATE ivc_rawmat_dtl
            SET setused = 1
            WHERE
                refid IN (
                    SELECT
                        dbo.ivc_rawmat_dtl.refid
                    FROM
                        dbo.ivc_rawmat_dtl
                    INNER JOIN dbo.ord_poset ON dbo.ord_poset.ivcrawmatdtl_refid = dbo.ivc_rawmat_dtl.refid
                    WHERE
                        dbo.ivc_rawmat_dtl.ivcrawmatmain_refid = :refid
                )"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(    ':refid' => $refid ) );
    
//echo $sth->getSQL(  array(    ':refid' => $refid ) );

    //delete unused
    
$sql "DELETE FROM ivc_rawmat_dtl
            WHERE ivcrawmatmain_refid = :refid AND setused = 0"
;
    
$sth $dbh->prepare($sql);
    
$q $sth->execute(  array(    ':refid' => $refid ) );
    
//echo $sth->getSQL(  array(    ':refid' => $refid ) );
    
    
updateIvcSummary($refid);
}

//exit;
$dbh->commit();    

//echo "msg=$msg";            
//exit;
    
form_dest($_REQUEST['godest'], $_REQUEST['formdest']);
    
header("Location: ivc_rawmat_modifyform.php?refid=$refid&msg=Saved.");
    print 
"Saved.";
    exit;
    
    
}
print 
INVALID.WS.REQUEST;
?>