mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Auto commit for release 'develop' on 2025-03-14
This commit is contained in:
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\PiggyBank;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Events\Model\PiggyBank\ChangedAmount;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\PiggyBankFactory;
|
||||
@@ -69,9 +68,9 @@ trait ModifiesPiggyBanks
|
||||
$pivot->native_current_amount = null;
|
||||
|
||||
// also update native_current_amount.
|
||||
$userCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
$userCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
if ($userCurrency->id !== $piggyBank->transaction_currency_id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$pivot->native_current_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $pivot->current_amount);
|
||||
}
|
||||
@@ -92,9 +91,9 @@ trait ModifiesPiggyBanks
|
||||
$pivot->native_current_amount = null;
|
||||
|
||||
// also update native_current_amount.
|
||||
$userCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
$userCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
if ($userCurrency->id !== $piggyBank->transaction_currency_id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$pivot->native_current_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $pivot->current_amount);
|
||||
}
|
||||
@@ -126,8 +125,8 @@ trait ModifiesPiggyBanks
|
||||
Log::debug(sprintf('Maximum amount: %s', $maxAmount));
|
||||
}
|
||||
|
||||
$compare = bccomp($amount, $maxAmount);
|
||||
$result = $compare <= 0;
|
||||
$compare = bccomp($amount, $maxAmount);
|
||||
$result = $compare <= 0;
|
||||
|
||||
Log::debug(sprintf('Compare <= 0? %d, so canAddAmount is %s', $compare, var_export($result, true)));
|
||||
|
||||
@@ -142,7 +141,7 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(PiggyBank $piggyBank): bool
|
||||
{
|
||||
@@ -161,11 +160,11 @@ trait ModifiesPiggyBanks
|
||||
|
||||
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank
|
||||
{
|
||||
$repetition = $this->getRepetition($piggyBank);
|
||||
$repetition = $this->getRepetition($piggyBank);
|
||||
if (null === $repetition) {
|
||||
return $piggyBank;
|
||||
}
|
||||
$max = $piggyBank->target_amount;
|
||||
$max = $piggyBank->target_amount;
|
||||
if (1 === bccomp($amount, $max) && 0 !== bccomp($piggyBank->target_amount, '0')) {
|
||||
$amount = $max;
|
||||
}
|
||||
@@ -208,14 +207,14 @@ trait ModifiesPiggyBanks
|
||||
|
||||
public function update(PiggyBank $piggyBank, array $data): PiggyBank
|
||||
{
|
||||
$piggyBank = $this->updateProperties($piggyBank, $data);
|
||||
$piggyBank = $this->updateProperties($piggyBank, $data);
|
||||
if (array_key_exists('notes', $data)) {
|
||||
$this->updateNote($piggyBank, (string) $data['notes']);
|
||||
}
|
||||
|
||||
// update the order of the piggy bank:
|
||||
$oldOrder = $piggyBank->order;
|
||||
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
||||
$oldOrder = $piggyBank->order;
|
||||
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
||||
if ($oldOrder !== $newOrder) {
|
||||
$this->setOrder($piggyBank, $newOrder);
|
||||
}
|
||||
@@ -308,7 +307,7 @@ trait ModifiesPiggyBanks
|
||||
|
||||
return;
|
||||
}
|
||||
$dbNote = $piggyBank->notes()->first();
|
||||
$dbNote = $piggyBank->notes()->first();
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note();
|
||||
$dbNote->noteable()->associate($piggyBank);
|
||||
@@ -319,15 +318,16 @@ trait ModifiesPiggyBanks
|
||||
|
||||
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
|
||||
{
|
||||
$oldOrder = $piggyBank->order;
|
||||
$oldOrder = $piggyBank->order;
|
||||
// Log::debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
||||
if ($newOrder > $oldOrder) {
|
||||
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
|
||||
->where('piggy_banks.id', '!=', $piggyBank->id)
|
||||
->distinct()->decrement('piggy_banks.order');
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
|
||||
->where('piggy_banks.id', '!=', $piggyBank->id)
|
||||
->distinct()->decrement('piggy_banks.order')
|
||||
;
|
||||
|
||||
$piggyBank->order = $newOrder;
|
||||
Log::debug(sprintf('[1] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
||||
@@ -336,11 +336,12 @@ trait ModifiesPiggyBanks
|
||||
return true;
|
||||
}
|
||||
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
|
||||
->where('piggy_banks.id', '!=', $piggyBank->id)
|
||||
->distinct()->increment('piggy_banks.order');
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
|
||||
->where('piggy_banks.id', '!=', $piggyBank->id)
|
||||
->distinct()->increment('piggy_banks.order')
|
||||
;
|
||||
|
||||
$piggyBank->order = $newOrder;
|
||||
Log::debug(sprintf('[2] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
||||
@@ -361,7 +362,7 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
// if this account contains less than the amount, remove the current amount, update the amount and continue.
|
||||
$this->removeAmount($piggyBank, $account, $current);
|
||||
$amount = bcsub($amount, $current);
|
||||
$amount = bcsub($amount, $current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -41,7 +41,6 @@ use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Override;
|
||||
|
||||
/**
|
||||
* Class PiggyBankRepository.
|
||||
@@ -57,9 +56,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
Log::channel('audit')->info('Delete all piggy banks through destroyAll');
|
||||
|
||||
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->delete();
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->delete()
|
||||
;
|
||||
}
|
||||
|
||||
public function findPiggyBank(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
|
||||
@@ -90,9 +90,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
public function find(int $piggyBankId): ?PiggyBank
|
||||
{
|
||||
return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*'])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,14 +102,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
public function findByName(string $name): ?PiggyBank
|
||||
{
|
||||
return PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.name', $name)->first(['piggy_banks.*']);
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->where('piggy_banks.name', $name)->first(['piggy_banks.*'])
|
||||
;
|
||||
}
|
||||
|
||||
public function getAttachments(PiggyBank $piggyBank): Collection
|
||||
{
|
||||
$set = $piggyBank->attachments()->get();
|
||||
$set = $piggyBank->attachments()->get();
|
||||
|
||||
$disk = Storage::disk('upload');
|
||||
|
||||
@@ -155,15 +157,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
{
|
||||
app('log')->debug(sprintf('Now in getExactAmount(%d, %d)', $piggyBank->id, $journal->id));
|
||||
|
||||
$operator = null;
|
||||
$currency = null;
|
||||
$operator = null;
|
||||
$currency = null;
|
||||
|
||||
/** @var JournalRepositoryInterface $journalRepost */
|
||||
$journalRepost = app(JournalRepositoryInterface::class);
|
||||
$journalRepost = app(JournalRepositoryInterface::class);
|
||||
$journalRepost->setUser($this->user);
|
||||
|
||||
/** @var AccountRepositoryInterface $accountRepos */
|
||||
$accountRepos = app(AccountRepositoryInterface::class);
|
||||
$accountRepos = app(AccountRepositoryInterface::class);
|
||||
$accountRepos->setUser($this->user);
|
||||
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($this->user->userGroup);
|
||||
@@ -171,11 +173,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
app('log')->debug(sprintf('Piggy bank #%d currency is %s', $piggyBank->id, $piggyBank->transactionCurrency->code));
|
||||
|
||||
/** @var Transaction $source */
|
||||
$source = $journal->transactions()->with(['account'])->where('amount', '<', 0)->first();
|
||||
$source = $journal->transactions()->with(['account'])->where('amount', '<', 0)->first();
|
||||
|
||||
/** @var Transaction $destination */
|
||||
$destination = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first();
|
||||
$hits = 0;
|
||||
$destination = $journal->transactions()->with(['account'])->where('amount', '>', 0)->first();
|
||||
$hits = 0;
|
||||
foreach ($piggyBank->accounts as $account) {
|
||||
|
||||
// matches source, which means amount will be removed from piggy:
|
||||
@@ -207,7 +209,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
}
|
||||
// currency of the account + the piggy bank currency are almost the same.
|
||||
// which amount from the transaction matches?
|
||||
$amount = null;
|
||||
$amount = null;
|
||||
if ((int) $source->transaction_currency_id === $currency->id) {
|
||||
app('log')->debug('Use normal amount');
|
||||
$amount = app('steam')->{$operator}($source->amount); // @phpstan-ignore-line
|
||||
@@ -223,9 +225,9 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
}
|
||||
|
||||
app('log')->debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
|
||||
$currentAmount = $this->getCurrentAmount($piggyBank);
|
||||
$room = bcsub($piggyBank->target_amount, $currentAmount);
|
||||
$compare = bcmul($currentAmount, '-1');
|
||||
$currentAmount = $this->getCurrentAmount($piggyBank);
|
||||
$room = bcsub($piggyBank->target_amount, $currentAmount);
|
||||
$compare = bcmul($currentAmount, '-1');
|
||||
|
||||
if (0 === bccomp($piggyBank->target_amount, '0')) {
|
||||
// amount is zero? then the "room" is positive amount of we wish to add or remove.
|
||||
@@ -306,20 +308,23 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
public function getPiggyBanks(): Collection
|
||||
{
|
||||
$query = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id');
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
;
|
||||
if (null === $this->user) {
|
||||
$query->where('accounts.user_group_id', $this->userGroup->id);
|
||||
}
|
||||
if (null !== $this->user) {
|
||||
$query->where('accounts.user_id', $this->user->id);
|
||||
}
|
||||
|
||||
return $query
|
||||
->with(
|
||||
[
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->orderBy('piggy_banks.order', 'ASC')->distinct()->get(['piggy_banks.*']);
|
||||
->orderBy('piggy_banks.order', 'ASC')->distinct()->get(['piggy_banks.*'])
|
||||
;
|
||||
}
|
||||
|
||||
public function getRepetition(PiggyBank $piggyBank, bool $overrule = false): ?PiggyBankRepetition
|
||||
@@ -385,23 +390,24 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
return $balance;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
#[\Override]
|
||||
public function purgeAll(): void
|
||||
{
|
||||
PiggyBank::withTrashed()
|
||||
->whereNotNull('piggy_banks.deleted_at')
|
||||
->leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->with(
|
||||
[
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->delete();
|
||||
->whereNotNull('piggy_banks.deleted_at')
|
||||
->leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->with(
|
||||
[
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->delete()
|
||||
;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
#[\Override]
|
||||
public function resetOrder(): void
|
||||
{
|
||||
$factory = new PiggyBankFactory();
|
||||
@@ -412,19 +418,21 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface, UserGroupInte
|
||||
public function searchPiggyBank(string $query, int $limit): Collection
|
||||
{
|
||||
$search = PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->with(
|
||||
[
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->orderBy('piggy_banks.order', 'ASC')->distinct();
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
|
||||
->where('accounts.user_id', $this->user->id)
|
||||
->with(
|
||||
[
|
||||
'objectGroups',
|
||||
]
|
||||
)
|
||||
->orderBy('piggy_banks.order', 'ASC')->distinct()
|
||||
;
|
||||
if ('' !== $query) {
|
||||
$search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search->orderBy('piggy_banks.order', 'ASC')
|
||||
->orderBy('piggy_banks.name', 'ASC');
|
||||
->orderBy('piggy_banks.name', 'ASC')
|
||||
;
|
||||
|
||||
return $search->take($limit)->get(['piggy_banks.*']);
|
||||
}
|
||||
|
@@ -44,7 +44,6 @@ use Illuminate\Support\Collection;
|
||||
* @method checkUserGroupAccess(UserRoleEnum $role)
|
||||
* @method setUser(null|Authenticatable|User $user)
|
||||
* @method setUserGroupById(int $userGroupId)
|
||||
*
|
||||
*/
|
||||
interface PiggyBankRepositoryInterface
|
||||
{
|
||||
|
Reference in New Issue
Block a user