Skip to content

Firefly III: a personal finances manager

New converters for #180 (Bill)

James Colee4d93cad27fb
diff --git a/app/Helpers/Csv/Converter/BillId.php b/app/Helpers/Csv/Converter/BillId.php
index 5dcde18d9a..fa939852de 100644
--- a/app/Helpers/Csv/Converter/BillId.php
+++ b/app/Helpers/Csv/Converter/BillId.php
@@ -4,6 +4,7 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Bill;
+use FireflyIII\Repositories\Bill\BillRepositoryInterface;
/**
* Class BillId
@@ -18,11 +19,14 @@ class BillId extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var BillRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
- $bill = Auth::user()->bills()->find($this->mapped[$this->index][$this->value]);
+ $bill = $repository->find($this->mapped[$this->index][$this->value]);
} else {
- $bill = Auth::user()->bills()->find($this->value);
+ $bill = $repository->find($this->value);
}
return $bill;
diff --git a/app/Helpers/Csv/Converter/BillName.php b/app/Helpers/Csv/Converter/BillName.php
index e6229240d1..5f6c008f25 100644
--- a/app/Helpers/Csv/Converter/BillName.php
+++ b/app/Helpers/Csv/Converter/BillName.php
@@ -2,8 +2,8 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
-use Auth;
use FireflyIII\Models\Bill;
+use FireflyIII\Repositories\Bill\BillRepositoryInterface;
/**
* Class BillName
@@ -18,13 +18,15 @@ class BillName extends BasicConverter implements ConverterInterface
*/
public function convert()
{
+ /** @var BillRepositoryInterface $repository */
+ $repository = app('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+
$bill = null;
// is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) {
- $bill = Auth::user()->bills()->find($this->mapped[$this->index][$this->value]);
+ $bill = $repository->find($this->mapped[$this->index][$this->value]);
} else {
-
- $bills = Auth::user()->bills()->get();
+ $bills = $repository->getBills();
/** @var Bill $bill */
foreach ($bills as $bill) {
if ($bill->name == $this->value) {
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index fcda2b7703..6eb28ea1d3 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -49,6 +49,23 @@ class BillRepository implements BillRepositoryInterface
return true;
}
+ /**
+ * Find a bill by ID.
+ *
+ * @param int $billId
+ *
+ * @return Bill
+ */
+ public function find(int $billId) : Bill
+ {
+ $bill = $this->user->bills()->find($billId);
+ if (is_null($bill)) {
+ $bill = new Bill;
+ }
+
+ return $bill;
+ }
+
/**
* @return Collection
*/
diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php
index 10b3156dd1..948e0c67fd 100644
--- a/app/Repositories/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/Bill/BillRepositoryInterface.php
@@ -23,6 +23,15 @@ interface BillRepositoryInterface
*/
public function destroy(Bill $bill): bool;
+ /**
+ * Find a bill by ID.
+ *
+ * @param int $billId
+ *
+ * @return Bill
+ */
+ public function find(int $billId) : Bill;
+
/**
* @return Collection
*/