Skip to content

Commit

Permalink
Merge branch 'stg' into feature/PAOPN-460-refactoring-price-cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Picoloto committed Aug 1, 2023
2 parents 7463465 + 97ecd65 commit 8ca9317
Show file tree
Hide file tree
Showing 26 changed files with 429 additions and 673 deletions.
117 changes: 10 additions & 107 deletions Block/Adminhtml/Order/Charge/Tab/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace Pagarme\Pagarme\Block\Adminhtml\Order\Charge\Tab;

use Exception;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\Registry;
use Magento\Sales\Model\Order;
use Pagarme\Core\Kernel\Aggregates\Charge;
use Pagarme\Core\Kernel\Exceptions\InvalidParamException;
use Pagarme\Pagarme\Block\BaseTemplateWithCurrency;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Pagarme\Helper\HtmlTableHelper;
use Pagarme\Pagarme\Service\Order\ChargeService;

class View extends Template implements TabInterface
class View extends BaseTemplateWithCurrency implements TabInterface
{
// @codingStandardsIgnoreLine
protected $_template = 'tab/view/order_charge.phtml';
Expand All @@ -24,11 +23,6 @@ class View extends Template implements TabInterface
*/
private $registry;

/**
* @var HtmlTableHelper
*/
private $htmlTableHelper;

/**
* @var ChargeService
*/
Expand All @@ -37,114 +31,34 @@ class View extends Template implements TabInterface
/**
* @param Context $context
* @param Registry $registry
* @param HtmlTableHelper $htmlTableHelper
* @param Data $priceHelper
* @param ChargeService $chargeService
* @param array $data
* @throws Exception
*/
public function __construct(
Context $context,
Registry $registry,
HtmlTableHelper $htmlTableHelper,
Data $priceHelper,
ChargeService $chargeService,
array $data = []
) {
Magento2CoreSetup::bootstrap();

$this->registry = $registry;
$this->htmlTableHelper = $htmlTableHelper;
$this->chargeService = $chargeService;

parent::__construct($context, $data);
parent::__construct($context, $priceHelper, $data);
}

/**
* @return string
* @return array
* @throws InvalidParamException
*/
public function getChargesTableBody()
{
$tbody = '';

foreach ($this->getCharges() as $charge) {
$tbody .= '<tr>';
$tbody .= $this->htmlTableHelper->formatTableDataCell($charge->getPagarmeId()->getValue());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($charge->getAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($charge->getPaidAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($charge->getCanceledAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($charge->getRefundedAmount());
$tbody .= $this->htmlTableHelper->formatTableDataCell($charge->getStatus()->getStatus());
$tbody .= $this->htmlTableHelper->formatTableDataCell($this->getAmountInput($charge), 'amount');
$tbody .= $this->getCaptureChargeButtonDataCell($charge);
$tbody .= $this->getCancelChargeButtonDataCell($charge);
$tbody .= '</tr>';
}

return $tbody;
}

/**
* @param Charge $charge
* @return string
*/
public function getAmountInput($charge)
{
return sprintf('<input class="amount-value" value="%s" />', $charge->getAmount());
}

/**
* @param Charge $charge
* @return string
*/
public function getCaptureChargeButtonDataCell($charge)
{
$buttonTableDataCell = '';

if ($charge->getCanceledAmount() <= 0) {
$button = $this->getActionChargeButton(
$charge,
'capture',
__('Do you want to capture this charge?'),
__('Capture')
);
$buttonTableDataCell .= $this->htmlTableHelper->formatTableDataCell($button);
}

return $buttonTableDataCell;
}

/**
* @param Charge $charge
* @return string
*/
public function getCancelChargeButtonDataCell($charge)
public function getCharges()
{
$button = $this->getActionChargeButton(
$charge,
'cancel',
__('Do you want to cancel this charge?'),
__('Cancel')
);
return $this->htmlTableHelper->formatTableDataCell($button);
}

/**
* @param Charge $charge
* @param string $action
* @param string $message
* @param string $label
* @return string
*/
public function getActionChargeButton($charge, $action, $message, $label)
{
return sprintf(
'<button class="action charge-button" data-action="%s" data-order="%s"' .
' data-charge="%s" data-message="%s">%s</button>',
$action,
$charge->getOrderId()->getValue(),
$charge->getPagarmeId()->getValue(),
$message,
$label
return $this->chargeService->findChargesByIncrementId(
$this->getOrderIncrementId()
);
}

Expand Down Expand Up @@ -200,17 +114,6 @@ private function getOrder()
return $this->registry->registry('current_order');
}

/**
* @return array
* @throws InvalidParamException
*/
private function getCharges()
{
return $this->chargeService->findChargesByIncrementId(
$this->getOrderIncrementId()
);
}

/**
* Retrieve order increment id
*
Expand Down
45 changes: 45 additions & 0 deletions Block/BaseTemplateWithCurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Class BaseTemplateWithCurrency
*
* @author Open Source Team
* @copyright 2023 Pagar.me (https://pagar.me)
* @license https://pagar.me Copyright
*
* @link https://pagar.me
*/

namespace Pagarme\Pagarme\Block;

use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class BaseTemplateWithCurrency extends Template
{
/**
* @var Data
*/
private $priceHelper;

/**
* @param Context $context
* @param Data $priceHelper
* @param array $data
*/
public function __construct(Context $context, Data $priceHelper, array $data = [])
{
parent::__construct($context, $data);
$this->priceHelper = $priceHelper;
}

/**
* @param mixed $number
* @return float|string
*/
public function formatToCurrency($number)
{
return $this->priceHelper->currency(($number) / 100);
}
}
85 changes: 13 additions & 72 deletions Block/Customer/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
namespace Pagarme\Pagarme\Block\Customer;

use Magento\Framework\Exception\AuthorizationException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;
use Magento\Customer\Model\Session;
use Pagarme\Core\Kernel\ValueObjects\Id\SubscriptionId;
use Pagarme\Pagarme\Api\Data\RecurrenceProductsSubscriptionInterface;
use Pagarme\Pagarme\Block\BaseTemplateWithCurrency;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Core\Recurrence\Repositories\SubscriptionRepository;
use Pagarme\Core\Kernel\Exceptions\InvalidParamException;
use Pagarme\Core\Kernel\Abstractions\AbstractEntity;
use Pagarme\Core\Recurrence\Repositories\ChargeRepository;
use Pagarme\Core\Recurrence\Aggregates\Charge;
use Pagarme\Pagarme\Helper\HtmlTableHelper;

class Invoice extends Template
class Invoice extends BaseTemplateWithCurrency
{
/**
* @var Session
Expand All @@ -49,29 +48,23 @@ class Invoice extends Template
*/
protected $coreRegistry;

/**
* @var HtmlTableHelper
*/
private $htmlTableHelper;

/**
* @param Context $context
* @param Session $customerSession
* @param Registry $coreRegistry
* @param HtmlTableHelper $htmlTableHelper
* @param Data $priceHelper
*/
public function __construct(
Context $context,
Session $customerSession,
Registry $coreRegistry,
HtmlTableHelper $htmlTableHelper
Data $priceHelper
) {
parent::__construct($context, []);
parent::__construct($context, $priceHelper, []);
Magento2CoreSetup::bootstrap();

$this->coreRegistry = $coreRegistry;
$this->customerSession = $customerSession;
$this->htmlTableHelper = $htmlTableHelper;
$this->chargeRepository = new ChargeRepository();
$this->subscriptionRepository = new SubscriptionRepository();

Expand Down Expand Up @@ -106,73 +99,21 @@ public function getSubscriptionPaymentMethod()
}

/**
* @return string
*/
public function getBilletHeader()
{
if (!$this->isBillet()) {
return "";
}

return sprintf('<th>%s</th>', __('Billet'));
}

/**
* @return string
* @throws InvalidParamException
* @param int $id
* @return int
*/
public function getInvoicesTableBody()
public function getVisualChargeId($id)
{
$tbody = "";

foreach ($this->getAllChargesByCodeOrder() as $id => $item) {
$tbody .= "<tr>";
$visualId = $id + 1;
$tbody .= $this->htmlTableHelper->formatTableDataCell($visualId);
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($item->getAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($item->getPaidAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($item->getCanceledAmount());
$tbody .= $this->htmlTableHelper->formatNumberTableDataCell($item->getRefundedAmount());
$tbody .= $this->htmlTableHelper->formatTableDataCell($item->getStatus()->getStatus());
$tbody .= $this->htmlTableHelper->formatTableDataCell($item->getPaymentMethod()->getPaymentMethod());
$tbody .= $this->addBilletButton($item);
$tbody .= '</tr>';
}

return $tbody;
}

/**
* @param mixed $item
* @return string
*/
private function addBilletButton($item)
{
$button = '';
if (!$this->isBillet()) {
return $button;
}

$button = '<td>';
$hasBilletLink = !empty($item->getBoletoLink());
if ($hasBilletLink) {
$button .= sprintf(
'<button onclick="location.href = \'%s\';" id="details">%s</button>',
$item->getBoletoLink(),
'download'
);
}
$button .= '</td>';

return $button;
return $id + 1;
}

/**
* @param Charge $item
* @return bool
*/
private function isBillet()
public function isBillet($item)
{
return $this->getSubscriptionPaymentMethod() === RecurrenceProductsSubscriptionInterface::BOLETO;
return !empty($item->getBoletoLink());
}

/**
Expand Down
14 changes: 11 additions & 3 deletions Block/Payment/Info/Pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public function _construct()
}

/**
* @return string|null
* @return PixHelper
* @throws LocalizedException
*/
public function getPixInfo()
{
$pixHelper = new PixHelper();
return $pixHelper->getQrCode($this->getInfo());
return $pixHelper->getInfo($this->getInfo());
}

public function getTitle()
Expand Down Expand Up @@ -65,7 +65,15 @@ public function getTransactionInfo()
if ($orderObject === null) {
return [];
}
$charge = current($orderObject->getCharges());
return $charge->getLastTransaction();
}

return $orderObject->getCharges()[0]->getLastTransaction();
public function showPrintButton()
{
if (!empty($this->getPixInfo()) && $this->getInfo()->getOrder()->getStatus() == 'pending') {
return true;
}
return false;
}
}
Loading

0 comments on commit 8ca9317

Please sign in to comment.