diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index d886846a8c..402edbebd4 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -238,6 +238,7 @@ class MatchBankTransactions implements ShouldQueue $amount = $this->bt->amount; if ($_invoices->count() > 0 && $this->checkPayable($_invoices)) { + $this->createPayment($_invoices, $amount); $this->bts->push($this->bt->id); @@ -293,6 +294,8 @@ class MatchBankTransactions implements ShouldQueue $this->attachable_invoices = []; $this->available_balance = $amount; + nlog($invoices->count()); + \DB::connection(config('database.default'))->transaction(function () use ($invoices) { $invoices->each(function ($invoice) { $this->invoice = Invoice::withTrashed()->where('id', $invoice->id)->lockForUpdate()->first(); @@ -326,11 +329,16 @@ class MatchBankTransactions implements ShouldQueue }); }, 2); + nlog("pre"); + // @phpstan-ignore-next-line if (!$this->invoice) { return; } + +nlog("post"); + /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); @@ -395,6 +403,9 @@ class MatchBankTransactions implements ShouldQueue $this->bt->status_id = BankTransaction::STATUS_CONVERTED; $this->bt->payment_id = $payment->id; $this->bt->save(); + +nlog($this->bt->toArray()); + } private function resolveCategory($input): ?int diff --git a/app/Services/Bank/ProcessBankRules.php b/app/Services/Bank/ProcessBankRules.php index 19787f2b91..17a6c8bdd8 100644 --- a/app/Services/Bank/ProcessBankRules.php +++ b/app/Services/Bank/ProcessBankRules.php @@ -13,6 +13,7 @@ namespace App\Services\Bank; use App\Factory\ExpenseCategoryFactory; use App\Factory\ExpenseFactory; +use App\Jobs\Bank\MatchBankTransactions; use App\Models\BankTransaction; use App\Models\Client; use App\Models\ExpenseCategory; @@ -28,7 +29,7 @@ class ProcessBankRules extends AbstractService { use GeneratesCounter; use MakesHash; - + protected $credit_rules; protected $debit_rules; @@ -89,6 +90,8 @@ class ProcessBankRules extends AbstractService foreach ($bank_transaction_rule['rules'] as $rule) { + $results = []; + $payments = Payment::query() ->withTrashed() ->whereIn('status_id', [1,4]) @@ -103,8 +106,6 @@ class ProcessBankRules extends AbstractService ->where('is_deleted', 0) ->get(); - $results = []; - 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)], @@ -141,84 +142,50 @@ class ProcessBankRules extends AbstractService $match_set[] = $results; } } + + if (($bank_transaction_rule['matches_on_all'] && $this->checkMatchSetForKey($match_set, $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && count($match_set) > 0)) + { - if (($bank_transaction_rule['matches_on_all'] && $this->checkMatchSetForKey($match_set, $rule_count)) || (!$bank_transaction_rule['matches_on_all'] && count($match_set) > 0)) { - - // $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(); + $first_result = reset($match_set); - + + $invoice_id = false; + $payment_id = false; + if($first_result[0] == Payment::class) { $payment_id = $first_result[1][0]; - $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(); - //auto-convert - if ($bank_transaction_rule['auto_convert']) { + (new MatchBankTransactions($this->company->id, $this->company->db, [ + 'transactions' => [ + [ + 'id' => $this->bank_transaction->id, + 'invoice_ids' => $invoice_id ?? '', + 'payment_id' => $payment_id ?? '', + ], + ], + ]))->handle(); + } + else { - //all types must match. - $entity = $match_set[0][0]; - - foreach($match_set as $set) - { - if($set[0] != $entity) - return false; + if($invoice_id){ + $this->bank_transaction->invoice_ids = $this->encodePrimaryKey($invoice_id); + } + elseif($payment_id){ + $this->bank_transaction->payment_id = $payment_id; } - -// $result_set = []; - -// foreach($match_set as $key => $set) { - -// $parseable_set = $match_set; -// unset($parseable_set[$key]); - -// $entity_ids = $set[1]; - -// foreach($parseable_set as $kkey => $vvalue) { - -// $i = array_intersect($vvalue[1], $entity_ids); - -// if(count($i) == 0) { -// return false; -// } - - -// $result_set[] = $i; - -// } - - -// $commonValues = $result_set[0]; // Start with the first sub-array - -// foreach ($result_set as $subArray) { -// $commonValues = array_intersect($commonValues, $subArray); -// } - -// echo print_r($commonValues, true); - -//just need to ensure the result count = rule count -// } - - - - //there must be a key in each set - - //no misses allowed - - $this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED; $this->bank_transaction->save(); } + } } diff --git a/tests/Feature/Bank/BankTransactionRuleTest.php b/tests/Feature/Bank/BankTransactionRuleTest.php index 3b455b683e..7282d1030d 100644 --- a/tests/Feature/Bank/BankTransactionRuleTest.php +++ b/tests/Feature/Bank/BankTransactionRuleTest.php @@ -95,7 +95,7 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + $bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($i->id); @@ -215,7 +215,7 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + $bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($i->id); @@ -332,7 +332,7 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + $bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($p->id); @@ -389,7 +389,8 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + +$bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($p->id); @@ -446,7 +447,8 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + +$bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($p->id); @@ -503,7 +505,8 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + +$bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($p->id); @@ -560,7 +563,8 @@ class BankTransactionRuleTest extends TestCase (new ProcessBankRules($bt))->run(); - $bt->fresh(); + +$bt = $bt->fresh(); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id); $this->assertNotNull($p->id);