mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-23 06:24:10 +00:00
57 lines
1.8 KiB
PHP
57 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Jobs\Commands\V2;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use App\Models\Container;
|
|
use App\Models\ContainerPackingList;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class FixDuplicateContainerReferenceV2CommandJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
public function handle()
|
|
{
|
|
Log::info(Carbon::now() . ': Start job - Fix Duplicate Container Reference.');
|
|
$start = new Carbon();
|
|
|
|
// ini_set('memory_limit', '-1');
|
|
|
|
|
|
$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();
|
|
Log::info('Deleted container ' . $container->id);
|
|
$container->delete();
|
|
}
|
|
}
|
|
|
|
|
|
$end = new Carbon();
|
|
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
|
|
Log::info(Carbon::now() . ': End job - Fix Duplicate Container Reference. ElapsedTime: ' . $elapsedTime . '.');
|
|
}
|
|
}
|