diff --git a/code/app/Http/Controllers/MailController.php b/code/app/Http/Controllers/MailController.php index f2874ae2..5d886199 100644 --- a/code/app/Http/Controllers/MailController.php +++ b/code/app/Http/Controllers/MailController.php @@ -3,10 +3,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Http\Controllers\Controller; - -use DB; -use Log; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Aws\Sns\Message; use Aws\Sns\MessageValidator; @@ -33,6 +31,7 @@ private function saveInstances($email, $message) $db->delete("DELETE FROM contacts WHERE type = 'email' and value = '$email'"); $message = _i('Rimosso indirizzo email ' . $email); $db->insert("INSERT INTO inner_logs (level, type, message, created_at, updated_at) VALUES ('error', 'mailsuppression', '$message', '$now', '$now')"); + Log::info($message); } } } diff --git a/code/app/Models/Concerns/ReducibleTrait.php b/code/app/Models/Concerns/ReducibleTrait.php index c4e80e64..c023103f 100644 --- a/code/app/Models/Concerns/ReducibleTrait.php +++ b/code/app/Models/Concerns/ReducibleTrait.php @@ -242,7 +242,7 @@ public function minimumRedux($modifiers) } } - if (($faster || $target_priority <= 1) && ($booking && $order)) { + if (($faster && $target_priority <= 1) && ($booking && $order)) { $aggregate_data = $aggregate->reduxData([ 'orders' => [$order], 'bookings' => [$booking] diff --git a/code/app/Singletons/ModifierEngine.php b/code/app/Singletons/ModifierEngine.php index 27d1ce5b..11eb2e4b 100644 --- a/code/app/Singletons/ModifierEngine.php +++ b/code/app/Singletons/ModifierEngine.php @@ -108,7 +108,7 @@ private function handlingAttributes($booking, $modifier) OrdersController::postFixModifiers(), previa conferma dell'utente, quando l'ordine è davvero in stato "consegnato" */ - if ($booking->status != 'pending') { + if (in_array($booking->status, ['saved', 'shipped'])) { switch($modifier->applies_type) { case 'none': case 'quantity': diff --git a/code/tests/Services/DynamicBookingsServiceTest.php b/code/tests/Services/DynamicBookingsServiceTest.php index aa1e05e2..f4afc253 100644 --- a/code/tests/Services/DynamicBookingsServiceTest.php +++ b/code/tests/Services/DynamicBookingsServiceTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Collection; use App\Exceptions\AuthException; +use App\Booking; class DynamicBookingsServiceTest extends TestCase { @@ -65,7 +66,7 @@ public function testSimple() $this->nextRound(); - $booking = \App\Booking::where('order_id', $this->order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertEquals($booking->getValue('effective', true), $total); $this->assertEquals($booking->products()->count(), $booked_count); } @@ -96,7 +97,7 @@ public function testFriend() $this->nextRound(); - $booking = \App\Booking::where('order_id', $this->order->id)->where('user_id', $this->userWithBasePerms->id)->first(); + $booking = Booking::where('order_id', $this->order->id)->where('user_id', $this->userWithBasePerms->id)->first(); $this->assertEquals($booking->getValue('effective', true), $total_master + $total_friend); $this->nextRound(); @@ -318,7 +319,7 @@ private function attachDiscount($product) /* Lettura dinamica delle prenotazioni, prodotto con modificatori */ - public function testModifiers() + public function testModifiersOnProduct() { $product = $this->order->products->random(); $this->attachDiscount($product); @@ -349,7 +350,7 @@ public function testModifiers() /* Lettura dinamica delle prenotazioni, prodotto con varianti e modificatori */ - public function testModifiersAndVariants() + public function testModifiersOnProductAndVariants() { $this->actingAs($this->userReferrer); @@ -391,4 +392,103 @@ public function testModifiersAndVariants() } } } + + /* + Lettura dinamica delle prenotazioni, modificatore su ordine + */ + public function testModifiersOnOrder() + { + $this->actingAs($this->userReferrer); + + /* + Creo modificatore su ordine + */ + $mod = null; + $modifiers = $this->order->applicableModificationTypes(); + foreach ($modifiers as $mod) { + if ($mod->id == 'sconto') { + $mod = $this->order->modifiers()->where('modifier_type_id', $mod->id)->first(); + app()->make('ModifiersService')->update($mod->id, [ + 'value' => 'percentage', + 'arithmetic' => 'sub', + 'scale' => 'major', + 'applies_type' => 'price', + 'applies_target' => 'order', + 'distribution_type' => 'price', + 'threshold' => [1000], + 'amount' => [10], + ]); + + break; + } + } + + $this->assertNotNull($mod); + $product = $this->order->products->random(); + + /* + Effettuo prenotazione senza attivare modificatori + */ + + $this->nextRound(); + $this->actingAs($this->userWithBasePerms); + + $data = [ + 'action' => 'booked', + $product->id => 2, + ]; + + $ret = app()->make('DynamicBookingsService')->dynamicModifiers($data, $this->order->aggregate, $this->userWithBasePerms); + + $this->assertEquals(count($ret->bookings), 1); + + foreach($ret->bookings as $b) { + $this->assertTrue($b->total < 1000); + $this->assertEquals(count($b->products), 1); + $this->assertEquals($b->total, $product->price * 2); + $this->assertEquals(count($b->modifiers), 0); + } + + /* + Creo una prenotazione che faccia attivare i modificatori + */ + + $this->nextRound(); + $this->actingAs($this->userReferrer); + + $data = ['action' => 'booked']; + $total = 0; + + foreach($this->order->products as $p) { + $data[$p->id] = 100; + $total += $p->price * 100; + } + + $this->assertTrue($total > 100); + $this->updateAndFetch($data, $this->order, $this->userReferrer, false); + $booking = Booking::where('order_id', $this->order->id)->where('user_id', $this->userReferrer->id)->first(); + $this->assertNotNull($booking); + + /* + Aggiorno prenotazione con modificatori attivi + */ + + $this->nextRound(); + $this->actingAs($this->userWithBasePerms); + + $data = [ + 'action' => 'booked', + $product->id => 2, + ]; + + $ret = app()->make('DynamicBookingsService')->dynamicModifiers($data, $this->order->aggregate, $this->userWithBasePerms); + + $this->assertEquals(count($ret->bookings), 1); + + foreach($ret->bookings as $b) { + $this->assertEquals(count($b->products), 1); + $this->assertEquals($b->total, $product->price * 2 - ($product->price * 0.10 * 2)); + $this->assertEquals(count($b->modifiers), 1); + } + } } diff --git a/code/tests/Services/ModifiersServiceTest.php b/code/tests/Services/ModifiersServiceTest.php index d6fdf940..4c3a3594 100644 --- a/code/tests/Services/ModifiersServiceTest.php +++ b/code/tests/Services/ModifiersServiceTest.php @@ -175,7 +175,7 @@ public function testThresholdUnitPricePortions() $mod = null; $thresholds = [10, 5, 0]; - $threshold_prices = [0.9, 0.92, 0.94]; + $threshold_prices = [1, 2, 3]; foreach ($modifiers as $mod) { if ($mod->id == 'sconto') { @@ -253,6 +253,10 @@ public function testThresholdUnitPricePortions() $this->nextRound(); + /* + Da qui agisco in consegna, dunque con le quantità a peso + */ + $this->actingAs($this->userWithShippingPerms); $data = [ @@ -280,7 +284,21 @@ public function testThresholdUnitPricePortions() $product->id => 4, ]; app()->make('BookingsService')->bookingUpdate($data, $order->aggregate, $booking->user, true); + + $this->nextRound(); + $booking = $booking->fresh(); + $found = false; + + foreach($booking->products as $prod) { + if ($prod->product_id == $product->id) { + $found = true; + $this->assertEquals(4, $prod->delivered); + } + } + + $this->assertTrue($found); + $mods = $booking->applyModifiers(null, false); $this->assertEquals(\App\ModifiedValue::count(), 1); $this->assertEquals($mods->count(), 1);