Skip to content

Firefly III: a personal finances manager

Expand API for bills.

James Cole983508e2914a
diff --git a/app/Api/V1/Controllers/Chart/AccountController.php b/app/Api/V1/Controllers/Chart/AccountController.php
index c01606a6b0..b82d861692 100644
--- a/app/Api/V1/Controllers/Chart/AccountController.php
+++ b/app/Api/V1/Controllers/Chart/AccountController.php
@@ -42,10 +42,8 @@ use Illuminate\Http\JsonResponse;
class AccountController extends Controller
{
use ApiSupport;
- /** @var CurrencyRepositoryInterface */
- private $currencyRepository;
- /** @var AccountRepositoryInterface */
- private $repository;
+ private CurrencyRepositoryInterface $currencyRepository;
+ private AccountRepositoryInterface $repository;
/**
* AccountController constructor.
diff --git a/app/Api/V1/Requests/BillRequest.php b/app/Api/V1/Requests/BillRequest.php
index ca0ee49a2a..9600af660e 100644
--- a/app/Api/V1/Requests/BillRequest.php
+++ b/app/Api/V1/Requests/BillRequest.php
@@ -70,6 +70,7 @@ class BillRequest extends Request
'repeat_freq' => $this->string('repeat_freq'),
'skip' => $this->integer('skip'),
'active' => $active,
+ 'order' => $this->integer('order'),
'notes' => $this->nlString('notes'),
];
}
@@ -83,13 +84,13 @@ class BillRequest extends Request
public function rules(): array
{
$rules = [
- 'name' => 'required|between:1,255|uniqueObjectForUser:bills,name',
- 'amount_min' => 'required|numeric|more:0',
- 'amount_max' => 'required|numeric|more:0',
+ 'name' => 'between:1,255|uniqueObjectForUser:bills,name',
+ 'amount_min' => 'numeric|more:0',
+ 'amount_max' => 'numeric|more:0',
'currency_id' => 'numeric|exists:transaction_currencies,id',
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
- 'date' => 'required|date',
- 'repeat_freq' => 'required|in:weekly,monthly,quarterly,half-year,yearly',
+ 'date' => 'date',
+ 'repeat_freq' => 'in:weekly,monthly,quarterly,half-year,yearly',
'skip' => 'between:0,31',
'active' => [new IsBoolean],
'notes' => 'between:1,65536',
diff --git a/app/Services/Internal/Update/BillUpdateService.php b/app/Services/Internal/Update/BillUpdateService.php
index 7df3070334..b48fd03ae2 100644
--- a/app/Services/Internal/Update/BillUpdateService.php
+++ b/app/Services/Internal/Update/BillUpdateService.php
@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
+use DB;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Rule;
@@ -31,6 +32,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
use FireflyIII\Services\Internal\Support\BillServiceTrait;
+use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@@ -86,16 +88,33 @@ class BillUpdateService
];
// new values
$data['transaction_currency_name'] = $currency->name;
- $bill->name = $data['name'];
- $bill->match = $data['match'] ?? $bill->match;
- $bill->amount_min = $data['amount_min'];
- $bill->amount_max = $data['amount_max'];
- $bill->date = $data['date'];
- $bill->transaction_currency_id = $currency->id;
- $bill->repeat_freq = $data['repeat_freq'];
- $bill->skip = $data['skip'];
- $bill->automatch = true;
- $bill->active = $data['active'] ?? true;
+
+ if (isset($data['name']) && '' !== (string) $data['name']) {
+ $bill->name = $data['name'];
+ }
+
+ if (isset($data['amount_min']) && '' !== (string) $data['amount_min']) {
+ $bill->amount_min = $data['amount_min'];
+ }
+ if (isset($data['amount_max']) && '' !== (string) $data['amount_max']) {
+ $bill->amount_max = $data['amount_max'];
+ }
+ if (isset($data['date']) && '' !== (string) $data['date']) {
+ $bill->date = $data['date'];
+ }
+ if (isset($data['repeat_freq']) && '' !== (string) $data['repeat_freq']) {
+ $bill->repeat_freq = $data['repeat_freq'];
+ }
+ if (isset($data['skip']) && '' !== (string) $data['skip']) {
+ $bill->skip = $data['skip'];
+ }
+ if (isset($data['active']) && '' !== (string) $data['active']) {
+ $bill->active = $data['active'];
+ }
+
+ $bill->transaction_currency_id = $currency->id;
+ $bill->match = 'EMPTY';
+ $bill->automatch = true;
$bill->save();
// update note:
@@ -103,6 +122,14 @@ class BillUpdateService
$this->updateNote($bill, (string) $data['notes']);
}
+ // update order.
+ // update the order of the piggy bank:
+ $oldOrder = (int) $bill->order;
+ $newOrder = (int) ($data['order'] ?? $oldOrder);
+ if ($oldOrder !== $newOrder) {
+ $this->updateOrder($bill, $oldOrder, $newOrder);
+ }
+
// update rule actions.
$this->updateBillActions($bill, $oldData['name'], $data['name']);
$this->updateBillTriggers($bill, $oldData, $data);
@@ -200,4 +227,32 @@ class BillUpdateService
{
return $rule->ruleTriggers()->where('trigger_type', $key)->first();
}
+
+ /**
+ * @param Bill $bill
+ * @param int $oldOrder
+ * @param int $newOrder
+ */
+ private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void
+ {
+ if ($newOrder > $oldOrder) {
+ /** @var User $user */
+ $user = $this->user;
+ $user->bills()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
+ ->where('bills.id', '!=', $bill->id)
+ ->update(['order' => DB::raw('bills.order-1')]);
+ $bill->order = $newOrder;
+ $bill->save();
+ }
+ if ($newOrder < $oldOrder) {
+ /** @var User $user */
+ $user = $this->user;
+ $user->bills()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
+ ->where('bills.id', '!=', $bill->id)
+ ->update(['order' => DB::raw('bills.order+1')]);
+ $bill->order = $newOrder;
+ $bill->save();
+ }
+
+ }
}