1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00

Add unlink functionality for bank transactions

This commit is contained in:
David Bomba 2023-08-03 07:33:35 +10:00
parent 1b5bbb43da
commit 3aaeb8ff5c
6 changed files with 82 additions and 13 deletions

View File

@ -22,14 +22,17 @@ class BulkBankTransactionRequest extends Request
*/
public function authorize() : bool
{
return auth()->user()->isAdmin();
/** @var \App\Models\User $user **/
$user = auth()->user();
return $user->isAdmin();
}
public function rules()
public function rules(): array
{
return [
'ids' => 'required|bail|array',
'action' => 'in:archive,restore,delete,convert_matched'
'action' => 'in:archive,restore,delete,convert_matched,unlink'
];
}
}

View File

@ -130,22 +130,27 @@ class BankTransaction extends BaseModel
return self::class;
}
public function company()
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Company::class);
}
public function vendor()
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Vendor::class);
}
public function user()
public function expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Expense::class);
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class)->withTrashed();
}
public function bank_integration()
public function bank_integration(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(BankIntegration::class)->withTrashed();
}
@ -155,7 +160,7 @@ class BankTransaction extends BaseModel
return $this->belongsTo(Account::class)->withTrashed();
}
public function payment()
public function payment(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Payment::class)->withTrashed();
}

View File

@ -41,4 +41,24 @@ class BankTransactionRepository extends BaseRepository
$bts = (new MatchBankTransactions(auth()->user()->company()->id, auth()->user()->company()->db, $data))->handle();
}
public function unlink($bt)
{
if($bt->payment()->exists()){
$bt->payment->transaction_id = null;
$bt->payment_id = null;
}
if($bt->expense()->exists()) {
$bt->expense->transaction_id = null;
$bt->expense_id = null;
}
$bt->vendor_id = null;
$bt->status_id = 1;
$bt->invoice_ids = null;
$bt->ninja_category_id = null;
$bt->push();
}
}

View File

@ -31,7 +31,7 @@ trait ClientGroupSettingsSaver
* Works for groups|clients|companies
* @param array|object $settings The request input settings array
* @param object $entity The entity which the settings belongs to
* @return void
* @return array|object
*/
public function saveSettings($settings, $entity)
{

View File

@ -15,4 +15,6 @@ parameters:
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::company\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::entityFilter\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::entityFilter\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::withTrashed()\(\)#'
- '#Undefined method#'

View File

@ -11,12 +11,14 @@
namespace Tests\Feature;
use Tests\TestCase;
use Tests\MockAccountData;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -109,4 +111,41 @@ class BankTransactionApiTest extends TestCase
$this->assertTrue($arr['data'][0]['is_deleted']);
}
public function testBankTransactionUnlink()
{
BankTransaction::truncate();
$bi = BankIntegration::factory()->create([
'account_id' => $this->account->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
]);
$bank_transaction = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'payment_id' => $this->payment->id,
'expense_id' => $this->expense->id,
'invoice_ids' => $this->invoice->hashed_id,
]);
$data = [
'ids' => [$this->encodePrimaryKey($bank_transaction->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_transactions/bulk?action=unlink', $data);
$arr = $response->json();
$this->assertEquals(1, $arr['data'][0]['status_id']);
$this->assertEquals("", $arr['data'][0]['payment_id']);
$this->assertEquals("", $arr['data'][0]['invoice_ids']);
$this->assertEquals("", $arr['data'][0]['expense_id']);
}
}