cli) { die("run form cli only"); } } function get($v) { // set up the failure code.. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); // we have to have a pointer to work out where we left off.. // this process can go by invdetail_id ASC.. if (!empty($v)) { $this->runSingle($v); die("DONE"); } die("single only"); } function runSingle($itemsite ) { //DB_DataObject::debugLevel(1); $itemsite = (int)$itemsite; $start = time(); $id = DB_DataObject::Factory('invdetail'); $id->query("SELECT count(invdetail_id) as nres FROM invdetailview WHERE invhist_itemsite_id = $itemsite AND invfifo_void = 0 "); $id->fetch(); $total = $id->nres; //exit; $id = DB_DataObject::Factory('invdetail'); $id->query("SELECT * FROM invdetailview WHERE invhist_itemsite_id = {$itemsite} AND invfifo_void = 0 AND ( invhist_transtype = 'RP' OR invhist_transtype = 'AD' OR invhist_transtype = 'RS' ) AND invdetail_qty > 0 ORDER BY invhist_transdate ASC, invdetail_id ASC "); while ($id->fetch()) { $all[] = clone($id); } foreach($all as $id) { $idr = DB_DataObject::Factory('invdetail'); $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)"); // fill in all outgoing based on these reciepts.. $this->regBuy( $id->invdetail_id, $id->invdetail_location_id, $id->invfifo_qty_before, $id->invfifo_qty_after, 'S'); } while (true) { $before = count($this->done); foreach($this->locs as $loc=>$ar) { $this->processOut($loc, $itemsite); } if (count($this->done) == $before) { break; } // process remaining locs... } echo "DONE : ". count($this->done) . " out of $total \n"; //print_R($done); exit; } function processOut($loc, $itemsite) { if (!isset($this->locs[$loc]) || $this->locs[$loc][0] > 0) { return; // no record or can not start.. } $end = $this->locs[$loc][1]; $id = DB_DataObject::Factory('invdetail'); $q = array(); for($i = 0; $i < count($this->locs[$loc]); $i+=2) { $q[] = " (invfifo_qty_before >= {$this->locs[$loc][$i]} AND invfifo_qty_after <= {$this->locs[$loc][$i+1]}) "; } $id->query("SELECT * FROM invdetailview WHERE invhist_itemsite_id = {$itemsite} AND invfifo_void = 0 AND invdetail_qty < 0 AND (". implode(' OR ', $q) . ") AND invdetail_location_id = $loc AND invdetail_id NOT IN (". implode(',', $this->done) . ") ORDER BY invhist_transdate ASC, invdetail_id ASC "); $all = array(); while($id->fetch()) { $all[] = clone($id); $ret[] = $id->invdetail_id; } // $done = array(); foreach($all as $id) { //var_dump($id->invdetail_id); if (in_array($id->invdetail_id, $this->done)) { continue; } $idr = DB_DataObject::Factory('invdetail'); $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)"); // if it's a transfer, then flag the reverse as done as well.. if ($id->invhist_transtype == 'RL') { $idr = DB_DataObject::Factory('invdetail'); $idr->query("SELECT * FROM invdetailview WHERE invdetail_invhist_id = {$id->invhist_id} AND invdetail_id != {$id->invdetail_id} "); $idr->fetch(); $this->regBuy( $idr->invdetail_id, $idr->invdetail_location_id, $idr->invfifo_qty_before, $idr->invfifo_qty_after, 'R'); $invdetail_id = $idr->invdetail_id; $out_loc = $idr->invdetail_location_id; $idr = DB_DataObject::Factory('invdetail'); $idr->query("SELECT invfifo_update_from_invdetail({$invdetail_id},true)"); } } } var $done = array( 0 ); var $locs = array(); function regBuy($invdetail_id, $loc, $start, $end, $type) { if (in_array($invdetail_id, $this->done)) { return; } $this->done[] = $invdetail_id; echo "$loc S:$start, $end : $type\n"; if (!isset($this->locs[$loc])) { $this->locs[$loc] = array($start, $end); return; } // replace the last line.. if (count($this->locs[$loc]) == 2 && $this->locs[$loc][1] == $start) { $this->locs[$loc][1] = $end; print_r($this->locs); return; } // got an array, work out how to maniupulate it.. $added = false; for ($i = 0; $i < count($this->locs[$loc]); $i +=2) { $ss = $this->locs[$loc][$i]; $ee = $this->locs[$loc][$i+1]; echo "CHECK $ee == $start?"; if ($ee == $start) { echo "fixed?"; $this->locs[$loc][$i+1] = $end; $added = true; break; } } if (!$added) { $this->locs[$loc][] = $start; $this->locs[$loc][] = $end; } sort($this->locs[$loc]); $nl = array(); for ($i = 0; $i < count($this->locs[$loc]); $i+=2) { $ss = $this->locs[$loc][$i]; $ee = $this->locs[$loc][$i+1]; if (empty($nl)) { $nl[] = $ss; $nl[] = $ee; continue; } if ($ss == $nl[count($nl)-1]) { $nl[count($nl)-1] = $ee; continue; } $nl[] = $ss; $nl[] = $ee; } // normalize the list.. $this->locs[$loc] = $nl; print_r($this->locs); } }