Skip to content

Commit

Permalink
Dunning column 'is_paid'
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanuelBertrand committed Sep 29, 2023
1 parent a906e73 commit 6ddf1f9
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Model/Dunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();
}
}
25 changes: 25 additions & 0 deletions Service/Booker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -54,6 +59,8 @@ public function __construct(
MatchConfidenceRepository $matchConfidenceRepository,
InvoiceRepository $invoiceRepository,
CreditmemoRepository $creditmemoRepository,
CollectionFactory $dunningCollectionFactory,
DunningRepository $dunningRepository,
Config $config,
Matching $matching,
Logger $logger,
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
}

/**
Expand Down
74 changes: 74 additions & 0 deletions Setup/Patch/Data/DunningIsPaid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Ibertrand\BankSync\Setup\Patch\Data;

use Exception;
use Ibertrand\BankSync\Model\Dunning;
use Ibertrand\BankSync\Model\DunningRepository;
use Ibertrand\BankSync\Model\ResourceModel\Dunning\CollectionFactory;
use Ibertrand\BankSync\Setup\Patch\Schema\IsBanksynced as IsBanksyncedSchemaPatch;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class DunningIsPaid implements DataPatchInterface, PatchRevertableInterface
{
private CollectionFactory $dunningCollectionFactory;
private DunningRepository $dunningRepository;


/**
* @param CollectionFactory $dunningCollectionFactory
* @param DunningRepository $dunningRepository
*/
public function __construct(
CollectionFactory $dunningCollectionFactory,
DunningRepository $dunningRepository,
) {
$this->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
}
}
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true" comment="Record ID"/>
<column xsi:type="int" name="invoice_id" unsigned="true" nullable="false" comment="Invoice ID"/>
<column xsi:type="varchar" name="dunning_type" nullable="false" length="20" comment="Dunning Type"/>
<column xsi:type="int" name="is_paid" unsigned="true" nullable="false" comment="Is Paid"/>
<column xsi:type="datetime" name="sent_at" nullable="true" comment="Sent Time"/>
<column xsi:type="datetime" name="created_at" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
<column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP" on_update="true" comment="Update Time"/>
Expand Down
1 change: 1 addition & 0 deletions etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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.17">
<module name="Ibertrand_BankSync" setup_version="0.0.18">
</module>
</config>
3 changes: 2 additions & 1 deletion i18n/de_DE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,5 @@ Transactions,Transaktionen
Type,Typ
Types,Typen
Unbook,Rückgängig machen
Weights,Gewichte
Weights,Gewichte
Is paid,Ist bezahlt
8 changes: 8 additions & 0 deletions view/adminhtml/ui_component/dunning_listing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
<label translate="true">Sent At</label>
</settings>
</column>
<column name="is_paid" sortOrder="60" component="Magento_Ui/js/grid/columns/select">
<settings>
<filter>select</filter>
<options class="Magento\Config\Model\Config\Source\Yesno"/>
<label translate="true">Is paid</label>
<dataType>select</dataType>
</settings>
</column>
<actionsColumn name="actions" class="Ibertrand\BankSync\Ui\Component\Listing\Column\Dunning\Actions"
sortOrder="120">
<settings>
Expand Down

0 comments on commit 6ddf1f9

Please sign in to comment.