Changed whole logic gainst based on week upload production plan import
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Failing after 20s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 18s
Laravel Larastan / larastan (pull_request) Failing after 40s
Laravel Pint / pint (pull_request) Successful in 3m56s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Failing after 20s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 18s
Laravel Larastan / larastan (pull_request) Failing after 40s
Laravel Pint / pint (pull_request) Successful in 3m56s
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Models\Block;
|
||||
use App\Models\Item;
|
||||
use App\Models\Line;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProductionCalendar;
|
||||
use App\Models\ProductionPlan;
|
||||
use App\Models\Shift;
|
||||
use App\Models\User;
|
||||
@@ -35,7 +36,7 @@ class ProductionPlanImporter extends Importer
|
||||
ImportColumn::make('plant')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Plant Code')
|
||||
->example(['1000', '1000'])
|
||||
->example(['1000'])
|
||||
->label('Plant Code')
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
@@ -49,17 +50,37 @@ class ProductionPlanImporter extends Importer
|
||||
ImportColumn::make('item')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Item Code')
|
||||
->example(['123456', '210987'])
|
||||
->example(['123456'])
|
||||
->label('Item Code')
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('plan_quantity')
|
||||
// ImportColumn::make('plan_quantity')
|
||||
// ->requiredMapping()
|
||||
// ->exampleHeader('Plan Quantity')
|
||||
// ->example(['500', '450'])
|
||||
// ->label('Plan Quantity')
|
||||
// ->numeric()
|
||||
// ->rules(['required', 'integer']),
|
||||
ImportColumn::make('week_1')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Plan Quantity')
|
||||
->example(['500', '450'])
|
||||
->label('Plan Quantity')
|
||||
->numeric()
|
||||
->rules(['required', 'integer']),
|
||||
->exampleHeader('Week 1')
|
||||
->label('Week 1'),
|
||||
ImportColumn::make('week_2')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Week 2')
|
||||
->label('Week 2'),
|
||||
ImportColumn::make('week_3')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Week 3')
|
||||
->label('Week 3'),
|
||||
ImportColumn::make('week_4')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Week 4')
|
||||
->label('Week 4'),
|
||||
ImportColumn::make('week_5')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Week 5')
|
||||
->label('Week 5'),
|
||||
// ImportColumn::make('production_quantity')
|
||||
// ->requiredMapping()
|
||||
// ->exampleHeader('Production Quantity')
|
||||
@@ -151,48 +172,332 @@ class ProductionPlanImporter extends Importer
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
foreach (['week_1', 'week_2', 'week_3', 'week_4'] as $week) {
|
||||
$value = $this->data[$week] ?? null;
|
||||
|
||||
if (filled($value) && (! is_numeric($value) || $value < 0)) {
|
||||
$warnMsg[] = "Invalid {$week} value found";
|
||||
}
|
||||
}
|
||||
|
||||
$week5 = $this->data['week_5'] ?? null;
|
||||
|
||||
if (
|
||||
filled($week5) &&
|
||||
(! is_numeric($week5) || $week5 < 0)
|
||||
) {
|
||||
$warnMsg[] = 'Invalid week_5 value found';
|
||||
}
|
||||
|
||||
$recordDate = now();
|
||||
$month = $recordDate->month;
|
||||
$year = $recordDate->year;
|
||||
|
||||
$previousRecord = ProductionPlan::where('plant_id', $plant->id)
|
||||
$previousRecord = ProductionCalendar::where('plant_id', $plant->id)
|
||||
->whereMonth('created_at', $month)
|
||||
->whereYear('created_at', $year)
|
||||
->first();
|
||||
|
||||
if(!$previousRecord){
|
||||
$warnMsg[] = 'Update this month production calendar';
|
||||
}
|
||||
|
||||
if (! empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
|
||||
$recordDate = now();
|
||||
$month = $recordDate->month;
|
||||
$year = $recordDate->year;
|
||||
|
||||
$totalDays = $recordDate->daysInMonth;
|
||||
|
||||
$previousRecord = ProductionCalendar::where('plant_id', $plant->id)
|
||||
->whereMonth('created_at', $month)
|
||||
->whereYear('created_at', $year)
|
||||
->first();
|
||||
|
||||
$workingDays = $previousRecord?->working_days ?? null;
|
||||
|
||||
// Get leave dates
|
||||
$leaveDates = array_filter(
|
||||
array_map(
|
||||
'trim',
|
||||
explode(',', (string) ($previousRecord?->leave_dates ?? ''))
|
||||
)
|
||||
);
|
||||
|
||||
// Convert leave dates into day numbers
|
||||
$leaveDayNumbers = [];
|
||||
|
||||
foreach ($leaveDates as $leaveDate) {
|
||||
try {
|
||||
$date = Carbon::parse($leaveDate);
|
||||
|
||||
if ($date->format('Y-m') === $recordDate->format('Y-m')) {
|
||||
$leaveDayNumbers[] = (int) $date->format('d');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Ignore invalid leave dates
|
||||
}
|
||||
}
|
||||
|
||||
// Get existing production plan for this item
|
||||
$productionPlan1 = ProductionPlan::where('plant_id', $plant->id)
|
||||
->where('item_id', $itemAgaPlant->id)
|
||||
->whereYear('created_at', $recordDate->year)
|
||||
->whereMonth('created_at', $recordDate->month)
|
||||
->whereYear('created_at', $year)
|
||||
->whereMonth('created_at', $month)
|
||||
->first();
|
||||
|
||||
if ($productionPlan1) {
|
||||
$productionPlan1->update([
|
||||
'line_id' => $line,
|
||||
'plan_quantity' => $this->data['plan_quantity'],
|
||||
'working_days' => $workingDays,
|
||||
'operator_id' => $operatorName,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
} else {
|
||||
ProductionPlan::create([
|
||||
'plant_id' => $plant->id,
|
||||
'line_id' => $line,
|
||||
'item_id' => $itemAgaPlant->id,
|
||||
'plan_quantity' => $this->data['plan_quantity'],
|
||||
'working_days' => $workingDays,
|
||||
'operator_id' => $operatorName,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
// Initialize daily data
|
||||
$dailyData = [];
|
||||
|
||||
for ($day = 1; $day <= 31; $day++) {
|
||||
|
||||
if ($day <= $totalDays) {
|
||||
$dailyData['day_' . $day] =
|
||||
(int) ($productionPlan1->{'day_' . $day} ?? 0);
|
||||
} else {
|
||||
// Invalid dates must always remain zero
|
||||
$dailyData['day_' . $day] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Weekly ranges
|
||||
$weekRanges = [
|
||||
'week_1' => [1, min(7, $totalDays)],
|
||||
'week_2' => [8, min(14, $totalDays)],
|
||||
'week_3' => [15, min(21, $totalDays)],
|
||||
'week_4' => [22, min(28, $totalDays)],
|
||||
'week_5' => [29, min(31, $totalDays)],
|
||||
];
|
||||
|
||||
// Process weekly values
|
||||
foreach ($weekRanges as $week => [$start, $end]) {
|
||||
|
||||
if ($start > $totalDays) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$weeklyValue = $this->data[$week] ?? null;
|
||||
|
||||
// Empty Excel value: retain existing daily values
|
||||
if ($weeklyValue === null || $weeklyValue === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get working days excluding leave dates
|
||||
$remainingDays = [];
|
||||
|
||||
for ($day = $start; $day <= $end; $day++) {
|
||||
|
||||
if (! in_array($day, $leaveDayNumbers, true)) {
|
||||
$remainingDays[] = $day;
|
||||
} else {
|
||||
$dailyData['day_' . $day] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$daysCount = count($remainingDays);
|
||||
|
||||
if ($daysCount === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Replace existing weekly quantity with Excel quantity
|
||||
$weeklyTotal = (int) round((float) $weeklyValue);
|
||||
|
||||
$dailyValue = intdiv($weeklyTotal, $daysCount);
|
||||
$remainder = $weeklyTotal % $daysCount;
|
||||
|
||||
foreach ($remainingDays as $index => $day) {
|
||||
|
||||
$dailyData['day_' . $day] =
|
||||
$dailyValue + ($index < $remainder ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate final monthly plan quantity
|
||||
$planQuantity = array_sum($dailyData);
|
||||
|
||||
// Prepare plan data
|
||||
$planData = array_merge([
|
||||
'line_id' => $line,
|
||||
'plan_quantity' => $planQuantity,
|
||||
'working_days' => $workingDays,
|
||||
'operator_id' => $operatorName,
|
||||
'updated_at' => now(),
|
||||
], $dailyData);
|
||||
|
||||
// Update or create production plan
|
||||
if ($productionPlan1) {
|
||||
|
||||
$productionPlan1->update($planData);
|
||||
|
||||
} else {
|
||||
|
||||
ProductionPlan::create(array_merge($planData, [
|
||||
'plant_id' => $plant->id,
|
||||
'item_id' => $itemAgaPlant->id,
|
||||
'created_at' => now(),
|
||||
]));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//..Working Logic
|
||||
|
||||
// $recordDate = now();
|
||||
// $month = $recordDate->month;
|
||||
// $year = $recordDate->year;
|
||||
|
||||
// $previousRecord = ProductionPlan::where('plant_id', $plant->id)
|
||||
// ->whereMonth('created_at', $month)
|
||||
// ->whereYear('created_at', $year)
|
||||
// ->first();
|
||||
|
||||
// $workingDays = $previousRecord?->working_days ?? null;
|
||||
|
||||
// // Get leave dates from the existing production plan
|
||||
// $leaveDates = array_filter(
|
||||
// array_map('trim', explode(',', (string) ($previousRecord?->leave_dates ?? '')))
|
||||
// );
|
||||
|
||||
// // Convert leave dates into day numbers for the current month
|
||||
// $leaveDayNumbers = [];
|
||||
|
||||
// foreach ($leaveDates as $leaveDate) {
|
||||
// $date = Carbon::parse($leaveDate);
|
||||
|
||||
// if ($date->format('Y-m') === $recordDate->format('Y-m')) {
|
||||
// $leaveDayNumbers[] = (int) $date->format('d');
|
||||
// }
|
||||
// }
|
||||
|
||||
// $totalDays = $recordDate->daysInMonth;
|
||||
|
||||
// $dailyData = [];
|
||||
|
||||
// for ($day = 1; $day <= 31; $day++) {
|
||||
// $dailyData['day_' . $day] = 0;
|
||||
// }
|
||||
|
||||
// $planQuantity = 0;
|
||||
|
||||
// foreach (['week_1', 'week_2', 'week_3', 'week_4', 'week_5'] as $week) {
|
||||
// $planQuantity += (float) ($this->data[$week] ?? 0);
|
||||
// }
|
||||
|
||||
// $planQuantity = (int) $planQuantity;
|
||||
|
||||
// // Dynamic weekly ranges
|
||||
// $weekRanges = [
|
||||
// 'week_1' => [1, min(7, $totalDays)],
|
||||
// 'week_2' => [8, min(14, $totalDays)],
|
||||
// 'week_3' => [15, min(21, $totalDays)],
|
||||
// 'week_4' => [22, min(28, $totalDays)],
|
||||
// 'week_5' => [29, min(31, $totalDays)],
|
||||
// ];
|
||||
|
||||
// foreach ($weekRanges as $week => [$start, $end]) {
|
||||
|
||||
// if ($start > $totalDays) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $weeklyValue = $this->data[$week] ?? null;
|
||||
|
||||
// if ($weeklyValue === null || $weeklyValue === '') {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $remainingDays = [];
|
||||
|
||||
// for ($day = $start; $day <= $end; $day++) {
|
||||
// if (! in_array($day, $leaveDayNumbers)) {
|
||||
// $remainingDays[] = $day;
|
||||
// }
|
||||
// }
|
||||
|
||||
// $daysCount = count($remainingDays);
|
||||
|
||||
// if ($daysCount === 0) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $weeklyTotal = (int) round((float) $weeklyValue);
|
||||
|
||||
// $dailyValue = intdiv($weeklyTotal, $daysCount);
|
||||
// $remainder = $weeklyTotal % $daysCount;
|
||||
|
||||
// foreach ($remainingDays as $index => $day) {
|
||||
// $dailyData['day_' . $day] =
|
||||
// $dailyValue + ($index < $remainder ? 1 : 0);
|
||||
// }
|
||||
// }
|
||||
|
||||
// $dailyTotal = array_sum($dailyData);
|
||||
|
||||
// $productionPlan1 = ProductionPlan::where('plant_id', $plant->id)
|
||||
// ->where('item_id', $itemAgaPlant->id)
|
||||
// ->whereYear('created_at', $recordDate->year)
|
||||
// ->whereMonth('created_at', $recordDate->month)
|
||||
// ->first();
|
||||
|
||||
// $planData = array_merge([
|
||||
// 'line_id' => $line,
|
||||
// 'plan_quantity' => $planQuantity,
|
||||
// 'working_days' => $workingDays,
|
||||
// 'operator_id' => $operatorName,
|
||||
// 'updated_at' => now(),
|
||||
// ], $dailyData);
|
||||
|
||||
// if ($productionPlan1) {
|
||||
|
||||
// $productionPlan1->update($planData);
|
||||
|
||||
// } else {
|
||||
|
||||
// ProductionPlan::create(array_merge($planData, [
|
||||
// 'plant_id' => $plant->id,
|
||||
// 'item_id' => $itemAgaPlant->id,
|
||||
// 'created_at' => now(),
|
||||
// ]));
|
||||
|
||||
// }
|
||||
|
||||
//..Old Logic
|
||||
|
||||
// $productionPlan1 = ProductionPlan::where('plant_id', $plant->id)
|
||||
// ->where('item_id', $itemAgaPlant->id)
|
||||
// ->whereYear('created_at', $recordDate->year)
|
||||
// ->whereMonth('created_at', $recordDate->month)
|
||||
// ->first();
|
||||
|
||||
// if ($productionPlan1) {
|
||||
// $productionPlan1->update([
|
||||
// 'line_id' => $line,
|
||||
// 'plan_quantity' => $this->data['plan_quantity'],
|
||||
// 'working_days' => $workingDays,
|
||||
// 'operator_id' => $operatorName,
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// } else {
|
||||
// ProductionPlan::create([
|
||||
// 'plant_id' => $plant->id,
|
||||
// 'line_id' => $line,
|
||||
// 'item_id' => $itemAgaPlant->id,
|
||||
// 'plan_quantity' => $this->data['plan_quantity'],
|
||||
// 'working_days' => $workingDays,
|
||||
// 'operator_id' => $operatorName,
|
||||
// 'created_at' => now(),
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user