mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 03:24:35 -04:00
Refactor to allow link multiple expenses to a transaction
This commit is contained in:
parent
aaa4c82d2e
commit
f780545b1b
@ -49,7 +49,7 @@ class ExpenseController extends BaseController
|
|||||||
protected $entity_transformer = ExpenseTransformer::class;
|
protected $entity_transformer = ExpenseTransformer::class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Expenseepository
|
* @var ExpensRepository
|
||||||
*/
|
*/
|
||||||
protected $expense_repo;
|
protected $expense_repo;
|
||||||
|
|
||||||
|
@ -104,8 +104,6 @@ class MatchBankTransactions implements ShouldQueue
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->input as $input) {
|
foreach ($this->input as $input) {
|
||||||
nlog($input);
|
|
||||||
|
|
||||||
if (array_key_exists('invoice_ids', $input) && strlen($input['invoice_ids']) >= 1) {
|
if (array_key_exists('invoice_ids', $input) && strlen($input['invoice_ids']) >= 1) {
|
||||||
$this->matchInvoicePayment($input);
|
$this->matchInvoicePayment($input);
|
||||||
} elseif (array_key_exists('payment_id', $input) && strlen($input['payment_id']) >= 1) {
|
} elseif (array_key_exists('payment_id', $input) && strlen($input['payment_id']) >= 1) {
|
||||||
@ -164,7 +162,7 @@ class MatchBankTransactions implements ShouldQueue
|
|||||||
$expense->transaction_id = $this->bt->id;
|
$expense->transaction_id = $this->bt->id;
|
||||||
$expense->save();
|
$expense->save();
|
||||||
|
|
||||||
$this->bt->expense_id = $expense->id;
|
$this->bt->expense_id = $this->coalesceExpenses($expense->hashed_id);
|
||||||
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
|
$this->bt->status_id = BankTransaction::STATUS_CONVERTED;
|
||||||
$this->bt->vendor_id = $expense->vendor_id;
|
$this->bt->vendor_id = $expense->vendor_id;
|
||||||
$this->bt->ninja_category_id = $expense->category_id;
|
$this->bt->ninja_category_id = $expense->category_id;
|
||||||
@ -176,6 +174,15 @@ class MatchBankTransactions implements ShouldQueue
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function coalesceExpenses($expense): string
|
||||||
|
{
|
||||||
|
|
||||||
|
if(!$this->bt->expense_id || strlen($this->bt->expense_id) < 1)
|
||||||
|
return $expense;
|
||||||
|
|
||||||
|
return collect(explode(",", $this->bt->expense_id))->push($expense)->implode(",");
|
||||||
|
}
|
||||||
|
|
||||||
private function linkPayment($input)
|
private function linkPayment($input)
|
||||||
{
|
{
|
||||||
$this->bt = BankTransaction::find($input['id']);
|
$this->bt = BankTransaction::find($input['id']);
|
||||||
@ -209,7 +216,10 @@ class MatchBankTransactions implements ShouldQueue
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
$_invoices = Invoice::withTrashed()->find($this->getInvoices($input['invoice_ids']));
|
$_invoices = Invoice::query()
|
||||||
|
->withTrashed()
|
||||||
|
->where('company_id', $this->bt->company_id)
|
||||||
|
->where('id',$this->getInvoices($input['invoice_ids']));
|
||||||
|
|
||||||
$amount = $this->bt->amount;
|
$amount = $this->bt->amount;
|
||||||
|
|
||||||
@ -249,7 +259,7 @@ class MatchBankTransactions implements ShouldQueue
|
|||||||
$expense->should_be_invoiced = $this->company->mark_expenses_invoiceable;
|
$expense->should_be_invoiced = $this->company->mark_expenses_invoiceable;
|
||||||
$expense->save();
|
$expense->save();
|
||||||
|
|
||||||
$this->bt->expense_id = $expense->id;
|
$this->bt->expense_id = $expense->hashed_id;
|
||||||
|
|
||||||
if (array_key_exists('vendor_id', $input)) {
|
if (array_key_exists('vendor_id', $input)) {
|
||||||
$this->bt->vendor_id = $input['vendor_id'];
|
$this->bt->vendor_id = $input['vendor_id'];
|
||||||
|
@ -1566,6 +1566,11 @@ class CompanyImport implements ShouldQueue
|
|||||||
return $this->encodePrimaryKey($encodeable);
|
return $this->encodePrimaryKey($encodeable);
|
||||||
})->implode(",");
|
})->implode(",");
|
||||||
|
|
||||||
|
$obj_array['expense_id'] = collect(explode(",", $obj_array['expense_id']))->map(function ($id) {
|
||||||
|
return $this->transformId('expenses', $id);
|
||||||
|
})->map(function ($encodeable) {
|
||||||
|
return $this->encodePrimaryKey($encodeable);
|
||||||
|
})->implode(",");
|
||||||
|
|
||||||
$new_obj->fill($obj_array);
|
$new_obj->fill($obj_array);
|
||||||
$new_obj->save(['timestamps' => false]);
|
$new_obj->save(['timestamps' => false]);
|
||||||
|
@ -82,11 +82,10 @@ class UpdateCalculatedFields
|
|||||||
$duration += $end_time - $start_time;
|
$duration += $end_time - $start_time;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return round(($duration/60/60), 0);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return round(($duration/60/60), 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,23 @@ class BankTransaction extends BaseModel
|
|||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getExpenseIds()
|
||||||
|
{
|
||||||
|
$collection = collect();
|
||||||
|
|
||||||
|
$expenses = explode(",", $this->expense_id);
|
||||||
|
|
||||||
|
if (count($expenses) >= 1) {
|
||||||
|
foreach ($expenses as $expense) {
|
||||||
|
if (is_string($expense) && strlen($expense) > 1) {
|
||||||
|
$collection->push($this->decodePrimaryKey($expense));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $collection;
|
||||||
|
}
|
||||||
|
|
||||||
public function getEntityType()
|
public function getEntityType()
|
||||||
{
|
{
|
||||||
return self::class;
|
return self::class;
|
||||||
|
@ -99,6 +99,20 @@ class ExpenseRepository extends BaseRepository
|
|||||||
return $expense;
|
return $expense;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function delete($expense) :Expense
|
||||||
|
{
|
||||||
|
if ($expense->transaction_id) {
|
||||||
|
$expense->transaction_id = null;
|
||||||
|
$expense->saveQuietly();
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::delete($expense);
|
||||||
|
|
||||||
|
return $expense;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle race conditions when creating expense numbers
|
* Handle race conditions when creating expense numbers
|
||||||
*
|
*
|
||||||
|
@ -123,7 +123,7 @@ class ProcessBankRules extends AbstractService
|
|||||||
$expense->should_be_invoiced = $this->bank_transaction->company->mark_expenses_invoiceable;
|
$expense->should_be_invoiced = $this->bank_transaction->company->mark_expenses_invoiceable;
|
||||||
$expense->save();
|
$expense->save();
|
||||||
|
|
||||||
$this->bank_transaction->expense_id = $expense->id;
|
$this->bank_transaction->expense_id = $expense->hashed_id;
|
||||||
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
|
$this->bank_transaction->status_id = BankTransaction::STATUS_CONVERTED;
|
||||||
$this->bank_transaction->save();
|
$this->bank_transaction->save();
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ class BankTransactionTransformer extends EntityTransformer
|
|||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param BankTransaction $bank_integration
|
* @param BankTransaction $bank_transaction
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function transform(BankTransaction $bank_transaction)
|
public function transform(BankTransaction $bank_transaction)
|
||||||
@ -63,7 +63,7 @@ class BankTransactionTransformer extends EntityTransformer
|
|||||||
'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_ids' => (string) $bank_transaction->invoice_ids ?: '',
|
'invoice_ids' => (string) $bank_transaction->invoice_ids ?: '',
|
||||||
'expense_id'=> (string) $this->encodePrimaryKey($bank_transaction->expense_id) ?: '',
|
'expense_id'=> (string) $bank_transaction->expense_id ?: '',
|
||||||
'payment_id'=> (string) $this->encodePrimaryKey($bank_transaction->payment_id) ?: '',
|
'payment_id'=> (string) $this->encodePrimaryKey($bank_transaction->payment_id) ?: '',
|
||||||
'vendor_id'=> (string) $this->encodePrimaryKey($bank_transaction->vendor_id) ?: '',
|
'vendor_id'=> (string) $this->encodePrimaryKey($bank_transaction->vendor_id) ?: '',
|
||||||
'bank_transaction_rule_id' => (string) $this->encodePrimaryKey($bank_transaction->bank_transaction_rule_id) ?: '',
|
'bank_transaction_rule_id' => (string) $this->encodePrimaryKey($bank_transaction->bank_transaction_rule_id) ?: '',
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\BankTransaction;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration
|
||||||
{
|
{
|
||||||
|
use MakesHash;
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*
|
*
|
||||||
@ -31,6 +34,17 @@ return new class extends Migration
|
|||||||
$table->unsignedInteger('current_hours')->nullable();
|
$table->unsignedInteger('current_hours')->nullable();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Schema::table('bank_transactions', function(Illuminate\Database\Schema\Blueprint $table) {
|
||||||
|
$table->text('expense_id')->default('')->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
BankTransaction::withTrashed()
|
||||||
|
->whereNotNull('expense_id')
|
||||||
|
->cursor()
|
||||||
|
->each(function ($transaction) {
|
||||||
|
$transaction->expense_id = $this->encodePrimaryKey($transaction->expense_id);
|
||||||
|
$transaction->save();
|
||||||
|
});
|
||||||
|
|
||||||
Company::query()
|
Company::query()
|
||||||
->cursor()
|
->cursor()
|
||||||
|
@ -112,7 +112,7 @@ class BankTransactionTest extends TestCase
|
|||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
|
|
||||||
$this->assertEquals($this->expense->refresh()->transaction_id, $bt->id);
|
$this->assertEquals($this->expense->refresh()->transaction_id, $bt->id);
|
||||||
$this->assertEquals($bt->refresh()->expense_id, $this->expense->id);
|
$this->assertEquals($this->expense->hashed_id, $bt->refresh()->expense_id);
|
||||||
$this->assertEquals($this->vendor->id, $bt->vendor_id);
|
$this->assertEquals($this->vendor->id, $bt->vendor_id);
|
||||||
$this->assertEquals(BankTransaction::STATUS_CONVERTED, $bt->status_id);
|
$this->assertEquals(BankTransaction::STATUS_CONVERTED, $bt->status_id);
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,13 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
use App\Models\Expense;
|
||||||
|
use Tests\MockAccountData;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Tests\MockAccountData;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use Tests\TestCase;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
@ -41,6 +42,22 @@ class ExpenseApiTest extends TestCase
|
|||||||
Model::reguard();
|
Model::reguard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTransactionIdClearedOnDelete()
|
||||||
|
{
|
||||||
|
$e = Expense::factory()->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'transaction_id' => '123',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertNotNull($e->transaction_id);
|
||||||
|
|
||||||
|
$expense_repo = app('App\Repositories\ExpenseRepository');
|
||||||
|
$e = $expense_repo->delete($e);
|
||||||
|
|
||||||
|
$this->assertNull($e->transaction_id);
|
||||||
|
}
|
||||||
|
|
||||||
public function testExpenseGetClientStatus()
|
public function testExpenseGetClientStatus()
|
||||||
{
|
{
|
||||||
$response = $this->withHeaders([
|
$response = $this->withHeaders([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user