1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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
}
2023-01-31 13:53:54 +01:00
2023-02-16 02:36:09 +01:00
if ($purchase_order->is_deleted) {
$event = Webhook::EVENT_DELETE_PURCHASE_ORDER;
}
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
}
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)
{
//
}
}