1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Http/Requests/Report/ProfitLossRequest.php

78 lines
2.0 KiB
PHP
Raw Normal View History

2022-05-13 10:53:38 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-05-13 10:53:38 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Report;
2024-03-25 05:19:46 +01:00
use App\Utils\Ninja;
2022-05-13 10:53:38 +02:00
use App\Http\Requests\Request;
2024-03-25 05:17:16 +01:00
use Illuminate\Auth\Access\AuthorizationException;
2022-05-13 10:53:38 +02:00
class ProfitLossRequest extends Request
{
2024-03-25 05:17:16 +01:00
private string $error_message = '';
2022-05-13 10:53:38 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
2024-01-14 05:05:00 +01:00
public function authorize(): bool
2022-05-13 10:53:38 +02:00
{
2024-03-25 05:17:16 +01:00
return $this->checkAuthority();
2022-05-13 10:53:38 +02:00
}
public function rules()
{
return [
2022-12-14 06:41:05 +01:00
'start_date' => 'bail|nullable|required_if:date_range,custom|string|date',
'end_date' => 'bail|nullable|required_if:date_range,custom|string|date',
2022-05-13 10:53:38 +02:00
'is_income_billed' => 'required|bail|bool',
2022-09-23 08:54:22 +02:00
'is_expense_billed' => 'bool',
2022-05-13 10:53:38 +02:00
'include_tax' => 'required|bail|bool',
2022-05-26 08:58:14 +02:00
'date_range' => 'sometimes|string',
'send_email' => 'bool',
2022-05-13 10:53:38 +02:00
];
}
2022-05-26 08:58:14 +02:00
public function prepareForValidation()
{
$input = $this->all();
2023-02-07 10:10:47 +01:00
if (! array_key_exists('date_range', $input) || $input['date_range'] == '') {
2022-05-26 08:58:14 +02:00
$input['date_range'] = 'all';
}
2022-05-26 08:58:14 +02:00
$this->replace($input);
}
2024-03-25 05:17:16 +01:00
2024-06-14 09:09:44 +02:00
private function checkAuthority()
2024-03-25 05:17:16 +01:00
{
$this->error_message = ctrans('texts.authorization_failure');
/** @var \App\Models\User $user */
$user = auth()->user();
2024-06-14 09:09:44 +02:00
if(Ninja::isHosted() && $user->account->isFreeHostedClient()) {
2024-03-25 05:17:16 +01:00
$this->error_message = ctrans('texts.upgrade_to_view_reports');
return false;
}
return $user->isAdmin() || $user->hasPermission('view_reports');
}
protected function failedAuthorization()
{
throw new AuthorizationException($this->error_message);
}
2022-05-13 10:53:38 +02:00
}