Tests forbank processing rules

This commit is contained in:
David Bomba 2024-08-27 10:38:16 +10:00
parent 47311c5f2c
commit 376b728e68
2 changed files with 822 additions and 341 deletions

View File

@ -20,12 +20,14 @@ use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Services\AbstractService; use App\Services\AbstractService;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
class ProcessBankRules extends AbstractService class ProcessBankRules extends AbstractService
{ {
use GeneratesCounter; use GeneratesCounter;
use MakesHash;
protected $credit_rules; protected $credit_rules;
@ -106,23 +108,23 @@ class ProcessBankRules extends AbstractService
match($rule['search_key']) { match($rule['search_key']) {
'$payment.amount' => $results = [Payment::class, $this->searchPaymentResource('amount', $rule, $payments)], '$payment.amount' => $results = [Payment::class, $this->searchPaymentResource('amount', $rule, $payments)],
'$payment.transaction_reference' => $results = [Payment::class, $this->searchPaymentResource('transaction_reference', $rule, $payments)], '$payment.transaction_reference' => $results = [Payment::class, $this->searchPaymentResource('transaction_reference', $rule, $payments)],
'$payment.custom1' => $results = [Payment::class, $this->searchPaymentResource('custom1', $rule, $payments)], '$payment.custom1' => $results = [Payment::class, $this->searchPaymentResource('custom_value1', $rule, $payments)],
'$payment.custom2' => $results = [Payment::class, $this->searchPaymentResource('custom2', $rule, $payments)], '$payment.custom2' => $results = [Payment::class, $this->searchPaymentResource('custom_value2', $rule, $payments)],
'$payment.custom3' => $results = [Payment::class, $this->searchPaymentResource('custom3', $rule, $payments)], '$payment.custom3' => $results = [Payment::class, $this->searchPaymentResource('custom_value3', $rule, $payments)],
'$payment.custom4' => $results = [Payment::class, $this->searchPaymentResource('custom4', $rule, $payments)], '$payment.custom4' => $results = [Payment::class, $this->searchPaymentResource('custom_value4', $rule, $payments)],
'$invoice.amount' => $results = [Invoice::class, $this->searchInvoiceResource('amount', $rule, $invoices)], '$invoice.amount' => $results = [Invoice::class, $this->searchInvoiceResource('amount', $rule, $invoices)],
'$invoice.number' => $results = [Invoice::class, $this->searchInvoiceResource('number', $rule, $invoices)], '$invoice.number' => $results = [Invoice::class, $this->searchInvoiceResource('number', $rule, $invoices)],
'$invoice.po_number' => $results = [Invoice::class, $this->searchInvoiceResource('po_number', $rule, $invoices)], '$invoice.po_number' => $results = [Invoice::class, $this->searchInvoiceResource('po_number', $rule, $invoices)],
'$invoice.custom1' => $results = [Invoice::class, $this->searchInvoiceResource('custom1', $rule, $invoices)], '$invoice.custom1' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value1', $rule, $invoices)],
'$invoice.custom2' => $results = [Invoice::class, $this->searchInvoiceResource('custom2', $rule, $invoices)], '$invoice.custom2' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value2', $rule, $invoices)],
'$invoice.custom3' => $results = [Invoice::class, $this->searchInvoiceResource('custom3', $rule, $invoices)], '$invoice.custom3' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value3', $rule, $invoices)],
'$invoice.custom4' => $results = [Invoice::class, $this->searchInvoiceResource('custom4', $rule, $invoices)], '$invoice.custom4' => $results = [Invoice::class, $this->searchInvoiceResource('custom_value4', $rule, $invoices)],
'$client.id_number' => $results = [Client::class, $this->searchClientResource('id_number', $rule, $invoices, $payments)], '$client.id_number' => $results = [Client::class, $this->searchClientResource('id_number', $rule, $invoices, $payments)],
'$client.email' => $results = [Client::class, $this->searchClientResource('email', $rule, $invoices, $payments)], '$client.email' => $results = [Client::class, $this->searchClientResource('email', $rule, $invoices, $payments)],
'$client.custom1' => $results = [Client::class, $this->searchClientResource('custom1', $rule, $invoices, $payments)], '$client.custom1' => $results = [Client::class, $this->searchClientResource('custom_value1', $rule, $invoices, $payments)],
'$client.custom2' => $results = [Client::class, $this->searchClientResource('custom2', $rule, $invoices, $payments)], '$client.custom2' => $results = [Client::class, $this->searchClientResource('custom_value2', $rule, $invoices, $payments)],
'$client.custom3' => $results = [Client::class, $this->searchClientResource('custom3', $rule, $invoices, $payments)], '$client.custom3' => $results = [Client::class, $this->searchClientResource('custom_value3', $rule, $invoices, $payments)],
'$client.custom4' => $results = [Client::class, $this->searchClientResource('custom4', $rule, $invoices, $payments)], '$client.custom4' => $results = [Client::class, $this->searchClientResource('custom_value4', $rule, $invoices, $payments)],
default => $results = [Client::class, [collect([]), Invoice::class]], default => $results = [Client::class, [collect([]), Invoice::class]],
}; };
@ -147,13 +149,16 @@ class ProcessBankRules extends AbstractService
$this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED; $this->bank_transaction->status_id = BankTransaction::STATUS_MATCHED;
$this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id; $this->bank_transaction->bank_transaction_rule_id = $bank_transaction_rule->id;
$first_result = reset($match_set); $first_result = reset($match_set);
if($first_result[0] == Payment::class) { if($first_result[0] == Payment::class) {
$payment_id = $first_result[1][0]; $payment_id = $first_result[1][0];
$this->bank_transaction->payment_id = $payment_id; $this->bank_transaction->payment_id = $payment_id;
} }
elseif($first_result[0] == Invoice::class) {
$invoice_id = $first_result[1][0];
$this->bank_transaction->invoice_ids = $this->encodePrimaryKey($invoice_id);
}
$this->bank_transaction->save(); $this->bank_transaction->save();
//auto-convert //auto-convert
@ -245,7 +250,9 @@ class ProcessBankRules extends AbstractService
{ {
return $payments->when($column != 'amount', function ($q) use ($rule, $column) { return $payments->when($column != 'amount', function ($q) use ($rule, $column) {
return $q->filter(function ($record) use ($rule, $column) { return $q->filter(function ($record) use ($rule, $column) {
return $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
$bool = $this->matchStringOperator($this->bank_transaction->description, $record->{$column}, $rule['operator']);
return $bool;
}); });
}) })
->when($column == 'amount', function ($q) use ($rule, $column) { ->when($column == 'amount', function ($q) use ($rule, $column) {
@ -303,6 +310,132 @@ class ProcessBankRules extends AbstractService
return [Client::class, collect([])]; return [Client::class, collect([])];
} }
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;
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'] == 'description') {
if ($this->matchStringOperator($this->bank_transaction->description, $rule['value'], $rule['operator'])) {
$matches++;
}
}
if ($rule['search_key'] == 'amount') {
if ($this->matchNumberOperator($this->bank_transaction->amount, $rule['value'], $rule['operator'])) {
$matches++;
}
}
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();
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();
$this->bank_transaction->expense_id = $this->coalesceExpenses($expense->hashed_id);
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
$this->bank_transaction->save();
break;
}
}
}
}
}
private function coalesceExpenses($expense): string
{
if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id ?? '') < 2) {
return $expense;
}
return collect(explode(",", $this->bank_transaction->expense_id))->push($expense)->implode(",");
}
private function resolveCategory()
{
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bank_transaction->category_id);
$ec = ExpenseCategory::query()->where('company_id', $this->bank_transaction->company_id)->where('bank_category_id', $this->bank_transaction->category_id)->first();
if ($ec) {
return $ec->id;
}
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();
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);
// 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,
};
}
}
// $payment.amount => "Payment Amount", float // $payment.amount => "Payment Amount", float
// $payment.transaction_reference => "Payment Transaction Reference", string // $payment.transaction_reference => "Payment Transaction Reference", string
// $invoice.amount => "Invoice Amount", float // $invoice.amount => "Invoice Amount", float
@ -621,124 +754,3 @@ class ProcessBankRules extends AbstractService
// return null; // return null;
// } // }
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;
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'] == 'description') {
if ($this->matchStringOperator($this->bank_transaction->description, $rule['value'], $rule['operator'])) {
$matches++;
}
}
if ($rule['search_key'] == 'amount') {
if ($this->matchNumberOperator($this->bank_transaction->amount, $rule['value'], $rule['operator'])) {
$matches++;
}
}
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();
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();
$this->bank_transaction->expense_id = $this->coalesceExpenses($expense->hashed_id);
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
$this->bank_transaction->save();
break;
}
}
}
}
}
private function coalesceExpenses($expense): string
{
if (!$this->bank_transaction->expense_id || strlen($this->bank_transaction->expense_id ?? '') < 2) {
return $expense;
}
return collect(explode(",", $this->bank_transaction->expense_id))->push($expense)->implode(",");
}
private function resolveCategory()
{
$category = $this->categories->firstWhere('highLevelCategoryId', $this->bank_transaction->category_id);
$ec = ExpenseCategory::query()->where('company_id', $this->bank_transaction->company_id)->where('bank_category_id', $this->bank_transaction->category_id)->first();
if ($ec) {
return $ec->id;
}
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();
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' => stripos($bt_value, $rule_value) !== false,
'starts_with' => substr($bt_value, 0, $rule_length) == $rule_value,
'is_empty' => empty($bt_value),
default => false,
};
}
}

View File

@ -18,10 +18,12 @@ use Tests\MockAccountData;
use App\Models\BankIntegration; use App\Models\BankIntegration;
use App\Models\BankTransaction; use App\Models\BankTransaction;
use App\Models\BankTransactionRule; use App\Models\BankTransactionRule;
use App\Models\Invoice;
use App\Services\Bank\ProcessBankRules; use App\Services\Bank\ProcessBankRules;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Str;
class BankTransactionRuleTest extends TestCase class BankTransactionRuleTest extends TestCase
{ {
@ -41,7 +43,361 @@ class BankTransactionRuleTest extends TestCase
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
} }
public function testNewCreditMatchingRules() public function testNewCreditMatchingRulesInvoiceStartsWith()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$invoice.number',
'operator' => 'starts_with',
]
]
]);
$i = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'balance' => $rand_amount,
'number' => $hash,
'status_id' => 2,
'custom_value1' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($i->id);
$this->assertNotNull($bt->invoice_ids);
$this->assertEquals($i->hashed_id, $bt->invoice_ids);
}
public function testNewCreditMatchingRulesInvoiceContains()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$invoice.number',
'operator' => 'contains',
]
]
]);
$i = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'balance' => $rand_amount,
'number' => $hash,
'status_id' => 2,
'custom_value1' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($i->id);
$this->assertNotNull($bt->invoice_ids);
$this->assertEquals($i->hashed_id, $bt->invoice_ids);
}
public function testNewCreditMatchingRulesInvoiceNumber()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$invoice.number',
'operator' => 'is',
]
]
]);
$i = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'balance' => $rand_amount,
'number' => $hash,
'status_id' => 2,
'custom_value1' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($i->id);
$this->assertNotNull($bt->invoice_ids);
$this->assertEquals($i->hashed_id, $bt->invoice_ids);
}
public function testNewCreditMatchingRulesInvoiceAmount()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$invoice.amount',
'operator' => '=',
]
]
]);
$i = Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'balance' => $rand_amount,
'status_id' => 2,
'custom_value1' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($i->id);
$this->assertNotNull($bt->invoice_ids);
$this->assertEquals($i->hashed_id, $bt->invoice_ids);
}
public function testNewCreditMatchingRulesPaymentCustomValue()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$payment.custom1',
'operator' => 'starts_with',
]
]
]);
$p = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'custom_value1' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($p->id);
$this->assertNotNull($bt->payment_id);
$this->assertEquals($p->id, $bt->payment_id);
}
public function testNewCreditMatchingRulesPaymentStartsWith()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$payment.transaction_reference',
'operator' => 'starts_with',
]
]
]);
$p = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'transaction_reference' => substr($hash, 0, 8)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($p->id);
$this->assertNotNull($bt->payment_id);
$this->assertEquals($p->id, $bt->payment_id);
}
public function testNewCreditMatchingRulesPaymentAmount()
{ {
$bi = BankIntegration::factory()->create([ $bi = BankIntegration::factory()->create([
@ -86,7 +442,62 @@ class BankTransactionRuleTest extends TestCase
'transaction_reference' => 'nein' 'transaction_reference' => 'nein'
]); ]);
nlog($p->id); $this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($p->id);
$this->assertNotNull($bt->payment_id);
$this->assertEquals($p->id, $bt->payment_id);
}
public function testNewCreditMatchingRulesPaymentTransactionReferenceExactMatch()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = md5(time());
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$payment.transaction_reference',
'operator' => 'is',
]
]
]);
$p = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'transaction_reference' => $hash
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id); $this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
@ -100,6 +511,64 @@ class BankTransactionRuleTest extends TestCase
$this->assertEquals($p->id, $bt->payment_id); $this->assertEquals($p->id, $bt->payment_id);
} }
public function testNewCreditMatchingRulesPaymentTransactionReferenceContains()
{
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$hash = Str::random(32);
$rand_amount = rand(1000,10000000);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => $hash,
'base_type' => 'CREDIT',
'amount' => $rand_amount
]);
$this->assertNull($bt->payment_id);
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => false,
'applies_to' => 'CREDIT',
'rules' => [
[
'search_key' => '$payment.transaction_reference',
'operator' => 'contains',
]
]
]);
$p = Payment::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'amount' => $rand_amount,
'transaction_reference' => substr($hash, 3, 13)
]);
$this->assertEquals(BankTransaction::STATUS_UNMATCHED, $bt->status_id);
(new ProcessBankRules($bt))->run();
$bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
$this->assertNotNull($p->id);
$this->assertNotNull($bt->payment_id);
$this->assertEquals($p->id, $bt->payment_id);
}
public function testMatchCreditOnInvoiceNumber() public function testMatchCreditOnInvoiceNumber()
{ {