1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Repositories/BankTransactionRepository.php

74 lines
2.0 KiB
PHP
Raw Normal View History

2022-08-11 08:26:47 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-08-11 08:26:47 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Repositories;
use App\Jobs\Bank\MatchBankTransactions;
2023-10-26 04:57:44 +02:00
use App\Models\BankTransaction;
use App\Models\Expense;
2022-08-11 08:26:47 +02:00
/**
* Class for bank transaction repository.
*/
class BankTransactionRepository extends BaseRepository
{
public function save($data, BankTransaction $bank_transaction)
2022-08-11 08:26:47 +02:00
{
2023-02-16 02:36:09 +01:00
if (array_key_exists('bank_integration_id', $data)) {
2022-10-24 00:14:42 +02:00
$bank_transaction->bank_integration_id = $data['bank_integration_id'];
2023-02-16 02:36:09 +01:00
}
2022-10-24 00:14:42 +02:00
2022-09-21 07:30:57 +02:00
$bank_transaction->fill($data);
$bank_transaction->save();
2022-08-11 08:26:47 +02:00
2022-11-20 03:55:19 +01:00
$bank_transaction->service()->processRules();
2022-11-10 11:57:55 +01:00
2022-11-20 03:55:19 +01:00
return $bank_transaction->fresh();
2022-08-11 08:26:47 +02:00
}
2022-11-24 21:52:47 +01:00
public function convert_matched($bank_transactions)
{
2023-10-09 06:16:35 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-02-16 02:36:09 +01:00
$data['transactions'] = $bank_transactions->map(function ($bt) {
return ['id' => $bt->id, 'invoice_ids' => $bt->invoice_ids, 'ninja_category_id' => $bt->ninja_category_id];
2022-11-24 21:52:47 +01:00
})->toArray();
2023-10-09 06:16:35 +02:00
$bts = (new MatchBankTransactions($user->company()->id, $user->company()->db, $data))->handle();
2022-11-24 21:52:47 +01:00
}
public function unlink($bt)
{
2023-10-26 04:57:44 +02:00
if($bt->payment()->exists()) {
$bt->payment->transaction_id = null;
$bt->payment_id = null;
}
2023-08-06 09:03:12 +02:00
$e = Expense::query()->whereIn('id', $this->transformKeys(explode(",", $bt->expense_id)))
->cursor()
2023-10-26 04:57:44 +02:00
->each(function ($expense) {
2024-01-14 05:05:00 +01:00
$expense->transaction_id = null;
$expense->saveQuietly();
});
2024-01-14 05:05:00 +01:00
$bt->expense_id = null;
$bt->vendor_id = null;
$bt->status_id = 1;
$bt->invoice_ids = null;
$bt->ninja_category_id = null;
$bt->push();
2024-01-14 05:05:00 +01:00
}
2022-08-11 08:26:47 +02:00
}