Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Fifo / FillPurchasePrices.php
1 <?php
2
3 /**
4  * fill in products pruchase price
5  * 
6  *
7  */
8 require_once 'ProcessBase.php';
9  
10 class Pman_Xtuple_Fifo_FillPurchasePrices extends  Pman_Xtuple_Fifo_ProcessBase
11 {
12     static $cli_desc = "Fill in products pruchase price";
13    
14     static $permitError = false;
15     
16     function get()
17     {
18         
19        // DB_DataObject::debugLevel(1);
20         // set up the failure code..
21         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
22         
23         $i = DB_DataObject::factory('item');
24         $i->_join .= "
25             LEFT JOIN 
26                 itemsite 
27             ON 
28                 itemsite_item_id = item_id
29             WHERE
30                 itemsite_item_id > 0
31         ";
32         $i->selectAdd();
33         $i->selectAdd("item_id,itemsite_id");
34         $i->orderby('item_id');
35         $items = $i->fetchAll('item_id','itemsite_id');
36         foreach ($items as $item_id => $itemsite_id){
37             // now see if we have any itemsrcs..
38             $pi = $this->fetch_poitem($itemsite_id );
39             if (!$pi) {
40                 continue;
41             }
42             
43             $is =DB_DataObject::factory('itemsrc');
44             $is->itemsrc_item_id = $item_id;
45             $vender_to_src = $is->fetchAll('itemsrc_vend_id', 'itemsrc_id');
46             
47             $product_has_price = false;
48             
49             foreach($vender_to_src as $vend_id => $itemsrc_id) {
50                 if ($pi->pohead_vend_id != $vend_id) {
51                     // give up, the last purchase is not from this vendor..
52                     continue;
53                     
54                 }
55                 $has_price = false;
56                 
57                 $ip = DB_DataObject::factory('itemsrcp');
58                 $ip->itemsrcp_itemsrc_id = $itemsrc_id;
59                 if ($ip->count()) {
60                     // we have some..
61                     foreach($ip->fetchAll() as $ip) {
62                         $has_price =  1;
63                         $product_has_price = 1;
64                         
65                         if($ip->itemsrcp_price != ''){
66                             continue;
67                         }
68                         $ip->itemsrcp_qtybreak = 0;
69                         $ip->itemsrcp_price = $pi->poitem_unitprice;
70                         $ip->itemsrcp_curr_id = $pi->pohead_curr_id;
71
72                         $ip->update();
73                         
74                     }
75                 }
76                 if ($has_price) {
77                     continue;
78                 }
79                 
80                 $ip->itemsrcp_itemsrc_id = $itemsrc_id;
81                 $ip->itemsrcp_qtybreak = 0;
82                 $ip->itemsrcp_price = $pi->poitem_unitprice;
83                 $ip->itemsrcp_curr_id = $pi->pohead_curr_id;
84                 $ip->itemsrcp_updated = date('Y-m-d');
85
86                 $ip->insert();
87                 $product_has_price = 1;
88             }
89             // now we have looked at all the itemsrc's and not found any pu
90             if ($product_has_price) {
91                 // we have already filled in a price..
92                 continue;
93             }
94                 
95             // at this point we do not have a itemsrc for this product..
96             // create one based on the pi.
97             
98             $is = DB_DataObject::factory('itemsrc');
99             $vend = DB_DataObject::factory('vendinfo');
100             $vend->get($pi->pohead_vend_id);
101             $is->itemsrc_item_id = $item_id;
102             $is->itemsrc_vend_id = $pi->pohead_vend_id;
103             $is->itemsrc_vend_item_number = $vend->vend_number;
104             $is->itemsrc_vend_uom = 1;
105             $is->itemsrc_invvendoruomratio = 1;
106             $is->itemsrc_minordqty = 0;
107             $is->itemsrc_multordqty = 0;
108             $is->itemsrc_leadtime = 0;
109             $is->itemsrc_ranking = 1;
110             $is->itemsrc_active = TRUE;
111             $is->itemsrc_default = TRUE;
112             $is->insert();
113             
114             $ip = DB_DataObject::factory('itemsrcp');
115             $ip->itemsrcp_itemsrc_id = $is->itemsrc_id;
116             $ip->itemsrcp_qtybreak = 0;
117             $ip->itemsrcp_price = $pi->poitem_unitprice;
118             $ip->itemsrcp_curr_id = $pi->pohead_curr_id;
119             $ip->itemsrcp_updated = date('Y-m-d');
120             $ip->insert();
121             // then create an itemsrcp for it as weell..
122             
123         }
124         exit;
125         
126     }
127     
128     function fetch_poitem($itemsite_id){
129         $pi = DB_DataObject::factory('poitem');
130         $pi->_join .= "
131             LEFT JOIN 
132                 pohead 
133             ON 
134                 poitem_pohead_id = pohead_id
135         ";
136
137         $pi->whereAdd("poitem_itemsite_id = {$itemsite_id}");
138         $pi->orderby("poitem_duedate DESC");
139         $pi->limit(1);
140         return $pi->find(true) ? $pi : '';
141     }
142     
143 }