Skip to content

Commit

Permalink
Merge pull request #205 from pagarme/feature/PAOPN-458-refactor-order…
Browse files Browse the repository at this point in the history
…-charge-block

PAOPN-458: Refactoring order_charge block and template
  • Loading branch information
mateus-picoloto authored Jul 21, 2023
2 parents deb7806 + 3619218 commit b54b6ed
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 184 deletions.
178 changes: 140 additions & 38 deletions Block/Adminhtml/Order/Charge/Tab/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,150 @@

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\Registry;
use Magento\Sales\Model\Order;
use Pagarme\Core\Kernel\Aggregates\Charge;
use Pagarme\Core\Kernel\Exceptions\InvalidParamException;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Pagarme\Helper\HtmlTableHelper;
use Pagarme\Pagarme\Service\Order\ChargeService;

use Pagarme\Core\Kernel\Repositories\ChargeRepository;
use Pagarme\Core\Kernel\Repositories\OrderRepository;
use Pagarme\Core\Kernel\ValueObjects\Id\OrderId;

class View extends \Magento\Backend\Block\Template implements \Magento\Backend\Block\Widget\Tab\TabInterface
class View extends Template implements TabInterface
{
// @codingStandardsIgnoreLine
protected $_template = 'tab/view/order_charge.phtml';

/**
* View constructor.
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @var Registry
*/
private $registry;

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

/**
* @var ChargeService
*/
private $chargeService;

/**
* @param Context $context
* @param Registry $registry
* @param HtmlTableHelper $htmlTableHelper
* @param ChargeService $chargeService
* @param array $data
* @throws Exception
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
Context $context,
Registry $registry,
HtmlTableHelper $htmlTableHelper,
ChargeService $chargeService,
array $data = []
) {
Magento2CoreSetup::bootstrap();

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

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

/**
* Retrieve order model instance
*
* @return \Magento\Sales\Model\Order
* @return string
* @throws InvalidParamException
*/
public function getOrder()
public function getChargesTableBody()
{
return $this->_coreRegistry->registry('current_order');
$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;
}

public function getCharges()
/**
* @param Charge $charge
* @return string
*/
public function getAmountInput($charge)
{
//@todo Create service to return the charges
$platformOrderID = $this->getOrderIncrementId();
$pagarmeOrder = (new OrderRepository)->findByPlatformId($platformOrderID);
return sprintf('<input class="amount-value" value="%s" />', $charge->getAmount());
}

if ($pagarmeOrder === null) {
return [];
/**
* @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);
}

$charges = (new ChargeRepository)->findByOrderId(
new OrderId($pagarmeOrder->getPagarmeId()->getValue())
);

return $charges;
return $buttonTableDataCell;
}

/**
* Retrieve order model instance
*
* @return \Magento\Sales\Model\Order
* @param Charge $charge
* @return string
*/
public function getOrderId()
public function getCancelChargeButtonDataCell($charge)
{
return $this->getOrder()->getEntityId();
$button = $this->getActionChargeButton(
$charge,
'cancel',
__('Do you want to cancel this charge?'),
__('Cancel')
);
return $this->htmlTableHelper->formatTableDataCell($button);
}

/**
* Retrieve order increment id
*
* @param Charge $charge
* @param string $action
* @param string $message
* @param string $label
* @return string
*/
public function getOrderIncrementId()
public function getActionChargeButton($charge, $action, $message, $label)
{
return $this->getOrder()->getIncrementId();
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
);
}

/**
Expand All @@ -90,7 +161,7 @@ public function getTabLabel()
*/
public function getTabTitle()
{
return __('Charges');
return $this->getTabLabel();
}

/**
Expand Down Expand Up @@ -118,4 +189,35 @@ public function getChargeCaptureUrl()
{
return $this->_urlBuilder->getUrl('pagarme_pagarme/charges/capture');
}

/**
* Retrieve order model instance
*
* @return Order
*/
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
*
* @return string
*/
private function getOrderIncrementId()
{
return $this->getOrder()->getIncrementId();
}
}
55 changes: 13 additions & 42 deletions Block/Customer/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use Pagarme\Core\Kernel\Abstractions\AbstractEntity;
use Pagarme\Core\Recurrence\Repositories\ChargeRepository;
use Pagarme\Core\Recurrence\Aggregates\Charge;
use Pagarme\Pagarme\Helper\NumberFormatHelper;
use Pagarme\Pagarme\Helper\HtmlTableHelper;

class Invoice extends Template
{
Expand All @@ -50,30 +50,28 @@ class Invoice extends Template
protected $coreRegistry;

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

/**
* @param Context $context
* @param Session $customerSession
* @param Registry $coreRegistry
* @param NumberFormatHelper $numberFormatter
* @throws AuthorizationException
* @throws InvalidParamException
* @param HtmlTableHelper $htmlTableHelper
*/
public function __construct(
Context $context,
Session $customerSession,
Registry $coreRegistry,
NumberFormatHelper $numberFormatter
HtmlTableHelper $htmlTableHelper
) {
parent::__construct($context, []);
Magento2CoreSetup::bootstrap();

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

Expand Down Expand Up @@ -130,13 +128,13 @@ public function getInvoicesTableBody()
foreach ($this->getAllChargesByCodeOrder() as $id => $item) {
$tbody .= "<tr>";
$visualId = $id + 1;
$tbody .= $this->formatTableDataCell($visualId);
$tbody .= $this->formatNumberTableDataCell($item->getAmount());
$tbody .= $this->formatNumberTableDataCell($item->getPaidAmount());
$tbody .= $this->formatNumberTableDataCell($item->getCanceledAmount());
$tbody .= $this->formatNumberTableDataCell($item->getRefundedAmount());
$tbody .= $this->formatTableDataCell($item->getStatus()->getStatus());
$tbody .= $this->formatTableDataCell($item->getPaymentMethod()->getPaymentMethod());
$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>';
}
Expand Down Expand Up @@ -169,33 +167,6 @@ private function addBilletButton($item)
return $button;
}

/**
* @param mixed $text
* @return string
*/
private function formatTableDataCell($text)
{
return sprintf('<td>%s</td>', $text);
}

/**
* @param mixed $number
* @return string
*/
private function formatNumberTableDataCell($number)
{
return $this->formatTableDataCell($this->formatNumber($number));
}

/**
* @param mixed $number
* @return false|string
*/
private function formatNumber($number)
{
return $this->numberFormatter->formatToLocalCurrency(($number) / 100);
}

/**
* @return bool
*/
Expand Down
Loading

0 comments on commit b54b6ed

Please sign in to comment.