summaryrefslogtreecommitdiffstats
path: root/modules/product/commerce_pos_product.module
blob: dea948eeb3e54fcefd043aa8848960149d77fd10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php

/**
 * Implements hook_commerce_pos_action_info().
 */
function commerce_pos_product_commerce_pos_action_info() {
  $actions['commerce_pos_product'] = array(
    'callbacks' => array(
      'parse' => 'commerce_pos_product_parse',
      'execute' => 'commerce_pos_product_execute',
      'validate' => array(
      	'commerce_pos_order_require_modifiable_order',
      	'commerce_pos_product_validate',
      	'commerce_pos_line_item_require_integer_quantity'
      ),
      'access' => 'commerce_pos_product_access',
      'extract' => 'commerce_pos_product_extract',
    ),
    'weight' => 100,
  );
  return $actions;
}

/**
 * Parse callback for product action.
 */
function commerce_pos_product_parse($input, $action_text) {
  //This is just the default action; it is heavy.
  return TRUE;
}

/**
 * Implements hook_enable().
 */
function commerce_pos_product_enable() {
  $vocabulary = taxonomy_vocabulary_machine_name_load('commerce_pos_categories');

  if (empty($vocabulary)) {
    $vocabulary = (object) array(
      'name' => t('POS Categories'),
      'machine_name' => 'commerce_pos_categories',
      'description' => t('The categories used in the POS'),
      'help' => t('This taxonomy is used for categorizing products for display in the POS interface.'),
    );
    taxonomy_vocabulary_save($vocabulary);
  }

  $field_name = 'commerce_pos_category';
  $field = field_info_field($field_name);

  if (empty($field)) {
    $field = array(
      'field_name' => 'commerce_pos_category',
      'type' => 'taxonomy_term_reference',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);
  }

  $entity_info = entity_get_info('commerce_product');
  $bundles = $entity_info['bundles'];

  foreach($bundles as $bundle_name => $bundle) {
    commerce_pos_product_configure_product_type($bundle_name);
  }
}

/**
 * Implements hook_modules_enabled().
 */
function commerce_pos_product_modules_enabled($modules) {
  $field_name = 'commerce_pos_category';
  // Loop through all the newly enabled modules.
  foreach ($modules as $module) {
    // If the module implements hook_commerce_customer_profile_type_info()...
    if (module_hook($module, 'commerce_product_type_info')) {
      $product_types = module_invoke($module, 'commerce_product_type_info');

      foreach ($product_types as $type_name => $product_type) {
        commerce_pos_product_configure_product_type($type_name);
      }
    }
  }
}

/**
 * Add the POS taxonomy field to a product bundle.
 *
 * 	@param $bundle_name
 */
function commerce_pos_product_configure_product_type($bundle_name) {
  $field_name = 'commerce_pos_category';
  $vocabulary = taxonomy_vocabulary_machine_name_load('commerce_pos_categories');

  $instance = field_info_instance('commerce_product', $field_name, $bundle_name);
  if (empty($instance)) {
    $instance = array(
    		'field_name' => $field_name,
    		'entity_type' => 'commerce_product',
        'label' => $vocabulary->name,
        'bundle' => $bundle_name,
      	'description' => $vocabulary->help,
    		'widget' => array(
      		'type' => 'options_list',
        	'weight' => -4,
      ),
    );
    field_create_instance($instance);
  }
}

/**
 * Extract callback for product action.
 */
function commerce_pos_product_extract(&$variables, $values) {
  if (!empty($values['order']['order_id'])) {
    $variables['order'] = commerce_order_load($values['order']['order_id']);
  }
  else {
    $variables['order'] = commerce_pos_order_new();
  }
}

/**
 *  Access callback for adding a product.
 *
 *  @param $variables
 *    An array of POS variables
 */
function commerce_pos_product_access($variables) {
  $order = $variables['order'];
  return commerce_order_access('update', $order);
}

/**
 * Validator for product action.
 */
function commerce_pos_product_validate(&$variables) {
  $errors = array();
  $args = explode('*', $variables['input']);
  if (count($args) > 2) {
    $errors[] = t('Unable to determine product code');
    //Return now to avoid passing empty products to entity_metadata_wrapper().
    return $errors;
  }
  $quantity = empty($args[1]) ? 1 : $args[0];
  $product_code = empty($args[1]) ? $args[0] : $args[1];

  //Make sure that the product code corresponds to a real product.
  //TODO: we might want a different product code field instead of sku for this
  $product = commerce_product_load_by_sku($product_code);

  if ($product === FALSE) {
    $errors[] = t('Invalid product.');
    return $errors;
  }

  if ( round($quantity, 2) == 0 || abs($quantity) > 9999999999) {
    $errors[] = t('Invalid quantity');
    return $errors;
  }

  $variables['quantity'] = $quantity;
  $variables['product_code'] = $product_code;

  $order = $variables['order'];

  $line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);

  rules_invoke_event('commerce_pos_line_item_add', $order, $line_item);
  if (!empty($order->error)) {
    $errors[] = $order->error;
  }

  $variables['line_item'] = $line_item;
  $variables['product'] = $product;
  $variables['order'] = $order;

  return $errors;
}

/**
 * Execute callback for product action.
 */
function commerce_pos_product_execute(&$variables) {
  //Add the product to the order.
  $order = $variables['order'];
  if (!$order->order_id) {
    commerce_order_save($order);
  }
  $product = $variables['product'];
  $line_item = $variables['line_item'];
  //Set the order_id, in case it didn't exist when we created the line item.
  $line_item->order_id = $order->order_id;

  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
  commerce_pos_line_item_add_to_order($line_item, $order);
  commerce_order_save($order);
  $variables['order'] = $order;
}

/**
 * Implements hook_views_api() {
 */
function commerce_pos_product_views_api() {
  return array(
  	'api' => 3,
    'path' => drupal_get_path('module', 'commerce_pos_product') . '/includes/views',
  );
}