Skip to content

Commit

Permalink
unit tests permessi accesso fatture
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Dec 3, 2023
1 parent 37bdac0 commit 9389d52
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 137 deletions.
34 changes: 16 additions & 18 deletions code/app/AggregateBooking.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,25 @@ public function getUpdatedAtAttribute()

public function getStatusAttribute()
{
foreach ($this->bookings as $booking) {
/*
Nota bene: in questo aggregato ci vanno sia le prenotazioni
effettivamente salvate sul database che le prenotazioni allocate
ma non realmente esistenti (ma che fungono da wrapper in molte
circostanze).
Lo stato dell'aggregato dipende solo da quelle reali: se una
prenotazioni vera risulta consegnata, ed una "virtuale" no
(quelle virtuali non lo sono mai, per definizione), comunque
tutto l'aggregato deve risultare consegnato
*/
if ($booking->exists && $booking->status != 'shipped') {
return $booking->status;
}
/*
Nota bene: in questo aggregato ci vanno sia le prenotazioni
effettivamente salvate sul database che le prenotazioni allocate
ma non realmente esistenti (ma che fungono da wrapper in molte
circostanze).
Lo stato dell'aggregato dipende solo da quelle reali: se una
prenotazioni vera risulta consegnata, ed una "virtuale" no
(quelle virtuali non lo sono mai, per definizione), comunque tutto
l'aggregato deve risultare consegnato
*/
$target = $this->bookings->filter(fn($b) => $b->exists && $b->status != 'shipped')->first();
if ($target) {
return $target->status;
}

foreach ($this->bookings as $booking) {
foreach($booking->friends_bookings as $fbooking) {
if ($fbooking->exists && $fbooking->status != 'shipped') {
return $fbooking->status;
}
$target = $booking->friends_bookings->filter(fn($b) => $b->exists && $b->status != 'shipped')->first();
if ($target) {
return $target->status;
}
}

Expand Down
20 changes: 16 additions & 4 deletions code/app/Helpers/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,22 @@ public static function orders()
public static function invoices()
{
return [
'pending' => _i('In Attesa'),
'to_verify' => _i('Da Verificare'),
'verified' => _i('Verificata'),
'payed' => _i('Pagata'),
'pending' => (object) [
'label' => _i('In Attesa'),
'icon' => 'clock',
],
'to_verify' => (object) [
'label' => _i('Da Verificare'),
'icon' => 'pin-angle',
],
'verified' => (object) [
'label' => _i('Verificata'),
'icon' => 'search',
],
'payed' => (object) [
'label' => _i('Pagata'),
'icon' => 'check',
],
];
}
}
39 changes: 19 additions & 20 deletions code/app/Http/Controllers/ReceiptsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

use Illuminate\Http\Request;

use Auth;
use DB;
use PDF;
use Log;
use Mail;

use App\Services\ReceiptsService;
use App\Notifications\ReceiptForward;
Expand All @@ -21,7 +17,7 @@ public function __construct(ReceiptsService $service)
$this->middleware('auth');

$this->commonInit([
'reference_class' => 'App\\Receipt',
'reference_class' => Receipt::class,
'service' => $service,
]);
}
Expand All @@ -36,11 +32,10 @@ public function index(Request $request)
]);
}

public function show($id)
public function show(Request $request, $id)
{
$receipt = $this->service->show($id);

$user = Auth::user();
$user = $request->user();

if ($user->can('movements.admin', $user->gas)) {
return view('receipt.edit', ['receipt' => $receipt]);
Expand Down Expand Up @@ -123,6 +118,20 @@ 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());
}
}
}
}

public function search(Request $request)
{
$start = decodeDate($request->input('startdate'));
Expand All @@ -134,18 +143,8 @@ public function search(Request $request)

switch($format) {
case 'send':
foreach($elements as $receipt) {
if ($receipt->mailed == false) {
try {
$this->sendByMail($receipt);
}
catch(\Exception $e) {
\Log::error('Errore in inoltro ricevuta: ' . $e->getMessage());
}
}
}

$elements = $this->service->list($start, $end, $supplier_id);
$this->send($elements);
$elements = $this->service->list($start, $end, $supplier_id);

/*
Qui il break manca di proposito
Expand Down
2 changes: 1 addition & 1 deletion code/app/Models/Concerns/RoleableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function targetsByAction($actions, $exclude_trashed = true)
$action = trim($action);
$class = classByRule($action);

$roles = $this->roles()->get()->filter(function($role) use ($action) {
$roles = $this->roles->filter(function($role) use ($action) {
return $role->enabledAction($action);
});

Expand Down
34 changes: 18 additions & 16 deletions code/app/Services/InvoicesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,24 @@ private function testAccess($supplier)

public function list($start, $end, $supplier_id)
{
if ($supplier_id) {
$supplier = Supplier::tFind($supplier_id);
}
else {
$supplier = null;
}

$supplier = Supplier::withTrashed()->find($supplier_id);
$user = $this->testAccess($supplier);

$query = Invoice::where(function($query) use($start, $end) {
$query->whereHas('payment', function($query) use($start, $end) {
$query->where('date', '>=', $start)->where('date', '<=', $end);
})->orWhereDoesntHave('payment');
});
$query = Invoice::whereBetween('date', [$start, $end]);

if ($supplier) {
$query->where('supplier_id', $supplier->id);
}
else {
$suppliers = $user->targetsByAction('movements.admin,supplier.orders,supplier.movements');
$query->whereIn('supplier_id', array_keys($suppliers));
if ($user->can('movements.admin', $user->gas)) {
$suppliers = Supplier::withTrashed()->pluck('id');
}
else {
$suppliers = $user->targetsByAction('supplier.invoices,supplier.movements', false);
$suppliers = array_keys($suppliers);
}

$query->whereIn('supplier_id', $suppliers);
}

$elements = $query->get();
Expand Down Expand Up @@ -107,7 +104,7 @@ public function update($id, array $request)

private function initGlobalSummeries($invoice)
{
$global_summary = (object)[
$global_summary = (object) [
'products' => [],
'total' => 0,
'total_taxable' => 0,
Expand Down Expand Up @@ -205,7 +202,7 @@ private function movementAttach($type, $user, $invoice)
public function saveMovements($id, $request)
{
$invoice = $this->show($id);
$this->ensureAuth(['movements.admin' => 'gas', 'supplier.movements' => $invoice->supplier]);
$user = $this->ensureAuth(['movements.admin' => 'gas', 'supplier.movements' => $invoice->supplier]);

$invoice->deleteMovements();

Expand Down Expand Up @@ -239,6 +236,11 @@ public function saveMovements($id, $request)
}
}

/*
Il parametro $invoice->payment_id viene settato direttamente nella
callback di elaborazione del tipo movimento InvoicePayment
*/

$invoice->otherMovements()->sync($other_movements);
}

Expand Down
8 changes: 2 additions & 6 deletions code/app/Services/UsersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ public function list($term = '', $all = false)

public function show($id)
{
$user = Auth::user();
if (is_null($user)) {
throw new AuthException(401);
}

$searched = User::withTrashed()->findOrFail($id);

if ($searched->testUserAccess() == false)
if ($searched->testUserAccess() == false) {
$this->ensureAuth(['users.admin' => 'gas', 'users.view' => 'gas']);
}

return $searched;
}
Expand Down
15 changes: 15 additions & 0 deletions code/app/View/Icons/IconsMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ public static function selective()
return [];
}

protected static function unrollStatuses($array, $statuses)
{
foreach($statuses as $identifier => $meta) {
$array[$meta->icon] = (object) [
'test' => function ($obj) use ($identifier) {
return $obj->status == $identifier;
},
'text' => $meta->label,
'group' => 'status',
];
}

return $array;
}

/*
Questa funzione deve ritornare un array associativo che contiene la
definizione di ogni icona prevista per la classe in oggetto. Le chiavi
Expand Down
31 changes: 3 additions & 28 deletions code/app/View/Icons/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,12 @@

namespace App\View\Icons;

use App\Helpers\Status;

class Invoice extends IconsMap
{
public static function commons($user)
{
$ret = [
'clock' => (object) [
'test' => function ($obj) {
return $obj->status == 'pending';
},
'text' => _i('In Attesa'),
],
'pin-angle' => (object) [
'test' => function ($obj) {
return $obj->status == 'to_verify';
},
'text' => _i('Da Verificare'),
],
'search' => (object) [
'test' => function ($obj) {
return $obj->status == 'verified';
},
'text' => _i('Verificata'),
],
'check' => (object) [
'test' => function ($obj) {
return $obj->status == 'payed';
},
'text' => _i('Pagata'),
],
];

return $ret;
return self::unrollStatuses([], Status::invoices());
}
}
11 changes: 1 addition & 10 deletions code/app/View/Icons/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ public static function commons($user)
]
];

foreach(Status::orders() as $identifier => $meta) {
$ret[$meta->icon] = (object) [
'test' => function ($obj) use ($identifier) {
return $obj->status == $identifier;
},
'text' => $meta->label,
'group' => 'status',
];
}

$ret = self::unrollStatuses($ret, Status::orders());
return $ret;
}
}
2 changes: 1 addition & 1 deletion code/database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function definition()
{
return [
'name' => $this->faker->text(10),
'price' => $this->faker->randomNumber(2),
'price' => $this->faker->randomNumber(2, true),
];
}
}
Loading

0 comments on commit 9389d52

Please sign in to comment.