Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Fifo / FillValuesWalk2.php
1 <?php
2
3
4 /**
5  *
6  * Idea is to test the walking through of a sequence
7  * to see if we can work out a method of ordering the calculations.
8  *
9  *
10  *
11  */
12  
13 require_once 'ProcessBase.php';
14  
15 class Pman_Xtuple_Fifo_FillValuesWalk2 extends  Pman_Xtuple_Fifo_ProcessBase
16
17 {
18   
19     static $cli_desc = "Fill in Fifo dollar values.";
20    
21     static $permitError = false;
22     
23     var $loc_only = false;//187; //false;
24     
25     
26     function getAuth()
27     {
28         $ff = HTML_FlexyFramework::get();
29         if (!$ff->cli) {
30             die("run form cli only");
31         }
32          
33     }
34     
35     
36     
37     function get($v)
38     {
39         // set up the failure code..
40         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
41         
42         // we have to have a pointer to work out where we left off..
43         // this process can go by invdetail_id ASC..
44         
45         
46       
47         
48         
49         if (!empty($v)) {
50             $this->runSingle($v);
51             die("DONE");
52         }
53         
54         die("single only");
55     }
56     
57     function runSingle($itemsite )
58     {
59         //DB_DataObject::debugLevel(1);
60          
61          
62          
63         $itemsite = (int)$itemsite;
64         $start = time();
65         
66         
67         $id = DB_DataObject::Factory('invdetail');
68          
69         
70         $id->query("SELECT
71        
72                   count(invdetail_id) as nres
73             FROM 
74                     
75                   invdetailview
76           WHERE
77               invhist_itemsite_id = $itemsite
78               AND
79               invfifo_void = 0
80               
81         ");
82         $id->fetch();
83         
84         $total = $id->nres;
85         
86         
87         
88         
89         //exit;
90         $id = DB_DataObject::Factory('invdetail');
91         $id->query("SELECT
92                         *
93                     FROM
94                       
95                 invdetailview
96             WHERE
97                 invhist_itemsite_id =  {$itemsite}
98                 AND
99                 invfifo_void = 0
100                 AND
101                 (
102                     invhist_transtype = 'RP'
103                     OR
104                     invhist_transtype = 'AD'
105                     OR
106                     invhist_transtype = 'RS'
107                 ) AND
108                     invdetail_qty >  0
109                     
110             ORDER BY
111          
112                 invhist_transdate ASC,
113                 invdetail_id ASC
114             
115         ");
116        
117         while ($id->fetch()) {
118             $all[] = clone($id);
119         }
120         
121         
122        
123         foreach($all as $id) {
124             $idr = DB_DataObject::Factory('invdetail');
125             $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)");
126             
127             
128             // fill in all outgoing based on these reciepts..
129             
130             $this->regBuy( $id->invdetail_id, $id->invdetail_location_id, $id->invfifo_qty_before, $id->invfifo_qty_after, 'S');
131            
132              
133         }
134           while (true) {
135             $before = count($this->done);
136             foreach($this->locs as $loc=>$ar) {
137                 
138                $this->processOut($loc, $itemsite);
139                 
140             }
141             if (count($this->done) == $before) {
142                 break;
143             }
144         // process remaining locs...
145         } 
146         echo "DONE : ". count($this->done) . " out of $total \n";
147         //print_R($done);
148     
149         exit;
150           
151         
152    
153     }
154     
155     function processOut($loc, $itemsite)
156     {
157         
158         if (!isset($this->locs[$loc]) || $this->locs[$loc][0] > 0)  {
159             return; // no record or can not start..
160         }
161         
162          
163         $end = $this->locs[$loc][1];
164       
165         $id = DB_DataObject::Factory('invdetail');
166         
167         $q = array();
168         for($i = 0; $i < count($this->locs[$loc]); $i+=2) {
169             
170             $q[] = "
171                 
172                 (invfifo_qty_before >= {$this->locs[$loc][$i]} AND invfifo_qty_after <= {$this->locs[$loc][$i+1]})
173             ";
174         }
175         
176         
177         $id->query("SELECT
178        
179               *
180                 FROM
181                     
182               invdetailview
183           WHERE
184               invhist_itemsite_id =  {$itemsite}
185               AND
186               invfifo_void = 0
187               AND
188               invdetail_qty < 0
189               AND
190               (". implode(' OR ', $q) . ") 
191              AND
192              invdetail_location_id = $loc
193              AND
194              invdetail_id NOT IN (". implode(',', $this->done) . ") 
195           ORDER BY
196        
197               invhist_transdate ASC,
198               invdetail_id ASC
199           
200         ");
201         $all = array();
202         while($id->fetch()) {
203             $all[] = clone($id);
204             
205             $ret[] = $id->invdetail_id;
206             
207         }
208        // $done = array();
209         foreach($all as $id) {
210             //var_dump($id->invdetail_id);
211             if (in_array($id->invdetail_id, $this->done)) {
212                 continue;
213             }
214             
215             
216             $idr = DB_DataObject::Factory('invdetail');
217             $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)");
218            
219              
220             // if it's a transfer, then flag the reverse as done as well..
221             if ($id->invhist_transtype == 'RL') {
222                 $idr = DB_DataObject::Factory('invdetail');
223                 $idr->query("SELECT *
224                                 FROM invdetailview WHERE invdetail_invhist_id = {$id->invhist_id} AND invdetail_id != {$id->invdetail_id}
225                             ");
226                 $idr->fetch();
227                 $this->regBuy(  $idr->invdetail_id,   $idr->invdetail_location_id, $idr->invfifo_qty_before, $idr->invfifo_qty_after, 'R');
228                 
229                 
230                 $invdetail_id =  $idr->invdetail_id;
231                 $out_loc = $idr->invdetail_location_id;
232                 
233                 $idr = DB_DataObject::Factory('invdetail');
234                 $idr->query("SELECT invfifo_update_from_invdetail({$invdetail_id},true)");
235                 
236                
237                 
238                
239                 
240             }
241             
242             
243         }
244          
245         
246         
247         
248     }
249     var $done = array( 0 );
250     var $locs = array();
251     function regBuy($invdetail_id, $loc, $start, $end, $type) {
252         
253         if (in_array($invdetail_id, $this->done)) {
254             return;
255         }
256         $this->done[] = $invdetail_id;
257         
258         echo "$loc S:$start, $end  : $type\n";
259         if (!isset($this->locs[$loc])) {
260             $this->locs[$loc] = array($start, $end);
261             return;
262         }
263         
264         // replace the last line..
265         if (count($this->locs[$loc]) == 2 && $this->locs[$loc][1] == $start) {
266             $this->locs[$loc][1] = $end;
267             print_r($this->locs);
268             return;
269         }
270         // got an array, work out how to maniupulate it..
271         $added = false;
272         for ($i = 0; $i < count($this->locs[$loc]); $i +=2) {
273             $ss = $this->locs[$loc][$i];
274             $ee = $this->locs[$loc][$i+1];
275             echo "CHECK $ee == $start?";
276             
277             if ($ee == $start) {
278                 echo "fixed?";
279                 $this->locs[$loc][$i+1] = $end;
280                 $added = true;
281                 break;
282             }
283             
284         }
285         if (!$added) {
286             $this->locs[$loc][] = $start;
287             $this->locs[$loc][] = $end;
288         }
289         
290         sort($this->locs[$loc]);
291         $nl = array();
292         
293         for ($i = 0; $i < count($this->locs[$loc]); $i+=2) {
294             $ss = $this->locs[$loc][$i];
295             $ee = $this->locs[$loc][$i+1];
296             if (empty($nl)) {
297                 $nl[] = $ss;
298                 $nl[] = $ee;
299                 continue;
300             }
301             if ($ss == $nl[count($nl)-1]) {
302                 $nl[count($nl)-1] = $ee;
303                 
304                 continue;
305             }
306             $nl[] = $ss;
307             $nl[] = $ee;
308             
309         }
310         // normalize the list..
311         $this->locs[$loc] = $nl;
312         
313         print_r($this->locs);
314         
315         
316         
317     }
318     
319     
320     
321     
322 }
323
324
325
326