From 25db6a68eb7bcd014a7275486a234d7de4b7d75b Mon Sep 17 00:00:00 2001 From: Dinesh V B Date: Thu, 10 Dec 2020 16:54:20 +0530 Subject: [PATCH] issue-30387-initial commit of changing all resolvers to suit the 'formatted' field addition in the 'Money' type --- .../Model/Credit/Balance.php | 74 ++++++ .../Model/Credit/OperationExtractor.php | 83 +++++++ .../Model/Resolver/Credit.php | 105 +++++++++ .../GetAppliedStoreCreditFromCart.php | 132 +++++++++++ .../Model/Resolver/GetCustomerStoreCredit.php | 100 ++++++++ .../GetCustomerStoreCreditHistory.php | 213 ++++++++++++++++++ .../Model/Money/Formatter.php | 52 +++++ .../GiftCardCartItem/GiftCardAmount.php | 63 ++++++ .../Model/Resolver/Cart/GiftOptionsPrices.php | 71 ++++++ .../Model/Resolver/Cart/GiftWrapping.php | 103 +++++++++ .../Model/Resolver/Cart/GiftWrappingList.php | 150 ++++++++++++ .../Model/Resolver/Cart/Item/GiftWrapping.php | 102 +++++++++ .../Resolver/Cart/Item/GiftWrappingList.php | 155 +++++++++++++ .../Model/Resolver/Order/GiftWrapping.php | 118 ++++++++++ .../Resolver/Order/Item/GiftWrapping.php | 120 ++++++++++ .../Model/Formatter/Customer/Balance.php | 47 ++++ .../Formatter/Customer/BalanceHistory.php | 106 +++++++++ .../Model/Resolver/CartRewardPoints.php | 73 ++++++ .../Resolver/Order/Item/BundleOptions.php | 12 +- .../Model/Resolver/PriceTiers.php | 3 +- .../Model/Resolver/Product/Price.php | 15 +- .../Model/Resolver/Product/PriceRange.php | 22 +- app/code/Magento/GraphQl/etc/schema.graphqls | 1 + .../Model/Resolver/CartItemPrices.php | 16 +- .../Model/Resolver/CartPrices.php | 23 +- .../QuoteGraphQl/Model/Resolver/Discounts.php | 15 ++ .../AvailableShippingMethods.php | 17 +- .../SelectedShippingMethod.php | 15 ++ .../Model/OrderItem/DataProvider.php | 17 +- .../Resolver/CreditMemo/CreditMemoItems.php | 17 +- .../Resolver/CreditMemo/CreditMemoTotal.php | 43 ++-- .../Model/Resolver/Invoice/InvoiceItems.php | 17 +- .../Model/Resolver/Invoice/InvoiceTotal.php | 39 +++- .../Model/Resolver/OrderTotal.php | 46 +++- .../Shipment/Item/ShipmentItemFormatter.php | 18 +- .../Model/Resolver/FixedProductTax.php | 11 +- 36 files changed, 2145 insertions(+), 69 deletions(-) create mode 100644 .b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/Balance.php create mode 100644 .b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/OperationExtractor.php create mode 100644 .b2b/app/code/Magento/CompanyCreditGraphQl/Model/Resolver/Credit.php create mode 100644 .ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetAppliedStoreCreditFromCart.php create mode 100644 .ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCredit.php create mode 100644 .ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCreditHistory.php create mode 100644 .ee/app/code/Magento/GiftCardAccountGraphQl/Model/Money/Formatter.php create mode 100644 .ee/app/code/Magento/GiftCardGraphQl/Model/Resolver/GiftCardCartItem/GiftCardAmount.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftOptionsPrices.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrapping.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrappingList.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrapping.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrappingList.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/GiftWrapping.php create mode 100644 .ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/Item/GiftWrapping.php create mode 100644 .ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/Balance.php create mode 100644 .ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/BalanceHistory.php create mode 100644 .ee/app/code/Magento/RewardGraphQl/Model/Resolver/CartRewardPoints.php diff --git a/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/Balance.php b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/Balance.php new file mode 100644 index 0000000000000..bac333a96d3d3 --- /dev/null +++ b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/Balance.php @@ -0,0 +1,74 @@ +priceCurrency = $priceCurrency; + } + + /** + * Get operation balance data + * + * @param HistoryInterface $creditOperation + * @return array[] + */ + public function getBalance(HistoryInterface $creditOperation): array + { + return [ + 'outstanding_balance' => $this->formatData( + $creditOperation->getCurrencyOperation(), + (float)$creditOperation->getBalance(), + $this->priceCurrency->format((float)$creditOperation->getBalance(),false,null,null,$creditOperation->getCurrencyOperation()) + ), + 'available_credit' => $this->formatData( + $creditOperation->getCurrencyOperation(), + (float)$creditOperation->getAvailableLimit(), + $this->priceCurrency->format((float)$creditOperation->getAvailableLimit(),false,null,null,$creditOperation->getCurrencyOperation()) + ), + 'credit_limit' => $this->formatData( + $creditOperation->getCurrencyOperation(), + (float)$creditOperation->getCreditLimit(), + $this->priceCurrency->format((float)$creditOperation->getCreditLimit(),false,null,null,$creditOperation->getCurrencyOperation()) + ) + ]; + } + + /** + * Format credit response data + * + * @param string $currency + * @param float $value + * @param string $formatted + * @return array + */ + public function formatData(string $currency, float $value, string $formatted): array + { + return [ + 'currency' => $currency, + 'value' => $value, + 'formatted' => $formatted + ]; + } +} diff --git a/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/OperationExtractor.php b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/OperationExtractor.php new file mode 100644 index 0000000000000..f15e9c33f9bc8 --- /dev/null +++ b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Credit/OperationExtractor.php @@ -0,0 +1,83 @@ +historyType = $historyType; + $this->operationUser = $operationUser; + $this->balance = $balance; + $this->priceCurrency = $priceCurrency; + } + + /** + * Extract credit history data + * + * @param HistoryInterface $creditOperation + * @return array + */ + public function extractOperation(HistoryInterface $creditOperation): array + { + return [ + 'amount' => $this->balance->formatData( + $creditOperation->getCurrencyOperation(), + (float)$creditOperation->getAmount(), + $this->priceCurrency->format((float)$creditOperation->getAmount(),false,null,null,$creditOperation->getCurrencyOperation()) + ), + 'date' => $creditOperation->getDatetime(), + 'custom_reference_number' => $creditOperation->getCustomReferenceNumber(), + 'type' => $this->historyType->getHistoryType((int)$creditOperation->getType()), + 'updated_by' => [ + 'name' => $this->operationUser->getUserName( + (int)$creditOperation->getUserType(), + (int)$creditOperation->getUserId() + ), + 'type' => $this->operationUser->getUserType((int)$creditOperation->getUserType()) + ], + 'balance' => $this->balance->getBalance($creditOperation) + ]; + } +} diff --git a/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Resolver/Credit.php b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Resolver/Credit.php new file mode 100644 index 0000000000000..a887ceb6bd6bf --- /dev/null +++ b/.b2b/app/code/Magento/CompanyCreditGraphQl/Model/Resolver/Credit.php @@ -0,0 +1,105 @@ +resolverAccess = $resolverAccess; + $this->allowedResources = $allowedResources; + $this->creditDataProvider = $creditDataProvider; + $this->paymentMethodStatus = $paymentMethodStatus; + $this->balance = $balance; + $this->priceCurrency = $priceCurrency; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + + $this->resolverAccess->isAllowed($this->allowedResources); + + if (!$this->paymentMethodStatus->isEnabled()) { + throw new GraphQlInputException(__('"Payment on Account" is disabled.')); + } + + $company = $value['model']; + $credit = $this->creditDataProvider->get($company->getId()); + $currencyCode = $credit->getCurrencyCode(); + + return [ + 'outstanding_balance' => $this->balance->formatData($currencyCode, (float)$credit->getBalance(), $this->priceCurrency->format((float)$credit->getBalance(),false,null,null,$currencyCode)), + 'available_credit' => $this->balance->formatData($currencyCode, (float)$credit->getAvailableLimit(), $this->priceCurrency->format((float)$credit->getAvailableLimit(),false,null,null,$currencyCode)), + 'credit_limit' => $this->balance->formatData($currencyCode, (float)$credit->getCreditLimit(), $this->priceCurrency->format((float)$credit->getCreditLimit(),false,null,null,$currencyCode)) + ]; + } +} diff --git a/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetAppliedStoreCreditFromCart.php b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetAppliedStoreCreditFromCart.php new file mode 100644 index 0000000000000..7d15fe08c24b4 --- /dev/null +++ b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetAppliedStoreCreditFromCart.php @@ -0,0 +1,132 @@ +getCartForUser = $getCartForUser; + $this->cartRepository = $cartRepository; + $this->priceCurrency = $priceCurrency; + $this->balanceFactory = $balanceFactory; + $this->customerBalanceHelper = $customerBalanceHelper; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + $cart = $value['model']; + $cartId = $cart->getId(); + $quote = $this->cartRepository->get($cartId); + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + $currentCurrency = $store->getCurrentCurrency(); + $customerId = $context->getUserId(); + if (empty($customerId)) { + throw new GraphQlAuthorizationException(__('Please specify a valid customer')); + } + $customerBalance = $this->getCustomerBalance( + $customerId, + (int)$store->getWebsiteId(), + (int)$store->getId() + ); + $balanceApplied = $quote->getCustomerBalanceAmountUsed(); + + return [ + 'enabled' => $this->customerBalanceHelper->isEnabled(), + 'current_balance' => $this->customerBalanceHelper->isEnabled() ? [ + 'value' => $customerBalance, + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($customerBalance,false,null,null,$currentCurrency->getCode()) + ] : null, + 'applied_balance' => [ + 'value' => $balanceApplied, + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($balanceApplied,false,null,null,$currentCurrency->getCode()) + ] + ]; + } + + /** + * Return store credit for customer + * + * @param int $customerId + * @param int $websiteId + * @param int $storeId + * @return float + * @throws LocalizedException + */ + private function getCustomerBalance($customerId, int $websiteId, int $storeId): float + { + $baseBalance = $this->balanceFactory->create() + ->setCustomerId($customerId) + ->setWebsiteId($websiteId) + ->loadByCustomer() + ->getAmount(); + $customerBalance = $this->priceCurrency->convert($baseBalance, $storeId); + return $customerBalance; + } +} diff --git a/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCredit.php b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCredit.php new file mode 100644 index 0000000000000..4561bc406cdac --- /dev/null +++ b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCredit.php @@ -0,0 +1,100 @@ +priceCurrency = $priceCurrency; + $this->balanceFactory = $balanceFactory; + $this->customerBalanceHelper = $customerBalanceHelper; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + $currentCurrency = $store->getCurrentCurrency(); + $customerId = $context->getUserId(); + return [ + 'enabled' => $this->customerBalanceHelper->isEnabled(), + 'current_balance' => $this->customerBalanceHelper->isEnabled() ? [ + 'value' => $this->getCustomerBalance( + $customerId, + (int)$store->getWebsiteId(), + (int)$store->getId() + ), + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($this->getCustomerBalance( + $customerId, + (int)$store->getWebsiteId(), + (int)$store->getId() + ),false,null,null,$currentCurrency->getCode()) + ] : null + ]; + } + + /** + * Return store credit for customer + * + * @param int $customerId + * @param int $websiteId + * @param int $storeId + * @return float + * @throws LocalizedException + */ + private function getCustomerBalance($customerId, int $websiteId, int $storeId): float + { + $baseBalance = $this->balanceFactory->create() + ->setCustomerId($customerId) + ->setWebsiteId($websiteId) + ->loadByCustomer() + ->getAmount(); + $customerBalance = $this->priceCurrency->convert($baseBalance, $storeId); + return $customerBalance; + } +} diff --git a/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCreditHistory.php b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCreditHistory.php new file mode 100644 index 0000000000000..ad5402bfd364f --- /dev/null +++ b/.ee/app/code/Magento/CustomerBalanceGraphQl/Model/Resolver/GetCustomerStoreCreditHistory.php @@ -0,0 +1,213 @@ +priceCurrency = $priceCurrency; + $this->historyFactory = $historyFactory; + $this->collectionFactory = $collectionFactory; + $this->customerBalanceHelper = $customerBalanceHelper; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + $customerId = $context->getUserId(); + + if ($args['currentPage'] < 1) { + throw new GraphQlInputException(__('currentPage value must be greater than 0.')); + } + if ($args['pageSize'] < 1) { + throw new GraphQlInputException(__('pageSize value must be greater than 0.')); + } + + return $this->canShowHistory() ? $this->getBalanceHistoryArray( + $customerId, + $store, + $args['pageSize'] ?? 20, + $args['currentPage'] ?? 1 + ) : null; + } + + /** + * Check if settings for history along with customer balance are enabled + * + * @return bool + */ + private function canShowHistory(): bool + { + return $this->customerBalanceHelper->isEnabled() && $this->customerBalanceHelper->isHistoryEnabled(); + } + + /** + * Retrieve history events + * + * @param int $customerId + * @param StoreInterface $store + * @param int $pageSize + * @param int $currentPage + * @return array + * @throws GraphQlInputException + */ + private function getBalanceHistoryArray( + int $customerId, + StoreInterface $store, + int $pageSize = 20, + int $currentPage = 1 + ): array { + $currentCurrency = $store->getCurrentCurrency(); + $headerNamesArray = $this->getActionNamesArray(); + + $collection = $this->collectionFactory->create(); + $collection->addFieldToFilter( + 'customer_id', + $customerId + )->addFieldToFilter( + 'website_id', + $store->getWebsiteId() + )->addOrder( + 'updated_at', + 'DESC' + )->addOrder( + 'history_id', + 'DESC' + )->setCurPage($currentPage) + ->setPageSize($pageSize); + + $collectionTotal = $this->collectionFactory->create(); + $collectionTotal->addFieldToFilter( + 'customer_id', + $customerId + )->addFieldToFilter( + 'website_id', + $store->getWebsiteId() + )->addFieldToSelect('history_id'); + + $historyArray = []; + foreach ($collection as $event) { + $itemArray = [ + 'action' => $headerNamesArray[$event->getAction()], + 'balance_change' => [ + 'value' => $this->priceCurrency->convert($event->getBalanceDelta(), $store), + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($this->priceCurrency->convert($event->getBalanceDelta(), $store),false,null,null,$currentCurrency->getCode()) + ], + 'actual_balance' => [ + 'value' => $this->priceCurrency->convert($event->getBalanceAmount(), $store), + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($this->priceCurrency->convert($event->getBalanceAmount(), $store),false,null,null,$currentCurrency->getCode()) + ], + 'date_time_changed' => $event->getUpdatedAt(), + ]; + $historyArray[] = $itemArray; + } + $totalItems = $collectionTotal->count(); + + //possible division by 0 + if ($pageSize) { + $maxPages = ceil($totalItems / $pageSize); + } else { + $maxPages = 0; + } + + if ($currentPage > $maxPages && $totalItems > 0) { + $currentPage = new GraphQlInputException( + __( + 'currentPage value %1 specified is greater than the number of pages available.', + [$maxPages] + ) + ); + } + + return [ + 'items' => $historyArray, + 'page_info' => [ + 'current_page' => $currentPage, + 'page_size' => $pageSize, + 'total_pages' => $maxPages, + ], + 'total_count' => $totalItems + ]; + } + + /** + * Available action names getter + * + * @return array + */ + private function getActionNamesArray(): array + { + return [ + self::ACTION_CREATED => __('Created'), + self::ACTION_UPDATED => __('Updated'), + self::ACTION_USED => __('Used'), + self::ACTION_REFUNDED => __('Refunded'), + self::ACTION_REVERTED => __('Reverted') + ]; + } +} diff --git a/.ee/app/code/Magento/GiftCardAccountGraphQl/Model/Money/Formatter.php b/.ee/app/code/Magento/GiftCardAccountGraphQl/Model/Money/Formatter.php new file mode 100644 index 0000000000000..578f511717539 --- /dev/null +++ b/.ee/app/code/Magento/GiftCardAccountGraphQl/Model/Money/Formatter.php @@ -0,0 +1,52 @@ +priceCurrency = $priceCurrency; + } + /** + * Convert amount value into a Money type array + * + * @param float|string $amount + * @param Store $store + * @throws LocalizedException + * @return array + */ + public function formatAmountAsMoney($amount, Store $store): array + { + /** @var Store $store */ + $currentCurrency = $store->getCurrentCurrency(); + $baseCurrency = $store->getBaseCurrency(); + + $convertedBalance = $baseCurrency->convert($amount, $currentCurrency); + + return [ + 'value' => $convertedBalance, + 'currency' => $currentCurrency->getCode(), + 'formatted' => $this->priceCurrency->format($convertedBalance,false,null,null,$currentCurrency->getCode()) + ]; + } +} diff --git a/.ee/app/code/Magento/GiftCardGraphQl/Model/Resolver/GiftCardCartItem/GiftCardAmount.php b/.ee/app/code/Magento/GiftCardGraphQl/Model/Resolver/GiftCardCartItem/GiftCardAmount.php new file mode 100644 index 0000000000000..635289aecaf99 --- /dev/null +++ b/.ee/app/code/Magento/GiftCardGraphQl/Model/Resolver/GiftCardCartItem/GiftCardAmount.php @@ -0,0 +1,63 @@ +priceCurrency = $priceCurrency; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!(($value['model'] ?? null) instanceof CartItemInterface)) { + throw new LocalizedException(__('"model" value should be specified')); + } + /** @var QuoteItem $cartItem */ + $cartItem = $value['model']; + /** @var QuoteItemOption $amountOption */ + $amountOption = $cartItem->getOptionByCode(GiftcardOption::KEY_AMOUNT); + + return [ + 'value' => floatval($amountOption->getValue()), + 'currency' => $cartItem->getQuote()->getQuoteCurrencyCode(), + 'formatted' => $this->priceCurrency->format(floatval($amountOption->getValue()),false,null,null,$cartItem->getQuote()->getQuoteCurrencyCode()) + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftOptionsPrices.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftOptionsPrices.php new file mode 100644 index 0000000000000..b75bc2a328526 --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftOptionsPrices.php @@ -0,0 +1,71 @@ +priceCurrency = $priceCurrency; + } + + /** + * Get information about gift wrapping prices + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return Value|mixed|void + * + * @throws LocalizedException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!(($value['model'] ?? null) instanceof CartInterface)) { + throw new LocalizedException(__('"model" value should be specified')); + } + + /** @var Quote $quote */ + $quote = $value['model']; + $currency = $quote->getQuoteCurrencyCode(); + + return [ + 'gift_wrapping_for_order' => ['value' => $quote->getGwPrice(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($quote->getGwPrice(),false,null,null,$currency)], + 'gift_wrapping_for_items' => ['value' => $quote->getGwItemsPrice(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($quote->getGwItemsPrice(),false,null,null,$currency)], + 'printed_card' => ['value' => $quote->getGwCardPrice(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($quote->getGwCardPrice(),false,null,null,$currency)] + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrapping.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrapping.php new file mode 100644 index 0000000000000..0e7b36b04885d --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrapping.php @@ -0,0 +1,103 @@ +wrappingRepository = $wrappingRepository; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get gift wrapping data for cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * + * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!(($value['model'] ?? null) instanceof CartInterface)) { + throw new GraphQlInputException(__('"model" value should be specified')); + } + + $cart = $value['model']; + $giftWrappingId = $cart->getGwId(); + + if (empty($giftWrappingId)) { + return null; + } + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + + try { + $cartGiftWrapping = $this->wrappingRepository->get((int)$giftWrappingId, (int)$store->getStoreId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for cart.')); + } + + return [ + 'id' => $cartGiftWrapping->getWrappingId() ?? '', + 'design' => $cartGiftWrapping->getDesign() ?? '', + 'price' => [ + 'value' => $cartGiftWrapping->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($cartGiftWrapping->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $cartGiftWrapping->getImageName() ?? '', + 'url'=> $cartGiftWrapping->getImageUrl() ?? '' + ] + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrappingList.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrappingList.php new file mode 100644 index 0000000000000..cceb76cc229ee --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/GiftWrappingList.php @@ -0,0 +1,150 @@ +wrappingRepository = $wrappingRepository; + $this->criteriaBuilder = $criteriaBuilder; + $this->giftWrappingData = $giftWrappingData; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get data about available gift wrappings for cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$this->giftWrappingData->isGiftWrappingAvailableForOrder()) { + return []; + } + + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + $giftWrappingsList = $this->getGiftWrappingsList($store); + + if (empty($giftWrappingsList)) { + return []; + } + + $availableGiftWrappings = []; + foreach ($giftWrappingsList as $item) { + $availableGiftWrappings[] = $this->getGiftWrappingItemData($item, $store); + } + + return $availableGiftWrappings; + } + + /** + * Get data for gift wrapping item + * + * @param WrappingInterface $item + * @param StoreInterface $store + * + * @return array + */ + private function getGiftWrappingItemData(WrappingInterface $item, StoreInterface $store): array + { + // phpcs:ignore Magento2.Functions.DiscouragedFunction + return [ + 'id' => base64_encode($item->getWrappingId()) ?? '', + 'design' => $item->getDesign() ?? '', + 'price' => [ + 'value' => $item->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($item->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $item->getImage() ?? '', + 'url'=> $item->getImageUrl() ?? '' + ] + ]; + } + + /** + * Get available gift wrappings for cart + * + * @param StoreInterface $store + * + * @return WrappingInterface[] + */ + private function getGiftWrappingsList(StoreInterface $store): array + { + return $this->wrappingRepository->getList( + $this->criteriaBuilder + ->addFilter(WrappingInterface::STATUS, self::ENABLE_STATUS) + ->addFilter(Store::STORE_ID, $store->getStoreId()) + ->addFilter(WrappingInterface::WEBSITE_IDS, [$store->getWebsiteId()], 'in') + ->create() + )->getItems(); + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrapping.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrapping.php new file mode 100644 index 0000000000000..e349b129a5fa6 --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrapping.php @@ -0,0 +1,102 @@ +wrappingRepository = $giftWrappingRepository; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get gift wrapping data for cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * + * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!(($value['model'] ?? null) instanceof CartItemInterface)) { + throw new GraphQlInputException(__('"model" value should be specified')); + } + $cart = $value['model']; + $giftWrappingId = $cart->getGwId(); + + if (empty($giftWrappingId)) { + return null; + } + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + + try { + $cartGiftWrapping = $this->wrappingRepository->get((int)$giftWrappingId, (int)$store->getStoreId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for item.')); + } + + return [ + 'id' => $cartGiftWrapping->getWrappingId() ?? '', + 'design' => $cartGiftWrapping->getDesign() ?? '', + 'price' => [ + 'value' => $cartGiftWrapping->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($cartGiftWrapping->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $cartGiftWrapping->getImageName() ?? '', + 'url'=> $cartGiftWrapping->getImageUrl() ?? '' + ] + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrappingList.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrappingList.php new file mode 100644 index 0000000000000..d4129cea63bd0 --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Cart/Item/GiftWrappingList.php @@ -0,0 +1,155 @@ +wrappingRepository = $wrappingRepository; + $this->criteriaBuilder = $criteriaBuilder; + $this->giftWrappingData = $giftWrappingData; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get data about available gift wrappings for cart item + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$this->giftWrappingData->isGiftWrappingAvailableForItems()) { + return []; + } + + if (isset($value['product']['gift_wrapping_available'])) { + if (empty($value['product']['gift_wrapping_available'])) { + return []; + } + } + + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + $giftWrappingsList = $this->getGiftWrappingsList($store); + + if (empty($giftWrappingsList)) { + return []; + } + + $availableGiftWrappings = []; + foreach ($giftWrappingsList as $item) { + $availableGiftWrappings[] = $this->getGiftWrappingItemData($item, $store); + } + + return $availableGiftWrappings; + } + + /** + * Get available gift wrappings for cart item + * + * @param StoreInterface $store + * + * @return WrappingInterface[] + */ + private function getGiftWrappingsList(StoreInterface $store): array + { + return $this->wrappingRepository->getList( + $this->criteriaBuilder + ->addFilter(WrappingInterface::STATUS, self::ENABLE_STATUS) + ->addFilter(Store::STORE_ID, $store->getStoreId()) + ->addFilter(WrappingInterface::WEBSITE_IDS, [$store->getWebsiteId()], 'in') + ->create() + )->getItems(); + } + + /** + * Get data for gift wrapping item + * + * @param WrappingInterface $item + * @param StoreInterface $store + * + * @return array + */ + private function getGiftWrappingItemData(WrappingInterface $item, StoreInterface $store): array + { + return [ + 'id' => $item->getWrappingId() ?? '', + 'design' => $item->getDesign() ?? '', + 'price' => [ + 'value' => $item->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($item->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $item->getImage() ?? '', + 'url'=> $item->getImageUrl() ?? '' + ] + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/GiftWrapping.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/GiftWrapping.php new file mode 100644 index 0000000000000..5fa9e21c1ec83 --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/GiftWrapping.php @@ -0,0 +1,118 @@ +wrappingRepository = $wrappingRepository; + $this->orderRepository = $orderRepository; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get gift wrapping data for cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * + * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!isset($value['id'])) { + throw new GraphQlInputException(__('"id" value should be specified')); + } + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $orderId = (int)base64_decode($value['id']) ?: (int)$value['id']; + + try { + $order = $this->orderRepository->get($orderId); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for order')); + } + + $giftWrappingId = $order->getGwId(); + + if (empty($giftWrappingId)) { + return null; + } + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + + try { + $cartGiftWrapping = $this->wrappingRepository->get((int)$giftWrappingId, (int)$store->getStoreId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for order.')); + } + + return [ + 'id' => $cartGiftWrapping->getWrappingId() ?? '', + 'design' => $cartGiftWrapping->getDesign() ?? '', + 'price' => [ + 'value' => $cartGiftWrapping->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($cartGiftWrapping->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $cartGiftWrapping->getImageName() ?? '', + 'url'=> $cartGiftWrapping->getImageUrl() ?? '' + ] + ]; + } +} diff --git a/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/Item/GiftWrapping.php b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/Item/GiftWrapping.php new file mode 100644 index 0000000000000..574c236eea875 --- /dev/null +++ b/.ee/app/code/Magento/GiftWrappingGraphQl/Model/Resolver/Order/Item/GiftWrapping.php @@ -0,0 +1,120 @@ +wrappingRepository = $wrappingRepository; + $this->orderItemRepository = $orderItemRepository; + $this->priceCurrency = $priceCurrency; + } + + /** + * Get gift wrapping data for order items + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed|null + * + * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!isset($value['id'])) { + throw new GraphQlInputException(__('"id" value should be specified')); + } + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $orderItemId = (int)base64_decode($value['id']) ?: (int)$value['id']; + + try { + /** @var $orderItem OrderItemInterface*/ + $orderItem = $this->orderItemRepository->get($orderItemId); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for order')); + } + + $giftWrappingId = $orderItem->getGwId(); + + if (empty($giftWrappingId)) { + return null; + } + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + + try { + $cartGiftWrapping = $this->wrappingRepository->get((int)$giftWrappingId, (int)$store->getStoreId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load gift wrapping for order item.')); + } + + return [ + 'id' => $cartGiftWrapping->getWrappingId() ?? '', + 'design' => $cartGiftWrapping->getDesign() ?? '', + 'price' => [ + 'value' => $cartGiftWrapping->getBasePrice() ?? '', + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($cartGiftWrapping->getBasePrice() ?? '',false,null,null,$store->getCurrentCurrencyCode()) + ], + 'image' => [ + 'label'=> $cartGiftWrapping->getImageName() ?? '', + 'url'=> $cartGiftWrapping->getImageUrl() ?? '' + ] + ]; + } +} diff --git a/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/Balance.php b/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/Balance.php new file mode 100644 index 0000000000000..368e5bc8d1cb2 --- /dev/null +++ b/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/Balance.php @@ -0,0 +1,47 @@ +priceCurrency = $priceCurrency; + } + + /** + * @inheritdoc + */ + public function format(Customer $customer, StoreInterface $store, Reward $rewardInstance): array + { + return [ + 'money' => [ + 'currency' => $store->getCurrentCurrency()->getCode(), + 'value' => $rewardInstance->getCurrencyAmount(), + 'formatted' => $this->priceCurrency->format($rewardInstance->getCurrencyAmount(),false,null,null,$store->getCurrentCurrency()->getCode()) + ], + 'points' => $rewardInstance->getPointsBalance() + ]; + } +} diff --git a/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/BalanceHistory.php b/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/BalanceHistory.php new file mode 100644 index 0000000000000..3bd8dbe58be1c --- /dev/null +++ b/.ee/app/code/Magento/RewardGraphQl/Model/Formatter/Customer/BalanceHistory.php @@ -0,0 +1,106 @@ +config = $config; + $this->rewardHistoryCollectionFactory = $rewardHistoryCollectionFactory; + $this->priceCurrency = $priceCurrency; + } + + /** + * @inheritdoc + */ + public function format(Customer $customer, StoreInterface $store, Reward $rewardInstance): array + { + if (!$this->config->customersMaySeeHistory((int)$store->getWebsite()->getId())) { + return []; + } + + $balanceHistory = []; + $rewardHistoryCollection = $this->getHistoryCollection( + (int)$customer->getId(), + (int)$store->getWebsite()->getId() + ); + + /** @var History $rewardHistoryItem */ + foreach ($rewardHistoryCollection as $rewardHistoryItem) { + $balanceHistory[] = [ + 'balance' => [ + 'money' => [ + 'currency' => $store->getCurrentCurrency()->getCode(), + 'value' => $rewardHistoryItem->getCurrencyAmount(), + 'formatted' => $this->priceCurrency->format($rewardHistoryItem->getCurrencyAmount(),false,null,null,$store->getCurrentCurrency()->getCode()) + ], + 'points' => $rewardHistoryItem->getPointsBalance() + ], + 'change_reason' => $rewardHistoryItem->getMessage(), + 'date' => $rewardHistoryItem->getCreatedAt(), + 'points_change' => $rewardHistoryItem->getPointsDelta() + ]; + } + + return $balanceHistory; + } + + /** + * Get reward history for a given customer in a given website + * + * @param int $customerId + * @param int $websiteId + * @return RewardHistoryCollection + */ + private function getHistoryCollection(int $customerId, int $websiteId): RewardHistoryCollection + { + return $this->rewardHistoryCollectionFactory->create() + ->addCustomerFilter($customerId) + ->addWebsiteFilter($websiteId) + ->setExpiryConfig($this->config->getExpirationDetails()) + ->addExpirationDate($websiteId) + ->skipExpiredDuplicates() + ->setDefaultOrder(); + } +} diff --git a/.ee/app/code/Magento/RewardGraphQl/Model/Resolver/CartRewardPoints.php b/.ee/app/code/Magento/RewardGraphQl/Model/Resolver/CartRewardPoints.php new file mode 100644 index 0000000000000..4fd94dd0fe37c --- /dev/null +++ b/.ee/app/code/Magento/RewardGraphQl/Model/Resolver/CartRewardPoints.php @@ -0,0 +1,73 @@ +priceCurrency = $priceCurrency; + } + + /** + * Fetch applied reward points to cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * @return array|Value|mixed|null + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws LocalizedException + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + + $quote = $value['model']; + if ($quote->getUseRewardPoints()) { + return [ + 'money' => [ + 'currency' => $quote->getQuoteCurrencyCode(), + 'value' => $quote->getRewardCurrencyAmount(), + 'formatted' => $this->priceCurrency->format($quote->getRewardCurrencyAmount(),false,null,null,$quote->getQuoteCurrencyCode()) + ], + 'points' => $quote->getRewardPointsBalance() + ]; + } + + return null; + } +} diff --git a/app/code/Magento/BundleGraphQl/Model/Resolver/Order/Item/BundleOptions.php b/app/code/Magento/BundleGraphQl/Model/Resolver/Order/Item/BundleOptions.php index 9bbe69c95f552..c1ea0f79b9399 100644 --- a/app/code/Magento/BundleGraphQl/Model/Resolver/Order/Item/BundleOptions.php +++ b/app/code/Magento/BundleGraphQl/Model/Resolver/Order/Item/BundleOptions.php @@ -14,6 +14,7 @@ use Magento\Framework\GraphQl\Query\Uid; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\Serialize\Serializer\Json; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\InvoiceItemInterface; use Magento\Sales\Api\Data\OrderItemInterface; use Magento\Sales\Api\Data\ShipmentItemInterface; @@ -44,19 +45,27 @@ class BundleOptions implements ResolverInterface /** @var Uid */ private $uidEncoder; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param ValueFactory $valueFactory * @param Json $serializer * @param Uid $uidEncoder + * @param PriceCurrency $priceCurrency */ public function __construct( ValueFactory $valueFactory, Json $serializer, - Uid $uidEncoder + Uid $uidEncoder, + PriceCurrency $priceCurrency ) { $this->valueFactory = $valueFactory; $this->serializer = $serializer; $this->uidEncoder = $uidEncoder; + $this->priceCurrency = $priceCurrency; } /** @@ -163,6 +172,7 @@ private function formatBundleOptionItems( 'value' => $bundleChildAttributes['price'], //use currency from order 'currency' => $formattedItem['product_sale_price']['currency'] ?? null, + 'formatted' => $this->priceCurrency->format($bundleChildAttributes['price'],false,null,null,$formattedItem['product_sale_price']['currency'] ?? null) ] ]; } diff --git a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php index efba88ff154bb..0646f400f8753 100644 --- a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php +++ b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php @@ -182,7 +182,8 @@ private function formatTierPrices(float $productPrice, string $currencyCode, $ti "quantity" => $tierPrice->getQty(), "final_price" => [ "value" => $tierPrice->getValue(), - "currency" => $currencyCode + "currency" => $currencyCode, + "formatted" => $this->priceCurrency->format($tierPrice->getValue(),false,null,null,$currencyCode) ] ]; } diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price.php index c542fc26495f7..a741268d0015d 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price.php @@ -18,6 +18,7 @@ use Magento\Framework\Pricing\Adjustment\AdjustmentInterface; use Magento\Framework\Pricing\Amount\AmountInterface; use Magento\Framework\Pricing\PriceInfo\Factory as PriceInfoFactory; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Store\Api\Data\StoreInterface; /** @@ -30,13 +31,21 @@ class Price implements ResolverInterface */ private $priceInfoFactory; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param PriceInfoFactory $priceInfoFactory + * @param PriceCurrency $priceCurrency */ public function __construct( - PriceInfoFactory $priceInfoFactory + PriceInfoFactory $priceInfoFactory, + PriceCurrency $priceCurrency ) { $this->priceInfoFactory = $priceInfoFactory; + $this->priceCurrency = $priceCurrency; } /** @@ -104,7 +113,8 @@ private function createAdjustmentsArray(array $adjustments, AmountInterface $amo $priceArray = [ 'amount' => [ 'value' => $amount->getValue(), - 'currency' => $store->getCurrentCurrencyCode() + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($amount->getValue(),false,null,null,$store->getCurrentCurrencyCode()) ], 'adjustments' => [] ]; @@ -116,6 +126,7 @@ private function createAdjustmentsArray(array $adjustments, AmountInterface $amo 'amount' => [ 'value' => $amount->getAdjustmentAmount($adjustmentCode), 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($amount->getAdjustmentAmount($adjustmentCode),false,null,null,$store->getCurrentCurrencyCode()), ], 'description' => $adjustment->isIncludedInDisplayPrice() ? 'INCLUDED' : 'EXCLUDED' diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php index 805571d58d634..5b2692413f074 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php @@ -16,6 +16,7 @@ use Magento\Catalog\Model\Product; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Pricing\SaleableInterface; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Store\Api\Data\StoreInterface; /** @@ -33,14 +34,21 @@ class PriceRange implements ResolverInterface */ private $priceProviderPool; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param PriceProviderPool $priceProviderPool * @param Discount $discount + * @param PriceCurrency $priceCurrency */ - public function __construct(PriceProviderPool $priceProviderPool, Discount $discount) + public function __construct(PriceProviderPool $priceProviderPool, Discount $discount, PriceCurrency $priceCurrency) { $this->priceProviderPool = $priceProviderPool; $this->discount = $discount; + $this->priceCurrency = $priceCurrency; } /** @@ -124,11 +132,13 @@ private function formatPrice(float $regularPrice, float $finalPrice, StoreInterf return [ 'regular_price' => [ 'value' => $regularPrice, - 'currency' => $store->getCurrentCurrencyCode() + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($regularPrice,false,null,null,$store->getCurrentCurrencyCode()) ], 'final_price' => [ 'value' => $finalPrice, - 'currency' => $store->getCurrentCurrencyCode() + 'currency' => $store->getCurrentCurrencyCode(), + 'formatted' => $this->priceCurrency->format($finalPrice,false,null,null,$store->getCurrentCurrencyCode()) ], 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), ]; @@ -159,11 +169,13 @@ private function formatEmptyResult(): array return [ 'regular_price' => [ 'value' => null, - 'currency' => null + 'currency' => null, + 'formatted' => null ], 'final_price' => [ 'value' => null, - 'currency' => null + 'currency' => null, + 'formatted' => null ], 'discount' => null ]; diff --git a/app/code/Magento/GraphQl/etc/schema.graphqls b/app/code/Magento/GraphQl/etc/schema.graphqls index 65280ad1ad2aa..f41d17f113455 100644 --- a/app/code/Magento/GraphQl/etc/schema.graphqls +++ b/app/code/Magento/GraphQl/etc/schema.graphqls @@ -103,6 +103,7 @@ type ComplexTextValue { type Money @doc(description: "A Money object defines a monetary value, including a numeric value and a currency code.") { value: Float @doc(description: "A number expressing a monetary value") currency: CurrencyEnum @doc(description: "A three-letter currency code, such as USD or EUR") + formatted: String @doc(description: "Formatted price with currency symbol and floating point precision") } enum CurrencyEnum @doc(description: "The list of available currency codes") { diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php index d4ced5b8b97b0..9b76a71e98db5 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Quote\Model\Cart\Totals; use Magento\Quote\Model\Quote\Item; use Magento\Quote\Model\Quote\TotalsCollector; @@ -30,13 +31,21 @@ class CartItemPrices implements ResolverInterface */ private $totals; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param TotalsCollector $totalsCollector + * @param PriceCurrency $priceCurrency */ public function __construct( - TotalsCollector $totalsCollector + TotalsCollector $totalsCollector, + PriceCurrency $priceCurrency ) { $this->totalsCollector = $totalsCollector; + $this->priceCurrency = $priceCurrency; } /** @@ -61,18 +70,22 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value 'price' => [ 'currency' => $currencyCode, 'value' => $cartItem->getCalculationPrice(), + 'formatted' => $this->priceCurrency->format($cartItem->getCalculationPrice(),false,null,null,$currencyCode), ], 'row_total' => [ 'currency' => $currencyCode, 'value' => $cartItem->getRowTotal(), + 'formatted' => $this->priceCurrency->format($cartItem->getRowTotal(),false,null,null,$currencyCode), ], 'row_total_including_tax' => [ 'currency' => $currencyCode, 'value' => $cartItem->getRowTotalInclTax(), + 'formatted' => $this->priceCurrency->format($cartItem->getRowTotalInclTax(),false,null,null,$currencyCode), ], 'total_item_discount' => [ 'currency' => $currencyCode, 'value' => $cartItem->getDiscountAmount(), + 'formatted' => $this->priceCurrency->format($cartItem->getDiscountAmount(),false,null,null,$currencyCode), ], 'discounts' => $this->getDiscountValues($cartItem, $currencyCode) ]; @@ -99,6 +112,7 @@ private function getDiscountValues($cartItem, $currencyCode) $discount['label'] = $value->getRuleLabel() ?: __('Discount'); $amount['value'] = $discountAmount; $amount['currency'] = $currencyCode; + $amount['formatted']= $this->priceCurrency->format($discountAmount,false,null,null,$currencyCode); $discount['amount'] = $amount; $discountValues[] = $discount; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php index 66cc9ed11ed9f..a35e2103fe14e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Quote\Model\Quote; use Magento\Quote\Model\Quote\Address\Total; use Magento\Quote\Model\Quote\TotalsCollector; @@ -25,13 +26,21 @@ class CartPrices implements ResolverInterface */ private $totalsCollector; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param TotalsCollector $totalsCollector + * @param PriceCurrency $priceCurrency */ public function __construct( - TotalsCollector $totalsCollector + TotalsCollector $totalsCollector, + PriceCurrency $priceCurrency ) { $this->totalsCollector = $totalsCollector; + $this->priceCurrency = $priceCurrency; } /** @@ -55,11 +64,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $currency = $quote->getQuoteCurrencyCode(); return [ - 'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency], - 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency], - 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency], + 'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($cartTotals->getGrandTotal(),false,null,null,$currency)], + 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($cartTotals->getSubtotalInclTax(),false,null,null,$currency)], + 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($cartTotals->getSubtotal(),false,null,null,$currency)], 'subtotal_with_discount_excluding_tax' => [ - 'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency + 'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($cartTotals->getSubtotalWithDiscount(),false,null,null,$currency) ], 'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency), 'discount' => $this->getDiscount($cartTotals, $currency), @@ -86,7 +95,7 @@ private function getAppliedTaxes(Total $total, string $currency): array foreach ($appliedTaxes as $appliedTax) { $appliedTaxesData[] = [ 'label' => $appliedTax['id'], - 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency] + 'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency, 'formatted' => $this->priceCurrency->format($appliedTax['amount'],false,null,null,$currency)] ]; } return $appliedTaxesData; @@ -106,7 +115,7 @@ private function getDiscount(Total $total, string $currency) } return [ 'label' => explode(', ', $total->getDiscountDescription()), - 'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency] + 'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($total->getDiscountAmount(),false,null,null,$currency)] ]; } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php index 703015fd7ddb1..9ca57c5e001ae 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/Discounts.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Quote\Model\Quote; /** @@ -18,6 +19,19 @@ */ class Discounts implements ResolverInterface { + /** + * @var PriceCurrency + */ + private $priceCurrency; + + /** + * Discounts constructor. + * @param PriceCurrency $priceCurrency + */ + public function __construct(PriceCurrency $priceCurrency){ + $this->priceCurrency = $priceCurrency; + } + /** * @inheritdoc */ @@ -51,6 +65,7 @@ private function getDiscountValues(Quote $quote) $discountData = $value->getDiscountData(); $amount['value'] = $discountData->getAmount(); $amount['currency'] = $quote->getQuoteCurrencyCode(); + $amount['formatted']= $this->priceCurrency->format($discountData->getAmount(),false,null,null,$quote->getQuoteCurrencyCode()); $discount['amount'] = $amount; $discountValues[] = $discount; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php index 74513382df821..e6c175273fe4b 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php @@ -12,6 +12,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Quote\Api\Data\ShippingMethodInterface; use Magento\Quote\Model\Cart\ShippingMethodConverter; use Magento\Quote\Model\Quote\TotalsCollector; @@ -36,19 +37,27 @@ class AvailableShippingMethods implements ResolverInterface */ private $totalsCollector; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param ExtensibleDataObjectConverter $dataObjectConverter * @param ShippingMethodConverter $shippingMethodConverter * @param TotalsCollector $totalsCollector + * @param PriceCurrency $priceCurrency */ public function __construct( ExtensibleDataObjectConverter $dataObjectConverter, ShippingMethodConverter $shippingMethodConverter, - TotalsCollector $totalsCollector + TotalsCollector $totalsCollector, + PriceCurrency $priceCurrency ) { $this->dataObjectConverter = $dataObjectConverter; $this->shippingMethodConverter = $shippingMethodConverter; $this->totalsCollector = $totalsCollector; + $this->priceCurrency = $priceCurrency; } /** @@ -98,18 +107,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array { if (isset($data['amount'])) { - $data['amount'] = ['value' => $data['amount'], 'currency' => $quoteCurrencyCode]; + $data['amount'] = ['value' => $data['amount'], 'currency' => $quoteCurrencyCode, 'formatted' => $this->priceCurrency->format($data['amount'],false,null,null,$quoteCurrencyCode)]; } /** @deprecated The field should not be used on the storefront */ $data['base_amount'] = null; if (isset($data['price_excl_tax'])) { - $data['price_excl_tax'] = ['value' => $data['price_excl_tax'], 'currency' => $quoteCurrencyCode]; + $data['price_excl_tax'] = ['value' => $data['price_excl_tax'], 'currency' => $quoteCurrencyCode, 'formatted' => $this->priceCurrency->format($data['price_excl_tax'],false,null,null,$quoteCurrencyCode)]; } if (isset($data['price_incl_tax'])) { - $data['price_incl_tax'] = ['value' => $data['price_incl_tax'], 'currency' => $quoteCurrencyCode]; + $data['price_incl_tax'] = ['value' => $data['price_incl_tax'], 'currency' => $quoteCurrencyCode, 'formatted' => $this->priceCurrency->format($data['price_incl_tax'],false,null,null,$quoteCurrencyCode)]; } return $data; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php index 429fda816efd3..0b866b948ac20 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php @@ -12,6 +12,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Quote\Model\Quote\Address; use Magento\Quote\Model\Quote\Address\Rate; @@ -20,6 +21,19 @@ */ class SelectedShippingMethod implements ResolverInterface { + /** + * @var PriceCurrency + */ + private $priceCurrency; + + /** + * Discounts constructor. + * @param PriceCurrency $priceCurrency + */ + public function __construct(PriceCurrency $priceCurrency){ + $this->priceCurrency = $priceCurrency; + } + /** * @inheritdoc */ @@ -57,6 +71,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value 'amount' => [ 'value' => $address->getShippingAmount(), 'currency' => $address->getQuote()->getQuoteCurrencyCode(), + 'formatted' => $this->priceCurrency->format($address->getShippingAmount(),false,null,null,$address->getQuote()->getQuoteCurrencyCode()), ], /** @deprecated The field should not be used on the storefront */ 'base_amount' => null, diff --git a/app/code/Magento/SalesGraphQl/Model/OrderItem/DataProvider.php b/app/code/Magento/SalesGraphQl/Model/OrderItem/DataProvider.php index a69d9bf58ee8d..b1d6b5940ee0b 100644 --- a/app/code/Magento/SalesGraphQl/Model/OrderItem/DataProvider.php +++ b/app/code/Magento/SalesGraphQl/Model/OrderItem/DataProvider.php @@ -10,6 +10,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\OrderItemInterface; use Magento\Sales\Api\OrderItemRepositoryInterface; @@ -45,6 +46,11 @@ class DataProvider */ private $optionsProcessor; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @var int[] */ @@ -61,19 +67,22 @@ class DataProvider * @param OrderRepositoryInterface $orderRepository * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param OptionsProcessor $optionsProcessor + * @param PriceCurrency $priceCurrency */ public function __construct( OrderItemRepositoryInterface $orderItemRepository, ProductRepositoryInterface $productRepository, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $searchCriteriaBuilder, - OptionsProcessor $optionsProcessor + OptionsProcessor $optionsProcessor, + PriceCurrency $priceCurrency ) { $this->orderItemRepository = $orderItemRepository; $this->productRepository = $productRepository; $this->orderRepository = $orderRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->optionsProcessor = $optionsProcessor; + $this->priceCurrency = $priceCurrency; } /** @@ -141,7 +150,8 @@ private function fetch() 'discounts' => $this->getDiscountDetails($associatedOrder, $orderItem), 'product_sale_price' => [ 'value' => $orderItem->getPrice(), - 'currency' => $associatedOrder->getOrderCurrencyCode() + 'currency' => $associatedOrder->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($orderItem->getPrice(),false,null,null,$associatedOrder->getOrderCurrencyCode()) ], 'selected_options' => $itemOptions['selected_options'], 'entered_options' => $itemOptions['entered_options'], @@ -228,7 +238,8 @@ private function getDiscountDetails(OrderInterface $associatedOrder, OrderItemIn 'label' => $associatedOrder->getDiscountDescription() ?? __('Discount'), 'amount' => [ 'value' => abs($orderItem->getDiscountAmount()) ?? 0, - 'currency' => $associatedOrder->getOrderCurrencyCode() + 'currency' => $associatedOrder->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($orderItem->getDiscountAmount()) ?? 0,false,null,null,$associatedOrder->getOrderCurrencyCode()) ] ]; } diff --git a/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoItems.php b/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoItems.php index e1cee27e93f87..736938c26f69a 100644 --- a/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoItems.php +++ b/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoItems.php @@ -12,6 +12,7 @@ use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\CreditmemoInterface; use Magento\Sales\Api\Data\CreditmemoItemInterface; use Magento\Sales\Api\Data\OrderInterface; @@ -33,16 +34,24 @@ class CreditMemoItems implements ResolverInterface */ private $orderItemProvider; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param ValueFactory $valueFactory * @param OrderItemProvider $orderItemProvider + * @param PriceCurrency $priceCurrency */ public function __construct( ValueFactory $valueFactory, - OrderItemProvider $orderItemProvider + OrderItemProvider $orderItemProvider, + PriceCurrency $priceCurrency ) { $this->valueFactory = $valueFactory; $this->orderItemProvider = $orderItemProvider; + $this->priceCurrency = $priceCurrency; } /** @@ -119,7 +128,8 @@ private function getCreditMemoItemData(OrderInterface $order, CreditmemoItemInte 'product_sku' => $creditMemoItem->getSku(), 'product_sale_price' => [ 'value' => $creditMemoItem->getPrice(), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($creditMemoItem->getPrice(),false,null,null,$order->getOrderCurrencyCode()) ], 'quantity_refunded' => $creditMemoItem->getQty(), 'model' => $creditMemoItem, @@ -149,7 +159,8 @@ private function formatDiscountDetails( 'label' => $associatedOrder->getDiscountDescription() ?? _('Discount'), 'amount' => [ 'value' => abs($creditmemoItem->getDiscountAmount()) ?? 0, - 'currency' => $associatedOrder->getOrderCurrencyCode() + 'currency' => $associatedOrder->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($creditmemoItem->getDiscountAmount()) ?? 0,false,null,null,$associatedOrder->getOrderCurrencyCode()) ] ]; } diff --git a/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoTotal.php b/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoTotal.php index 5a8f4f7f17ce6..8f958e19aa106 100644 --- a/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoTotal.php +++ b/app/code/Magento/SalesGraphQl/Model/Resolver/CreditMemo/CreditMemoTotal.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\CreditmemoInterface; use Magento\Sales\Api\Data\OrderInterface; use Magento\SalesGraphQl\Model\SalesItem\ShippingTaxCalculator; @@ -36,19 +37,28 @@ class CreditMemoTotal implements ResolverInterface * @var ShippingTaxCalculator */ private $shippingTaxCalculator; + + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param OrderTaxManagementInterface $orderTaxManagement * @param TaxHelper $taxHelper * @param ShippingTaxCalculator $shippingTaxCalculator + * @param PriceCurrency $priceCurrency */ public function __construct( OrderTaxManagementInterface $orderTaxManagement, TaxHelper $taxHelper, - ShippingTaxCalculator $shippingTaxCalculator + ShippingTaxCalculator $shippingTaxCalculator, + PriceCurrency $priceCurrency ) { $this->taxHelper = $taxHelper; $this->orderTaxManagement = $orderTaxManagement; $this->shippingTaxCalculator = $shippingTaxCalculator; + $this->priceCurrency = $priceCurrency; } /** @@ -76,11 +86,11 @@ public function resolve( $currency = $orderModel->getOrderCurrencyCode(); $baseCurrency = $orderModel->getBaseCurrencyCode(); return [ - 'base_grand_total' => ['value' => $creditMemo->getBaseGrandTotal(), 'currency' => $baseCurrency], - 'grand_total' => ['value' => $creditMemo->getGrandTotal(), 'currency' => $currency], - 'subtotal' => ['value' => $creditMemo->getSubtotal(), 'currency' => $currency], - 'total_tax' => ['value' => $creditMemo->getTaxAmount(), 'currency' => $currency], - 'total_shipping' => ['value' => $creditMemo->getShippingAmount(), 'currency' => $currency], + 'base_grand_total' => ['value' => $creditMemo->getBaseGrandTotal(), 'currency' => $baseCurrency, 'formatted' => $this->priceCurrency->format($creditMemo->getBaseGrandTotal(),false,null,null,$baseCurrency)], + 'grand_total' => ['value' => $creditMemo->getGrandTotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($creditMemo->getGrandTotal(),false,null,null,$currency)], + 'subtotal' => ['value' => $creditMemo->getSubtotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($creditMemo->getSubtotal(),false,null,null,$currency)], + 'total_tax' => ['value' => $creditMemo->getTaxAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($creditMemo->getTaxAmount(),false,null,null,$currency)], + 'total_shipping' => ['value' => $creditMemo->getShippingAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($creditMemo->getShippingAmount(),false,null,null,$currency)], 'discounts' => $this->getDiscountDetails($creditMemo), 'taxes' => $this->formatTaxes( $orderModel, @@ -89,15 +99,18 @@ public function resolve( 'shipping_handling' => [ 'amount_excluding_tax' => [ 'value' => $creditMemo->getShippingAmount() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($creditMemo->getShippingAmount() ?? 0,false,null,null,$currency) ], 'amount_including_tax' => [ 'value' => $creditMemo->getShippingInclTax() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($creditMemo->getShippingInclTax() ?? 0,false,null,null,$currency) ], 'total_amount' => [ 'value' => $creditMemo->getShippingAmount() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($creditMemo->getShippingAmount() ?? 0,false,null,null,$currency) ], 'discounts' => $this->getShippingDiscountDetails($creditMemo, $orderModel), 'taxes' => $this->formatTaxes( @@ -107,7 +120,8 @@ public function resolve( ], 'adjustment' => [ 'value' => abs($creditMemo->getAdjustment()), - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format(abs($creditMemo->getAdjustment()),false,null,null,$currency) ] ]; } @@ -133,7 +147,8 @@ private function getShippingDiscountDetails(CreditmemoInterface $creditmemoModel $shippingDiscounts[] = [ 'amount' => [ 'value' => sprintf('%.2f', abs($calculatedCreditmemoShippingDiscount)), - 'currency' => $creditmemoModel->getOrderCurrencyCode() + 'currency' => $creditmemoModel->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(sprintf('%.2f', abs($calculatedCreditmemoShippingDiscount)),false,null,null,$creditmemoModel->getOrderCurrencyCode()) ] ]; } @@ -154,7 +169,8 @@ private function getDiscountDetails(CreditmemoInterface $creditmemo): array 'label' => $creditmemo->getDiscountDescription() ?? __('Discount'), 'amount' => [ 'value' => abs($creditmemo->getDiscountAmount()), - 'currency' => $creditmemo->getOrderCurrencyCode() + 'currency' => $creditmemo->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($creditmemo->getDiscountAmount()),false,null,null,$creditmemo->getOrderCurrencyCode()) ] ]; } @@ -177,7 +193,8 @@ private function formatTaxes(OrderInterface $order, array $appliedTaxes): array 'title' => $appliedTax['title'] ?? null, 'amount' => [ 'value' => $appliedTax['tax_amount'] ?? 0, - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($appliedTax['tax_amount'] ?? 0,false,null,null,$order->getOrderCurrencyCode()) ] ]; $taxes[] = $appliedTaxesArray; diff --git a/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceItems.php b/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceItems.php index 1e9d282d80d94..a9a77980d3d01 100644 --- a/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceItems.php +++ b/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceItems.php @@ -12,6 +12,7 @@ use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\InvoiceInterface; use Magento\Sales\Api\Data\InvoiceItemInterface; use Magento\Sales\Api\Data\OrderInterface; @@ -33,16 +34,24 @@ class InvoiceItems implements ResolverInterface */ private $orderItemProvider; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param ValueFactory $valueFactory * @param OrderItemProvider $orderItemProvider + * @param PriceCurrency $priceCurrency */ public function __construct( ValueFactory $valueFactory, - OrderItemProvider $orderItemProvider + OrderItemProvider $orderItemProvider, + PriceCurrency $priceCurrency ) { $this->valueFactory = $valueFactory; $this->orderItemProvider = $orderItemProvider; + $this->priceCurrency = $priceCurrency; } /** @@ -118,7 +127,8 @@ private function getInvoiceItemData(OrderInterface $order, InvoiceItemInterface 'product_sku' => $invoiceItem->getSku(), 'product_sale_price' => [ 'value' => $invoiceItem->getPrice(), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($invoiceItem->getPrice(),false,null,null,$order->getOrderCurrencyCode()) ], 'quantity_invoiced' => $invoiceItem->getQty(), 'model' => $invoiceItem, @@ -147,7 +157,8 @@ private function formatDiscountDetails(OrderInterface $associatedOrder, InvoiceI 'label' => $associatedOrder->getDiscountDescription() ?? _('Discount'), 'amount' => [ 'value' => abs($invoiceItem->getDiscountAmount()) ?? 0, - 'currency' => $associatedOrder->getOrderCurrencyCode() + 'currency' => $associatedOrder->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($invoiceItem->getDiscountAmount()) ?? 0,false,null,null,$associatedOrder->getOrderCurrencyCode()) ] ]; } diff --git a/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceTotal.php b/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceTotal.php index b77fda9523843..dd4098b85e38a 100644 --- a/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceTotal.php +++ b/app/code/Magento/SalesGraphQl/Model/Resolver/Invoice/InvoiceTotal.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\InvoiceInterface; use Magento\Sales\Api\Data\OrderInterface; use Magento\Tax\Api\OrderTaxManagementInterface; @@ -37,19 +38,27 @@ class InvoiceTotal implements ResolverInterface */ private $shippingTaxCalculator; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param OrderTaxManagementInterface $orderTaxManagement * @param TaxHelper $taxHelper * @param ShippingTaxCalculator $shippingTaxCalculator + * @param PriceCurrency $priceCurrency */ public function __construct( OrderTaxManagementInterface $orderTaxManagement, TaxHelper $taxHelper, - ShippingTaxCalculator $shippingTaxCalculator + ShippingTaxCalculator $shippingTaxCalculator, + PriceCurrency $priceCurrency ) { $this->taxHelper = $taxHelper; $this->orderTaxManagement = $orderTaxManagement; $this->shippingTaxCalculator = $shippingTaxCalculator; + $this->priceCurrency = $priceCurrency; } /** @@ -77,11 +86,11 @@ public function resolve( $currency = $orderModel->getOrderCurrencyCode(); $baseCurrency = $orderModel->getBaseCurrencyCode(); return [ - 'base_grand_total' => ['value' => $invoiceModel->getBaseGrandTotal(), 'currency' => $baseCurrency], - 'grand_total' => ['value' => $invoiceModel->getGrandTotal(), 'currency' => $currency], - 'subtotal' => ['value' => $invoiceModel->getSubtotal(), 'currency' => $currency], - 'total_tax' => ['value' => $invoiceModel->getTaxAmount(), 'currency' => $currency], - 'total_shipping' => ['value' => $invoiceModel->getShippingAmount(), 'currency' => $currency], + 'base_grand_total' => ['value' => $invoiceModel->getBaseGrandTotal(), 'currency' => $baseCurrency, 'formatted' => $this->priceCurrency->format($invoiceModel->getBaseGrandTotal(),false,null,null,$baseCurrency)], + 'grand_total' => ['value' => $invoiceModel->getGrandTotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($invoiceModel->getGrandTotal(),false,null,null,$currency)], + 'subtotal' => ['value' => $invoiceModel->getSubtotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($invoiceModel->getSubTotal(),false,null,null,$currency)], + 'total_tax' => ['value' => $invoiceModel->getTaxAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($invoiceModel->getTaxAmount(),false,null,null,$currency)], + 'total_shipping' => ['value' => $invoiceModel->getShippingAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($invoiceModel->getShippingAmount(),false,null,null,$currency)], 'discounts' => $this->getDiscountDetails($invoiceModel), 'taxes' => $this->formatTaxes( $orderModel, @@ -90,15 +99,18 @@ public function resolve( 'shipping_handling' => [ 'amount_excluding_tax' => [ 'value' => $invoiceModel->getShippingAmount() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($invoiceModel->getShippingAmount() ?? 0,false,null,null,$currency) ], 'amount_including_tax' => [ 'value' => $invoiceModel->getShippingInclTax() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($invoiceModel->getShippingInclTax() ?? 0,false,null,null,$currency) ], 'total_amount' => [ 'value' => $invoiceModel->getShippingAmount() ?? 0, - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($invoiceModel->getShippingAmount() ?? 0,false,null,null,$currency) ], 'discounts' => $this->getShippingDiscountDetails($invoiceModel, $orderModel), 'taxes' => $this->formatTaxes( @@ -130,7 +142,8 @@ private function getShippingDiscountDetails(InvoiceInterface $invoiceModel, Orde [ 'amount' => [ 'value' => sprintf('%.2f', abs($calculatedInvoiceShippingDiscount)), - 'currency' => $invoiceModel->getOrderCurrencyCode() + 'currency' => $invoiceModel->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(sprintf('%.2f', abs($calculatedInvoiceShippingDiscount)),false,null,null,$invoiceModel->getOrderCurrencyCode()) ] ]; } @@ -151,7 +164,8 @@ private function getDiscountDetails(InvoiceInterface $invoice): array 'label' => $invoice->getDiscountDescription() ?? __('Discount'), 'amount' => [ 'value' => abs($invoice->getDiscountAmount()), - 'currency' => $invoice->getOrderCurrencyCode() + 'currency' => $invoice->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($invoice->getDiscountAmount()),false,null,null,$invoice->getOrderCurrencyCode()) ] ]; } @@ -174,7 +188,8 @@ private function formatTaxes(OrderInterface $order, array $appliedTaxes): array 'title' => $appliedTax['title'] ?? null, 'amount' => [ 'value' => $appliedTax['tax_amount'] ?? 0, - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($appliedTax['tax_amount'] ?? 0,false,null,null,$order->getOrderCurrencyCode()) ] ]; $taxes[] = $appliedTaxesArray; diff --git a/app/code/Magento/SalesGraphQl/Model/Resolver/OrderTotal.php b/app/code/Magento/SalesGraphQl/Model/Resolver/OrderTotal.php index ab3ace45f336c..e2d9b5e178d54 100644 --- a/app/code/Magento/SalesGraphQl/Model/Resolver/OrderTotal.php +++ b/app/code/Magento/SalesGraphQl/Model/Resolver/OrderTotal.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\OrderInterface; /** @@ -18,6 +19,20 @@ */ class OrderTotal implements ResolverInterface { + /** + * @var PriceCurrency + */ + private $priceCurrency; + + /** + * OrderTotal constructor. + * @param PriceCurrency $priceCurrency + */ + public function __construct(PriceCurrency $priceCurrency) + { + $this->priceCurrency = $priceCurrency; + } + /** * @inheritDoc */ @@ -38,25 +53,28 @@ public function resolve( $baseCurrency = $order->getBaseCurrencyCode(); return [ - 'base_grand_total' => ['value' => $order->getBaseGrandTotal(), 'currency' => $baseCurrency], - 'grand_total' => ['value' => $order->getGrandTotal(), 'currency' => $currency], - 'subtotal' => ['value' => $order->getSubtotal(), 'currency' => $currency], - 'total_tax' => ['value' => $order->getTaxAmount(), 'currency' => $currency], + 'base_grand_total' => ['value' => $order->getBaseGrandTotal(), 'currency' => $baseCurrency, 'formatted' => $this->priceCurrency->format($order->getBaseGrandTotal(),false,null,null,$baseCurrency)], + 'grand_total' => ['value' => $order->getGrandTotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($order->getGrandTotal(),false,null,null,$currency)], + 'subtotal' => ['value' => $order->getSubtotal(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($order->getSubtotal(),false,null,null,$currency)], + 'total_tax' => ['value' => $order->getTaxAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($order->getTaxAmount(),false,null,null,$currency)], 'taxes' => $this->getAppliedTaxesDetails($order), 'discounts' => $this->getDiscountDetails($order), - 'total_shipping' => ['value' => $order->getShippingAmount(), 'currency' => $currency], + 'total_shipping' => ['value' => $order->getShippingAmount(), 'currency' => $currency, 'formatted' => $this->priceCurrency->format($order->getShippingAmount(),false,null,null,$currency)], 'shipping_handling' => [ 'amount_excluding_tax' => [ 'value' => $order->getShippingAmount(), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($order->getShippingAmount(),false,null,null,$order->getOrderCurrencyCode()) ], 'amount_including_tax' => [ 'value' => $order->getShippingInclTax(), - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($order->getShippingInclTax(),false,null,null,$currency) ], 'total_amount' => [ 'value' => $order->getShippingAmount(), - 'currency' => $currency + 'currency' => $currency, + 'formatted' => $this->priceCurrency->format($order->getShippingAmount(),false,null,null,$currency) ], 'taxes' => $this->getAppliedShippingTaxesDetails($order), 'discounts' => $this->getShippingDiscountDetails($order), @@ -101,7 +119,8 @@ private function getAppliedTaxesDetails(OrderInterface $order): array 'title' => $appliedTaxes['title'] ?? null, 'amount' => [ 'value' => $appliedTaxes['amount'] ?? 0, - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($appliedTaxes['amount'] ?? 0,false,null,null,$order->getOrderCurrencyCode()) ] ]; $taxes[] = $appliedTaxesArray; @@ -123,7 +142,8 @@ private function getDiscountDetails(OrderInterface $order): array 'label' => $order->getDiscountDescription() ?? __('Discount'), 'amount' => [ 'value' => abs($order->getDiscountAmount()), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($order->getDiscountAmount()),false,null,null,$order->getOrderCurrencyCode()) ] ]; } @@ -173,7 +193,8 @@ private function getAppliedShippingTaxesDetails( 'title' => $appliedShippingTaxes['title'] ?? null, 'amount' => [ 'value' => $appliedShippingTaxes['amount'] ?? 0, - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($appliedShippingTaxes['amount'] ?? 0,false,null,null,$order->getOrderCurrencyCode()) ] ]; $shippingTaxes[] = $appliedShippingTaxesArray; @@ -196,7 +217,8 @@ private function getShippingDiscountDetails(OrderInterface $order): array 'label' => $order->getDiscountDescription() ?? __('Discount'), 'amount' => [ 'value' => abs($order->getShippingDiscountAmount()), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format(abs($order->getShippingDiscountAmount()),false,null,null,$order->getOrderCurrencyCode()) ] ]; } diff --git a/app/code/Magento/SalesGraphQl/Model/Shipment/Item/ShipmentItemFormatter.php b/app/code/Magento/SalesGraphQl/Model/Shipment/Item/ShipmentItemFormatter.php index e8ba6e5f784ec..e0c15f0ee2f57 100644 --- a/app/code/Magento/SalesGraphQl/Model/Shipment/Item/ShipmentItemFormatter.php +++ b/app/code/Magento/SalesGraphQl/Model/Shipment/Item/ShipmentItemFormatter.php @@ -7,6 +7,7 @@ namespace Magento\SalesGraphQl\Model\Shipment\Item; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\Data\ShipmentItemInterface; @@ -15,6 +16,20 @@ */ class ShipmentItemFormatter implements FormatterInterface { + /** + * @var PriceCurrency + */ + private $priceCurrency; + + /** + * ShipmentItemFormatter constructor. + * @param PriceCurrency $priceCurrency + */ + public function __construct(PriceCurrency $priceCurrency) + { + $this->priceCurrency = $priceCurrency; + } + /** * @inheritDoc */ @@ -27,7 +42,8 @@ public function formatShipmentItem(ShipmentInterface $shipment, ShipmentItemInte 'product_sku' => $item->getSku(), 'product_sale_price' => [ 'value' => $item->getPrice(), - 'currency' => $order->getOrderCurrencyCode() + 'currency' => $order->getOrderCurrencyCode(), + 'formatted' => $this->priceCurrency->format($item->getPrice(),false,null,null,$order->getOrderCurrencyCode()) ], 'product_type' => $item->getOrderItem()->getProductType(), 'quantity_shipped' => $item->getQty(), diff --git a/app/code/Magento/WeeeGraphQl/Model/Resolver/FixedProductTax.php b/app/code/Magento/WeeeGraphQl/Model/Resolver/FixedProductTax.php index 98164c18e858f..7f97d2829cf74 100644 --- a/app/code/Magento/WeeeGraphQl/Model/Resolver/FixedProductTax.php +++ b/app/code/Magento/WeeeGraphQl/Model/Resolver/FixedProductTax.php @@ -10,6 +10,7 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; +use Magento\NegotiableQuote\Model\PriceCurrency; use Magento\Weee\Helper\Data; use Magento\Framework\Exception\LocalizedException; use Magento\Tax\Helper\Data as TaxHelper; @@ -31,14 +32,21 @@ class FixedProductTax implements ResolverInterface */ private $taxHelper; + /** + * @var PriceCurrency + */ + private $priceCurrency; + /** * @param Data $weeeHelper * @param TaxHelper $taxHelper + * @param PriceCurrency $priceCurrency */ - public function __construct(Data $weeeHelper, TaxHelper $taxHelper) + public function __construct(Data $weeeHelper, TaxHelper $taxHelper, PriceCurrency $priceCurrency) { $this->weeeHelper = $weeeHelper; $this->taxHelper = $taxHelper; + $this->priceCurrency = $priceCurrency; } /** @@ -76,6 +84,7 @@ public function resolve( 'amount' => [ 'value' => $amount, 'currency' => $value['final_price']['currency'], + 'formatted' => $this->priceCurrency->format($amount,false,null,null,$value['final_price']['currency']), ], 'label' => $attribute->getData('name') ];