Projet

Général

Profil

Paste
Télécharger (11,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / line_item / commerce_line_item.rules.inc @ 9d13637e

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules integration for line items.
6
 *
7
 * @addtogroup rules
8
 * @{
9
 */
10

    
11
/**
12
 * Rules integration access callback.
13
 */
14
function commerce_line_item_rules_access($type, $name) {
15
  if ($type == 'event' || $type == 'condition') {
16
    return TRUE;
17
  }
18
}
19

    
20
/**
21
 * Implements hook_rules_action_info().
22
 */
23
function commerce_line_item_rules_action_info() {
24
  $actions = array();
25

    
26
  // Prepare an array of arithmetical actions for altering prices.
27
  $action_types = array(
28
    'commerce_line_item_unit_price_add' => array(
29
      'label' => t('Add an amount to the unit price'),
30
      'amount description' => t('Specify a numeric amount to add to the unit price.'),
31
    ),
32
    'commerce_line_item_unit_price_subtract' => array(
33
      'label' => t('Subtract an amount from the unit price'),
34
      'amount description' => t('Specify a numeric amount to subtract from the price.'),
35
    ),
36
    'commerce_line_item_unit_price_multiply' => array(
37
      'label' => t('Multiply the unit price by some amount'),
38
      'amount description' => t('Specify the numeric amount by which to multiply the price.'),
39
    ),
40
    'commerce_line_item_unit_price_divide' => array(
41
      'label' => t('Divide the unit price by some amount'),
42
      'amount description' => t('Specify a numeric amount by which to divide the price.'),
43
    ),
44
    'commerce_line_item_unit_price_amount' => array(
45
      'label' => t('Set the unit price to a specific amount'),
46
      'amount description' => t('Specify the numeric amount to set the price to.'),
47
    ),
48
  );
49

    
50
  // Define the action using a common set of parameters.
51
  foreach ($action_types as $key => $value) {
52
    $actions[$key] = array(
53
      'label' => $value['label'],
54
      'parameter' => array(
55
        'commerce_line_item' => array(
56
          'type' => 'commerce_line_item',
57
          'label' => t('Line item'),
58
        ),
59
        'amount' => array(
60
          'type' => 'decimal',
61
          'label' => t('Amount'),
62
          'description' => $value['amount description'],
63
        ),
64
        'component_name' => array(
65
          'type' => 'text',
66
          'label' => t('Price component type'),
67
          'description' => t('Price components track changes to prices made during the price calculation process, and they are carried over from the unit price to the total price of a line item. When an order total is calculated, it combines all the components of every line item on the order. When the unit price is altered by this action, the selected type of price component will be added to its data array and reflected in the order total display when it is formatted with components showing. Defaults to base price, which displays as the order Subtotal.'),
68
          'options list' => 'commerce_line_item_price_component_options_list',
69
          'default value' => 'base_price',
70
        ),
71
        'round_mode' => array(
72
          'type' => 'integer',
73
          'label' => t('Price rounding mode'),
74
          'description' => t('Round the resulting price amount after performing this operation.'),
75
          'options list' => 'commerce_round_mode_options_list',
76
          'default value' => COMMERCE_ROUND_HALF_UP,
77
        ),
78
      ),
79
      'group' => t('Commerce Line Item'),
80
    );
81
  }
82

    
83
  $actions['commerce_line_item_unit_price_currency_code'] = array(
84
    'label' => t("Set the unit price's currency code"),
85
    'parameter' => array(
86
      'commerce_line_item' => array(
87
        'type' => 'commerce_line_item',
88
        'label' => t('Line item'),
89
      ),
90
      'currency_code' => array(
91
        'type' => 'text',
92
        'label' => t('Currency'),
93
        'options list' => 'commerce_currency_get_code',
94
      ),
95
    ),
96
    'group' => t('Commerce Line Item'),
97
  );
98

    
99
  $actions['commerce_line_item_unit_price_currency_convert'] = array(
100
    'label' => t("Convert the unit price to a different currency"),
101
    'parameter' => array(
102
      'commerce_line_item' => array(
103
        'type' => 'commerce_line_item',
104
        'label' => t('Line item'),
105
      ),
106
      'currency_code' => array(
107
        'type' => 'text',
108
        'label' => t('Currency'),
109
        'options list' => 'commerce_currency_get_code',
110
      ),
111
    ),
112
    'group' => t('Commerce Line Item'),
113
  );
114

    
115
  return $actions;
116
}
117

    
118
/**
119
 * Options list callback: price component selection list.
120
 */
121
function commerce_line_item_price_component_options_list() {
122
  return commerce_price_component_titles();
123
}
124

    
125
/**
126
 * Rules action: add an amount to the unit price.
127
 */
128
function commerce_line_item_unit_price_add($line_item, $amount, $component_name, $round_mode) {
129
  if (is_numeric($amount)) {
130
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
131
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
132

    
133
    // Calculate the updated amount and create a price array representing the
134
    // difference between it and the current amount.
135
    $current_amount = $unit_price['amount'];
136
    $updated_amount = commerce_round($round_mode, $current_amount + $amount);
137

    
138
    $difference = array(
139
      'amount' => $updated_amount - $current_amount,
140
      'currency_code' => $unit_price['currency_code'],
141
      'data' => array(),
142
    );
143

    
144
    // Set the amount of the unit price and add the difference as a component.
145
    $wrapper->commerce_unit_price->amount = $updated_amount;
146

    
147
    $wrapper->commerce_unit_price->data = commerce_price_component_add(
148
      $wrapper->commerce_unit_price->value(),
149
      $component_name,
150
      $difference,
151
      TRUE
152
    );
153
  }
154
}
155

    
156
/**
157
 * Rules action: subtract an amount from the unit price.
158
 */
159
function commerce_line_item_unit_price_subtract($line_item, $amount, $component_name, $round_mode) {
160
  if (is_numeric($amount)) {
161
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
162
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
163

    
164
    // Calculate the updated amount and create a price array representing the
165
    // difference between it and the current amount.
166
    $current_amount = $unit_price['amount'];
167
    $updated_amount = commerce_round($round_mode, $current_amount - $amount);
168

    
169
    $difference = array(
170
      'amount' => $updated_amount - $current_amount,
171
      'currency_code' => $unit_price['currency_code'],
172
      'data' => array(),
173
    );
174

    
175
    // Set the amount of the unit price and add the difference as a component.
176
    $wrapper->commerce_unit_price->amount = $updated_amount;
177

    
178
    $wrapper->commerce_unit_price->data = commerce_price_component_add(
179
      $wrapper->commerce_unit_price->value(),
180
      $component_name,
181
      $difference,
182
      TRUE
183
    );
184
  }
185
}
186

    
187
/**
188
 * Rules action: multiply the unit price by some amount.
189
 */
190
function commerce_line_item_unit_price_multiply($line_item, $amount, $component_name, $round_mode) {
191
  if (is_numeric($amount)) {
192
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
193
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
194

    
195
    // Calculate the updated amount and create a price array representing the
196
    // difference between it and the current amount.
197
    $current_amount = $unit_price['amount'];
198
    $updated_amount = commerce_round($round_mode, $current_amount * $amount);
199

    
200
    $difference = array(
201
      'amount' => $updated_amount - $current_amount,
202
      'currency_code' => $unit_price['currency_code'],
203
      'data' => array(),
204
    );
205

    
206
    // Set the amount of the unit price and add the difference as a component.
207
    $wrapper->commerce_unit_price->amount = $updated_amount;
208

    
209
    $wrapper->commerce_unit_price->data = commerce_price_component_add(
210
      $wrapper->commerce_unit_price->value(),
211
      $component_name,
212
      $difference,
213
      TRUE
214
    );
215
  }
216
}
217

    
218
/**
219
 * Rules action: divide the unit price by some amount.
220
 */
221
function commerce_line_item_unit_price_divide($line_item, $amount, $component_name, $round_mode) {
222
  if (is_numeric($amount)) {
223
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
224
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
225

    
226
    // Calculate the updated amount and create a price array representing the
227
    // difference between it and the current amount.
228
    $current_amount = $unit_price['amount'];
229
    $updated_amount = commerce_round($round_mode, $current_amount / $amount);
230

    
231
    $difference = array(
232
      'amount' => $updated_amount - $current_amount,
233
      'currency_code' => $unit_price['currency_code'],
234
      'data' => array(),
235
    );
236

    
237
    // Set the amount of the unit price and add the difference as a component.
238
    $wrapper->commerce_unit_price->amount = $updated_amount;
239

    
240
    $wrapper->commerce_unit_price->data = commerce_price_component_add(
241
      $wrapper->commerce_unit_price->value(),
242
      $component_name,
243
      $difference,
244
      TRUE
245
    );
246
  }
247
}
248

    
249
/**
250
 * Rules action: set the unit price to a specific amount.
251
 */
252
function commerce_line_item_unit_price_amount($line_item, $amount, $component_name, $round_mode) {
253
  if (is_numeric($amount)) {
254
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
255
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
256

    
257
    // Calculate the updated amount and create a price array representing the
258
    // difference between it and the current amount.
259
    $current_amount = $unit_price['amount'];
260
    $updated_amount = commerce_round($round_mode, $amount);
261

    
262
    $difference = array(
263
      'amount' => $updated_amount - $current_amount,
264
      'currency_code' => $unit_price['currency_code'],
265
      'data' => array(),
266
    );
267

    
268
    // Set the amount of the unit price and add the difference as a component.
269
    $wrapper->commerce_unit_price->amount = $updated_amount;
270

    
271
    $wrapper->commerce_unit_price->data = commerce_price_component_add(
272
      $wrapper->commerce_unit_price->value(),
273
      $component_name,
274
      $difference,
275
      TRUE
276
    );
277
  }
278
}
279

    
280
/**
281
 * Rules action: set the unit price's currency code.
282
 */
283
function commerce_line_item_unit_price_currency_code($line_item, $currency_code) {
284
  $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
285
  $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price');
286

    
287
  // Only set the currency on prices with non-NULL amounts.
288
  if (empty($unit_price)) {
289
    return;
290
  }
291

    
292
  $wrapper->commerce_unit_price->currency_code = $currency_code;
293

    
294
  // Update the currency code of the price's components.
295
  if (!empty($unit_price['data']['components'])) {
296
    foreach ($unit_price['data']['components'] as $key => &$component) {
297
      $component['price']['currency_code'] = $currency_code;
298
    }
299

    
300
    $wrapper->commerce_unit_price->data = $unit_price['data'];
301
  }
302
}
303

    
304
/**
305
 * Rules action: convert the unit price to a different currency.
306
 */
307
function commerce_line_item_unit_price_currency_convert($line_item, $currency_code) {
308
  $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
309
  $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price');
310

    
311
  // Only convert non-NULL amounts since NULL amounts do not have a currency.
312
  if (empty($unit_price)) {
313
    return;
314
  }
315

    
316
  $wrapper->commerce_unit_price->amount = commerce_currency_convert($wrapper->commerce_unit_price->amount->value(), $wrapper->commerce_unit_price->currency_code->value(), $currency_code);
317
  $wrapper->commerce_unit_price->currency_code = $currency_code;
318

    
319
  // Convert the currency code of the price's components.
320
  if (!empty($unit_price['data']['components'])) {
321
    foreach ($unit_price['data']['components'] as $key => &$component) {
322
      $component['price']['amount'] = commerce_currency_convert($component['price']['amount'], $component['price']['currency_code'], $currency_code);
323
      $component['price']['currency_code'] = $currency_code;
324
    }
325

    
326
    $wrapper->commerce_unit_price->data = $unit_price['data'];
327
  }
328
}
329

    
330
/**
331
 * @}
332
 */