Skip to content

Issue 30387 - Add formatted price to Money type #31242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CompanyCreditGraphQl\Model\Credit;

use Magento\CompanyCredit\Model\HistoryInterface;
use Magento\NegotiableQuote\Model\PriceCurrency;

/**
* Credit history balance
*/
class Balance
{
/**
* @var PriceCurrency
*/
private $priceCurrency;

/**
* Balance constructor.
* @param PriceCurrency $priceCurrency
*/
public function __construct(PriceCurrency $priceCurrency){
$this->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
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CompanyCreditGraphQl\Model\Credit;

use Magento\CompanyCredit\Model\HistoryInterface;
use Magento\NegotiableQuote\Model\PriceCurrency;

/**
* Extract operation details
*/
class OperationExtractor
{
/**
* @var HistoryType
*/
private $historyType;

/**
* @var OperationUser
*/
private $operationUser;

/**
* @var Balance
*/
private $balance;

/**
* @var PriceCurrency
*/
private $priceCurrency;

/**
* @param HistoryType $historyType
* @param OperationUser $operationUser
* @param Balance $balance
* @param PriceCurrency $priceCurrency
*/
public function __construct(
HistoryType $historyType,
OperationUser $operationUser,
Balance $balance,
PriceCurrency $priceCurrency
) {
$this->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)
];
}
}
105 changes: 105 additions & 0 deletions .b2b/app/code/Magento/CompanyCreditGraphQl/Model/Resolver/Credit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CompanyCreditGraphQl\Model\Resolver;

use Magento\CompanyCredit\Api\CreditDataProviderInterface;
use Magento\CompanyCredit\Model\PaymentMethodStatus;
use Magento\CompanyCreditGraphQl\Model\Credit\Balance;
use Magento\CompanyGraphQl\Model\Company\ResolverAccess;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\NegotiableQuote\Model\PriceCurrency;

/**
* Company credit resolver
*/
class Credit implements ResolverInterface
{
/**
* @var ResolverAccess
*/
private $resolverAccess;

/**
* @var array
*/
private $allowedResources;

/**
* @var CreditDataProviderInterface
*/
private $creditDataProvider;

/**
* @var PaymentMethodStatus
*/
private $paymentMethodStatus;

/**
* @var Balance
*/
private $balance;

/**
* @var PriceCurrency
*/
private $priceCurrency;

/**
* @param ResolverAccess $resolverAccess
* @param CreditDataProviderInterface $creditDataProvider
* @param PaymentMethodStatus $paymentMethodStatus
* @param Balance $balance
* @param PriceCurrency $priceCurrency
* @param array $allowedResources
*/
public function __construct(
ResolverAccess $resolverAccess,
CreditDataProviderInterface $creditDataProvider,
PaymentMethodStatus $paymentMethodStatus,
Balance $balance,
PriceCurrency $priceCurrency,
array $allowedResources = []
) {
$this->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))
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerBalanceGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\CustomerBalance\Model\BalanceFactory;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\CustomerBalance\Helper\Data as CustomerBalanceHelper;

/**
* Resolver for checking applied Store credit balance
*/
class GetAppliedStoreCreditFromCart implements ResolverInterface
{
/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var PriceCurrencyInterface
*/
private $priceCurrency;

/**
* @var BalanceFactory
*/
private $balanceFactory;

/**
* @var CustomerBalanceHelper
*/
private $customerBalanceHelper;

/**
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
* @param PriceCurrencyInterface $priceCurrency
* @param BalanceFactory $balanceFactory
* @param CustomerBalanceHelper $customerBalanceHelper
*/
public function __construct(
GetCartForUser $getCartForUser,
CartRepositoryInterface $cartRepository,
PriceCurrencyInterface $priceCurrency,
BalanceFactory $balanceFactory,
CustomerBalanceHelper $customerBalanceHelper
) {
$this->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;
}
}
Loading