Projet

Général

Profil

Paste
Télécharger (16 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / tax / commerce_tax_ui.module @ 70a4c29b

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides a UI for creating simple tax types and rates.
6
 */
7

    
8

    
9
/**
10
 * Implements hook_menu().
11
 */
12
function commerce_tax_ui_menu() {
13
  $items = array();
14

    
15
  $items['admin/commerce/config/taxes'] = array(
16
    'title' => 'Taxes',
17
    'description' => 'Manage tax rates and types.',
18
    'page callback' => 'commerce_tax_ui_overview',
19
    'page arguments' => array('rates'),
20
    'access arguments' => array('administer taxes'),
21
    'file' => 'includes/commerce_tax_ui.admin.inc',
22
  );
23

    
24
  $items['admin/commerce/config/taxes/rates'] = array(
25
    'title' => 'Tax rates',
26
    'description' => 'Manage tax rates.',
27
    'weight' => 0,
28
    'type' => MENU_DEFAULT_LOCAL_TASK,
29
  );
30
  $items['admin/commerce/config/taxes/rates/add'] = array(
31
    'title' => 'Add a tax rate',
32
    'description' => 'Create a new tax rate.',
33
    'page callback' => 'commerce_tax_ui_tax_rate_add_form_wrapper',
34
    'page arguments' => array(commerce_tax_ui_tax_rate_new()),
35
    'access arguments' => array('administer taxes'),
36
    'type' => MENU_LOCAL_ACTION,
37
    'file' => 'includes/commerce_tax_ui.admin.inc',
38
  );
39
  foreach (commerce_tax_rates() as $name => $tax_rate) {
40
    // Convert underscores to hyphens for the menu item argument.
41
    $name_arg = strtr($name, '_', '-');
42

    
43
    if ($tax_rate['module'] == 'commerce_tax_ui') {
44
      $items['admin/commerce/config/taxes/rates/' . $name_arg] = array(
45
        'title callback' => 'commerce_tax_ui_tax_rate_title',
46
        'title arguments' => array($name),
47
        'description' => 'Edit a tax rate.',
48
        'page callback' => 'drupal_get_form',
49
        'page arguments' => array('commerce_tax_ui_tax_rate_form', $tax_rate),
50
        'access arguments' => array('administer taxes'),
51
        'file' => 'includes/commerce_tax_ui.admin.inc',
52
      );
53
      $items['admin/commerce/config/taxes/rates/' . $name_arg . '/edit'] = array(
54
        'title' => 'Edit',
55
        'type' => MENU_DEFAULT_LOCAL_TASK,
56
        'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
57
        'weight' => 0,
58
      );
59
      $items['admin/commerce/config/taxes/rates/' . $name_arg . '/delete'] = array(
60
        'title' => 'Delete',
61
        'page arguments' => array('commerce_tax_ui_tax_rate_delete_form', $tax_rate),
62
        'access arguments' => array('administer taxes'),
63
        'file' => 'includes/commerce_tax_ui.admin.inc',
64
        'type' => MENU_LOCAL_TASK,
65
        'context' => MENU_CONTEXT_INLINE,
66
        'weight' => 10,
67
      );
68
    }
69
    else {
70
      $items['admin/commerce/config/taxes/rates/' . $name_arg] = array(
71
        'title callback' => 'commerce_tax_ui_tax_rate_title',
72
        'title arguments' => array($name),
73
        'description' => 'Redirect to the tax rate list.',
74
        'page callback' => 'drupal_goto',
75
        'page arguments' => array('admin/commerce/config/taxes/rates'),
76
        'access arguments' => array('administer taxes'),
77
      );
78
    }
79

    
80
    if (rules_config_load($tax_rate['rules_component'])) {
81
      $items['admin/commerce/config/taxes/rates/' . $name_arg . '/component'] = array(
82
        'title' => 'Configure component',
83
        'description' => 'Add conditions to the Rules component used to apply this tax to products.',
84
        'page callback' => 'drupal_goto',
85
        'page arguments' => array('admin/config/workflow/rules/components/manage/' . $tax_rate['rules_component']),
86
        'access arguments' => array('administer rules'),
87
        'type' => MENU_LOCAL_TASK,
88
        'context' => MENU_CONTEXT_INLINE,
89
        'weight' => 5,
90
      );
91
    }
92
  }
93

    
94
  $items['admin/commerce/config/taxes/types'] = array(
95
    'title' => 'Tax types',
96
    'description' => 'Manage tax types.',
97
    'page callback' => 'commerce_tax_ui_overview',
98
    'page arguments' => array('types'),
99
    'access arguments' => array('administer taxes'),
100
    'weight' => 5,
101
    'type' => MENU_LOCAL_TASK,
102
    'file' => 'includes/commerce_tax_ui.admin.inc',
103
  );
104
  $items['admin/commerce/config/taxes/types/add'] = array(
105
    'title' => 'Add a tax type',
106
    'description' => 'Create a new tax type.',
107
    'page callback' => 'drupal_get_form',
108
    'page arguments' => array('commerce_tax_ui_tax_type_form', commerce_tax_ui_tax_type_new()),
109
    'access arguments' => array('administer taxes'),
110
    'type' => MENU_LOCAL_ACTION,
111
    'file' => 'includes/commerce_tax_ui.admin.inc',
112
  );
113
  foreach (commerce_tax_types() as $name => $tax_type) {
114
    // Convert underscores to hyphens for the menu item argument.
115
    $name_arg = strtr($name, '_', '-');
116

    
117
    if ($tax_type['module'] == 'commerce_tax_ui') {
118
      $items['admin/commerce/config/taxes/types/' . $name_arg] = array(
119
        'title callback' => 'commerce_tax_ui_tax_type_title',
120
        'title arguments' => array($name),
121
        'description' => 'Edit a tax type.',
122
        'page callback' => 'drupal_get_form',
123
        'page arguments' => array('commerce_tax_ui_tax_type_form', $tax_type),
124
        'access arguments' => array('administer taxes'),
125
        'file' => 'includes/commerce_tax_ui.admin.inc',
126
      );
127
      $items['admin/commerce/config/taxes/types/' . $name_arg . '/edit'] = array(
128
        'title' => 'Edit',
129
        'type' => MENU_DEFAULT_LOCAL_TASK,
130
        'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
131
        'weight' => 0,
132
      );
133
      $items['admin/commerce/config/taxes/types/' . $name_arg . '/delete'] = array(
134
        'title' => 'Delete',
135
        'page callback' => 'commerce_tax_ui_tax_type_delete_form_wrapper',
136
        'page arguments' => array($tax_type),
137
        'access arguments' => array('administer taxes'),
138
        'file' => 'includes/commerce_tax_ui.admin.inc',
139
        'type' => MENU_LOCAL_TASK,
140
        'context' => MENU_CONTEXT_INLINE,
141
        'weight' => 10,
142
      );
143
    }
144
    else {
145
      $items['admin/commerce/config/taxes/types/' . $name_arg] = array(
146
        'title callback' => 'commerce_tax_ui_tax_type_title',
147
        'title arguments' => array($name),
148
        'description' => 'Redirect to the tax type list.',
149
        'page callback' => 'drupal_goto',
150
        'page arguments' => array('admin/commerce/config/taxes/types'),
151
        'access arguments' => array('administer taxes'),
152
      );
153
    }
154

    
155
    if (rules_config_load($tax_type['rule'])) {
156
      $items['admin/commerce/config/taxes/types/' . $name_arg . '/rule'] = array(
157
        'title' => 'Configure rule',
158
        'description' => 'Add conditions to the rule used to apply taxes of this type to products.',
159
        'page callback' => 'drupal_goto',
160
        'page arguments' => array('admin/config/workflow/rules/reaction/manage/' . $tax_type['rule']),
161
        'access arguments' => array('administer rules'),
162
        'type' => MENU_LOCAL_TASK,
163
        'context' => MENU_CONTEXT_INLINE,
164
        'weight' => 5,
165
      );
166
    }
167
  }
168

    
169
  return $items;
170
}
171

    
172
/**
173
 * Title callback: return the title of a tax rate.
174
 */
175
function commerce_tax_ui_tax_rate_title($name) {
176
  $titles = commerce_tax_rate_titles();
177
  return !empty($titles[$name]) ? $titles[$name] : '';
178
}
179

    
180
/**
181
 * Title callback: return the title of a tax type.
182
 */
183
function commerce_tax_ui_tax_type_title($name) {
184
  $titles = commerce_tax_type_titles();
185
  return !empty($titles[$name]) ? $titles[$name] : '';
186
}
187

    
188
/**
189
 * Implements hook_permission().
190
 */
191
function commerce_tax_ui_permission() {
192
  return array(
193
    'administer taxes' => array(
194
      'title' => t('Administer taxes'),
195
      'description' => t('Manage the UI defined tax rates and types.'),
196
      'restrict access' => TRUE,
197
    ),
198
  );
199
}
200

    
201
/**
202
 * Implements hook_theme().
203
 */
204
function commerce_tax_ui_theme() {
205
  return array(
206
    'tax_rate_admin_overview' => array(
207
      'variables' => array('tax_rate' => NULL),
208
      'file' => 'includes/commerce_tax_ui.admin.inc',
209
    ),
210
    'tax_type_admin_overview' => array(
211
      'variables' => array('tax_type' => NULL),
212
      'file' => 'includes/commerce_tax_ui.admin.inc',
213
    ),
214
  );
215
}
216

    
217
/**
218
 * Implements hook_help().
219
 */
220
function commerce_tax_ui_help($path, $arg) {
221
  switch ($path) {
222
    case 'admin/commerce/config/taxes':
223
    case 'admin/commerce/config/taxes/rates':
224
      return '<p>' . t('Define a tax rate for each tax you must collect from your customers. Tax rates are defined in decimal format and may be applied to line item via Rules. Default Rules components are defined for any tax rate specifying it and can be accessed using the appropriate operations link below.') . '</p>';
225

    
226
    case 'admin/commerce/config/taxes/types':
227
      return '<p>' . t('Tax types categorize tax rates and specify whether or not the calculated tax should be included in product prices on display. Default rules are defined for each tax type for applying its tax rates to line items. These can be accessed using the appropriate operations link below.') . '</p>';
228
  }
229
}
230

    
231
/**
232
 * Implements hook_commerce_tax_type_info().
233
 */
234
function commerce_tax_ui_commerce_tax_type_info() {
235
  return db_query('SELECT * FROM {commerce_tax_type}')->fetchAllAssoc('name', PDO::FETCH_ASSOC);
236
}
237

    
238
/**
239
 * Implements hook_commerce_tax_type_info_alter().
240
 */
241
function commerce_tax_ui_commerce_tax_type_info_alter(&$tax_types) {
242
  // Default all commerce_tax_ui defined types to appear in the admin list.
243
  foreach ($tax_types as $name => &$tax_type) {
244
    $tax_type += array('admin_list' => ($tax_type['module'] == 'commerce_tax_ui'));
245
  }
246
}
247

    
248
/**
249
 * Returns an initialized tax type array.
250
 */
251
function commerce_tax_ui_tax_type_new() {
252
  return array(
253
    'name' => '',
254
    'title' => '',
255
    'display_title' => '',
256
    'description' => '',
257
    'display_inclusive' => FALSE,
258
    'round_mode' => COMMERCE_ROUND_NONE,
259
    'admin_list' => TRUE,
260
    'module' => 'commerce_tax_ui',
261
    'is_new' => TRUE,
262
  );
263
}
264

    
265
/**
266
 * Saves a tax type.
267
 *
268
 * This function will either insert a new tax type if $tax_type['is_new'] is set
269
 * or attempt to update an existing tax type if it is not. It does not currently
270
 * support changing the machine-name of the tax type, nor is this possible
271
 * through the form supplied by the Tax UI module.
272
 *
273
 * @param $tax_type
274
 *   The tax type array containing the basic properties as initialized in
275
 *   commerce_tax_ui_tax_type_new().
276
 * @param $skip_reset
277
 *   Boolean indicating whether or not this save should result in entities
278
 *   being reset and the menu being rebuilt; defaults to FALSE. This is useful
279
 *   when you intend to perform many saves at once, as menu rebuilding is very
280
 *   costly in terms of performance.
281
 *
282
 * @return
283
 *   The return value of the call to drupal_write_record() to save the tax type,
284
 *   either FALSE on failure or SAVED_NEW or SAVED_UPDATED indicating the type
285
 *   of query performed to save the tax type.
286
 */
287
function commerce_tax_ui_tax_type_save($tax_type, $skip_reset = FALSE) {
288
  $op = drupal_write_record('commerce_tax_type', $tax_type, empty($tax_type['is_new']) ? 'name' : array());
289
  commerce_tax_types_reset();
290

    
291
  // If this is a new tax type and the insert did not fail...
292
  if (!empty($tax_type['is_new']) && $op !== FALSE) {
293
    // Notify other modules that a new tax type has been created.
294
    module_invoke_all('commerce_tax_type_insert', $tax_type, $skip_reset);
295
  }
296
  elseif ($op !== FALSE) {
297
    // Notify other modules that an existing tax type has been updated.
298
    module_invoke_all('commerce_tax_type_update', $tax_type, $skip_reset);
299
  }
300

    
301
  // Clear the necessary caches and rebuild the menu items.
302
  if (!$skip_reset) {
303
    entity_defaults_rebuild();
304
    rules_clear_cache(TRUE);
305
    variable_set('menu_rebuild_needed', TRUE);
306
  }
307

    
308
  return $op;
309
}
310

    
311
/**
312
 * Deletes a tax type.
313
 *
314
 * @param $name
315
 *   The machine-name of the tax type.
316
 * @param $skip_reset
317
 *   Boolean indicating whether or not this delete should result in entities
318
 *   being reset and the menu being rebuilt; defaults to FALSE. This is useful
319
 *   when you intend to perform many deletions at once, as menu rebuilding is
320
 *   very costly in terms of performance.
321
 */
322
function commerce_tax_ui_tax_type_delete($name, $skip_reset = FALSE) {
323
  $tax_type = commerce_tax_type_load($name);
324

    
325
  db_delete('commerce_tax_type')
326
    ->condition('name', $name)
327
    ->execute();
328
  commerce_tax_types_reset();
329

    
330
  rules_config_delete(array(rules_config_load($tax_type['rule'])->id));
331

    
332
  // Clear the necessary caches and rebuild the menu items.
333
  if (!$skip_reset) {
334
    entity_defaults_rebuild();
335
    rules_clear_cache();
336
    variable_set('menu_rebuild_needed', TRUE);
337
  }
338

    
339
  // Notify other modules that this tax type has been deleted.
340
  module_invoke_all('commerce_tax_type_delete', $tax_type, $skip_reset);
341
}
342

    
343
/**
344
 * Implements hook_commerce_tax_rate_info().
345
 */
346
function commerce_tax_ui_commerce_tax_rate_info() {
347
  return db_query('SELECT * FROM {commerce_tax_rate}')->fetchAllAssoc('name', PDO::FETCH_ASSOC);
348
}
349

    
350
/**
351
 * Implements hook_commerce_tax_rate_info_alter().
352
 */
353
function commerce_tax_ui_commerce_tax_rate_info_alter(&$tax_rates) {
354
  // Default all commerce_tax_ui defined rates to appear in the admin list.
355
  foreach ($tax_rates as $name => &$tax_rate) {
356
    $tax_rate += array('admin_list' => ($tax_rate['module'] == 'commerce_tax_ui'));
357
  }
358
}
359

    
360
/**
361
 * Returns an initialized tax rate array.
362
 *
363
 * @param $type
364
 *   The name of the tax type for the new rate.
365
 */
366
function commerce_tax_ui_tax_rate_new($type = '') {
367
  return array(
368
    'name' => '',
369
    'title' => '',
370
    'display_title' => '',
371
    'description' => '',
372
    'rate' => 0,
373
    'type' => $type,
374
    'default_rules_component' => TRUE,
375
    'tax_component' => '',
376
    'admin_list' => TRUE,
377
    'calculation_callback' => 'commerce_tax_rate_calculate',
378
    'module' => 'commerce_tax_ui',
379
    'is_new' => TRUE,
380
  );
381
}
382

    
383
/**
384
 * Saves a tax rate.
385
 *
386
 * This function will either insert a new tax rate if $tax_rate['is_new'] is set
387
 * or attempt to update an existing tax rate if it is not. It does not currently
388
 * support changing the machine-name of the tax rate, nor is this possible
389
 * through the form supplied by the Tax UI module.
390
 *
391
 * @param $tax_rate
392
 *   The tax rate array containing the basic properties as initialized in
393
 *   commerce_tax_ui_tax_rate_new().
394
 * @param $skip_reset
395
 *   Boolean indicating whether or not this save should result in entities
396
 *   being reset and the menu being rebuilt; defaults to FALSE. This is useful
397
 *   when you intend to perform many d at once, as menu rebuilding is very
398
 *   costly in terms of performance.
399
 *
400
 * @return
401
 *   The return value of the call to drupal_write_record() to save the tax rate,
402
 *   either FALSE on failure or SAVED_NEW or SAVED_UPDATED indicating the rate
403
 *   of query performed to save the tax rate.
404
 */
405
function commerce_tax_ui_tax_rate_save($tax_rate, $skip_reset = FALSE) {
406
  $op = drupal_write_record('commerce_tax_rate', $tax_rate, empty($tax_rate['is_new']) ? 'name' : array());
407
  commerce_tax_rates_reset();
408

    
409
  // If this is a new tax rate and the insert did not fail...
410
  if (!empty($tax_rate['is_new']) && $op !== FALSE) {
411
    // Notify other modules that a new tax rate has been created.
412
    module_invoke_all('commerce_tax_rate_insert', $tax_rate, $skip_reset);
413
  }
414
  elseif ($op !== FALSE) {
415
    // Notify other modules that an existing tax rate has been updated.
416
    module_invoke_all('commerce_tax_rate_update', $tax_rate, $skip_reset);
417
  }
418

    
419
  // Clear the necessary caches and rebuild the menu items.
420
  if (!$skip_reset) {
421
    entity_defaults_rebuild();
422
    rules_clear_cache(TRUE);
423
    variable_set('menu_rebuild_needed', TRUE);
424
  }
425

    
426
  return $op;
427
}
428

    
429
/**
430
 * Deletes a tax rate.
431
 *
432
 * @param $name
433
 *   The machine-name of the tax rate.
434
 * @param $skip_reset
435
 *   Boolean indicating whether or not this delete should result in entities
436
 *   being reset and the menu being rebuilt; defaults to FALSE. This is useful
437
 *   when you intend to perform many deletions at once, as menu rebuilding is
438
 *   very costly in terms of performance.
439
 */
440
function commerce_tax_ui_tax_rate_delete($name, $skip_reset = FALSE) {
441
  $tax_rate = commerce_tax_rate_load($name);
442

    
443
  db_delete('commerce_tax_rate')
444
    ->condition('name', $name)
445
    ->execute();
446
  commerce_tax_rates_reset();
447

    
448
  rules_config_delete(array(rules_config_load($tax_rate['rules_component'])->id));
449

    
450
  // Clear the necessary caches and rebuild the menu items.
451
  if (!$skip_reset) {
452
    entity_defaults_rebuild();
453
    rules_clear_cache(TRUE);
454
    variable_set('menu_rebuild_needed', TRUE);
455
  }
456

    
457
  // Notify other modules that this tax rate has been deleted.
458
  module_invoke_all('commerce_tax_rate_delete', $tax_rate, $skip_reset);
459
}