Skip to content

Commit db0dce6

Browse files
authored
Merge pull request magento#4851 from magento-honey-badgers/MC-20244
[honey] MC-20244: Pricing :: FPT calculation
2 parents d574dce + 28f5700 commit db0dce6

File tree

14 files changed

+964
-63
lines changed

14 files changed

+964
-63
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ public function resolve(
6262
$product = $value['model'];
6363
$product->unsetData('minimal_price');
6464

65-
return [
66-
'minimum_price' => $this->getMinimumProductPrice($product, $store),
67-
'maximum_price' => $this->getMaximumProductPrice($product, $store)
68-
];
65+
$requestedFields = $info->getFieldSelection(10);
66+
$returnArray = [];
67+
68+
if (isset($requestedFields['minimum_price'])) {
69+
$returnArray['minimum_price'] = $this->getMinimumProductPrice($product, $store);
70+
}
71+
if (isset($requestedFields['maximum_price'])) {
72+
$returnArray['maximum_price'] = $this->getMaximumProductPrice($product, $store);
73+
}
74+
return $returnArray;
6975
}
7076

7177
/**
@@ -80,8 +86,9 @@ private function getMinimumProductPrice(SaleableInterface $product, StoreInterfa
8086
$priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId());
8187
$regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue();
8288
$finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue();
83-
84-
return $this->formatPrice($regularPrice, $finalPrice, $store);
89+
$minPriceArray = $this->formatPrice($regularPrice, $finalPrice, $store);
90+
$minPriceArray['model'] = $product;
91+
return $minPriceArray;
8592
}
8693

8794
/**
@@ -96,8 +103,9 @@ private function getMaximumProductPrice(SaleableInterface $product, StoreInterfa
96103
$priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId());
97104
$regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue();
98105
$finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue();
99-
100-
return $this->formatPrice($regularPrice, $finalPrice, $store);
106+
$maxPriceArray = $this->formatPrice($regularPrice, $finalPrice, $store);
107+
$maxPriceArray['model'] = $product;
108+
return $maxPriceArray;
101109
}
102110

103111
/**

app/code/Magento/CatalogGraphQl/etc/schema.graphqls

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ type Price @doc(description: "Price is deprecated, replaced by ProductPrice. The
2121
adjustments: [PriceAdjustment] @deprecated(reason: "Price is deprecated, use ProductPrice.") @doc(description: "An array that provides information about tax, weee, or weee_tax adjustments.")
2222
}
2323

24-
type PriceAdjustment @doc(description: "The PricedAdjustment object defines the amount of money to apply as an adjustment, the type of adjustment to apply, and whether the item is included or excluded from the adjustment.") {
24+
type PriceAdjustment @doc(description: "PriceAdjustment is deprecated. Taxes will be included or excluded in the price. The PricedAdjustment object defines the amount of money to apply as an adjustment, the type of adjustment to apply, and whether the item is included or excluded from the adjustment.") {
2525
amount: Money @doc(description: "The amount of the price adjustment and its currency code.")
26-
code: PriceAdjustmentCodesEnum @doc(description: "Indicates whether the adjustment involves tax, weee, or weee_tax.")
27-
description: PriceAdjustmentDescriptionEnum @doc(description: "Indicates whether the entity described by the code attribute is included or excluded from the adjustment.")
26+
code: PriceAdjustmentCodesEnum @deprecated(reason: "PriceAdjustment is deprecated.") @doc(description: "Indicates whether the adjustment involves tax, weee, or weee_tax.")
27+
description: PriceAdjustmentDescriptionEnum @deprecated(reason: "PriceAdjustment is deprecated.") @doc(description: "Indicates whether the entity described by the code attribute is included or excluded from the adjustment.")
2828
}
2929

30-
enum PriceAdjustmentCodesEnum @doc(description: "Note: This enumeration contains values defined in modules other than the Catalog module.") {
30+
enum PriceAdjustmentCodesEnum @doc(description: "PriceAdjustment.code is deprecated. This enumeration contains values defined in modules other than the Catalog module.") {
3131
}
3232

33-
enum PriceAdjustmentDescriptionEnum @doc(description: "This enumeration states whether a price adjustment is included or excluded.") {
33+
enum PriceAdjustmentDescriptionEnum @doc(description: "PriceAdjustmentDescriptionEnum is deprecated. This enumeration states whether a price adjustment is included or excluded.") {
3434
INCLUDED
3535
EXCLUDED
3636
}

app/code/Magento/TaxGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# See COPYING.txt for license details.
33

44
enum PriceAdjustmentCodesEnum {
5-
TAX
5+
TAX @deprecated(reason: "PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
66
}

app/code/Magento/Weee/Model/ResourceModel/Tax.php

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
*/
66
namespace Magento\Weee\Model\ResourceModel;
77

8-
use Magento\Catalog\Model\Product;
9-
use Magento\Catalog\Model\Product\Condition\ConditionInterface;
10-
118
/**
129
* Wee tax resource model
1310
*
@@ -21,6 +18,11 @@ class Tax extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
2118
*/
2219
protected $dateTime;
2320

21+
/**
22+
* @var array
23+
*/
24+
private $weeeTaxCalculationsByEntityCache = [];
25+
2426
/**
2527
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
2628
* @param \Magento\Framework\Stdlib\DateTime $dateTime
@@ -46,7 +48,7 @@ protected function _construct()
4648
}
4749

4850
/**
49-
* Fetch one
51+
* Fetch one calculated weee attribute from a select criteria
5052
*
5153
* @param \Magento\Framework\DB\Select|string $select
5254
* @return string
@@ -57,6 +59,8 @@ public function fetchOne($select)
5759
}
5860

5961
/**
62+
* Is there a weee attribute available for the location provided
63+
*
6064
* @param int $countryId
6165
* @param int $regionId
6266
* @param int $websiteId
@@ -91,6 +95,8 @@ public function isWeeeInLocation($countryId, $regionId, $websiteId)
9195
}
9296

9397
/**
98+
* Fetch calculated weee attributes by location, store and entity
99+
*
94100
* @param int $countryId
95101
* @param int $regionId
96102
* @param int $websiteId
@@ -100,43 +106,56 @@ public function isWeeeInLocation($countryId, $regionId, $websiteId)
100106
*/
101107
public function fetchWeeeTaxCalculationsByEntity($countryId, $regionId, $websiteId, $storeId, $entityId)
102108
{
103-
$attributeSelect = $this->getConnection()->select();
104-
$attributeSelect->from(
105-
['eavTable' => $this->getTable('eav_attribute')],
106-
['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
107-
)->joinLeft(
108-
['eavLabel' => $this->getTable('eav_attribute_label')],
109-
'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' .((int) $storeId),
110-
'eavLabel.value as label_value'
111-
)->joinInner(
112-
['weeeTax' => $this->getTable('weee_tax')],
113-
'weeeTax.attribute_id = eavTable.attribute_id',
114-
'weeeTax.value as weee_value'
115-
)->where(
116-
'eavTable.frontend_input = ?',
117-
'weee'
118-
)->where(
119-
'weeeTax.website_id IN(?)',
120-
[$websiteId, 0]
121-
)->where(
122-
'weeeTax.country = ?',
123-
$countryId
124-
)->where(
125-
'weeeTax.state IN(?)',
126-
[$regionId, 0]
127-
)->where(
128-
'weeeTax.entity_id = ?',
129-
(int)$entityId
109+
$cacheKey = sprintf(
110+
'%s-%s-%s-%s-%s',
111+
$countryId,
112+
$regionId,
113+
$websiteId,
114+
$storeId,
115+
$entityId
130116
);
117+
if (!isset($this->weeeTaxCalculationsByEntityCache[$cacheKey])) {
118+
$attributeSelect = $this->getConnection()->select();
119+
$attributeSelect->from(
120+
['eavTable' => $this->getTable('eav_attribute')],
121+
['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
122+
)->joinLeft(
123+
['eavLabel' => $this->getTable('eav_attribute_label')],
124+
'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' . ((int)$storeId),
125+
'eavLabel.value as label_value'
126+
)->joinInner(
127+
['weeeTax' => $this->getTable('weee_tax')],
128+
'weeeTax.attribute_id = eavTable.attribute_id',
129+
'weeeTax.value as weee_value'
130+
)->where(
131+
'eavTable.frontend_input = ?',
132+
'weee'
133+
)->where(
134+
'weeeTax.website_id IN(?)',
135+
[$websiteId, 0]
136+
)->where(
137+
'weeeTax.country = ?',
138+
$countryId
139+
)->where(
140+
'weeeTax.state IN(?)',
141+
[$regionId, 0]
142+
)->where(
143+
'weeeTax.entity_id = ?',
144+
(int)$entityId
145+
);
131146

132-
$order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
133-
'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
134-
$attributeSelect->order($order);
147+
$order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
148+
'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
149+
$attributeSelect->order($order);
135150

136-
$values = $this->getConnection()->fetchAll($attributeSelect);
151+
$values = $this->getConnection()->fetchAll($attributeSelect);
137152

138-
if ($values) {
139-
return $values;
153+
if ($values) {
154+
$this->weeeTaxCalculationsByEntityCache[$cacheKey] = $values;
155+
return $values;
156+
}
157+
} else {
158+
return $this->weeeTaxCalculationsByEntityCache[$cacheKey];
140159
}
141160

142161
return [];
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WeeeGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Weee\Helper\Data;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Tax\Helper\Data as TaxHelper;
16+
use Magento\Store\Api\Data\StoreInterface;
17+
use Magento\Tax\Model\Config;
18+
19+
/**
20+
* Resolver for FixedProductTax object that retrieves an array of FPT attributes with prices
21+
*/
22+
class FixedProductTax implements ResolverInterface
23+
{
24+
/**
25+
* @var Data
26+
*/
27+
private $weeeHelper;
28+
29+
/**
30+
* @var TaxHelper
31+
*/
32+
private $taxHelper;
33+
34+
/**
35+
* @param Data $weeeHelper
36+
* @param TaxHelper $taxHelper
37+
*/
38+
public function __construct(Data $weeeHelper, TaxHelper $taxHelper)
39+
{
40+
$this->weeeHelper = $weeeHelper;
41+
$this->taxHelper = $taxHelper;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
) {
54+
if (!isset($value['model'])) {
55+
throw new LocalizedException(__('"model" value should be specified'));
56+
}
57+
58+
$fptArray = [];
59+
$product = $value['model'];
60+
61+
/** @var StoreInterface $store */
62+
$store = $context->getExtensionAttributes()->getStore();
63+
64+
if ($this->weeeHelper->isEnabled($store)) {
65+
$attributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
66+
foreach ($attributes as $attribute) {
67+
$displayInclTaxes = $this->taxHelper->getPriceDisplayType($store);
68+
$amount = $attribute->getData('amount');
69+
//add display mode for WEE to not return WEE if excluded
70+
if ($displayInclTaxes === Config::DISPLAY_TYPE_EXCLUDING_TAX) {
71+
$amount = $attribute->getData('amount_excl_tax');
72+
} elseif ($displayInclTaxes === Config::DISPLAY_TYPE_INCLUDING_TAX) {
73+
$amount = $attribute->getData('amount_excl_tax') + $attribute->getData('tax_amount');
74+
}
75+
$fptArray[] = [
76+
'amount' => [
77+
'value' => $amount,
78+
'currency' => $value['final_price']['currency'],
79+
],
80+
'label' => $attribute->getData('name')
81+
];
82+
}
83+
}
84+
85+
return $fptArray;
86+
}
87+
}

app/code/Magento/WeeeGraphQl/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0||~7.3.0",
7-
"magento/framework": "*"
7+
"magento/framework": "*",
8+
"magento/module-store": "*",
9+
"magento/module-tax": "*",
10+
"magento/module-weee": "*"
811
},
912
"suggest": {
10-
"magento/module-weee": "*",
1113
"magento/module-catalog-graph-ql": "*"
1214
},
1315
"license": [

app/code/Magento/WeeeGraphQl/etc/schema.graphqls

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
# See COPYING.txt for license details.
33

44
enum PriceAdjustmentCodesEnum {
5-
WEE
6-
WEETAX
5+
WEEE @deprecated(reason: "WEEE code is deprecated, use fixed_product_taxes.label")
6+
WEEE_TAX @deprecated(reason: "Use fixed_product_taxes. PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
7+
}
8+
9+
type ProductPrice {
10+
fixed_product_taxes: [FixedProductTax] @doc(description: "The multiple FPTs that can be applied to a product price.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTax")
11+
}
12+
13+
type FixedProductTax @doc(description: "A single FPT that can be applied to a product price.") {
14+
amount: Money @doc(description: "Amount of the FPT as a money object.")
15+
label: String @doc(description: "The label assigned to the FPT to be displayed on the frontend.")
716
}

0 commit comments

Comments
 (0)