2022-09-15 05:58:42 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2022-09-15 05:58:42 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\BankTransaction;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
2023-02-28 12:07:58 +01:00
|
|
|
use App\Models\BankTransaction;
|
2022-11-30 08:39:22 +01:00
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Payment;
|
2022-09-15 05:58:42 +02:00
|
|
|
|
|
|
|
class MatchBankTransactionRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize() : bool
|
|
|
|
{
|
2023-02-28 12:07:58 +01:00
|
|
|
return auth()->user()->isAdmin() || auth()->user()->can('create', BankTransaction::class || auth()->user()->hasPermission('edit_bank_transaction'));
|
2022-09-15 05:58:42 +02:00
|
|
|
}
|
2022-09-15 06:15:02 +02:00
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
2022-09-22 07:54:58 +02:00
|
|
|
$rules = [
|
2022-10-24 10:50:45 +02:00
|
|
|
'transactions' => 'bail|array',
|
|
|
|
'transactions.*.invoice_ids' => 'nullable|string|sometimes',
|
2022-09-15 06:15:02 +02:00
|
|
|
];
|
|
|
|
|
2022-11-03 11:56:04 +01:00
|
|
|
$rules['transactions.*.ninja_category_id'] = 'bail|nullable|sometimes|exists:expense_categories,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
2022-11-30 08:39:22 +01:00
|
|
|
$rules['transactions.*.vendor_id'] = 'bail|nullable|sometimes|exists:vendors,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
2022-11-17 00:15:22 +01:00
|
|
|
$rules['transactions.*.id'] = 'bail|required|exists:bank_transactions,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
2022-11-30 08:39:22 +01:00
|
|
|
$rules['transactions.*.payment_id'] = 'bail|sometimes|nullable|exists:payments,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
|
|
|
$rules['transactions.*.expense_id'] = 'bail|sometimes|nullable|exists:expenses,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
2022-09-22 07:54:58 +02:00
|
|
|
|
|
|
|
return $rules;
|
2022-09-15 06:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareForValidation()
|
|
|
|
{
|
2022-10-24 10:11:37 +02:00
|
|
|
$inputs = $this->all();
|
2022-10-24 10:27:40 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($inputs['transactions'] as $key => $input) {
|
|
|
|
if (array_key_exists('id', $inputs['transactions'][$key])) {
|
2022-10-24 11:10:01 +02:00
|
|
|
$inputs['transactions'][$key]['id'] = $this->decodePrimaryKey($input['id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-09-22 08:20:54 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('ninja_category_id', $inputs['transactions'][$key]) && strlen($inputs['transactions'][$key]['ninja_category_id']) >= 1) {
|
2022-10-24 11:10:01 +02:00
|
|
|
$inputs['transactions'][$key]['ninja_category_id'] = $this->decodePrimaryKey($inputs['transactions'][$key]['ninja_category_id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-09-22 08:20:54 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('vendor_id', $inputs['transactions'][$key]) && strlen($inputs['transactions'][$key]['vendor_id']) >= 1) {
|
2022-10-24 12:20:29 +02:00
|
|
|
$inputs['transactions'][$key]['vendor_id'] = $this->decodePrimaryKey($inputs['transactions'][$key]['vendor_id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-10-24 12:20:29 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('payment_id', $inputs['transactions'][$key]) && strlen($inputs['transactions'][$key]['payment_id']) >= 1) {
|
2022-11-30 08:39:22 +01:00
|
|
|
$inputs['transactions'][$key]['payment_id'] = $this->decodePrimaryKey($inputs['transactions'][$key]['payment_id']);
|
2022-12-16 00:40:03 +01:00
|
|
|
$p = Payment::withTrashed()->where('company_id', auth()->user()->company()->id)->where('id', $inputs['transactions'][$key]['payment_id'])->first();
|
2022-11-30 08:39:22 +01:00
|
|
|
|
|
|
|
/*Ensure we don't relink an existing payment*/
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$p || is_numeric($p->transaction_id)) {
|
2022-12-15 23:38:02 +01:00
|
|
|
unset($inputs['transactions'][$key]);
|
|
|
|
}
|
2022-11-30 08:39:22 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('expense_id', $inputs['transactions'][$key]) && strlen($inputs['transactions'][$key]['expense_id']) >= 1) {
|
2022-11-30 08:39:22 +01:00
|
|
|
$inputs['transactions'][$key]['expense_id'] = $this->decodePrimaryKey($inputs['transactions'][$key]['expense_id']);
|
2022-12-16 00:40:03 +01:00
|
|
|
$e = Expense::withTrashed()->where('company_id', auth()->user()->company()->id)->where('id', $inputs['transactions'][$key]['expense_id'])->first();
|
2022-11-30 08:39:22 +01:00
|
|
|
|
|
|
|
/*Ensure we don't relink an existing expense*/
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$e || is_numeric($e->transaction_id)) {
|
2022-12-16 00:36:53 +01:00
|
|
|
unset($inputs['transactions'][$key]['expense_id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-30 08:39:22 +01:00
|
|
|
}
|
2022-10-24 10:11:37 +02:00
|
|
|
}
|
2022-10-24 10:18:50 +02:00
|
|
|
|
2022-10-24 10:11:37 +02:00
|
|
|
$this->replace($inputs);
|
2022-09-15 06:15:02 +02:00
|
|
|
}
|
2022-09-15 05:58:42 +02:00
|
|
|
}
|