Skip to content

Commit

Permalink
tracciamento modifiche e visualizzazione data e utente. closes #231
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 21, 2023
1 parent ccad413 commit 9f7e6e5
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 41 deletions.
31 changes: 27 additions & 4 deletions code/app/AggregateBooking.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use Illuminate\Database\Eloquent\Model;

use Log;

use App\Models\Concerns\TracksUpdater;
use App\GASModel;
use App\Aggregate;
use App\User;
Expand All @@ -18,7 +17,7 @@

class AggregateBooking extends Model
{
use GASModel;
use GASModel, TracksUpdater;

public $id;
public $user;
Expand Down Expand Up @@ -185,7 +184,7 @@ public function generateReceipt()
}

if (empty($ids)) {
Log::error('Tentativo di creare fattura non assegnata a nessuna prenotazione');
\Log::error('Tentativo di creare fattura non assegnata a nessuna prenotazione');
return;
}

Expand All @@ -208,4 +207,28 @@ public function printableHeader()
{
return $this->user->printableName() . $this->headerIcons();
}

/********************************************************** TracksUpdater */

public function getPrintableUpdaterAttribute()
{
$last_update = null;
$last_updater = null;

foreach($this->bookings as $booking) {
if ($booking->updater) {
if (is_null($last_update) || $booking->updated_at->greaterThan($last_update)) {
$last_update = $booking->updated_at;
$last_updater = $booking->updater;
}
}
}

if ($last_updater) {
return _i('Ultima Modifica: <br class="d-block d-md-none">%s - %s', $last_update->format('d/m/Y'), $last_updater->printableName());
}
else {
return '';
}
}
}
5 changes: 4 additions & 1 deletion code/app/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
use App\Models\Concerns\PayableTrait;
use App\Models\Concerns\CreditableTrait;
use App\Models\Concerns\ReducibleTrait;
use App\Models\Concerns\TracksUpdater;
use App\Scopes\RestrictedGAS;
use App\Events\SluggableCreating;
use App\Events\BookingDeleting;

class Booking extends Model
{
use HasFactory, GASModel, SluggableID, ModifiedTrait, PayableTrait, CreditableTrait, ReducibleTrait, Cachable;
use HasFactory, GASModel, SluggableID, TracksUpdater, ModifiedTrait, PayableTrait, CreditableTrait, ReducibleTrait, Cachable;

public $incrementing = false;
protected $keyType = 'string';
Expand All @@ -48,6 +49,8 @@ protected static function boot()
{
parent::boot();

static::initTrackingEvents();

/*
Questo è per limitare le prenotazioni a quelle effettivamente
accessibili nel GAS corrente
Expand Down
19 changes: 12 additions & 7 deletions code/app/Delivery.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

/*
Questa classe rappresenta un luogo di consegna
*/

namespace App;

use Illuminate\Database\Eloquent\Model;
Expand All @@ -8,20 +12,15 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

use Auth;

use App\Models\Concerns\ModifiableTrait;
use App\Models\Concerns\WithinGas;
use App\Models\Concerns\TracksUpdater;
use App\Events\SluggableCreating;
use App\Events\AttachableToGas;

/*
Questa classe rappresenta un luogo di consegna
*/

class Delivery extends Model
{
use HasFactory, ModifiableTrait, GASModel, SluggableID, WithinGas, Cachable;
use HasFactory, TracksUpdater, ModifiableTrait, GASModel, SluggableID, WithinGas, Cachable;

public $incrementing = false;
protected $keyType = 'string';
Expand All @@ -31,6 +30,12 @@ class Delivery extends Model
'created' => AttachableToGas::class
];

protected static function boot()
{
parent::boot();
static::initTrackingEvents();
}

public function users(): HasMany
{
return $this->hasMany(User::class, 'preferred_delivery_id');
Expand Down
24 changes: 22 additions & 2 deletions code/app/Helpers/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ function formatPeriodicToComponent($component, $params)
return $params;
}

function formatUpdater($buttons, $params)
{
$obj = $params['obj'];

if ($obj && hasTrait($obj, \App\Models\Concerns\TracksUpdater::class)) {
$buttons[] = [
'element' => 'larastrap::updater',
];
}

return $buttons;
}

function formatInnerLastUpdater($component, $params)
{
$params['buttons'] = formatUpdater($params['buttons'], $params);
return $params;
}

function formatMainFormButtons($component, $params)
{
/*
Expand All @@ -192,10 +211,11 @@ function formatMainFormButtons($component, $params)
$params['main_form_managed'] = 'ongoing';
$buttons = $params['attributes']['other_buttons'] ?? [];

$buttons = formatUpdater($buttons, $params);
$obj = $params['obj'];

$nodelete = filter_var($params['attributes']['nodelete'] ?? false, FILTER_VALIDATE_BOOLEAN);
if (!$nodelete) {
$obj = $params['obj'];

$buttons[] = [
'color' => 'danger',
'classes' => ['delete-button'],
Expand Down
4 changes: 3 additions & 1 deletion code/app/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use App\Models\Concerns\CreditableTrait;
use App\Models\Concerns\HierarcableTrait;
use App\Models\Concerns\AttachableTrait;
use App\Models\Concerns\TracksUpdater;
use App\Scopes\RestrictedGAS;
use App\Events\SluggableCreating;

class Invoice extends Model implements Datable
{
use GASModel, PayableTrait, CreditableTrait, HierarcableTrait, AttachableTrait, SluggableID;
use GASModel, SluggableID, TracksUpdater, PayableTrait, CreditableTrait, HierarcableTrait, AttachableTrait;

public $incrementing = false;
protected $keyType = 'string';
Expand All @@ -28,6 +29,7 @@ class Invoice extends Model implements Datable
protected static function boot()
{
parent::boot();
static::initTrackingEvents();
static::addGlobalScope(new RestrictedGAS());
}

Expand Down
42 changes: 42 additions & 0 deletions code/app/Models/Concerns/TracksUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Auth;

use App\User;

trait TracksUpdater
{
public function updater(): BelongsTo
{
return $this->belongsTo(User::class, 'updated_by');
}

public function getPrintableUpdaterAttribute(): string
{
if ($this->updater) {
return _i('Ultima Modifica: <br class="d-block d-md-none">%s - %s', $this->updated_at->format('d/m/Y'), $this->updater->printableName());
}
else {
return '';
}
}

private static function updateUser($model): void
{
if ($model->isDirty('updated_by') == false) {
$user = Auth::user();
if ($user) {
$model->updated_by = $user->id;
}
}
}

protected static function initTrackingEvents(): void
{
static::creating(fn($model) => static::updateUser($model));
static::updating(fn($model) => static::updateUser($model));
}
}
15 changes: 11 additions & 4 deletions code/app/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Collection;

use GeneaLabs\LaravelModelCaching\Traits\Cachable;

use Log;
use App\Models\Concerns\TracksUpdater;

class Modifier extends Model
{
use GASModel, Cachable;
use GASModel, TracksUpdater, Cachable;

protected static function boot()
{
parent::boot();
static::initTrackingEvents();
}

public function modifierType(): BelongsTo
{
return $this->belongsTo('App\ModifierType');
return $this->belongsTo(ModifierType::class);
}

public function movementType(): BelongsTo
{
return $this->belongsTo('App\MovementType');
return $this->belongsTo(MovementType::class);
}

public function target(): MorphTo
Expand Down
7 changes: 3 additions & 4 deletions code/app/Movement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Http\Request;

use Auth;
use Log;

use App\Models\Concerns\TracksUpdater;
use App\Scopes\RestrictedGAS;

class Movement extends Model
{
use HasFactory, GASModel;
use HasFactory, TracksUpdater, GASModel;

/*
Per verificare il corretto salvataggio di un movimento, non consultare
Expand All @@ -40,6 +38,7 @@ class Movement extends Model
protected static function boot()
{
parent::boot();
static::initTrackingEvents();
static::addGlobalScope(new RestrictedGAS('registerer', true));
}

Expand Down
18 changes: 8 additions & 10 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

use App;
use Auth;
use DB;
use Mail;
use URL;
use Log;
use Carbon\Carbon;

use App\Models\Concerns\AttachableTrait;
Expand All @@ -24,12 +20,13 @@
use App\Models\Concerns\ModifiableTrait;
use App\Models\Concerns\ExportableTrait;
use App\Models\Concerns\ReducibleTrait;
use App\Models\Concerns\TracksUpdater;
use App\Scopes\RestrictedGAS;
use App\Events\SluggableCreating;

class Order extends Model
{
use HasFactory, AttachableTrait, ExportableTrait, ModifiableTrait, PayableTrait, CreditableTrait, GASModel, SluggableID, ReducibleTrait;
use HasFactory, TracksUpdater, AttachableTrait, ExportableTrait, ModifiableTrait, PayableTrait, CreditableTrait, GASModel, SluggableID, ReducibleTrait;

public $incrementing = false;
protected $keyType = 'string';
Expand All @@ -41,6 +38,7 @@ class Order extends Model
protected static function boot()
{
parent::boot();
static::initTrackingEvents();
static::addGlobalScope(new RestrictedGAS('aggregate.gas'));
}

Expand Down Expand Up @@ -234,7 +232,7 @@ public function topLevelBookings($status = null)

public function getInternalNumberAttribute()
{
return App::make('OrderNumbersDispatcher')->getNumber($this);
return app()->make('OrderNumbersDispatcher')->getNumber($this);
}

public function getLongCommentAttribute()
Expand Down Expand Up @@ -443,7 +441,7 @@ public function pendingPackages()

if ($products->isEmpty() == false) {
$order = $this;
$order_data = App::make('GlobalScopeHub')->executedForAll($this->keep_open_packages != 'each', function() use ($order) {
$order_data = app()->make('GlobalScopeHub')->executedForAll($this->keep_open_packages != 'each', function() use ($order) {
return $order->reduxData();
});

Expand Down Expand Up @@ -799,7 +797,7 @@ public function exportXML()

public function exportJSON()
{
$hub = App::make('GlobalScopeHub');
$hub = app()->make('GlobalScopeHub');
$gas = Gas::find($hub->getGas());
return view('gdxp.json.supplier', ['obj' => $this->supplier, 'order' => $this, 'currentgas' => $gas])->render();
}
Expand Down
9 changes: 8 additions & 1 deletion code/app/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

use App\Models\Concerns\ModifiableTrait;
use App\Models\Concerns\Priceable;
use App\Models\Concerns\TracksUpdater;
use App\Events\VariantChanged;
use App\Events\SluggableCreating;

class Product extends Model
{
use HasFactory, SoftDeletes, Priceable, ModifiableTrait, GASModel, SluggableID, Cachable;
use HasFactory, SoftDeletes, TracksUpdater, Priceable, ModifiableTrait, GASModel, SluggableID, Cachable;

public $incrementing = false;
protected $keyType = 'string';
Expand All @@ -34,6 +35,12 @@ class Product extends Model
'active' => 'boolean',
];

protected static function boot()
{
parent::boot();
static::initTrackingEvents();
}

public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
Expand Down
Loading

0 comments on commit 9f7e6e5

Please sign in to comment.