Various code reshuffelling.

This commit is contained in:
James Cole
2021-03-12 06:20:01 +01:00
parent 97561ab9c9
commit 748d61fb8f
51 changed files with 1874 additions and 1873 deletions

View File

@@ -90,6 +90,33 @@ class BudgetRepository implements BudgetRepositoryInterface
return true;
}
/**
* Destroy all budgets.
*/
public function destroyAll(): void
{
$budgets = $this->getBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();
RecurrenceTransactionMeta::where('name', 'budget_id')->where('value', $budget->id)->delete();
RuleAction::where('action_type', 'set_budget')->where('action_value', $budget->id)->delete();
$budget->delete();
}
}
/**
* @inheritDoc
*/
public function destroyAutoBudget(Budget $budget): void
{
/** @var AutoBudget $autoBudget */
foreach ($budget->autoBudgets()->get() as $autoBudget) {
$autoBudget->delete();
}
}
/**
* @param int|null $budgetId
* @param string|null $budgetName
@@ -176,6 +203,35 @@ class BudgetRepository implements BudgetRepositoryInterface
->get();
}
/**
* @inheritDoc
*/
public function getAttachments(Budget $budget): Collection
{
$set = $budget->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
return $set->each(
static function (Attachment $attachment) use ($disk) {
$notes = $attachment->notes()->first();
$attachment->file_exists = $disk->exists($attachment->fileName());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
/**
* @inheritDoc
*/
public function getAutoBudget(Budget $budget): ?AutoBudget
{
return $budget->autoBudgets()->first();
}
/**
* @return Collection
*/
@@ -207,6 +263,11 @@ class BudgetRepository implements BudgetRepositoryInterface
->orderBy('name', 'ASC')->where('active', 0)->get();
}
public function getMaxOrder(): int
{
return (int)$this->user->budgets()->max('order');
}
/**
* @param string $query
* @param int $limit
@@ -380,27 +441,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $budget;
}
/**
* @param string $oldName
* @param string $newName
*/
private function updateRuleActions(string $oldName, string $newName): void
{
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @param string $oldName
* @param string $newName
@@ -423,63 +463,23 @@ class BudgetRepository implements BudgetRepositoryInterface
}
/**
* Destroy all budgets.
* @param string $oldName
* @param string $newName
*/
public function destroyAll(): void
private function updateRuleActions(string $oldName, string $newName): void
{
$budgets = $this->getBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();
RecurrenceTransactionMeta::where('name', 'budget_id')->where('value', $budget->id)->delete();
RuleAction::where('action_type', 'set_budget')->where('action_value', $budget->id)->delete();
$budget->delete();
$types = ['set_budget',];
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*']);
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
}
}
/**
* @inheritDoc
*/
public function getAutoBudget(Budget $budget): ?AutoBudget
{
return $budget->autoBudgets()->first();
}
/**
* @inheritDoc
*/
public function destroyAutoBudget(Budget $budget): void
{
/** @var AutoBudget $autoBudget */
foreach ($budget->autoBudgets()->get() as $autoBudget) {
$autoBudget->delete();
}
}
/**
* @inheritDoc
*/
public function getAttachments(Budget $budget): Collection
{
$set = $budget->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
return $set->each(
static function (Attachment $attachment) use ($disk) {
$notes = $attachment->notes()->first();
$attachment->file_exists = $disk->exists($attachment->fileName());
$attachment->notes = $notes ? $notes->text : '';
return $attachment;
}
);
}
public function getMaxOrder(): int
{
return (int)$this->user->budgets()->max('order');
}
}