From b80d506243c2d54ff02a2b587bc057678b0f7ed9 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Tue, 28 Nov 2023 18:36:31 +0100 Subject: [PATCH] tracciamento modifiche anche per prodotti prenotati. ref #231 --- code/app/BookedProduct.php | 22 ++++++++++----- code/app/Booking.php | 2 +- code/app/Helpers/Components.php | 19 ++++++------- code/app/Observers/BookedProductObserver.php | 15 +++++++++++ code/app/Providers/EventServiceProvider.php | 3 +++ ...23_11_20_212439_tracking_updating_user.php | 1 + code/tests/Services/BookingsServiceTest.php | 27 +++++++++++++------ 7 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 code/app/Observers/BookedProductObserver.php diff --git a/code/app/BookedProduct.php b/code/app/BookedProduct.php index 798be687..220e6fcc 100644 --- a/code/app/BookedProduct.php +++ b/code/app/BookedProduct.php @@ -9,8 +9,7 @@ use Illuminate\Support\Str; use GeneaLabs\LaravelModelCaching\Traits\Cachable; -use Log; - +use App\Models\Concerns\TracksUpdater; use App\Models\Concerns\ModifiedTrait; use App\Models\Concerns\LeafReducibleTrait; use App\Parameters\Constraints\Constraint; @@ -19,21 +18,21 @@ class BookedProduct extends Model { - use HasFactory, GASModel, SluggableID, ModifiedTrait, LeafReducibleTrait, Cachable; + use HasFactory, GASModel, SluggableID, TracksUpdater, ModifiedTrait, LeafReducibleTrait, Cachable; public $incrementing = false; protected $keyType = 'string'; + protected $touches = ['booking']; protected $dispatchesEvents = [ 'creating' => SluggableCreating::class, ]; - /* - public function product(): BelongsTo + protected static function boot() { - return $this->belongsTo('App\Product')->withTrashed(); + parent::boot(); + static::initTrackingEvents(); } - */ public function booking(): BelongsTo { @@ -50,6 +49,15 @@ public function getStatusAttribute() return $this->booking->status; } + /* + Non viene espressa una relazione con il prodotto di riferimento, ma + questo viene recuperato direttamente dalla gerarchia cui l'elemento + appartiene. Questo, sia per motivi di ottimizzazione sia per attingere + al modello Product che si trova dentro l'ordine e manipolato per + veicolare con sé il suo prezzo nel contesto dell'ordine stesso (che non + necessariamente è uguale a quello di un Product recuperato ex-novo dal + database) + */ public function getProductAttribute() { return $this->booking->order->products->firstWhere('id', $this->product_id); diff --git a/code/app/Booking.php b/code/app/Booking.php index b74b6582..5e373d05 100644 --- a/code/app/Booking.php +++ b/code/app/Booking.php @@ -510,7 +510,7 @@ public function saveFinalPrices() foreach($this->products as $p) { $p->setRelation('booking', $this); $p->final_price = $p->getValue('delivered'); - $p->save(); + $p->saveQuietly(); } $this->status = $keep_status; diff --git a/code/app/Helpers/Components.php b/code/app/Helpers/Components.php index 12f5ffa9..0bd5d3a4 100644 --- a/code/app/Helpers/Components.php +++ b/code/app/Helpers/Components.php @@ -180,9 +180,13 @@ function formatUpdater($buttons, $params) $obj = $params['obj']; if ($obj && hasTrait($obj, \App\Models\Concerns\TracksUpdater::class)) { - $buttons[] = [ - 'element' => 'larastrap::updater', - ]; + $exists = array_filter($buttons, fn($b) => isset($b['element']) && $b['element'] == 'larastrap::updater'); + + if (count($exists) == 0) { + $buttons[] = [ + 'element' => 'larastrap::updater', + ]; + } } return $buttons; @@ -190,14 +194,7 @@ function formatUpdater($buttons, $params) function formatInnerLastUpdater($component, $params) { - if (isset($params['inner_form_managed'])) { - unset($params['inner_form_managed']); - } - else { - $params['inner_form_managed'] = 'ongoing'; - $params['buttons'] = formatUpdater($params['buttons'], $params); - } - + $params['buttons'] = formatUpdater($params['buttons'], $params); return $params; } diff --git a/code/app/Observers/BookedProductObserver.php b/code/app/Observers/BookedProductObserver.php new file mode 100644 index 00000000..5e5f08a0 --- /dev/null +++ b/code/app/Observers/BookedProductObserver.php @@ -0,0 +1,15 @@ +booking; + $booking->updated_by = $booked->updated_by; + $booking->save(); + } +} diff --git a/code/app/Providers/EventServiceProvider.php b/code/app/Providers/EventServiceProvider.php index 592d2d46..3fed1d2f 100644 --- a/code/app/Providers/EventServiceProvider.php +++ b/code/app/Providers/EventServiceProvider.php @@ -9,6 +9,7 @@ use App\Observers\UserObserver; use App\Observers\SupplierObserver; use App\Observers\OrderObserver; +use App\Observers\BookedProductObserver; use App\Observers\InvoiceObserver; use App\Observers\ModifierObserver; use App\Observers\ContactObserver; @@ -20,6 +21,7 @@ use App\User; use App\Supplier; use App\Order; +use App\BookedProduct; use App\Booking; use App\Invoice; use App\Modifier; @@ -62,6 +64,7 @@ public function boot() User::observe(UserObserver::class); Supplier::observe(SupplierObserver::class); Order::observe(OrderObserver::class); + BookedProduct::observe(BookedProductObserver::class); Invoice::observe(InvoiceObserver::class); Modifier::observe(ModifierObserver::class); Contact::observe(ContactObserver::class); diff --git a/code/database/migrations/2023_11_20_212439_tracking_updating_user.php b/code/database/migrations/2023_11_20_212439_tracking_updating_user.php index 71bb5d20..696e62ab 100644 --- a/code/database/migrations/2023_11_20_212439_tracking_updating_user.php +++ b/code/database/migrations/2023_11_20_212439_tracking_updating_user.php @@ -14,6 +14,7 @@ private function involvedTables() 'products', 'orders', 'bookings', + 'booked_products', 'modifiers', 'deliveries', 'movements', diff --git a/code/tests/Services/BookingsServiceTest.php b/code/tests/Services/BookingsServiceTest.php index 7fb5ce50..fed37c40 100644 --- a/code/tests/Services/BookingsServiceTest.php +++ b/code/tests/Services/BookingsServiceTest.php @@ -8,6 +8,7 @@ use App\Exceptions\AuthException; use App\Exceptions\IllegalArgumentException; +use App\Booking; use App\Movement; class BookingsServiceTest extends TestCase @@ -37,6 +38,16 @@ public function testReadBooking() $this->assertEquals($booking->products()->count(), $booked_count); $this->assertEquals($booking->getValue('booked', true), $total); + $this->nextRound(); + + $booking = Booking::find($booking->id); + $this->assertEquals($booking->updater->id, $this->userWithBasePerms->id); + foreach($booking->products as $prod) { + $this->assertEquals($prod->updater->id, $this->userWithBasePerms->id); + } + + $this->nextRound(); + $this->actingAs($this->userWithShippingPerms); list($data, $booked_count, $total) = $this->randomQuantities($this->sample_order->products); $data['notes_' . $this->sample_order->id] = ''; @@ -64,7 +75,7 @@ public function testShipping() $data['action'] = 'booked'; $this->updateAndFetch($data, $this->sample_order, $this->userWithBasePerms, false); - $booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertNotNull($booking); /* @@ -134,7 +145,7 @@ public function testUnaggregatedFriend() $this->nextRound(); - $booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $products = $booking->products_with_friends; $this->assertEquals(2, $products->count()); @@ -169,10 +180,10 @@ public function testShippingWithFriend() $merged_data['action'] = 'shipped'; $this->updateAndFetch($merged_data, $this->sample_order, $this->userWithBasePerms, true); - $booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertNotNull($booking); - $friend_booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $friend->id)->first(); + $friend_booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $friend->id)->first(); $this->assertNotNull($friend_booking); $booking = $booking->fresh(); @@ -271,12 +282,12 @@ public function testMultipleRead() $this->nextRound(); - $booking = \App\Booking::where('user_id', $this->userWithBasePerms->id)->where('order_id', $this->sample_order->id)->first(); + $booking = Booking::where('user_id', $this->userWithBasePerms->id)->where('order_id', $this->sample_order->id)->first(); $this->assertEquals($booking->status, 'shipped'); $this->assertNotNull($booking->payment_id); $this->assertEquals($booking->payment->amount, $total); - $booking2 = \App\Booking::where('user_id', $this->userWithBasePerms->id)->where('order_id', $order2->id)->first(); + $booking2 = Booking::where('user_id', $this->userWithBasePerms->id)->where('order_id', $order2->id)->first(); $this->assertEquals($booking2->status, 'shipped'); $this->assertNotNull($booking2->payment_id); $this->assertEquals($booking2->payment->amount, $total2); @@ -327,7 +338,7 @@ public function testKeepBookedQuantities() $data['action'] = 'booked'; $this->updateAndFetch($data, $this->sample_order, $this->userWithBasePerms, false); - $booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertEquals($booking->status, 'pending'); $this->assertEquals($booking->products()->count(), $booked_count); @@ -345,7 +356,7 @@ public function testKeepBookedQuantities() $shipped_data['action'] = 'shipped'; $this->updateAndFetch($shipped_data, $this->sample_order, $this->userWithBasePerms, true); - $booking = \App\Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->sample_order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertEquals($booking->status, 'saved'); $this->assertEquals($booking->products()->count(), $booked_count);