Skip to content

Commit

Permalink
pannello ricevute in profilo utente
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Jul 15, 2024
1 parent 5a05c50 commit 240c33c
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 42 deletions.
5 changes: 5 additions & 0 deletions code/app/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public function payment(): BelongsTo
return $this->belongsTo(Movement::class);
}

public function receipts(): BelongsToMany
{
return $this->belongsToMany('App\Receipt');
}

/*
Con questo scope si caricano le relazioni utilizzate per il calcolo dei
modificatori.
Expand Down
35 changes: 22 additions & 13 deletions code/app/Http/Controllers/ReceiptsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ public function __construct(ReceiptsService $service)
]);
}

private function filterByUser($request)
{
return $request->input('user_id', '0');
}

public function index(Request $request)
{
$past = date('Y-m-d', strtotime('-1 months'));
$future = date('Y-m-d', strtotime('+10 years'));
$receipts = $this->service->list($past, $future, 0);
$user_id = $this->filterByUser($request);
$receipts = $this->service->list($past, $future, 0, $user_id);

return view('receipt.index', [
'receipts' => $receipts,
'user_id' => $user_id,
]);
}

Expand All @@ -40,7 +48,7 @@ public function show(Request $request, $id)
if ($user->can('movements.admin', $user->gas)) {
return view('receipt.edit', ['receipt' => $receipt]);
}
else if ($user->can('movements.view', $user->gas)) {
else if ($user->can('movements.view', $user->gas) || $receipt->user->id == $user->id) {
return view('receipt.show', ['receipt' => $receipt]);
}
else {
Expand All @@ -50,7 +58,7 @@ public function show(Request $request, $id)

private function testAccess($user, $receipt)
{
if ($user->can('movements.admin', $user->gas) || $user->can('movements.view', $user->gas) || $receipt->user_id == $user->id) {
if ($user->can('movements.admin', $user->gas) || $user->can('movements.view', $user->gas) || $receipt->user->id == $user->id) {
return true;
}
else {
Expand Down Expand Up @@ -120,14 +128,14 @@ private function outputCSV($elements)

private function send($elements): void
{
foreach($elements as $receipt) {
if ($receipt->mailed == false) {
try {
$this->sendByMail($receipt);
}
catch(\Exception $e) {
\Log::error('Errore in inoltro ricevuta: ' . $e->getMessage());
}
$to_send = $elements->filter(fn($r) => $r->mailed == false);

foreach($to_send as $receipt) {
try {
$this->sendByMail($receipt);
}
catch(\Exception $e) {
\Log::error('Errore in inoltro ricevuta: ' . $e->getMessage());
}
}
}
Expand All @@ -136,8 +144,9 @@ public function search(Request $request)
{
$start = decodeDate($request->input('startdate'));
$end = decodeDate($request->input('enddate'));
$supplier_id = $request->input('supplier_id');
$elements = $this->service->list($start, $end, $supplier_id);
$supplier_id = $request->input('supplier_id', '0');
$user_id = $this->filterByUser($request);
$elements = $this->service->list($start, $end, $supplier_id, $user_id);

$format = $request->input('format', 'none');

Expand Down
3 changes: 3 additions & 0 deletions code/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected function enforceInstance($event)
{
$payload = $this->getEventPayload($event);

\Log::debug('Inizializzazione job: ' . ($payload['env_file'] ?? '[no env]') . ' / ' . ($payload['gas_id'] ?? '[no gas]'));

$env_file = $payload['env_file'] ?? null;
if ($env_file) {
/*
Expand Down Expand Up @@ -106,6 +108,7 @@ private function initQueues()
$ret['env_file'] = env_file();
}

\Log::debug('Avvio job: ' . print_r($ret, true));
return $ret;
});

Expand Down
32 changes: 25 additions & 7 deletions code/app/Services/ReceiptsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@

namespace App\Services;

use Illuminate\Support\Facades\Auth;

use App\Receipt;

class ReceiptsService extends BaseService
{
public function list($start, $end, $supplier_id)
public function list($start, $end, $supplier_id, $user_id = 0)
{
$this->ensureAuth(['movements.admin' => 'gas']);
if ($user_id == '0') {
$this->ensureAuth(['movements.admin' => 'gas']);
}
else {
$user = Auth::user();

if ($user->id != $user_id) {
$this->ensureAuth(['movements.admin' => 'gas']);
}
}

$query = Receipt::where('date', '>=', $start)->where('date', '<=', $end)->orderBy('date', 'desc');

if ($supplier_id != '0') {
$query->whereHas('bookings', function($query) use ($supplier_id) {
$query->whereHas('order', function($query) use ($supplier_id) {
$query->where('supplier_id', $supplier_id);
});
if ($supplier_id != '0' || $user_id != 0) {
$query->whereHas('bookings', function($query) use ($supplier_id, $user_id) {
if ($user_id != 0) {
$query->where('user_id', $user_id);
}

if ($supplier_id != '0') {
$query->whereHas('order', function($query) use ($supplier_id) {
$query->where('supplier_id', $supplier_id);
});
}
});
}

Expand Down
1 change: 1 addition & 0 deletions code/app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

Expand Down
44 changes: 26 additions & 18 deletions code/resources/views/receipt/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
<div>
<div class="row">
<div class="col-12 col-md-6">
@php
@php
$loadable_attributes = [
'identifier' => 'receipts-list',
'items' => $receipts,
];
$actions = [
['link' => route('receipts.search', ['format' => 'send']), 'label' => _i('Inoltra Ricevute in Attesa')],
];
if ($user_id == '0') {
$actions = [
['link' => route('receipts.search', ['format' => 'send']), 'label' => _i('Inoltra Ricevute in Attesa')],
];
$downloads = [
['link' => route('receipts.search', ['format' => 'csv']), 'label' => _i('Esporta CSV')],
];
$downloads = [
['link' => route('receipts.search', ['format' => 'csv']), 'label' => _i('Esporta CSV')],
];
@endphp
$loadable_attributes['legend'] = (object)['class' => App\Receipt::class];
}
else {
$actions = [];
$downloads = [];
}
@endphp

<div>
<div class="row">
<div class="col-12 col-md-6">
<x-filler :data-action="route('receipts.search')" data-fill-target="#receipts-in-range" :actionButtons="$actions" :downloadButtons="$downloads">
@include('commons.genericdaterange', ['start_date' => strtotime('-1 months')])
<x-larastrap::hidden name="user_id" :value="$user_id" />
<x-larastrap::selectobj name="supplier_id" :label="_i('Fornitore')" :options="$currentgas->suppliers" :extraitem="_i('Nessuno')" />
</x-filler>
</div>
Expand All @@ -24,13 +38,7 @@

<div class="row">
<div class="col" id="receipts-in-range">
@include('commons.loadablelist', [
'identifier' => 'receipts-list',
'items' => $receipts,
'legend' => (object)[
'class' => App\Receipt::class
],
])
@include('commons.loadablelist', $loadable_attributes)
</div>
</div>
</div>
13 changes: 9 additions & 4 deletions code/resources/views/receipt/show.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<x-larastrap::mform :obj="$receipt" classes="receipt-editor" nosave nodelete :other_buttons="[['label' => _i('Scarica'), 'classes' => ['float-start', 'link-button'], 'attributes' => ['data-link' => route('receipts.download', $receipt->id)]]]">
<x-larastrap::mform :obj="$receipt" classes="receipt-editor" nosave nodelete :other_buttons="[['label' => _i('Scarica'), 'classes' => ['link-button'], 'attributes' => ['data-link' => route('receipts.download', $receipt->id)]]]">
<div class="row">
<div class="col-md-6">
@include('commons.staticobjfield', ['obj' => $receipt, 'name' => 'user', 'label' => _i('Utente')])
<x-larastrap::text name="number" :label="_i('Numero')" readonly disabled />
<x-larastrap::datepicker name="date" :label="_i('Data')" readonly disabled />
<x-larastrap::price name="total" :label="_i('Totale Imponibile')" readonly disabled />
<x-larastrap::price name="total_vat" :label="_i('Totale IVA')" readonly disabled />
<x-larastrap::price name="total_other" :label="_i('Altro')" readonly disabled />

@if($receipt->total_tax)
<x-larastrap::price name="total" :label="_i('Totale Imponibile')" readonly disabled />
<x-larastrap::price name="total_tax" :label="_i('Totale IVA')" readonly disabled />
<x-larastrap::price name="total_other" :label="_i('Altro')" readonly disabled />
@else
<x-larastrap::price name="total" :label="_i('Totale')" readonly disabled />
@endif
</div>
<div class="col-md-6">
<x-larastrap::field :label="_i('Prenotazioni Coinvolte')">
Expand Down
5 changes: 5 additions & 0 deletions code/resources/views/user/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@
</x-larastrap::remotetabpane>
@endif

@if($has_accounting && $user->gas->hasFeature('extra_invoicing'))
<x-larastrap::remotetabpane :id="sprintf('receipts-%s', sanitizeId($user->id))" :label="_i('Ricevute')" :button_attributes="['data-tab-url' => route('receipts.index', ['user_id' => $user->id])]" icon="bi-graph-up">
</x-larastrap::remotetabpane>
@endif

@if($has_bookings)
<x-larastrap::remotetabpane :id="sprintf('bookings-%s', sanitizeId($user->id))" :label="_i('Prenotazioni')" :button_attributes="['data-tab-url' => route('users.bookings', $user->id)]" icon="bi-list-task">
</x-larastrap::remotetabpane>
Expand Down

0 comments on commit 240c33c

Please sign in to comment.