Files
GSB-Construction/Modules/ProjectManagement/app/Imports/TaskImport.php

67 lines
2.0 KiB
PHP

<?php
namespace Modules\ProjectManagement\Imports;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Modules\ProjectManagement\Models\Project;
use Modules\ProjectManagement\Models\Task;
class TaskImport implements ToArray, WithHeadingRow
{
private Project $project;
private int $imported = 0;
private int $skipped = 0;
public function __construct(Project $project)
{
$this->project = $project;
}
public function array(array $rows): void
{
foreach ($rows as $row) {
$name = trim($row['name'] ?? '');
if (empty($name)) {
$this->skipped++;
continue;
}
$data = [
'project_id' => $this->project->id,
'name' => $name,
'description' => $row['description'] ?? null,
'status' => 'pending',
'labor_cost' => is_numeric($row['labor_cost'] ?? null) ? $row['labor_cost'] : 0,
'estimated_hours' => is_numeric($row['estimated_hours'] ?? null) ? $row['estimated_hours'] : 0,
'sort_order' => is_numeric($row['sort_order'] ?? null) ? (int) $row['sort_order'] : 0,
];
// Parse dates safely
$startDate = $row['start_date_yyyy_mm_dd'] ?? $row['start_date'] ?? null;
$endDate = $row['end_date_yyyy_mm_dd'] ?? $row['end_date'] ?? null;
if ($startDate) {
try { $data['start_date'] = Carbon::parse($startDate)->format('Y-m-d'); } catch (\Exception) {}
}
if ($endDate) {
try { $data['end_date'] = Carbon::parse($endDate)->format('Y-m-d'); } catch (\Exception) {}
}
Task::create($data);
$this->imported++;
}
}
public function getImportedCount(): int
{
return $this->imported;
}
public function getSkippedCount(): int
{
return $this->skipped;
}
}