From 0f32e43fb601d3afe1c2818a4627a1aeaf7012a0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 15 Jun 2022 16:27:21 +1000 Subject: [PATCH] Accept listener --- .../PurchaseOrderWasAccepted.php | 51 ++++++++++++++++ .../VendorPortal/PurchaseOrderController.php | 23 +++++-- app/Http/Livewire/PurchaseOrdersTable.php | 19 +++--- .../Misc/InvitationViewedListener.php | 6 +- .../PurchaseOrderAcceptedActivity.php | 61 +++++++++++++++++++ app/Mail/Admin/EntityViewedObject.php | 12 +++- app/Models/Activity.php | 3 +- app/Providers/EventServiceProvider.php | 7 ++- .../Traits/Notifications/UserNotifies.php | 11 +++- composer.json | 7 ++- resources/lang/en/texts.php | 1 + .../livewire/purchase-orders-table.blade.php | 12 ++-- routes/vendor.php | 2 +- 13 files changed, 182 insertions(+), 33 deletions(-) create mode 100644 app/Events/PurchaseOrder/PurchaseOrderWasAccepted.php create mode 100644 app/Listeners/PurchaseOrder/PurchaseOrderAcceptedActivity.php diff --git a/app/Events/PurchaseOrder/PurchaseOrderWasAccepted.php b/app/Events/PurchaseOrder/PurchaseOrderWasAccepted.php new file mode 100644 index 000000000000..ed2147b5fdb1 --- /dev/null +++ b/app/Events/PurchaseOrder/PurchaseOrderWasAccepted.php @@ -0,0 +1,51 @@ +purchase_order = $purchase_order; + $this->contact = $contact; + $this->company = $company; + $this->event_vars = $event_vars; + } +} diff --git a/app/Http/Controllers/VendorPortal/PurchaseOrderController.php b/app/Http/Controllers/VendorPortal/PurchaseOrderController.php index d285f455b988..6a821aaa1e1a 100644 --- a/app/Http/Controllers/VendorPortal/PurchaseOrderController.php +++ b/app/Http/Controllers/VendorPortal/PurchaseOrderController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers\VendorPortal; use App\Events\Misc\InvitationWasViewed; +use App\Events\PurchaseOrder\PurchaseOrderWasAccepted; use App\Events\PurchaseOrder\PurchaseOrderWasViewed; use App\Http\Controllers\Controller; use App\Http\Requests\VendorPortal\PurchaseOrders\ProcessPurchaseOrdersInBulkRequest; @@ -138,12 +139,26 @@ class PurchaseOrderController extends Controller $purchase_orders = PurchaseOrder::query() ->whereIn('id', $this->transformKeys($data['purchase_orders'])) ->where('company_id', auth()->guard('vendor')->user()->vendor->company_id) - ->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT]); + ->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT]) + ->cursor()->each(function ($purchase_order){ - $purchase_orders->update(['status_id' => PurchaseOrder::STATUS_ACCEPTED]); + $purchase_order->service() + ->markSent() + ->applyNumber() + ->setStatus(PurchaseOrder::STATUS_ACCEPTED) + ->save(); - if($purchase_orders->count() == 1) - return redirect()->route('vendor.purchase_order.show', ['purchase_order' => $purchase_orders->first()->hashed_id]); + event(new PurchaseOrderWasAccepted($purchase_order, auth()->guard('vendor')->user(), $purchase_order->company, Ninja::eventVars())); + + }); + + if(count($data['purchase_orders']) == 1){ + + $purchase_order = PurchaseOrder::whereIn('id', $this->transformKeys($data['purchase_orders']))->first(); + + return redirect()->route('vendor.purchase_order.show', ['purchase_order' => $purchase_order->hashed_id]); + + } else return redirect()->route('vendor.purchase_orders.index'); diff --git a/app/Http/Livewire/PurchaseOrdersTable.php b/app/Http/Livewire/PurchaseOrdersTable.php index 7ce7e01e79c0..e97264362d6b 100644 --- a/app/Http/Livewire/PurchaseOrdersTable.php +++ b/app/Http/Livewire/PurchaseOrdersTable.php @@ -46,22 +46,17 @@ class PurchaseOrdersTable extends Component $query = PurchaseOrder::query() ->with('vendor.contacts') ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc') + ->whereIn('status_id', [PurchaseOrder::STATUS_SENT, PurchaseOrder::STATUS_ACCEPTED]) ->where('company_id', $this->company->id) ->where('is_deleted', false); - // if (in_array('paid', $this->status)) { - // $local_status[] = Invoice::STATUS_PAID; - // } + if (in_array('sent', $this->status)) { + $local_status[] = PurchaseOrder::STATUS_SENT; + } - // if (in_array('unpaid', $this->status)) { - // $local_status[] = Invoice::STATUS_SENT; - // $local_status[] = Invoice::STATUS_PARTIAL; - // } - - // if (in_array('overdue', $this->status)) { - // $local_status[] = Invoice::STATUS_SENT; - // $local_status[] = Invoice::STATUS_PARTIAL; - // } + if (in_array('accepted', $this->status)) { + $local_status[] = PurchaseOrder::STATUS_ACCEPTED; + } if (count($local_status) > 0) { $query = $query->whereIn('status_id', array_unique($local_status)); diff --git a/app/Listeners/Misc/InvitationViewedListener.php b/app/Listeners/Misc/InvitationViewedListener.php index 773cc50be5d8..70275dc4498a 100644 --- a/app/Listeners/Misc/InvitationViewedListener.php +++ b/app/Listeners/Misc/InvitationViewedListener.php @@ -49,7 +49,9 @@ class InvitationViewedListener implements ShouldQueue if($entity_name == 'recurringInvoice') return; - + elseif($entity_name == 'purchaseOrder') + $entity_name = 'purchase_order'; + $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer( (new EntityViewedObject($invitation, $entity_name))->build() ); $nmo->company = $invitation->company; @@ -60,6 +62,8 @@ class InvitationViewedListener implements ShouldQueue $entity_viewed = "{$entity_name}_viewed"; $entity_viewed_all = "{$entity_name}_viewed_all"; + + $methods = $this->findUserNotificationTypes($invitation, $company_user, $entity_name, ['all_notifications', $entity_viewed, $entity_viewed_all]); if (($key = array_search('mail', $methods)) !== false) { diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedActivity.php b/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedActivity.php new file mode 100644 index 000000000000..6baba48e0914 --- /dev/null +++ b/app/Listeners/PurchaseOrder/PurchaseOrderAcceptedActivity.php @@ -0,0 +1,61 @@ +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; + + $event->purchase_order->service()->markSent()->save(); + + $fields->user_id = $user_id; + $fields->company_id = $event->purchase_order->company_id; + $fields->activity_type_id = Activity::ACCEPT_PURCHASE_ORDER; + $fields->vendor_id = $event->purchase_order->vendor_id; + $fields->vendor_contact_id = $event->contact->id; + $fields->purchase_order_id = $event->purchase_order->id; + + $this->activity_repo->save($fields, $event->purchase_order, $event->event_vars); + } +} diff --git a/app/Mail/Admin/EntityViewedObject.php b/app/Mail/Admin/EntityViewedObject.php index fa2f7b9d24a9..85e4d827c34c 100644 --- a/app/Mail/Admin/EntityViewedObject.php +++ b/app/Mail/Admin/EntityViewedObject.php @@ -65,7 +65,12 @@ class EntityViewedObject private function getAmount() { - return Number::formatMoney($this->entity->amount, $this->entity->client); + if($this->entity->client) + $currency_entity = $this->entity->client; + else + $currency_entity = $this->company; + + return Number::formatMoney($this->entity->amount, $currency_entity); } private function getSubject() @@ -82,7 +87,10 @@ class EntityViewedObject private function getData() { - $settings = $this->entity->client->getMergedSettings(); + if($this->entity->client) + $settings = $this->entity->client->getMergedSettings(); + else + $settings = $this->company->settings; $data = [ 'title' => $this->getSubject(), diff --git a/app/Models/Activity.php b/app/Models/Activity.php index f5a8ce9eb297..570fc16eda91 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -116,7 +116,8 @@ class Activity extends StaticModel const RESTORE_PURCHASE_ORDER = 134; const EMAIL_PURCHASE_ORDER = 135; const VIEW_PURCHASE_ORDER = 136; - + const ACCEPT_PURCHASE_ORDER = 137; + protected $casts = [ 'is_system' => 'boolean', 'updated_at' => 'timestamp', diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 3fe1741fd755..e619deb06319 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -60,11 +60,12 @@ use App\Events\Payment\PaymentWasRefunded; use App\Events\Payment\PaymentWasRestored; use App\Events\Payment\PaymentWasUpdated; use App\Events\Payment\PaymentWasVoided; -use App\Events\PurchaseOrder\PurchaseOrderWasMarkedSent; +use App\Events\PurchaseOrder\PurchaseOrderWasAccepted; use App\Events\PurchaseOrder\PurchaseOrderWasArchived; use App\Events\PurchaseOrder\PurchaseOrderWasCreated; use App\Events\PurchaseOrder\PurchaseOrderWasDeleted; use App\Events\PurchaseOrder\PurchaseOrderWasEmailed; +use App\Events\PurchaseOrder\PurchaseOrderWasMarkedSent; use App\Events\PurchaseOrder\PurchaseOrderWasRestored; use App\Events\PurchaseOrder\PurchaseOrderWasUpdated; use App\Events\PurchaseOrder\PurchaseOrderWasViewed; @@ -179,6 +180,7 @@ use App\Listeners\Payment\PaymentEmailedActivity; use App\Listeners\Payment\PaymentNotification; use App\Listeners\Payment\PaymentRestoredActivity; use App\Listeners\PurchaseOrder\CreatePurchaseOrderActivity; +use App\Listeners\PurchaseOrder\PurchaseOrderAcceptedActivity; use App\Listeners\PurchaseOrder\PurchaseOrderArchivedActivity; use App\Listeners\PurchaseOrder\PurchaseOrderDeletedActivity; use App\Listeners\PurchaseOrder\PurchaseOrderEmailActivity; @@ -471,6 +473,9 @@ class EventServiceProvider extends ServiceProvider PurchaseOrderWasViewed::class => [ PurchaseOrderViewedActivity::class, ], + PurchaseOrderWasAccepted::class => [ + PurchaseOrderAcceptedActivity::class, + ], CompanyDocumentsDeleted::class => [ DeleteCompanyDocuments::class, ], diff --git a/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index 3356a1b9f0f5..3995a4f21517 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -15,6 +15,7 @@ use App\Models\Client; use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; +use App\Models\PurchaseOrder; use App\Models\Quote; /** @@ -99,7 +100,10 @@ trait UserNotifies break; case ($entity instanceof Credit): return array_merge($required_permissions, ["all_notifications","all_user_notifications","credit_created_user","credit_sent_user","credit_viewed_user"]); - break; + break; + case ($entity instanceof PurchaseOrder): + return array_merge($required_permissions, ["all_notifications","all_user_notifications","purchase_order_created_user","purchase_order_sent_user","purchase_order_viewed_user"]); + break; default: return []; break; @@ -122,7 +126,10 @@ trait UserNotifies break; case ($entity instanceof Credit): return array_diff($required_permissions, ["all_user_notifications","credit_created_user","credit_sent_user","credit_viewed_user"]); - break; + break; + case ($entity instanceof PurchaseOrder): + return array_diff($required_permissions, ["all_user_notifications","purchase_order_created_user","purchase_order_sent_user","purchase_order_viewed_user"]); + break; default: // code... break; diff --git a/composer.json b/composer.json index 452f41241b6c..8c66241034cd 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,12 @@ "Credit card billing", "projects", "tasks", - "freelancer" + "freelancer", + "quotes", + "purchase orders", + "stripe billing", + "invoices", + "subscriptions" ], "license": "Elastic License", "authors": [ diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 00a578f1470d..b4fef4c8f6d3 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4629,6 +4629,7 @@ $LANG = array( 'purchase_orders' => 'Purchase Orders', 'purchase_order_number_placeholder' => 'Purchase Order # :purchase_order', 'accepted' => 'Accepted', + 'activity_137' => ':contact accepted purchase order :purchase_order', ); return $LANG; diff --git a/resources/views/portal/ninja2020/components/livewire/purchase-orders-table.blade.php b/resources/views/portal/ninja2020/components/livewire/purchase-orders-table.blade.php index 237003fcbff7..c207f6bc42e3 100644 --- a/resources/views/portal/ninja2020/components/livewire/purchase-orders-table.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/purchase-orders-table.blade.php @@ -11,16 +11,12 @@
- - + +
- - -
-
- - + +
diff --git a/routes/vendor.php b/routes/vendor.php index 6e42a60ddb4c..8af6f10189cf 100644 --- a/routes/vendor.php +++ b/routes/vendor.php @@ -33,7 +33,7 @@ Route::group(['middleware' => ['auth:vendor', 'vendor_locale', 'domain_db'], 'pr Route::get('profile/{vendor_contact}/edit', [PurchaseOrderController::class, 'index'])->name('profile.edit'); Route::post('purchase_orders/bulk', [PurchaseOrderController::class, 'bulk'])->name('purchase_orders.bulk'); - Route::post('logout', [VendorContactLoginController::class, 'logout'])->name('logout'); + Route::get('logout', [VendorContactLoginController::class, 'logout'])->name('logout'); });