2023-01-13 02:43:38 +01: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)
|
2023-01-13 02:43:38 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\TaskScheduler;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2023-12-16 04:23:07 +01:00
|
|
|
use App\Http\ValidationRules\Scheduler\ValidClientIds;
|
2023-01-13 02:43:38 +01:00
|
|
|
|
|
|
|
class StoreSchedulerRequest extends Request
|
|
|
|
{
|
2024-03-08 00:05:48 +01:00
|
|
|
public array $client_statuses = [
|
|
|
|
'all',
|
|
|
|
'draft',
|
|
|
|
'paid',
|
|
|
|
'unpaid',
|
|
|
|
'overdue',
|
|
|
|
'pending',
|
|
|
|
'invoiced',
|
|
|
|
'logged',
|
|
|
|
'partial',
|
|
|
|
'applied',
|
2024-06-14 09:09:44 +02:00
|
|
|
'active',
|
2024-03-08 00:05:48 +01:00
|
|
|
'paused',
|
|
|
|
'completed',
|
|
|
|
'approved',
|
|
|
|
'expired',
|
|
|
|
'upcoming',
|
|
|
|
'converted',
|
|
|
|
'uninvoiced',
|
|
|
|
];
|
|
|
|
|
2023-01-13 02:43:38 +01:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2023-12-16 21:40:45 +01:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
2023-11-03 05:14:34 +01:00
|
|
|
|
2023-12-16 21:40:45 +01:00
|
|
|
return $user->isAdmin();
|
2023-01-13 02:43:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
$rules = [
|
2023-03-21 21:30:37 +01:00
|
|
|
'name' => 'bail|sometimes|nullable|string',
|
2023-01-13 02:43:38 +01:00
|
|
|
'is_paused' => 'bail|sometimes|boolean',
|
2023-02-17 11:05:01 +01:00
|
|
|
'frequency_id' => 'bail|sometimes|integer|digits_between:1,12',
|
2023-01-17 09:42:34 +01:00
|
|
|
'next_run' => 'bail|required|date:Y-m-d|after_or_equal:today',
|
|
|
|
'next_run_client' => 'bail|sometimes|date:Y-m-d',
|
2023-01-13 02:43:38 +01:00
|
|
|
'template' => 'bail|required|string',
|
|
|
|
'parameters' => 'bail|array',
|
2023-01-17 12:40:40 +01:00
|
|
|
'parameters.clients' => ['bail','sometimes', 'array', new ValidClientIds()],
|
2023-12-10 23:28:39 +01:00
|
|
|
'parameters.date_range' => 'bail|sometimes|string|in:last7_days,last30_days,last365_days,this_month,last_month,this_quarter,last_quarter,this_year,last_year,all_time,custom,all',
|
2023-02-20 06:47:30 +01:00
|
|
|
'parameters.start_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom'],
|
|
|
|
'parameters.end_date' => ['bail', 'sometimes', 'date:Y-m-d', 'required_if:parameters.date_rate,custom', 'after_or_equal:parameters.start_date'],
|
2023-03-18 09:06:32 +01:00
|
|
|
'parameters.entity' => ['bail', 'sometimes', 'string', 'in:invoice,credit,quote,purchase_order'],
|
|
|
|
'parameters.entity_id' => ['bail', 'sometimes', 'string'],
|
2024-03-10 00:13:00 +01:00
|
|
|
'parameters.report_name' => ['bail','sometimes', 'string', 'required_if:template,email_report','in:vendor,purchase_order_item,purchase_order,ar_detailed,ar_summary,client_balance,tax_summary,profitloss,client_sales,user_sales,product_sales,activity,activities,client,clients,client_contact,client_contacts,credit,credits,document,documents,expense,expenses,invoice,invoices,invoice_item,invoice_items,quote,quotes,quote_item,quote_items,recurring_invoice,recurring_invoices,payment,payments,product,products,task,tasks'],
|
2023-04-14 08:44:04 +02:00
|
|
|
'parameters.date_key' => ['bail','sometimes', 'string'],
|
2024-02-04 01:15:53 +01:00
|
|
|
'parameters.status' => ['bail','sometimes', 'nullable', 'string'],
|
2023-01-13 02:43:38 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
2023-01-17 09:42:34 +01:00
|
|
|
|
|
|
|
public function prepareForValidation()
|
|
|
|
{
|
|
|
|
$input = $this->all();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('next_run', $input) && is_string($input['next_run'])) {
|
2023-06-25 04:38:15 +02:00
|
|
|
$input['next_run_client'] = $input['next_run'];
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($input['template'] == 'email_record') {
|
2023-06-06 10:19:47 +02:00
|
|
|
$input['frequency_id'] = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-11 12:56:24 +01:00
|
|
|
if(isset($input['parameters']) && !isset($input['parameters']['clients'])) {
|
2023-12-11 03:14:05 +01:00
|
|
|
$input['parameters']['clients'] = [];
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-12-12 00:53:21 +01:00
|
|
|
if(isset($input['parameters']['status'])) {
|
|
|
|
|
2024-03-08 00:05:48 +01:00
|
|
|
$task_statuses = [];
|
|
|
|
|
2024-03-08 03:13:19 +01:00
|
|
|
if(isset($input['parameters']['report_name']) && $input['parameters']['report_name'] == 'task') {
|
2024-03-08 00:05:48 +01:00
|
|
|
$task_statuses = array_diff(explode(",", $input['parameters']['status']), $this->client_statuses);
|
|
|
|
}
|
|
|
|
|
2023-12-12 01:36:55 +01:00
|
|
|
$input['parameters']['status'] = collect(explode(",", $input['parameters']['status']))
|
2023-12-16 21:40:45 +01:00
|
|
|
->filter(function ($status) {
|
2024-03-08 00:05:48 +01:00
|
|
|
return in_array($status, $this->client_statuses);
|
|
|
|
})->merge($task_statuses)
|
|
|
|
->implode(",") ?? '';
|
|
|
|
|
2023-12-12 00:53:21 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 10:19:47 +02:00
|
|
|
$this->replace($input);
|
2023-01-17 09:42:34 +01:00
|
|
|
}
|
2023-01-13 02:43:38 +01:00
|
|
|
}
|