From 12ef9b79cc20d3a99411a961542faf5440e5bcb5 Mon Sep 17 00:00:00 2001 From: Artha Date: Mon, 24 Feb 2025 16:54:44 +0800 Subject: [PATCH] fix: download progress --- .../HumanResources/Employee/StoreEmployee.php | 10 ++-- .../Employee/UI/CreateEmployee.php | 1 - .../Inventory/Location/DownloadLocations.php | 28 +++++------ .../Inventory/Location/LocationsExport.php | 48 +++++++++---------- .../Inventory/Location/UI/IndexLocations.php | 14 +++--- app/Events/FileDownloadProgress.php | 45 +++++++++++++++++ ...436_add_more_fields_to_employees_table.php | 3 +- routes/channels.php | 4 ++ routes/grp/web/models/inventory/warehouse.php | 1 - tests/Feature/AikuSections/OrderingTest.php | 11 ++--- 10 files changed, 104 insertions(+), 61 deletions(-) create mode 100644 app/Events/FileDownloadProgress.php diff --git a/app/Actions/HumanResources/Employee/StoreEmployee.php b/app/Actions/HumanResources/Employee/StoreEmployee.php index 214b7ab01c..22f2005299 100644 --- a/app/Actions/HumanResources/Employee/StoreEmployee.php +++ b/app/Actions/HumanResources/Employee/StoreEmployee.php @@ -55,7 +55,7 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ $organisation = $parent; } - if(Arr::get($modelData, 'contact_address')) { + if (Arr::get($modelData, 'contact_address')) { $contactAddressData = Arr::get($modelData, 'contact_address', []); Arr::forget($modelData, 'contact_address'); } @@ -75,11 +75,11 @@ public function handle(Organisation|Workplace $parent, array $modelData): Employ $employee = $parent->employees()->create($modelData); $employee->stats()->create(); - if($contactAddressData){ + if ($contactAddressData) { $employee = $this->addAddressToModelFromArray( - model: $employee, - addressData: $contactAddressData, - ); + model: $employee, + addressData: $contactAddressData, + ); $employee->refresh(); } diff --git a/app/Actions/HumanResources/Employee/UI/CreateEmployee.php b/app/Actions/HumanResources/Employee/UI/CreateEmployee.php index cb0e47e549..f428adc23f 100644 --- a/app/Actions/HumanResources/Employee/UI/CreateEmployee.php +++ b/app/Actions/HumanResources/Employee/UI/CreateEmployee.php @@ -26,7 +26,6 @@ class CreateEmployee extends OrgAction { - public function handle(Organisation $organisation, ActionRequest $request): Response { return Inertia::render( diff --git a/app/Actions/Inventory/Location/DownloadLocations.php b/app/Actions/Inventory/Location/DownloadLocations.php index 0f4c48d01a..d3d1fa6071 100644 --- a/app/Actions/Inventory/Location/DownloadLocations.php +++ b/app/Actions/Inventory/Location/DownloadLocations.php @@ -1,4 +1,5 @@ id . '.xlsx'; - return Excel::download(new LocationsExport($warehouse, $modelData), $fileName); + + Excel::queue(new LocationsExport($warehouse, $modelData), 'public/'.$fileName)->chain([ + function () use ($warehouse, $fileName) { + broadcast(new FileDownloadProgress($warehouse->id, 100, $fileName)); + } + ]); } - public function asController(Organisation $organisation, Warehouse $warehouse, ActionRequest $request) + public function asController(Organisation $organisation, Warehouse $warehouse, ActionRequest $request): void { $this->initialisationFromWarehouse($warehouse, $request); $columns = explode(',', $request->query('columns', '')); - - return $this->handle($warehouse, $columns); + + $this->handle($warehouse, $columns); } -} \ No newline at end of file +} diff --git a/app/Actions/Inventory/Location/LocationsExport.php b/app/Actions/Inventory/Location/LocationsExport.php index 8e175155f6..b8dc8016ab 100644 --- a/app/Actions/Inventory/Location/LocationsExport.php +++ b/app/Actions/Inventory/Location/LocationsExport.php @@ -1,54 +1,54 @@ warehouse = $warehouse; $this->columns = $columns; } - public function map($row): array - { - return collect($this->columns)->map(fn($column) => $row->{$column})->toArray(); + { + $this->processed++; + + $progress = $this->totalCount > 0 + ? ($this->processed / $this->totalCount) * 100 + : 100; + + broadcast(new FileDownloadProgress($this->warehouse->id, (int) $progress)); + + return collect($this->columns)->map(fn ($col) => $row->{$col} ?? '')->toArray(); } public function headings(): array { - return array_map(fn($col) => str_replace('_', ' ', ucfirst($col)), $this->columns); + return array_map(fn ($col) => str_replace('_', ' ', ucfirst($col)), $this->columns); } + public function query() { - return Location::query()->where('warehouse_id', $this->warehouse->id); + $query = Location::query()->where('warehouse_id', $this->warehouse->id); + $this->totalCount = $query->count(); + + return $query; } -} \ No newline at end of file +} diff --git a/app/Actions/Inventory/Location/UI/IndexLocations.php b/app/Actions/Inventory/Location/UI/IndexLocations.php index 1cd50aa838..4fb7fcfd60 100644 --- a/app/Actions/Inventory/Location/UI/IndexLocations.php +++ b/app/Actions/Inventory/Location/UI/IndexLocations.php @@ -245,14 +245,14 @@ public function htmlResponse(LengthAwarePaginator $locations, ActionRequest $req $container = null; $export = []; $columns = collect([ - 'code', - 'status', - 'stock_value', - 'stock_commercial_value', - 'max_weight', - 'max_volume', + 'code', + 'status', + 'stock_value', + 'stock_commercial_value', + 'max_weight', + 'max_volume', 'barcode' - ])->map(fn($col) => [ + ])->map(fn ($col) => [ 'label' => __(str_replace('_', ' ', ucfirst($col))), // Convert _ to space and capitalize first letter 'value' => $col ])->toArray(); diff --git a/app/Events/FileDownloadProgress.php b/app/Events/FileDownloadProgress.php new file mode 100644 index 0000000000..e9c51988a5 --- /dev/null +++ b/app/Events/FileDownloadProgress.php @@ -0,0 +1,45 @@ +userId = $userId; + $this->progress = $progress; + $this->fileName = $fileName; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn(): Channel|PrivateChannel|array + { + return new PrivateChannel('grp.download-progress.' . $this->userId); + } + + public function broadcastAs(): string + { + return 'FileDownloadProgress'; + } +} diff --git a/database/migrations/2025_02_24_073436_add_more_fields_to_employees_table.php b/database/migrations/2025_02_24_073436_add_more_fields_to_employees_table.php index e7421392da..7acefb9545 100644 --- a/database/migrations/2025_02_24_073436_add_more_fields_to_employees_table.php +++ b/database/migrations/2025_02_24_073436_add_more_fields_to_employees_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class () extends Migration { /** * Run the migrations. * diff --git a/routes/channels.php b/routes/channels.php index af19383b6a..3246d4257c 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -19,6 +19,10 @@ return $userID === $user->id; }); +Broadcast::channel('grp.download-progress.{userID}', function (User $user, int $userID) { + return $userID === $user->id; +}); + Broadcast::channel('grp.{groupID}.general', function (User $user, int $groupID) { return $user->group_id === $groupID; }); diff --git a/routes/grp/web/models/inventory/warehouse.php b/routes/grp/web/models/inventory/warehouse.php index 8ed881bbaf..b316350beb 100644 --- a/routes/grp/web/models/inventory/warehouse.php +++ b/routes/grp/web/models/inventory/warehouse.php @@ -7,7 +7,6 @@ */ use App\Actions\Fulfilment\Pallet\UpdatePalletLocation; -use App\Actions\Inventory\Location\DownloadLocations; use App\Actions\Inventory\Location\ImportLocation; use App\Actions\Inventory\Location\StoreLocation; use App\Actions\Inventory\Warehouse\UpdateWarehouse; diff --git a/tests/Feature/AikuSections/OrderingTest.php b/tests/Feature/AikuSections/OrderingTest.php index 811ce4e609..72df0cbbc2 100644 --- a/tests/Feature/AikuSections/OrderingTest.php +++ b/tests/Feature/AikuSections/OrderingTest.php @@ -22,13 +22,11 @@ use App\Actions\Dropshipping\CustomerClient\UpdateCustomerClient; use App\Actions\Ordering\Adjustment\StoreAdjustment; use App\Actions\Ordering\Adjustment\UpdateAdjustment; -use App\Actions\Ordering\Order\DeleteOrder; use App\Actions\Ordering\Order\Search\ReindexOrdersSearch; use App\Actions\Ordering\Order\SendOrderToWarehouse; use App\Actions\Ordering\Order\StoreOrder; use App\Actions\Ordering\Order\UpdateOrder; use App\Actions\Ordering\Order\UpdateOrderStateToSubmitted; -use App\Actions\Ordering\Order\UpdateStateToCreatingOrder; use App\Actions\Ordering\Order\UpdateStateToFinalizedOrder; use App\Actions\Ordering\Order\UpdateStateToHandlingOrder; use App\Actions\Ordering\Order\UpdateStateToPackedOrder; @@ -70,7 +68,6 @@ use App\Models\Ordering\ShippingZoneSchema; use App\Models\Ordering\Transaction; use Carbon\Carbon; -use Illuminate\Validation\ValidationException; use Illuminate\Support\Facades\Date; use Inertia\Testing\AssertableInertia; @@ -347,7 +344,7 @@ $order->refresh(); expect($deliveryNote)->toBeInstanceOf(DeliveryNote::class) ->and($order->state)->toEqual(OrderStateEnum::IN_WAREHOUSE); - + return $order; })->depends('update order state to submitted'); @@ -356,7 +353,7 @@ $order->refresh(); expect($order)->toBeInstanceOf(Order::class) ->and($order->state)->toEqual(OrderStateEnum::HANDLING); - + return $order; })->depends('update order state to in warehouse'); @@ -365,7 +362,7 @@ $order->refresh(); expect($order)->toBeInstanceOf(Order::class) ->and($order->state)->toEqual(OrderStateEnum::PACKED); - + return $order; })->depends('update order state to Handling'); @@ -374,7 +371,7 @@ $order->refresh(); expect($order)->toBeInstanceOf(Order::class) ->and($order->state)->toEqual(OrderStateEnum::FINALISED); - + return $order; })->depends('update order state to Handling');