mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
Move method to correct repository.
This commit is contained in:
@@ -214,7 +214,7 @@ class AvailableBudgetController extends Controller
|
|||||||
$data['currency_id'] = $currency->id;
|
$data['currency_id'] = $currency->id;
|
||||||
|
|
||||||
|
|
||||||
$this->repository->updateAvailableBudget($availableBudget, $data);
|
$this->abRepository->updateAvailableBudget($availableBudget, $data);
|
||||||
$manager = new Manager;
|
$manager = new Manager;
|
||||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
@@ -233,7 +233,7 @@ class BudgetController extends Controller
|
|||||||
{
|
{
|
||||||
$data = $request->getAll();
|
$data = $request->getAll();
|
||||||
$data['budget'] = $budget;
|
$data['budget'] = $budget;
|
||||||
$budgetLimit = $this->repository->storeBudgetLimit($data);
|
$budgetLimit = $this->blRepository->storeBudgetLimit($data);
|
||||||
$manager = new Manager;
|
$manager = new Manager;
|
||||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
@@ -178,7 +178,7 @@ class BudgetLimitController extends Controller
|
|||||||
throw new FireflyException('Unknown budget.');
|
throw new FireflyException('Unknown budget.');
|
||||||
}
|
}
|
||||||
$data['budget'] = $budget;
|
$data['budget'] = $budget;
|
||||||
$budgetLimit = $this->repository->storeBudgetLimit($data);
|
$budgetLimit = $this->blRepository->storeBudgetLimit($data);
|
||||||
$manager = new Manager;
|
$manager = new Manager;
|
||||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
@@ -259,7 +259,7 @@ class BudgetLimitController extends Controller
|
|||||||
{
|
{
|
||||||
$data = $request->getAll();
|
$data = $request->getAll();
|
||||||
$data['budget'] = $budgetLimit->budget;
|
$data['budget'] = $budgetLimit->budget;
|
||||||
$budgetLimit = $this->repository->updateBudgetLimit($budgetLimit, $data);
|
$budgetLimit = $this->blRepository->updateBudgetLimit($budgetLimit, $data);
|
||||||
$manager = new Manager;
|
$manager = new Manager;
|
||||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
@@ -29,6 +29,7 @@ use FireflyIII\Http\Controllers\Controller;
|
|||||||
use FireflyIII\Http\Requests\BudgetIncomeRequest;
|
use FireflyIII\Http\Requests\BudgetIncomeRequest;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||||
@@ -46,6 +47,8 @@ class AmountController extends Controller
|
|||||||
|
|
||||||
/** @var AvailableBudgetRepositoryInterface */
|
/** @var AvailableBudgetRepositoryInterface */
|
||||||
private $abRepository;
|
private $abRepository;
|
||||||
|
/** @var BudgetLimitRepositoryInterface */
|
||||||
|
private $blRepository;
|
||||||
/** @var OperationsRepositoryInterface */
|
/** @var OperationsRepositoryInterface */
|
||||||
private $opsRepository;
|
private $opsRepository;
|
||||||
/** @var BudgetRepositoryInterface The budget repository */
|
/** @var BudgetRepositoryInterface The budget repository */
|
||||||
@@ -67,6 +70,7 @@ class AmountController extends Controller
|
|||||||
$this->repository = app(BudgetRepositoryInterface::class);
|
$this->repository = app(BudgetRepositoryInterface::class);
|
||||||
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
$this->opsRepository = app(OperationsRepositoryInterface::class);
|
||||||
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
|
$this->abRepository = app(AvailableBudgetRepositoryInterface::class);
|
||||||
|
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
@@ -99,7 +103,7 @@ class AmountController extends Controller
|
|||||||
$periodLength = $start->diffInDays($end) + 1; // absolute period length.
|
$periodLength = $start->diffInDays($end) + 1; // absolute period length.
|
||||||
|
|
||||||
// update limit amount:
|
// update limit amount:
|
||||||
$budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount);
|
$budgetLimit = $this->blRepository->updateLimitAmount($budget, $start, $end, $amount);
|
||||||
|
|
||||||
// calculate what the user has spent in current period.
|
// calculate what the user has spent in current period.
|
||||||
$spent = $this->opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
$spent = $this->opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
||||||
|
@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Budget;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\AvailableBudget;
|
use FireflyIII\Models\AvailableBudget;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
@@ -175,5 +176,34 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
|
|||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AvailableBudget $availableBudget
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return AvailableBudget
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget
|
||||||
|
{
|
||||||
|
$existing = $this->user->availableBudgets()
|
||||||
|
->where('transaction_currency_id', $data['currency_id'])
|
||||||
|
->where('start_date', $data['start']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('end_date', $data['end']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('id', '!=', $availableBudget->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (null !== $existing) {
|
||||||
|
throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id));
|
||||||
|
}
|
||||||
|
$availableBudget->transaction_currency_id = $data['currency_id'];
|
||||||
|
$availableBudget->start_date = $data['start'];
|
||||||
|
$availableBudget->end_date = $data['end'];
|
||||||
|
$availableBudget->amount = $data['amount'];
|
||||||
|
$availableBudget->save();
|
||||||
|
|
||||||
|
return $availableBudget;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -65,17 +65,6 @@ interface AvailableBudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
|
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TransactionCurrency $currency
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
* @param string $amount
|
|
||||||
*
|
|
||||||
* @return AvailableBudget
|
|
||||||
*/
|
|
||||||
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all available budget objects.
|
* Returns all available budget objects.
|
||||||
*
|
*
|
||||||
@@ -87,9 +76,27 @@ interface AvailableBudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection;
|
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TransactionCurrency $currency
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return AvailableBudget
|
||||||
|
*/
|
||||||
|
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
public function setUser(User $user): void;
|
public function setUser(User $user): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AvailableBudget $availableBudget
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return AvailableBudget
|
||||||
|
*/
|
||||||
|
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget;
|
||||||
|
|
||||||
}
|
}
|
@@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
@@ -230,4 +231,150 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
|||||||
{
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return BudgetLimit
|
||||||
|
*/
|
||||||
|
public function storeBudgetLimit(array $data): BudgetLimit
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $data['budget'];
|
||||||
|
|
||||||
|
// if no currency has been provided, use the user's default currency:
|
||||||
|
/** @var TransactionCurrencyFactory $factory */
|
||||||
|
$factory = app(TransactionCurrencyFactory::class);
|
||||||
|
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||||
|
if (null === $currency) {
|
||||||
|
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// find limit with same date range.
|
||||||
|
// if it exists, return that one.
|
||||||
|
$limit = $budget->budgetlimits()
|
||||||
|
->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.transaction_currency_id', $currency->id)
|
||||||
|
->get(['budget_limits.*'])->first();
|
||||||
|
if (null !== $limit) {
|
||||||
|
return $limit;
|
||||||
|
}
|
||||||
|
Log::debug('No existing budget limit, create a new one');
|
||||||
|
|
||||||
|
// or create one and return it.
|
||||||
|
$limit = new BudgetLimit;
|
||||||
|
$limit->budget()->associate($budget);
|
||||||
|
$limit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
||||||
|
$limit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
||||||
|
$limit->amount = $data['amount'];
|
||||||
|
$limit->transaction_currency_id = $currency->id;
|
||||||
|
$limit->save();
|
||||||
|
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
||||||
|
|
||||||
|
return $limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BudgetLimit $budgetLimit
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return BudgetLimit
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $data['budget'];
|
||||||
|
|
||||||
|
$budgetLimit->budget()->associate($budget);
|
||||||
|
$budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
||||||
|
$budgetLimit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
||||||
|
$budgetLimit->amount = $data['amount'];
|
||||||
|
|
||||||
|
// if no currency has been provided, use the user's default currency:
|
||||||
|
/** @var TransactionCurrencyFactory $factory */
|
||||||
|
$factory = app(TransactionCurrencyFactory::class);
|
||||||
|
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||||
|
if (null === $currency) {
|
||||||
|
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||||
|
}
|
||||||
|
$currency->enabled = true;
|
||||||
|
$currency->save();
|
||||||
|
$budgetLimit->transaction_currency_id = $currency->id;
|
||||||
|
|
||||||
|
$budgetLimit->save();
|
||||||
|
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
|
||||||
|
|
||||||
|
return $budgetLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return BudgetLimit|null
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit
|
||||||
|
{
|
||||||
|
// count the limits:
|
||||||
|
$limits = $budget->budgetlimits()
|
||||||
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||||
|
->get(['budget_limits.*'])->count();
|
||||||
|
Log::debug(sprintf('Found %d budget limits.', $limits));
|
||||||
|
|
||||||
|
// there might be a budget limit for these dates:
|
||||||
|
/** @var BudgetLimit $limit */
|
||||||
|
$limit = $budget->budgetlimits()
|
||||||
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||||
|
->first(['budget_limits.*']);
|
||||||
|
|
||||||
|
// if more than 1 limit found, delete the others:
|
||||||
|
if ($limits > 1 && null !== $limit) {
|
||||||
|
Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
|
||||||
|
$budget->budgetlimits()
|
||||||
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.id', '!=', $limit->id)->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete if amount is zero.
|
||||||
|
// Returns 0 if the two operands are equal,
|
||||||
|
// 1 if the left_operand is larger than the right_operand, -1 otherwise.
|
||||||
|
if (null !== $limit && bccomp($amount, '0') <= 0) {
|
||||||
|
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
|
||||||
|
try {
|
||||||
|
$limit->delete();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
Log::debug(sprintf('Could not delete limit: %s', $e->getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// update if exists:
|
||||||
|
if (null !== $limit) {
|
||||||
|
Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
|
||||||
|
$limit->amount = $amount;
|
||||||
|
$limit->save();
|
||||||
|
|
||||||
|
return $limit;
|
||||||
|
}
|
||||||
|
Log::debug('No existing budget limit, create a new one');
|
||||||
|
// or create one and return it.
|
||||||
|
$limit = new BudgetLimit;
|
||||||
|
$limit->budget()->associate($budget);
|
||||||
|
$limit->start_date = $start->startOfDay();
|
||||||
|
$limit->end_date = $end->startOfDay();
|
||||||
|
$limit->amount = $amount;
|
||||||
|
$limit->save();
|
||||||
|
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
|
||||||
|
|
||||||
|
return $limit;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -75,4 +75,29 @@ interface BudgetLimitRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function setUser(User $user): void;
|
public function setUser(User $user): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return BudgetLimit
|
||||||
|
*/
|
||||||
|
public function storeBudgetLimit(array $data): BudgetLimit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BudgetLimit $budgetLimit
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return BudgetLimit
|
||||||
|
*/
|
||||||
|
public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return BudgetLimit|null
|
||||||
|
*/
|
||||||
|
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit;
|
||||||
|
|
||||||
}
|
}
|
@@ -24,21 +24,14 @@ namespace FireflyIII\Repositories\Budget;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
|
||||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
|
||||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
|
||||||
use FireflyIII\Models\AvailableBudget;
|
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\RuleTrigger;
|
use FireflyIII\Models\RuleTrigger;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
|
||||||
use FireflyIII\Models\TransactionType;
|
|
||||||
use FireflyIII\Services\Internal\Destroy\BudgetDestroyService;
|
use FireflyIII\Services\Internal\Destroy\BudgetDestroyService;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
use Navigation;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetRepository.
|
* Class BudgetRepository.
|
||||||
@@ -294,50 +287,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $newBudget;
|
return $newBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return BudgetLimit
|
|
||||||
*/
|
|
||||||
public function storeBudgetLimit(array $data): BudgetLimit
|
|
||||||
{
|
|
||||||
$this->cleanupBudgets();
|
|
||||||
/** @var Budget $budget */
|
|
||||||
$budget = $data['budget'];
|
|
||||||
|
|
||||||
// if no currency has been provided, use the user's default currency:
|
|
||||||
/** @var TransactionCurrencyFactory $factory */
|
|
||||||
$factory = app(TransactionCurrencyFactory::class);
|
|
||||||
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
|
||||||
if (null === $currency) {
|
|
||||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
|
||||||
}
|
|
||||||
|
|
||||||
// find limit with same date range.
|
|
||||||
// if it exists, return that one.
|
|
||||||
$limit = $budget->budgetlimits()
|
|
||||||
->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.transaction_currency_id', $currency->id)
|
|
||||||
->get(['budget_limits.*'])->first();
|
|
||||||
if (null !== $limit) {
|
|
||||||
return $limit;
|
|
||||||
}
|
|
||||||
Log::debug('No existing budget limit, create a new one');
|
|
||||||
|
|
||||||
// or create one and return it.
|
|
||||||
$limit = new BudgetLimit;
|
|
||||||
$limit->budget()->associate($budget);
|
|
||||||
$limit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
|
||||||
$limit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
|
||||||
$limit->amount = $data['amount'];
|
|
||||||
$limit->transaction_currency_id = $currency->id;
|
|
||||||
$limit->save();
|
|
||||||
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
|
||||||
|
|
||||||
return $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@@ -357,142 +306,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $budget;
|
return $budget;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO only used in the API.
|
|
||||||
*
|
|
||||||
* @param AvailableBudget $availableBudget
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return AvailableBudget
|
|
||||||
* @throws FireflyException
|
|
||||||
*/
|
|
||||||
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget
|
|
||||||
{
|
|
||||||
$existing = $this->user->availableBudgets()
|
|
||||||
->where('transaction_currency_id', $data['currency_id'])
|
|
||||||
->where('start_date', $data['start']->format('Y-m-d 00:00:00'))
|
|
||||||
->where('end_date', $data['end']->format('Y-m-d 00:00:00'))
|
|
||||||
->where('id', '!=', $availableBudget->id)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if (null !== $existing) {
|
|
||||||
throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id));
|
|
||||||
}
|
|
||||||
$availableBudget->transaction_currency_id = $data['currency_id'];
|
|
||||||
$availableBudget->start_date = $data['start'];
|
|
||||||
$availableBudget->end_date = $data['end'];
|
|
||||||
$availableBudget->amount = $data['amount'];
|
|
||||||
$availableBudget->save();
|
|
||||||
|
|
||||||
return $availableBudget;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param BudgetLimit $budgetLimit
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return BudgetLimit
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit
|
|
||||||
{
|
|
||||||
$this->cleanupBudgets();
|
|
||||||
/** @var Budget $budget */
|
|
||||||
$budget = $data['budget'];
|
|
||||||
|
|
||||||
$budgetLimit->budget()->associate($budget);
|
|
||||||
$budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
|
||||||
$budgetLimit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
|
||||||
$budgetLimit->amount = $data['amount'];
|
|
||||||
|
|
||||||
// if no currency has been provided, use the user's default currency:
|
|
||||||
/** @var TransactionCurrencyFactory $factory */
|
|
||||||
$factory = app(TransactionCurrencyFactory::class);
|
|
||||||
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
|
||||||
if (null === $currency) {
|
|
||||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
|
||||||
}
|
|
||||||
$currency->enabled = true;
|
|
||||||
$currency->save();
|
|
||||||
$budgetLimit->transaction_currency_id = $currency->id;
|
|
||||||
|
|
||||||
$budgetLimit->save();
|
|
||||||
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
|
|
||||||
|
|
||||||
return $budgetLimit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Budget $budget
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
* @param string $amount
|
|
||||||
*
|
|
||||||
* @return BudgetLimit|null
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit
|
|
||||||
{
|
|
||||||
$this->cleanupBudgets();
|
|
||||||
// count the limits:
|
|
||||||
$limits = $budget->budgetlimits()
|
|
||||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
|
||||||
->get(['budget_limits.*'])->count();
|
|
||||||
Log::debug(sprintf('Found %d budget limits.', $limits));
|
|
||||||
|
|
||||||
// there might be a budget limit for these dates:
|
|
||||||
/** @var BudgetLimit $limit */
|
|
||||||
$limit = $budget->budgetlimits()
|
|
||||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
|
||||||
->first(['budget_limits.*']);
|
|
||||||
|
|
||||||
// if more than 1 limit found, delete the others:
|
|
||||||
if ($limits > 1 && null !== $limit) {
|
|
||||||
Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
|
|
||||||
$budget->budgetlimits()
|
|
||||||
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
|
||||||
->where('budget_limits.id', '!=', $limit->id)->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete if amount is zero.
|
|
||||||
// Returns 0 if the two operands are equal,
|
|
||||||
// 1 if the left_operand is larger than the right_operand, -1 otherwise.
|
|
||||||
if (null !== $limit && bccomp($amount, '0') <= 0) {
|
|
||||||
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
|
|
||||||
try {
|
|
||||||
$limit->delete();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
Log::debug(sprintf('Could not delete limit: %s', $e->getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// update if exists:
|
|
||||||
if (null !== $limit) {
|
|
||||||
Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
|
|
||||||
$limit->amount = $amount;
|
|
||||||
$limit->save();
|
|
||||||
|
|
||||||
return $limit;
|
|
||||||
}
|
|
||||||
Log::debug('No existing budget limit, create a new one');
|
|
||||||
// or create one and return it.
|
|
||||||
$limit = new BudgetLimit;
|
|
||||||
$limit->budget()->associate($budget);
|
|
||||||
$limit->start_date = $start->startOfDay();
|
|
||||||
$limit->end_date = $end->startOfDay();
|
|
||||||
$limit->amount = $amount;
|
|
||||||
$limit->save();
|
|
||||||
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
|
|
||||||
|
|
||||||
return $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $oldName
|
* @param string $oldName
|
||||||
* @param string $newName
|
* @param string $newName
|
||||||
|
@@ -23,10 +23,7 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Repositories\Budget;
|
namespace FireflyIII\Repositories\Budget;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Models\AvailableBudget;
|
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
@@ -125,16 +122,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function setUser(User $user);
|
public function setUser(User $user);
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
@@ -142,13 +129,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function store(array $data): Budget;
|
public function store(array $data): Budget;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return BudgetLimit
|
|
||||||
*/
|
|
||||||
public function storeBudgetLimit(array $data): BudgetLimit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@@ -156,31 +136,4 @@ interface BudgetRepositoryInterface
|
|||||||
* @return Budget
|
* @return Budget
|
||||||
*/
|
*/
|
||||||
public function update(Budget $budget, array $data): Budget;
|
public function update(Budget $budget, array $data): Budget;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param AvailableBudget $availableBudget
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return AvailableBudget
|
|
||||||
*/
|
|
||||||
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param BudgetLimit $budgetLimit
|
|
||||||
* @param array $data
|
|
||||||
*
|
|
||||||
* @return BudgetLimit
|
|
||||||
*/
|
|
||||||
public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Budget $budget
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
* @param string $amount
|
|
||||||
*
|
|
||||||
* @return BudgetLimit|null
|
|
||||||
*/
|
|
||||||
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit;
|
|
||||||
}
|
}
|
||||||
|
@@ -52,58 +52,6 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array
|
|
||||||
{
|
|
||||||
/** @var GroupCollectorInterface $collector */
|
|
||||||
$collector = app(GroupCollectorInterface::class);
|
|
||||||
|
|
||||||
$collector->setUser($this->user);
|
|
||||||
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
|
||||||
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$collector->setAccounts($accounts);
|
|
||||||
}
|
|
||||||
$journals = $collector->getExtractedJournals();
|
|
||||||
$return = [];
|
|
||||||
$total = [];
|
|
||||||
$currencies = [];
|
|
||||||
/** @var array $journal */
|
|
||||||
foreach ($journals as $journal) {
|
|
||||||
$code = $journal['currency_code'];
|
|
||||||
if (!isset($currencies[$code])) {
|
|
||||||
$currencies[$code] = [
|
|
||||||
'id' => $journal['currency_id'],
|
|
||||||
'name' => $journal['currency_name'],
|
|
||||||
'symbol' => $journal['currency_symbol'],
|
|
||||||
'decimal_places' => $journal['currency_decimal_places'],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
|
|
||||||
}
|
|
||||||
foreach ($total as $code => $spent) {
|
|
||||||
/** @var TransactionCurrency $currency */
|
|
||||||
$currency = $currencies[$code];
|
|
||||||
$return[] = [
|
|
||||||
'currency_id' => $currency['id'],
|
|
||||||
'currency_code' => $code,
|
|
||||||
'currency_name' => $currency['name'],
|
|
||||||
'currency_symbol' => $currency['symbol'],
|
|
||||||
'currency_decimal_places' => $currency['decimal_places'],
|
|
||||||
'amount' => $spent,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@@ -157,4 +105,56 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface
|
|||||||
{
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array
|
||||||
|
{
|
||||||
|
/** @var GroupCollectorInterface $collector */
|
||||||
|
$collector = app(GroupCollectorInterface::class);
|
||||||
|
|
||||||
|
$collector->setUser($this->user);
|
||||||
|
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
||||||
|
|
||||||
|
if ($accounts->count() > 0) {
|
||||||
|
$collector->setAccounts($accounts);
|
||||||
|
}
|
||||||
|
$journals = $collector->getExtractedJournals();
|
||||||
|
$return = [];
|
||||||
|
$total = [];
|
||||||
|
$currencies = [];
|
||||||
|
/** @var array $journal */
|
||||||
|
foreach ($journals as $journal) {
|
||||||
|
$code = $journal['currency_code'];
|
||||||
|
if (!isset($currencies[$code])) {
|
||||||
|
$currencies[$code] = [
|
||||||
|
'id' => $journal['currency_id'],
|
||||||
|
'name' => $journal['currency_name'],
|
||||||
|
'symbol' => $journal['currency_symbol'],
|
||||||
|
'decimal_places' => $journal['currency_decimal_places'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount'];
|
||||||
|
}
|
||||||
|
foreach ($total as $code => $spent) {
|
||||||
|
/** @var TransactionCurrency $currency */
|
||||||
|
$currency = $currencies[$code];
|
||||||
|
$return[] = [
|
||||||
|
'currency_id' => $currency['id'],
|
||||||
|
'currency_code' => $code,
|
||||||
|
'currency_name' => $currency['name'],
|
||||||
|
'currency_symbol' => $currency['symbol'],
|
||||||
|
'currency_decimal_places' => $currency['decimal_places'],
|
||||||
|
'amount' => $spent,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -33,21 +33,21 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface NoBudgetRepositoryInterface
|
interface NoBudgetRepositoryInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
public function setUser(User $user): void;
|
public function setUser(User $user): void;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
|
Reference in New Issue
Block a user