mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Extend transaction model for #351
This commit is contained in:
@@ -64,7 +64,7 @@ class Journal implements JournalInterface
|
|||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function storeTransaction(TransactionJournal $journal, array $transaction): Collection
|
public function storeTransaction(TransactionJournal $journal, array $transaction, int $identifier): Collection
|
||||||
{
|
{
|
||||||
// store accounts (depends on type)
|
// store accounts (depends on type)
|
||||||
list($sourceAccount, $destinationAccount) = $this->storeAccounts($journal->transactionType->type, $transaction);
|
list($sourceAccount, $destinationAccount) = $this->storeAccounts($journal->transactionType->type, $transaction);
|
||||||
@@ -73,11 +73,11 @@ class Journal implements JournalInterface
|
|||||||
/** @var Transaction $one */
|
/** @var Transaction $one */
|
||||||
$one = Transaction::create(
|
$one = Transaction::create(
|
||||||
['account_id' => $sourceAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'] * -1,
|
['account_id' => $sourceAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'] * -1,
|
||||||
'description' => $transaction['description']]
|
'description' => $transaction['description'], 'identifier' => $identifier]
|
||||||
);
|
);
|
||||||
$two = Transaction::create(
|
$two = Transaction::create(
|
||||||
['account_id' => $destinationAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'],
|
['account_id' => $destinationAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'],
|
||||||
'description' => $transaction['description']]
|
'description' => $transaction['description'], 'identifier' => $identifier]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (strlen($transaction['category']) > 0) {
|
if (strlen($transaction['category']) > 0) {
|
||||||
@@ -118,8 +118,10 @@ class Journal implements JournalInterface
|
|||||||
// delete original transactions, and recreate them.
|
// delete original transactions, and recreate them.
|
||||||
$journal->transactions()->delete();
|
$journal->transactions()->delete();
|
||||||
|
|
||||||
|
$identifier = 0;
|
||||||
foreach ($data['transactions'] as $transaction) {
|
foreach ($data['transactions'] as $transaction) {
|
||||||
$this->storeTransaction($journal, $transaction);
|
$this->storeTransaction($journal, $transaction, $identifier);
|
||||||
|
$identifier++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$journal->completed = true;
|
$journal->completed = true;
|
||||||
|
@@ -467,7 +467,7 @@ class TestData
|
|||||||
private function createMultiDeposits()
|
private function createMultiDeposits()
|
||||||
{
|
{
|
||||||
foreach ($this->data['multi-deposits'] as $deposit) {
|
foreach ($this->data['multi-deposits'] as $deposit) {
|
||||||
$journalId = DB::table('transaction_journals')->insertGetId(
|
$journalId = DB::table('transaction_journals')->insertGetId(
|
||||||
[
|
[
|
||||||
'created_at' => $this->time,
|
'created_at' => $this->time,
|
||||||
'updated_at' => $this->time,
|
'updated_at' => $this->time,
|
||||||
@@ -485,6 +485,7 @@ class TestData
|
|||||||
'tag_count' => 0,
|
'tag_count' => 0,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier = 0;
|
||||||
foreach ($deposit['source_ids'] as $index => $source) {
|
foreach ($deposit['source_ids'] as $index => $source) {
|
||||||
$description = $deposit['description'] . ' (#' . ($index + 1) . ')';
|
$description = $deposit['description'] . ' (#' . ($index + 1) . ')';
|
||||||
$amount = $deposit['amounts'][$index];
|
$amount = $deposit['amounts'][$index];
|
||||||
@@ -496,6 +497,7 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount,
|
'amount' => $amount,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$second = DB::table('transactions')->insertGetId(
|
$second = DB::table('transactions')->insertGetId(
|
||||||
@@ -506,8 +508,10 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount * -1,
|
'amount' => $amount * -1,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier++;
|
||||||
// link first and second to budget and category, if present.
|
// link first and second to budget and category, if present.
|
||||||
|
|
||||||
if (isset($deposit['category_ids'][$index])) {
|
if (isset($deposit['category_ids'][$index])) {
|
||||||
@@ -531,7 +535,7 @@ class TestData
|
|||||||
private function createMultiTransfers()
|
private function createMultiTransfers()
|
||||||
{
|
{
|
||||||
foreach ($this->data['multi-transfers'] as $transfer) {
|
foreach ($this->data['multi-transfers'] as $transfer) {
|
||||||
$journalId = DB::table('transaction_journals')->insertGetId(
|
$journalId = DB::table('transaction_journals')->insertGetId(
|
||||||
[
|
[
|
||||||
'created_at' => $this->time,
|
'created_at' => $this->time,
|
||||||
'updated_at' => $this->time,
|
'updated_at' => $this->time,
|
||||||
@@ -549,6 +553,7 @@ class TestData
|
|||||||
'tag_count' => 0,
|
'tag_count' => 0,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier = 0;
|
||||||
foreach ($transfer['destination_ids'] as $index => $destination) {
|
foreach ($transfer['destination_ids'] as $index => $destination) {
|
||||||
$description = $transfer['description'] . ' (#' . ($index + 1) . ')';
|
$description = $transfer['description'] . ' (#' . ($index + 1) . ')';
|
||||||
$amount = $transfer['amounts'][$index];
|
$amount = $transfer['amounts'][$index];
|
||||||
@@ -561,6 +566,7 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount * -1,
|
'amount' => $amount * -1,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$second = DB::table('transactions')->insertGetId(
|
$second = DB::table('transactions')->insertGetId(
|
||||||
@@ -571,9 +577,10 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount,
|
'amount' => $amount,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier++;
|
||||||
if (isset($transfer['category_ids'][$index])) {
|
if (isset($transfer['category_ids'][$index])) {
|
||||||
DB::table('category_transaction')->insert(
|
DB::table('category_transaction')->insert(
|
||||||
[
|
[
|
||||||
@@ -598,7 +605,7 @@ class TestData
|
|||||||
private function createMultiWithdrawals()
|
private function createMultiWithdrawals()
|
||||||
{
|
{
|
||||||
foreach ($this->data['multi-withdrawals'] as $withdrawal) {
|
foreach ($this->data['multi-withdrawals'] as $withdrawal) {
|
||||||
$journalId = DB::table('transaction_journals')->insertGetId(
|
$journalId = DB::table('transaction_journals')->insertGetId(
|
||||||
[
|
[
|
||||||
'created_at' => $this->time,
|
'created_at' => $this->time,
|
||||||
'updated_at' => $this->time,
|
'updated_at' => $this->time,
|
||||||
@@ -616,6 +623,7 @@ class TestData
|
|||||||
'tag_count' => 0,
|
'tag_count' => 0,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier = 0;
|
||||||
foreach ($withdrawal['destination_ids'] as $index => $destination) {
|
foreach ($withdrawal['destination_ids'] as $index => $destination) {
|
||||||
$description = $withdrawal['description'] . ' (#' . ($index + 1) . ')';
|
$description = $withdrawal['description'] . ' (#' . ($index + 1) . ')';
|
||||||
$amount = $withdrawal['amounts'][$index];
|
$amount = $withdrawal['amounts'][$index];
|
||||||
@@ -627,6 +635,7 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount * -1,
|
'amount' => $amount * -1,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$second = DB::table('transactions')->insertGetId(
|
$second = DB::table('transactions')->insertGetId(
|
||||||
@@ -637,8 +646,10 @@ class TestData
|
|||||||
'transaction_journal_id' => $journalId,
|
'transaction_journal_id' => $journalId,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'amount' => $amount,
|
'amount' => $amount,
|
||||||
|
'identifier' => $identifier,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
$identifier++;
|
||||||
// link first and second to budget and category, if present.
|
// link first and second to budget and category, if present.
|
||||||
if (isset($withdrawal['budget_ids'][$index])) {
|
if (isset($withdrawal['budget_ids'][$index])) {
|
||||||
DB::table('budget_transaction')->insert(
|
DB::table('budget_transaction')->insert(
|
||||||
|
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ExpandTransactionsTable
|
||||||
|
*/
|
||||||
|
class ExpandTransactionsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table(
|
||||||
|
'transactions', function (Blueprint $table) {
|
||||||
|
$table->smallInteger('identifier', false, false)->default(0);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user