From 6ddf1f9f1768659af5ae8d9439c7e99f5f463aa7 Mon Sep 17 00:00:00 2001 From: Imanuel Bertrand Date: Fri, 29 Sep 2023 13:17:39 +0200 Subject: [PATCH] Dunning column 'is_paid' --- Model/Dunning.php | 15 ++++ Service/Booker.php | 25 +++++++ Setup/Patch/Data/DunningIsPaid.php | 74 +++++++++++++++++++ etc/db_schema.xml | 1 + etc/db_schema_whitelist.json | 1 + etc/module.xml | 2 +- i18n/de_DE.csv | 3 +- .../ui_component/dunning_listing.xml | 8 ++ 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 Setup/Patch/Data/DunningIsPaid.php diff --git a/Model/Dunning.php b/Model/Dunning.php index 4d1c071..4a3a907 100644 --- a/Model/Dunning.php +++ b/Model/Dunning.php @@ -29,6 +29,8 @@ * @method $this setInvoiceId(int $id) * @method string getDunningType() * @method $this setDunningType(string $type) + * @method bool getIsPaid() + * @method $this setIsPaid(bool $value) * @method string getSentAt() * @method $this setSentAt(string $date) * @method string getCreatedAt() @@ -168,4 +170,17 @@ public function sendMail(): bool return false; } } + + /** + * @return bool + * @throws InputException + * @throws NoSuchEntityException + */ + public function updatePaidStatus(): bool + { + $oldStatus = (int)$this->getIsPaid(); + /** @noinspection PhpUndefinedMethodInspection */ + $this->setIsPaid($this->getInvoice()->getIsBanksynced()); + return $oldStatus !== (int)$this->getIsPaid(); + } } diff --git a/Service/Booker.php b/Service/Booker.php index 726870e..2541873 100644 --- a/Service/Booker.php +++ b/Service/Booker.php @@ -6,8 +6,11 @@ use Ibertrand\BankSync\Helper\Config; use Ibertrand\BankSync\Helper\Matching; use Ibertrand\BankSync\Logger\Logger; +use Ibertrand\BankSync\Model\Dunning; +use Ibertrand\BankSync\Model\DunningRepository; use Ibertrand\BankSync\Model\MatchConfidence; use Ibertrand\BankSync\Model\MatchConfidenceRepository; +use Ibertrand\BankSync\Model\ResourceModel\Dunning\CollectionFactory; use Ibertrand\BankSync\Model\ResourceModel\MatchConfidence\CollectionFactory as MatchConfidenceCollectionFactory; use Ibertrand\BankSync\Model\ResourceModel\TempTransaction as TempTransactionResource; use Ibertrand\BankSync\Model\ResourceModel\TempTransaction\CollectionFactory as TempTransactionCollectionFactory; @@ -42,6 +45,8 @@ class Booker protected Config $config; protected Matching $matching; protected Logger $logger; + protected CollectionFactory $dunningCollectionFactory; + protected DunningRepository $dunningRepository; public function __construct( TempTransactionResource $tempTransactionResource, @@ -54,6 +59,8 @@ public function __construct( MatchConfidenceRepository $matchConfidenceRepository, InvoiceRepository $invoiceRepository, CreditmemoRepository $creditmemoRepository, + CollectionFactory $dunningCollectionFactory, + DunningRepository $dunningRepository, Config $config, Matching $matching, Logger $logger, @@ -68,6 +75,8 @@ public function __construct( $this->matchConfidenceRepository = $matchConfidenceRepository; $this->invoiceRepository = $invoiceRepository; $this->creditmemoRepository = $creditmemoRepository; + $this->dunningCollectionFactory = $dunningCollectionFactory; + $this->dunningRepository = $dunningRepository; $this->config = $config; $this->matching = $matching; $this->logger = $logger; @@ -101,6 +110,22 @@ private function saveDocument(Invoice|Creditmemo $document, bool $isBanksynced): $document->setIsBanksynced((int)$isBanksynced) ->setHasDataChanges(true); $this->resolveDocumentRepository($document)->save($document); + + if ($document instanceof Invoice) { + $dunnings = $this->dunningCollectionFactory->create() + ->addFieldToFilter('invoice_id', $document->getId()); + foreach ($dunnings as $dunning) { + /** @var Dunning $dunning */ + try { + if ($dunning->updatePaidStatus()) { + $dunning->setHasDataChanges(true); + $this->dunningRepository->save($dunning); + } + } catch (Exception $e) { + $this->logger->error($e->getMessage() . "\n" . $e->getTraceAsString()); + } + } + } } /** diff --git a/Setup/Patch/Data/DunningIsPaid.php b/Setup/Patch/Data/DunningIsPaid.php new file mode 100644 index 0000000..bbc2e1f --- /dev/null +++ b/Setup/Patch/Data/DunningIsPaid.php @@ -0,0 +1,74 @@ +dunningCollectionFactory = $dunningCollectionFactory; + $this->dunningRepository = $dunningRepository; + } + + /** + * @return string[] + */ + public static function getDependencies(): array + { + return [ + IsBanksyncedSchemaPatch::class, + ]; + } + + /** + * @return array|string[] + */ + public function getAliases(): array + { + return []; + } + + /** + * @return void + */ + public function apply() + { + $dunnings = $this->dunningCollectionFactory->create(); + foreach ($dunnings as $dunning) { + /** @var Dunning $dunning */ + try { + if ($dunning->updatePaidStatus()) { + $this->dunningRepository->save($dunning); + } + } catch (Exception $e) { + echo $e->getMessage(); + } + } + } + + /** + * @return void + */ + public function revert(): void + { + // No revert + } +} diff --git a/etc/db_schema.xml b/etc/db_schema.xml index f63331d..514e002 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -4,6 +4,7 @@ + diff --git a/etc/db_schema_whitelist.json b/etc/db_schema_whitelist.json index 6276402..3095168 100644 --- a/etc/db_schema_whitelist.json +++ b/etc/db_schema_whitelist.json @@ -4,6 +4,7 @@ "entity_id": true, "invoice_id": true, "dunning_type": true, + "is_paid": true, "sent_at": true, "created_at": true, "updated_at": true diff --git a/etc/module.xml b/etc/module.xml index cdb47c5..081ffb8 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 df51ed5..0b0424e 100644 --- a/i18n/de_DE.csv +++ b/i18n/de_DE.csv @@ -152,4 +152,5 @@ Transactions,Transaktionen Type,Typ Types,Typen Unbook,Rückgängig machen -Weights,Gewichte \ No newline at end of file +Weights,Gewichte +Is paid,Ist bezahlt \ No newline at end of file diff --git a/view/adminhtml/ui_component/dunning_listing.xml b/view/adminhtml/ui_component/dunning_listing.xml index ebd0d03..81dade3 100644 --- a/view/adminhtml/ui_component/dunning_listing.xml +++ b/view/adminhtml/ui_component/dunning_listing.xml @@ -82,6 +82,14 @@ + + + select + + + select + +