DataObjects/Ipshead.php
[Pman.Xtuple] / DataObjects / Ipshead.php
1 <?php
2 /**
3  * Table Definition for ipshead
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Xtuple_DataObjects_Ipshead extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'ipshead';             // table name
13     public $ipshead_id;                      // int4(4)  not_null default_nextval%28%28%22ipshead_ipshead_id_seq%22%29%3A%3Aregclass%29 primary_key
14     public $ipshead_name;                    // text(-1)  unique_key
15     public $ipshead_descrip;                 // text(-1)  
16     public $ipshead_effective;               // date(4)  
17     public $ipshead_expires;                 // date(4)  
18     public $ipshead_curr_id;                 // int4(4)  not_null default_basecurrid%28%29
19     public $ipshead_updated;                 // date(4)  
20
21     
22    /**
23     * Getter / Setter for $ipshead_curr_id
24     *
25     * @param    mixed   (optional) value to assign
26     * @access   public
27     */
28     public function curr() {
29         return func_num_args() ? $this->link('ipshead_curr_id', func_get_arg(0)) : $this->link('ipshead_curr_id');
30     }
31
32
33     /* the code above is auto generated do not remove the tag below */
34     ###END_AUTOCODE
35     
36     function applyFilters($q, $au, $roo)
37     {
38         if(!empty($q['_with_item'])){
39             $item_id = (int) $q['_with_item'];
40             $this->selectAdd("
41                 (
42                     SELECT 
43                             COALESCE(ipsitem_price,0.0)
44                     FROM
45                             ipsiteminfo
46                     WHERE
47                             ipsitem_ipshead_id = ipshead_id
48                         AND
49                             ipsitem_item_id = {$item_id}
50                 ) AS ipshead_ipsitem_price,
51                 (
52                     SELECT 
53                             COALESCE(ipsitem_id,0)
54                     FROM
55                             ipsiteminfo
56                     WHERE
57                             ipsitem_ipshead_id = ipshead_id
58                         AND
59                             ipsitem_item_id = {$item_id}
60                 ) AS ipshead_ipsitem_id
61             ");
62         }
63         
64     }
65     
66     function beforeUpdate()
67     {
68         $this->ipshead_updated = $this->sqlValue('NOW()');
69     }
70     function beforeInsert()
71     {
72         $this->ipshead_updated = $this->sqlValue('NOW()');
73     }
74     
75     function beforeDelete($dependants_array, $roo)
76     {
77         // check if assigned.
78         
79         $ass = DB_DataObject::factory('ipsass');
80         $ass->ipsass_ipshead_id = $this->ipshead_id;
81         if ($ass->count()) {
82             $roo->jerr("This price list is assigned to a customer - please change that customer to another price list first");
83         }
84         $ass = DB_DataObject::factory('ipsiteminfo');
85         $ass->query("DELETE FROM ipsiteminfo WHERE ipsiteminfo_ipshead_id = {$this->id}");
86         return true;
87     }
88     
89     
90     function itemPrice($item) {
91         $ipsitem = DB_DataObject::Factory('ipsiteminfo');
92         $ipsitem->ipsitem_item_id  = $item->pid();
93         $ipsitem->ipsitem_ipshead_id = $this->pid();
94         if (!$ipsitem->find(true)) {
95             return 0;
96         }
97         return $ipsitem->ipsitem_price;
98         
99         
100     }
101     
102     function updatePrices($kv, $imap)
103     {
104         /*
105             public $__table = 'ipsitem';             // table name
106             public $ipsitem_id;                      // int4(4)  
107             public $ipsitem_ipshead_id;              // int4(4)  
108             public $ipsitem_item_id;                 // int4(4)  
109             public $ipsitem_qtybreak;                // numeric(-1)  
110             public $ipsitem_price;                   // numeric(-1)  
111             public $ipsitem_qty_uom_id;              // int4(4)  
112             public $ipsitem_price_uom_id;            // int4(4)  
113             public $ipsitem_discntprcnt;             // numeric(-1)  
114             public $ipsitem_fixedamtdiscount;        // numeric(-1)
115             
116             ipsitem_id               | 10517
117            ipsitem_ipshead_id       | 65
118            ipsitem_item_id          | 2768
119            ipsitem_qtybreak         | 0.000000
120            ipsitem_price            | 14.5000
121            ipsitem_qty_uom_id       | 4
122            ipsitem_price_uom_id     | 4
123            ipsitem_discntprcnt      | 0.000000
124            ipsitem_fixedamtdiscount | 0.0000
125
126                     
127         */
128         
129         // grab a list of current prices with ids'
130         $d = DB_DataObject::Factory('ipsiteminfo');
131         $d->selectAdd();
132         $d->selectAdd('
133             ipsitem_id,
134             item_number,
135             ipsitem_price
136             
137         ');
138         $d->_join = ' LEFT JOIN item ON ipsiteminfo.ipsitem_item_id = item.item_id ';
139         
140         $d->ipsitem_ipshead_id = $this->pid();
141         $d->find();
142         $old = array();
143         while ($d->fetch()) {
144             $old[$d->item_number] = clone($d);
145         }
146         
147         $deleted = 0;
148         $updated = 0;
149         $inserted = 0;
150         
151         foreach($kv as $item=>$price) {
152             $o = isset($old[$item]) ? $old[$item] : false;
153             // no record, and not updated.
154             if (!strlen($price) && $o === false) {
155                 continue;
156             }
157             // updated or not changed.
158             if (strlen($price) && $o !== false) {
159                 // not changed.
160                 if ($o->ipsitem_price == $price) {
161                     continue;
162                 }
163                 $oo = clone($o);
164                 $o->ipsitem_price = $price;
165                 $o->update($oo);
166                 $updated++;
167                 continue;
168             }
169             // new price
170             if (strlen($price) && $o === false) {
171                  $d = DB_DataObject::Factory('ipsiteminfo');
172                 $d->setFrom(array(
173
174                     'ipsitem_ipshead_id' => $this->pid(),
175                     'ipsitem_item_id' => $imap[$item],
176                     'ipsitem_qtybreak' =>  0.0,
177                     'ipsitem_price' => $price,
178                    
179                     'ipsitem_discntprcnt' => 0.0,
180                     'ipsitem_fixedamtdiscount' => 0.0
181                     
182                     
183                 ));
184                 $d->ipsitem_qty_uom_id = $d->sqlValue("getuomid('EA')");
185                 $d->ipsitem_price_uom_id = $d->sqlValue("getuomid('EA')");
186                 $d->insert();
187                 $inserted++;
188                 continue;
189                 
190                 
191                 
192             }
193             // o is true, and price is empty..
194             // theory goes we should delete this item..
195             //$o->delete();
196             //$deleted++;
197             // do not delete prices... 
198         }
199         
200         $x = $this->factory($this->tableName());
201         $x->get($this->pid());
202         $xx = clone($x);
203         $x->ipshead_updated = $this->sqlValue('NOW()');
204         $x->update($xx);
205         
206         
207         return array(
208             'deleted' => $deleted,
209             'updated' => $updated,
210             'inserted' => $inserted
211         );
212         
213         
214         
215         
216         
217     }
218     
219 }