|
@@ -2,8 +2,8 @@
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
+use App\Models\ClassPackage;
|
|
|
use App\Models\ClassPackageUnit;
|
|
use App\Models\ClassPackageUnit;
|
|
|
-use App\Models\ClassPackageUnitProduct;
|
|
|
|
|
use App\Models\IrrecusableProposalUnit;
|
|
use App\Models\IrrecusableProposalUnit;
|
|
|
use App\Models\IrrecusableProposalUnitProduct;
|
|
use App\Models\IrrecusableProposalUnitProduct;
|
|
|
use App\Models\PavaoProposalUnit;
|
|
use App\Models\PavaoProposalUnit;
|
|
@@ -17,7 +17,6 @@ public function __construct(
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
private const EAGER_LOAD = [
|
|
private const EAGER_LOAD = [
|
|
|
- 'products.product',
|
|
|
|
|
'pavaoProposalUnit.products.product',
|
|
'pavaoProposalUnit.products.product',
|
|
|
'irrecusableProposalUnit.products.product',
|
|
'irrecusableProposalUnit.products.product',
|
|
|
];
|
|
];
|
|
@@ -48,19 +47,19 @@ public function findByIdForUnit(int $id, int $unitId): ClassPackageUnit
|
|
|
|
|
|
|
|
public function create(array $data): ClassPackageUnit
|
|
public function create(array $data): ClassPackageUnit
|
|
|
{
|
|
{
|
|
|
- $materials = $data['materials'] ?? [];
|
|
|
|
|
- $pavao = $data['pavao'] ?? [];
|
|
|
|
|
- $irrecusavel = $data['irrecusavel'] ?? [];
|
|
|
|
|
- unset($data['materials'], $data['pavao'], $data['irrecusavel']);
|
|
|
|
|
-
|
|
|
|
|
- $data['contract_material_value'] = $this->calcMaterialValue($materials);
|
|
|
|
|
|
|
+ $pavao = array_key_exists('pavao', $data) ? $data['pavao'] : null;
|
|
|
|
|
+ $irrecusavel = array_key_exists('irrecusavel', $data) ? $data['irrecusavel'] : null;
|
|
|
|
|
+ unset($data['pavao'], $data['irrecusavel']);
|
|
|
|
|
|
|
|
$packageUnit = ClassPackageUnit::create($data);
|
|
$packageUnit = ClassPackageUnit::create($data);
|
|
|
|
|
|
|
|
- $this->syncProducts($packageUnit, $materials);
|
|
|
|
|
|
|
+ if ($pavao !== null) {
|
|
|
|
|
+ $this->pricingService->upsert(PavaoProposalUnit::class, PavaoProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $pavao);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $this->pricingService->upsert(PavaoProposalUnit::class, PavaoProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $pavao);
|
|
|
|
|
- $this->pricingService->upsert(IrrecusableProposalUnit::class, IrrecusableProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $irrecusavel, withCondition: true);
|
|
|
|
|
|
|
+ if ($irrecusavel !== null) {
|
|
|
|
|
+ $this->pricingService->upsert(IrrecusableProposalUnit::class, IrrecusableProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $irrecusavel, withCondition: true);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return $packageUnit->load(self::EAGER_LOAD);
|
|
return $packageUnit->load(self::EAGER_LOAD);
|
|
|
}
|
|
}
|
|
@@ -69,28 +68,29 @@ public function update(int $id, int $unitId, array $data): ClassPackageUnit
|
|
|
{
|
|
{
|
|
|
$packageUnit = $this->findByIdForUnit($id, $unitId);
|
|
$packageUnit = $this->findByIdForUnit($id, $unitId);
|
|
|
|
|
|
|
|
- $materials = $data['materials'] ?? null;
|
|
|
|
|
|
|
+ $pavaoProvided = array_key_exists('pavao', $data);
|
|
|
|
|
+ $irrecusavelProvided = array_key_exists('irrecusavel', $data);
|
|
|
$pavao = $data['pavao'] ?? null;
|
|
$pavao = $data['pavao'] ?? null;
|
|
|
$irrecusavel = $data['irrecusavel'] ?? null;
|
|
$irrecusavel = $data['irrecusavel'] ?? null;
|
|
|
- unset($data['materials'], $data['pavao'], $data['irrecusavel']);
|
|
|
|
|
-
|
|
|
|
|
- if ($materials !== null) {
|
|
|
|
|
- $data['contract_material_value'] = $this->calcMaterialValue($materials);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ unset($data['pavao'], $data['irrecusavel']);
|
|
|
|
|
|
|
|
$packageUnit->update($data);
|
|
$packageUnit->update($data);
|
|
|
|
|
|
|
|
- if ($materials !== null) {
|
|
|
|
|
- $this->syncProducts($packageUnit, $materials);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// Edits here only affect this unit's own copy — never propagate up to the base package.
|
|
// Edits here only affect this unit's own copy — never propagate up to the base package.
|
|
|
- if ($pavao !== null) {
|
|
|
|
|
- $this->pricingService->upsert(PavaoProposalUnit::class, PavaoProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $pavao);
|
|
|
|
|
|
|
+ if ($pavaoProvided) {
|
|
|
|
|
+ if ($pavao !== null) {
|
|
|
|
|
+ $this->pricingService->upsert(PavaoProposalUnit::class, PavaoProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $pavao);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->pricingService->deleteFor(PavaoProposalUnit::class, 'class_package_unit_id', $packageUnit->id);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if ($irrecusavel !== null) {
|
|
|
|
|
- $this->pricingService->upsert(IrrecusableProposalUnit::class, IrrecusableProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $irrecusavel, withCondition: true);
|
|
|
|
|
|
|
+ if ($irrecusavelProvided) {
|
|
|
|
|
+ if ($irrecusavel !== null) {
|
|
|
|
|
+ $this->pricingService->upsert(IrrecusableProposalUnit::class, IrrecusableProposalUnitProduct::class, 'class_package_unit_id', $packageUnit->id, $irrecusavel, withCondition: true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->pricingService->deleteFor(IrrecusableProposalUnit::class, 'class_package_unit_id', $packageUnit->id);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $packageUnit->fresh(self::EAGER_LOAD);
|
|
return $packageUnit->fresh(self::EAGER_LOAD);
|
|
@@ -112,34 +112,13 @@ public function delete(int $id, int $unitId): bool
|
|
|
return $packageUnit->delete();
|
|
return $packageUnit->delete();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * Sync products on an existing ClassPackageUnit from the base package.
|
|
|
|
|
- */
|
|
|
|
|
- public function syncProductsFromBasePackage(ClassPackageUnit $packageUnit, \App\Models\ClassPackage $basePackage): void
|
|
|
|
|
- {
|
|
|
|
|
- $packageUnit->products()->delete();
|
|
|
|
|
-
|
|
|
|
|
- $basePackage->products->each(function ($product) use ($packageUnit) {
|
|
|
|
|
- ClassPackageUnitProduct::create([
|
|
|
|
|
- 'class_package_unit_id' => $packageUnit->id,
|
|
|
|
|
- 'product_id' => $product->id,
|
|
|
|
|
- 'quantity' => $product->pivot->quantity,
|
|
|
|
|
- 'price' => $product->pivot->price,
|
|
|
|
|
- ]);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function replicateFromBasePackage(int $unitId, \App\Models\ClassPackage $basePackage): ClassPackageUnit
|
|
|
|
|
|
|
+ public function replicateFromBasePackage(int $unitId, ClassPackage $basePackage): ClassPackageUnit
|
|
|
{
|
|
{
|
|
|
$packageUnit = ClassPackageUnit::create([
|
|
$packageUnit = ClassPackageUnit::create([
|
|
|
'unit_id' => $unitId,
|
|
'unit_id' => $unitId,
|
|
|
'class_package_id' => $basePackage->id,
|
|
'class_package_id' => $basePackage->id,
|
|
|
'name' => $basePackage->name,
|
|
'name' => $basePackage->name,
|
|
|
'quantity_classes' => $basePackage->quantity_classes,
|
|
'quantity_classes' => $basePackage->quantity_classes,
|
|
|
- 'contract_value' => $basePackage->contract_value,
|
|
|
|
|
- 'contract_material_value' => $basePackage->contract_material_value,
|
|
|
|
|
- 'contract_register_value' => $basePackage->contract_register_value,
|
|
|
|
|
- 'contrat_discount_value' => $basePackage->contrat_discount_value,
|
|
|
|
|
'class_duration_minutes' => $basePackage->class_duration_minutes,
|
|
'class_duration_minutes' => $basePackage->class_duration_minutes,
|
|
|
'weekday' => $basePackage->weekday,
|
|
'weekday' => $basePackage->weekday,
|
|
|
'start_time' => $basePackage->start_time,
|
|
'start_time' => $basePackage->start_time,
|
|
@@ -148,15 +127,6 @@ public function replicateFromBasePackage(int $unitId, \App\Models\ClassPackage $
|
|
|
'visible' => true,
|
|
'visible' => true,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- $basePackage->products->each(function ($product) use ($packageUnit) {
|
|
|
|
|
- ClassPackageUnitProduct::create([
|
|
|
|
|
- 'class_package_unit_id' => $packageUnit->id,
|
|
|
|
|
- 'product_id' => $product->id,
|
|
|
|
|
- 'quantity' => $product->pivot->quantity,
|
|
|
|
|
- 'price' => $product->pivot->price,
|
|
|
|
|
- ]);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
$this->pricingService->cloneToUnit(
|
|
$this->pricingService->cloneToUnit(
|
|
|
$basePackage->pavaoProposal,
|
|
$basePackage->pavaoProposal,
|
|
|
PavaoProposalUnit::class,
|
|
PavaoProposalUnit::class,
|
|
@@ -178,23 +148,4 @@ public function replicateFromBasePackage(int $unitId, \App\Models\ClassPackage $
|
|
|
|
|
|
|
|
return $packageUnit;
|
|
return $packageUnit;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private function syncProducts(ClassPackageUnit $packageUnit, array $materials): void
|
|
|
|
|
- {
|
|
|
|
|
- $packageUnit->products()->delete();
|
|
|
|
|
-
|
|
|
|
|
- foreach ($materials as $m) {
|
|
|
|
|
- ClassPackageUnitProduct::create([
|
|
|
|
|
- 'class_package_unit_id' => $packageUnit->id,
|
|
|
|
|
- 'product_id' => $m['product_id'],
|
|
|
|
|
- 'quantity' => $m['quantity'],
|
|
|
|
|
- 'price' => $m['price'],
|
|
|
|
|
- ]);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private function calcMaterialValue(array $materials): float
|
|
|
|
|
- {
|
|
|
|
|
- return array_reduce($materials, fn($carry, $m) => $carry + ($m['quantity'] * $m['price']), 0.0);
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|