summaryrefslogtreecommitdiffstats
path: root/modules/order/commerce_pos_order.module
blob: 22bcbb73c885f5cc3c81d62cd911f9eabcd18fdb (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php

/**
 * @file
 * Contains functions for the commerce_pos_order module
 */

/**
 * Implements hook_entity_info_alter().
 */
function commerce_pos_order_entity_info_alter(&$entity_info) {
  //Adds display modes for the receipt and POS screen.
  $entity_info['commerce_order']['view modes']['pos screen'] = array(
    'label' => t('POS Screen'),
    'custom settings' => TRUE,
  );
  $entity_info['commerce_order']['view modes']['pos receipt'] = array(
    'label' => t('POS Receipt'),
    'custom settings' => TRUE,
  );
}

/**
 * Implements hook_element_info().
 */
function commerce_pos_order_element_info() {
  $items = array();

  $items['commerce_pos_order'] = array(
    '#input' => TRUE,
    '#process' => array('commerce_pos_order_element_process'),
    '#element_validate' => array('commerce_pos_order_element_element_validate'),
    '#theme_wrappers' => array('commerce_pos_order_element_wrapper'),
    '#theme' => array('commerce_pos_order_element'),
  );

  return $items;
}

/**
 * Process callback for commerce_pos_order form element.
 */
function commerce_pos_order_element_process($element, &$form_state, $complete_form) {
  drupal_add_library('system', 'ui.button');
  drupal_add_js(drupal_get_path('module', 'commerce_pos_order') . '/commerce_pos_order.js', array('weight' => 20));

  $element['#tree'] = TRUE;
  $order = $element['#order'];
  $element['order_id'] = array(
    '#type' => 'value',
    '#value' => isset($order->order_id) ? $order->order_id : NULL,
  );

  $element['order_status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#options' => commerce_pos_order_status_options(),
    '#default_value' => $order->status,
    '#attributes' => array('class' => array('commerce-pos-order-status')),
    '#weight' => 20,
  );

  return $element;
}

/**
 * Template preprocessor for POS order element.
 */
function template_preprocess_commerce_pos_order_element(&$variables) {
  $element = $variables['commerce_pos_order'];
  $order = $element['#order'];

  if (is_object($order)) {
    $element['content'] = entity_get_controller('commerce_order')->view(array($order->order_id => $order), 'pos screen');
    $variables['commerce_pos_order'] = $element;
  }
}

/**
 * Theme function for POS order element.
 */
function theme_commerce_pos_order_element($variables) {
  return drupal_render($variables['commerce_pos_order']['content']) . drupal_render_children($variables['commerce_pos_order']);
}

/**
 * Validate callback for commerce_pos_order form element
 */
function commerce_pos_order_element_element_validate($element, &$form_state) {
  $order_id = $element['#order']->order_id;
  if (!commerce_order_load($order_id)) {
    form_error($element, t('Attempted to modify an invalid order'));
  }
}

/**
 * Implements hook_commerce_pos_action_info().
 */
function commerce_pos_order_commerce_pos_action_info() {
  return array(
    'commerce_pos_order_load' => array(
      'callbacks' => array(
        'parse' => 'commerce_pos_order_load_parse',
        'access' => 'commerce_pos_order_access',
        'validate' => 'commerce_pos_order_load_validate',
        'execute' => 'commerce_pos_order_load_execute',
        'extract' => 'commerce_pos_order_load_extract',
      ),
      'action text' => 'OR',
      /*'button' => array(
        'title' => t('Load order'),
        'section' => 'order',
      ),*/
      'weight' => 99,
    ),
		'commerce_pos_order_set_status' => array(
      'callbacks' => array(
        'access' => 'commerce_pos_order_access',
        'validate' => 'commerce_pos_order_set_status_validate',
        'execute' => 'commerce_pos_order_set_status_execute',
        'extract' => 'commerce_pos_order_set_status_extract',
      ),
      'weight' => 99,
    ),
		'commerce_pos_order_print_receipt' => array(
      'callbacks' => array(
        'access' => 'commerce_pos_order_access',
        'parse' => 'commerce_pos_order_print_receipt_parse',
    		'validate' => 'commerce_pos_order_action_print_receipt_validate',
        'execute' => 'commerce_pos_order_print_receipt_execute',
      ),
      'action text' => 'PR',
      'button' => array(
        'title' => t('Receipt'),
        'section' => 'order',
        'weight' => 75,
      ),
      'weight' => 80,
    ),
  );
}

/**
 * Parse callback for receipt printing.
 */
function commerce_pos_order_print_receipt_parse($input, $action_text) {
  return $input == $action_text;
}


/**
 * Validate callback for printing a receipt.
 */
function commerce_pos_order_action_print_receipt_validate($variables) {
  if (empty($variables['order'])) {
    return array(t('You must have an order loaded to print a receipt'));
  }
  else {
    $status = commerce_order_status_load($variables['order']->status);
    if ($status['state'] != 'completed') {
      return array(t('You can only print a receipt for a completed order.'));
    }
  }
}

/**
 * Execute callback for receipt printing.
 */
function commerce_pos_order_print_receipt_execute(&$variables) {
  $variables['order']->data['print_receipt'] = TRUE;
}

/**
 * Extract callback for setting the order status.
 */
function commerce_pos_order_set_status_extract(&$variables, $values) {
  $variables['order_status'] = $values['order']['order_status'];
}

/**
 * Validate callback for setting order status.
 */
function commerce_pos_order_set_status_validate(&$variables) {
  //Allow administrators to configure this by rules.
  $order = $variables['order'];
  $order->status = $variables['order_status'];
  rules_invoke_event('commerce_pos_order_set_status', $order);

  if (!empty($order->error)) {
    return array($order->error);
  }
  $variables['order'] = $order;
}

/**
 * Validation callback to be used by actions that need to modify an order.
 */
function commerce_pos_order_require_modifiable_order(&$variables) {
  //TODO: if rules won't do this reliably, it may be necessary to replace this with hard-coded validation
  if (!empty($variables['order'])) {
    $order = $variables['order'];
    rules_invoke_event('commerce_pos_modify_order_validate', $order);
  }
  if (!empty($order->error)) {
    $error = $order->error;
    unset($order->error);
    return array($error);
  }
}

/**
 * Execute callback for set order status command.
 */
function commerce_pos_order_set_status_execute(&$variables) {
  commerce_order_save($variables['order']);
}

/**
 * Access callback for commerce_pos_order load.
 */
function commerce_pos_order_access($variables) {
  $order = $variables['order'];
  if (empty($order->order_id)) {
    return commerce_order_access('create');
  }
  else {
    return commerce_order_access('view', $order);
  }
}

/**
 * Parse callback for order load action.
 */
function commerce_pos_order_load_parse($input, $action_text = 'OR') {
  return commerce_pos_string_ends_with($action_text, $input);
}

/**
 * Extract callback for order load action.
 */
function commerce_pos_order_load_extract(&$variables, $values) {
  $input = $variables['input'];
  if (!empty($values['action_name']) || !empty($values['new_action_name'])) {
    $order_number = $input;
  }
  else {
    $action_text = $variables['action']['action text'];
    $order_number = drupal_substr($input, 0, drupal_strlen($input) - drupal_strlen($action_text));
  }
  $variables['order_number'] = $order_number;
}

/**
 * Validate callback for order load action.
 */
function commerce_pos_order_load_validate(&$variables) {
  if(!empty($variables['order'])) {
    return array(t('Please clear the current order before loading another.'));
  }
  else {
    $order_number = $variables['order_number'];
    $order = commerce_order_load_by_number($order_number);

    if ($order && $order->type == 'commerce_order') {
      $variables['order_id'] = $order->order_id;
      $variables['order'] = $order;
    }
    else {
      return array(t('The order could not be loaded.'));
    }
  }
}


/**
 * Implements hook_commerce_pos_build().
 */
function commerce_pos_order_commerce_pos_build($variables = array()) {
  $return_val = array();
  if (!empty($variables['order'])) {
    $order = $variables['order'];
//    if (commerce_pos_order_is_pos_order($order)) {
//      commerce_pos_order_refresh($order);
//    }
    $return_val['order'] = array(
      '#type' => 'commerce_pos_order',
      '#title' => t('Order'),
      '#order' => $order,
    );

    if (!empty($order->data['print_receipt']) && $order->data['print_receipt'] == REQUEST_TIME) {
      $header = commerce_pos_order_receipt_wrapper('header');
      $footer = commerce_pos_order_receipt_wrapper('footer');
      $return_val['receipt'] = array(
        '#type' => 'commerce_pos_receipt',
        '#time' => format_date($order->changed),
        '#header' => check_markup($header['value'], $header['format']),
        '#footer' => check_markup($footer['value'], $footer['format']),
        'content' => entity_get_controller('commerce_order')->view(array($order->order_id => $order), 'pos receipt'),
      );
  }
  elseif (!empty($order->data['print_empty_receipt']) && $order->data['print_empty_receipt'] == REQUEST_TIME) {
    $return_val['empty_receipt'] = array(
      '#type' => 'commerce_pos_receipt',
      '#unstyled' => TRUE,
      'content' => array('#markup' => t('No content')),
    );
   }
   //Give modules a hook to update customer displays.
   module_invoke_all('commerce_pos_order_display', $variables['order']);
  }
  return $return_val;
}

/**
 * Make sure that the pos screen and pos receipt view modes will show the order total.
 *
 * @param $type
 *  The order bundle to configure.
 */
function commerce_pos_order_configure_order_type($type = 'commerce_order') {
  //Update the order total field.
  $instance = field_info_instance('commerce_order', 'commerce_order_total', $type);
  if (!empty($instance)) {
    $display = array(
      'type' => 'commerce_price_formatted_components',
      'settings' => array(
        'calculation' => FALSE,
    ),
    );
    $instance['display']['pos screen'] = $display;
    $instance['display']['pos receipt'] = $display;
    field_update_instance($instance);
  }
}

/**
 * Implements hook_enable().
 */
function commerce_pos_order_enable() {
  commerce_pos_order_configure_order_type();
}

/**
 * Implements hook_theme().
 */
function commerce_pos_order_theme($existing, $type, $theme, $path) {
  return array(
    'commerce_pos_order_element_wrapper' => array(
      'render element' => 'commerce_pos_order',
    ),
    'commerce_pos_order_element' => array(
      'render element' => 'commerce_pos_order',
    ),
  );
}

/**
 * Theme wrapper for commerce_pos_order element
 */
function theme_commerce_pos_order_element_wrapper($variables) {
  drupal_add_css(drupal_get_path('module', 'commerce_pos_order') . '/theme/commerce_pos_order.css');
  $element = $variables['commerce_pos_order'];
  $prefix = '<div class = \'commerce-pos-order\' tabindex=\'2\' >';
  $suffix = '</div>';
  return $prefix . $element['#children'] . $suffix;
}

/**
 * Implements hook_commerce_pos_extract().
 */
function commerce_pos_order_commerce_pos_extract(&$variables, $values) {
  if(!empty($values['order']['order_id']) && $variables['action']['name'] != 'commerce_pos_clear') {
    $variables['order'] = commerce_order_load($values['order']['order_id']);
    //commerce_pos_order_refresh($variables['order']);
  }
}

/**
 * Implements hook_commerce_order_status_info().
 */
function commerce_pos_order_commerce_order_status_info() {
  $order_statuses = array();

  $order_statuses['pos_canceled'] = array(
    'name' => 'pos_canceled',
    'title' => t('Canceled (POS)'),
    'state' => 'canceled',
  	'weight' => 10,
  	'pos' => TRUE,
  	'pos title' => t('Canceled'),
  );

  $order_statuses['pos_in_progress'] = array(
    'name' => 'pos_in_progress',
    'title' => t('In Progress (POS)'),
    'state' => 'pos',
  	'pos' => TRUE,
  	'pos title' => t('In Progress'),
  );

  $order_statuses['pos_completed'] = array(
    'name' => 'pos_completed',
    'title' => t('Completed (POS)'),
    'state' => 'completed',
		'pos' => TRUE,
  	'pos title' => t('Completed'),
  );

  return $order_statuses;
}

/**
 * Implements hook_commerce_order_state_info().
 */
function commerce_pos_order_commerce_order_state_info() {
  $order_states = array();

  $order_states['pos'] = array(
    'name' => 'pos',
    'title' => t('At POS'),
    'description' => t('Orders in this state have started checkout at the POS but have not been completed.'),
    'weight' => -5,
    'default_status' => 'pos_in_progress',
  );

  return $order_states;
}

/**
 * Return an array of POS order statuses.
 */
function commerce_pos_order_statuses() {
  $statuses = &drupal_static(__FUNCTION__);
  if (empty($statuses)) {
    $statuses = commerce_order_statuses(array('pos' => TRUE));
  }
  return $statuses;
}

/**
 * Implements hook_commerce_order_status_info_alter().
 */
function commerce_pos_order_commerce_order_status_info_alter(&$order_status_info) {
  foreach ($order_status_info as $name => $status) {
    if(empty($status['pos'])) {
      $order_status_info[$name]['pos'] = FALSE;
    }
  }
}

/**
 * Return a list of options for order status selection in the POS.
 */
function commerce_pos_order_status_options() {
  $statuses = commerce_pos_order_statuses();
  $options = array();
  foreach ($statuses as $key => $status) {
    $options[$status['name']] = $status['pos title'];
  }
  return $options;
 }

 /**
  * Returns a new empty order
  */
 function commerce_pos_order_new($uid = 0) {
   return commerce_order_new($uid, 'pos_in_progress', 'commerce_order');
 }

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

/**
 * Determines whether or not the given order is at the POS.
 */
function commerce_pos_order_is_pos_order($order) {
  // If the order is in a pos order status, assume it is at the POS.
  $status = commerce_order_status_load($order->status);
  $is_pos_order = ($status['state'] == 'pos');

  // Allow other modules to make the judgment based on some other criteria.
  if (in_array(FALSE, module_invoke_all('commerce_pos_order_is_pos_order', $order, $is_pos_order))) {
    $is_pos_order = FALSE;
  }

  return $is_pos_order;
}



/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_pos_order_form_commerce_pos_settings_form_alter(&$form, &$form_state) {
  $form['order'] = array(
    '#type' => 'fieldset',
    '#title' => t('Order settings'),
  );
  $header = commerce_pos_order_receipt_wrapper('header');
  $form['order']['commerce_pos_order_receipt_header'] = array(
    '#type' => 'text_format',
    '#title' => t('Receipt header'),
    '#default_value' => $header['value'],
    '#format' => $header['format'],
  );
  $footer = commerce_pos_order_receipt_wrapper('footer');
  $form['order']['commerce_pos_order_receipt_footer'] = array(
    '#type' => 'text_format',
    '#title' => t('Receipt footer'),
    '#default_value' => $footer['value'],
    '#format' => $footer['format'],
  );
}

/**
 * Function to get the receipt header or footer.
 * 	@param $part
 * 		'header' or 'footer'
 */
function commerce_pos_order_receipt_wrapper($part) {
  return variable_get('commerce_pos_order_receipt_' . $part, array());
}

/**
 * Implements hook_commerce_pos_button_section_info().
 */
function commerce_pos_order_commerce_pos_button_section_info() {
  return array('order' => array('#weight' => -70));
}

/**
 * Implements hook_commerce_pos_preload().
 */
function commerce_pos_order_commerce_pos_preload() {
  drupal_add_css(drupal_get_path('module', 'commerce_pos_order') . '/theme/commerce_pos_order.css');
  drupal_add_js(drupal_get_path('module', 'commerce_pos_order') . '/commerce_pos_order.js', array('weight' => 20));
}