Fixes for tests

This commit is contained in:
David Bomba 2024-06-19 15:42:06 +10:00
parent d8a753055a
commit 0bca8b6074
4 changed files with 179 additions and 22 deletions

View File

@ -0,0 +1,76 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Activity;
use Illuminate\Support\Str;
use App\Http\Requests\Request;
use Illuminate\Validation\Rule;
class StoreNoteRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return $this->checkAuthority();
}
public function rules()
{
/** @var \App\Models\User $user */
$user = auth()->user();
$rules = [
'entity' => 'required|in:invoices,quotes,credits,recurring_invoices,clients,vendors,credits,payments,projects,tasks,expenses,recurring_expenses,bank_transactions,purchase_orders',
'entity_id' => ['required','bail', Rule::exists($this->entity, 'id')->where('company_id', $user->company()->id)],
'notes' => 'required',
];
return $rules;
}
public function prepareForValidation()
{
$input = $this->all();
if(isset($input['entity_id']) && $input['entity_id'] != null) {
$input['entity_id'] = $this->decodePrimaryKey($input['entity_id']);
}
$this->replace($input);
}
public function checkAuthority(): bool
{
$this->error_message = ctrans('texts.authorization_failure');
/** @var \App\Models\User $user */
$user = auth()->user();
$entity = $this->getEntity();
return $user->isAdmin() || $user->can('view', $entity);
}
public function getEntity()
{
$class = "\\App\\Models\\".ucfirst(Str::camel(rtrim($this->entity, 's')));
return $class::withTrashed()->find(is_string($this->entity_id) ? $this->decodePrimaryKey($this->entity_id) : $this->entity_id);
}
}

View File

@ -842,7 +842,6 @@ class Company extends BaseModel
->get(); ->get();
} }
public function resolveRouteBinding($value, $field = null) public function resolveRouteBinding($value, $field = null)
{ {
return $this->where('id', $this->decodePrimaryKey($value)) return $this->where('id', $this->decodePrimaryKey($value))

View File

@ -20,22 +20,6 @@ class BankService
{ {
} }
public function matchInvoiceNumber()
{
if (strlen($this->bank_transaction->description) > 1) {
$i = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->where('number', 'LIKE', '%'.$this->bank_transaction->description.'%')
->first();
return $i ?: false;
}
return false;
}
public function processRules() public function processRules()
{ {
(new ProcessBankRules($this->bank_transaction))->run(); (new ProcessBankRules($this->bank_transaction))->run();

View File

@ -16,6 +16,7 @@ use App\Factory\ExpenseFactory;
use App\Models\BankTransaction; use App\Models\BankTransaction;
use App\Models\ExpenseCategory; use App\Models\ExpenseCategory;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService; use App\Services\AbstractService;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -70,19 +71,116 @@ class ProcessBankRules extends AbstractService
$this->credit_rules = $this->bank_transaction->company->credit_rules(); $this->credit_rules = $this->bank_transaction->company->credit_rules();
//stub for credit rules //stub for credit rules
foreach ($this->credit_rules as $rule) { foreach ($this->credit_rules as $bank_transaction_rule) {
// $this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id; $matches = 0;
if (!is_array($bank_transaction_rule['rules'])) {
continue;
}
foreach ($bank_transaction_rule['rules'] as $rule) {
$rule_count = count($bank_transaction_rule['rules']);
if ($rule['search_key'] == '$invoice.number') {
$this->invoices = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->get();
$invoiceNumber = $this->invoices->first(function ($value, $key) {
return str_contains($this->bank_transaction->description, $value->number) || str_contains(str_replace("\n", "", $this->bank_transaction->description), $value->number);
});
if($invoiceNumber)
$matches++;
}
if ($rule['search_key'] == '$invoice.amount') {
$this->invoices = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->where('amount', $rule['operator'], $this->bank_transaction->amount)
->get();
$invoiceAmounts = $this->invoices;
if($invoiceAmounts->count() > 0) {
$matches++;
}
}
if ($rule['search_key'] == '$payment.amount') {
$paymentAmounts = Payment::query()->where('company_id', $this->bank_transaction->company_id)
->whereIn('status_id', [1,4])
->where('is_deleted', 0)
->whereNull('transaction_id')
->where('amount', $rule['operator'], $this->bank_transaction->amount)
->get();
if($paymentAmounts->count() > 0) {
$matches++;
}
}
if ($rule['search_key'] == '$payment.transaction_reference') {
$ref_search = $this->bank_transaction->description;
switch ($rule['operator']) {
case 'is':
$operator = '=';
break;
case 'contains':
$ref_search = "%".$ref_search."%";
$operator = 'LIKE';
break;
default:
$operator = '=';
break;
}
$paymentReferences = Payment::query()->where('company_id', $this->bank_transaction->company_id)
->whereIn('status_id', [1,4])
->where('is_deleted', 0)
->whereNull('transaction_id')
->where('transaction_reference', $operator, $ref_search)
->get();
if($paymentReferences->count() > 0) {
$matches++;
}
}
}
} }
} }
private function matchDebit() private function matchDebit()
{ {
$this->debit_rules = $this->bank_transaction->company->debit_rules(); $this->debit_rules = $this->bank_transaction->company->debit_rules();
$this->categories = collect(Cache::get('bank_categories')); $this->categories = collect(Cache::get('bank_categories'));
foreach ($this->debit_rules as $bank_transaction_rule) { foreach ($this->debit_rules as $bank_transaction_rule) {
$matches = 0; $matches = 0;
@ -142,7 +240,7 @@ class ProcessBankRules extends AbstractService
private function coalesceExpenses($expense): string private function coalesceExpenses($expense): string
{ {
if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id) < 1) { if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id ?? '') < 2) {
return $expense; return $expense;
} }