Skip to content

Commit

Permalink
fix modificatori per ordini. closes #245
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Dec 13, 2023
1 parent 60f5278 commit f98146a
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
7 changes: 3 additions & 4 deletions code/app/Http/Controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/app/Models/Concerns/ReducibleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion code/app/Singletons/ModifierEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
108 changes: 104 additions & 4 deletions code/tests/Services/DynamicBookingsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Collection;

use App\Exceptions\AuthException;
use App\Booking;

class DynamicBookingsServiceTest extends TestCase
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}
}
20 changes: 19 additions & 1 deletion code/tests/Services/ModifiersServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f98146a

Please sign in to comment.