Skip to content

Commit

Permalink
Merge pull request #302 from FatchipRobert/MAG2-146-BaseCurrency
Browse files Browse the repository at this point in the history
MAG2-146 - Fixed getter of current scope
  • Loading branch information
fjbender committed Aug 21, 2020
2 parents 04ba379 + 7b9e7dd commit 72f39a7
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 41 deletions.
12 changes: 8 additions & 4 deletions Helper/Api.php
Expand Up @@ -278,7 +278,11 @@ public function addPayoneOrderData(SalesOrder $oOrder, $aRequest, $aResponse)
*/
public function isInvoiceDataNeeded(PayoneMethod $oPayment)
{
$blInvoiceEnabled = (bool)$this->getConfigParam('transmit_enabled', 'invoicing'); // invoicing enabled?
$sStoreCode = null;
if ($oPayment->getInfoInstance()->getOrder()) {
$sStoreCode = $oPayment->getInfoInstance()->getOrder()->getStore()->getCode();
}
$blInvoiceEnabled = (bool)$this->getConfigParam('transmit_enabled', 'invoicing', 'payone_general', $sStoreCode); // invoicing enabled?
if ($blInvoiceEnabled || $oPayment->needsProductInfo()) {
return true; // invoice data needed
}
Expand All @@ -294,7 +298,7 @@ public function isInvoiceDataNeeded(PayoneMethod $oPayment)
public function getCurrencyFromOrder(SalesOrder $oOrder)
{
$sCurrency = $oOrder->getBaseCurrencyCode();
if ($this->getConfigParam('currency') == 'display') {
if ($this->getConfigParam('currency', 'global', 'payone_general', $oOrder->getStore()->getCode()) == 'display') {
$sCurrency = $oOrder->getOrderCurrencyCode();
}
return $sCurrency;
Expand All @@ -309,7 +313,7 @@ public function getCurrencyFromOrder(SalesOrder $oOrder)
public function getCurrencyFromQuote(Quote $oQuote)
{
$sCurrency = $oQuote->getBaseCurrencyCode();
if ($this->getConfigParam('currency') == 'display') {
if ($this->getConfigParam('currency', 'global', 'payone_general', $oQuote->getStore()->getCode()) == 'display') {
$sCurrency = $oQuote->getQuoteCurrencyCode();
}
return $sCurrency;
Expand All @@ -324,7 +328,7 @@ public function getCurrencyFromQuote(Quote $oQuote)
public function getQuoteAmount(Quote $oQuote)
{
$dAmount = $oQuote->getBaseGrandTotal();
if ($this->getConfigParam('currency') == 'display') {
if ($this->getConfigParam('currency', 'global', 'payone_general', $oQuote->getStore()->getCode()) == 'display') {
$dAmount = $oQuote->getGrandTotal(); // send display amount instead of base amount
}
return $dAmount;
Expand Down
51 changes: 40 additions & 11 deletions Model/Api/Invoice.php
Expand Up @@ -85,6 +85,13 @@ class Invoice
*/
protected $oRequest;

/**
* Current store code
*
* @var string
*/
protected $sStoreCode;

/**
* Constructor
*
Expand Down Expand Up @@ -132,6 +139,7 @@ protected function addInvoicePosition($sId, $dPrice, $sItemType, $iAmount, $sDes
public function addProductInfo(Base $oRequest, $oOrder, $aPositions = false, $blDebit = false, $dShippingCosts = false)
{
$this->oRequest = $oRequest; // write request to property for manipulation of the object
$this->setStoreCode($oOrder->getStore()->getCode());
if ($oOrder instanceof Order) {
$sInvoiceAppendix = $this->toolkitHelper->getInvoiceAppendix($oOrder); // get invoice appendix
if (!empty($sInvoiceAppendix)) { // invoice appendix existing?
Expand Down Expand Up @@ -181,7 +189,7 @@ protected function addProductItem($oItem, $aPositions)
}
$iAmount = $this->convertItemAmount($dItemAmount);
$dPrice = $oItem->getBasePriceInclTax();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dPrice = $oItem->getPriceInclTax();
}
$this->addInvoicePosition($oItem->getSku(), $dPrice, 'goods', $iAmount, $oItem->getName(), $oItem->getTaxPercent()); // add invoice params to request
Expand Down Expand Up @@ -218,7 +226,7 @@ protected function addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCos
$dPrice = $dShippingCosts;
if ($dPrice === false) {
$dPrice = $oOrder->getBaseShippingInclTax();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dPrice = $oOrder->getShippingInclTax();
}
}
Expand All @@ -232,7 +240,7 @@ protected function addShippingItem($oOrder, $aPositions, $blDebit, $dShippingCos
if ($dPrice < 0) { // negative shipping cost
$sDelDesc = __('Deduction').' '.__('Shipping Costs'); // change item description to deduction
}
$sShippingSku = $this->toolkitHelper->getConfigParam('sku', 'costs', 'payone_misc'); // get configured shipping SKU
$sShippingSku = $this->toolkitHelper->getConfigParam('sku', 'costs', 'payone_misc', $this->getStoreCode()); // get configured shipping SKU
$this->addInvoicePosition($sShippingSku, $dPrice, 'shipment', 1, $sDelDesc, $this->dTax); // add invoice params to request
}
}
Expand All @@ -249,13 +257,13 @@ protected function addDiscountItem($oOrder, $aPositions, $blDebit)
{
// discount costs existing or given for partial captures/debit?
$dTransmitDiscount = $oOrder->getBaseDiscountAmount();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dTransmitDiscount = $oOrder->getDiscountAmount();
}

if ($oOrder instanceof Quote) {
$dTransmitDiscount = $oOrder->getBaseSubtotal() - $oOrder->getBaseSubtotalWithDiscount();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dTransmitDiscount = $oOrder->getSubtotal() - $oOrder->getSubtotalWithDiscount();
}
}
Expand All @@ -269,16 +277,16 @@ protected function addDiscountItem($oOrder, $aPositions, $blDebit)
// The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if
// the calculated positions don't match, so we compensate for rounding-problems here
$dTotal = $oOrder->getBaseGrandTotal();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dTotal = $oOrder->getGrandTotal();
}
$dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy
$dDiscount -= $dDiff; // subtract difference from discount
}
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'discount', 'payone_misc'); // get configured discount SKU
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'discount', 'payone_misc', $this->getStoreCode()); // get configured discount SKU
$sDesc = (string)__('Discount'); // default description
if ($oOrder->getCouponCode()) {// was a coupon code used?
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc'); // get configured voucher SKU
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU
$sDesc = (string)__('Coupon').' - '.$oOrder->getCouponCode(); // add counpon code to description
}
$this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request
Expand All @@ -304,7 +312,7 @@ protected function addAmastyGiftcards($oOrder, $aPositions, $blDebit)
}

$dTransmitDiscount = $aGiftCard['base_gift_amount'];
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dTransmitDiscount = $aGiftCard['gift_amount'];
}
if ($dTransmitDiscount != 0 && ($aPositions === false || ($blDebit === false || array_key_exists('discount', $aPositions) !== false))) {
Expand All @@ -314,15 +322,15 @@ protected function addAmastyGiftcards($oOrder, $aPositions, $blDebit)
// The calculations broken down to single items of Magento2 are unprecise and the Payone API will send an error if
// the calculated positions don't match, so we compensate for rounding-problems here
$dTotal = $oOrder->getBaseGrandTotal();
if ($this->toolkitHelper->getConfigParam('currency') == 'display') {
if ($this->toolkitHelper->getConfigParam('currency', 'global', 'payone_general', $this->getStoreCode()) == 'display') {
$dTotal = $oOrder->getGrandTotal();
}
$dDiff = ($this->dAmount + $dTransmitDiscount - $dTotal); // calc rounding discrepancy
$dDiscount -= $dDiff; // subtract difference from discount
}

if ($dDiscount != 0) {
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc'); // get configured voucher SKU
$sDiscountSku = $this->toolkitHelper->getConfigParam('sku', 'voucher', 'payone_misc', $this->getStoreCode()); // get configured voucher SKU
$sDesc = (string)__('Amasty Coupon').' - '.$aGiftCard['code']; // add counpon code to description
$this->addInvoicePosition($sDiscountSku, $dDiscount, 'voucher', 1, $sDesc, $this->dTax); // add invoice params to request
}
Expand All @@ -347,4 +355,25 @@ protected function convertItemAmount($dItemAmount)
return intval($dItemAmount);
}
}

/**
* Set store code
*
* @param $sStoreCode
* @return void
*/
protected function setStoreCode($sStoreCode)
{
$this->sStoreCode = $sStoreCode;
}

/**
* Returns store code
*
* @return string
*/
protected function getStoreCode()
{
return $this->sStoreCode;
}
}
4 changes: 2 additions & 2 deletions Model/Api/Request/Debit.php
Expand Up @@ -119,13 +119,13 @@ protected function getInvoiceList(Order $oOrder, Creditmemo $oCreditmemo)
}
if (isset($aCreditmemo['shipping_amount']) && $aCreditmemo['shipping_amount'] != 0) {
$aPositions['delcost'] = $oCreditmemo->getBaseShippingInclTax();
if ($this->shopHelper->getConfigParam('currency') == 'display') {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') {
$aPositions['delcost'] = $oCreditmemo->getShippingInclTax();
}
}
if ($blFull !== true && $oCreditmemo->getBaseDiscountAmount() != 0) {
$aPositions['discount'] = $oCreditmemo->getBaseDiscountAmount();
if ($this->shopHelper->getConfigParam('currency') == 'display') {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') {
$aPositions['discount'] = $oCreditmemo->getDiscountAmount();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Methods/Payolution/Invoice.php
Expand Up @@ -64,7 +64,7 @@ class Invoice extends PayolutionBase
*/
public function authorize(InfoInterface $payment, $amount)
{
if ($this->shopHelper->getConfigParam('currency') == 'display') {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $payment->getOrder()->getStore()->getCode()) == 'display') {
$amount = $payment->getOrder()->getTotalDue(); // send display amount instead of base amount
}
$this->sendPayonePreCheck($amount);
Expand Down
2 changes: 1 addition & 1 deletion Model/Methods/Payolution/PayolutionBase.php
Expand Up @@ -187,7 +187,7 @@ public function getPaymentSpecificParameters(Order $oOrder)
public function sendPayonePreCheck($dAmount)
{
$oQuote = $this->checkoutSession->getQuote();
if ($this->shopHelper->getConfigParam('currency') == 'display') {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $oQuote->getStore()->getCode()) == 'display') {
$dAmount = $oQuote->getGrandTotal(); // send display amount instead of base amount
}
$aResponse = $this->precheckRequest->sendRequest($this, $oQuote, $dAmount);
Expand Down
6 changes: 3 additions & 3 deletions Model/Methods/PayoneMethod.php
Expand Up @@ -75,7 +75,7 @@ public function getAuthorizationMode()
*/
protected function sendPayoneDebit(InfoInterface $payment, $amount)
{
if ($this->shopHelper->getConfigParam('currency') == 'display' && $payment->getCreditmemo()) {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $payment->getOrder()->getStore()->getCode()) == 'display' && $payment->getCreditmemo()) {
$amount = $payment->getCreditmemo()->getGrandTotal(); // send display amount instead of base amount
}
$aResponse = $this->debitRequest->sendRequest($this, $payment, $amount);
Expand All @@ -99,7 +99,7 @@ protected function sendPayoneDebit(InfoInterface $payment, $amount)
*/
protected function sendPayoneCapture(InfoInterface $payment, $amount)
{
if ($this->shopHelper->getConfigParam('currency') == 'display' && $payment->getOrder()->hasInvoices()) {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $payment->getOrder()->getStore()->getCode()) == 'display' && $payment->getOrder()->hasInvoices()) {
$oInvoice = $payment->getOrder()->getInvoiceCollection()->getLastItem();
$amount = $oInvoice->getGrandTotal(); // send display amount instead of base amount
}
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function sendPayoneAuthorization(InfoInterface $payment, $amount)
$oOrder = $payment->getOrder();
$oOrder->setCanSendNewEmailFlag(false); // dont send email now, will be sent on appointed

if ($this->shopHelper->getConfigParam('currency') == 'display') {
if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $payment->getOrder()->getStore()->getCode()) == 'display') {
$amount = $oOrder->getTotalDue(); // send display amount instead of base amount
}

Expand Down
34 changes: 28 additions & 6 deletions Model/Source/Currency.php
Expand Up @@ -29,6 +29,7 @@
use Magento\Framework\Option\ArrayInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\View\Element\Template\Context;

/**
* Source class for currency to transmit
Expand All @@ -39,18 +40,39 @@ class Currency implements ArrayInterface
/**
* Store object
*
* @var Store
* @var Context
*/
private $store;
private $context;

/**
* Constructor
*
* @param StoreManagerInterface $storeManager
* @param Context $context
*/
public function __construct(StoreManagerInterface $storeManager)
public function __construct(Context $context)
{
$this->store = $storeManager->getStore();
$this->context = $context;
}

/**
* Returns current base currency code
* Seems like Magento2 framework doesn't have a reliable method to get the current scope in backend configuration..
*
* @return string
*/
protected function getBaseCurrencyCode()
{
$aRequestParams = $this->context->getRequest()->getParams();
$oStoreManager = $this->context->getStoreManager();
if (isset($aRequestParams['website'])) {
return $oStoreManager->getWebsite($aRequestParams['website'])->getBaseCurrencyCode();
}

if (isset($aRequestParams['store'])) {
return $oStoreManager->getStore($aRequestParams['store'])->getBaseCurrencyCode();
}

return $oStoreManager->getStore(0)->getBaseCurrencyCode(); // storeId 0 = Default Config
}

/**
Expand All @@ -63,7 +85,7 @@ public function toOptionArray()
return [
[
'value' => 'base',
'label' => __('Base Currency').' ('.$this->store->getBaseCurrencyCode().')'
'label' => __('Base Currency').' ('.$this->getBaseCurrencyCode().')'
],
[
'value' => 'display',
Expand Down

0 comments on commit 72f39a7

Please sign in to comment.