Skip to content

Commit

Permalink
Block invoices for dunning process
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanuelBertrand committed Oct 31, 2023
1 parent b30e571 commit 3793ae3
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 2 deletions.
90 changes: 90 additions & 0 deletions Block/Adminhtml/DunningBlockButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Ibertrand\BankSync\Block\Adminhtml;

use Magento\Backend\Block\Widget\Container;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;
use Magento\Sales\Model\EntityInterface;
use Magento\Sales\Model\Order\Invoice;

class DunningBlockButton extends Container
{
/**
* Core registry
*
* @var Registry
*/
protected Registry $coreRegistry;

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

protected function _construct()
{
$this->addButton(
'banksync_block_dunning_button',
[
'label' => $this->getLabel(),
'class' => 'dunning_block',
'onclick' => "setLocation('{$this->getTargetUrl()}')",
]
);

parent::_construct();
}

/**
* @return string
*/
protected function getLabel(): string
{
return __(
$this->invoiceIsBlocked() ? 'Remove dunning block' : 'Dunning block'
);
}

/**
* @return bool
*/
protected function invoiceIsBlocked(): bool
{
return !empty($this->getInvoice()->getBanksyncDunningBlockedAt());
}

/**
* @return Invoice
*/
protected function getInvoice(): EntityInterface
{
return $this->coreRegistry->registry('current_invoice');
}

/**
* @return string
*/
protected function getTargetUrl(): string
{
$setBlocked = $this->invoiceIsBlocked() ? 0 : 1;
return $this->getUrl("banksync/dunning/block", ['invoice_id' => $this->getInvoiceId(), 'set_blocked' => $setBlocked]);
}

/**
* @return integer
*/
protected function getInvoiceId(): int
{
return $this->getInvoice()->getId();
}
}
57 changes: 57 additions & 0 deletions Controller/Adminhtml/Dunning/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Ibertrand\BankSync\Controller\Adminhtml\Dunning;

use Ibertrand\BankSync\Logger\Logger;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Model\Order\InvoiceRepository;

class Block extends Action
{
const ADMIN_RESOURCE = 'Ibertrand_BankSync::sub_menu_dunnings';
private InvoiceRepository $invoiceRepository;
private Logger $logger;

/**
* @param Context $context
* @param InvoiceRepository $invoiceRepository
* @param Logger $logger
*/
public function __construct(Context $context, InvoiceRepository $invoiceRepository, Logger $logger)
{
parent::__construct($context);
$this->invoiceRepository = $invoiceRepository;
$this->logger = $logger;
}

/**
* @return Redirect
* @throws NoSuchEntityException
* @throws InputException
*/
public function execute()
{
$invoiceId = $this->getRequest()->getParam('invoice_id');
$setBlocked = !empty($this->getRequest()->getParam('set_blocked'));

$invoice = $this->invoiceRepository->get($invoiceId);
if ($setBlocked) {
$this->logger->info('Invoice ' . $invoice->getIncrementId() . ' blocked for dunning');
$invoice->setBanksyncDunningBlockedAt(date('Y-m-d H:i:s'));
} else {
$this->logger->info('Invoice ' . $invoice->getIncrementId() . ' unblocked for dunning');
$invoice->setBanksyncDunningBlockedAt(null);
}
$this->invoiceRepository->save($invoice);

$redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$redirect->setPath('sales/invoice/view', ['invoice_id' => $invoiceId]);

return $redirect;
}
}
3 changes: 2 additions & 1 deletion Helper/Dunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ public function getOpenInvoices(int $storeId): InvoiceCollection
->addFieldToFilter('main_table.created_at', ['gt' => $minCreationDate])
->addFieldToFilter('main_table.created_at', ['lt' => $latestCreationDate])
->addFieldToFilter('main_table.store_id', $storeId)
->addFieldToFilter('is_banksynced', ['eq' => 0]);
->addFieldToFilter('is_banksynced', ['eq' => 0])
->addFieldToFilter('banksync_dunning_blocked_at', ['null' => true]);

$paymentMethods = $this->config->getPaymentMethods();
if (!empty($paymentMethods)) {
Expand Down
3 changes: 3 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@
<column name="entity_id"/>
</constraint>
</table>
<table name="sales_invoice">
<column xsi:type="datetime" name="banksync_dunning_blocked_at" nullable="true" comment="Block dunnings"/>
</table>
</schema>
5 changes: 5 additions & 0 deletions etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,10 @@
"constraint": {
"PRIMARY": true
}
},
"sales_invoice": {
"column": {
"banksync_dunning_blocked_at": true
}
}
}
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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="Ibertrand_BankSync" setup_version="0.0.20">
<module name="Ibertrand_BankSync" setup_version="0.0.21">
</module>
</config>
2 changes: 2 additions & 0 deletions i18n/de_DE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,5 @@ Archive transaction,Transaktion archivieren
"Archive ""%1""","""%1"" archivieren"
Transaction archived.,Transaktion archiviert.
Transaction archived,Transaktion archiviert
Dunning block,Mahnsperre
Remove dunning block,Mahnsperre aufheben
9 changes: 9 additions & 0 deletions view/adminhtml/layout/sales_order_invoice_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="page.actions.toolbar">
<block class="Ibertrand\BankSync\Block\Adminhtml\DunningBlockButton" as="banksync_block_dunning_button"/>
</referenceBlock>
</body>
</page>

0 comments on commit 3793ae3

Please sign in to comment.