Update date related code to fix several issues with SQLite

This commit is contained in:
James Cole
2018-02-04 09:22:52 +01:00
parent 36f67793cb
commit 089300d57e
6 changed files with 24 additions and 22 deletions

View File

@@ -342,7 +342,7 @@ class JournalCollector implements JournalCollectorInterface
*/ */
public function setAfter(Carbon $after): 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); $this->query->where('transaction_journals.date', '>=', $afterStr);
Log::debug(sprintf('JournalCollector range is now after %s (inclusive)', $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 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); $this->query->where('transaction_journals.date', '<=', $beforeStr);
Log::debug(sprintf('JournalCollector range is now before %s (inclusive)', $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 public function setRange(Carbon $start, Carbon $end): JournalCollectorInterface
{ {
if ($start <= $end) { if ($start <= $end) {
$startStr = $start->format('Y-m-d'); $startStr = $start->format('Y-m-d 00:00:00');
$endStr = $end->format('Y-m-d'); $endStr = $end->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '>=', $startStr); $this->query->where('transaction_journals.date', '>=', $startStr);
$this->query->where('transaction_journals.date', '<=', $endStr); $this->query->where('transaction_journals.date', '<=', $endStr);
Log::debug(sprintf('JournalCollector range is now %s - %s (inclusive)', $startStr, $endStr)); Log::debug(sprintf('JournalCollector range is now %s - %s (inclusive)', $startStr, $endStr));

View File

@@ -43,8 +43,6 @@ class BudgetLimit extends Model
'end_date' => 'date', 'end_date' => 'date',
'repeats' => 'boolean', 'repeats' => 'boolean',
]; ];
/** @var array */
protected $dates = ['start_date', 'end_date'];
/** /**
* @param string $value * @param string $value

View File

@@ -62,8 +62,7 @@ class TransactionJournal extends Model
'encrypted' => 'boolean', 'encrypted' => 'boolean',
'completed' => 'boolean', 'completed' => 'boolean',
]; ];
/** @var array */
protected $dates = ['date', 'interest_date', 'book_date', 'process_date'];
/** @var array */ /** @var array */
protected $fillable protected $fillable
= ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date', = ['user_id', 'transaction_type_id', 'bill_id', 'interest_date', 'book_date', 'process_date',

View File

@@ -367,8 +367,8 @@ class BillRepository implements BillRepositoryInterface
public function getYearAverage(Bill $bill, Carbon $date): string public function getYearAverage(Bill $bill, Carbon $date): string
{ {
$journals = $bill->transactionJournals() $journals = $bill->transactionJournals()
->where('date', '>=', $date->year . '-01-01') ->where('date', '>=', $date->year . '-01-01 00:00:00')
->where('date', '<=', $date->year . '-12-31') ->where('date', '<=', $date->year . '-12-31 00:00:00')
->get(); ->get();
$sum = '0'; $sum = '0';
$count = strval($journals->count()); $count = strval($journals->count());

View File

@@ -489,8 +489,8 @@ class BudgetRepository implements BudgetRepositoryInterface
$availableBudget = new AvailableBudget; $availableBudget = new AvailableBudget;
$availableBudget->user()->associate($this->user); $availableBudget->user()->associate($this->user);
$availableBudget->transactionCurrency()->associate($currency); $availableBudget->transactionCurrency()->associate($currency);
$availableBudget->start_date = $start; $availableBudget->start_date = $start->format('Y-m-d 00:00:00');
$availableBudget->end_date = $end; $availableBudget->end_date = $end->format('Y-m-d 00:00:00');
} }
$availableBudget->amount = $amount; $availableBudget->amount = $amount;
$availableBudget->save(); $availableBudget->save();
@@ -619,23 +619,23 @@ class BudgetRepository implements BudgetRepositoryInterface
{ {
// count the limits: // count the limits:
$limits = $budget->budgetlimits() $limits = $budget->budgetlimits()
->where('budget_limits.start_date', $start->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')) ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
->get(['budget_limits.*'])->count(); ->get(['budget_limits.*'])->count();
Log::debug(sprintf('Found %d budget limits.', $limits)); Log::debug(sprintf('Found %d budget limits.', $limits));
// there might be a budget limit for these dates: // there might be a budget limit for these dates:
/** @var BudgetLimit $limit */ /** @var BudgetLimit $limit */
$limit = $budget->budgetlimits() $limit = $budget->budgetlimits()
->where('budget_limits.start_date', $start->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')) ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
->first(['budget_limits.*']); ->first(['budget_limits.*']);
// if more than 1 limit found, delete the others: // if more than 1 limit found, delete the others:
if ($limits > 1 && null !== $limit) { if ($limits > 1 && null !== $limit) {
Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id)); Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
$budget->budgetlimits() $budget->budgetlimits()
->where('budget_limits.start_date', $start->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')) ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
->where('budget_limits.id', '!=', $limit->id)->delete(); ->where('budget_limits.id', '!=', $limit->id)->delete();
} }
@@ -660,8 +660,8 @@ class BudgetRepository implements BudgetRepositoryInterface
// or create one and return it. // or create one and return it.
$limit = new BudgetLimit; $limit = new BudgetLimit;
$limit->budget()->associate($budget); $limit->budget()->associate($budget);
$limit->start_date = $start; $limit->start_date = $start->format('Y-m-d 00:00:00');
$limit->end_date = $end; $limit->end_date = $end->format('Y-m-d 00:00:00');
$limit->amount = $amount; $limit->amount = $amount;
$limit->save(); $limit->save();
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount)); Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));

View File

@@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Journal; namespace FireflyIII\Repositories\Journal;
use Carbon\Carbon;
use DB; use DB;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
@@ -301,6 +302,10 @@ class JournalRepository implements JournalRepositoryInterface
$accounts = $this->storeAccounts($this->user, $transactionType, $data); $accounts = $this->storeAccounts($this->user, $transactionType, $data);
$data = $this->verifyNativeAmount($data, $accounts); $data = $this->verifyNativeAmount($data, $accounts);
$amount = strval($data['amount']); $amount = strval($data['amount']);
$dateString = $data['date'];
if ($data['date'] instanceof Carbon) {
$dateString = $data['date']->format('Y-m-d 00:00:00');
}
$journal = new TransactionJournal( $journal = new TransactionJournal(
[ [
'user_id' => $this->user->id, 'user_id' => $this->user->id,
@@ -308,7 +313,7 @@ class JournalRepository implements JournalRepositoryInterface
'transaction_currency_id' => $data['currency_id'], // no longer used. 'transaction_currency_id' => $data['currency_id'], // no longer used.
'description' => $data['description'], 'description' => $data['description'],
'completed' => 0, 'completed' => 0,
'date' => $data['date'], 'date' => $dateString,
] ]
); );
$journal->save(); $journal->save();