Refactor bank transactions

This commit is contained in:
David Bomba 2022-09-21 13:21:52 +10:00
parent 08802deeb1
commit 3aa0df508e
4 changed files with 79 additions and 22 deletions

View File

@ -11,11 +11,19 @@
namespace App\Models; namespace App\Models;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
class BankTransaction extends BaseModel class BankTransaction extends BaseModel
{ {
use SoftDeletes; use SoftDeletes;
use MakesHash;
const STATUS_UNMATCHED = 1;
const STATUS_MATCHED = 2;
const STATUS_CONVERTED = 3;
protected $fillable = [ protected $fillable = [
]; ];
@ -23,6 +31,26 @@ class BankTransaction extends BaseModel
protected $dates = [ protected $dates = [
]; ];
public function getInvoiceIds()
{
$collection = collect();
$invoices = explode(",", $this->invoice_ids);
if(count($invoices) >= 1)
{
foreach($invoices as $invoice){
if(is_string($invoice) && strlen($invoice) > 1)
$collection->push($this->decodePrimaryKey($invoice));
}
}
return $collection;
}
public function getEntityType() public function getEntityType()
{ {
return self::class; return self::class;
@ -33,9 +61,9 @@ class BankTransaction extends BaseModel
return $this->belongsTo(Company::class); return $this->belongsTo(Company::class);
} }
public function invoice() public function vendor()
{ {
return $this->belongsTo(Invoice::class); return $this->belongsTo(Vendor::class);
} }
public function expense() public function expense()

View File

@ -16,6 +16,7 @@ use App\Models\BankTransaction;
use App\Models\Company; use App\Models\Company;
use App\Models\Expense; use App\Models\Expense;
use App\Models\Invoice; use App\Models\Invoice;
use App\Transformers\VendorTransformer;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
/** /**
@ -37,8 +38,8 @@ class BankTransactionTransformer extends EntityTransformer
protected $availableIncludes = [ protected $availableIncludes = [
'company', 'company',
'account', 'account',
'invoice',
'expense', 'expense',
'vendor',
'bank_account', 'bank_account',
]; ];
@ -56,16 +57,17 @@ class BankTransactionTransformer extends EntityTransformer
'currency_code' => (string) $bank_transaction->currency_code ?: '', 'currency_code' => (string) $bank_transaction->currency_code ?: '',
'account_type' => (string) $bank_transaction->account_type ?: '', 'account_type' => (string) $bank_transaction->account_type ?: '',
'category_id' => (int) $bank_transaction->category_id, 'category_id' => (int) $bank_transaction->category_id,
'ninja_category_id' => (int) $bank_transaction->ninja_category_id,
'category_type' => (string) $bank_transaction->category_type ?: '', 'category_type' => (string) $bank_transaction->category_type ?: '',
'date' => (string) $bank_transaction->date ?: '', 'date' => (string) $bank_transaction->date ?: '',
'bank_account_id' => (int) $bank_transaction->bank_account_id, 'bank_account_id' => (int) $bank_transaction->bank_account_id,
'status_id' => (int) $bank_transaction->status_id,
'description' => (string) $bank_transaction->description ?: '', 'description' => (string) $bank_transaction->description ?: '',
'base_type' => (string) $bank_transaction->base_type ?: '', 'base_type' => (string) $bank_transaction->base_type ?: '',
'invoice_id' => (string) $this->encodePrimaryKey($bank_transaction->invoice_id) ?: '', 'invoice_ids' => (string) $bank_transaction->invoice_ids ?: '',
'expense_id'=> (string) $this->encodePrimaryKey($bank_transaction->expense_id) ?: '', 'expense_id'=> (string) $this->encodePrimaryKey($bank_transaction->expense_id) ?: '',
'is_matched'=> (bool) $bank_transaction->is_matched, 'vendor_id'=> (string) $this->encodePrimaryKey($bank_transaction->vendor_id) ?: '',
'is_deleted' => (bool) $bank_transaction->is_deleted, 'is_deleted' => (bool) $bank_transaction->is_deleted,
'provisional_match' => (bool) $bank_transaction->provisional_match,
'created_at' => (int) $bank_transaction->created_at, 'created_at' => (int) $bank_transaction->created_at,
'updated_at' => (int) $bank_transaction->updated_at, 'updated_at' => (int) $bank_transaction->updated_at,
'archived_at' => (int) $bank_transaction->deleted_at, 'archived_at' => (int) $bank_transaction->deleted_at,
@ -86,13 +88,6 @@ class BankTransactionTransformer extends EntityTransformer
return $this->includeItem($bank_transaction->company, $transformer, Company::class); return $this->includeItem($bank_transaction->company, $transformer, Company::class);
} }
public function includeInvoice(BankTransaction $bank_transaction)
{
$transformer = new InvoiceTransformer($this->serializer);
return $this->includeItem($bank_transaction->invoice, $transformer, Invoice::class);
}
public function includeExpense(BankTransaction $bank_transaction) public function includeExpense(BankTransaction $bank_transaction)
{ {
$transformer = new ExpenseTransformer($this->serializer); $transformer = new ExpenseTransformer($this->serializer);
@ -100,4 +95,11 @@ class BankTransactionTransformer extends EntityTransformer
return $this->includeItem($bank_transaction->expense, $transformer, Expense::class); return $this->includeItem($bank_transaction->expense, $transformer, Expense::class);
} }
public function includeVendor(BankTransaction $bank_transaction)
{
$transformer = new VendorTransformer($this->serializer);
return $this->includeItem($bank_transaction->vendor, $transformer, Vendor::class);
}
} }

View File

@ -56,18 +56,19 @@ return new class extends Migration
$table->unsignedInteger('user_id'); $table->unsignedInteger('user_id');
$table->unsignedBigInteger('bank_integration_id'); $table->unsignedBigInteger('bank_integration_id');
$table->unsignedBigInteger('transaction_id')->index(); $table->unsignedBigInteger('transaction_id')->index();
$table->decimal('amount', 20, 6); $table->decimal('amount', 20, 6)->default(0);
$table->string('currency_code'); $table->string('currency_code')->nullable();
$table->string('account_type'); $table->string('account_type')->nullable();
$table->unsignedInteger('category_id'); $table->unsignedInteger('category_id')->nullable();
$table->unsignedInteger('ninja_category_id')->nullable();
$table->string('category_type')->index(); $table->string('category_type')->index();
$table->date('date'); $table->date('date')->nullable();
$table->unsignedBigInteger('bank_account_id'); $table->unsignedBigInteger('bank_account_id');
$table->text('description'); $table->text('description')->nullable();
$table->unsignedInteger('invoice_id')->nullable(); $table->text('invoice_ids')->default('');
$table->unsignedInteger('expense_id')->nullable(); $table->unsignedInteger('expense_id')->nullable();
$table->boolean('is_matched')->default(0); $table->unsignedInteger('vendor_id')->nullable();
$table->boolean('provisional_match')->default(0); $table->unsignedInteger('status_id')->default(1); //unmatched / matched / converted
$table->boolean('is_deleted')->default(0); $table->boolean('is_deleted')->default(0);
$table->timestamps(6); $table->timestamps(6);

View File

@ -11,6 +11,7 @@
namespace Tests\Unit; namespace Tests\Unit;
use App\Utils\Traits\MakesHash;
use Tests\TestCase; use Tests\TestCase;
/** /**
@ -19,6 +20,8 @@ use Tests\TestCase;
*/ */
class CompareCollectionTest extends TestCase class CompareCollectionTest extends TestCase
{ {
use MakesHash;
protected function setUp() :void protected function setUp() :void
{ {
parent::setUp(); parent::setUp();
@ -42,6 +45,29 @@ class CompareCollectionTest extends TestCase
$this->is_not_admin = false; $this->is_not_admin = false;
} }
public function testCollectionCreation()
{
$collection = collect();
$invoice_ids = '';
$invoices = explode(",", $invoice_ids);
if(count($invoices) >= 1)
{
foreach($invoices as $invoice){
if(is_string($invoice) && strlen($invoice) > 1)
$collection->push($this->decodePrimaryKey($invoice));
}
}
$this->assertEquals(0, $collection->count());
}
public function testCompareResultOfComparison() public function testCompareResultOfComparison()
{ {
$this->assertEquals(7, $this->map->count()); $this->assertEquals(7, $this->map->count());