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;
|
2024-08-27 03:36:31 +02:00
|
|
|
use App\Jobs\Bank\MatchBankTransactions;
|
2022-11-20 03:55:19 +01:00
|
|
|
use App\Models\BankTransaction;
|
2024-06-20 06:28:33 +02:00
|
|
|
use App\Models\Client;
|
2022-11-20 03:55:19 +01:00
|
|
|
use App\Models\ExpenseCategory;
|
|
|
|
use App\Models\Invoice;
|
2024-06-19 07:42:06 +02:00
|
|
|
use App\Models\Payment;
|
2022-11-20 03:55:19 +01:00
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2024-08-27 02:38:16 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2022-11-20 03:55:19 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
class ProcessBankRules extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
2024-08-27 02:38:16 +02:00
|
|
|
use MakesHash;
|
2024-08-27 03:36:31 +02:00
|
|
|
|
2022-11-20 03:55:19 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
// $payment.amount
|
|
|
|
// $payment.transaction_reference
|
|
|
|
// $payment.custom1
|
|
|
|
// $payment.custom2
|
|
|
|
// $payment.custom3
|
|
|
|
// $payment.custom4
|
|
|
|
// $invoice.amount
|
|
|
|
// $invoice.number
|
|
|
|
// $invoice.po_number
|
|
|
|
// $invoice.custom1
|
|
|
|
// $invoice.custom2
|
|
|
|
// $invoice.custom3
|
|
|
|
// $invoice.custom4
|
|
|
|
// $client.id_number
|
|
|
|
// $client.email
|
|
|
|
// $client.custom1
|
|
|
|
// $client.custom2
|
|
|
|
// $client.custom3
|
|
|
|
// $client.custom4
|
2022-11-20 03:55:19 +01:00
|
|
|
private function matchCredit()
|
|
|
|
{
|
2024-08-22 08:45:06 +02:00
|
|
|
$match_set = [];
|
|
|
|
|
2022-12-09 03:47:54 +01:00
|
|
|
$this->credit_rules = $this->bank_transaction->company->credit_rules();
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
foreach ($this->credit_rules as $bank_transaction_rule) {
|
|
|
|
$match_set = [];
|
2024-06-19 07:42:06 +02:00
|
|
|
|
|
|
|
if (!is_array($bank_transaction_rule['rules'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
$rule_count = count($bank_transaction_rule['rules']);
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
foreach ($bank_transaction_rule['rules'] as $rule) {
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 03:36:31 +02:00
|
|
|
$results = [];
|
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
$payments = Payment::query()
|
|
|
|
->withTrashed()
|
|
|
|
->whereIn('status_id', [1,4])
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereNull('transaction_id')
|
|
|
|
->get();
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
$invoices = Invoice::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
->whereIn('status_id', [1,2,3])
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
match($rule['search_key']) {
|
|
|
|
'$payment.amount' => $results = [Payment::class, $this->searchPaymentResource('amount', $rule, $payments)],
|
|
|
|
'$payment.transaction_reference' => $results = [Payment::class, $this->searchPaymentResource('transaction_reference', $rule, $payments)],
|
2024-08-27 02:38:16 +02:00
|
|
|
'$payment.custom1' => $results = [Payment::class, $this->searchPaymentResource('custom_value1', $rule, $payments)],
|
|
|
|
'$payment.custom2' => $results = [Payment::class, $this->searchPaymentResource('custom_value2', $rule, $payments)],
|
|
|
|
'$payment.custom3' => $results = [Payment::class, $this->searchPaymentResource('custom_value3', $rule, $payments)],
|
|
|
|
'$payment.custom4' => $results = [Payment::class, $this->searchPaymentResource('custom_value4', $rule, $payments)],
|
2024-08-22 08:45:06 +02:00
|
|
|
'$invoice.amount' => $results = [Invoice::class, $this->searchInvoiceResource('amount', $rule, $invoices)],
|
|
|
|
'$invoice.number' => $results = [Invoice::class, $this->searchInvoiceResource('number', $rule, $invoices)],
|
|
|
|
'$invoice.po_number' => $results = [Invoice::class, $this->searchInvoiceResource('po_number', $rule, $invoices)],
|
2024-08-27 02:38:16 +02:00
|
|
|
'$invoice.custom1' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value1', $rule, $invoices)],
|
|
|
|
'$invoice.custom2' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value2', $rule, $invoices)],
|
|
|
|
'$invoice.custom3' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value3', $rule, $invoices)],
|
|
|
|
'$invoice.custom4' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value4', $rule, $invoices)],
|
2024-08-22 08:45:06 +02:00
|
|
|
'$client.id_number' => $results = [Client::class, $this->searchClientResource('id_number', $rule, $invoices, $payments)],
|
|
|
|
'$client.email' => $results = [Client::class, $this->searchClientResource('email', $rule, $invoices, $payments)],
|
2024-08-27 02:38:16 +02:00
|
|
|
'$client.custom1' => $results = [Client::class, $this->searchClientResource('custom_value1', $rule, $invoices, $payments)],
|
|
|
|
'$client.custom2' => $results = [Client::class, $this->searchClientResource('custom_value2', $rule, $invoices, $payments)],
|
|
|
|
'$client.custom3' => $results = [Client::class, $this->searchClientResource('custom_value3', $rule, $invoices, $payments)],
|
|
|
|
'$client.custom4' => $results = [Client::class, $this->searchClientResource('custom_value4', $rule, $invoices, $payments)],
|
2024-08-22 08:45:06 +02:00
|
|
|
default => $results = [Client::class, [collect([]), Invoice::class]],
|
2024-08-21 09:19:44 +02:00
|
|
|
};
|
2024-08-22 08:45:06 +02:00
|
|
|
|
|
|
|
if($results[0] == 'App\Models\Client') {
|
|
|
|
$set = $results[1];
|
|
|
|
$result_set = $set[0];
|
|
|
|
$entity = $set[1];
|
|
|
|
|
|
|
|
if($result_set->count() > 0) {
|
|
|
|
$match_set[] = [$entity, $result_set->pluck('id')];
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif($results[1]->count() > 0) {
|
|
|
|
$match_set[] = $results;
|
|
|
|
}
|
|
|
|
}
|
2024-08-27 03:36:31 +02:00
|
|
|
|
|
|
|
if (($bank_transaction_rule['matches_on_all'] && $this->checkMatchSetForKey($match_set, $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && count($match_set) > 0))
|
|
|
|
{
|
2024-08-22 08:45:06 +02:00
|
|
|
|
|
|
|
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
|
|
|
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
|
2024-08-27 03:36:31 +02:00
|
|
|
$this->bank_transaction->save();
|
|
|
|
|
2024-08-27 02:06:11 +02:00
|
|
|
$first_result = reset($match_set);
|
2024-08-27 03:36:31 +02:00
|
|
|
|
|
|
|
$invoice_id = false;
|
|
|
|
$payment_id = false;
|
|
|
|
|
2024-08-27 02:06:11 +02:00
|
|
|
if($first_result[0] == Payment::class) {
|
|
|
|
$payment_id = $first_result[1][0];
|
2024-08-27 02:38:16 +02:00
|
|
|
}
|
|
|
|
elseif($first_result[0] == Invoice::class) {
|
|
|
|
$invoice_id = $first_result[1][0];
|
2024-08-27 02:06:11 +02:00
|
|
|
}
|
2024-08-27 02:38:16 +02:00
|
|
|
|
2024-08-23 00:08:28 +02:00
|
|
|
if ($bank_transaction_rule['auto_convert']) {
|
2024-08-27 03:36:31 +02:00
|
|
|
(new MatchBankTransactions($this->company->id, $this->company->db, [
|
|
|
|
'transactions' => [
|
|
|
|
[
|
|
|
|
'id' => $this->bank_transaction->id,
|
|
|
|
'invoice_ids' => $invoice_id ?? '',
|
|
|
|
'payment_id' => $payment_id ?? '',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]))->handle();
|
|
|
|
}
|
|
|
|
else {
|
2024-08-23 00:08:28 +02:00
|
|
|
|
2024-08-27 03:36:31 +02:00
|
|
|
if($invoice_id){
|
|
|
|
$this->bank_transaction->invoice_ids = $this->encodePrimaryKey($invoice_id);
|
|
|
|
}
|
|
|
|
elseif($payment_id){
|
|
|
|
$this->bank_transaction->payment_id = $payment_id;
|
2024-08-23 00:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->bank_transaction->save();
|
|
|
|
|
|
|
|
}
|
2024-08-27 03:36:31 +02:00
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-23 00:08:28 +02:00
|
|
|
}
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-23 00:08:28 +02:00
|
|
|
private function checkMatchSetForKey(array $match_set, $rule_count)
|
|
|
|
{
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
}
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
private function searchInvoiceResource(string $column, array $rule, $invoices)
|
2024-08-21 09:19:44 +02:00
|
|
|
{
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:06:11 +02:00
|
|
|
return $invoices->when($column != 'amount', function ($q) use ($rule, $column) {
|
|
|
|
return $q->filter(function ($record) use ($rule, $column) {
|
2024-08-22 08:45:06 +02:00
|
|
|
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
|
|
|
});
|
|
|
|
})
|
2024-08-27 02:06:11 +02:00
|
|
|
->when($column == 'amount', function ($q) use ($rule, $column) {
|
|
|
|
return $q->filter(function ($record) use ($rule, $column) {
|
2024-08-22 08:45:06 +02:00
|
|
|
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
|
|
|
});
|
|
|
|
})->pluck("id");
|
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
}
|
2024-08-22 08:45:06 +02:00
|
|
|
|
|
|
|
private function searchPaymentResource(string $column, array $rule, $payments)
|
2024-08-21 09:19:44 +02:00
|
|
|
{
|
2024-08-27 02:06:11 +02:00
|
|
|
return $payments->when($column != 'amount', function ($q) use ($rule, $column) {
|
|
|
|
return $q->filter(function ($record) use ($rule, $column) {
|
2024-08-27 02:38:16 +02:00
|
|
|
|
|
|
|
$bool = $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
|
|
|
return $bool;
|
2024-08-22 08:45:06 +02:00
|
|
|
});
|
|
|
|
})
|
2024-08-27 02:06:11 +02:00
|
|
|
->when($column == 'amount', function ($q) use ($rule, $column) {
|
|
|
|
return $q->filter(function ($record) use ($rule, $column) {
|
|
|
|
return $this->matchNumberOperator($this->bank_transaction->amount, $record->{$column}, $rule['operator']);
|
|
|
|
});
|
|
|
|
})->pluck("id");
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
}
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
private function searchClientResource(string $column, array $rule, $invoices, $payments)
|
2024-08-21 09:19:44 +02:00
|
|
|
{
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
$invoice_matches = Client::query()
|
|
|
|
->whereIn('id', $invoices->pluck('client_id'))
|
|
|
|
->when($column == 'email', function ($q) {
|
|
|
|
return $q->whereHas('contacts', function ($qc) {
|
|
|
|
$qc->where('email', $this->bank_transaction->description);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
->when($column != 'email', function ($q) use ($rule, $column) {
|
|
|
|
|
|
|
|
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
|
|
|
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
|
|
|
});
|
|
|
|
})->pluck('id');
|
|
|
|
|
|
|
|
|
|
|
|
$intersection = $invoices->whereIn('client_id', $invoice_matches);
|
|
|
|
|
|
|
|
if($intersection->count() > 0) {
|
|
|
|
return [$intersection, Invoice::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
$payments_matches = Client::query()
|
|
|
|
->whereIn('id', $payments->pluck('client_id'))
|
|
|
|
->when($column == 'email', function ($q) {
|
|
|
|
return $q->whereHas('contacts', function ($qc) {
|
|
|
|
$qc->where('email', $this->bank_transaction->description);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
->when($column != 'email', function ($q) use ($rule, $column) {
|
|
|
|
|
|
|
|
return $q->cursor()->filter(function ($record) use ($rule, $column) {
|
|
|
|
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
|
|
|
|
});
|
|
|
|
})->pluck('id');
|
|
|
|
|
|
|
|
$intersection = $payments->whereIn('client_id', $payments_matches);
|
|
|
|
|
|
|
|
if($intersection->count() > 0) {
|
|
|
|
return [$intersection, Payment::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [Client::class, collect([])];
|
|
|
|
|
2024-08-21 09:19:44 +02:00
|
|
|
}
|
2024-08-27 02:38:16 +02:00
|
|
|
|
|
|
|
private function matchDebit()
|
|
|
|
{
|
|
|
|
$this->debit_rules = $this->bank_transaction->company->debit_rules();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
$this->categories = collect(Cache::get('bank_categories'));
|
2024-06-19 10:18:05 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
foreach ($this->debit_rules as $bank_transaction_rule) {
|
|
|
|
$matches = 0;
|
2024-06-19 10:18:05 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if (!is_array($bank_transaction_rule['rules'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-19 10:18:05 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
foreach ($bank_transaction_rule['rules'] as $rule) {
|
|
|
|
$rule_count = count($bank_transaction_rule['rules']);
|
2024-06-19 10:18:05 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if ($rule['search_key'] == 'description') {
|
|
|
|
if ($this->matchStringOperator($this->bank_transaction->description, $rule['value'], $rule['operator'])) {
|
|
|
|
$matches++;
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if ($rule['search_key'] == 'amount') {
|
|
|
|
if ($this->matchNumberOperator($this->bank_transaction->amount, $rule['value'], $rule['operator'])) {
|
|
|
|
$matches++;
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if (($bank_transaction_rule['matches_on_all'] && ($matches == $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && $matches > 0)) {
|
|
|
|
// $this->bank_transaction->client_id = empty($rule['client_id']) ? null : $rule['client_id'];
|
|
|
|
$this->bank_transaction->vendor_id = $bank_transaction_rule->vendor_id;
|
|
|
|
$this->bank_transaction->ninja_category_id = $bank_transaction_rule->category_id;
|
|
|
|
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
|
|
|
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
|
|
|
|
$this->bank_transaction->save();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if ($bank_transaction_rule['auto_convert']) {
|
|
|
|
$expense = ExpenseFactory::create($this->bank_transaction->company_id, $this->bank_transaction->user_id);
|
|
|
|
$expense->category_id = $bank_transaction_rule->category_id ?: $this->resolveCategory();
|
|
|
|
$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;
|
|
|
|
$expense->vendor_id = $bank_transaction_rule->vendor_id;
|
|
|
|
$expense->invoice_documents = $this->bank_transaction->company->invoice_expense_documents;
|
|
|
|
$expense->should_be_invoiced = $this->bank_transaction->company->mark_expenses_invoiceable;
|
|
|
|
$expense->save();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
$this->bank_transaction->expense_id = $this->coalesceExpenses($expense->hashed_id);
|
|
|
|
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
|
|
|
|
$this->bank_transaction->save();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
private function coalesceExpenses($expense): string
|
|
|
|
{
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id ?? '') < 2) {
|
|
|
|
return $expense;
|
|
|
|
}
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
return collect(explode(",", $this->bank_transaction->expense_id))->push($expense)->implode(",");
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
}
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
private function resolveCategory()
|
|
|
|
{
|
|
|
|
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bank_transaction->category_id);
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
$ec = ExpenseCategory::query()->where('company_id', $this->bank_transaction->company_id)->where('bank_category_id', $this->bank_transaction->category_id)->first();
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if ($ec) {
|
|
|
|
return $ec->id;
|
|
|
|
}
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
if ($category) {
|
|
|
|
$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();
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
return $ec->id;
|
|
|
|
}
|
|
|
|
}
|
2024-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
private function matchNumberOperator($bt_value, $rule_value, $operator): bool
|
|
|
|
{
|
|
|
|
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-08-21 09:19:44 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
private function matchStringOperator($bt_value, $rule_value, $operator): bool
|
|
|
|
{
|
|
|
|
$bt_value = strtolower(str_replace(" ", "", $bt_value));
|
|
|
|
$rule_value = strtolower(str_replace(" ", "", $rule_value));
|
|
|
|
$rule_length = iconv_strlen($rule_value);
|
|
|
|
// nlog($bt_value);
|
|
|
|
// nlog($rule_value);
|
|
|
|
// nlog($rule_length);
|
|
|
|
return match ($operator) {
|
|
|
|
'is' => $bt_value == $rule_value,
|
|
|
|
'contains' => stripos($bt_value, $rule_value) !== false && strlen($rule_value) > 1,
|
|
|
|
'starts_with' => substr($bt_value, 0, $rule_length) == $rule_value && strlen($rule_value) > 1,
|
|
|
|
'is_empty' => empty($bt_value),
|
|
|
|
default => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-08-21 09:19:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $payment.amount => "Payment Amount", float
|
|
|
|
// $payment.transaction_reference => "Payment Transaction Reference", string
|
|
|
|
// $invoice.amount => "Invoice Amount", float
|
|
|
|
// $invoice.number => "Invoice Number", string
|
|
|
|
// $client.id_number => "Client ID Number", string
|
|
|
|
// $client.email => "Client Email", string
|
|
|
|
// $invoice.po_number => "Invoice Purchase Order Number", string
|
2024-08-21 09:19:44 +02:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// private function matchCredit()
|
|
|
|
// {
|
|
|
|
// $this->invoices = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
// ->whereIn('status_id', [1,2,3])
|
|
|
|
// ->where('is_deleted', 0)
|
|
|
|
// ->get();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoice = $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);
|
|
|
|
// });
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($invoice) {
|
|
|
|
// $this->bank_transaction->invoice_ids = $invoice->hashed_id;
|
|
|
|
// $this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
|
|
|
// $this->bank_transaction->save();
|
|
|
|
// return;
|
|
|
|
// }
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $this->credit_rules = $this->bank_transaction->company->credit_rules();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// //stub for credit rules
|
|
|
|
// foreach ($this->credit_rules as $bank_transaction_rule) {
|
|
|
|
// $matches = 0;
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if (!is_array($bank_transaction_rule['rules'])) {
|
|
|
|
// continue;
|
|
|
|
// }
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// foreach ($bank_transaction_rule['rules'] as $rule) {
|
|
|
|
// $rule_count = count($bank_transaction_rule['rules']);
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoiceNumbers = false;
|
|
|
|
// $invoiceNumber = false;
|
|
|
|
// $invoiceAmounts = false;
|
|
|
|
// $paymentAmounts = false;
|
|
|
|
// $paymentReferences = false;
|
|
|
|
// $clientIdNumbers = false;
|
|
|
|
// $clientEmails = false;
|
|
|
|
// $invoicePONumbers = false;
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$invoice.number') {
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoiceNumbers = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
// ->whereIn('status_id', [1,2,3])
|
|
|
|
// ->where('is_deleted', 0)
|
|
|
|
// ->get();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoiceNumber = $invoiceNumbers->first(function ($value, $key) {
|
|
|
|
// return str_contains($this->bank_transaction->description, $value->number) || str_contains(str_replace("\n", "", $this->bank_transaction->description), $value->number);
|
|
|
|
// });
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoiceNumber)
|
|
|
|
// $matches++;
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$invoice.po_number') {
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoicePONumbers = Invoice::query()->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
// ->whereIn('status_id', [1,2,3])
|
|
|
|
// ->where('is_deleted', 0)
|
|
|
|
// ->where('po_number', $this->bank_transaction->description)
|
|
|
|
// ->get();
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoicePONumbers->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$invoice.amount') {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $$invoiceAmounts = 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();
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoiceAmounts = $this->invoices;
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoiceAmounts->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$payment.amount') {
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $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();
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($paymentAmounts->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$payment.transaction_reference') {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $ref_search = $this->bank_transaction->description;
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// switch ($rule['operator']) {
|
|
|
|
// case 'is':
|
|
|
|
// $operator = '=';
|
|
|
|
// break;
|
|
|
|
// case 'contains':
|
|
|
|
// $ref_search = "%".$ref_search."%";
|
|
|
|
// $operator = 'LIKE';
|
|
|
|
// break;
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// default:
|
|
|
|
// $operator = '=';
|
|
|
|
// break;
|
|
|
|
// }
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $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();
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($paymentReferences->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$client.id_number') {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $ref_search = $this->bank_transaction->description;
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// switch ($rule['operator']) {
|
|
|
|
// case 'is':
|
|
|
|
// $operator = '=';
|
|
|
|
// break;
|
|
|
|
// case 'contains':
|
|
|
|
// $ref_search = "%".$ref_search."%";
|
|
|
|
// $operator = 'LIKE';
|
|
|
|
// break;
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// default:
|
|
|
|
// $operator = '=';
|
|
|
|
// break;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $clientIdNumbers = Client::query()->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
// ->where('id_number', $operator, $ref_search)
|
|
|
|
// ->get();
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientIdNumbers->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if ($rule['search_key'] == '$client.email') {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $clientEmails = Client::query()
|
|
|
|
// ->where('company_id', $this->bank_transaction->company_id)
|
|
|
|
// ->whereHas('contacts', function ($q){
|
|
|
|
// $q->where('email', $this->bank_transaction->description);
|
|
|
|
// })
|
|
|
|
// ->get();
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientEmails->count() > 0) {
|
|
|
|
// $matches++;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if (($bank_transaction_rule['matches_on_all'] && ($matches == $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && $matches > 0)) {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// //determine which combination has succeeded, ie link a payment / or / invoice
|
|
|
|
// $invoice_ids = null;
|
|
|
|
// $payment_id = null;
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoiceNumber){
|
|
|
|
// $invoice_ids = $invoiceNumber->hashed_id;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoicePONumbers && strlen($invoice_ids ?? '') == 0){
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientEmails){ // @phpstan-ignore-line
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoice_ids = $this->matchInvoiceAndClient($invoicePONumbers, $clientEmails);
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientIdNumbers && strlen($invoice_ids ?? '') == 0)
|
|
|
|
// {
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoice_ids = $this->matchInvoiceAndClient($invoicePONumbers, $clientIdNumbers);
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if(strlen($invoice_ids ?? '') == 0)
|
|
|
|
// {
|
|
|
|
// $invoice_ids = $invoicePONumbers->first()->hashed_id;
|
|
|
|
// }
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoiceAmounts && strlen($invoice_ids ?? '') == 0) {
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientEmails) {// @phpstan-ignore-line
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoice_ids = $this->matchInvoiceAndClient($invoiceAmounts, $clientEmails);
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientIdNumbers && strlen($invoice_ids ?? '') == 0) {
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $invoice_ids = $this->matchInvoiceAndClient($invoiceAmounts, $clientIdNumbers);
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if(strlen($invoice_ids ?? '') == 0) {
|
|
|
|
// $invoice_ids = $invoiceAmounts->first()->hashed_id;
|
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2024-06-20 06:28:33 +02:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($paymentAmounts && strlen($invoice_ids ?? '') == 0 && is_null($payment_id)) {
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientEmails) {// @phpstan-ignore-line
|
2024-06-20 06:28:33 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $payment_id = $this->matchPaymentAndClient($paymentAmounts, $clientEmails);
|
2024-06-19 07:42:06 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($clientIdNumbers && is_null($payment_id)) {
|
2022-11-20 03:55:19 +01:00
|
|
|
|
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $payment_id = $this->matchPaymentAndClient($paymentAmounts, $clientEmails);
|
2022-12-09 03:47:54 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if(is_null($payment_id)) {
|
|
|
|
// $payment_id = $paymentAmounts->first()->id;
|
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if(strlen($invoice_ids ?? '') > 1 || is_int($payment_id))
|
|
|
|
// {
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// $this->bank_transaction->payment_id = $payment_id;
|
|
|
|
// $this->bank_transaction->invoice_ids = $invoice_ids;
|
|
|
|
// $this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
|
|
|
// $this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
|
|
|
|
// $this->bank_transaction->save();
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2023-04-06 03:38:29 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2023-04-06 03:38:29 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2023-04-06 03:38:29 +02:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
2023-04-06 03:38:29 +02:00
|
|
|
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// private function matchPaymentAndClient($payments, $clients): ?int
|
|
|
|
// {
|
|
|
|
// /** @var \Illuminate\Support\Collection<Payment> $payments */
|
|
|
|
// foreach($payments as $payment) {
|
|
|
|
// foreach($clients as $client) {
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($payment->client_id == $client->id) {
|
|
|
|
// return $payment->id;
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// return null;
|
|
|
|
// }
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// private function matchInvoiceAndClient($invoices, $clients): ?Invoice
|
|
|
|
// {
|
|
|
|
// /** @var \Illuminate\Support\Collection<Invoice> $invoices */
|
|
|
|
// foreach($invoices as $invoice) {
|
|
|
|
// foreach($clients as $client) {
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// if($invoice->client_id == $client->id) {
|
|
|
|
// return $invoice->hashed_id;
|
2022-11-20 03:55:19 +01:00
|
|
|
|
2024-08-27 02:38:16 +02:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return null;
|
|
|
|
// }
|