diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasArchived.php b/app/Events/PurchaseOrder/PurchaseOrderWasArchived.php new file mode 100644 index 000000000000..cb6a29d1dc79 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasArchived.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasCreated.php b/app/Events/PurchaseOrder/PurchaseOrderWasCreated.php new file mode 100644 index 000000000000..e27497efd9e9 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasCreated.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasDeleted.php b/app/Events/PurchaseOrder/PurchaseOrderWasDeleted.php new file mode 100644 index 000000000000..0061f8759817 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasDeleted.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasEmailed.php b/app/Events/PurchaseOrder/PurchaseOrderWasEmailed.php new file mode 100644 index 000000000000..7836a8442636 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasEmailed.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php b/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php index 3b17142b9aab..8b6a0d87c3ff 100644 --- a/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php +++ b/app/Events/PurchaseOrder/PurchaseOrderWasMarkedSent.php @@ -1,5 +1,13 @@ purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasUpdated.php b/app/Events/PurchaseOrder/PurchaseOrderWasUpdated.php new file mode 100644 index 000000000000..bf241d002b92 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasUpdated.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasViewed.php b/app/Events/PurchaseOrder/PurchaseOrderWasViewed.php new file mode 100644 index 000000000000..7a0d67b56615 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasViewed.php @@ -0,0 +1,47 @@ +purchase_order = $purchase_order; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Http/Controllers/PurchaseOrderController.php b/app/Http/Controllers/PurchaseOrderController.php index 60c8a33138c0..8bb1f722ceaf 100644 --- a/app/Http/Controllers/PurchaseOrderController.php +++ b/app/Http/Controllers/PurchaseOrderController.php @@ -12,6 +12,8 @@ namespace App\Http\Controllers; +use App\Events\PurchaseOrder\PurchaseOrderWasCreated; +use App\Events\PurchaseOrder\PurchaseOrderWasUpdated; use App\Factory\PurchaseOrderFactory; use App\Filters\PurchaseOrderFilters; use App\Http\Requests\PurchaseOrder\ActionPurchaseOrderRequest; @@ -26,6 +28,7 @@ use App\Models\Client; use App\Models\PurchaseOrder; use App\Repositories\PurchaseOrderRepository; use App\Transformers\PurchaseOrderTransformer; +use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use Illuminate\Http\Response; @@ -182,6 +185,8 @@ class PurchaseOrderController extends BaseController ->fillDefaults() ->save(); + event(new PurchaseOrderWasCreated($purchase_order, $purchase_order->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->itemResponse($purchase_order); } /** @@ -352,6 +357,8 @@ class PurchaseOrderController extends BaseController $purchase_order = $this->purchase_order_repository->save($request->all(), $purchase_order); + event(new PurchaseOrderWasUpdated($purchase_order, $purchase_order->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->itemResponse($purchase_order); } /** diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d2b7e232acfa..ec8508e8e2ff 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -61,6 +61,13 @@ use App\Events\Payment\PaymentWasRestored; use App\Events\Payment\PaymentWasUpdated; use App\Events\Payment\PaymentWasVoided; use App\Events\PurchaseOrder\PurchaseOrderWasMarkedSent; +use App\Events\PurchaseOrder\PurchaseOrderWasArchived; +use App\Events\PurchaseOrder\PurchaseOrderWasCreated; +use App\Events\PurchaseOrder\PurchaseOrderWasDeleted; +use App\Events\PurchaseOrder\PurchaseOrderWasEmailed; +use App\Events\PurchaseOrder\PurchaseOrderWasRestored; +use App\Events\PurchaseOrder\PurchaseOrderWasUpdated; +use App\Events\PurchaseOrder\PurchaseOrderWasViewed; use App\Events\Quote\QuoteWasApproved; use App\Events\Quote\QuoteWasArchived; use App\Events\Quote\QuoteWasCreated; @@ -436,6 +443,22 @@ class EventServiceProvider extends ServiceProvider PaymentWasEmailedAndFailed::class => [ PaymentEmailFailureActivity::class, ], + PurchaseOrderWasArchived::class => [ + ], + PurchaseOrderWasCreated::class => [ + ], + PurchaseOrderWasDeleted::class => [ + ], + PurchaseOrderWasEmailed::class => [ + ], + PurchaseOrderWasMarkedSent::class => [ + ], + PurchaseOrderWasRestored::class => [ + ], + PurchaseOrderWasUpdated::class => [ + ], + PurchaseOrderWasViewed::class => [ + ], CompanyDocumentsDeleted::class => [ DeleteCompanyDocuments::class, ], @@ -558,9 +581,7 @@ class EventServiceProvider extends ServiceProvider ], VendorWasUpdated::class => [ VendorUpdatedActivity::class, - ], - PurchaseOrderWasMarkedSent::class => [ - ], + ] ]; diff --git a/tests/Integration/EventTest.php b/tests/Integration/EventTest.php index 4445cb2711f8..d13e61f1d632 100644 --- a/tests/Integration/EventTest.php +++ b/tests/Integration/EventTest.php @@ -31,6 +31,11 @@ use App\Events\Invoice\InvoiceWasDeleted; use App\Events\Invoice\InvoiceWasRestored; use App\Events\Invoice\InvoiceWasUpdated; use App\Events\Payment\PaymentWasArchived; +use App\Events\PurchaseOrder\PurchaseOrderWasCreated; +use App\Events\PurchaseOrder\PurchaseOrderWasUpdated; +use App\Events\PurchaseOrder\PurchaseOrderWasArchived; +use App\Events\PurchaseOrder\PurchaseOrderWasRestored; +use App\Events\PurchaseOrder\PurchaseOrderWasDeleted; use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasDeleted; use App\Events\Payment\PaymentWasRestored; @@ -807,4 +812,77 @@ class EventTest extends TestCase ->assertStatus(200); } + +public function PurchaseOrderEvents() + { + + /* Test fire new invoice */ + $data = [ + 'client_id' => $this->vendor->hashed_id, + 'number' => 'dude', + ]; + + $this->expectsEvents([ + PurchaseOrderWasCreated::class, + PurchaseOrderWasUpdated::class, + PurchaseOrderWasArchived::class, + PurchaseOrderWasRestored::class, + PurchaseOrderWasDeleted::class, + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/purchase_orders/', $data) + ->assertStatus(200); + + + $arr = $response->json(); + + $data = [ + 'client_id' => $this->vendor->hashed_id, + 'number' => 'dude2', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/purchase_orders/' . $arr['data']['id'], $data) + ->assertStatus(200); + + + $data = [ + 'ids' => [$arr['data']['id']], + ]; + + $quote = PurchaseOrder::find($this->decodePrimaryKey($arr['data']['id'])); + $quote->status_id = PurchaseOrder::STATUS_SENT; + $quote->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/purchase_orders/bulk?action=archive', $data) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/purchase_orders/bulk?action=restore', $data) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/purchase_orders/bulk?action=approve', $data) + ->assertStatus(200); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/purchase_orders/bulk?action=delete', $data) + ->assertStatus(200); + } + + }