fix duplicate packages

This commit is contained in:
Omair Saleh
2023-08-22 15:20:06 +08:00
parent 336cc0b37f
commit f8131b3a1e
+45 -1
View File
@@ -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:<br>";
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}<br>";
}
// 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<br><br>";
}
// Echo the total number of duplicate groups
echo "Total Number of Duplicate Groups: {$totalGroups}";
});