Merge pull request 'Changed whole logic gainst based on week upload production plan import' (#1371) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Reviewed-on: #1371
This commit was merged in pull request #1371.
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Models\Block;
|
|||||||
use App\Models\Item;
|
use App\Models\Item;
|
||||||
use App\Models\Line;
|
use App\Models\Line;
|
||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
|
use App\Models\ProductionCalendar;
|
||||||
use App\Models\ProductionPlan;
|
use App\Models\ProductionPlan;
|
||||||
use App\Models\Shift;
|
use App\Models\Shift;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
@@ -35,7 +36,7 @@ class ProductionPlanImporter extends Importer
|
|||||||
ImportColumn::make('plant')
|
ImportColumn::make('plant')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plant Code')
|
->exampleHeader('Plant Code')
|
||||||
->example(['1000', '1000'])
|
->example(['1000'])
|
||||||
->label('Plant Code')
|
->label('Plant Code')
|
||||||
->relationship(resolveUsing: 'code')
|
->relationship(resolveUsing: 'code')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
@@ -49,17 +50,37 @@ class ProductionPlanImporter extends Importer
|
|||||||
ImportColumn::make('item')
|
ImportColumn::make('item')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Item Code')
|
->exampleHeader('Item Code')
|
||||||
->example(['123456', '210987'])
|
->example(['123456'])
|
||||||
->label('Item Code')
|
->label('Item Code')
|
||||||
->relationship(resolveUsing: 'code')
|
->relationship(resolveUsing: 'code')
|
||||||
->rules(['required']),
|
->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()
|
->requiredMapping()
|
||||||
->exampleHeader('Plan Quantity')
|
->exampleHeader('Week 1')
|
||||||
->example(['500', '450'])
|
->label('Week 1'),
|
||||||
->label('Plan Quantity')
|
ImportColumn::make('week_2')
|
||||||
->numeric()
|
->requiredMapping()
|
||||||
->rules(['required', 'integer']),
|
->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')
|
// ImportColumn::make('production_quantity')
|
||||||
// ->requiredMapping()
|
// ->requiredMapping()
|
||||||
// ->exampleHeader('Production Quantity')
|
// ->exampleHeader('Production Quantity')
|
||||||
@@ -151,48 +172,332 @@ class ProductionPlanImporter extends Importer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! empty($warnMsg)) {
|
foreach (['week_1', 'week_2', 'week_3', 'week_4'] as $week) {
|
||||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
$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();
|
$recordDate = now();
|
||||||
$month = $recordDate->month;
|
$month = $recordDate->month;
|
||||||
$year = $recordDate->year;
|
$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)
|
->whereMonth('created_at', $month)
|
||||||
->whereYear('created_at', $year)
|
->whereYear('created_at', $year)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$workingDays = $previousRecord?->working_days ?? null;
|
$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)
|
$productionPlan1 = ProductionPlan::where('plant_id', $plant->id)
|
||||||
->where('item_id', $itemAgaPlant->id)
|
->where('item_id', $itemAgaPlant->id)
|
||||||
->whereYear('created_at', $recordDate->year)
|
->whereYear('created_at', $year)
|
||||||
->whereMonth('created_at', $recordDate->month)
|
->whereMonth('created_at', $month)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if ($productionPlan1) {
|
// Initialize daily data
|
||||||
$productionPlan1->update([
|
$dailyData = [];
|
||||||
'line_id' => $line,
|
|
||||||
'plan_quantity' => $this->data['plan_quantity'],
|
for ($day = 1; $day <= 31; $day++) {
|
||||||
'working_days' => $workingDays,
|
|
||||||
'operator_id' => $operatorName,
|
if ($day <= $totalDays) {
|
||||||
'updated_at' => now(),
|
$dailyData['day_' . $day] =
|
||||||
]);
|
(int) ($productionPlan1->{'day_' . $day} ?? 0);
|
||||||
} else {
|
} else {
|
||||||
ProductionPlan::create([
|
// Invalid dates must always remain zero
|
||||||
'plant_id' => $plant->id,
|
$dailyData['day_' . $day] = 0;
|
||||||
'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(),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user