Skip to content

Commit

Permalink
Issue #2873394 by bojanz: Make taxes functional
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Apr 27, 2017
1 parent b8a7444 commit 6be1d5c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 12 deletions.
4 changes: 2 additions & 2 deletions modules/cart/tests/src/Kernel/CartManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testCartManager() {

$order_item1 = $this->cartManager->addEntity($cart, $this->variation1);
$order_item1 = $this->reloadEntity($order_item1);
$this->assertEquals([$order_item1], $cart->getItems());
$this->assertNotEmpty($cart->hasItem($order_item1));
$this->assertEquals(1, $order_item1->getQuantity());
$this->assertEquals(new Price('1.00', 'USD'), $cart->getTotalPrice());

Expand Down Expand Up @@ -145,7 +145,7 @@ public function testCartManager() {
}

/**
* Tests that order items without purchaseable entity do not cause crashes.
* Tests that order items without purchasable entities do not cause crashes.
*/
public function testAddOrderItem() {
$this->installCommerceCart();
Expand Down
8 changes: 7 additions & 1 deletion modules/order/src/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ public function preSave(EntityStorageInterface $storage) {
}
}

// Ensure that $order_item->getOrder() works, for the order refresh
// process. The actual reference will be saved in postSave().
foreach ($this->getItems() as $order_item) {
if ($order_item->order_id->isEmpty()) {
$order_item->order_id = $this;
}
}
// Maintain the completed timestamp.
$state = $this->getState()->value;
$original_state = isset($this->original) ? $this->original->getState()->value : '';
Expand All @@ -493,7 +500,6 @@ public function preSave(EntityStorageInterface $storage) {
$this->setCompletedTime(\Drupal::time()->getRequestTime());
}
}

// Refresh draft orders on every save.
if ($this->getState()->value == 'draft' && empty($this->getRefreshState())) {
$this->setRefreshState(self::REFRESH_ON_SAVE);
Expand Down
2 changes: 1 addition & 1 deletion modules/tax/config/schema/commerce_tax.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ commerce_tax.commerce_tax_type.plugin.custom:
type: label
label: 'Label'
amount:
type: float
type: string
label: 'Amount'
territories:
type: sequence
Expand Down
10 changes: 5 additions & 5 deletions modules/tax/src/Event/CustomerProfileEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ class CustomerProfileEvent extends Event {
* Constructs a new CustomerProfileEvent.
*
* @param \Drupal\profile\Entity\ProfileInterface $customer_profile
* The customer profile.
* The initially selected customer profile.
* @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item
* The removed order item.
* The order item.
*/
public function __construct(ProfileInterface $customer_profile, OrderItemInterface $order_item) {
public function __construct(ProfileInterface $customer_profile = NULL, OrderItemInterface $order_item) {
$this->customerProfile = $customer_profile;
$this->orderItem = $order_item;
}

/**
* Gets the customer profile.
*
* @return \Drupal\profile\Entity\ProfileInterface
* The customer profile.
* @return \Drupal\profile\Entity\ProfileInterface|null
* The customer profile, or NULL if not yet known.
*/
public function getCustomerProfile() {
return $this->customerProfile;
Expand Down
4 changes: 1 addition & 3 deletions modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ protected function getTaxableType(OrderItemInterface $order_item) {
*/
protected function resolveCustomerProfile(OrderItemInterface $order_item) {
$order = $order_item->getOrder();
if (!$order) {
return;
}
$store = $order->getStore();
$prices_include_tax = $store->get('prices_include_tax')->value;
$customer_profile = $order->getBillingProfile();
Expand All @@ -232,6 +229,7 @@ protected function resolveCustomerProfile(OrderItemInterface $order_item) {
// better to show the store's default tax than nothing.
$profile_storage = $this->entityTypeManager->getStorage('profile');
$customer_profile = $profile_storage->create([
'type' => 'customer',
'uid' => $order->getCustomerId(),
'address' => $store->getAddress(),
]);
Expand Down
141 changes: 141 additions & 0 deletions modules/tax/tests/src/Kernel/OrderIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Drupal\Tests\commerce_tax\Kernel;

use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemType;
use Drupal\commerce_price\Price;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;

/**
* Tests integration with orders.
*
* @group commerce
*/
class OrderIntegrationTest extends CommerceKernelTestBase {

/**
* A sample order.
*
* @var \Drupal\commerce_order\Entity\OrderInterface
*/
protected $order;

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'entity_reference_revisions',
'path',
'profile',
'state_machine',
'commerce_order',
'commerce_tax',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->installEntitySchema('profile');
$this->installEntitySchema('commerce_order');
$this->installEntitySchema('commerce_order_item');
$this->installConfig(['commerce_order']);
$user = $this->createUser(['mail' => $this->randomString() . '@example.com']);

$this->store->set('prices_include_tax', TRUE);
$this->store->save();

// The default store is US-WI, so imagine that the US has VAT.
TaxType::create([
'id' => 'us_vat',
'label' => 'US VAT',
'plugin' => 'custom',
'configuration' => [
'display_inclusive' => TRUE,
'rates' => [
[
'id' => 'standard',
'label' => 'Standard',
'amount' => '0.2',
],
],
'territories' => [
['country_code' => 'US', 'administrative_area' => 'WI'],
['country_code' => 'US', 'administrative_area' => 'SC'],
],
],
])->save();
OrderItemType::create([
'id' => 'test',
'label' => 'Test',
'orderType' => 'default',
])->save();
$order = Order::create([
'type' => 'default',
'store_id' => $this->store->id(),
'state' => 'draft',
'mail' => $user->getEmail(),
'uid' => $user->id(),
'ip_address' => '127.0.0.1',
'order_number' => '6',
]);
$order->save();
$this->order = $this->reloadEntity($order);
}

/**
* Tests that the store address is used as a default for new orders.
*/
public function testDefaultProfile() {
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => '1',
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item->save();
$this->order->addItem($order_item);
$this->order->save();
$adjustments = $this->order->collectAdjustments();
$adjustment = reset($adjustments);
$this->assertCount(1, $adjustments);
$this->assertEquals(new Price('2.00', 'USD'), $adjustment->getAmount());
$this->assertEquals('us_vat|default|standard', $adjustment->getSourceId());
}

/**
* Tests the usage of default billing profile.
*/
public function testBillingProfile() {
$profile = Profile::create([
'type' => 'customer',
'address' => [
'country_code' => 'US',
'administrative_area' => 'SC',
],
]);
$profile->save();
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => '1',
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item->save();
$this->order->addItem($order_item);
$this->order->setBillingProfile($profile);
$this->order->save();
$adjustments = $this->order->collectAdjustments();
$adjustment = reset($adjustments);
$this->assertCount(1, $adjustments);
$this->assertEquals(new Price('2.00', 'USD'), $adjustment->getAmount());
$this->assertEquals('us_vat|default|standard', $adjustment->getSourceId());
}

}

0 comments on commit 6be1d5c

Please sign in to comment.