Merge pull request 'Added day plan target import page' (#1375) 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: #1375
This commit was merged in pull request #1375.
This commit is contained in:
188
app/Imports/DayPlanTargetImport.php
Normal file
188
app/Imports/DayPlanTargetImport.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\Item;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProductionCalendar;
|
||||
use App\Models\ProductionPlan;
|
||||
use App\Models\ProductionPlanTarget;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Exception;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class DayPlanTargetImport implements ToCollection, WithHeadingRow, WithCalculatedFormulas
|
||||
{
|
||||
protected $plantId;
|
||||
protected $lineId;
|
||||
protected $year;
|
||||
protected $month;
|
||||
protected $week;
|
||||
|
||||
public function __construct($plantId, $lineId, $year, $month, $week)
|
||||
{
|
||||
$this->plantId = $plantId;
|
||||
$this->lineId = $lineId;
|
||||
$this->year = $year;
|
||||
$this->month = $month;
|
||||
$this->week = strtolower($week);
|
||||
}
|
||||
|
||||
|
||||
public function collection(Collection $rows)
|
||||
{
|
||||
$weekMapping = [
|
||||
'week_1' => 1,
|
||||
'week_2' => 8,
|
||||
'week_3' => 15,
|
||||
'week_4' => 22,
|
||||
'week_5' => 29,
|
||||
];
|
||||
|
||||
if (!isset($weekMapping[$this->week])) {
|
||||
throw new Exception('Invalid week selected.');
|
||||
}
|
||||
|
||||
$startDay = $weekMapping[$this->week];
|
||||
|
||||
$daysInMonth = cal_days_in_month(
|
||||
CAL_GREGORIAN,
|
||||
(int) $this->month,
|
||||
(int) $this->year
|
||||
);
|
||||
|
||||
$maxDays = min(7, $daysInMonth - $startDay + 1);
|
||||
|
||||
if ($maxDays <= 0) {
|
||||
throw new Exception('Selected week is outside the selected month.');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($rows, $startDay, $maxDays) {
|
||||
|
||||
// Get calendar leave dates
|
||||
$calendar = ProductionCalendar::where('plant_id', $this->plantId)
|
||||
->whereMonth('created_at', $this->month)
|
||||
->whereYear('created_at', $this->year)
|
||||
->first();
|
||||
|
||||
$leaveDates = $calendar->leave_dates ?? [];
|
||||
|
||||
if (is_string($leaveDates)) {
|
||||
$leaveDates = explode(',', $leaveDates);
|
||||
}
|
||||
|
||||
$leaveDates = collect($leaveDates)
|
||||
->map(fn ($date) => trim((string) $date))
|
||||
->filter()
|
||||
->map(fn ($date) => Carbon::parse($date)->format('Y-m-d'))
|
||||
->toArray();
|
||||
|
||||
foreach ($rows as $index => $row) {
|
||||
|
||||
$excelValues = array_values($row->toArray());
|
||||
|
||||
// First two columns
|
||||
$plantCode = trim((string) ($excelValues[0] ?? ''));
|
||||
$itemCode = trim((string) ($excelValues[1] ?? ''));
|
||||
|
||||
if ($plantCode === '' || $itemCode === '') {
|
||||
throw new Exception(
|
||||
'Plant Code or Item Code missing in Excel row ' . ($index + 2)
|
||||
);
|
||||
}
|
||||
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
|
||||
if (!$plant) {
|
||||
throw new Exception("Plant Code {$plantCode} not found.");
|
||||
}
|
||||
|
||||
$item = Item::where('code', $itemCode)
|
||||
->where('plant_id', $plant->id)
|
||||
->first();
|
||||
|
||||
if (!$item) {
|
||||
throw new Exception(
|
||||
"Item Code {$itemCode} not found for Plant Code {$plantCode}."
|
||||
);
|
||||
}
|
||||
|
||||
$plan = ProductionPlan::where('plant_id', $plant->id)
|
||||
->where('item_id', $item->id)
|
||||
->whereMonth('created_at', $this->month)
|
||||
->whereYear('created_at', $this->year)
|
||||
->first();
|
||||
|
||||
if (!$plan) {
|
||||
throw new Exception(
|
||||
"Production plan not found for Item Code: {$itemCode}"
|
||||
);
|
||||
}
|
||||
|
||||
// Remaining columns contain daily quantities
|
||||
$dayValues = array_slice($excelValues, 2);
|
||||
|
||||
$updateData = [];
|
||||
|
||||
for ($i = 0; $i < $maxDays; $i++) {
|
||||
|
||||
$databaseDay = $startDay + $i;
|
||||
|
||||
$date = Carbon::create(
|
||||
$this->year,
|
||||
$this->month,
|
||||
$databaseDay
|
||||
)->format('Y-m-d');
|
||||
|
||||
// Leave date: always zero
|
||||
if (in_array($date, $leaveDates)) {
|
||||
$updateData["day_{$databaseDay}"] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
$quantity = $dayValues[$i] ?? null;
|
||||
|
||||
// Blank quantity: do not update
|
||||
if ($quantity === null || trim((string) $quantity) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_numeric($quantity) || $quantity < 0) {
|
||||
throw new Exception(
|
||||
"Invalid quantity for Plant {$plantCode}, Item {$itemCode}, Day {$databaseDay}."
|
||||
);
|
||||
}
|
||||
|
||||
$updateData["day_{$databaseDay}"] = $quantity;
|
||||
}
|
||||
|
||||
if (!empty($updateData)) {
|
||||
$plan->update($updateData);
|
||||
|
||||
// Refresh the latest database values
|
||||
$plan->refresh();
|
||||
|
||||
// Calculate total of day_1 to day_31
|
||||
$totalPlanQuantity = 0;
|
||||
|
||||
for ($day = 1; $day <= 31; $day++) {
|
||||
|
||||
$totalPlanQuantity += (float) (
|
||||
$plan->{"day_{$day}"} ?? 0
|
||||
);
|
||||
}
|
||||
|
||||
// Update monthly plan quantity
|
||||
$plan->update([
|
||||
'plan_quantity' => $totalPlanQuantity,
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user