From 3f27a96c6c68ac15060b848e99b78e5655b9bb7a Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 10:33:11 +0200 Subject: [PATCH 1/7] Allow to have leading - in substitution --- app/Http/Controllers/ImportController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index f83b24f9..252d0bae 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -136,7 +136,7 @@ public function postComplete(Request $request, Import $import) 'tag_id' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER 'happened_on' => 'date|date_format:' . $date_format, 'description' => 'max:255', - 'amount' => 'regex:/^\d*([\,\.]\d{2})?$/' + 'amount' => 'regex:/^(\-)?\d*([\,\.]\d{2})?$/' ]); if ($validator->fails()) { From 1bbc94cb03fb9c2b68fb09aa7d43beabe51e8ee4 Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 10:37:35 +0200 Subject: [PATCH 2/7] Multiply amount by 100 if there is a dot --- app/Http/Controllers/ImportController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 252d0bae..65703417 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -158,6 +158,11 @@ public function postComplete(Request $request, Import $import) // TODO CHECK HOW THIS WORKS WITH 1k+ AMOUNTS $amount = str_replace(',', '.', $row['amount']); + //correct amount to be in cents + if(strpos($row['amount'], '.')!==false){ + $amount *= 100; + } + $this->spendingRepository->create( session('space_id'), $import->id, From 579c8e4590e9c93f924e2554bd0851995e62682b Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 10:46:27 +0200 Subject: [PATCH 3/7] Adding ability to import earnings --- app/Http/Controllers/ImportController.php | 24 +++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 65703417..ec17b6c1 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -6,16 +6,19 @@ use App\Models\Import; use App\Models\Tag; use App\Repositories\SpendingRepository; +use App\Repositories\EarningRepository; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class ImportController extends Controller { private $spendingRepository; + private $earningRepository; - public function __construct(SpendingRepository $spendingRepository) + public function __construct(SpendingRepository $spendingRepository, EarningRepository $earningRepository) { $this->spendingRepository = $spendingRepository; + $this->earningRepository = $earningRepository; } public function index() @@ -163,15 +166,28 @@ public function postComplete(Request $request, Import $import) $amount *= 100; } - $this->spendingRepository->create( + if($row['amount']<0){ + $row['amount']*=-1; + $this->spendingRepository->create( + session('space_id'), + $import->id, + null, + $row['tag_id'], + $row['happened_on'], + $row['description'], + $amount + ); + } else { + $this-earningRepository->create( session('space_id'), - $import->id, null, $row['tag_id'], $row['happened_on'], $row['description'], $amount - ); + ); + } + } } From 679f8b892344dd3da8dacef88044ba452535def0 Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 11:05:45 +0200 Subject: [PATCH 4/7] Typo error --- app/Http/Controllers/ImportController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index ec17b6c1..1d827199 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -178,7 +178,7 @@ public function postComplete(Request $request, Import $import) $amount ); } else { - $this-earningRepository->create( + $this->earningRepository->create( session('space_id'), null, $row['tag_id'], From c0f02c98c4eada13d4c7fedf3472e343844d8521 Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 11:12:52 +0200 Subject: [PATCH 5/7] Remove tag for earnings --- app/Http/Controllers/ImportController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 1d827199..c0aeab3f 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -181,7 +181,6 @@ public function postComplete(Request $request, Import $import) $this->earningRepository->create( session('space_id'), null, - $row['tag_id'], $row['happened_on'], $row['description'], $amount From 0d255ba25d132bb857d78c3b40b9290259be00e3 Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 11:14:31 +0200 Subject: [PATCH 6/7] Using amount instead of row_amount --- app/Http/Controllers/ImportController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index c0aeab3f..1778d14d 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -162,12 +162,12 @@ public function postComplete(Request $request, Import $import) $amount = str_replace(',', '.', $row['amount']); //correct amount to be in cents - if(strpos($row['amount'], '.')!==false){ + if(strpos($amount, '.')!==false){ $amount *= 100; } - if($row['amount']<0){ - $row['amount']*=-1; + if($amount<0){ + $amount*=-1; $this->spendingRepository->create( session('space_id'), $import->id, From 53428c1b08a7b281e98636e06a1419bebd275e38 Mon Sep 17 00:00:00 2001 From: Kevin Chollet Date: Mon, 17 May 2021 11:21:33 +0200 Subject: [PATCH 7/7] If no amount present, don't process the row --- app/Http/Controllers/ImportController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 1778d14d..24bcc23c 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -157,7 +157,7 @@ public function postComplete(Request $request, Import $import) } foreach ($request->input('rows') as $row) { - if (isset($row['import'])) { + if (isset($row['import']) && isset($row['amount'])) { // TODO CHECK HOW THIS WORKS WITH 1k+ AMOUNTS $amount = str_replace(',', '.', $row['amount']);