diff --git a/app/Console/Commands/FixDuplicateContainerReference.php b/app/Console/Commands/FixDuplicateContainerReference.php new file mode 100644 index 00000000..4ff87f86 --- /dev/null +++ b/app/Console/Commands/FixDuplicateContainerReference.php @@ -0,0 +1,77 @@ +info(Carbon::now() . ' : Started.'); + $start = new Carbon(); + + $duplicatedContainers = Container::select('reference', DB::raw('COUNT(reference) as count')) + ->groupBy('reference') + ->having('count', '>', 1) + ->get(); + + foreach ($duplicatedContainers as $duplicatedContainer) { + $containers = Container::where('reference', $duplicatedContainer->reference)->get(); + + $containers = $containers->sortByDesc(function ($container) { + return $container->packingLists->count(); + }); + + $firstContainer = $containers->shift(); + + foreach ($containers as $container) { + $firstContainer->packingLists()->syncWithoutDetaching($container->packingLists); + $container->packingLists()->detach(); + $this->info('Deleted container ' . $container->id); + $container->delete(); + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + + $this->info(Carbon::now() . ' : Done. ElapsedTime: ' . $elapsedTime); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 6164852a..be5caaa9 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -48,6 +48,11 @@ class Kernel extends ConsoleKernel // ->withoutOverlapping() // ->appendOutputTo (storage_path().'/logs/departure_email.log'); + $schedule->command('fix-duplicate-container-reference') + ->cron('0 1 * * *') + ->withoutOverlapping() + ->appendOutputTo (storage_path().'/logs/fix_duplicate_container_reference.log'); + $schedule->command('invoice:generate') ->hourly() ->withoutOverlapping()