Fix #7123 - getting abra ready to test
[Pman.Xtuple] / DataObjects / Recv.php
1 <?php
2 /**
3  * Table Definition for recv
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Xtuple_DataObjects_Recv extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'recv';                // table name
13     public $recv_id;                         // int4(4)  not_null default_nextval%28recv_recv_id_seq%29 primary_key
14     public $recv_order_type;                 // text(-1)  not_null multiple_key
15     public $recv_order_number;               // text(-1)  not_null
16     public $recv_orderitem_id;               // int4(4)  not_null multiple_key
17     public $recv_agent_username;             // text(-1)  
18     public $recv_itemsite_id;                // int4(4)  
19     public $recv_vend_id;                    // int4(4)  
20     public $recv_vend_item_number;           // text(-1)  
21     public $recv_vend_item_descrip;          // text(-1)  
22     public $recv_vend_uom;                   // text(-1)  
23     public $recv_purchcost;                  // numeric(-1)  
24     public $recv_purchcost_curr_id;          // int4(4)  
25     public $recv_duedate;                    // date(4)  
26     public $recv_qty;                        // numeric(-1)  
27     public $recv_recvcost;                   // numeric(-1)  
28     public $recv_recvcost_curr_id;           // int4(4)  
29     public $recv_freight;                    // numeric(-1)  
30     public $recv_freight_curr_id;            // int4(4)  
31     public $recv_date;                       // timestamptz(8)  
32     public $recv_value;                      // numeric(-1)  
33     public $recv_posted;                     // bool(1)  not_null default_false
34     public $recv_invoiced;                   // bool(1)  not_null default_false
35     public $recv_vohead_id;                  // int4(4)  
36     public $recv_voitem_id;                  // int4(4)  
37     public $recv_trans_usr_name;             // text(-1)  not_null default_%22current_user%22%28%29
38     public $recv_notes;                      // text(-1)  
39     public $recv_gldistdate;                 // date(4)  
40     public $recv_splitfrom_id;               // int4(4)  
41     public $recv_rlsd_duedate;               // date(4)  
42     public $recv_recvgrp_id;               // date(4)  
43     
44    /**
45     * Getter / Setter for $recv_freight_curr_id
46     *
47     * @param    mixed   (optional) value to assign
48     * @access   public
49     */
50     public function freight_curr() {
51         return func_num_args() ? $this->link('recv_freight_curr_id', func_get_arg(0)) : $this->link('recv_freight_curr_id');
52     }
53
54    /**
55     * Getter / Setter for $recv_itemsite_id
56     *
57     * @param    mixed   (optional) value to assign
58     * @access   public
59     */
60     public function itemsite() {
61         return func_num_args() ? $this->link('recv_itemsite_id', func_get_arg(0)) : $this->link('recv_itemsite_id');
62     }
63
64    /**
65     * Getter / Setter for $recv_purchcost_curr_id
66     *
67     * @param    mixed   (optional) value to assign
68     * @access   public
69     */
70     public function purchcost_curr() {
71         return func_num_args() ? $this->link('recv_purchcost_curr_id', func_get_arg(0)) : $this->link('recv_purchcost_curr_id');
72     }
73
74    /**
75     * Getter / Setter for $recv_recvcost_curr_id
76     *
77     * @param    mixed   (optional) value to assign
78     * @access   public
79     */
80     public function recvcost_curr() {
81         return func_num_args() ? $this->link('recv_recvcost_curr_id', func_get_arg(0)) : $this->link('recv_recvcost_curr_id');
82     }
83
84    /**
85     * Getter / Setter for $recv_splitfrom_id
86     *
87     * @param    mixed   (optional) value to assign
88     * @access   public
89     */
90     public function splitfrom() {
91         return func_num_args() ? $this->link('recv_splitfrom_id', func_get_arg(0)) : $this->link('recv_splitfrom_id');
92     }
93
94    /**
95     * Getter / Setter for $recv_vend_id
96     *
97     * @param    mixed   (optional) value to assign
98     * @access   public
99     */
100     public function vend() {
101         return func_num_args() ? $this->link('recv_vend_id', func_get_arg(0)) : $this->link('recv_vend_id');
102     }
103
104    /**
105     * Getter / Setter for $recv_vohead_id
106     *
107     * @param    mixed   (optional) value to assign
108     * @access   public
109     */
110     public function vohead() {
111         return func_num_args() ? $this->link('recv_vohead_id', func_get_arg(0)) : $this->link('recv_vohead_id');
112     }
113
114    /**
115     * Getter / Setter for $recv_voitem_id
116     *
117     * @param    mixed   (optional) value to assign
118     * @access   public
119     */
120     public function voitem() {
121         return func_num_args() ? $this->link('recv_voitem_id', func_get_arg(0)) : $this->link('recv_voitem_id');
122     }
123     /**
124     * Getter / Setter for $recv_recvgrp_id
125     *
126     * @param    mixed   (optional) value to assign
127     * @access   public
128     */
129     public function recvgrp() {
130         return func_num_args() ? $this->link('recv_vrecvgrp_id', func_get_arg(0)) : $this->link('recv_recvgrp_id');
131     }
132
133     /* the code above is auto generated do not remove the tag below */
134     ###END_AUTOCODE
135     
136     
137     function applyFilters($q, $au, $roo)
138     {
139         
140         if (isset($q['recv_orderitem_id_poitem_pohead_id'])) {
141             $this->joinAddPoitem();
142             $this->joinAdditem();
143             $this->whereAdd('join_recv_orderitem_id_poitem_id.poitem_pohead_id = ' .
144                             (int)$q['recv_orderitem_id_poitem_pohead_id']);
145             
146         }
147         
148         
149     }
150     function joinAdditem()
151     {
152         $this->_join .= "
153             LEFT JOIN item AS join_recv_item
154                     ON (join_recv_item.item_id =join_recv_itemsite_id_itemsite_id.itemsite_item_id)
155         ";
156         $t = DB_DataObject::Factory('item');
157         $this->selectAs($t, '%s', 'join_recv_item');
158         
159     }
160     function joinAddPoitem()
161     {
162         $this->_join .= "
163             LEFT JOIN poitem AS join_recv_orderitem_id_poitem_id
164                     ON (join_recv_orderitem_id_poitem_id.poitem_id=recv.recv_orderitem_id)
165         ";
166         $t = DB_DataObject::Factory('poitem');
167         $this->selectAs($t, 'recv_orderitem_id_%s', 'join_recv_orderitem_id_poitem_id');
168        // $this->
169         
170         
171         
172     }
173     
174     function orderitem() {
175         $poitem = DB_DataObject::factory('poitem');
176         $poitem->get($this->recv_orderitem_id);
177         return $poitem;
178     }
179     
180     function createFromShipItem($roo,   $shipitem, $poitem)
181     {
182         $po = $poitem->pohead();
183         
184         $poid = $po->pid();
185         
186         $shiphead = $shipitem->shiphead();
187         
188         $coitem = $shipitem->orderitem();
189         $loc = $coitem->coitem_location_src;
190         
191         // see if we have a recvgrp for it..
192         $rg = DB_DataObject::Factory('recvgrp');
193         if (!$rg->get('recvgrp_number', 'XF-' . $shiphead->shiphead_number)) {
194             $rg->recvgrp_number =  'XF-' .$shiphead->shiphead_number;
195             $rg->insert();
196         }
197         
198         
199         
200         
201         // where are we going to locate it from..
202         
203         //DB_DataObject::debugLevel(1);
204         
205       
206         
207         // see if it's already been entered...
208         $d = DB_DataObject::factory('recv');
209         $d->setFrom(array(
210             'recv_order_type' => 'PO',
211             'recv_orderitem_id' =>  $poitem->pid(),
212            // 'recv_qty' =>           $shipitem->shipitem_qty,
213             'recv_purchcost_curr_id' => $po->pohead_curr_id,
214             'recv_recvgrp_id' =>  $rg->pid()
215         ));
216         
217         // has it already been created???
218        
219         if ($d->count()) {
220             $d->find(true);
221             
222             $d->updateQuantity($roo, $shipitem->shipitem_qty);
223             
224             return $d->recv_id;
225         }
226          
227           
228           
229         return  $this->enterReceipt($roo,
230                         $rg,
231                         $poitem->pid(),
232                         $shipitem->shipitem_qty,
233                         'auto recv for shipitem ' . $shipitem->pid(),
234                         $po->pohead_curr_id,
235                         $poitem->poitem_duedate,
236                         $loc);
237           
238          
239     }
240     
241     
242     
243     
244     function enterReceipt($roo, $rg, $poitem_id, $qty, $notes, $curr_id, $date, $loc )
245     {
246         $notes = $this->escape($notes);
247         //DB_DataObject::DebugLevel(1);
248         $d = DB_DataObject::factory('recv');
249         $d->query("SELECT enterReceipt('PO',
250                         {$poitem_id},
251                         {$qty}, 0,
252                         '$notes', {$curr_id},
253                         '{$date}'
254                     ) AS result");
255         $d->fetch();
256         
257         $recvid = empty($d->result) ? 0 : $d->result;
258         if (empty($recvid) || $recvid < 1) {
259             $roo->jerr("enterReceipt returned {$recvid}");
260         }
261         // ensure we can identify it later..
262         $d = DB_DataObject::factory('recv');
263         $d->get($recvid);
264         $dd = clone($d);
265         
266         if (!$dd->itemsite()->itemsite_loccntrl) {
267             $roo->jerr("Item ".$dd->itemsite()->item()->item_number .
268                        " Is not flagged for Multiple Location Control - under Item .. Inventory .. Location" );
269         }
270         
271         $d->recv_recvgrp_id =  $rg->pid();
272         $d->update($dd);
273         
274          
275         
276         $d = DB_DataObject::factory('recv');
277         $d->query("SELECT postReceipt(recv_id, 0) AS result FROM recv WHERE (recv_id={$recvid}) ");
278         $d->fetch();
279         
280         if (empty( $d->result) ||  $d->result < 1) {
281             $roo->jerr("enterReceipt returned { $d->result}");
282         }
283         
284         $itemlocdist_series = $d->result;
285         
286         $d = DB_DataObject::factory('recv');
287         $d->query("SELECT itemlocdist_id,
288                   itemlocdist_reqlotserial,
289                   itemlocdist_distlotserial,
290                   itemlocdist_qty,
291                   itemsite_loccntrl,
292                   itemsite_controlmethod,
293                 itemsite_perishable,
294                 itemsite_warrpurc,
295                 COALESCE(itemsite_lsseq_id,-1) AS itemsite_lsseq_id,
296                 COALESCE(itemlocdist_source_id,-1) AS itemlocdist_source_id
297                 FROM itemlocdist, itemsite
298                 WHERE ( (itemlocdist_itemsite_id=itemsite_id) AND (itemlocdist_series={$itemlocdist_series }) ) ORDER BY itemlocdist_id
299                 ");
300         $d->fetch();
301         
302         if (empty( $d->itemlocdist_id) ||  $d->itemlocdist_id < 1) {
303             $roo->jerr("SEARCHING FOR itemlocdist_id  returned " . ( empty($d->itemlocdist_id) ? 'NOTHING' : $d->itemlocdist_id) . " FOR ".
304                       $dd->itemsite()->item()->item_number 
305                        );
306         }
307         
308         
309         $itemlocdist_id = $d->itemlocdist_id;
310            
311         $d = DB_DataObject::factory('recv');
312         $d->query("INSERT INTO itemlocdist (
313                 itemlocdist_itemlocdist_id,  itemlocdist_source_type,
314                 itemlocdist_source_id,  itemlocdist_qty,
315                 itemlocdist_ls_id, itemlocdist_expiration
316             ) SELECT
317                 itemlocdist_id,'L',
318                 {$loc},       {$qty},
319                 itemlocdist_ls_id, endOfTime()
320                 FROM itemlocdist WHERE
321                 (itemlocdist_id={$itemlocdist_id }) ");
322         
323         // this generates an additional itemlocdist record..
324         // they throw exceptions if things fail.
325         $d = DB_DataObject::factory('recv');
326         $d->query("SELECT distributeToLocations({$itemlocdist_id}) AS result");
327         $d->fetch();
328         if (empty($d->result) || $d->result < 0) {
329             $roo->jerr("distribute to locations failed:" . (int)@$d->result);
330         }
331         $d = DB_DataObject::factory('recv');
332         $d->query("SELECT postItemlocseries({$itemlocdist_series}) AS result");
333         $d->fetch();
334         if (empty($d->result) || $d->result < 0) {
335             $roo->jerr("post to locations failed:" . (int)@$d->result);
336         }
337      
338         return $recvid;
339     }
340     
341     
342     
343     
344     
345     
346     
347     
348     function revertFromShipItem($roo, $po, $shipitem )
349     {
350         
351         $coitem = $shipitem->orderitem();
352         //$roo->jerr(print_R($coitem,true));
353         $shiphead = $shipitem->shiphead();
354         
355         $rg = DB_DataObject::Factory('recvgrp');
356         if (!$rg->get('recvgrp_number', 'XF-' . $shiphead->shiphead_number)) {
357             $roo->jerr("Could not find original ItemReciept group {$shiphead->shiphead_number}");
358         }
359         
360         $recv = DB_DataObject::factory('recv');
361         $note = 'auto recv for shipitem ' . $shipitem->pid();
362         $recv->setFrom(array(
363             'recv_order_type' => 'PO',
364         //    'recv_orderitem_id' =>  $poitem->pid(),
365            // 'recv_qty' =>           $shipitem->shipitem_qty,
366             'recv_purchcost_curr_id' => $po->pohead_curr_id,
367             'recv_notes' =>  $note,
368             'recv_recvgrp_id' => $rg->recvgrp_id, 
369            // 'recv_duedate' =>       $poitem->poitem_duedate,
370         ));
371         // has it already been created???
372        
373         if (!$recv->count()) {
374             
375             
376             $roo->jerr("could not find shipitem as recieved.");
377
378             
379         }
380         $recv->find(true);
381         
382         $recv->updateQuantity($roo, 0);
383         // got it.. 
384   
385     }
386     
387     
388     function invdetail($roo)
389     {
390         
391         $poitem = $this->orderitem();
392         
393         $po = $poitem->pohead();
394         $number  = $po->pohead_number;
395         
396         $rg = DB_DataObject::Factory('recvgrp');
397         if ($this->recv_recvgrp_id &&  $rg->get($this->recv_recvgrp_id)) {
398             $number =  $rg->recvgrp_number;
399             
400         //    $roo->jerr("Could not find original ItemReciept group");
401         }
402         
403         
404         
405         $invhist = DB_DAtaObject::Factory('invhist');
406         $invhist->setFrom(array( 
407             'invhist_ordnumber' => $number . '-' . $poitem->poitem_linenumber,
408             'invhist_ordtype' => 'PO',
409             'invhist_itemsite_id' => $poitem->poitem_itemsite_id,
410         ));
411         
412         $invhist->orderBy('invhist_id DESC'); // last entry..
413         $invhist->limit(1);
414         if (!$invhist->find(true)) {
415             $roo->jerr("Could not find invhist previous transaction {$rg->recvgrp_number} / {$poitem->poitem_itemsite_id}");
416         }
417         
418         $ivd = DB_DataObject::Factory('invdetail');
419         $ivd->invdetail_invhist_id = $invhist->pid();
420         $ivd->whereAdd('invdetail_location_id IS NOT NULL AND invdetail_location_id > 0');
421         $ivd->limit(1);
422         if (!$ivd->count()) {
423             $roo->jerr("Could not find location of previous item reciept");
424         }
425         $ivd->find(true);
426         return $ivd;
427         
428         
429         
430     }
431     
432     
433     
434     function updateQuantity($roo, $newqty)
435     {
436         if ($newqty == $this->recv_qty) {
437             // no change in quantity
438             return true;
439         }
440         
441         //correctReceipt(previous_recv_id, pQty, pFreight, _itemlocseries, curr_id, pEffective
442         // this actually modfies the recv...
443         // calls postinvtrans
444         $poitem = $this->orderitem();
445         $pohead = $poitem->pohead();
446         
447         
448         // finding invhist data for this...
449         // which should provide the location.
450         $ivd = $this->invdetail($roo);
451         
452        
453          
454          
455         
456         // $roo->jerr(print_R($details,true));
457         
458         $loc = $ivd->invdetail_location_id;
459         
460         $change = $newqty - $this->recv_qty;
461         
462         $cor =  DB_DataObject::factory('recv');
463         
464         $cor->query("  SELECT correctReceipt(
465                     {$this->pid()} , {$newqty}, 0, 0, $pohead->pohead_curr_id,  '{$this->recv_duedate}') AS result
466                 ");
467         $cor->fetch();
468         if (empty($cor->result) || $cor->result < 1)  {
469             $roo->jerr("correctReciept returned {$cor->result}");
470         }
471         
472         $itemlocdist_series = $cor->result;
473          
474         $d = DB_DataObject::factory('recv');
475         $d->query("SELECT itemlocdist_id,
476                   itemlocdist_reqlotserial,
477                   itemlocdist_distlotserial,
478                   itemlocdist_qty,
479                   itemsite_loccntrl,
480                   itemsite_controlmethod,
481                   itemsite_perishable,
482                    itemsite_warrpurc,
483                 COALESCE(itemsite_lsseq_id,-1) AS itemsite_lsseq_id,
484                 COALESCE(itemlocdist_source_id,-1) AS itemlocdist_source_id
485                 FROM itemlocdist, itemsite
486                 WHERE ( (itemlocdist_itemsite_id=itemsite_id) AND (itemlocdist_series={$itemlocdist_series }) ) ORDER BY itemlocdist_id
487                 ");
488         $d->fetch();
489         
490         if (empty( $d->itemlocdist_id) ||  $d->itemlocdist_id < 1) {
491             $roo->jerr("SEARCHING FOR itemlocdist_id  returned { $d->itemlocdist_id}");
492         }
493         
494         
495         $itemlocdist_id = $d->itemlocdist_id;
496  
497          
498  
499         $d = DB_DataObject::factory('recv');
500         $d->query("INSERT INTO itemlocdist (
501                 itemlocdist_itemlocdist_id,  itemlocdist_source_type,
502                 itemlocdist_source_id,  itemlocdist_qty,
503                 itemlocdist_ls_id, itemlocdist_expiration
504             ) SELECT
505                 itemlocdist_id,'L',
506                 {$loc},       {$change},
507                 itemlocdist_ls_id, endOfTime()
508                 FROM itemlocdist WHERE
509                 (itemlocdist_id={$itemlocdist_id }) ");
510         
511         // this generates an additional itemlocdist record..
512         // they throw exceptions if things fail.
513         
514         $d = DB_DataObject::factory('recv');
515         $d->query("SELECT distributeToLocations({$itemlocdist_id}) AS result");
516         $d = DB_DataObject::factory('recv');
517         $d->query("SELECT postItemlocseries({$itemlocdist_series}) AS result");
518         
519         
520     }
521     
522     
523 }