/[drupal]/contributions/modules/uc_multi_stock/uc_multi_stock.module
ViewVC logotype

Contents of /contributions/modules/uc_multi_stock/uc_multi_stock.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Sep 5 19:48:17 2008 UTC (14 months, 2 weeks ago) by talbone
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +207 -215 lines
File MIME type: text/x-php
#304226 by talbone: implementing drupal coding standards.
1 <?php
2 // $Id$
3
4 /**
5 * prevent purchasing pruducts below stock levels.
6 *
7 * this module includes some code from the Inventory API & Simple Stock Levels
8 * (http://www.ubercart.org/contrib/132), by CpILL (http://www.ubercart.org/user/29).
9 *
10 */
11
12 /*******************************************************************************
13 * Hook Functions (Drupal)
14 ******************************************************************************/
15
16 /**
17 * Implementation of hook_menu().
18 */
19 function uc_multi_stock_menu($may_cache) {
20 if ($may_cache) {
21 $items[] = array(
22 'path' => 'admin/store/settings/multi_stock',
23 'title' => t('Multi Stock Settings'),
24 'access' => user_access('administer store'),
25 'description' => t('Configure multi stock settings.'),
26 'callback' => 'drupal_get_form',
27 'callback arguments' => array('uc_multi_stock_settings'),
28 'type' => MENU_NORMAL_ITEM,
29 );
30 }
31 return $items;
32 }
33
34 /**
35 * Implementation of hook_form_alter().
36 *
37 * Add validation for cart view form.
38 */
39 function uc_multi_stock_form_alter($form_id, &$form) {
40 // create validations for all the product forms
41 switch ($form_id) {
42 case 'uc_cart_view_form':
43 //$form['#validate'] = array('uc_multi_stock_uc_cart_view_validate' => array()) + $form['#validate'];
44 $form['#validate'] = array('uc_multi_stock_uc_cart_view_validate' => array());
45 break;
46 }
47 }
48
49 /*******************************************************************************
50 * Hook Functions (Ubercart)
51 ******************************************************************************/
52
53 /**
54 * Implementation of hook_cart_item().
55 *
56 * change the model according to attributes - so it will be recognize by tocca_uc_stock_order()
57 */
58 function uc_multi_stock_cart_item($op, &$item) {
59 switch ($op) {
60 case 'load':
61 $sku = uc_multi_get_sku($item->nid, $item->data['attributes']);
62 $item->model = $sku;
63 break;
64 }
65 }
66
67 /**
68 * Implementation of hook_add_to_cart()
69 *
70 * prevent addition of out of stock products
71 */
72 function uc_multi_stock_add_to_cart($nid, $qty, $data) {
73 $node = node_load($nid); // needed for the product
74 $product = uc_product_load($node); // the product
75 $product->title = $node->title; // will be used in error messages
76 $sku = uc_multi_get_sku($nid, $data['attributes']);
77
78 // get the product quantities already in the cart
79 $items = uc_cart_get_contents();
80 foreach ($items as $item) {
81 $cart_sku = uc_multi_get_sku($item->nid, $item->data['attributes']);
82 if ($cart_sku == $sku) {
83 $qty += $item->qty;
84 }
85 }
86
87 // check whether can but this product
88 $error = uc_multi_stock_can_buy($product, $sku, $qty, $data['attributes']);
89
90 if ($error) {
91 $result[] = array(
92 'success' => FALSE,
93 'message' => $error,
94 );
95 }
96 return $result;
97 }
98
99 /**
100 * Implementation of hook_order().
101 * @see uc_cart_checkout_review_form_submit()
102 * @see hook_order()
103 *
104 * Final check with the Inventory Manager before allowing purchase
105 */
106 function uc_multi_stock_order($op, &$order, $arg = null) {
107 switch ($op) {
108 case 'submit':
109 $errors = array();
110 foreach ($order->products as $product) {
111 $data = $product->data;
112 //$sku = uc_multi_get_sku($product->nid, $data['attributes']);
113 $error = uc_multi_stock_can_buy($product, $product->model, $product->qty, $data['attributes']);
114
115 if ($error) {
116 $errors[] = array('pass' => FALSE, 'message' => $error);
117 }
118 }
119
120 if (count($errors) > 0) {
121 return $errors;
122 }
123
124 break;
125 }
126 }
127
128 /*******************************************************************************
129 * Module and Helper Functions
130 ******************************************************************************/
131
132 /**
133 * The settings form.
134 */
135 function uc_multi_stock_settings() {
136 // general settings
137 $form['uc_multi_stock_settings'] = array(
138 '#type' => 'fieldset',
139 '#title' => t('General Multi Stock settings'),
140 '#collapsible' => FALSE,
141 );
142 $form['uc_multi_stock_settings']['uc_multi_stock_use_thershold'] = array(
143 '#type' => 'checkbox',
144 '#title' => t('use threshld for stock'),
145 '#default_value' => variable_get('uc_multi_stock_use_thershold', FALSE),
146 '#description' => t('sell untill stock level reaches threshold value, not 0.'),
147 );
148
149 return system_settings_form($form);
150 }
151
152 /**
153 * validate the view cart form
154 */
155 function uc_multi_stock_uc_cart_view_validate($form_id, $form_values) {
156
157 // for every item in the cart
158 foreach ($form_values['items'] as $item) {
159 $nid = $item['nid'];
160 $data = unserialize($item['data']);
161
162 $node = node_load($nid); // needed for the product
163 $product = uc_product_load($node); // the product
164 $product->title = $node->title; // for use in error messages
165 $sku = uc_multi_get_sku($nid, $data['attributes']);
166
167 // dont test the removal of out of stock products
168 if (!$item['remove'] && $item['qty']>0) {
169 $tmp_error = uc_multi_stock_can_buy($product, $sku, $item['qty'], $data['attributes']);
170 if ($tmp_error) {
171 $error .= $tmp_error .'<br /><br />';
172 }
173 }
174 }
175
176 if ($error) {
177 form_set_error('stock', $error);
178 }
179 }
180
181 /**
182 * Test if we can buy this product.
183 */
184 function uc_multi_stock_can_buy($product, $sku, $qty, $attributes) {
185 // test for missing sku - can happen when the deafult option is selected
186 if (!$sku) {
187 $sku = $product->model;
188 }
189 $stock = db_fetch_object(db_query("SELECT active, threshold, stock FROM {uc_product_stock} WHERE sku = '%s'", $sku));
190
191 // check if stock level are active for this product
192 if ($stock->active) {
193 // check the buttom limit for stock - 0 or threshold ?
194 $stock_qty = 0;
195 if (variable_get('uc_multi_stock_use_thershold', FALSE)) {
196 $stock_qty = $stock->threshold;
197 }
198
199 // check for out of stock
200 $error = uc_multi_stock_not_in_stock($stock->stock, $stock_qty, $product, $attributes);
201 if ($error) {
202 return $error;
203 }
204
205 // check for buying too many
206 $left = $stock->stock - $stock_qty;
207 $error = uc_multi_stock_not_enough_in_stock($qty, $left, $product, $attributes);
208 if ($error) {
209 return $error;
210 }
211 }
212 }
213
214 /**
215 * Test for product not in stock.
216 */
217 function uc_multi_stock_not_in_stock($stock, $threshold, $product, $attributes) {
218 if ($stock <= $threshold) {
219 return theme('uc_multi_stock_out_of_stock_error', $product, $attributes);
220 }
221 return FALSE;
222 }
223
224 /**
225 * test for not enough in stock
226 */
227 function uc_multi_stock_not_enough_in_stock($qty, $left, $product, $attributes) {
228 if ($qty > $left) {
229 return theme('uc_multi_stock_too_many_in_order_error', $left, $product, $attributes);
230 }
231 return FALSE;
232 }
233
234 /**
235 * themeable error messages
236 */
237 function theme_uc_multi_stock_out_of_stock_error($product = null, $attributes) {
238 if (is_array($attributes) and count($attributes) > 0) {
239 return (t("We're sorry. The @product in your selected size/color combination is out of stock. Please consider another size/color in this style.", array('@product' => $product->title)));
240 }
241 else{
242 return (t("We're sorry. The @product is out of stock.", array('@product' => $product->title)));
243 }
244 }
245
246 function theme_uc_multi_stock_too_many_in_order_error($qty = 0, $product = null , $attributes) {
247 if (is_array($attributes) and count($attributes) > 0) {
248 return (t("We're sorry. We have only @qty unit(s) of the @product in your selected size/color combination left in stock. Please try again with fewer units or consider another size/color in this style.", array('@qty' => $qty, '@product' => $product->title)));
249 }
250 else{
251 return (t("We're sorry. We have only @qty unit(s) of the @product left in stock. Please try again with less units.", array('@qty' => $qty, '@product' => $product->title)));
252 }
253 }
254
255
256 // //////////////////////////////////////////////
257 //
258 // code from Inventory API & Simple Stock Levels
259 //
260 // //////////////////////////////////////////////
261 function uc_multi_get_sku($nid, $attributes) {
262 if (is_array($attributes) and count($attributes) > 0) {
263 // There are attributes so the model (SKU) is dependant on the combo of
264 // attributes, so get all the sub products and find the attribute combo
265 // to match what we've been passed
266 foreach (uc_multi_get_subproducts($nid) as $product)
267 if (_uc_array_same($product['attributes'], $attributes)) {
268 return $product['model'];
269 }
270 }
271 else {
272 // NO attributes so get SKU from the products node
273 $product = node_load($nid);
274 return $product->model;
275 }
276 return FALSE;
277 }
278
279 /**
280 * For products that have attributes there will be Sub-Products,
281 * one model (SKU) for every combination of attributes.
282 * This function retrives a list of these
283 * for the given Node ID
284 *
285 * @param
286 * Product Node ID
287 *
288 * @return
289 * Array of sub products, empty array if the product has no attrributes
290 */
291 function uc_multi_get_subproducts($nid){
292 $out = array();
293
294 if(!empty($nid) and module_exists('uc_attribute')){
295 $sql = 'SELECT * FROM {uc_product_adjustments} WHERE nid = '.$nid;
296 $q = db_query($sql);
297 $i = 0;
298 while($product = db_fetch_array($q)){
299 $out[$i]['attributes'] = unserialize($product['combination']);
300 $out[$i]['model'] = $product['model'];
301 $i++;
302 }
303 return $out;
304 }
305 else {
306 return array();
307 }
308 }
309
310 function _uc_array_same($a1, $a2) {
311 if (!is_array($a1) || !is_array($a2)) {
312 return FALSE;
313 }
314
315 $keys = array_merge(array_keys($a1), array_keys($a2));
316
317 foreach ($keys as $k) {
318 if (!isset($a2[$k]) || !isset($a1[$k])) {
319 return FALSE;
320 }
321
322 if (is_array($a1[$k]) || is_array($a2[$k])) {
323 if (!array_same($a1[$k], $a2[$k])) {
324 return FALSE;
325 }
326 }
327 else {
328 if (! ($a1[$k] == $a2[$k])) {
329 return FALSE;
330 }
331 }
332 }
333
334 return TRUE;
335 }
336

  ViewVC Help
Powered by ViewVC 1.1.2