Skip to content

Commit

Permalink
Merge branch 'master' into invoice_permission
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Dec 18, 2023
2 parents c3cafb9 + f32b819 commit d870ad6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 55 deletions.
34 changes: 13 additions & 21 deletions code/app/Http/Controllers/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MailController extends Controller
{
private function saveInstances($email, $message)
private function saveInstances($event, $email, $message)
{
$instances = get_instances();
$now = date('Y-m-d G:i:s');
Expand All @@ -26,10 +26,11 @@ private function saveInstances($email, $message)
$db_emails = $db->select("SELECT COUNT(*) as count FROM contacts WHERE type = 'email' and value = '$email'");
if ($db_emails[0]->count != 0) {
$db->insert("INSERT INTO inner_logs (level, type, message, created_at, updated_at) VALUES ('error', 'mail', '$message', '$now', '$now')");
$db_failures = $db->select("SELECT COUNT(*) as count FROM inner_logs WHERE type = 'email' and message like '%$email%'");
if ($db_failures[0]->count >= 3) {
$db->delete("DELETE FROM contacts WHERE type = 'email' and value = '$email'");
$message = _i('Rimosso indirizzo email %s', [$email]);
$db_failures = $db->select("SELECT COUNT(*) as count FROM inner_logs WHERE type = 'mail' and message = '$message'");

if ($db_failures[0]->count >= 3 || $event == 'blocked') {
$db->delete("UPDATE contacts SET type = 'skip_email' 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 All @@ -41,13 +42,13 @@ private function saveInstances($email, $message)
}
}

private function registerBounce($email, $message)
private function registerBounce($event, $email, $message)
{
$message = sprintf(_i('Impossibile inoltrare mail a %s: %s', [$email, $message]));
$message = addslashes($message);

if (global_multi_installation()) {
$this->saveInstances($email, $message);
$this->saveInstances($event, $email, $message);
}
else {
InnerLog::error('mail', $message);
Expand All @@ -73,11 +74,13 @@ public function postStatusSES(Request $request)
}
else if ($message['Type'] === 'Notification') {
$data = json_decode($message['Message']);
if ($data->notificationType == 'Bounce') {
$event = $data->notificationType;

if ($event == 'Bounce') {
try {
$email = $data->bounce->bouncedRecipients[0]->emailAddress;
$message = $data->bounce->bouncedRecipients[0]->diagnosticCode ?? '???';
$this->registerBounce($email, $message);
$this->registerBounce($event, $email, $message);
}
catch(\Exception $e) {
Log::error('Notifica SNS illeggibile: ' . $e->getMessage() . ' - ' . print_r($data, true));
Expand Down Expand Up @@ -107,18 +110,7 @@ public function postStatusSendinblue(Request $request)
try {
$email = $request->input('email');
$message = $request->input('reason', '???');
$this->registerBounce($email, $message);

/*
Se l'indirizzo mail è stato bloccato, è inutile
inoltrare altri messaggi: qui ne cambio il tipo per
evitare di generare altre mail a vuoto
*/
if ($event == 'blocked') {
Contact::where('type', 'email')->where('value', $email)->update([
'type' => 'skip_email',
]);
}
$this->registerBounce($event, $email, $message);
}
catch(\Exception $e) {
Log::error('Notifica SendInBlue illeggibile: ' . $e->getMessage() . ' - ' . print_r($request->all(), true));
Expand Down
1 change: 1 addition & 0 deletions code/app/Services/DynamicBookingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private function translateBooking($booking, $delivering)
}, []),
];

$booking->status = $delivering ? 'shipped' : 'pending';
$modified = $booking->applyModifiers(null, false);
foreach($modified as $mod) {
if (!isset($ret->modifiers[$mod->modifier_id])) {
Expand Down
85 changes: 52 additions & 33 deletions code/app/Singletons/ModifierEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Singletons;

use App\ModifiedValue;
use Illuminate\Support\Facades\Log;

use Log;
use App\ModifiedValue;
use App\Product;

class ModifierEngine
{
Expand Down Expand Up @@ -95,45 +96,68 @@ private function targetDefinition($modifier, $value)
return null;
}

private function handlingAttributes($booking, $modifier)
private function handlingAttributes($booking, $modifier, $attribute)
{
/*
Fintantoché l'ordine non è marcato come "consegnato" uso le quantità
prenotate come riferimento per i calcoli (sulle soglie o per la
distribuzione dei costi sulle prenotazioni).
Se poi, alla fine, le quantità consegnate non corrispondono con
quelle prenotate, e dunque i calcoli devono essere revisionati per
ridistribuire in modo corretto il tutto, allora uso come riferimento
le quantità realmente consegnate: tale ricalcolo viene invocato da
OrdersController::postFixModifiers(), previa conferma dell'utente,
quando l'ordine è davvero in stato "consegnato"
*/
if (in_array($booking->status, ['saved', 'shipped'])) {
switch($modifier->applies_type) {
case 'none':
/*
Se l'ordine è chiuso (ma non consegnato e archiviato) attingo dai
valori relativi, che includono sia il consegnato che il prenotato ma
non ancora consegnato. Questo si applica in particolare in fase di
consegna
*/
if ($modifier->applies_target == 'order' || $booking->order->status == 'closed') {
switch($modifier->$attribute) {
case 'quantity':
$attribute = 'delivered';
$attribute = 'relative_quantity';
break;
case 'none':
case 'price':
$attribute = 'price_delivered';
$attribute = 'relative_price';
break;
case 'weight':
$attribute = 'weight_delivered';
$attribute = 'relative_weight';
break;
default:
$attribute = '';
break;
}

$mod_attribute = 'price_delivered';
}
else {
$attribute = $modifier->applies_type;
$mod_attribute = 'relative_price';
}

/*
Se sono qui, è perché sono in fase di prenotazione dunque mi baso
sui valori del prenotato
*/
else if ($booking->status == 'pending') {
$attribute = $modifier->$attribute;
if ($attribute == 'none') {
$attribute = 'price';
}

$mod_attribute = 'price';
}

/*
In tutti gli altri casi, opero sui valori del consegnato
*/
else {
switch($modifier->$attribute) {
case 'none':
case 'quantity':
$attribute = 'delivered';
break;
case 'price':
$attribute = 'price_delivered';
break;
case 'weight':
$attribute = 'weight_delivered';
break;
default:
$attribute = '';
break;
}

$mod_attribute = 'price_delivered';
}

return [$attribute, $mod_attribute];
Expand Down Expand Up @@ -221,7 +245,7 @@ public function apply($modifier, $booking, $aggregate_data)

$product_target_id = 0;

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

switch($modifier->applies_target) {
Expand Down Expand Up @@ -276,7 +300,7 @@ public function apply($modifier, $booking, $aggregate_data)
return null;
}

list($attribute, $mod_attribute) = $this->handlingAttributes($booking, $modifier);
list($attribute, $mod_attribute) = $this->handlingAttributes($booking, $modifier, 'applies_type');
$check_value = $check_target->$attribute ?? 0;
$target_definition = null;
$altered_amount = null;
Expand All @@ -295,14 +319,9 @@ public function apply($modifier, $booking, $aggregate_data)
singola prenotazione il suo valore relativo e proporzionale.
*/
if ($modifier->applies_target == 'order') {
$distribution_attribute = $modifier->distribution_type;
if ($distribution_attribute == 'none') {
$distribution_attribute = 'price';
}

$distribution_attribute = 'relative_' . $distribution_attribute;
list($distribution_attribute, $useless) = $this->handlingAttributes($booking, $modifier, 'distribution_type');

if ($modifier->target_type == 'App\Product') {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion code/resources/views/order/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php $summary = $master_summary->orders[$order->id] ?>

<x-larastrap::mform :obj="$order" classes="order-editor" method="PUT" :action="route('orders.update', $order->id)" :nodelete="$order->isActive() == false" :other_buttons="[['label' => _i('Esporta'), 'classes' => ['float-start', 'link-button', 'me-2'], 'attributes' => ['data-link' => $order->exportableURL()]]]">
<x-larastrap::mform :obj="$order" classes="order-editor" method="PUT" :action="route('orders.update', $order->id)" :nodelete="$order->bookings()->count() > 0" :other_buttons="[['label' => _i('Esporta'), 'classes' => ['float-start', 'link-button', 'me-2'], 'attributes' => ['data-link' => $order->exportableURL()]]]">
<input type="hidden" name="order_id" value="{{ $order->id }}" />
<input type="hidden" name="post-saved-function" value="afterAggregateChange" class="skip-on-submit">

Expand Down

0 comments on commit d870ad6

Please sign in to comment.