From 3793ae3585e1799a09e4275ecafb2245c36a707c Mon Sep 17 00:00:00 2001 From: Imanuel Bertrand Date: Tue, 31 Oct 2023 08:38:06 +0100 Subject: [PATCH] Block invoices for dunning process --- Block/Adminhtml/DunningBlockButton.php | 90 +++++++++++++++++++ Controller/Adminhtml/Dunning/Block.php | 57 ++++++++++++ Helper/Dunning.php | 3 +- etc/db_schema.xml | 3 + etc/db_schema_whitelist.json | 5 ++ etc/module.xml | 2 +- i18n/de_DE.csv | 2 + .../layout/sales_order_invoice_view.xml | 9 ++ 8 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 Block/Adminhtml/DunningBlockButton.php create mode 100644 Controller/Adminhtml/Dunning/Block.php create mode 100644 view/adminhtml/layout/sales_order_invoice_view.xml diff --git a/Block/Adminhtml/DunningBlockButton.php b/Block/Adminhtml/DunningBlockButton.php new file mode 100644 index 0000000..cab7579 --- /dev/null +++ b/Block/Adminhtml/DunningBlockButton.php @@ -0,0 +1,90 @@ +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(); + } +} diff --git a/Controller/Adminhtml/Dunning/Block.php b/Controller/Adminhtml/Dunning/Block.php new file mode 100644 index 0000000..6d4bdc3 --- /dev/null +++ b/Controller/Adminhtml/Dunning/Block.php @@ -0,0 +1,57 @@ +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; + } +} diff --git a/Helper/Dunning.php b/Helper/Dunning.php index 5fdfbe7..705ea0d 100644 --- a/Helper/Dunning.php +++ b/Helper/Dunning.php @@ -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)) { diff --git a/etc/db_schema.xml b/etc/db_schema.xml index 40e8e4e..c765f43 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -109,4 +109,7 @@ + + +
diff --git a/etc/db_schema_whitelist.json b/etc/db_schema_whitelist.json index d8ae67d..6e4790d 100644 --- a/etc/db_schema_whitelist.json +++ b/etc/db_schema_whitelist.json @@ -104,5 +104,10 @@ "constraint": { "PRIMARY": true } + }, + "sales_invoice": { + "column": { + "banksync_dunning_blocked_at": true + } } } \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index 380fc89..62bddb9 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - + diff --git a/i18n/de_DE.csv b/i18n/de_DE.csv index 3b0b9b9..e4f9024 100644 --- a/i18n/de_DE.csv +++ b/i18n/de_DE.csv @@ -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 \ No newline at end of file diff --git a/view/adminhtml/layout/sales_order_invoice_view.xml b/view/adminhtml/layout/sales_order_invoice_view.xml new file mode 100644 index 0000000..930cfa2 --- /dev/null +++ b/view/adminhtml/layout/sales_order_invoice_view.xml @@ -0,0 +1,9 @@ + + + + + + + +