From 1949260452d64b4ce8051aafba45b0befc1e5ac7 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Thu, 18 Mar 2021 12:55:04 +0000 Subject: [PATCH] fix bugs with begin of decay implementation --- .../src/Model/Entity/StateBalance.php | 19 +++++++++++++++---- .../Model/Transactions/TransactionBody.php | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/community_server/src/Model/Entity/StateBalance.php b/community_server/src/Model/Entity/StateBalance.php index 174e785b4..871c55bd8 100644 --- a/community_server/src/Model/Entity/StateBalance.php +++ b/community_server/src/Model/Entity/StateBalance.php @@ -35,6 +35,15 @@ class StateBalance extends Entity protected $_virtual = ['decay']; + private function convertToTimestamp($dateOrTime) + { + if(method_exists($dateOrTime, 'getTimestamp')) { + return $dateOrTime->getTimestamp(); + } else { + return $dateOrTime->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT); + } + } + protected function _getDecay() { // decay factor in seconds per year @@ -44,7 +53,7 @@ class StateBalance extends Entity // SELECT TIMESTAMPDIFF(SECOND, modified, CURDATE()) AS age_in_seconds from state_balances // decay_for_duration = decay_factor^seconds // decay = gradido_cent * decay_for_duration - $decay_duration = intval(Time::now()->getTimestamp() - $this->record_date->getTimestamp()); + $decay_duration = $this->decayDuration(Time::now()); if($decay_duration === 0) { return $this->amount; } @@ -53,7 +62,8 @@ class StateBalance extends Entity } public function partDecay($target_date) { - $decay_duration = intval($target_date->getTimestamp() - $this->record_date->getTimestamp()); + if($target_date == null) return 0; + $decay_duration = $this->decayDuration($target_date); if($decay_duration <= 0) { return $this->amount; } @@ -61,7 +71,8 @@ class StateBalance extends Entity } public function decayDuration($target_date) - { - return intval($target_date->getTimestamp() - $this->record_date->getTimestamp()); + { + if($this->record_date == null) return 0; + return intval($this->convertToTimestamp($target_date) - $this->record_date->getTimestamp()); } } diff --git a/community_server/src/Model/Transactions/TransactionBody.php b/community_server/src/Model/Transactions/TransactionBody.php index 8164f33f7..ef785705c 100644 --- a/community_server/src/Model/Transactions/TransactionBody.php +++ b/community_server/src/Model/Transactions/TransactionBody.php @@ -3,6 +3,7 @@ namespace Model\Transactions; use Cake\ORM\TableRegistry; +use Cake\I18n\Date; class TransactionBody extends TransactionBase { private $mProtoTransactionBody = null; @@ -75,6 +76,7 @@ class TransactionBody extends TransactionBase { $transactionEntity->transaction_type_id = $this->transactionTypeId; $transactionEntity->memo = $this->getMemo(); + $transactionEntity->received = new Date(); if ($transactionsTable->save($transactionEntity)) { // success