Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Fifo / ProcessValuesWalk.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_ProcessValuesWalk 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         $itemsite = (int)$itemsite;
62         $start = time();
63         
64         
65           $id = DB_DataObject::Factory('invdetail');
66         
67         
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         //exit;
87         $id = DB_DataObject::Factory('invdetail');
88         $id->query("SELECT
89                         *
90                     FROM
91                       
92                 invdetailview
93             WHERE
94                 invhist_itemsite_id =  {$itemsite}
95                 AND
96                 invfifo_void = 0
97                 AND
98                 (
99                     invhist_transtype = 'RP'
100                     OR
101                     invhist_transtype = 'AD'
102                     OR
103                     invhist_transtype = 'RS'
104                 ) AND
105                     invdetail_qty > 0
106                     
107             ORDER BY
108          
109                 invhist_transdate ASC,
110                 invdetail_id ASC
111             
112         ");
113        
114         while ($id->fetch()) {
115             $all[] = clone($id);
116         }
117         $done = array();
118         foreach($all as $id) {
119             $idr = DB_DataObject::Factory('invdetail');
120             $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)");
121             
122             $done[] = $id->invdetail_id;
123             // fill in all outgoing based on these reciepts..
124             
125             
126             $done = array_unique(array_merge($done, $this->processOut($id, $done)));
127             
128             
129         }
130         echo "DONE : ". count($done) . " out of $total \n";
131         //print_R($done);
132     
133         exit;
134         
135         print_R($all);
136         
137         // pass 1 - incomming stock only..
138         // pass 2 - 
139         
140         
141         $locs = array();
142         while (true) {
143             $id = array_shift($all);
144             switch($id->invhist_transtype) {
145                 case 'AD': // adjustment
146                     
147                 
148                 
149             }
150             exit;
151             
152             
153             
154             
155         }
156         
157         
158            
159         
160         exit;
161         
162    
163     }
164     
165     function processOut($idr, $done= array())
166     {
167         if (empty($done)) {
168             $done = array(0);
169         }
170       
171         $id = DB_DataObject::Factory('invdetail');
172         
173         
174         
175         
176         $id->query("SELECT
177        
178               *
179                 FROM
180                     
181               invdetailview
182           WHERE
183               invhist_itemsite_id =  {$idr->invhist_itemsite_id}
184               AND
185               invfifo_void = 0
186               AND
187               invdetail_qty < 0
188               AND
189               invfifo_qty_before >= {$idr->invfifo_qty_before}
190               AND
191               invfifo_qty_before <= {$idr->invfifo_qty_after}
192               AND
193               invdetail_location_id = {$idr->invdetail_location_id}
194              
195              
196              AND
197              invdetail_id NOT IN (". implode(',', $done) . ") 
198           ORDER BY
199        
200               invhist_transdate ASC,
201               invdetail_id ASC
202           
203         ");
204         $all = array();
205         while($id->fetch()) {
206             $all[] = clone($id);
207             $ret[] = $id->invdetail_id;
208         }
209        // $done = array();
210         foreach($all as $id) {
211             $idr = DB_DataObject::Factory('invdetail');
212             $idr->query("SELECT invfifo_update_from_invdetail({$id->invdetail_id},true)");
213             $done[] = $id->invdetail_id;
214             
215             // if it's a transfer, then flag the reverse as done as well..
216             if ($id->invhist_transtype == 'RL') {
217                 $idr = DB_DataObject::Factory('invdetail');
218                 $idr->query("SELECT *
219                                 FROM invdetailview WHERE invdetail_invhist_id = {$id->invhist_id} AND invdetail_id != {$id->invdetail_id}
220                             ");
221                 $idr->fetch();
222                 $done[] = $idr->invdetail_id ;
223                 $done = array_unique(array_merge($this->processOut($idr , $done)));
224                 
225                 
226             }
227             
228             
229         }
230         return $done;
231         
232         
233         
234     }
235     
236 }
237
238