user(); if ($user->hasRole('vendor')) { $order_ids = DB::table('order_items')->where('channel_id', $user->channel()->id)->distinct()->pluck('order_id'); $query->whereIntegerInRaw('id', $order_ids); } return $query; } /** * After order was updated */ public static function afterUpdate(NovaRequest $request, Model $model): void { if ($model->status === OrderStatus::CANCELLED) { $orderItems = $model->items()->with('product')->get(['id', 'product_id', 'order_id', 'quantity']); $orderItems->each(function ($item) { $item->product->update([ 'stock' => intval($item->product->stock) + intval($item->quantity), ]); }); } } /** * Get the fields for index. */ public function fieldsForIndex(NovaRequest $request): array { return OrderFieldsForIndex::make($this, $request); } /** * Get the fields for create. */ public function fieldsForCreate(NovaRequest $request): array { return OrderFieldsForCreate::make($this, $request); } /** * Get the fields displayed by the resource on detail page. */ public function fieldsForDetail(NovaRequest $request): array { return OrderFieldsForDetail::make($this, $request); } /** * Get the fields displayed by the resource on detail page. */ public function fieldsForUpdate(NovaRequest $request): array { return OrderFieldsForUpdate::make($this, $request); } /** * Get the fields displayed by the resource. */ public function fields(NovaRequest $request): array { return [ ID::make()->sortable(), HasMany::make(__('Products'), 'items', OrderItem::class), ]; } /** * Get the actions available for the resource. */ public function actions(NovaRequest $request): array { return [ ExportOrderReportAction::make() ->standalone() ->canSeeWhen('isAdmin', $this) ->onlyOnIndex() ->icon(IconRepository::make()->documentReport()), ExportOrderInvoiceAction::make() ->confirmText(__('Are you sure you want to run this action?')) ->confirmButtonText(__('Yes')) ->cancelButtonText(__('No')) ->exceptOnIndex() ->icon(IconRepository::make()->documentDownload()), ]; } /** * Get the filters available for the resource. */ public function filters(NovaRequest $request): array { return [ RegionFilter::make(), StatusFilter::make(), ]; } /** * Get the lenses available for the resource. */ public function lenses(NovaRequest $request): array { return [ ]; } }