Skip to content

Commit

Permalink
Add new scenario-related new-invoice trigger
Browse files Browse the repository at this point in the history
remp/crm#3003
  • Loading branch information
Matus Kalafut committed Oct 16, 2023
1 parent cc7f9a5 commit 0f3f848
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Events/InvoiceEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Crm\InvoicesModule\Events;

use Nette\Database\Table\ActiveRow;

interface InvoiceEventInterface
{
public function getInvoice(): ActiveRow;
}
21 changes: 21 additions & 0 deletions src/Events/NewInvoiceEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Crm\InvoicesModule\Events;

use League\Event\AbstractEvent;
use Nette\Database\Table\ActiveRow;

class NewInvoiceEvent extends AbstractEvent implements InvoiceEventInterface
{
private ActiveRow $invoice;

public function __construct($invoice)
{
$this->invoice = $invoice;
}

public function getInvoice(): ActiveRow
{
return $this->invoice;
}
}
7 changes: 7 additions & 0 deletions src/InvoicesModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Crm\ApplicationModule\Criteria\ScenariosCriteriaStorage;
use Crm\ApplicationModule\CrmModule;
use Crm\ApplicationModule\DataProvider\DataProviderManager;
use Crm\ApplicationModule\Event\EventsStorage;
use Crm\ApplicationModule\Menu\MenuContainerInterface;
use Crm\ApplicationModule\Menu\MenuItem;
use Crm\ApplicationModule\SeederManager;
use Crm\ApplicationModule\User\UserDataRegistrator;
use Crm\ApplicationModule\Widget\LazyWidgetManagerInterface;
use Crm\InvoicesModule\Events\NewInvoiceEvent;
use Crm\InvoicesModule\Scenarios\HasInvoiceCriteria;
use Crm\InvoicesModule\Seeders\AddressTypesSeeder;
use Crm\InvoicesModule\Seeders\ConfigsSeeder;
Expand Down Expand Up @@ -153,4 +155,9 @@ public function registerUserData(UserDataRegistrator $dataRegistrator)
{
$dataRegistrator->addUserDataProvider($this->getInstance(\Crm\InvoicesModule\User\InvoicesUserDataProvider::class));
}

public function registerEvents(EventsStorage $eventsStorage)
{
$eventsStorage->register('new_invoice', NewInvoiceEvent::class, true);
}
}
12 changes: 11 additions & 1 deletion src/Models/Repository/InvoicesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Crm\ApplicationModule\Config\ApplicationConfig;
use Crm\ApplicationModule\Helpers\UserDateHelper;
use Crm\ApplicationModule\Hermes\HermesMessage;
use Crm\ApplicationModule\Repository;
use Crm\ApplicationModule\Repository\AuditLogRepository;
use Crm\InvoicesModule\Events\NewInvoiceEvent;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Crm\SubscriptionsModule\PaymentItem\SubscriptionTypePaymentItem;
use Crm\UsersModule\Repository\AddressesRepository;
use IntlDateFormatter;
use League\Event\Emitter;
use Nette\Database\Explorer;
use Nette\Database\Table\ActiveRow;
use Nette\Utils\DateTime;
Expand All @@ -31,7 +34,9 @@ public function __construct(
private AddressesRepository $addressesRepository,
private InvoiceItemsRepository $invoiceItemsRepository,
AuditLogRepository $auditLogRepository,
private UserDateHelper $userDateHelper
private UserDateHelper $userDateHelper,
private Emitter $emitter,
private \Tomaj\Hermes\Emitter $hermesEmitter,
) {
parent::__construct($database);

Expand Down Expand Up @@ -135,6 +140,11 @@ final public function add(ActiveRow $user, ActiveRow $payment, ActiveRow $invoic
}
}

$this->emitter->emit(new NewInvoiceEvent($invoice));
$this->hermesEmitter->emit(new HermesMessage('new-invoice', [
'invoice_id' => $invoice->id
]));

return $invoice;
}

Expand Down

0 comments on commit 0f3f848

Please sign in to comment.