Skip to content

Commit

Permalink
modificatore prodotto su valore complessivo dell'ordine. closes #295
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 23, 2024
1 parent f48f467 commit bc60232
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 66 deletions.
56 changes: 31 additions & 25 deletions code/app/Models/Concerns/ReducibleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,48 +225,54 @@ public function minimumRedux($modifiers)
break;
}

$priority = ['product', 'booking', 'order', 'aggregate'];
/*
Se il modificatore riguarda solo il prodotto nel contesto della
prenotazione o la prenotazione stessa, riduco solo la singola
prenotazione.
Se si applica all'ordine o ad prodotto complessivo, riduco solo
l'ordine.
In extremis, riduco l'intero aggregato
*/
$priority = [
['product'],
['booking'],
['order', 'global_product'],
['aggregate']
];

$target_priority = -1;
$aggregate_data = null;
$faster = true;

foreach($modifiers as $mod) {
$p = array_search($mod->applies_target, $priority);
if ($p > $target_priority) {
$target_priority = $p;
$target_level = $mod->getCheckTargetLevel();
\Log::debug('target_level = ' . $target_level);

foreach($priority as $priority_index => $items) {
if (in_array($target_level, $items) && $priority_index > $target_priority) {
$target_priority = $priority_index;
break;
}
}

if ($mod->value != 'percentage' || $mod->distribution_type != 'price') {
$faster = false;
}
}

if (($faster && $target_priority <= 1) && ($booking && $order)) {
$aggregate_data = $aggregate->reduxData([
'orders' => [$order],
'bookings' => [$booking]
]);
}
else {
$target_priority = 2;
}
$redux_filters = [];

if (is_null($aggregate_data)) {
if ($target_priority == 2 && $order) {
$aggregate_data = $aggregate->reduxData([
'orders' => [$order]
]);
}
else {
$target_priority = 3;
}
\Log::debug('target_priority = ' . $target_priority);

if ($target_priority == 3) {
$aggregate_data = $aggregate->reduxData();
if ($target_priority <= 2 && $order) {
if ($target_priority <= 1 && $faster && $booking) {
$redux_filters['bookings'] = [$booking];
}

$redux_filters['orders'] = [$order];
}

return $aggregate_data;
return $aggregate_data = $aggregate->reduxData($redux_filters);
}

/*
Expand Down
31 changes: 31 additions & 0 deletions code/app/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,37 @@ public function getROShowURL()
return route('modifiers.show', $this->id);
}

/*
Questa funzione permette di capire a che livello della gerarchia si
applica il modificatore.
"order" e "booking" si riferiscono, rispettivamente, all'ordine nel suo
complesso o alla specifica prenotazione.
"product" si riferisce al prodotto all'interno della prenotazione.
"global_product" si riferisce al prodotto complessivo nell'ordine
*/
public function getCheckTargetLevel()
{
if ($this->target_type == Product::class) {
if ($this->applies_type == 'order_price') {
return 'order';
}
else {
switch($this->applies_target) {
case 'order':
return 'global_product';
break;

default:
return 'product';
break;
}
}
}
else {
return $this->applies_target;
}
}

public function getActiveAttribute()
{
$data = $this->definitions;
Expand Down
72 changes: 32 additions & 40 deletions code/app/Singletons/ModifierEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ private function handlingAttributes($booking, $modifier, $attribute)
non ancora consegnato. Questo si applica in particolare in fase di
consegna
*/
if ($modifier->applies_target == 'order' || $booking->order->status == 'closed') {
if ($modifier->applies_target == 'order' || $booking->order->status == 'closed' || $modifier->applies_type == 'order_price') {
switch($modifier->$attribute) {
case 'quantity':
$attribute = 'relative_quantity';
break;
case 'none':
case 'price':
case 'order_price':
$attribute = 'relative_price';
break;
case 'weight':
Expand Down Expand Up @@ -237,62 +238,53 @@ public function apply($modifier, $booking, $aggregate_data)
return null;
}

$order_id = $booking->order_id;

$aggregate_data = $this->normalizeAggregateData($aggregate_data, $booking);
if (is_null($aggregate_data)) {
Log::debug('Applicazione modificatore: mancano dati ordine ' . $booking->order_id);
Log::debug('Applicazione modificatore: mancano dati ordine ' . $order_id);
return null;
}

$product_target_id = 0;

if ($modifier->target_type == Product::class) {
$product_target_id = $modifier->target_id;

switch($modifier->applies_target) {
case 'order':
$check_target = $aggregate_data->orders[$booking->order_id]->products[$product_target_id] ?? null;
break;

default:
$check_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id]->products[$product_target_id] ?? null;
break;
}
}
else {
switch($modifier->applies_target) {
case 'order':
$check_target = $aggregate_data->orders[$booking->order_id] ?? null;
break;
/*
$check_target è l'elemento su cui valutare l'applicabilità del
modificatore
*/
switch($modifier->getCheckTargetLevel()) {
case 'order':
$check_target = $aggregate_data->orders[$order_id] ?? null;
break;

case 'booking':
$check_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id] ?? null;
break;
case 'booking':
$check_target = $aggregate_data->orders[$order_id]->bookings[$booking->id] ?? null;
break;

case 'product':
$check_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id]->products[$modifier->target->id] ?? null;
break;
case 'product':
$check_target = $aggregate_data->orders[$order_id]->bookings[$booking->id]->products[$modifier->target->id] ?? null;
break;

default:
Log::error('applies_target non riconosciuto per modificatore: ' . $modifier->applies_target);
return null;
}
case 'global_product':
$check_target = $aggregate_data->orders[$order_id]->products[$modifier->target->id] ?? null;
break;
}

/*
$mod_target è l'elemento su cui si applica il modificatore
*/
switch($modifier->applies_target) {
case 'order':
$mod_target = $aggregate_data->orders[$booking->order_id] ?? null;
$mod_target = $aggregate_data->orders[$order_id] ?? null;
$obj_mod_target = $booking;
break;

case 'booking':
$mod_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id] ?? null;
$mod_target = $aggregate_data->orders[$order_id]->bookings[$booking->id] ?? null;
$obj_mod_target = $booking;
break;

case 'product':
$product_target_id = $modifier->target->id;
$mod_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id]->products[$product_target_id] ?? null;
$obj_mod_target = $booking->products()->where('product_id', $product_target_id)->first();
$mod_target = $aggregate_data->orders[$order_id]->bookings[$booking->id]->products[$modifier->target->id] ?? null;
$obj_mod_target = $booking->products()->where('product_id', $modifier->target->id)->first();
break;

default:
Expand Down Expand Up @@ -322,11 +314,11 @@ public function apply($modifier, $booking, $aggregate_data)
list($distribution_attribute, $useless) = $this->handlingAttributes($booking, $modifier, 'distribution_type');

if ($modifier->target_type == Product::class) {
$booking_mod_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id]->products[$product_target_id] ?? null;
$reference = $mod_target->products[$product_target_id]->$distribution_attribute;
$booking_mod_target = $aggregate_data->orders[$order_id]->bookings[$booking->id]->products[$modifier->target->id] ?? null;
$reference = $mod_target->products[$modifier->target->id]->$distribution_attribute;
}
else {
$booking_mod_target = $aggregate_data->orders[$booking->order_id]->bookings[$booking->id] ?? null;
$booking_mod_target = $aggregate_data->orders[$order_id]->bookings[$booking->id] ?? null;
$reference = $mod_target->$distribution_attribute;
}

Expand Down
2 changes: 2 additions & 0 deletions code/app/View/Texts/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ private static function valueLabels()
'none' => '',
'quantity' => _i('la quantità'),
'price' => _i('il valore'),
'order_price' => _i("il valore dell'ordine"),
'weight' => _i('il peso'),
];
}
Expand Down Expand Up @@ -61,6 +62,7 @@ private static function unitLabels($target)
'none' => 'X',
'quantity' => $quantity_label,
'price' => $currency,
'order_price' => $currency,
'weight' => _i('Chili'),
];
}
Expand Down
1 change: 1 addition & 0 deletions code/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"barryvdh/laravel-debugbar": "^3.8",
"barryvdh/laravel-dompdf": "^2.0.0",
"debril/feed-io": "^5.0",
"doctrine/dbal": "^3.9",
"dotworkers/laravel-gettext": "^7.6.2",
"eluceo/ical": "^2.0",
"genealabs/laravel-model-caching": "^0.13.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('modifiers', function (Blueprint $table) {
$table->string('applies_type')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('modifiers', function (Blueprint $table) {
$table->enum('applies_type', ['none', 'quantity', 'price', 'weight'])->change();
});
}
};
1 change: 1 addition & 0 deletions code/resources/views/modifier/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'none' => _i('Nessuna soglia'),
'quantity' => _i('Quantità'),
'price' => _i('Valore'),
'order_price' => _i("Valore dell'Ordine"),
'weight' => _i('Peso'),
];
Expand Down
Loading

0 comments on commit bc60232

Please sign in to comment.