3 * Table Definition for recv
5 require_once 'DB/DataObject.php';
7 class Pman_Xtuple_DataObjects_Recv extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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)
45 * Getter / Setter for $recv_freight_curr_id
47 * @param mixed (optional) value to assign
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');
55 * Getter / Setter for $recv_itemsite_id
57 * @param mixed (optional) value to assign
60 public function itemsite() {
61 return func_num_args() ? $this->link('recv_itemsite_id', func_get_arg(0)) : $this->link('recv_itemsite_id');
65 * Getter / Setter for $recv_purchcost_curr_id
67 * @param mixed (optional) value to assign
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');
75 * Getter / Setter for $recv_recvcost_curr_id
77 * @param mixed (optional) value to assign
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');
85 * Getter / Setter for $recv_splitfrom_id
87 * @param mixed (optional) value to assign
90 public function splitfrom() {
91 return func_num_args() ? $this->link('recv_splitfrom_id', func_get_arg(0)) : $this->link('recv_splitfrom_id');
95 * Getter / Setter for $recv_vend_id
97 * @param mixed (optional) value to assign
100 public function vend() {
101 return func_num_args() ? $this->link('recv_vend_id', func_get_arg(0)) : $this->link('recv_vend_id');
105 * Getter / Setter for $recv_vohead_id
107 * @param mixed (optional) value to assign
110 public function vohead() {
111 return func_num_args() ? $this->link('recv_vohead_id', func_get_arg(0)) : $this->link('recv_vohead_id');
115 * Getter / Setter for $recv_voitem_id
117 * @param mixed (optional) value to assign
120 public function voitem() {
121 return func_num_args() ? $this->link('recv_voitem_id', func_get_arg(0)) : $this->link('recv_voitem_id');
124 * Getter / Setter for $recv_recvgrp_id
126 * @param mixed (optional) value to assign
129 public function recvgrp() {
130 return func_num_args() ? $this->link('recv_vrecvgrp_id', func_get_arg(0)) : $this->link('recv_recvgrp_id');
133 /* the code above is auto generated do not remove the tag below */
137 function applyFilters($q, $au, $roo)
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']);
150 function joinAdditem()
153 LEFT JOIN item AS join_recv_item
154 ON (join_recv_item.item_id =join_recv_itemsite_id_itemsite_id.itemsite_item_id)
156 $t = DB_DataObject::Factory('item');
157 $this->selectAs($t, '%s', 'join_recv_item');
160 function joinAddPoitem()
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)
166 $t = DB_DataObject::Factory('poitem');
167 $this->selectAs($t, 'recv_orderitem_id_%s', 'join_recv_orderitem_id_poitem_id');
174 function orderitem() {
175 $poitem = DB_DataObject::factory('poitem');
176 $poitem->get($this->recv_orderitem_id);
180 function createFromShipItem($roo, $shipitem, $poitem)
182 $po = $poitem->pohead();
186 $shiphead = $shipitem->shiphead();
188 $coitem = $shipitem->orderitem();
189 $loc = $coitem->coitem_location_src;
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;
201 // where are we going to locate it from..
203 //DB_DataObject::debugLevel(1);
207 // see if it's already been entered...
208 $d = DB_DataObject::factory('recv');
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()
217 // has it already been created???
222 $d->updateQuantity($roo, $shipitem->shipitem_qty);
229 return $this->enterReceipt($roo,
232 $shipitem->shipitem_qty,
233 'auto recv for shipitem ' . $shipitem->pid(),
235 $poitem->poitem_duedate,
244 function enterReceipt($roo, $rg, $poitem_id, $qty, $notes, $curr_id, $date, $loc )
246 $notes = $this->escape($notes);
247 //DB_DataObject::DebugLevel(1);
248 $d = DB_DataObject::factory('recv');
249 $d->query("SELECT enterReceipt('PO',
252 '$notes', {$curr_id},
257 $recvid = empty($d->result) ? 0 : $d->result;
258 if (empty($recvid) || $recvid < 1) {
259 $roo->jerr("enterReceipt returned {$recvid}");
261 // ensure we can identify it later..
262 $d = DB_DataObject::factory('recv');
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" );
271 $d->recv_recvgrp_id = $rg->pid();
276 $d = DB_DataObject::factory('recv');
277 $d->query("SELECT postReceipt(recv_id, 0) AS result FROM recv WHERE (recv_id={$recvid}) ");
280 if (empty( $d->result) || $d->result < 1) {
281 $roo->jerr("enterReceipt returned { $d->result}");
284 $itemlocdist_series = $d->result;
286 $d = DB_DataObject::factory('recv');
287 $d->query("SELECT itemlocdist_id,
288 itemlocdist_reqlotserial,
289 itemlocdist_distlotserial,
292 itemsite_controlmethod,
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
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
309 $itemlocdist_id = $d->itemlocdist_id;
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
319 itemlocdist_ls_id, endOfTime()
320 FROM itemlocdist WHERE
321 (itemlocdist_id={$itemlocdist_id }) ");
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");
328 if (empty($d->result) || $d->result < 0) {
329 $roo->jerr("distribute to locations failed:" . (int)@$d->result);
331 $d = DB_DataObject::factory('recv');
332 $d->query("SELECT postItemlocseries({$itemlocdist_series}) AS result");
334 if (empty($d->result) || $d->result < 0) {
335 $roo->jerr("post to locations failed:" . (int)@$d->result);
348 function revertFromShipItem($roo, $po, $shipitem )
351 $coitem = $shipitem->orderitem();
352 //$roo->jerr(print_R($coitem,true));
353 $shiphead = $shipitem->shiphead();
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}");
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,
371 // has it already been created???
373 if (!$recv->count()) {
376 $roo->jerr("could not find shipitem as recieved.");
382 $recv->updateQuantity($roo, 0);
388 function invdetail($roo)
391 $poitem = $this->orderitem();
393 $po = $poitem->pohead();
394 $number = $po->pohead_number;
396 $rg = DB_DataObject::Factory('recvgrp');
397 if ($this->recv_recvgrp_id && $rg->get($this->recv_recvgrp_id)) {
398 $number = $rg->recvgrp_number;
400 // $roo->jerr("Could not find original ItemReciept group");
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,
412 $invhist->orderBy('invhist_id DESC'); // last entry..
414 if (!$invhist->find(true)) {
415 $roo->jerr("Could not find invhist previous transaction {$rg->recvgrp_number} / {$poitem->poitem_itemsite_id}");
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');
422 if (!$ivd->count()) {
423 $roo->jerr("Could not find location of previous item reciept");
434 function updateQuantity($roo, $newqty)
436 if ($newqty == $this->recv_qty) {
437 // no change in quantity
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();
448 // finding invhist data for this...
449 // which should provide the location.
450 $ivd = $this->invdetail($roo);
456 // $roo->jerr(print_R($details,true));
458 $loc = $ivd->invdetail_location_id;
460 $change = $newqty - $this->recv_qty;
462 $cor = DB_DataObject::factory('recv');
464 $cor->query(" SELECT correctReceipt(
465 {$this->pid()} , {$newqty}, 0, 0, $pohead->pohead_curr_id, '{$this->recv_duedate}') AS result
468 if (empty($cor->result) || $cor->result < 1) {
469 $roo->jerr("correctReciept returned {$cor->result}");
472 $itemlocdist_series = $cor->result;
474 $d = DB_DataObject::factory('recv');
475 $d->query("SELECT itemlocdist_id,
476 itemlocdist_reqlotserial,
477 itemlocdist_distlotserial,
480 itemsite_controlmethod,
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
490 if (empty( $d->itemlocdist_id) || $d->itemlocdist_id < 1) {
491 $roo->jerr("SEARCHING FOR itemlocdist_id returned { $d->itemlocdist_id}");
495 $itemlocdist_id = $d->itemlocdist_id;
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
507 itemlocdist_ls_id, endOfTime()
508 FROM itemlocdist WHERE
509 (itemlocdist_id={$itemlocdist_id }) ");
511 // this generates an additional itemlocdist record..
512 // they throw exceptions if things fail.
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");