Refactor for bank transactions

This commit is contained in:
David Bomba 2022-09-21 17:00:49 +10:00
parent d14df7ef2d
commit 3865d7193e
5 changed files with 104 additions and 49 deletions

View File

@ -132,7 +132,7 @@ class IncomeTransformer implements BankRevenueInterface
public function transformTransaction($transaction) public function transformTransaction($transaction)
{ {
nlog($transaction);
return [ return [
'transaction_id' => $transaction->id, 'transaction_id' => $transaction->id,
'amount' => $transaction->amount->amount, 'amount' => $transaction->amount->amount,
@ -143,7 +143,7 @@ class IncomeTransformer implements BankRevenueInterface
'date' => $transaction->date, 'date' => $transaction->date,
'bank_account_id' => $transaction->accountId, 'bank_account_id' => $transaction->accountId,
'description' => $transaction->description->original, 'description' => $transaction->description->original,
'base_type' => $transaction->baseType, 'base_type' => property_exists($transaction, 'baseType') ? $transaction->baseType : '',
]; ];
} }

View File

@ -29,6 +29,7 @@ use App\Models\Payment;
use App\Services\Bank\BankService; use App\Services\Bank\BankService;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
@ -39,7 +40,7 @@ use Illuminate\Support\Carbon;
class MatchBankTransactions implements ShouldQueue class MatchBankTransactions implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GeneratesCounter; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, GeneratesCounter, MakesHash;
private int $company_id; private int $company_id;
@ -54,6 +55,11 @@ class MatchBankTransactions implements ShouldQueue
private BankTransaction $bt; private BankTransaction $bt;
private $categories; private $categories;
private float $available_balance = 0;
private array $attachable_invoices = [];
/** /**
* Create a new job instance. * Create a new job instance.
*/ */
@ -75,7 +81,6 @@ class MatchBankTransactions implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
nlog("match bank transactions");
MultiDB::setDb($this->db); MultiDB::setDb($this->db);
@ -88,11 +93,9 @@ nlog("match bank transactions");
if($_categories) if($_categories)
$this->categories = collect($_categories->transactionCategory); $this->categories = collect($_categories->transactionCategory);
nlog($this->input);
foreach($this->input as $match) foreach($this->input as $match)
{ {
if(array_key_exists('invoice_id', $match) && strlen($match['invoice_id']) > 1) if(array_key_exists('invoice_ids', $match) && strlen($match['invoice_ids']) > 1)
$this->matchInvoicePayment($match); $this->matchInvoicePayment($match);
else else
$this->matchExpense($match); $this->matchExpense($match);
@ -100,24 +103,54 @@ nlog($this->input);
} }
private function getInvoices(string $invoice_hashed_ids)
{
$collection = collect();
$invoices = explode(",", $invoice_hashed_ids);
if(count($invoices) >= 1)
{
foreach($invoices as $invoice){
if(is_string($invoice) && strlen($invoice) > 1)
$collection->push($this->decodePrimaryKey($invoice));
}
}
return $collection;
}
private function checkPayable($invoices) :bool
{
foreach($invoices as $invoice){
if(!$invoice->isPayable())
return false;
}
return true;
}
private function matchInvoicePayment(array $match) :void private function matchInvoicePayment(array $match) :void
{ {
$this->bt = BankTransaction::find($match['id']); $this->bt = BankTransaction::find($match['id']);
nlog($this->bt->toArray()); $_invoices = Invoice::withTrashed()->find($this->getInvoices($match['invoice_ids']));
$_invoice = Invoice::withTrashed()->find($match['invoice_id']);
nlog($_invoice->toArray());
if(array_key_exists('amount', $match) && $match['amount'] > 0) if(array_key_exists('amount', $match) && $match['amount'] > 0)
$amount = $match['amount']; $amount = $match['amount'];
else else
$amount = $this->bt->amount; $amount = $this->bt->amount;
if($_invoice && $_invoice->isPayable()){ if($_invoices && $this->checkPayable($_invoices)){
$this->createPayment($match['invoice_id'], $amount); $this->createPayment($_invoices, $amount);
} }
@ -139,22 +172,42 @@ nlog($_invoice->toArray());
} }
private function createPayment(int $invoice_id, float $amount) :void private function createPayment($invoices, float $amount) :void
{ {
nlog("creating payment"); $this->available_balance = $amount;
\DB::connection(config('database.default'))->transaction(function () use($invoice_id, $amount) { \DB::connection(config('database.default'))->transaction(function () use($invoices) {
$this->invoice = Invoice::withTrashed()->where('id', $invoice_id)->lockForUpdate()->first(); $invoices->each(function ($invoice) use ($invoices){
$this->invoice = Invoice::withTrashed()->where('id', $invoice->id)->lockForUpdate()->first();
if($invoices->count() == 1){
$_amount = $this->available_balance;
}
elseif($invoices->count() > 1 && floatval($this->invoice->balance) < floatval($this->available_balance) && $this->available_balance > 0)
{
$_amount = $this->invoice->balance;
$this->available_balance = $this->available_balance - $this->invoice->balance;
}
elseif($invoices->count() > 1 && floatval($this->invoice->balance) > floatval($this->available_balance) && $this->available_balance > 0)
{
$_amount = $this->available_balance;
$this->available_balance = 0;
}
$this->attachable_invoices[] = ['id' => $this->invoice->id, 'amount' => $_amount];
$this->invoice $this->invoice
->service() ->service()
->setExchangeRate() ->setExchangeRate()
->updateBalance($amount * -1) ->updateBalance($_amount * -1)
->updatePaidToDate($amount) ->updatePaidToDate($_amount)
->setCalculatedStatus() ->setCalculatedStatus()
->save(); ->save();
});
}, 1); }, 1);
/* Create Payment */ /* Create Payment */
@ -164,7 +217,7 @@ nlog("creating payment");
$payment->applied = $amount; $payment->applied = $amount;
$payment->status_id = Payment::STATUS_COMPLETED; $payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id; $payment->client_id = $this->invoice->client_id;
$payment->transaction_reference = $this->bt->transaction_id; $payment->transaction_reference = $this->bt->description;
$payment->transaction_id = $this->bt->transaction_id; $payment->transaction_id = $this->bt->transaction_id;
$payment->currency_id = $this->harvestCurrencyId(); $payment->currency_id = $this->harvestCurrencyId();
$payment->is_manual = false; $payment->is_manual = false;
@ -176,8 +229,6 @@ nlog("creating payment");
$payment->saveQuietly(); $payment->saveQuietly();
nlog($payment->toArray());
$payment->service()->applyNumber()->save(); $payment->service()->applyNumber()->save();
if($payment->client->getSetting('send_email_on_mark_paid')) if($payment->client->getSetting('send_email_on_mark_paid'))
@ -186,10 +237,15 @@ nlog($payment->toArray());
$this->setExchangeRate($payment); $this->setExchangeRate($payment);
/* Create a payment relationship to the invoice entity */ /* Create a payment relationship to the invoice entity */
$payment->invoices()->attach($this->invoice->id, [ foreach($this->attachable_invoices as $attachable_invoice)
'amount' => $amount, {
$payment->invoices()->attach($attachable_invoice['id'], [
'amount' => $attachable_invoice['amount'],
]); ]);
}
event('eloquent.created: App\Models\Payment', $payment); event('eloquent.created: App\Models\Payment', $payment);
$this->invoice->next_send_date = null; $this->invoice->next_send_date = null;

View File

@ -33,6 +33,8 @@ class BankService implements ShouldQueue
private $invoices; private $invoices;
public $deleteWhenMissingModels = true;
public function __construct($company_id, $db) public function __construct($company_id, $db)
{ {
$this->company_id = $company_id; $this->company_id = $company_id;
@ -58,8 +60,7 @@ class BankService implements ShouldQueue
{ {
BankTransaction::where('company_id', $this->company->id) BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false) ->where('status_id', BankTransaction::STATUS_UNMATCHED)
->where('provisional_match', false)
->cursor() ->cursor()
->each(function ($bt){ ->each(function ($bt){
@ -71,8 +72,8 @@ class BankService implements ShouldQueue
if($invoice) if($invoice)
{ {
$bt->invoice_id = $invoice->id; $bt->invoice_ids = $invoice->hashed_id;
$bt->provisional_match = true; $bt->status_id = BankTransaction::STATUS_MATCHED;
$bt->save(); $bt->save();
} }

View File

@ -121,13 +121,13 @@ class YodleeApiTest extends TestCase
$data = [ $data = [
[ [
'id' => $bt->id, 'id' => $bt->id,
'invoice_id' => $invoice->id 'invoice_ids' => $invoice->hashed_id
] ]
]; ];
MatchBankTransactions::dispatchSync($this->company->id, $this->company->db, $data); MatchBankTransactions::dispatchSync($this->company->id, $this->company->db, $data);
$payment = Payment::where('transaction_reference', '123456')->first(); $payment = Payment::where('transaction_reference', $bt->description)->first();
$this->assertNotNull($payment); $this->assertNotNull($payment);
@ -202,11 +202,11 @@ class YodleeApiTest extends TestCase
BankService::dispatchSync($this->company->id, $this->company->db); BankService::dispatchSync($this->company->id, $this->company->db);
$bt = BankTransaction::where('invoice_id', $this->invoice->id)->first(); $bt = BankTransaction::where('invoice_ids', $this->invoice->hashed_id)->first();
$this->assertNotNull($bt); $this->assertNotNull($bt);
$this->assertEquals(1, $bt->provisional_match); $this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
} }

View File

@ -54,8 +54,7 @@ class YodleeBankTransactionTest extends TestCase
$invoices = Invoice::where('company_id', $this->company->id)->get(); $invoices = Invoice::where('company_id', $this->company->id)->get();
BankTransaction::where('company_id', $this->company->id) BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false) ->where('status_id', BankTransaction::STATUS_UNMATCHED)
->where('provisional_match', false)
->cursor() ->cursor()
->each(function ($bt) use($invoices){ ->each(function ($bt) use($invoices){
@ -67,15 +66,15 @@ class YodleeBankTransactionTest extends TestCase
if($invoice) if($invoice)
{ {
$bt->invoice_id = $invoice->id; $bt->invoice_ids = $invoice->hashed_id;
$bt->provisional_match = $invoice->id; $bt->status_id = BankTransaction::STATUS_MATCHED;
$bt->save(); $bt->save();
} }
}); });
$this->assertTrue(BankTransaction::where('invoice_id', $this->invoice->id)->exists()); $this->assertTrue(BankTransaction::where('invoice_ids', $this->invoice->hashed_id)->exists());
} }
@ -96,8 +95,7 @@ class YodleeBankTransactionTest extends TestCase
$invoices = Invoice::where('company_id', $this->company->id)->get(); $invoices = Invoice::where('company_id', $this->company->id)->get();
BankTransaction::where('company_id', $this->company->id) BankTransaction::where('company_id', $this->company->id)
->where('is_matched', false) ->where('status_id', BankTransaction::STATUS_UNMATCHED)
->where('provisional_match', false)
->cursor() ->cursor()
->each(function ($bt) use($invoices){ ->each(function ($bt) use($invoices){
@ -109,15 +107,15 @@ class YodleeBankTransactionTest extends TestCase
if($invoice) if($invoice)
{ {
$bt->invoice_id = $invoice->id; $bt->invoice_ids = $invoice->hashed_id;
$bt->provisional_match = $invoice->id; $bt->status_id = BankTransaction::STATUS_MATCHED;
$bt->save(); $bt->save();
} }
}); });
$this->assertTrue(BankTransaction::where('invoice_id', $this->invoice->id)->exists()); $this->assertTrue(BankTransaction::where('invoice_ids', $this->invoice->hashed_id)->exists());
} }