From f9eb90aea703b3c266ce26a3849ac9f6029f7778 Mon Sep 17 00:00:00 2001 From: Roberto Guido Date: Mon, 13 Nov 2023 01:43:23 +0100 Subject: [PATCH] piccoli aggiustamenti --- code/app/Booking.php | 34 ---------- code/app/Console/Commands/ArchiveBalances.php | 10 +-- .../Console/Commands/RecalculateBalances.php | 10 +-- code/app/Helpers/Reflection.php | 14 ++-- .../Controllers/DeliveryUserController.php | 4 +- code/app/Importers/CSV/Deliveries.php | 15 ++--- code/app/Providers/ServicesProvider.php | 66 +++++++++++++++++++ code/app/Role.php | 37 ----------- code/app/Services/BaseService.php | 6 +- code/app/Services/FastBookingsService.php | 2 +- code/app/Services/MultiGasService.php | 9 ++- code/app/Services/RolesService.php | 29 ++++++-- code/app/Singletons/ModifierEngine.php | 7 +- code/config/app.php | 1 + code/database/seeders/DemoSeeder.php | 6 +- .../views/permissions/edit.blade.php | 6 +- 16 files changed, 131 insertions(+), 125 deletions(-) create mode 100644 code/app/Providers/ServicesProvider.php diff --git a/code/app/Booking.php b/code/app/Booking.php index 8056be265..e86aa235c 100644 --- a/code/app/Booking.php +++ b/code/app/Booking.php @@ -467,45 +467,11 @@ public function printableName() return $this->order->printableName(); } - /* - TODO: questa funzione potrebbe essere da sopprimere. Da verificare - */ - public function printableHeader() - { - \Log::debug('printableHeader di Booking'); - - $ret = $this->printableName(); - - $user = Auth::user(); - - $tot = $this->getValue('effective', false); - $friends_tot = $this->total_friends_value; - - if($tot == 0 && $friends_tot == 0) { - $message = _i("Non hai partecipato a quest'ordine"); - } - else { - $message = _i('Hai ordinato %s', [printablePriceCurrency($tot)]); - if ($friends_tot != 0) { - // @phpstan-ignore-next-line - $message += sprintf(' + %s', printablePriceCurrency($friends_tot)); - } - } - - $ret .= '' . $message . ''; - return $ret; - } - public function getShowURL() { return route('booking.user.show', ['booking' => $this->order->aggregate_id, 'user' => $this->user_id]); } - public function getModalURL() - { - return route('booking.modal', ['aggregate_id' => $this->order->aggregate_id, 'user_id' => $this->user_id]); - } - public function wipeStatus() { if ($this->payment) { diff --git a/code/app/Console/Commands/ArchiveBalances.php b/code/app/Console/Commands/ArchiveBalances.php index be4418019..a5a9bc6d8 100644 --- a/code/app/Console/Commands/ArchiveBalances.php +++ b/code/app/Console/Commands/ArchiveBalances.php @@ -4,22 +4,14 @@ use Illuminate\Console\Command; -use App\Services\MovementsService; - class ArchiveBalances extends Command { protected $signature = 'balances:archive {date}'; protected $description = 'Archivia i saldi ad una certa data'; - public function __construct() - { - parent::__construct(); - } - public function handle() { $date = $this->argument('date'); - $service = new MovementsService(); - $service->closeBalance(['date' => $date]); + app()->make('MovementsService')->closeBalance(['date' => $date]); } } diff --git a/code/app/Console/Commands/RecalculateBalances.php b/code/app/Console/Commands/RecalculateBalances.php index 7c5cce704..b723f17f6 100644 --- a/code/app/Console/Commands/RecalculateBalances.php +++ b/code/app/Console/Commands/RecalculateBalances.php @@ -4,21 +4,13 @@ use Illuminate\Console\Command; -use App\Services\MovementsService; - class RecalculateBalances extends Command { protected $signature = 'balances:recalculate'; protected $description = 'Effettua un ricalcolo saldi'; - public function __construct() - { - parent::__construct(); - } - public function handle() { - $service = new MovementsService(); - $service->recalculate(); + app()->make('MovementsService')->recalculate(); } } diff --git a/code/app/Helpers/Reflection.php b/code/app/Helpers/Reflection.php index 194fa0d9f..e13c77377 100644 --- a/code/app/Helpers/Reflection.php +++ b/code/app/Helpers/Reflection.php @@ -115,16 +115,22 @@ function inlineId($obj) return sprintf('%s---%s', $class, $obj->id); } -function fromInlineId($id) +function fromInlineId($identifier) { - $parts = explode('---', $id); + $parts = explode('---', $identifier); if (count($parts) != 2) { - throw new \Exception("Identificativo non valido per recupero riferimento: " . $id, 1); + throw new \Exception("Identificativo non valido per recupero riferimento: " . $identifier, 1); } list($class, $id) = $parts; $class = sprintf('App\\%s', $class); - return $class::find($id); + + $ret = $class::find($id); + if (is_null($ret)) { + \Log::error("Identificativo non valido per recupero riferimento: " . $identifier); + } + + return $ret; } function unrollSpecialSelectors($users) diff --git a/code/app/Http/Controllers/DeliveryUserController.php b/code/app/Http/Controllers/DeliveryUserController.php index b71c677ca..ab063dc25 100644 --- a/code/app/Http/Controllers/DeliveryUserController.php +++ b/code/app/Http/Controllers/DeliveryUserController.php @@ -7,7 +7,6 @@ use URL; use App\Services\BookingsService; -use App\Services\FastBookingsService; use App\User; use App\Aggregate; @@ -83,8 +82,7 @@ public function postFastShipping(Request $request, $aggregate_id) ]; } - $fastshipping = new FastBookingsService(); - $fastshipping->fastShipping($deliverer, $aggregate, $users); + app()->make('FastBookingsService')->fastShipping($deliverer, $aggregate, $users); return $this->successResponse(); } diff --git a/code/app/Importers/CSV/Deliveries.php b/code/app/Importers/CSV/Deliveries.php index 9ccb9f039..291f0415f 100644 --- a/code/app/Importers/CSV/Deliveries.php +++ b/code/app/Importers/CSV/Deliveries.php @@ -4,12 +4,10 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; -use App; -use Auth; -use DB; +use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; -use App\Services\BookingsService; -use App\Services\FastBookingsService; use App\User; use App\Aggregate; use App\Order; @@ -59,7 +57,7 @@ public function guess($request) public function select($request) { $user = Auth::user(); - $service = new BookingsService(); + $service = app()->make('BookingsService'); $errors = []; list($reader, $columns) = $this->initRead($request); @@ -201,7 +199,7 @@ public function formatSelect($parameters) public function run($request) { $user = Auth::user(); - $service = new BookingsService(); + $service = app()->make('BookingsService'); $data = json_decode($request->input('data', '[]'), true); $users = $request->input('user', []); @@ -229,8 +227,7 @@ public function run($request) DB::commit(); if ($action == 'close') { - $fast = new FastBookingsService(); - $fast->fastShipping($user, $target_order->aggregate, null); + app()->make('FastBookingsService')->fastShipping($user, $target_order->aggregate, null); } return [ diff --git a/code/app/Providers/ServicesProvider.php b/code/app/Providers/ServicesProvider.php new file mode 100644 index 000000000..96c913077 --- /dev/null +++ b/code/app/Providers/ServicesProvider.php @@ -0,0 +1,66 @@ +services(); + + foreach($classes as $class) { + $this->app->singleton(class_basename($class), function ($app) use ($class) { + return new $class(); + }); + } + } + + public function provides(): array + { + $classes = $this->services(); + $ret = []; + + foreach($classes as $class) { + $ret[] = class_basename($class); + } + + return $ret; + } +} diff --git a/code/app/Role.php b/code/app/Role.php index abbab6c67..04838609a 100644 --- a/code/app/Role.php +++ b/code/app/Role.php @@ -391,43 +391,6 @@ public function mandatoryAction($action) return false; } - public function enableAction($action) - { - if ($this->enabledAction($action) == false) { - $this->actions .= ',' . $action; - $this->save(); - - /* - Se attivo un permesso che ha un solo target (di solito: il GAS), - attacco quest'ultimo direttamente a tutti gli utenti coinvolti - */ - $class = classByRule($action); - if ($class::count() == 1) { - $only_target = $class::first(); - - foreach($this->users as $user) { - $urole = $user->roles()->where('roles.id', $this->id)->first(); - if ($urole) - $urole->attachApplication($only_target); - } - } - } - } - - public function disableAction($action) - { - $new_actions = []; - $actions = explode(',', $this->actions); - foreach($actions as $a) { - if ($a == $action) - continue; - $new_actions[] = $a; - } - - $this->actions = join(',', $new_actions); - $this->save(); - } - public static function havingAction($action) { return Role::where('actions', 'LIKE', "%$action%")->get(); diff --git a/code/app/Services/BaseService.php b/code/app/Services/BaseService.php index 21e7f990e..380ce4ad1 100644 --- a/code/app/Services/BaseService.php +++ b/code/app/Services/BaseService.php @@ -2,10 +2,10 @@ namespace App\Services; -use App\Exceptions\AuthException; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; -use Auth; -use Log; +use App\Exceptions\AuthException; class BaseService { diff --git a/code/app/Services/FastBookingsService.php b/code/app/Services/FastBookingsService.php index 446a82a0c..e014e3255 100644 --- a/code/app/Services/FastBookingsService.php +++ b/code/app/Services/FastBookingsService.php @@ -70,7 +70,7 @@ public function fastShipping($deliverer, $aggregate, $users = null) { DB::beginTransaction(); - $service = new BookingsService(); + $service = app()->make('BookingsService'); $default_payment_method = defaultPaymentByType('booking-payment'); $bookings = $aggregate->bookings; diff --git a/code/app/Services/MultiGasService.php b/code/app/Services/MultiGasService.php index 5d5309433..3f9ae6d95 100644 --- a/code/app/Services/MultiGasService.php +++ b/code/app/Services/MultiGasService.php @@ -2,9 +2,9 @@ namespace App\Services; -use Auth; -use Log; -use DB; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\DB; use App\Gas; use App\User; @@ -66,9 +66,8 @@ public function store(array $request) DB::beginTransaction(); - $user_service = new UsersService(); $user_params = array_intersect_key($request, array_flip(['username', 'firstname', 'lastname', 'password', 'enforce_password_change'])); - $admin = $user_service->store($user_params); + $admin = app()->make('UsersService')->store($user_params); $gas = new Gas(); $this->setIfSet($gas, $request, 'name'); diff --git a/code/app/Services/RolesService.php b/code/app/Services/RolesService.php index 021886127..2d03ab3d0 100644 --- a/code/app/Services/RolesService.php +++ b/code/app/Services/RolesService.php @@ -110,18 +110,35 @@ public function attachAction($role_id, $action) $this->ensureAuth(['gas.permissions' => 'gas']); $r = Role::findOrFail($role_id); - if ($action) { - $r->enableAction($action); + if ($r->enabledAction($action)) { + return; } + + $r->actions .= ',' . $action; + $r->save(); + + /* + Se attivo un permesso che ha un solo target (di solito: il GAS), + attacco quest'ultimo direttamente a tutti gli utenti coinvolti + */ + $class = classByRule($action); + if ($class::count() == 1) { + $only_target = $class::first(); + + foreach($r->users as $user) { + $urole = $user->roles()->where('roles.id', $r->id)->first(); + $urole->attachApplication($only_target); + } + } } public function detachAction($role_id, $action) { $this->ensureAuth(['gas.permissions' => 'gas']); - $r = Role::findOrFail($role_id); - if ($action) { - $r->disableAction($action); - } + $actions = explode(',', $r->actions); + $new_actions = array_filter($actions, fn($a) => $a != $action); + $r->actions = join(',', $new_actions); + $r->save(); } } diff --git a/code/app/Singletons/ModifierEngine.php b/code/app/Singletons/ModifierEngine.php index dd1a37edb..747c0859b 100644 --- a/code/app/Singletons/ModifierEngine.php +++ b/code/app/Singletons/ModifierEngine.php @@ -184,6 +184,11 @@ public function apply($modifier, $booking, $aggregate_data) return null; } + if (is_null($modifier->target)) { + \Log::debug('Modificatore senza oggetto di riferimento: ' . $modifier->id); + return null; + } + if (!isset($aggregate_data->orders[$booking->order_id])) { return null; } @@ -191,7 +196,7 @@ public function apply($modifier, $booking, $aggregate_data) $product_target_id = 0; if ($modifier->target_type == 'App\Product') { - $product_target_id = $modifier->target->id; + $product_target_id = $modifier->target_id; switch($modifier->applies_target) { case 'order': diff --git a/code/config/app.php b/code/config/app.php index 97c16da5d..ebf31e332 100644 --- a/code/config/app.php +++ b/code/config/app.php @@ -154,6 +154,7 @@ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + App\Providers\ServicesProvider::class, App\Providers\SingletonsProvider::class, App\Providers\GraphicInitServiceProvider::class, App\Providers\MenuServiceProvider::class, diff --git a/code/database/seeders/DemoSeeder.php b/code/database/seeders/DemoSeeder.php index b46e6827b..a5444234f 100644 --- a/code/database/seeders/DemoSeeder.php +++ b/code/database/seeders/DemoSeeder.php @@ -71,7 +71,7 @@ public function run() } $role = roleByIdentifier('user'); - $role->enableAction('users.subusers'); + app()->make('RolesService')->attachAction($role->id, 'users.subusers'); $referrer_role = Role::where('name', 'Referente')->first(); $administrator = User::where('username', 'root')->first(); @@ -234,7 +234,7 @@ public function run() $d->description = '{"end":"10","shipping":"12","comment":"","suspend":"true"}'; $d->target_type = Supplier::class; $d->target_id = 'la-zucchina-dorata'; - $d->recurring = '{"day":"thursday","cycle":"biweekly","from":"' Carbon::today()->subMonths(1)->endOfMonth()->format('Y-m-d') '","to":"' . Carbon::today()->addMonths(2)->endOfMonth()->format('Y-m-d') . '"}'; + $d->recurring = '{"day":"thursday","cycle":"biweekly","from":"' . Carbon::today()->subMonths(1)->endOfMonth()->format('Y-m-d') . '","to":"' . Carbon::today()->addMonths(2)->endOfMonth()->format('Y-m-d') . '"}'; $d->save(); $d = new Date(); @@ -242,7 +242,7 @@ public function run() $d->description = '{"end":"10","shipping":"15","comment":"","suspend":"true"}'; $d->target_type = Supplier::class; $d->target_id = 'luigi-il-macellaio'; - $d->recurring = '{"day":"wednesday","cycle":"month_third","from":"' Carbon::today()->subMonths(1)->endOfMonth()->format('Y-m-d') '","to":"' . Carbon::today()->addMonths(2)->endOfMonth()->format('Y-m-d') . '"}'; + $d->recurring = '{"day":"wednesday","cycle":"month_third","from":"' . Carbon::today()->subMonths(1)->endOfMonth()->format('Y-m-d') . '","to":"' . Carbon::today()->addMonths(2)->endOfMonth()->format('Y-m-d') . '"}'; $d->save(); } } diff --git a/code/resources/views/permissions/edit.blade.php b/code/resources/views/permissions/edit.blade.php index aa784f5bb..ccf271fa3 100644 --- a/code/resources/views/permissions/edit.blade.php +++ b/code/resources/views/permissions/edit.blade.php @@ -19,9 +19,13 @@