1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Observers/PurchaseOrderObserver.php

110 lines
3.0 KiB
PHP
Raw Normal View History

2022-05-29 05:59:44 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
2022-05-29 05:59:44 +02:00
namespace App\Observers;
2023-01-31 13:53:54 +01:00
use App\Jobs\Util\WebhookHandler;
2022-05-29 05:59:44 +02:00
use App\Models\PurchaseOrder;
2023-01-31 13:53:54 +01:00
use App\Models\Webhook;
2022-05-29 05:59:44 +02:00
class PurchaseOrderObserver
{
2023-01-31 15:06:21 +01:00
public $afterCommit = true;
2022-05-29 05:59:44 +02:00
/**
* Handle the client "created" event.
*
* @param PurchaseOrder $purchase_order
* @return void
*/
public function created(PurchaseOrder $purchase_order)
{
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $purchase_order->company_id)
->where('event_id', Webhook::EVENT_CREATE_PURCHASE_ORDER)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-05-29 05:59:44 +02:00
}
/**
* Handle the client "updated" event.
*
* @param PurchaseOrder $purchase_order
* @return void
*/
public function updated(PurchaseOrder $purchase_order)
{
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_UPDATE_PURCHASE_ORDER;
2023-02-16 02:36:09 +01:00
if ($purchase_order->getOriginal('deleted_at') && !$purchase_order->deleted_at) {
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_RESTORE_PURCHASE_ORDER;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($purchase_order->is_deleted) {
$event = Webhook::EVENT_DELETE_PURCHASE_ORDER;
}
2024-01-14 05:05:00 +01:00
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $purchase_order->company_id)
->where('event_id', $event)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch($event, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-05-29 05:59:44 +02:00
}
/**
* Handle the client "deleted" event.
*
* @param PurchaseOrder $purchase_order
* @return void
*/
public function deleted(PurchaseOrder $purchase_order)
{
2023-02-16 02:36:09 +01:00
if ($purchase_order->is_deleted) {
2023-01-31 13:53:54 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $purchase_order->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_PURCHASE_ORDER)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-05-29 05:59:44 +02:00
}
/**
* Handle the client "restored" event.
*
* @param PurchaseOrder $purchase_order
* @return void
*/
public function restored(PurchaseOrder $purchase_order)
{
//
}
/**
* Handle the client "force deleted" event.
*
* @param PurchaseOrder $purchase_order
* @return void
*/
public function forceDeleted(PurchaseOrder $purchase_order)
{
//
}
}