diff --git a/app/Http/Requests/BankTransaction/BulkBankTransactionRequest.php b/app/Http/Requests/BankTransaction/BulkBankTransactionRequest.php index a95fb7fd23a3..74eed3c7a414 100644 --- a/app/Http/Requests/BankTransaction/BulkBankTransactionRequest.php +++ b/app/Http/Requests/BankTransaction/BulkBankTransactionRequest.php @@ -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' ]; } } diff --git a/app/Models/BankTransaction.php b/app/Models/BankTransaction.php index ed030f0a138f..f67e75a9d634 100644 --- a/app/Models/BankTransaction.php +++ b/app/Models/BankTransaction.php @@ -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(); } diff --git a/app/Repositories/BankTransactionRepository.php b/app/Repositories/BankTransactionRepository.php index 2f2cd569dc22..d9db85172a13 100644 --- a/app/Repositories/BankTransactionRepository.php +++ b/app/Repositories/BankTransactionRepository.php @@ -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(); + + } } diff --git a/app/Utils/Traits/ClientGroupSettingsSaver.php b/app/Utils/Traits/ClientGroupSettingsSaver.php index fa9d5fca2ae3..50afb4e09131 100644 --- a/app/Utils/Traits/ClientGroupSettingsSaver.php +++ b/app/Utils/Traits/ClientGroupSettingsSaver.php @@ -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) { diff --git a/phpstan.neon b/phpstan.neon index 11965c7dd020..8cd766f92d33 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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\(\)#' \ No newline at end of file + - '#Call to an undefined method [a-zA-Z0-9\\_]+::entityFilter\(\)#' + - '#Call to an undefined method [a-zA-Z0-9\\_]+::withTrashed()\(\)#' + - '#Undefined method#' \ No newline at end of file diff --git a/tests/Feature/BankTransactionApiTest.php b/tests/Feature/BankTransactionApiTest.php index c3c0373d483d..79496014fd28 100644 --- a/tests/Feature/BankTransactionApiTest.php +++ b/tests/Feature/BankTransactionApiTest.php @@ -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']); + } + }