ClassPackageUnitService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ClassPackageUnit;
  4. use App\Models\ClassPackageUnitProduct;
  5. use Illuminate\Database\Eloquent\Collection;
  6. class ClassPackageUnitService
  7. {
  8. public function getByUnit(int $unitId): Collection
  9. {
  10. return ClassPackageUnit::with('products.product')
  11. ->where('unit_id', $unitId)
  12. ->where('visible', true)
  13. ->orderBy('name')
  14. ->get();
  15. }
  16. public function getAllByUnit(int $unitId): Collection
  17. {
  18. return ClassPackageUnit::with('products.product')
  19. ->where('unit_id', $unitId)
  20. ->orderBy('name')
  21. ->get();
  22. }
  23. public function findByIdForUnit(int $id, int $unitId): ClassPackageUnit
  24. {
  25. return ClassPackageUnit::with('products.product')
  26. ->where('unit_id', $unitId)
  27. ->findOrFail($id);
  28. }
  29. public function create(array $data): ClassPackageUnit
  30. {
  31. $materials = $data['materials'] ?? [];
  32. unset($data['materials']);
  33. $data['contract_material_value'] = $this->calcMaterialValue($materials);
  34. $packageUnit = ClassPackageUnit::create($data);
  35. $this->syncProducts($packageUnit, $materials);
  36. return $packageUnit->load('products.product');
  37. }
  38. public function update(int $id, int $unitId, array $data): ClassPackageUnit
  39. {
  40. $packageUnit = $this->findByIdForUnit($id, $unitId);
  41. $materials = $data['materials'] ?? null;
  42. unset($data['materials']);
  43. if ($materials !== null) {
  44. $data['contract_material_value'] = $this->calcMaterialValue($materials);
  45. }
  46. $packageUnit->update($data);
  47. if ($materials !== null) {
  48. $this->syncProducts($packageUnit, $materials);
  49. }
  50. return $packageUnit->fresh('products.product');
  51. }
  52. public function toggleVisibility(int $id, int $unitId): ClassPackageUnit
  53. {
  54. $packageUnit = $this->findByIdForUnit($id, $unitId);
  55. $packageUnit->update(['visible' => !$packageUnit->visible]);
  56. return $packageUnit->fresh();
  57. }
  58. public function delete(int $id, int $unitId): bool
  59. {
  60. $packageUnit = $this->findByIdForUnit($id, $unitId);
  61. return $packageUnit->delete();
  62. }
  63. /**
  64. * Sync products on an existing ClassPackageUnit from the base package.
  65. */
  66. public function syncProductsFromBasePackage(ClassPackageUnit $packageUnit, \App\Models\ClassPackage $basePackage): void
  67. {
  68. $packageUnit->products()->delete();
  69. $basePackage->products->each(function ($product) use ($packageUnit) {
  70. ClassPackageUnitProduct::create([
  71. 'class_package_unit_id' => $packageUnit->id,
  72. 'product_id' => $product->id,
  73. 'quantity' => $product->pivot->quantity,
  74. 'price' => $product->pivot->price,
  75. ]);
  76. });
  77. }
  78. public function replicateFromBasePackage(int $unitId, \App\Models\ClassPackage $basePackage): ClassPackageUnit
  79. {
  80. $packageUnit = ClassPackageUnit::create([
  81. 'unit_id' => $unitId,
  82. 'class_package_id' => $basePackage->id,
  83. 'name' => $basePackage->name,
  84. 'quantity_classes' => $basePackage->quantity_classes,
  85. 'contract_value' => $basePackage->contract_value,
  86. 'contract_material_value' => $basePackage->contract_material_value,
  87. 'contract_register_value' => $basePackage->contract_register_value,
  88. 'contrat_discount_value' => $basePackage->contrat_discount_value,
  89. 'visible' => true,
  90. ]);
  91. $basePackage->products->each(function ($product) use ($packageUnit) {
  92. ClassPackageUnitProduct::create([
  93. 'class_package_unit_id' => $packageUnit->id,
  94. 'product_id' => $product->id,
  95. 'quantity' => $product->pivot->quantity,
  96. 'price' => $product->pivot->price,
  97. ]);
  98. });
  99. return $packageUnit;
  100. }
  101. private function syncProducts(ClassPackageUnit $packageUnit, array $materials): void
  102. {
  103. $packageUnit->products()->delete();
  104. foreach ($materials as $m) {
  105. ClassPackageUnitProduct::create([
  106. 'class_package_unit_id' => $packageUnit->id,
  107. 'product_id' => $m['product_id'],
  108. 'quantity' => $m['quantity'],
  109. 'price' => $m['price'],
  110. ]);
  111. }
  112. }
  113. private function calcMaterialValue(array $materials): float
  114. {
  115. return array_reduce($materials, fn($carry, $m) => $carry + ($m['quantity'] * $m['price']), 0.0);
  116. }
  117. }