Files
GSB-Construction/Modules/ProjectManagement/app/Imports/TaskImport.php
Ajjj ccbd23d474
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled
feat: enhance module RBAC permissions, financial billing workflows, task evidence uploading, and role analytics
2026-08-13 18:46:26 +08:00

87 lines
2.9 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) {}
}
$milestoneName = $row['milestone'] ?? $row['milestone_name'] ?? null;
if ($milestoneName) {
$milestone = \Modules\ProjectManagement\Models\ProjectMilestone::where('project_id', $this->project->id)
->where('name', 'like', "%{$milestoneName}%")
->first();
if ($milestone) {
$data['milestone_id'] = $milestone->id;
}
}
if (empty($data['milestone_id'])) {
$sortIndex = $data['sort_order'] ?? 0;
$projectMilestones = \Modules\ProjectManagement\Models\ProjectMilestone::where('project_id', $this->project->id)
->orderBy('sort_order')
->get();
if ($projectMilestones->isNotEmpty()) {
$data['milestone_id'] = ($projectMilestones->get($sortIndex) ?? $projectMilestones->first())->id;
}
}
Task::create($data);
$this->imported++;
}
}
public function getImportedCount(): int
{
return $this->imported;
}
public function getSkippedCount(): int
{
return $this->skipped;
}
}