From e6e651b4b4b931d4fee138a6ce7bf143fb611f58 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 9 Jun 2022 13:33:13 +1000 Subject: [PATCH] Event / Listeners for Purchase Orders --- .../PurchaseOrderWasMarkedSent.php | 46 -------------- .../PurchaseOrderWasRestored.php | 4 +- .../Controllers/PurchaseOrderController.php | 2 +- .../CreatePurchaseOrderActivity.php | 58 +++++++++++++++++ .../PurchaseOrderArchivedActivity.php | 59 ++++++++++++++++++ .../PurchaseOrderDeletedActivity.php | 58 +++++++++++++++++ .../PurchaseOrderEmailActivity.php | 59 ++++++++++++++++++ .../PurchaseOrderRestoredActivity.php | 58 +++++++++++++++++ .../PurchaseOrderViewedActivity.php | 62 +++++++++++++++++++ .../UpdatePurchaseOrderActivity.php | 58 +++++++++++++++++ app/Providers/EventServiceProvider.php | 16 ++++- app/Services/PurchaseOrder/MarkSent.php | 4 -- app/Transformers/PurchaseOrderTransformer.php | 2 +- ...reate_purchase_order_invitations_table.php | 4 ++ 14 files changed, 435 insertions(+), 55 deletions(-) delete mode 100644 app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php create mode 100644 app/Listeners/PurchaseOrder/CreatePurchaseOrderActivity.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderArchivedActivity.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderDeletedActivity.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderEmailActivity.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderRestoredActivity.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderViewedActivity.php create mode 100644 app/Listeners/PurchaseOrder/UpdatePurchaseOrderActivity.php diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php b/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php deleted file mode 100644 index 8b6a0d87c3..0000000000 --- a/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php +++ /dev/null @@ -1,46 +0,0 @@ -purchase_order = $purchase_order; - $this->company = $company; - $this->event_vars = $event_vars; - } -} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasRestored.php b/app/Events/PurchaseOrder/PurchaseOrderWasRestored.php index 7784d955ff..c957c52a75 100644 --- a/app/Events/PurchaseOrder/PurchaseOrderWasRestored.php +++ b/app/Events/PurchaseOrder/PurchaseOrderWasRestored.php @@ -31,6 +31,7 @@ class PurchaseOrderWasRestored public $event_vars; + public $fromDeleted; /** * Create a new event instance. * @@ -38,9 +39,10 @@ class PurchaseOrderWasRestored * @param Company $company * @param array $event_vars */ - public function __construct(PurchaseOrder $purchase_order, Company $company, array $event_vars) + public function __construct(PurchaseOrder $purchase_order, $fromDeleted, Company $company, array $event_vars) { $this->purchase_order = $purchase_order; + $this->fromDeleted = $fromDeleted; $this->company = $company; $this->event_vars = $event_vars; } diff --git a/app/Http/Controllers/PurchaseOrderController.php b/app/Http/Controllers/PurchaseOrderController.php index 8bb1f722ce..61877a56d6 100644 --- a/app/Http/Controllers/PurchaseOrderController.php +++ b/app/Http/Controllers/PurchaseOrderController.php @@ -477,7 +477,7 @@ class PurchaseOrderController extends BaseController $purchase_orders = PurchaseOrder::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get(); - if (! $invoices) { + if (! $purchase_orders) { return response()->json(['message' => 'No Purchase Orders Found']); } diff --git a/app/Listeners/PurchaseOrder/CreatePurchaseOrderActivity.php b/app/Listeners/PurchaseOrder/CreatePurchaseOrderActivity.php new file mode 100644 index 0000000000..f743c5fee5 --- /dev/null +++ b/app/Listeners/PurchaseOrder/CreatePurchaseOrderActivity.php @@ -0,0 +1,58 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->purchase_order->user_id; + + $fields->user_id = $user_id; + $fields->purchase_order_id = $event->purchase_order->id; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::CREATE_PURCHASE_ORDER; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderArchivedActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderArchivedActivity.php new file mode 100644 index 0000000000..09c8eb6797 --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderArchivedActivity.php @@ -0,0 +1,59 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->purchase_order->user_id; + + $fields->user_id = $user_id; + + $fields->purchase_order_id = $event->purchase_order->id; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::ARCHIVE_PURCHASE_ORDER; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderDeletedActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderDeletedActivity.php new file mode 100644 index 0000000000..8cfeda8a5e --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderDeletedActivity.php @@ -0,0 +1,58 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->purchase_order->user_id; + + $fields->user_id = $user_id; + $fields->purchase_order_id = $event->purchase_order->id; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::DELETE_PURCHASE_ORDER; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderEmailActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderEmailActivity.php new file mode 100644 index 0000000000..099ec3bad3 --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderEmailActivity.php @@ -0,0 +1,59 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->purchase_order->user_id; + + $fields->user_id = $user_id; + $fields->purchase_order_id = $event->invitation->purchase_order->id; + $fields->company_id = $event->invitation->purchase_order->company_id; + $fields->vendor_contact_id = $event->invitation->purchase_order->vendor_contact_id; + $fields->vendor_id = $event->invitation->purchase_order->vendor_id; + $fields->activity_type_id = Activity::EMAIL_PURCHASE_ORDER; + + $this->activity_repo->save($fields, $event->invitation->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderRestoredActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderRestoredActivity.php new file mode 100644 index 0000000000..367bf7874d --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderRestoredActivity.php @@ -0,0 +1,58 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->purchase_order->user_id; + + $fields->user_id = $user_id; + $fields->purchase_order_id = $event->purchase_order->id; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::RESTORE_PURCHASE_ORDER; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderViewedActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderViewedActivity.php new file mode 100644 index 0000000000..ea9a6a60b6 --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderViewedActivity.php @@ -0,0 +1,62 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDb($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->purchase_order->user_id; + + $event->invitation->purchase_order->service()->markSent()->save(); + + $fields->user_id = $user_id; + $fields->company_id = $event->invitation->company_id; + $fields->activity_type_id = Activity::VIEW_PURCHASE_ORDER; + $fields->vendor_id = $event->invitation->purchase_order->vendor_id; + $fields->vendor_contact_id = $event->invitation->vendor_contact_id; + $fields->invitation_id = $event->invitation->id; + $fields->purchase_order_id = $event->invitation->purchase_order_id; + + $this->activity_repo->save($fields, $event->invitation->purchase_order, $event->event_vars); + } +} diff --git a/app/Listeners/PurchaseOrder/UpdatePurchaseOrderActivity.php b/app/Listeners/PurchaseOrder/UpdatePurchaseOrderActivity.php new file mode 100644 index 0000000000..2b4b1039e7 --- /dev/null +++ b/app/Listeners/PurchaseOrder/UpdatePurchaseOrderActivity.php @@ -0,0 +1,58 @@ +activity_repo = $activity_repo; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle($event) + { + MultiDB::setDB($event->company->db); + + $fields = new stdClass; + + $user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->purchase_order->user_id; + + $fields->user_id = $user_id; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::UPDATE_PURCHASE_ORDER; + $fields->purchase_order_id = $event->purchase_order->id; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ec8508e8e2..b84ab256df 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -178,6 +178,13 @@ use App\Listeners\Payment\PaymentEmailFailureActivity; use App\Listeners\Payment\PaymentEmailedActivity; use App\Listeners\Payment\PaymentNotification; use App\Listeners\Payment\PaymentRestoredActivity; +use App\Listeners\PurchaseOrder\CreatePurchaseOrderActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderArchivedActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderDeletedActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderEmailActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderRestoredActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderViewedActivity; +use App\Listeners\PurchaseOrder\UpdatePurchaseOrderActivity; use App\Listeners\Quote\QuoteApprovedActivity; use App\Listeners\Quote\QuoteApprovedNotification; use App\Listeners\Quote\QuoteApprovedWebhook; @@ -444,20 +451,25 @@ class EventServiceProvider extends ServiceProvider PaymentEmailFailureActivity::class, ], PurchaseOrderWasArchived::class => [ + PurchaseOrderArchivedActivity::class, ], PurchaseOrderWasCreated::class => [ + CreatePurchaseOrderActivity::class, ], PurchaseOrderWasDeleted::class => [ + PurchaseOrderDeletedActivity::class, ], PurchaseOrderWasEmailed::class => [ - ], - PurchaseOrderWasMarkedSent::class => [ + PurchaseOrderEmailActivity::class, ], PurchaseOrderWasRestored::class => [ + PurchaseOrderRestoredActivity::class, ], PurchaseOrderWasUpdated::class => [ + UpdatePurchaseOrderActivity::class, ], PurchaseOrderWasViewed::class => [ + PurchaseOrderViewedActivity::class, ], CompanyDocumentsDeleted::class => [ DeleteCompanyDocuments::class, diff --git a/app/Services/PurchaseOrder/MarkSent.php b/app/Services/PurchaseOrder/MarkSent.php index c3b1df47b0..1450eefc1e 100644 --- a/app/Services/PurchaseOrder/MarkSent.php +++ b/app/Services/PurchaseOrder/MarkSent.php @@ -11,8 +11,6 @@ namespace App\Services\PurchaseOrder; - -use App\Events\PurchaseOrder\PurchaseOrderWasMarkedSent; use App\Models\PurchaseOrder; use App\Utils\Ninja; @@ -46,8 +44,6 @@ class MarkSent // ->touchPdf() ->save(); - event(new PurchaseOrderWasMarkedSent($this->purchase_order, $this->purchase_order->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - return $this->purchase_order; } } diff --git a/app/Transformers/PurchaseOrderTransformer.php b/app/Transformers/PurchaseOrderTransformer.php index 332879037a..7982fff5a6 100644 --- a/app/Transformers/PurchaseOrderTransformer.php +++ b/app/Transformers/PurchaseOrderTransformer.php @@ -89,7 +89,7 @@ class PurchaseOrderTransformer extends EntityTransformer 'custom_surcharge_tax3' => (bool)$purchase_order->custom_surcharge_tax3, 'custom_surcharge_tax4' => (bool)$purchase_order->custom_surcharge_tax4, 'line_items' => $purchase_order->line_items ?: (array)[], - 'entity_type' => 'credit', + 'entity_type' => 'purchase_order', 'exchange_rate' => (float)$purchase_order->exchange_rate, 'paid_to_date' => (float)$purchase_order->paid_to_date, 'subscription_id' => $this->encodePrimaryKey($purchase_order->subscription_id), diff --git a/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php b/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php index 4a3321de84..e914fefa7f 100644 --- a/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php +++ b/database/migrations/2022_06_01_224339_create_purchase_order_invitations_table.php @@ -57,6 +57,10 @@ class CreatePurchaseOrderInvitationsTable extends Migration $table->unsignedInteger('client_id')->nullable()->change(); }); + Schema::table('activities', function (Blueprint $table) { + $table->unsignedInteger('purchase_order_id')->nullable(); + $table->unsignedInteger('vendor_contact_id')->nullable(); + }); Company::cursor()->each(function ($company){