mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Matching Bank Transactions
This commit is contained in:
parent
3562c3376c
commit
9412760a25
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BankTransactionRule;
|
||||
use App\Models\Filterable;
|
||||
use App\Models\Invoice;
|
||||
use App\Services\Bank\BankService;
|
||||
|
@ -42,13 +42,9 @@ class BankTransactionRule extends BaseModel
|
||||
protected $dates = [
|
||||
];
|
||||
|
||||
/* Columns to search */
|
||||
protected array $search_keys = [
|
||||
'client_id' => 'client',
|
||||
'vendor_id' => 'vendor',
|
||||
'description' => 'description',
|
||||
'transaction_reference' => 'transaction_reference',
|
||||
'amount' => 'amount',
|
||||
'description' => 'string',
|
||||
'amount' => 'number',
|
||||
];
|
||||
|
||||
/* Amount */
|
||||
@ -79,68 +75,68 @@ class BankTransactionRule extends BaseModel
|
||||
// }
|
||||
//]
|
||||
|
||||
public function processRule(BankTransaction $bank_transaction)
|
||||
{
|
||||
foreach($this->rules as $key => $rule)
|
||||
{
|
||||
$this->search($rule, $key, $bank_transaction);
|
||||
}
|
||||
}
|
||||
// public function processRule(BankTransaction $bank_transaction)
|
||||
// {
|
||||
// foreach($this->rules as $key => $rule)
|
||||
// {
|
||||
// $this->search($rule, $key, $bank_transaction);
|
||||
// }
|
||||
// }
|
||||
|
||||
private function search($rule, $key, $bank_transaction)
|
||||
{
|
||||
if($rule->search_key == 'amount')
|
||||
{
|
||||
//number search
|
||||
}
|
||||
else {
|
||||
//string search
|
||||
}
|
||||
}
|
||||
// private function search($rule, $key, $bank_transaction)
|
||||
// {
|
||||
// if($rule->search_key == 'amount')
|
||||
// {
|
||||
// //number search
|
||||
// }
|
||||
// else {
|
||||
// //string search
|
||||
// }
|
||||
// }
|
||||
|
||||
private function findAmount($amount, $bank_transaction)
|
||||
{
|
||||
if($bank_transaction->base_type == 'CREDIT'){
|
||||
//search invoices
|
||||
}
|
||||
else{
|
||||
//search expenses
|
||||
}
|
||||
// private function findAmount($amount, $bank_transaction)
|
||||
// {
|
||||
// if($bank_transaction->base_type == 'CREDIT'){
|
||||
// //search invoices
|
||||
// }
|
||||
// else{
|
||||
// //search expenses
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
private function searchClient($rule, $bank_transaction)
|
||||
{
|
||||
if($bank_transaction->base_type == 'CREDIT'){
|
||||
//search invoices
|
||||
}
|
||||
else{
|
||||
//search expenses
|
||||
}
|
||||
// private function searchClient($rule, $bank_transaction)
|
||||
// {
|
||||
// if($bank_transaction->base_type == 'CREDIT'){
|
||||
// //search invoices
|
||||
// }
|
||||
// else{
|
||||
// //search expenses
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
private function searchVendor($rule, $bank_transaction)
|
||||
{
|
||||
//search expenses
|
||||
// private function searchVendor($rule, $bank_transaction)
|
||||
// {
|
||||
// //search expenses
|
||||
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
private function searchDescription($rule, $bank_transaction)
|
||||
{
|
||||
//search expenses public notes
|
||||
}
|
||||
// private function searchDescription($rule, $bank_transaction)
|
||||
// {
|
||||
// //search expenses public notes
|
||||
// }
|
||||
|
||||
private function searchReference($rule, $bank_transaction)
|
||||
{
|
||||
if($bank_transaction->base_type == 'CREDIT'){
|
||||
//search invoices
|
||||
}
|
||||
else{
|
||||
//search expenses
|
||||
}
|
||||
}
|
||||
// private function searchReference($rule, $bank_transaction)
|
||||
// {
|
||||
// if($bank_transaction->base_type == 'CREDIT'){
|
||||
// //search invoices
|
||||
// }
|
||||
// else{
|
||||
// //search expenses
|
||||
// }
|
||||
// }
|
||||
|
||||
public function getEntityType()
|
||||
{
|
||||
|
@ -13,6 +13,7 @@ namespace App\Models;
|
||||
|
||||
use App\DataMapper\CompanySettings;
|
||||
use App\Models\BankTransaction;
|
||||
use App\Models\BankTransactionRule;
|
||||
use App\Models\Language;
|
||||
use App\Models\Presenters\CompanyPresenter;
|
||||
use App\Models\PurchaseOrder;
|
||||
@ -541,6 +542,23 @@ class Company extends BaseModel
|
||||
return $this->company_users()->withTrashed()->where('is_owner', true)->first()?->user;
|
||||
}
|
||||
|
||||
public function credit_rules()
|
||||
{
|
||||
return BankTransactionRule::query()
|
||||
->where('company_id', $this->id)
|
||||
->where('applies_to', 'CREDIT')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function debit_rules()
|
||||
{
|
||||
return BankTransactionRule::query()
|
||||
->where('company_id', $this->id)
|
||||
->where('applies_to', 'DEBIT')
|
||||
->get();
|
||||
}
|
||||
|
||||
|
||||
public function resolveRouteBinding($value, $field = null)
|
||||
{
|
||||
return $this->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
||||
|
@ -11,19 +11,26 @@
|
||||
|
||||
namespace App\Services\Bank;
|
||||
|
||||
use App\Factory\ExpenseCategoryFactory;
|
||||
use App\Factory\ExpenseFactory;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\BankTransaction;
|
||||
use App\Models\Company;
|
||||
use App\Models\ExpenseCategory;
|
||||
use App\Models\Invoice;
|
||||
use App\Utils\Traits\GeneratesCounter;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class BankMatchingService implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GeneratesCounter;
|
||||
|
||||
private $company_id;
|
||||
|
||||
@ -35,12 +42,23 @@ class BankMatchingService implements ShouldQueue
|
||||
|
||||
public $deleteWhenMissingModels = true;
|
||||
|
||||
protected $credit_rules;
|
||||
|
||||
protected $debit_rules;
|
||||
|
||||
protected $categories;
|
||||
|
||||
public function __construct($company_id, $db)
|
||||
{
|
||||
$this->company_id = $company_id;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function middleware()
|
||||
{
|
||||
return [new WithoutOverlapping($this->company->company_key)];
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
|
||||
@ -48,19 +66,162 @@ class BankMatchingService implements ShouldQueue
|
||||
|
||||
$this->company = Company::find($this->company_id);
|
||||
|
||||
$this->categories = collect(Cache::get('bank_categories'));
|
||||
|
||||
$this->matchCredits();
|
||||
|
||||
$this->matchDebits();
|
||||
|
||||
}
|
||||
|
||||
private function matchDebits()
|
||||
{
|
||||
|
||||
$this->debit_rules = $this->company->debit_rules();
|
||||
|
||||
BankTransaction::where('company_id', $this->company->id)
|
||||
->where('status_id', BankTransaction::STATUS_UNMATCHED)
|
||||
->where('base_type', 'DEBIT')
|
||||
->cursor()
|
||||
->each(function ($bt){
|
||||
|
||||
$this->matchDebit($bt);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private function matchDebit(BankTransaction $bank_transaction)
|
||||
{
|
||||
$matches = 0;
|
||||
|
||||
foreach($this->debit_rules as $rule)
|
||||
{
|
||||
$rule_count = count($this->debit_rules);
|
||||
|
||||
if($rule['search_key'] == 'description')
|
||||
{
|
||||
|
||||
if($this->matchStringOperator($bank_transaction->description, 'description', $rule['operator'])){
|
||||
$matches++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if($rule['search_key'] == 'amount')
|
||||
{
|
||||
|
||||
if($this->matchNumberOperator($bank_transaction->description, 'amount', $rule['operator'])){
|
||||
$matches++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(($rule['matches_on_all'] && ($matches == $rule_count)) || (!$rule['matches_on_all'] &&$matches > 0))
|
||||
{
|
||||
|
||||
$bank_transaction->client_id = empty($rule['client_id']) ? null : $rule['client_id'];
|
||||
$bank_transaction->vendor_id = empty($rule['vendor_id']) ? null : $rule['vendor_id'];
|
||||
$bank_transaction->ninja_category_id = empty($rule['category_id']) ? null : $rule['category_id'];
|
||||
$bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
|
||||
$bank_transaction->save();
|
||||
|
||||
if($rule['auto_convert'])
|
||||
{
|
||||
|
||||
$expense = ExpenseFactory::create($bank_transaction->company_id, $bank_transaction->user_id);
|
||||
$expense->category_id = $bank_transaction->ninja_category_id ?: $this->resolveCategory($bank_transaction);
|
||||
$expense->amount = $bank_transaction->amount;
|
||||
$expense->number = $this->getNextExpenseNumber($expense);
|
||||
$expense->currency_id = $bank_transaction->currency_id;
|
||||
$expense->date = Carbon::parse($bank_transaction->date);
|
||||
$expense->payment_date = Carbon::parse($bank_transaction->date);
|
||||
$expense->transaction_reference = $bank_transaction->description;
|
||||
$expense->transaction_id = $bank_transaction->id;
|
||||
$expense->vendor_id = $bank_transaction->vendor_id;
|
||||
$expense->invoice_documents = $this->company->invoice_expense_documents;
|
||||
$expense->should_be_invoiced = $this->company->mark_expenses_invoiceable;
|
||||
$expense->save();
|
||||
|
||||
$bank_transaction->expense_id = $expense->id;
|
||||
$bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
|
||||
$bank_transaction->save();
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function resolveCategory(BankTransaction $bank_transaction)
|
||||
{
|
||||
$category = $this->categories->firstWhere('highLevelCategoryId', $bank_transaction->category_id);
|
||||
|
||||
$ec = ExpenseCategory::where('company_id', $this->company->id)->where('bank_category_id', $bank_transaction->category_id)->first();
|
||||
|
||||
if($ec)
|
||||
return $ec->id;
|
||||
|
||||
if($category)
|
||||
{
|
||||
$ec = ExpenseCategoryFactory::create($bank_transaction->company_id, $bank_transaction->user_id);
|
||||
$ec->bank_category_id = $bank_transaction->category_id;
|
||||
$ec->name = $category->highLevelCategoryName;
|
||||
$ec->save();
|
||||
|
||||
return $ec->id;
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return match ($operator) {
|
||||
'is' => $bt_value == $rule_value,
|
||||
'contains' => str_contains($bt_value, $rule_value),
|
||||
'starts_with' => substr($bt_value, 0, $rule_length) == $rule_value,
|
||||
'is_empty' => empty($bt_value),
|
||||
default => false,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Currently we don't consider rules here, only matching on invoice number*/
|
||||
private function matchCredits()
|
||||
{
|
||||
$this->credit_rules = $this->company->credit_rules();
|
||||
|
||||
$this->invoices = Invoice::where('company_id', $this->company->id)
|
||||
->whereIn('status_id', [1,2,3])
|
||||
->where('is_deleted', 0)
|
||||
->get();
|
||||
|
||||
$this->match();
|
||||
}
|
||||
|
||||
private function match()
|
||||
{
|
||||
|
||||
BankTransaction::where('company_id', $this->company->id)
|
||||
->where('status_id', BankTransaction::STATUS_UNMATCHED)
|
||||
->where('base_type', 'CREDIT')
|
||||
->cursor()
|
||||
->each(function ($bt){
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user