diff --git a/app/Http/Requests/Activity/StoreNoteRequest.php b/app/Http/Requests/Activity/StoreNoteRequest.php new file mode 100644 index 000000000000..88e90a45f306 --- /dev/null +++ b/app/Http/Requests/Activity/StoreNoteRequest.php @@ -0,0 +1,76 @@ +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); + + } + +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 8a0ef899de12..d1e402ece50b 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -842,7 +842,6 @@ class Company extends BaseModel ->get(); } - public function resolveRouteBinding($value, $field = null) { return $this->where('id', $this->decodePrimaryKey($value)) diff --git a/app/Services/Bank/BankService.php b/app/Services/Bank/BankService.php index f17bf8dba34c..8460a6249a20 100644 --- a/app/Services/Bank/BankService.php +++ b/app/Services/Bank/BankService.php @@ -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() { (new ProcessBankRules($this->bank_transaction))->run(); diff --git a/app/Services/Bank/ProcessBankRules.php b/app/Services/Bank/ProcessBankRules.php index e65a4462bcad..d0f207f9f4c5 100644 --- a/app/Services/Bank/ProcessBankRules.php +++ b/app/Services/Bank/ProcessBankRules.php @@ -16,6 +16,7 @@ use App\Factory\ExpenseFactory; use App\Models\BankTransaction; use App\Models\ExpenseCategory; use App\Models\Invoice; +use App\Models\Payment; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; use Illuminate\Support\Carbon; @@ -70,19 +71,116 @@ class ProcessBankRules extends AbstractService $this->credit_rules = $this->bank_transaction->company->credit_rules(); //stub for credit rules - foreach ($this->credit_rules as $rule) { - // $this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id; + foreach ($this->credit_rules as $bank_transaction_rule) { + $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() { $this->debit_rules = $this->bank_transaction->company->debit_rules(); $this->categories = collect(Cache::get('bank_categories')); - - foreach ($this->debit_rules as $bank_transaction_rule) { $matches = 0; @@ -142,7 +240,7 @@ class ProcessBankRules extends AbstractService 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; }