Skip to content

Commit d975c7e

Browse files
committed
Issue #2840683: Re-implement the tax module.
1 parent 5ebb309 commit d975c7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4042
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "GPL-2.0+",
77
"require": {
88
"drupal/core": "~8.3",
9-
"drupal/address": "~1.0",
9+
"drupal/address": "1.x-dev",
1010
"drupal/entity": "~1.0",
1111
"drupal/entity_reference_revisions": "~1.0",
1212
"drupal/inline_entity_form": "~1.0",

modules/order/src/Entity/Order.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ public function removeAdjustment(Adjustment $adjustment) {
295295
return $this;
296296
}
297297

298+
/**
299+
* {@inheritdoc}
300+
*/
301+
public function clearAdjustments() {
302+
foreach ($this->getItems() as $order_item) {
303+
$order_item->setAdjustments([]);
304+
}
305+
// Remove all order-level adjustments except the ones added via the UI.
306+
$adjustments = $this->getAdjustments();
307+
$adjustments = array_filter($adjustments, function ($adjustment) {
308+
/** @var \Drupal\commerce_order\Adjustment $adjustment */
309+
return $adjustment->getType() == 'custom';
310+
});
311+
$this->setAdjustments($adjustments);
312+
return $this;
313+
}
314+
298315
/**
299316
* {@inheritdoc}
300317
*/
@@ -309,6 +326,7 @@ public function collectAdjustments() {
309326
'label' => $adjustment->getLabel(),
310327
'source_id' => $adjustment->getSourceId(),
311328
'amount' => $adjustment->getAmount()->multiply($order_item->getQuantity()),
329+
'included' => $adjustment->isIncluded(),
312330
]);
313331
$adjustments[] = $multiplied_adjustment;
314332
}

modules/order/src/Entity/OrderInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ public function removeItem(OrderItemInterface $order_item);
220220
*/
221221
public function hasItem(OrderItemInterface $order_item);
222222

223+
/**
224+
* Removes all adjustments that belong to the order.
225+
*
226+
* This includes both order and order item adjustments, with the exception
227+
* of adjustments added via the UI.
228+
*
229+
* @return $this
230+
*/
231+
public function clearAdjustments();
232+
223233
/**
224234
* Collects all adjustments that belong to the order.
225235
*

modules/order/src/OrderRefresh.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,14 @@ public function needsRefresh(OrderInterface $order) {
128128
public function refresh(OrderInterface $order) {
129129
$current_time = $this->time->getCurrentTime();
130130
$order->setChangedTime($current_time);
131-
// Do not remove adjustments added in the user interface.
132-
$adjustments = $order->getAdjustments();
133-
foreach ($adjustments as $key => $adjustment) {
134-
if ($adjustment->getType() != 'custom') {
135-
unset($adjustments[$key]);
136-
}
137-
}
138-
$order->setAdjustments($adjustments);
131+
$order->clearAdjustments();
139132
// Nothing else can be done while the order is empty.
140133
if (!$order->hasItems()) {
141134
return;
142135
}
143136

144137
$context = new Context($order->getCustomer(), $order->getStore());
145138
foreach ($order->getItems() as $order_item) {
146-
$order_item->setAdjustments([]);
147-
148139
$purchased_entity = $order_item->getPurchasedEntity();
149140
if ($purchased_entity) {
150141
$order_item->setTitle($purchased_entity->getOrderItemTitle());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tax:
2+
label: Tax
3+
has_ui: true
4+
weight: 50

modules/tax/commerce_tax.info.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: Commerce Tax
2+
type: module
3+
description: 'Provides tax functionality.'
4+
package: Commerce
5+
core: 8.x
6+
dependencies:
7+
- commerce:commerce_order
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
entity.commerce_tax_type.add_form:
2+
route_name: entity.commerce_tax_type.add_form
3+
title: 'Add tax type'
4+
appears_on:
5+
- entity.commerce_tax_type.collection
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
entity.commerce_tax_type.collection:
2+
title: 'Tax types'
3+
route_name: 'entity.commerce_tax_type.collection'
4+
parent: 'commerce.configuration'
5+
description: 'Manage your tax types.'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
entity.commerce_tax_type.edit_form:
2+
route_name: entity.commerce_tax_type.edit_form
3+
base_route: entity.commerce_tax_type.edit_form
4+
title: Edit

modules/tax/commerce_tax.module

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Provides tax functionality.
6+
*/
7+
8+
use Drupal\commerce_tax\TaxableType;
9+
use Drupal\Core\Entity\EntityTypeInterface;
10+
use Drupal\Core\Field\BaseFieldDefinition;
11+
use Drupal\Core\Form\FormStateInterface;
12+
13+
/**
14+
* Implements hook_entity_base_field_info().
15+
*/
16+
function commerce_tax_entity_base_field_info(EntityTypeInterface $entity_type) {
17+
if ($entity_type->id() === 'commerce_store') {
18+
$fields['prices_include_tax'] = BaseFieldDefinition::create('boolean')
19+
->setLabel(t('Prices are entered with taxes included.'))
20+
->setDisplayOptions('form', [
21+
'type' => 'boolean_checkbox',
22+
'settings' => [
23+
'display_label' => TRUE,
24+
],
25+
'weight' => 3,
26+
])
27+
->setDisplayConfigurable('view', TRUE)
28+
->setDisplayConfigurable('form', TRUE)
29+
->setDefaultValue(FALSE);
30+
31+
$fields['tax_registrations'] = BaseFieldDefinition::create('list_string')
32+
->setLabel(t('Tax registrations'))
33+
->setDescription(t('The countries where the store is additionally registered to collect taxes.'))
34+
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
35+
->setSetting('allowed_values_function', ['\Drupal\commerce_store\Entity\Store', 'getAvailableCountries'])
36+
->setDisplayOptions('form', [
37+
'type' => 'options_select',
38+
'weight' => 4,
39+
])
40+
->setDisplayConfigurable('view', TRUE)
41+
->setDisplayConfigurable('form', TRUE);
42+
43+
return $fields;
44+
}
45+
}
46+
47+
/**
48+
* Implements hook_form_BASE_FORM_ID_alter() for 'commerce_store_form'.
49+
*/
50+
function commerce_tax_form_commerce_store_form_alter(&$form, FormStateInterface $form_state) {
51+
if (isset($form['prices_include_tax']) || isset($form['tax_registrations'])) {
52+
$form['tax_settings'] = [
53+
'#title' => t('Tax settings'),
54+
'#weight' => 99,
55+
'#type' => 'details',
56+
'#collapsible' => TRUE,
57+
'#open' => TRUE,
58+
];
59+
$form['prices_include_tax']['#group'] = 'tax_settings';
60+
$form['tax_registrations']['#group'] = 'tax_settings';
61+
}
62+
}
63+
64+
/**
65+
* Implements hook_form_FORM_ID_alter() for 'commerce_order_item_type_form'.
66+
*/
67+
function commerce_tax_form_commerce_order_item_type_form_alter(array &$form, FormStateInterface $form_state) {
68+
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */
69+
$order_item_type = $form_state->getFormObject()->getEntity();
70+
71+
$form['commerce_tax'] = [
72+
'#type' => 'container',
73+
'#weight' => 5,
74+
];
75+
$form['commerce_tax']['taxable_type'] = [
76+
'#type' => 'select',
77+
'#title' => t('Taxable type'),
78+
'#options' => TaxableType::getLabels(),
79+
'#default_value' => $order_item_type->getThirdPartySetting('commerce_tax', 'taxable_type', TaxableType::getDefault()),
80+
'#required' => TRUE,
81+
];
82+
$form['actions']['submit']['#submit'][] = 'commerce_tax_order_item_type_form_submit';
83+
}
84+
85+
/**
86+
* Submission handler for commerce_tax_form_commerce_order_item_type_form_alter().
87+
*/
88+
function commerce_tax_order_item_type_form_submit($form, FormStateInterface $form_state) {
89+
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */
90+
$order_item_type = $form_state->getFormObject()->getEntity();
91+
$settings = $form_state->getValue(['commerce_tax']);
92+
$order_item_type->setThirdPartySetting('commerce_tax', 'taxable_type', $settings['taxable_type']);
93+
$order_item_type->save();
94+
}

0 commit comments

Comments
 (0)