From 90b2001fe7966a342c1bc9004f4658d4303dabf5 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Sat, 11 Nov 2023 23:49:55 +0100 Subject: [PATCH] fix gestione modificatori --- code/app/Booking.php | 38 ++++++++++++++++---- code/app/Order.php | 14 ++++---- code/app/Providers/SingletonsProvider.php | 26 +++++++++++--- code/app/Singletons/ModifierEngine.php | 1 + code/tests/Services/ModifiersServiceTest.php | 4 ++- 5 files changed, 65 insertions(+), 18 deletions(-) diff --git a/code/app/Booking.php b/code/app/Booking.php index 3c3efb69..8056be26 100644 --- a/code/app/Booking.php +++ b/code/app/Booking.php @@ -191,21 +191,47 @@ public function getValue($type, $with_friends, $force_recalculate = false) if ($type == 'effective') { $value = 0; + $modified_values = null; - $modifiers = $this->involvedModifiers(); - if ($modifiers->isEmpty() == false) { - $aggregate_data = $obj->minimumRedux($modifiers); + /* + Se la prenotazione รจ stata consegnata, devo andare a + recuperare i modificatori che sono stati effettivamente + salvati sul DB a prescindere da quali sono quelli + "teorici" che potrei trovare (quelli restituiti da + involvedModifiers()). + Questo per recuperare anche gli eventuali modificatori + speciali delle consegne manuali + */ - $type = $obj->status == 'pending' ? 'booked' : 'delivered'; - $modified_values = $obj->applyModifiers($aggregate_data, false); + if ($obj->status != 'pending') { + $type = 'delivered'; + $modified_values = $obj->allModifiedValues(null, true); if ($with_friends) { foreach($obj->friends_bookings as $friend_booking) { - $friend_modified_values = $friend_booking->applyModifiers($aggregate_data, false); + $friend_modified_values = $friend_booking->allModifiedValues(null, true); $modified_values = $modified_values->merge($friend_modified_values); } } + } + else { + $type = 'booked'; + $modifiers = $obj->involvedModifiers(); + + if ($modifiers->isEmpty() == false) { + $aggregate_data = $obj->minimumRedux($modifiers); + $modified_values = $obj->calculateModifiers($aggregate_data, false); + + if ($with_friends) { + foreach($obj->friends_bookings as $friend_booking) { + $friend_modified_values = $friend_booking->calculateModifiers($aggregate_data, false); + $modified_values = $modified_values->merge($friend_modified_values); + } + } + } + } + if ($modified_values) { $value = ModifiedValue::sumAmounts($modified_values, $value); } } diff --git a/code/app/Order.php b/code/app/Order.php index f66312a8..d1ee27c2 100644 --- a/code/app/Order.php +++ b/code/app/Order.php @@ -340,10 +340,8 @@ public function showableContacts() if ($role) { return $role->usersByTarget($this->supplier); } - else { - Log::error('Role not found while displaying contacts for order: ' . $gas->booking_contacts); - return new Collection(); - } + + return new Collection(); } } @@ -633,15 +631,17 @@ public function involvedModifiers($include_shipping_places = false) public function applyModifiers($aggregate_data = null, $enforce_status = false) { $modifiers = $this->involvedModifiers(true); - if ($modifiers->isEmpty() == false) { - DB::beginTransaction(); + $has_shipped_bookings = $this->bookings->where('status', '!=', 'pending')->count() != 0; - $modifiers = new Collection(); + if ($modifiers->isEmpty() == false || $has_shipped_bookings) { + DB::beginTransaction(); if (is_null($aggregate_data)) { $aggregate_data = $this->minimumRedux($modifiers); } + $modifiers = new Collection(); + $old_status = $this->status; if ($enforce_status !== false) { $this->status = $enforce_status; diff --git a/code/app/Providers/SingletonsProvider.php b/code/app/Providers/SingletonsProvider.php index 5eff0b92..e6344332 100644 --- a/code/app/Providers/SingletonsProvider.php +++ b/code/app/Providers/SingletonsProvider.php @@ -3,21 +3,22 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Support\DeferrableProvider; -class SingletonsProvider extends ServiceProvider +class SingletonsProvider extends ServiceProvider implements DeferrableProvider { - public function boot() + public function boot(): void { // } - public function register() + private function singletons(): array { /* Questo si suppone essere l'elenco di tutte le classi in app/Singletons: da tenere aggiornato! */ - $classes = [ + return [ \App\Singletons\AggregationSwitch::class, \App\Singletons\GlobalScopeHub::class, \App\Singletons\LogHarvester::class, @@ -27,6 +28,11 @@ public function register() \App\Singletons\RemoteRepository::class, \App\Singletons\TempCache::class, ]; + } + + public function register(): void + { + $classes = $this->singletons(); foreach($classes as $class) { $this->app->singleton(class_basename($class), function ($app) use ($class) { @@ -34,4 +40,16 @@ public function register() }); } } + + public function provides(): array + { + $classes = $this->singletons(); + $ret = []; + + foreach($classes as $class) { + $ret[] = class_basename($class); + } + + return $ret; + } } diff --git a/code/app/Singletons/ModifierEngine.php b/code/app/Singletons/ModifierEngine.php index 50d86c7e..dd1a37ed 100644 --- a/code/app/Singletons/ModifierEngine.php +++ b/code/app/Singletons/ModifierEngine.php @@ -241,6 +241,7 @@ public function apply($modifier, $booking, $aggregate_data) break; default: + Log::error('applies_target non riconosciuto per modificatore: ' . $modifier->applies_target); return null; } diff --git a/code/tests/Services/ModifiersServiceTest.php b/code/tests/Services/ModifiersServiceTest.php index 32ab2a43..2c236cd6 100644 --- a/code/tests/Services/ModifiersServiceTest.php +++ b/code/tests/Services/ModifiersServiceTest.php @@ -60,7 +60,7 @@ private function enforceBookingsTotalQuantity($product_id, $total_quantity) } else { if ($missing_quantity > 0) { - $quantity = rand(0, $missing_quantity); + $quantity = rand(1, $missing_quantity); $data[$booked_product->product_id] = $quantity; $missing_quantity -= $quantity; } @@ -136,7 +136,9 @@ public function testThresholdUnitPrice() $this->assertNotNull($mod); foreach([21, 15, 3] as $threshold_index => $total_quantity) { + $this->nextRound(); $this->enforceBookingsTotalQuantity($product->id, $total_quantity); + $this->nextRound(); $order = $this->services['orders']->show($this->order->id); $modifiers = $order->applyModifiers();