summaryrefslogtreecommitdiffstats
path: root/modules/open_ring/commerce_pos_open_ring.module
blob: 74124282de5cbe8aa96bb211cc753d56035b3ffb (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
<?php

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


/**
 * Implements hook_commerce_product_type_info().
 */
function commerce_pos_open_ring_commerce_product_type_info() {
  return array(
    'pos_open_ring' => array(
      'type' => 'pos_open_ring',
      'name' => t('Open Ring'),
      'description' => t('This allows products to be sold for a price specified at the register.'),
    ),
  );
}

/**
 * Disallow editing the price of an open ring product.
 */
function commerce_pos_open_ring_field_access($op, $field, $entity_type, $entity, $account) {
  if($op == 'edit' && $entity_type == 'commerce_product' && $entity->type == 'pos_open_ring' && $field['field_name'] == 'commerce_price') {
    return FALSE;
  }
}

/**
 * Implements hook_commerce_pos_action_info()
 */
function commerce_pos_open_ring_commerce_pos_action_info() {
  return array(
    'commerce_pos_open_ring' => array(
      'weight' => 99,
      'callbacks' => array(
        'parse' => 'commerce_pos_open_ring_parse',
  			'extract' => 'commerce_pos_open_ring_extract',
        'access' => 'commerce_pos_product_access',
        'validate' => 'commerce_pos_open_ring_validate',
        'execute' => 'commerce_pos_product_execute',
      ),
    ),
  );
}

/**
 * Parse callback for the open ring action.
 */
function commerce_pos_open_ring_parse($input, $action_text) {
  $args = explode('*', $input);
  $sku = array_pop($args);
  //It seems not great to be loading the product here, so think reworking the workflow
  //to avoid this. However, it's not the worst thing since the product will be cached.
  $product = commerce_product_load_by_sku($sku);

  if ($product->type == 'pos_open_ring') {
    return TRUE;
  }
}

/**
 * Extract callback for the open ring action.
 */
function commerce_pos_open_ring_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();
  }

  $args = explode('*', $variables['input']);
  $sku = array_pop($args);
  $price_decimal = array_pop($args);
  $quantity = array_pop($args);
  if (empty($quantity)) {
    $quantity = 1;
  }

  $variables += array('sku' => $sku, 'price_decimal' => $price_decimal, 'quantity' => $quantity);
}

/**
 * Validator for product action.
 */
function commerce_pos_open_ring_validate(&$variables) {
  $errors = array();

  //TODO: we might want a different product code field instead of sku for this
  $product = commerce_product_load_by_sku($variables['sku']);
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

  //Determine the price.
  $currency_code = commerce_pos_default_currency();
  $amount = commerce_currency_decimal_to_amount($variables['price_decimal'], $currency_code, TRUE);
  $product->commerce_price[LANGUAGE_NONE][0] = array(
    'amount' => $amount,
    'currency_code' => $currency_code,
  );

  $order = $variables['order'];

  $line_item = commerce_product_line_item_new($product, $variables['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;
}

/**
 * Implements hook_commerce_pos_search_info_alter().
 */
function commerce_pos_open_ring_commerce_pos_search_info_alter(&$search_info) {
  //Add CSS and JS 
  $path = drupal_get_path('module', 'commerce_pos_open_ring');
  $search_info['product']['attached']['css'][] = array(
    'data' => $path . '/theme/commerce_pos_open_ring.css',
  );
  $search_info['product']['attached']['js'][] = array(
    'data' => $path . '/commerce_pos_open_ring.js',
  );
}