Ajouter un commentaire

Soumis par Kazu Hodota (non vérifié) le 19/10/2019 à 12:24 - Permalien

Hi,
I would like to add Asia / Japan Tax type at Drupal 8 Commerce 8.x-2.13 now, in Japan, Tax is "Standard 10%" and "Reduced 8%" from this month. I think this Tax style is same as Germany, so I made AsiaVat.php file from EuropeanUnionVat.php file. I attached AsiaVat.php,

<?php

namespace Drupal\commerce_tax\Plugin\Commerce\TaxType;

use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_tax\TaxableType;
use Drupal\commerce_tax\TaxZone;
use Drupal\Core\Form\FormStateInterface;
use Drupal\profile\Entity\ProfileInterface;

/**
* Provides the Aisa VAT tax type.
*
* @CommerceTaxType(
* id = "asia_vat",
* label = "Asia VAT",
* )
*/
class AsiaVat extends LocalTaxTypeBase {

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['rates'] = $this->buildRateSummary();
// The Intra-Community rate is special and should not be in the summary.
unset($form['rates']['table']['ic']);
unset($form['rates']['table']['ic|zero']);
// Replace the phrase "tax rates" with "VAT rates" to be more precise.
$form['rates']['#markup'] = $this->t('The following VAT rates are provided:');

return $form;
}

/**
* {@inheritdoc}
*/
protected function resolveZones(OrderItemInterface $order_item, ProfileInterface $customer_profile) {
$zones = $this->getZones();
/** @var \Drupal\address\AddressInterface $customer_address */
$customer_address = $customer_profile->get('address')->first();
$customer_country = $customer_address->getCountryCode();
$customer_zones = array_filter($zones, function ($zone) use ($customer_address) {
/** @var \Drupal\commerce_tax\TaxZone $zone */
return $zone->match($customer_address);
});
if (empty($customer_zones)) {
// The customer is not in the EU.
return [];
}
$order = $order_item->getOrder();
$store = $order->getStore();
$store_address = $store->getAddress();
$store_country = $store_address->getCountryCode();
$store_zones = array_filter($zones, function ($zone) use ($store_address) {
/** @var \Drupal\commerce_tax\TaxZone $zone */
return $zone->match($store_address);
});
$store_registration_zones = array_filter($zones, function ($zone) use ($store) {
/** @var \Drupal\commerce_tax\TaxZone $zone */
return $this->checkRegistrations($store, $zone);
});

$customer_tax_number = '';
if (!$customer_profile->get('tax_number')->isEmpty()) {
/** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
$tax_number_item = $customer_profile->get('tax_number')->first();
if ($tax_number_item->checkValue('asia_vat')) {
$customer_tax_number = $tax_number_item->value;
}
}
// Since january 1st 2015 all digital goods sold to EU customers
// must use the customer zone. For example, an ebook sold
// to Germany needs to have German VAT applied.
$taxable_type = $this->getTaxableType($order_item);
$year = $this->getCalculationDate($order)->format('Y');
$is_digital = $taxable_type == TaxableType::DIGITAL_GOODS && $year >= 2015;
if (empty($store_zones) && !empty($store_registration_zones)) {
// The store is not in the EU but is registered to collect VAT for
// digital goods.
$resolved_zones = [];
if ($is_digital) {
$resolved_zones = $customer_tax_number ? [$zones['ic']] : $customer_zones;
}
}
elseif ($customer_tax_number && $customer_country != $store_country) {
// Intra-community supply (B2B).
$resolved_zones = [$zones['ic']];
}
elseif ($is_digital) {
$resolved_zones = $customer_zones;
}
else {
// Physical products use the origin zone, unless the store is
// registered to pay taxes in the destination zone. This is required
// when the total yearly transactions breach the defined threshold.
// See http://www.vatlive.com/eu-vat-rules/vat-registration-threshold/
$resolved_zones = $store_zones;
$customer_zone = reset($customer_zones);
if ($this->checkRegistrations($store, $customer_zone)) {
$resolved_zones = $customer_zones;
}
}

return $resolved_zones;
}

/**
* {@inheritdoc}
*/
public function buildZones() {
// Avoid instantiating the same labels dozens of times.
$labels = [
'standard' => $this->t('Standard'),
'intermediate' => $this->t('Intermediate'),
'reduced' => $this->t('Reduced'),
'second_reduced' => $this->t('Second Reduced'),
'super_reduced' => $this->t('Super Reduced'),
'special' => $this->t('Special'),
'zero' => $this->t('Zero'),
'vat' => $this->t('VAT'),
];

$zones = [];

$zones['jp'] = new TaxZone([
'id' => 'jp',
'label' => $this->t('Japan'),
'display_label' => $labels['vat'],
/* 'territories' => [
// Germany without Heligoland and Büsingen.
['country_code' => 'DE', 'excluded_postal_codes' => '27498, 78266'],
// Austria (Jungholz and Mittelberg).
['country_code' => 'AT', 'included_postal_codes' => '6691, 6991:6993'],
],
*/
'rates' => [
[
'id' => 'standard',
'label' => $labels['standard'],
'percentages' => [
['number' => '0.1', 'start_date' => '2019-10-01'],
],
'default' => TRUE,
],
[
'id' => 'reduced',
'label' => $labels['reduced'],
'percentages' => [
['number' => '0.08', 'start_date' => '2019-10-01'],
],
],
],
]);
return $zones;
}
}

-------

I think it needs more customize source files or something, If possible, please let me know. Thank you for your support.