Basic search API integration.
[sandbox/serialjaywalker/1195272.git] / modules / product / commerce_pos_product.module
1 <?php
2
3 /**
4 * Implements hook_commerce_pos_action_info().
5 */
6 function commerce_pos_product_commerce_pos_action_info() {
7 $actions['commerce_pos_product'] = array(
8 'callbacks' => array(
9 'parse' => 'commerce_pos_product_parse',
10 'execute' => 'commerce_pos_product_execute',
11 'validate' => array(
12 'commerce_pos_order_require_modifiable_order',
13 'commerce_pos_product_validate',
14 'commerce_pos_line_item_require_integer_quantity'
15 ),
16 'access' => 'commerce_pos_product_access',
17 'extract' => 'commerce_pos_product_extract',
18 ),
19 'weight' => 100,
20 );
21 return $actions;
22 }
23
24 /**
25 * Parse callback for product action.
26 */
27 function commerce_pos_product_parse($input, $action_text) {
28 //This is just the default action; it is heavy.
29 return TRUE;
30 }
31
32 /**
33 * Implements hook_enable().
34 */
35 function commerce_pos_product_enable() {
36 $vocabulary = taxonomy_vocabulary_machine_name_load('commerce_pos_categories');
37
38 if (empty($vocabulary)) {
39 $vocabulary = (object) array(
40 'name' => t('POS Categories'),
41 'machine_name' => 'commerce_pos_categories',
42 'description' => t('The categories used in the POS'),
43 'help' => t('This taxonomy is used for categorizing products for display in the POS interface.'),
44 );
45 taxonomy_vocabulary_save($vocabulary);
46 }
47
48 $field_name = 'commerce_pos_category';
49 $field = field_info_field($field_name);
50
51 if (empty($field)) {
52 $field = array(
53 'field_name' => 'commerce_pos_category',
54 'type' => 'taxonomy_term_reference',
55 'cardinality' => 1,
56 'settings' => array(
57 'allowed_values' => array(
58 array(
59 'vocabulary' => $vocabulary->machine_name,
60 'parent' => 0,
61 ),
62 ),
63 ),
64 );
65 field_create_field($field);
66 }
67
68 $entity_info = entity_get_info('commerce_product');
69 $bundles = $entity_info['bundles'];
70
71 foreach($bundles as $bundle_name => $bundle) {
72 commerce_pos_product_configure_product_type($bundle_name);
73 }
74 }
75
76 /**
77 * Implements hook_modules_enabled().
78 */
79 function commerce_pos_product_modules_enabled($modules) {
80 $field_name = 'commerce_pos_category';
81 // Loop through all the newly enabled modules.
82 foreach ($modules as $module) {
83 // If the module implements hook_commerce_customer_profile_type_info()...
84 if (module_hook($module, 'commerce_product_type_info')) {
85 $product_types = module_invoke($module, 'commerce_product_type_info');
86
87 foreach ($product_types as $type_name => $product_type) {
88 commerce_pos_product_configure_product_type($type_name);
89 }
90 }
91 }
92 }
93
94 /**
95 * Add the POS taxonomy field to a product bundle.
96 *
97 * @param $bundle_name
98 */
99 function commerce_pos_product_configure_product_type($bundle_name) {
100 $field_name = 'commerce_pos_category';
101 $vocabulary = taxonomy_vocabulary_machine_name_load('commerce_pos_categories');
102
103 $instance = field_info_instance('commerce_product', $field_name, $bundle_name);
104 if (empty($instance)) {
105 $instance = array(
106 'field_name' => $field_name,
107 'entity_type' => 'commerce_product',
108 'label' => $vocabulary->name,
109 'bundle' => $bundle_name,
110 'description' => $vocabulary->help,
111 'widget' => array(
112 'type' => 'options_list',
113 'weight' => -4,
114 ),
115 );
116 field_create_instance($instance);
117 }
118 }
119
120 /**
121 * Extract callback for product action.
122 */
123 function commerce_pos_product_extract(&$variables, $values) {
124 if (!empty($values['order']['order_id'])) {
125 $variables['order'] = commerce_order_load($values['order']['order_id']);
126 }
127 else {
128 $variables['order'] = commerce_pos_order_new();
129 }
130 }
131
132 /**
133 * Access callback for adding a product.
134 *
135 * @param $variables
136 * An array of POS variables
137 */
138 function commerce_pos_product_access($variables) {
139 $order = $variables['order'];
140 return commerce_order_access('update', $order);
141 }
142
143 /**
144 * Validator for product action.
145 */
146 function commerce_pos_product_validate(&$variables) {
147 $errors = array();
148 $args = explode('*', $variables['input']);
149 if (count($args) > 2) {
150 $errors[] = t('Unable to determine product code');
151 //Return now to avoid passing empty products to entity_metadata_wrapper().
152 return $errors;
153 }
154 $quantity = empty($args[1]) ? 1 : $args[0];
155 $product_code = empty($args[1]) ? $args[0] : $args[1];
156
157 //Make sure that the product code corresponds to a real product.
158 //TODO: we might want a different product code field instead of sku for this
159 $product = commerce_product_load_by_sku($product_code);
160
161 if ($product === FALSE) {
162 $errors[] = t('Invalid product.');
163 return $errors;
164 }
165
166 if ( round($quantity, 2) == 0 || abs($quantity) > 9999999999) {
167 $errors[] = t('Invalid quantity');
168 return $errors;
169 }
170
171 $variables['quantity'] = $quantity;
172 $variables['product_code'] = $product_code;
173
174 $order = $variables['order'];
175
176 $line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);
177
178 rules_invoke_event('commerce_pos_line_item_add', $order, $line_item);
179 if (!empty($order->error)) {
180 $errors[] = $order->error;
181 }
182
183 $variables['line_item'] = $line_item;
184 $variables['product'] = $product;
185 $variables['order'] = $order;
186
187 return $errors;
188 }
189
190 /**
191 * Execute callback for product action.
192 */
193 function commerce_pos_product_execute(&$variables) {
194 //Add the product to the order.
195 $order = $variables['order'];
196 if (!$order->order_id) {
197 commerce_order_save($order);
198 }
199 $product = $variables['product'];
200 $line_item = $variables['line_item'];
201 //Set the order_id, in case it didn't exist when we created the line item.
202 $line_item->order_id = $order->order_id;
203
204 $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
205 commerce_pos_line_item_add_to_order($line_item, $order);
206 commerce_order_save($order);
207 $variables['order'] = $order;
208 }
209
210 /**
211 * Implements hook_views_api() {
212 */
213 function commerce_pos_product_views_api() {
214 return array(
215 'api' => 3,
216 'path' => drupal_get_path('module', 'commerce_pos_product') . '/includes/views',
217 );
218 }