From 06134e006c702875489239dfa96b3a89c1324cf9 Mon Sep 17 00:00:00 2001 From: edmondlang Date: Sun, 23 Jul 2023 15:42:35 +0800 Subject: [PATCH] fix deleted packinglist --- app/Console/Commands/FixPackingList.php | 88 +++++++++++++++++++ app/Console/Kernel.php | 13 ++- .../Resources/MappableTransactionResource.php | 1 + 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 app/Console/Commands/FixPackingList.php diff --git a/app/Console/Commands/FixPackingList.php b/app/Console/Commands/FixPackingList.php new file mode 100644 index 00000000..4f766235 --- /dev/null +++ b/app/Console/Commands/FixPackingList.php @@ -0,0 +1,88 @@ +get(); + + if ($packingLists->isNotEmpty()) { + $this->info(Carbon::now() . ' : ' . $this->description . ' cron started.'); + $start = Carbon::now(); + + $restoredPackagesCount = 0; + $restoredPackagesIds = []; + + // Eager loading packages for all packing lists + $packingLists->load(['packages' => function ($query) { + $query->onlyTrashed(); + }]); + + foreach ($packingLists as $packingList) { + $deletedPackages = $packingList->packages->filter(function ($package) use ($restoredPackagesIds) { + // Filter out packages that have already been restored + return !in_array($package->id, $restoredPackagesIds); + }); + + if ($deletedPackages->isNotEmpty()) { + $groupedPackages = $deletedPackages->groupBy(function ($item) { + return $item->only(['type', 'description', 'width', 'height', 'length', 'weight', 'quantity']); + }); + + foreach ($groupedPackages as $group) { + $latestPackage = $group->sortByDesc('created_at')->first(); + + // Check if the package ID is already restored before attempting to restore it + if (!in_array($latestPackage->id, $restoredPackagesIds)) { + $latestPackage->restore(); + $this->info('Package ID: ' . $latestPackage->id . ' has been restored'); + $restoredPackagesCount++; + $restoredPackagesIds[] = $latestPackage->id; + } + } + } + } + + $end = Carbon::now(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + + $this->info(Carbon::now() . ' : Done ' . $this->description . '. ElapsedTime: ' . $elapsedTime . '.'); + $this->info('Restored Packages Count: ' . $restoredPackagesCount); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index f97f1529..6164852a 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -34,14 +34,19 @@ class Kernel extends ConsoleKernel ->appendOutputTo (storage_path().'/logs/curlvt.log'); $schedule->command('command:curlYdOrderListCommand') - ->cron('30 9-18/3 * * *') + ->cron('0 9-18/3 * * *') ->withoutOverlapping() ->appendOutputTo (storage_path().'/logs/curlyd.log'); - $schedule->command('command:curlYdOrderListCommand') - ->cron('0 9 * * *') + $schedule->command('fix-packinglist') + ->cron('30 9-18/3 * * *') ->withoutOverlapping() - ->appendOutputTo (storage_path().'/logs/departure_email.log'); + ->appendOutputTo (storage_path().'/logs/fix_packinglist.log'); + + // $schedule->command('command:curlYdOrderListCommand') + // ->cron('0 9 * * *') + // ->withoutOverlapping() + // ->appendOutputTo (storage_path().'/logs/departure_email.log'); $schedule->command('invoice:generate') ->hourly() diff --git a/app/Http/Resources/MappableTransactionResource.php b/app/Http/Resources/MappableTransactionResource.php index d69c145c..861693e1 100644 --- a/app/Http/Resources/MappableTransactionResource.php +++ b/app/Http/Resources/MappableTransactionResource.php @@ -31,6 +31,7 @@ class MappableTransactionResource extends JsonResource } return [ + 'status' => 'success', 'system' => 'SHIPPING_PORTAL', 'type' => $this->type, 'owner_type' => $this->owner_type,