diff --git a/code/app/Booking.php b/code/app/Booking.php index 6c605033..26181b97 100644 --- a/code/app/Booking.php +++ b/code/app/Booking.php @@ -374,7 +374,19 @@ public function getDeliveredTaxedAttribute() public function getProductsWithFriendsAttribute() { return $this->innerCache('friends_products', function($obj) { - $products = $this->products; + /* + Qui devo fare una copia di $this->products anziché usarlo + direttamente, altrimenti finisco con l'alterare l'elenco stesso + dei prodotti relazionati alla prenotazione aggiungendoci anche + quelli degli amici con effetti poco graditi (e.g. in fase di + gestione della consegna, altero l'entità del prodotto dell'amico + anziché quello della prenotazione primaria) + */ + $products = new Collection(); + foreach($this->products as $p) { + $products->push($p); + } + $friends = $this->friends_bookings; foreach($friends as $sub) { diff --git a/code/tests/Services/FastBookingsServiceTest.php b/code/tests/Services/FastBookingsServiceTest.php index b1a8520c..5b6b235d 100644 --- a/code/tests/Services/FastBookingsServiceTest.php +++ b/code/tests/Services/FastBookingsServiceTest.php @@ -15,7 +15,7 @@ public function setUp(): void parent::setUp(); $this->sample_order = $this->initOrder(null); - $this->userWithBasePerms = $this->createRoleAndUser($this->gas, 'supplier.book'); + $this->userWithBasePerms = $this->createRoleAndUser($this->gas, 'supplier.book,users.subusers'); } /* @@ -88,6 +88,56 @@ public function testFastShippingFiltered() } } + /* + Consegne veloci con amici + */ + public function testFastShippingWithFriend() + { + $friend = $this->createFriend($this->userWithBasePerms); + $this->actingAs($friend); + + \Log::debug('qui: ' . $friend->id); + + list($data_friend, $booked_count_friend, $total_friend) = $this->randomQuantities($this->sample_order->products); + $data_friend['action'] = 'booked'; + app()->make('BookingsService')->bookingUpdate($data_friend, $this->sample_order->aggregate, $friend, false); + + $this->nextRound(); + + $this->actingAs($this->userWithShippingPerms); + $order = app()->make('OrdersService')->show($this->sample_order->id); + app()->make('FastBookingsService')->fastShipping($this->userWithShippingPerms, $order->aggregate, null); + + $this->nextRound(); + $order = app()->make('OrdersService')->show($this->sample_order->id); + $this->assertEquals(2, $order->bookings->count()); + $this->assertEquals(1, count($order->topLevelBookings())); + + foreach($order->bookings as $booking) { + $this->assertEquals($booking->status, 'shipped'); + + foreach($booking->products as $product) { + if ($booking->user->isFriend()) { + $this->assertEquals(0, $product->delivered); + } + else { + $this->assertEquals(0, $product->quantity); + $this->assertEquals($data_friend[$product->product_id], $product->delivered); + } + } + + if ($booking->user->isFriend()) { + $this->assertNull($booking->payment); + } + else { + $this->assertEquals($booking->payment->amount, $booking->getValue('effective', true)); + } + } + + $summary = $order->aggregate->reduxData(); + $this->assertTrue($summary->price_delivered > 0); + } + /* Consegne veloci su prenotazioni salvate */