1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Bank/ProcessBankRules.php

200 lines
7.6 KiB
PHP
Raw Normal View History

2022-11-20 03:55:19 +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)
2022-11-20 03:55:19 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Bank;
use App\Factory\ExpenseCategoryFactory;
use App\Factory\ExpenseFactory;
use App\Models\BankTransaction;
use App\Models\ExpenseCategory;
use App\Models\Invoice;
use App\Services\AbstractService;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
class ProcessBankRules extends AbstractService
{
use GeneratesCounter;
protected $credit_rules;
protected $debit_rules;
protected $categories;
2023-05-25 08:24:27 +02:00
protected $invoices;
2024-01-14 05:05:00 +01:00
2023-08-08 11:44:52 +02:00
/**
* @param \App\Models\BankTransaction $bank_transaction
*/
2023-02-16 02:36:09 +01:00
public function __construct(public BankTransaction $bank_transaction)
{
}
2022-11-20 03:55:19 +01:00
public function run()
{
2023-02-16 02:36:09 +01:00
if ($this->bank_transaction->base_type == 'DEBIT') {
2022-11-20 03:55:19 +01:00
$this->matchDebit();
2023-02-16 02:36:09 +01:00
} else {
2022-11-20 03:55:19 +01:00
$this->matchCredit();
2023-02-16 02:36:09 +01:00
}
2022-11-20 03:55:19 +01:00
}
private function matchCredit()
{
2023-08-06 09:35:19 +02:00
$this->invoices = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
2022-11-20 03:55:19 +01:00
->whereIn('status_id', [1,2,3])
->where('is_deleted', 0)
->get();
2023-02-16 02:36:09 +01:00
$invoice = $this->invoices->first(function ($value, $key) {
2024-05-23 01:44:33 +02:00
return str_contains($this->bank_transaction->description, $value->number) || str_contains(str_replace("\n", "", $this->bank_transaction->description), $value->number);
2022-11-20 03:55:19 +01:00
});
2023-02-16 02:36:09 +01:00
if ($invoice) {
2022-11-20 03:55:19 +01:00
$this->bank_transaction->invoice_ids = $invoice->hashed_id;
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
2023-02-16 02:36:09 +01:00
$this->bank_transaction->save();
2022-11-20 03:55:19 +01:00
return;
}
2022-12-09 03:47:54 +01:00
$this->credit_rules = $this->bank_transaction->company->credit_rules();
2022-11-20 03:55:19 +01:00
//stub for credit rules
2023-02-16 02:36:09 +01:00
foreach ($this->credit_rules as $rule) {
2022-11-24 21:07:12 +01:00
// $this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
2022-11-20 03:55:19 +01:00
}
}
private function matchDebit()
{
$this->debit_rules = $this->bank_transaction->company->debit_rules();
$this->categories = collect(Cache::get('bank_categories'));
2022-12-09 03:47:54 +01:00
2022-12-09 02:35:37 +01:00
2023-02-16 02:36:09 +01:00
foreach ($this->debit_rules as $bank_transaction_rule) {
2022-11-20 03:55:19 +01:00
$matches = 0;
2023-02-16 02:36:09 +01:00
if (!is_array($bank_transaction_rule['rules'])) {
2022-12-09 03:47:54 +01:00
continue;
2023-02-16 02:36:09 +01:00
}
2022-12-09 03:47:54 +01:00
2023-02-16 02:36:09 +01:00
foreach ($bank_transaction_rule['rules'] as $rule) {
2022-11-20 03:55:19 +01:00
$rule_count = count($bank_transaction_rule['rules']);
2023-02-16 02:36:09 +01:00
if ($rule['search_key'] == 'description') {
if ($this->matchStringOperator($this->bank_transaction->description, $rule['value'], $rule['operator'])) {
2022-11-20 03:55:19 +01:00
$matches++;
}
}
2023-02-16 02:36:09 +01:00
if ($rule['search_key'] == 'amount') {
if ($this->matchNumberOperator($this->bank_transaction->amount, $rule['value'], $rule['operator'])) {
2022-11-20 03:55:19 +01:00
$matches++;
}
}
2023-02-16 02:36:09 +01:00
if (($bank_transaction_rule['matches_on_all'] && ($matches == $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && $matches > 0)) {
2022-11-20 03:55:19 +01:00
// $this->bank_transaction->client_id = empty($rule['client_id']) ? null : $rule['client_id'];
2022-11-23 12:14:36 +01:00
$this->bank_transaction->vendor_id = $bank_transaction_rule->vendor_id;
$this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
2022-11-20 03:55:19 +01:00
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
2022-11-24 21:07:12 +01:00
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
2022-11-20 03:55:19 +01:00
$this->bank_transaction->save();
2023-02-16 02:36:09 +01:00
if ($bank_transaction_rule['auto_convert']) {
2022-11-20 03:55:19 +01:00
$expense = ExpenseFactory::create($this->bank_transaction->company_id, $this->bank_transaction->user_id);
2022-11-23 12:14:36 +01:00
$expense->category_id = $bank_transaction_rule->category_id ?: $this->resolveCategory();
2022-11-20 03:55:19 +01:00
$expense->amount = $this->bank_transaction->amount;
$expense->number = $this->getNextExpenseNumber($expense);
$expense->currency_id = $this->bank_transaction->currency_id;
$expense->date = Carbon::parse($this->bank_transaction->date);
$expense->payment_date = Carbon::parse($this->bank_transaction->date);
$expense->transaction_reference = $this->bank_transaction->description;
$expense->transaction_id = $this->bank_transaction->id;
2022-11-23 12:14:36 +01:00
$expense->vendor_id = $bank_transaction_rule->vendor_id;
2022-11-20 03:55:19 +01:00
$expense->invoice_documents = $this->bank_transaction->company->invoice_expense_documents;
$expense->should_be_invoiced = $this->bank_transaction->company->mark_expenses_invoiceable;
$expense->save();
2023-04-06 03:38:29 +02:00
$this->bank_transaction->expense_id = $this->coalesceExpenses($expense->hashed_id);
2022-11-20 03:55:19 +01:00
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
$this->bank_transaction->save();
break;
}
}
}
}
}
2023-10-26 04:57:44 +02:00
private function coalesceExpenses($expense): string
2023-04-06 03:38:29 +02:00
{
if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id) < 1) {
return $expense;
}
return collect(explode(",", $this->bank_transaction->expense_id))->push($expense)->implode(",");
}
2022-11-20 03:55:19 +01:00
private function resolveCategory()
{
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bank_transaction->category_id);
2023-08-06 09:35:19 +02:00
$ec = ExpenseCategory::query()->where('company_id', $this->bank_transaction->company_id)->where('bank_category_id', $this->bank_transaction->category_id)->first();
2022-11-20 03:55:19 +01:00
2023-02-16 02:36:09 +01:00
if ($ec) {
2022-11-20 03:55:19 +01:00
return $ec->id;
2023-02-16 02:36:09 +01:00
}
2022-11-20 03:55:19 +01:00
2023-02-16 02:36:09 +01:00
if ($category) {
2022-11-20 03:55:19 +01:00
$ec = ExpenseCategoryFactory::create($this->bank_transaction->company_id, $this->bank_transaction->user_id);
$ec->bank_category_id = $this->bank_transaction->category_id;
$ec->name = $category->highLevelCategoryName;
$ec->save();
return $ec->id;
}
}
2024-01-14 05:05:00 +01:00
private function matchNumberOperator($bt_value, $rule_value, $operator): bool
2022-11-20 03:55:19 +01:00
{
return match ($operator) {
'>' => floatval($bt_value) > floatval($rule_value),
'>=' => floatval($bt_value) >= floatval($rule_value),
'=' => floatval($bt_value) == floatval($rule_value),
'<' => floatval($bt_value) < floatval($rule_value),
'<=' => floatval($bt_value) <= floatval($rule_value),
default => false,
};
}
2024-01-14 05:05:00 +01:00
private function matchStringOperator($bt_value, $rule_value, $operator): bool
2022-11-20 03:55:19 +01:00
{
$bt_value = strtolower(str_replace(" ", "", $bt_value));
$rule_value = strtolower(str_replace(" ", "", $rule_value));
$rule_length = iconv_strlen($rule_value);
return match ($operator) {
'is' => $bt_value == $rule_value,
2022-11-20 04:19:35 +01:00
'contains' => stripos($bt_value, $rule_value) !== false,
2022-11-20 03:55:19 +01:00
'starts_with' => substr($bt_value, 0, $rule_length) == $rule_value,
'is_empty' => empty($bt_value),
default => false,
};
}
2023-02-16 02:36:09 +01:00
}