Skip to content

Commit

Permalink
Currency Type feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarchenko committed Jan 17, 2024
1 parent 8647be6 commit 584ae3f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
11 changes: 11 additions & 0 deletions app/code/Morfdev/Freshdesk/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
*/
Expand Down
27 changes: 18 additions & 9 deletions app/code/Morfdev/Freshdesk/Model/Management/OrderRecent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,6 +87,9 @@ class OrderRecent implements OrderRecentManagementInterface
/** @var CountryFactory */
protected $countryFactory;

/** @var Config */
protected $config;

/**
* @param OrderRepositoryInterface $orderRepository
* @param OrderItemRepositoryInterface $orderItemRepository
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -144,6 +150,7 @@ public function __construct(
$this->orderCollectionFactory = $orderCollectionFactory;
$this->customerCollectionFactory = $customerCollectionFactory;
$this->countryFactory = $countryFactory;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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())
];
}

Expand Down Expand Up @@ -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
];
Expand Down
35 changes: 35 additions & 0 deletions app/code/Morfdev/Freshdesk/Model/Source/CurrencyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Morfdev\Freshdesk\Model\Source;

use Magento\Framework\Data\OptionSourceInterface;

class CurrencyType implements OptionSourceInterface
{
const BASE_CODE = 'base';
const STORE_CODE = 'store';

/**
* @return array
*/
public function getOptionArray()
{
return [
self::BASE_CODE => __("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;
}
}
2 changes: 1 addition & 1 deletion app/code/Morfdev/Freshdesk/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "morfdev/module-freshdesk",
"description": "N/A",
"type": "magento2-module",
"version": "1.0.2",
"version": "1.0.3",
"license": [
"proprietary"
],
Expand Down
6 changes: 5 additions & 1 deletion app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file
showInWebsite="0" showInStore="0">
<frontend_model>Morfdev\Freshdesk\Block\Adminhtml\System\Config\Field\GenerateButton</frontend_model>
</field>

<field id="currency_type" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Currency Type</label>
<source_model>Morfdev\Freshdesk\Model\Source\CurrencyType</source_model>
<comment>Select to change displayed currency on Freshworks side. By default used Base Currency</comment>
</field>
<field id="freshsales_destination_url" translate="label" type="label" sortOrder="30" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Freshsales Destination URL</label>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Morfdev/Freshdesk/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Morfdev_Freshdesk" setup_version="1.0.2"></module>
<module name="Morfdev_Freshdesk" setup_version="1.0.3"></module>
</config>

0 comments on commit 584ae3f

Please sign in to comment.