|
@@ -17,7 +17,7 @@ public function getAll(): Collection
|
|
|
public function getSelectList(): Collection
|
|
public function getSelectList(): Collection
|
|
|
{
|
|
{
|
|
|
return InhabitantClassification::orderBy('id')
|
|
return InhabitantClassification::orderBy('id')
|
|
|
- ->get(['id', 'description', 'acronym']);
|
|
|
|
|
|
|
+ ->get(['id', 'description', 'start', 'end', 'tbr_percentage']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function findById(int $id): ?InhabitantClassification
|
|
public function findById(int $id): ?InhabitantClassification
|
|
@@ -40,13 +40,20 @@ public function update(int $id, array $data): ?InhabitantClassification
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $oldDescription = $model->description;
|
|
|
|
|
- $oldAcronym = $model->acronym;
|
|
|
|
|
|
|
+ $oldStart = $model->start;
|
|
|
|
|
+ $oldEnd = $model->end;
|
|
|
|
|
+ $oldTbrPercentage = $model->tbr_percentage;
|
|
|
|
|
+ $oldDescription = $model->description;
|
|
|
|
|
|
|
|
$model->update($data);
|
|
$model->update($data);
|
|
|
$updated = $model->fresh();
|
|
$updated = $model->fresh();
|
|
|
|
|
|
|
|
- $this->propagateUpdateToUnits($updated, $oldDescription, $oldAcronym);
|
|
|
|
|
|
|
+ $this->propagateUpdateToUnits($updated, [
|
|
|
|
|
+ 'description' => $oldDescription,
|
|
|
|
|
+ 'start' => $oldStart,
|
|
|
|
|
+ 'end' => $oldEnd,
|
|
|
|
|
+ 'tbr_percentage' => $oldTbrPercentage,
|
|
|
|
|
+ ]);
|
|
|
|
|
|
|
|
return $updated;
|
|
return $updated;
|
|
|
}
|
|
}
|
|
@@ -66,42 +73,52 @@ public function delete(int $id): bool
|
|
|
|
|
|
|
|
private function propagateToAllUnits(InhabitantClassification $parent): void
|
|
private function propagateToAllUnits(InhabitantClassification $parent): void
|
|
|
{
|
|
{
|
|
|
- $existingUnitIds = UnitInhabitantClassification::where('acronym', $parent->acronym)
|
|
|
|
|
|
|
+ $existingUnitIds = UnitInhabitantClassification::where('start', $parent->start)
|
|
|
|
|
+ ->where('end', $parent->end)
|
|
|
->pluck('unit_id');
|
|
->pluck('unit_id');
|
|
|
|
|
|
|
|
Unit::whereNotIn('id', $existingUnitIds)
|
|
Unit::whereNotIn('id', $existingUnitIds)
|
|
|
->each(function (Unit $unit) use ($parent) {
|
|
->each(function (Unit $unit) use ($parent) {
|
|
|
UnitInhabitantClassification::create([
|
|
UnitInhabitantClassification::create([
|
|
|
- 'unit_id' => $unit->id,
|
|
|
|
|
- 'description' => $parent->description,
|
|
|
|
|
- 'acronym' => $parent->acronym,
|
|
|
|
|
|
|
+ 'unit_id' => $unit->id,
|
|
|
|
|
+ 'description' => $parent->description,
|
|
|
|
|
+ 'start' => $parent->start,
|
|
|
|
|
+ 'end' => $parent->end,
|
|
|
|
|
+ 'tbr_percentage' => $parent->tbr_percentage,
|
|
|
]);
|
|
]);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private function propagateUpdateToUnits(InhabitantClassification $parent, string $oldDescription, string $oldAcronym): void
|
|
|
|
|
|
|
+ private function propagateUpdateToUnits(InhabitantClassification $parent, array $old): void
|
|
|
{
|
|
{
|
|
|
- // propaga a atualizacao para as classificacoes de unidades que tenham a mesma descricao e sigla antigas
|
|
|
|
|
-
|
|
|
|
|
- UnitInhabitantClassification::where('acronym', $oldAcronym)
|
|
|
|
|
- ->where('description', $oldDescription)
|
|
|
|
|
|
|
+ // propaga a atualizacao para as classificacoes de unidades que tenham os mesmos valores antigos
|
|
|
|
|
+ UnitInhabitantClassification::where('start', $old['start'])
|
|
|
|
|
+ ->where('end', $old['end'])
|
|
|
|
|
+ ->where('tbr_percentage', $old['tbr_percentage'])
|
|
|
|
|
+ ->where('description', $old['description'])
|
|
|
->update([
|
|
->update([
|
|
|
- 'description' => $parent->description,
|
|
|
|
|
- 'acronym' => $parent->acronym,
|
|
|
|
|
|
|
+ 'description' => $parent->description,
|
|
|
|
|
+ 'start' => $parent->start,
|
|
|
|
|
+ 'end' => $parent->end,
|
|
|
|
|
+ 'tbr_percentage' => $parent->tbr_percentage,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $coveredUnitIds = UnitInhabitantClassification::whereIn('acronym', array_unique([$parent->acronym, $oldAcronym]))
|
|
|
|
|
|
|
+ // garante que todas as unidades tenham a classificacao, criando para as que ainda nao tem
|
|
|
|
|
+ $coveredUnitIds = UnitInhabitantClassification::where(function ($q) use ($parent, $old) {
|
|
|
|
|
+ $q->where(fn($q) => $q->where('start', $parent->start)->where('end', $parent->end))
|
|
|
|
|
+ ->orWhere(fn($q) => $q->where('start', $old['start'])->where('end', $old['end']));
|
|
|
|
|
+ })
|
|
|
->pluck('unit_id')
|
|
->pluck('unit_id')
|
|
|
->unique();
|
|
->unique();
|
|
|
|
|
|
|
|
- // garante que todas as unidades tenham a classificacao atualizada, criando para as que ainda nao tem
|
|
|
|
|
-
|
|
|
|
|
Unit::whereNotIn('id', $coveredUnitIds)
|
|
Unit::whereNotIn('id', $coveredUnitIds)
|
|
|
->each(function (Unit $unit) use ($parent) {
|
|
->each(function (Unit $unit) use ($parent) {
|
|
|
UnitInhabitantClassification::create([
|
|
UnitInhabitantClassification::create([
|
|
|
- 'unit_id' => $unit->id,
|
|
|
|
|
- 'description' => $parent->description,
|
|
|
|
|
- 'acronym' => $parent->acronym,
|
|
|
|
|
|
|
+ 'unit_id' => $unit->id,
|
|
|
|
|
+ 'description' => $parent->description,
|
|
|
|
|
+ 'start' => $parent->start,
|
|
|
|
|
+ 'end' => $parent->end,
|
|
|
|
|
+ 'tbr_percentage' => $parent->tbr_percentage,
|
|
|
]);
|
|
]);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|