Skip to content

Commit

Permalink
fix gestione modificatori
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 11, 2023
1 parent fe7e9c8 commit 90b2001
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
38 changes: 32 additions & 6 deletions code/app/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,47 @@ public function getValue($type, $with_friends, $force_recalculate = false)

if ($type == 'effective') {
$value = 0;
$modified_values = null;

$modifiers = $this->involvedModifiers();
if ($modifiers->isEmpty() == false) {
$aggregate_data = $obj->minimumRedux($modifiers);
/*
Se la prenotazione è stata consegnata, devo andare a
recuperare i modificatori che sono stati effettivamente
salvati sul DB a prescindere da quali sono quelli
"teorici" che potrei trovare (quelli restituiti da
involvedModifiers()).
Questo per recuperare anche gli eventuali modificatori
speciali delle consegne manuali
*/

$type = $obj->status == 'pending' ? 'booked' : 'delivered';
$modified_values = $obj->applyModifiers($aggregate_data, false);
if ($obj->status != 'pending') {
$type = 'delivered';
$modified_values = $obj->allModifiedValues(null, true);

if ($with_friends) {
foreach($obj->friends_bookings as $friend_booking) {
$friend_modified_values = $friend_booking->applyModifiers($aggregate_data, false);
$friend_modified_values = $friend_booking->allModifiedValues(null, true);
$modified_values = $modified_values->merge($friend_modified_values);
}
}
}
else {
$type = 'booked';
$modifiers = $obj->involvedModifiers();

if ($modifiers->isEmpty() == false) {
$aggregate_data = $obj->minimumRedux($modifiers);
$modified_values = $obj->calculateModifiers($aggregate_data, false);

if ($with_friends) {
foreach($obj->friends_bookings as $friend_booking) {
$friend_modified_values = $friend_booking->calculateModifiers($aggregate_data, false);
$modified_values = $modified_values->merge($friend_modified_values);
}
}
}
}

if ($modified_values) {
$value = ModifiedValue::sumAmounts($modified_values, $value);
}
}
Expand Down
14 changes: 7 additions & 7 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,8 @@ public function showableContacts()
if ($role) {
return $role->usersByTarget($this->supplier);
}
else {
Log::error('Role not found while displaying contacts for order: ' . $gas->booking_contacts);
return new Collection();
}

return new Collection();
}
}

Expand Down Expand Up @@ -633,15 +631,17 @@ public function involvedModifiers($include_shipping_places = false)
public function applyModifiers($aggregate_data = null, $enforce_status = false)
{
$modifiers = $this->involvedModifiers(true);
if ($modifiers->isEmpty() == false) {
DB::beginTransaction();
$has_shipped_bookings = $this->bookings->where('status', '!=', 'pending')->count() != 0;

$modifiers = new Collection();
if ($modifiers->isEmpty() == false || $has_shipped_bookings) {
DB::beginTransaction();

if (is_null($aggregate_data)) {
$aggregate_data = $this->minimumRedux($modifiers);
}

$modifiers = new Collection();

$old_status = $this->status;
if ($enforce_status !== false) {
$this->status = $enforce_status;
Expand Down
26 changes: 22 additions & 4 deletions code/app/Providers/SingletonsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;

class SingletonsProvider extends ServiceProvider
class SingletonsProvider extends ServiceProvider implements DeferrableProvider
{
public function boot()
public function boot(): void
{
//
}

public function register()
private function singletons(): array
{
/*
Questo si suppone essere l'elenco di tutte le classi in
app/Singletons: da tenere aggiornato!
*/
$classes = [
return [
\App\Singletons\AggregationSwitch::class,
\App\Singletons\GlobalScopeHub::class,
\App\Singletons\LogHarvester::class,
Expand All @@ -27,11 +28,28 @@ public function register()
\App\Singletons\RemoteRepository::class,
\App\Singletons\TempCache::class,
];
}

public function register(): void
{
$classes = $this->singletons();

foreach($classes as $class) {
$this->app->singleton(class_basename($class), function ($app) use ($class) {
return new $class();
});
}
}

public function provides(): array
{
$classes = $this->singletons();
$ret = [];

foreach($classes as $class) {
$ret[] = class_basename($class);
}

return $ret;
}
}
1 change: 1 addition & 0 deletions code/app/Singletons/ModifierEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public function apply($modifier, $booking, $aggregate_data)
break;

default:
Log::error('applies_target non riconosciuto per modificatore: ' . $modifier->applies_target);
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion code/tests/Services/ModifiersServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function enforceBookingsTotalQuantity($product_id, $total_quantity)
}
else {
if ($missing_quantity > 0) {
$quantity = rand(0, $missing_quantity);
$quantity = rand(1, $missing_quantity);
$data[$booked_product->product_id] = $quantity;
$missing_quantity -= $quantity;
}
Expand Down Expand Up @@ -136,7 +136,9 @@ public function testThresholdUnitPrice()
$this->assertNotNull($mod);

foreach([21, 15, 3] as $threshold_index => $total_quantity) {
$this->nextRound();
$this->enforceBookingsTotalQuantity($product->id, $total_quantity);
$this->nextRound();

$order = $this->services['orders']->show($this->order->id);
$modifiers = $order->applyModifiers();
Expand Down

0 comments on commit 90b2001

Please sign in to comment.