Skip to content

Instantly share code, notes, and snippets.

@Saylt
Created October 12, 2021 06:25
Show Gist options
  • Save Saylt/dc2ff1af4dcdea4a15f86e9e6b4756e5 to your computer and use it in GitHub Desktop.
Save Saylt/dc2ff1af4dcdea4a15f86e9e6b4756e5 to your computer and use it in GitHub Desktop.
CommerceML taxes fix
diff --git a/app/addons/commerceml/src/Formators/OrderFormator.php b/app/addons/commerceml/src/Formators/OrderFormator.php
index e47f7aa93ac..dbc8aa638e7 100644
--- a/app/addons/commerceml/src/Formators/OrderFormator.php
+++ b/app/addons/commerceml/src/Formators/OrderFormator.php
@@ -366,7 +366,7 @@ private function formProductsData(array $order_data, $discount_rate)
$taxes_data = [];
if (!empty($order_data['taxes'])) {
- $taxes_data = $this->formTaxesData($order_data['taxes']);
+ $taxes_data = $this->formTaxesData($order_data['taxes'], $order_data['products']);
}
if ($this->settings['orders_exporter.export_shipping_fee'] === true && $order_data['shipping_cost'] > 0) {
@@ -396,18 +396,21 @@ private function formProductsData(array $order_data, $discount_rate)
* Format taxes data to commerceml format
*
* @param array<int, array<string, string|int|float|array>> $taxes_data Taxes data
+ * @param array<int, array<string, string|int|float|array>> $products Products data
*
* @return array<string, string|int|array>
*/
- private function formTaxesData(array $taxes_data)
+ private function formTaxesData(array $taxes_data, array $products)
{
$data = $products_taxes = [];
+ /**
+ * @psalm-var array{int, array{price: int|string}} $products
+ */
foreach ($taxes_data as $key => $tax) {
$tax_in_total = $tax['price_includes_tax'] === YesNo::YES ? 'true' : 'false';
-
- $tax_value = $this->import_entity_map_repository->findEntityIds(TaxDto::REPRESENT_ENTITY_TYPE, $key);
- $tax_value = empty($tax_value) ? $tax['rate_value'] : array_shift($tax_value);
+ $tax_value = $tax['rate_type'] === 'P' ? $tax['rate_value'] : 0;
+ $tax_value = round((float) $tax_value, 2);
$order_tax = [
'name' => $tax['description'],
@@ -419,6 +422,18 @@ private function formTaxesData(array $taxes_data)
if (!empty($tax['applies']['items'][TaxApplies::PRODUCT])) {
foreach (array_keys($tax['applies']['items'][TaxApplies::PRODUCT]) as $product_item) {
$products_taxes[$product_item][$key] = $order_tax;
+ if ($tax['rate_type'] === 'P') {
+ continue;
+ }
+
+ if ($tax['price_includes_tax'] === YesNo::YES) {
+ $tax_value = round(((float) $tax['rate_value'] /
+ ((float) $products[$product_item]['price'] - (float) $tax['rate_value']))
+ * 100, 2);
+ } else {
+ $tax_value = round((float) $tax['rate_value'] / (float) $products[$product_item]['price'] * 100, 2);
+ }
+ $products_taxes[$product_item][$key]['value'] = $tax_value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment