2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-10-17 14:26:27 +02:00
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
use App\Http\ValidationRules\User\RelatedUserRule;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2018-10-17 14:26:27 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
|
|
|
class Request extends FormRequest
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
use MakesHash;
|
2022-02-15 22:50:28 +01:00
|
|
|
use RuntimeFormRequest;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-09-04 06:34:55 +02:00
|
|
|
protected $file_validation = 'sometimes|file|mimes:png,ai,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx,webp,xml,zip,csv|max:100000';
|
2018-10-17 14:26:27 +02:00
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2018-11-03 02:01:40 +01:00
|
|
|
return [];
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
public function globalRules($rules)
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
$merge_rules = [];
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
foreach ($this->all() as $key => $value) {
|
|
|
|
if (method_exists($this, $key)) {
|
|
|
|
$merge_rules = $this->{$key}($rules);
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2022-02-01 00:03:51 +01:00
|
|
|
//01-02-2022 needed for CSV Imports
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $merge_rules) {
|
2022-02-01 00:03:51 +01:00
|
|
|
return $rules;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-01 00:03:51 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return array_merge($merge_rules, $rules);
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function assigned_user_id($rules)
|
2020-11-25 15:19:52 +01:00
|
|
|
{
|
|
|
|
$rules['assigned_user_id'] = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'bail',
|
2020-11-25 15:19:52 +01:00
|
|
|
'sometimes',
|
2020-10-22 08:46:02 +02:00
|
|
|
'nullable',
|
2022-06-21 11:57:17 +02:00
|
|
|
new RelatedUserRule($this->all()),
|
|
|
|
];
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function invoice_id($rules)
|
|
|
|
{
|
2023-09-01 04:25:52 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$rules['invoice_id'] = 'bail|nullable|sometimes|exists:invoices,id,company_id,'.$user->company()->id.',client_id,'.$this['client_id'];
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function vendor_id($rules)
|
|
|
|
{
|
2023-09-01 04:25:52 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$rules['vendor_id'] = 'bail|nullable|sometimes|exists:vendors,id,company_id,'.$user->company()->id;
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function decodePrimaryKeys($input)
|
|
|
|
{
|
2022-06-23 05:12:28 +02:00
|
|
|
if (array_key_exists('group_settings_id', $input) && is_string($input['group_settings_id'])) {
|
|
|
|
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
|
|
|
|
}
|
|
|
|
|
2021-03-31 00:58:50 +02:00
|
|
|
if (array_key_exists('group_id', $input) && is_string($input['group_id'])) {
|
|
|
|
$input['group_id'] = $this->decodePrimaryKey($input['group_id']);
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:08:58 +01:00
|
|
|
if (array_key_exists('subscription_id', $input) && is_string($input['subscription_id'])) {
|
|
|
|
$input['subscription_id'] = $this->decodePrimaryKey($input['subscription_id']);
|
|
|
|
}
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
if (array_key_exists('assigned_user_id', $input) && is_string($input['assigned_user_id'])) {
|
|
|
|
$input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('user_id', $input) && is_string($input['user_id'])) {
|
|
|
|
$input['user_id'] = $this->decodePrimaryKey($input['user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('vendor_id', $input) && is_string($input['vendor_id'])) {
|
|
|
|
$input['vendor_id'] = $this->decodePrimaryKey($input['vendor_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('client_id', $input) && is_string($input['client_id'])) {
|
|
|
|
$input['client_id'] = $this->decodePrimaryKey($input['client_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('invoice_id', $input) && is_string($input['invoice_id'])) {
|
|
|
|
$input['invoice_id'] = $this->decodePrimaryKey($input['invoice_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2022-09-15 06:15:02 +02:00
|
|
|
if (array_key_exists('expense_id', $input) && is_string($input['expense_id'])) {
|
|
|
|
$input['expense_id'] = $this->decodePrimaryKey($input['expense_id']);
|
|
|
|
}
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
if (array_key_exists('design_id', $input) && is_string($input['design_id'])) {
|
|
|
|
$input['design_id'] = $this->decodePrimaryKey($input['design_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('project_id', $input) && is_string($input['project_id'])) {
|
|
|
|
$input['project_id'] = $this->decodePrimaryKey($input['project_id']);
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:03:14 +02:00
|
|
|
if (array_key_exists('company_gateway_id', $input) && is_string($input['company_gateway_id'])) {
|
|
|
|
$input['company_gateway_id'] = $this->decodePrimaryKey($input['company_gateway_id']);
|
|
|
|
}
|
|
|
|
|
2023-02-24 02:40:36 +01:00
|
|
|
if (array_key_exists('transaction_id', $input) && is_string($input['transaction_id'])) {
|
|
|
|
$input['transaction_id'] = $this->decodePrimaryKey($input['transaction_id']);
|
|
|
|
}
|
|
|
|
|
2022-11-20 01:16:38 +01:00
|
|
|
if (array_key_exists('category_id', $input) && is_string($input['category_id'])) {
|
|
|
|
$input['category_id'] = $this->decodePrimaryKey($input['category_id']);
|
|
|
|
}
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
if (isset($input['client_contacts'])) {
|
|
|
|
foreach ($input['client_contacts'] as $key => $contact) {
|
|
|
|
if (! array_key_exists('send_email', $contact) || ! array_key_exists('id', $contact)) {
|
|
|
|
unset($input['client_contacts'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 06:43:10 +02:00
|
|
|
if (isset($input['invitations']) && is_array($input['invitations'])) {
|
2020-10-22 08:46:02 +02:00
|
|
|
foreach ($input['invitations'] as $key => $value) {
|
|
|
|
if (isset($input['invitations'][$key]['id']) && is_numeric($input['invitations'][$key]['id'])) {
|
|
|
|
unset($input['invitations'][$key]['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($input['invitations'][$key]['id']) && is_string($input['invitations'][$key]['id'])) {
|
|
|
|
$input['invitations'][$key]['id'] = $this->decodePrimaryKey($input['invitations'][$key]['id']);
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:59:25 +02:00
|
|
|
if (array_key_exists('client_contact_id', $input['invitations'][$key]) && is_string($input['invitations'][$key]['client_contact_id'])) {
|
2020-10-22 08:46:02 +02:00
|
|
|
$input['invitations'][$key]['client_contact_id'] = $this->decodePrimaryKey($input['invitations'][$key]['client_contact_id']);
|
|
|
|
}
|
2022-09-01 11:59:25 +02:00
|
|
|
|
|
|
|
if (array_key_exists('vendor_contact_id', $input['invitations'][$key]) && is_string($input['invitations'][$key]['vendor_contact_id'])) {
|
|
|
|
$input['invitations'][$key]['vendor_contact_id'] = $this->decodePrimaryKey($input['invitations'][$key]['vendor_contact_id']);
|
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 06:35:25 +02:00
|
|
|
if (isset($input['contacts']) && is_array($input['contacts'])) {
|
2020-10-22 08:46:02 +02:00
|
|
|
foreach ($input['contacts'] as $key => $contact) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! is_array($contact)) {
|
2021-09-13 00:42:15 +02:00
|
|
|
continue;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-13 00:42:15 +02:00
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
if (array_key_exists('id', $contact) && is_numeric($contact['id'])) {
|
|
|
|
unset($input['contacts'][$key]['id']);
|
|
|
|
} elseif (array_key_exists('id', $contact) && is_string($contact['id'])) {
|
|
|
|
$input['contacts'][$key]['id'] = $this->decodePrimaryKey($contact['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Filter the client contact password - if it is sent with ***** we should ignore it!
|
|
|
|
if (isset($contact['password'])) {
|
|
|
|
if (strlen($contact['password']) == 0) {
|
|
|
|
$input['contacts'][$key]['password'] = '';
|
|
|
|
} else {
|
|
|
|
$contact['password'] = str_replace('*', '', $contact['password']);
|
|
|
|
|
|
|
|
if (strlen($contact['password']) == 0) {
|
|
|
|
unset($input['contacts'][$key]['password']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 00:42:15 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('email', $contact)) {
|
2022-02-15 22:50:28 +01:00
|
|
|
$input['contacts'][$key]['email'] = trim($contact['email']);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $input;
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
2022-02-15 22:50:28 +01:00
|
|
|
|
2022-06-24 03:55:41 +02:00
|
|
|
public function prepareForValidation()
|
2022-02-15 22:50:28 +01:00
|
|
|
{
|
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
2023-08-20 06:36:22 +02:00
|
|
|
/**
|
|
|
|
* Convert to boolean
|
|
|
|
*
|
|
|
|
* @param $bool
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function toBoolean($bool): bool
|
|
|
|
{
|
|
|
|
return filter_var($bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-02-08 00:59:36 +01:00
|
|
|
public function checkTimeLog(array $log): bool
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (count($log) == 0) {
|
2023-02-08 00:59:36 +01:00
|
|
|
return true;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
/*Get first value of all arrays*/
|
|
|
|
$result = array_column($log, 0);
|
|
|
|
|
|
|
|
/*Sort the array in ascending order*/
|
|
|
|
asort($result);
|
|
|
|
|
|
|
|
$new_array = [];
|
|
|
|
|
|
|
|
/*Rebuild the array in order*/
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($result as $key => $value) {
|
2023-02-08 00:59:36 +01:00
|
|
|
$new_array[] = $log[$key];
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
/*Iterate through the array and perform checks*/
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($new_array as $key => $array) {
|
2023-02-08 00:59:36 +01:00
|
|
|
/*Flag which helps us know if there is a NEXT timelog*/
|
|
|
|
$next = false;
|
|
|
|
/* If there are more than 1 time log in the array, ensure the last timestamp is not zero*/
|
2023-02-16 02:36:09 +01:00
|
|
|
if (count($new_array) >1 && $array[1] == 0) {
|
2023-02-08 00:59:36 +01:00
|
|
|
return false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
/* Check if the start time is greater than the end time */
|
|
|
|
/* Ignore the last value for now, we'll do a separate check for this */
|
|
|
|
if ($array[0] > $array[1] && $array[1] != 0) {
|
2023-02-08 00:59:36 +01:00
|
|
|
return false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
/* Find the next time log value - if it exists */
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists($key+1, $new_array)) {
|
2023-02-08 00:59:36 +01:00
|
|
|
$next = $new_array[$key+1];
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
/* check the next time log and ensure the start time is GREATER than the end time of the previous record */
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($next && $next[0] < $array[1]) {
|
2023-02-08 00:59:36 +01:00
|
|
|
return false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
/* Get the last row of the timelog*/
|
|
|
|
$last_row = end($new_array);
|
|
|
|
|
|
|
|
/*If the last value is NOT zero, ensure start time is not GREATER than the endtime */
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($last_row[1] != 0 && $last_row[0] > $last_row[1]) {
|
2023-02-08 00:59:36 +01:00
|
|
|
return false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-08 00:59:36 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|