From f8131b3a1eab36aeecde042f7374ee30237ae3a9 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Tue, 22 Aug 2023 15:20:06 +0800 Subject: [PATCH] fix duplicate packages --- routes/web.php | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/routes/web.php b/routes/web.php index cc5cafff..0f22e3a7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -35,6 +35,7 @@ use App\Models\CompanyConnection; use App\Models\CompanyModule; use App\Models\Document; use App\Models\Order; +use App\Models\Package; use App\Models\PackingList; use Carbon\Carbon; use Illuminate\Http\Request; @@ -1157,8 +1158,51 @@ Route::get('/duplicate-package-clean-up', function(){ ->where('quantity', $group->quantity) ->where('created_at', '>', $group->min_created_at) ->get(); - dump($deletableGroups); } + + // Fetching duplicate rows based on given columns + $duplicates = DB::table('packages') + ->select('packing_list_id', 'description', 'width', 'height', 'weight', 'quantity', DB::raw('COUNT(*) as count')) + ->groupBy('packing_list_id', 'description', 'width', 'height', 'weight', 'quantity') + ->havingRaw('COUNT(*) > 1') + ->get(); + + $totalGroups = 0; + + // Loop through each group of duplicates + foreach ($duplicates as $duplicate) { + $totalGroups++; + + // Fetch all rows of the current group and order by created_at + $rows = Package::where('packages') + ->where('packing_list_id', $duplicate->packing_list_id) + ->where('description', $duplicate->description) + ->where('width', $duplicate->width) + ->where('height', $duplicate->height) + ->where('weight', $duplicate->weight) + ->where('quantity', $duplicate->quantity) + ->orderBy('created_at') + ->get(); + + // Echo the group details + echo "Group {$totalGroups} Details:
"; + foreach ($rows as $row) { + echo "ID: {$row->id}, Packing List ID: {$row->packing_list_id}, Description: {$row->description}, Width: {$row->width}, Height: {$row->height}, Weight: {$row->weight}, Quantity: {$row->quantity}, Created At: {$row->created_at}
"; + } + // Delete all rows in the group except the first one (oldest based on created_at) +// $firstRow = $rows->shift(); +// foreach ($rows as $row) { +// Package::where('packages')->where('id', $row->id)->delete(); +// } + + // Calculate the case age for the group (assuming case age means difference between current date and created_at of the oldest row) + $caseAge = Carbon::now()->diffInDays(Carbon::parse($firstRow->created_at)); + echo "Case Age for Group {$totalGroups}: {$caseAge} days

"; + } + + // Echo the total number of duplicate groups + echo "Total Number of Duplicate Groups: {$totalGroups}"; + });