From 089300d57e090dc4cb3f0c4d8c6d373c5943cf77 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 4 Feb 2018 09:22:52 +0100 Subject: [PATCH] Update date related code to fix several issues with SQLite --- app/Helpers/Collector/JournalCollector.php | 8 ++++---- app/Models/BudgetLimit.php | 2 -- app/Models/TransactionJournal.php | 3 +-- app/Repositories/Bill/BillRepository.php | 4 ++-- app/Repositories/Budget/BudgetRepository.php | 20 +++++++++---------- .../Journal/JournalRepository.php | 9 +++++++-- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php index 3e04a2217d..ac5243a7a3 100644 --- a/app/Helpers/Collector/JournalCollector.php +++ b/app/Helpers/Collector/JournalCollector.php @@ -342,7 +342,7 @@ class JournalCollector implements JournalCollectorInterface */ public function setAfter(Carbon $after): JournalCollectorInterface { - $afterStr = $after->format('Y-m-d'); + $afterStr = $after->format('Y-m-d 00:00:00'); $this->query->where('transaction_journals.date', '>=', $afterStr); Log::debug(sprintf('JournalCollector range is now after %s (inclusive)', $afterStr)); @@ -378,7 +378,7 @@ class JournalCollector implements JournalCollectorInterface */ public function setBefore(Carbon $before): JournalCollectorInterface { - $beforeStr = $before->format('Y-m-d'); + $beforeStr = $before->format('Y-m-d 00:00:00'); $this->query->where('transaction_journals.date', '<=', $beforeStr); Log::debug(sprintf('JournalCollector range is now before %s (inclusive)', $beforeStr)); @@ -565,8 +565,8 @@ class JournalCollector implements JournalCollectorInterface public function setRange(Carbon $start, Carbon $end): JournalCollectorInterface { if ($start <= $end) { - $startStr = $start->format('Y-m-d'); - $endStr = $end->format('Y-m-d'); + $startStr = $start->format('Y-m-d 00:00:00'); + $endStr = $end->format('Y-m-d 00:00:00'); $this->query->where('transaction_journals.date', '>=', $startStr); $this->query->where('transaction_journals.date', '<=', $endStr); Log::debug(sprintf('JournalCollector range is now %s - %s (inclusive)', $startStr, $endStr)); diff --git a/app/Models/BudgetLimit.php b/app/Models/BudgetLimit.php index ddab3dfab0..741e2706c3 100644 --- a/app/Models/BudgetLimit.php +++ b/app/Models/BudgetLimit.php @@ -43,8 +43,6 @@ class BudgetLimit extends Model 'end_date' => 'date', 'repeats' => 'boolean', ]; - /** @var array */ - protected $dates = ['start_date', 'end_date']; /** * @param string $value diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 12408be513..985ee14906 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -62,8 +62,7 @@ class TransactionJournal extends Model 'encrypted' => 'boolean', 'completed' => 'boolean', ]; - /** @var array */ - protected $dates = ['date', 'interest_date', 'book_date', 'process_date']; + /** @var array */ protected $fillable = ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date', diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 00fe9f70e3..0788e20884 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -367,8 +367,8 @@ class BillRepository implements BillRepositoryInterface public function getYearAverage(Bill $bill, Carbon $date): string { $journals = $bill->transactionJournals() - ->where('date', '>=', $date->year . '-01-01') - ->where('date', '<=', $date->year . '-12-31') + ->where('date', '>=', $date->year . '-01-01 00:00:00') + ->where('date', '<=', $date->year . '-12-31 00:00:00') ->get(); $sum = '0'; $count = strval($journals->count()); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index e448182f7e..efc063217f 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -489,8 +489,8 @@ class BudgetRepository implements BudgetRepositoryInterface $availableBudget = new AvailableBudget; $availableBudget->user()->associate($this->user); $availableBudget->transactionCurrency()->associate($currency); - $availableBudget->start_date = $start; - $availableBudget->end_date = $end; + $availableBudget->start_date = $start->format('Y-m-d 00:00:00'); + $availableBudget->end_date = $end->format('Y-m-d 00:00:00'); } $availableBudget->amount = $amount; $availableBudget->save(); @@ -619,23 +619,23 @@ class BudgetRepository implements BudgetRepositoryInterface { // count the limits: $limits = $budget->budgetlimits() - ->where('budget_limits.start_date', $start->format('Y-m-d')) - ->where('budget_limits.end_date', $end->format('Y-m-d')) + ->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')) - ->where('budget_limits.end_date', $end->format('Y-m-d')) + ->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')) - ->where('budget_limits.end_date', $end->format('Y-m-d')) + ->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(); } @@ -660,8 +660,8 @@ class BudgetRepository implements BudgetRepositoryInterface // or create one and return it. $limit = new BudgetLimit; $limit->budget()->associate($budget); - $limit->start_date = $start; - $limit->end_date = $end; + $limit->start_date = $start->format('Y-m-d 00:00:00'); + $limit->end_date = $end->format('Y-m-d 00:00:00'); $limit->amount = $amount; $limit->save(); Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount)); diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 7bf6bb78cd..055c4f0985 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Journal; +use Carbon\Carbon; use DB; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; @@ -301,14 +302,18 @@ class JournalRepository implements JournalRepositoryInterface $accounts = $this->storeAccounts($this->user, $transactionType, $data); $data = $this->verifyNativeAmount($data, $accounts); $amount = strval($data['amount']); - $journal = new TransactionJournal( + $dateString = $data['date']; + if ($data['date'] instanceof Carbon) { + $dateString = $data['date']->format('Y-m-d 00:00:00'); + } + $journal = new TransactionJournal( [ 'user_id' => $this->user->id, 'transaction_type_id' => $transactionType->id, 'transaction_currency_id' => $data['currency_id'], // no longer used. 'description' => $data['description'], 'completed' => 0, - 'date' => $data['date'], + 'date' => $dateString, ] ); $journal->save();