Skip to content

Commit

Permalink
aggiustamenti minori
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 26, 2023
1 parent 518589b commit e584dda
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 264 deletions.
23 changes: 9 additions & 14 deletions code/app/AggregateBooking.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

use App\Models\Concerns\TracksUpdater;
use App\GASModel;
Expand All @@ -22,18 +23,19 @@ class AggregateBooking extends Model
public $id;
public $user;
public $aggregate;
public $bookings = [];
public $bookings;

public function __construct($user_id, $aggregate)
{
$this->id = $user_id;
$this->user = User::withTrashed()->find($user_id);
$this->aggregate = $aggregate;
$this->bookings = new Collection();
}

public function add($booking)
{
$this->bookings[] = $booking;
$this->bookings->push($booking);
}

public function getCreatedAtAttribute()
Expand Down Expand Up @@ -176,12 +178,7 @@ public function getConvenientStringsAttribute()
public function generateReceipt()
{
if ($this->user->gas->hasFeature('extra_invoicing')) {
$ids = [];
foreach ($this->bookings as $booking) {
if ($booking->exists) {
$ids[] = $booking->id;
}
}
$ids = $this->bookings->filter(fn($b) => $b->exists)->pluck('id')->all();

if (empty($ids)) {
\Log::error('Tentativo di creare fattura non assegnata a nessuna prenotazione');
Expand Down Expand Up @@ -215,12 +212,10 @@ 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;
}
foreach($this->bookings->filter(fn($b) => $b->updater) as $booking) {
if (is_null($last_update) || $booking->updated_at->greaterThan($last_update)) {
$last_update = $booking->updated_at;
$last_updater = $booking->updater;
}
}

Expand Down
72 changes: 2 additions & 70 deletions code/app/Gas.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
use Log;

use App\Models\Concerns\AttachableTrait;
use App\Models\Concerns\Configurable;
use App\Models\Concerns\PayableTrait;
use App\Models\Concerns\CreditableTrait;
use App\Events\SluggableCreating;

class Gas extends Model
{
use HasFactory, AttachableTrait, CreditableTrait, PayableTrait, GASModel, SluggableID, Cachable;
use HasFactory, Configurable, AttachableTrait, CreditableTrait, PayableTrait, GASModel, SluggableID, Cachable;

public $incrementing = false;
protected $keyType = 'string';
Expand Down Expand Up @@ -66,75 +67,6 @@ public function deliveries(): BelongsToMany
return $this->belongsToMany('App\Delivery')->orderBy('name', 'asc');
}

public function configs(): HasMany
{
return $this->hasMany('App\Config');
}

private function availableConfigs()
{
return systemParameters('Config');
}

public function getConfig($name)
{
foreach ($this->configs as $conf) {
if ($conf->name == $name) {
return $conf->value;
}
}

$defined = $this->availableConfigs();
if (!isset($defined[$name])) {
Log::error(_i('Configurazione GAS non prevista'));
return '';
}
else {
$this->setConfig($name, $defined[$name]->default());
$this->load('configs');
return $this->getConfig($name);
}
}

public function setConfig($name, $value)
{
if (is_object($value) || is_array($value)) {
$value = json_encode($value);
}

foreach ($this->configs as $conf) {
if ($conf->name == $name) {
$conf->value = $value;
$conf->save();
return;
}
}

$conf = new Config();
$conf->name = $name;
$conf->value = $value;
$conf->gas_id = $this->id;
$conf->save();
}

/*
Questa funzione permette di accedere direttamente alle configurazioni
del GAS, usandone il nome (definito da ciascun parametro definito nelle
classi nel namespace App\Parameters\Config)
*/
public function getAttribute($key)
{
$configs = $this->availableConfigs();
$c = $configs[$key] ?? null;

if ($c) {
return $c->asAttribute($this);
}
else {
return parent::getAttribute($key);
}
}

public function nextInvoiceNumber()
{
$status = $this->extra_invoicing;
Expand Down
4 changes: 2 additions & 2 deletions code/app/Http/Controllers/AttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function store(Request $request)
return $this->errorResponse(_i('Non autorizzato'));
}

$a = $target->attachByRequest($request);
$a = $target->attachByRequest($request->all());
if ($a === false) {
return $this->errorResponse(_i('File non caricato correttamente'));
}
Expand All @@ -55,7 +55,7 @@ public function update(Request $request, $id)
}

if ($request->hasFile('file')) {
$a = $a->attached->attachByRequest($request, $a->id);
$a = $a->attached->attachByRequest($request->all(), $a->id);
if ($a === false) {
return $this->errorResponse(_i('File non caricato correttamente'));
}
Expand Down
8 changes: 4 additions & 4 deletions code/app/Http/Controllers/DeliveriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public function destroy($id)

if ($d->default) {
$fallback = Delivery::where('id', '!=', $d->id)->orderBy(DB::raw('RAND()'))->first();
if ($fallback != null)
$fallback_id = $fallback->id;
}
else {
$fallback = Delivery::where('default', true)->first();
if ($fallback != null)
$fallback_id = $fallback->id;
}

if (is_null($fallback) == false) {
$fallback_id = $fallback->id;
}

foreach($d->users as $u) {
Expand Down
Loading

0 comments on commit e584dda

Please sign in to comment.