Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
product_management.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_management.inc';
19 
31 {
32 
41  function Product_Management(&$pm)
42  {
43  $this->Asset_Management($pm);
44 
45  // include price class so unserialize() doesn't barf
46  require_once SQ_PACKAGES_PATH.'/ecommerce/price/product_price.inc';
47 
48  // images for products will be linked assets so dont include here
49  // Also quantity is not an issue at the moment
50  // NB: Price is 0 (zero) if free, or -1 if unknown
51  $this->vars = array(
52  'name' => array(
53  'added' => '0.1',
54  'type' => 'text',
55  'default' => '',
56  'description' => 'The name of the Product that this asset represents',
57  'is_contextable' => TRUE,
58  ),
59  'price' => array(
60  'added' => '0.1',
61  'type' => 'float',
62  'default' => 0.0,
63  'description' => 'Sale value of this Product',
64  'is_admin' => false,
65  ),
66  'price_obj' => array(
67  'added' => '0.1',
68  'type' => 'serialise',
69  'default' => new Product_Price(),
70  'description' => 'Proper object for manipulation of the price',
71  ),
72  /*
73  'quantity' => array(
74  'added' => '0.1',
75  'type' => 'int',
76  'default' => 0,
77  'description' => 'Quantity of the item on hand',
78  ),
79  */
80  'description' => array(
81  'added' => '0.1',
82  'type' => 'text',
83  'default' => '',
84  'description' => 'Long description of the product',
85  'is_admin' => false,
86  'is_contextable' => TRUE,
87  ),
88  'short_description' => array(
89  'added' => '0.1',
90  'type' => 'text',
91  'default' => '',
92  'description' => 'Short description of product for small display space',
93  'is_admin' => false,
94  'is_contextable' => TRUE,
95  ),
96  'details' => array(
97  'added' => '0.1',
98  'type' => 'text',
99  'default' => '',
100  'description' => 'Detailed specifications of the Product',
101  'is_admin' => false,
102  'is_contextable' => TRUE,
103  ),
104  'product_code' => array(
105  'added' => '0.2',
106  'type' => 'text',
107  'default' => '',
108  'description' => 'Product code of the Product',
109  'is_admin' => false,
110  'is_contextable' => FALSE,
111  ),
112  'financial_code' => array(
113  'added' => '0.2',
114  'type' => 'text',
115  'default' => '',
116  'description' => 'Financial code of the Product',
117  'is_admin' => false,
118  'is_contextable' => FALSE,
119  ),
120  'refund_type' => array(
121  'added' => '0.2',
122  'type' => 'selection',
123  'default' => 'NonRefundable',
124  'description' => 'If product is refundale',
125  'is_admin' => false,
126  'parameters' => Array(
127  'multiple' => FALSE,
128  'allow_empty' => FALSE,
129  'options' => Array(
130  'NonRefundable' => 'Non-Refundable',
131  'Expires' => 'Refund Period Expires in a Week',
132  'Custom' => 'Custom',
133  ),
134  ),
135  'is_contextable' => FALSE,
136 
137  ),
138  'accept_promotional_code' => Array(
139  'added' => '0.2',
140  'type' => 'boolean',
141  'default' => FALSE,
142  'description' => 'The flag to accept promotional codes on this product',
143  'parameters' => Array(
144  'allow_empty' => FALSE,
145  ),
146  'is_admin' => FALSE,
147  'is_contextable' => FALSE,
148  ),
149  );
150 
151  }//end Product_Management()
152 
153 
162  function _upgrade($current_version)
163  {
164  if (!parent::_upgrade($current_version)) return FALSE;
165 
166  if (version_compare($current_version,'0.2','<')) {
167  // renames an instance variable of the Price object included in all Product objects
168  // 'tax_inclusive' to 'value_has_tax'
169  // also will set 'calculate_tax' if 'tax_inclusive' was previously set
170 
171  pre_echo('UPGRADING PRODUCT ASSET - FROM VERSION '.$current_version.' TO VERSION 0.2');
172 
173  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
174  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
175  $db = MatrixDAL::getDb();
176 
177  // get all type descendants
178  $type_codes = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('product', TRUE);
179  for ($i=0; $i<count($type_codes); $i++) {
180  $type_codes[$i] = MatrixDAL::quote($type_codes[$i]);
181  }
182  $in = implode(',', $type_codes);
183 
184  // get attribute id of the price object
185  $sql = 'SELECT attrid
186  FROM sq_ast_attr
187  WHERE type_code IN ('.$in.')
188  AND name = '.MatrixDAL::quote('price_obj');
189 
190  try {
191  $attrid = MatrixDAL::executeSqlAll($sql);
192  } catch (Exception $e) {
193  throw new Exception('Unable to get attribute id of price object due to database error: '.$e->getMessage());
194  }
195  $ids = Array();
196  foreach ($attrid as $row => $data) {
197  $ids[] = $data['attrid'];
198  }
199  $in = implode(',', $ids);
200 
201  // get all assetids and their custom_vals
202  $sql = 'SELECT assetid, custom_val, attrid
203  FROM sq_ast_attr_val
204  WHERE attrid IN ('.$in.')';
205 
206  try {
207  $custom_vals = MatrixDAL::executeSqlAssoc($sql);
208  } catch (Exception $e) {
209  throw new Exception('Unable to get values of existing product prices due to database error: '.$e->getMessage());
210  }
211 
212  pre_echo('UPGRADING THE PRICE_OBJ ATTRIBUTE');
213 
214  // include the price class so unserialize() doesn't barf
215  require_once SQ_PACKAGES_PATH.'/ecommerce/price/product_price.inc';
216  // loop through all custom_vals and perform our updates
217  foreach ($custom_vals as $row => $info) {
218  $object = unserialize($info['custom_val']);
219  if (isset($object->tax_inclusive)) {
220  // if tax_inclusive was previously set, they would have wanted tax calculated
221  if ($object->tax_inclusive) {
222  $object->calculate_tax = TRUE;
223  // add tax to the existing amount
224  $object->value_has_tax = FALSE;
225  } else {
226  $object->calculate_tax = FALSE;
227  $object->value_has_tax = FALSE;
228  }
229  unset($object->tax_inclusive);
230  }
231  $serialized = serialize($object);
232 
233  // now we can update our db with this new value
234  $sql = 'UPDATE sq_ast_attr_val
235  SET custom_val = '.MatrixDAL::quote($serialized).'
236  WHERE attrid = '.MatrixDAL::quote($info['attrid']).'
237  AND assetid = '.MatrixDAL::quote($info['assetid']);
238 
239  try {
240  $result = MatrixDAL::executeSql($sql);
241  } catch (Exception $e) {
242  throw new Exception('Unable to update value of product due to database error: '.$e->getMessage());
243  }
244  }
245 
246  // lastly change the default value
247  // first get the default value
248  $sql = 'SELECT attrid, default_val
249  FROM sq_ast_attr
250  WHERE attrid IN ('.$in.')';
251 
252  try {
253  $default_val = MatrixDAL::executeSqlAssoc($sql);
254  } catch (Exception $e) {
255  throw new Exception('Unable to get default value of product due to database error: '.$e->getMessage());
256  }
257 
258  foreach ($default_val as $row => $info) {
259  // alter it
260  $object = unserialize($info['default_val']);
261  if (isset($object->tax_inclusive)) {
262  $object->value_has_tax = $object->tax_inclusive;
263  unset($object->tax_inclusive);
264  }
265  $serialized = serialize($object);
266 
267  $sql = 'UPDATE sq_ast_attr
268  SET default_val = '.MatrixDAL::quote($serialized).'
269  WHERE attrid = '.MatrixDAL::quote($info['attrid']);
270 
271  try {
272  $result = MatrixDAL::executeSql($sql);
273  } catch (Exception $e) {
274  throw new Exception('Unable to update default value of product due to database error: '.$e->getMessage());
275  }
276  }
277 
278  pre_echo('UPGRADE FOR PRODUCT ASSETS COMPLETE');
279 
280  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
281  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
282 
283  }//end if
284 
285  return TRUE;
286 
287  }//end _upgrade()
288 
289 
290 }//end class
291 ?>