From 584ae3fd93fec3489a543a11761ca77cad67d93a Mon Sep 17 00:00:00 2001 From: Marchenko Denis Date: Wed, 17 Jan 2024 18:48:38 +0300 Subject: [PATCH] Currency Type feature --- app/code/Morfdev/Freshdesk/Model/Config.php | 11 ++++++ .../Model/Management/OrderRecent.php | 27 +++++++++----- .../Freshdesk/Model/Source/CurrencyType.php | 35 +++++++++++++++++++ app/code/Morfdev/Freshdesk/composer.json | 2 +- .../Freshdesk/etc/adminhtml/system.xml | 6 +++- app/code/Morfdev/Freshdesk/etc/module.xml | 2 +- 6 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 app/code/Morfdev/Freshdesk/Model/Source/CurrencyType.php diff --git a/app/code/Morfdev/Freshdesk/Model/Config.php b/app/code/Morfdev/Freshdesk/Model/Config.php index 2c88d66..896aa6f 100644 --- a/app/code/Morfdev/Freshdesk/Model/Config.php +++ b/app/code/Morfdev/Freshdesk/Model/Config.php @@ -6,6 +6,7 @@ class Config { const FRESHWORKS_CONFIG_API_TOKEN_PATH = 'morfdev_freshdesk/general/token'; + const FRESHWORKS_CONFIG_CURRENCY_TYPE_PATH = 'morfdev_freshdesk/general/currency_type'; const FRESHWORKS_CONFIG_FRESHDESK_DESTINATION_URL_PATH = 'morfdev_freshdesk/general/freshdesk_destination_url'; const FRESHWORKS_CONFIG_FRESHSALES_DESTINATION_URL_PATH = 'morfdev_freshdesk/general/freshsales_destination_url'; @@ -31,6 +32,16 @@ public function getApiTokenForDefault() ); } + /** + * @return string + */ + public function getCurrencyType() + { + return $this->scopeConfig->getValue( + self::FRESHWORKS_CONFIG_CURRENCY_TYPE_PATH + ); + } + /** * @return string|null */ diff --git a/app/code/Morfdev/Freshdesk/Model/Management/OrderRecent.php b/app/code/Morfdev/Freshdesk/Model/Management/OrderRecent.php index 3f09429..8260b1e 100644 --- a/app/code/Morfdev/Freshdesk/Model/Management/OrderRecent.php +++ b/app/code/Morfdev/Freshdesk/Model/Management/OrderRecent.php @@ -2,6 +2,7 @@ namespace Morfdev\Freshdesk\Model\Management; +use Morfdev\Freshdesk\Model\Config; use Magento\Framework\Api\SearchCriteriaBuilder; use Morfdev\Freshdesk\Api\OrderRecentManagementInterface; use Magento\Sales\Api\OrderRepositoryInterface; @@ -86,6 +87,9 @@ class OrderRecent implements OrderRecentManagementInterface /** @var CountryFactory */ protected $countryFactory; + /** @var Config */ + protected $config; + /** * @param OrderRepositoryInterface $orderRepository * @param OrderItemRepositoryInterface $orderItemRepository @@ -105,6 +109,7 @@ class OrderRecent implements OrderRecentManagementInterface * @param OrderAddressRepositoryInterface $addressRepository * @param CustomerCollectionFactory $customerCollectionFactory * @param CountryFactory $countryFactory + * @param Config $config */ public function __construct( OrderRepositoryInterface $orderRepository, @@ -124,7 +129,8 @@ public function __construct( ScopeConfigInterface $scopeConfig, OrderAddressRepositoryInterface $addressRepository, CustomerCollectionFactory $customerCollectionFactory, - CountryFactory $countryFactory + CountryFactory $countryFactory, + Config $config ) { $this->orderRepository = $orderRepository; $this->orderItemRepository = $orderItemRepository; @@ -144,6 +150,7 @@ public function __construct( $this->orderCollectionFactory = $orderCollectionFactory; $this->customerCollectionFactory = $customerCollectionFactory; $this->countryFactory = $countryFactory; + $this->config = $config; } /** @@ -236,6 +243,7 @@ public function getInfo($email, $scope) ->create(); $orderList = $this->orderRepository->getList($searchCriteria)->getItems(); $orderInfo = []; + $isBaseCurrencyType = $this->config->getCurrencyType() !== 'store'; /** @var OrderInterface $order */ foreach ($orderList as $order) { $billingAddress = $order->getBillingAddress(); @@ -250,7 +258,8 @@ public function getInfo($email, $scope) ->create(); $orderItemsList = $this->orderItemRepository->getList($searchCriteria)->getItems(); - $currency = $this->currency->getCurrency($order->getBaseCurrencyCode()); + $currencyCode = $isBaseCurrencyType ? $order->getBaseCurrencyCode() : $order->getOrderCurrencyCode(); + $currency = $this->currency->getCurrency($currencyCode); $orderItemInfo = []; /** @var OrderItemInterface $orderItem */ foreach ($orderItemsList as $orderItem) { @@ -266,12 +275,12 @@ public function getInfo($email, $scope) 'name' => $orderItem->getName(), 'product_html' => $renderer->toHtml(), 'sku' => $orderItem->getSku(), - 'price' => $currency->toCurrency($orderItem->getBasePrice()), + 'price' => $isBaseCurrencyType ? $currency->toCurrency($orderItem->getBasePrice()) : $currency->toCurrency($orderItem->getPrice()), 'ordered_qty' => (int)$orderItem->getQtyOrdered(), 'invoiced_qty' => (int)$orderItem->getQtyInvoiced(), 'shipped_qty' => (int)$orderItem->getQtyShipped(), 'refunded_qty' => (int)$orderItem->getQtyRefunded(), - 'row_total' => $currency->toCurrency($orderItem->getBaseRowTotal()) + 'row_total' => $isBaseCurrencyType ? $currency->toCurrency($orderItem->getBaseRowTotal()) : $currency->toCurrency($orderItem->getRowTotal()) ]; } @@ -348,11 +357,11 @@ public function getInfo($email, $scope) 'status' => $order->getStatusLabel(), 'state' => $order->getState(), 'totals' => [ - 'subtotal' => $currency->toCurrency($order->getBaseSubtotal()), - 'shipping' => $currency->toCurrency($order->getBaseShippingAmount()), - 'discount' => $currency->toCurrency($order->getBaseDiscountAmount()), - 'tax' => $currency->toCurrency($order->getBaseTaxAmount()), - 'grand_total' => $currency->toCurrency($order->getBaseGrandTotal()) + 'subtotal' => $isBaseCurrencyType ? $currency->toCurrency($order->getBaseSubtotal()) : $currency->toCurrency($order->getSubtotal()), + 'shipping' => $isBaseCurrencyType ? $currency->toCurrency($order->getBaseShippingAmount()) : $currency->toCurrency($order->getShippingAmount()), + 'discount' => $isBaseCurrencyType ? $currency->toCurrency($order->getBaseDiscountAmount()) : $currency->toCurrency($order->getDiscountAmount()), + 'tax' => $isBaseCurrencyType ? $currency->toCurrency($order->getBaseTaxAmount()) : $currency->toCurrency($order->getTaxAmount()), + 'grand_total' => $isBaseCurrencyType ? $currency->toCurrency($order->getBaseGrandTotal()) : $currency->toCurrency($order->getGrandTotal()) ], 'items' => $orderItemInfo ]; diff --git a/app/code/Morfdev/Freshdesk/Model/Source/CurrencyType.php b/app/code/Morfdev/Freshdesk/Model/Source/CurrencyType.php new file mode 100644 index 0000000..c2f8486 --- /dev/null +++ b/app/code/Morfdev/Freshdesk/Model/Source/CurrencyType.php @@ -0,0 +1,35 @@ + __("Base Currency"), + self::STORE_CODE => __("Store Currency") + ]; + } + + /** + * @return array + */ + public function toOptionArray() + { + $options = $this->getOptionArray(); + $result = []; + foreach ($options as $value => $label) { + $result[] = ['value' => $value, 'label' => $label]; + } + return $result; + } +} \ No newline at end of file diff --git a/app/code/Morfdev/Freshdesk/composer.json b/app/code/Morfdev/Freshdesk/composer.json index f48b065..a2258a2 100644 --- a/app/code/Morfdev/Freshdesk/composer.json +++ b/app/code/Morfdev/Freshdesk/composer.json @@ -2,7 +2,7 @@ "name": "morfdev/module-freshdesk", "description": "N/A", "type": "magento2-module", - "version": "1.0.2", + "version": "1.0.3", "license": [ "proprietary" ], diff --git a/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml b/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml index 8754f2c..6d42423 100644 --- a/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml +++ b/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml @@ -21,7 +21,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file showInWebsite="0" showInStore="0"> Morfdev\Freshdesk\Block\Adminhtml\System\Config\Field\GenerateButton - + + + Morfdev\Freshdesk\Model\Source\CurrencyType + Select to change displayed currency on Freshworks side. By default used Base Currency + diff --git a/app/code/Morfdev/Freshdesk/etc/module.xml b/app/code/Morfdev/Freshdesk/etc/module.xml index 6cbb6f8..bf1d983 100644 --- a/app/code/Morfdev/Freshdesk/etc/module.xml +++ b/app/code/Morfdev/Freshdesk/etc/module.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file