Skip to content

Commit

Permalink
tracciamento modifiche anche per prodotti prenotati. ref #231
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 28, 2023
1 parent 989715c commit b80d506
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
22 changes: 15 additions & 7 deletions code/app/BookedProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion code/app/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 8 additions & 11 deletions code/app/Helpers/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,21 @@ 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;
}

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;
}

Expand Down
15 changes: 15 additions & 0 deletions code/app/Observers/BookedProductObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Observers;

use App\BookedProduct;

class BookedProductObserver
{
public function saved(BookedProduct $booked)
{
$booking = $booked->booking;
$booking->updated_by = $booked->updated_by;
$booking->save();
}
}
3 changes: 3 additions & 0 deletions code/app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private function involvedTables()
'products',
'orders',
'bookings',
'booked_products',
'modifiers',
'deliveries',
'movements',
Expand Down
27 changes: 19 additions & 8 deletions code/tests/Services/BookingsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use App\Exceptions\AuthException;
use App\Exceptions\IllegalArgumentException;
use App\Booking;
use App\Movement;

class BookingsServiceTest extends TestCase
Expand Down Expand Up @@ -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] = '';
Expand Down Expand Up @@ -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);

/*
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b80d506

Please sign in to comment.