diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php
index 180d704c1..729cd88b0 100644
--- a/src/Controller/AppController.php
+++ b/src/Controller/AppController.php
@@ -91,12 +91,11 @@ class AppController extends Controller
$stateBalancesTable = TableRegistry::getTableLocator()->get('stateBalances');
$stateBalanceQuery = $stateBalancesTable
->find('all')
- ->select('amount')
->contain(false)
->where(['state_user_id' => $state_user_id]);
if ($stateBalanceQuery->count() == 1) {
//var_dump($stateBalanceEntry->first());
- $session->write('StateUser.balance', $stateBalanceQuery->first()->amount);
+ $session->write('StateUser.balance', $stateBalanceQuery->first()->decay);
//echo "stateUser.balance: " . $session->read('StateUser.balance');
}
}
@@ -229,7 +228,7 @@ class AppController extends Controller
}
//var_dump($stateUser);
if (count($stateUser->state_balances) > 0) {
- $session->write('StateUser.balance', $stateUser->state_balances[0]->amount);
+ $session->write('StateUser.balance', $stateUser->state_balances[0]->decay);
}
$session->write('StateUser.id', $stateUser->id);
//echo $stateUser['id'];
diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php
index 31febe498..a0b3142e0 100644
--- a/src/Controller/StateBalancesController.php
+++ b/src/Controller/StateBalancesController.php
@@ -2,11 +2,14 @@
namespace App\Controller;
use Cake\ORM\TableRegistry;
-use App\Controller\AppController;
+use Cake\I18n\Time;
use Model\Navigation\NaviHierarchy;
use Model\Navigation\NaviHierarchyEntry;
+use App\Controller\AppController;
+
+
/**
* StateBalances Controller
*
@@ -202,7 +205,53 @@ class StateBalancesController extends AppController
]);
}
uasort($transactions, array($this, 'sortTransactions'));
- $this->set('transactions', $transactions);
+
+ // add decay transactions
+ $month_start_state_balance = null;
+ $current_state_balance = null;
+ $cursor = 0;
+ $transactions_reversed = array_reverse($transactions);
+ foreach($transactions_reversed as $i => $transaction) {
+ $date = $transaction['date'];
+ $month = $date->month;
+ $year = $date->year;
+ if(!$month_start_state_balance) {
+ $month_start_state_balance = $this->StateBalances->chooseForMonthAndUser($month, $year, $user['id']);
+ if(is_array($month_start_state_balance)) {
+ $this->Flash->error(__('Error in state balance: ' . json_encode($month_start_state_balance)));
+ break;
+ }
+ $current_state_balance = $month_start_state_balance;
+
+ }
+ $prev_amount = $current_state_balance->amount;
+ $decay_duration = $current_state_balance->decayDuration($date);
+ $current_state_balance->amount = $current_state_balance->partDecay($date);
+
+ $decay_transaction = [
+ 'type' => 'decay',
+ 'balance' => -($prev_amount - $current_state_balance->amount),
+ 'decay_duration' => $decay_duration . ' ' . __('seconds'),
+ 'memo' => ''
+ ];
+ if($decay_duration > 0) {
+ array_splice($transactions_reversed, $i + $cursor, 0, [$decay_transaction]);
+ $cursor++;
+ }
+
+ $current_state_balance->record_date = $date;
+ $current_state_balance->amount += $transaction['balance'];
+
+ }
+ echo "amount: ". $current_state_balance->amount . ", duration: " . $current_state_balance->decayDuration(Time::now()) . "
";
+ array_push($transactions_reversed, [
+ 'type' => 'decay',
+ 'balance' => -($current_state_balance->amount - $current_state_balance->decay),
+ 'decay_duration' => $current_state_balance->record_date->timeAgoInWords(),// $current_state_balance->decayDuration(Time::now()),
+ 'memo' => ''
+ ]);
+
+ $this->set('transactions', array_reverse($transactions_reversed));
$this->set('transactionExecutingCount', $session->read('Transaction.executing'));
$this->set('balance', $session->read('StateUser.balance'));
$this->set('timeUsed', microtime(true) - $startTime);
diff --git a/src/Model/Entity/StateBalance.php b/src/Model/Entity/StateBalance.php
index 174e785b4..35af8477b 100644
--- a/src/Model/Entity/StateBalance.php
+++ b/src/Model/Entity/StateBalance.php
@@ -28,6 +28,7 @@ class StateBalance extends Entity
protected $_accessible = [
'state_user_id' => true,
'modified' => true,
+ 'record_date' => true,
'amount' => true,
'record_date' => true,
'state_user' => true
@@ -65,3 +66,4 @@ class StateBalance extends Entity
return intval($target_date->getTimestamp() - $this->record_date->getTimestamp());
}
}
+
diff --git a/src/Model/Table/StateBalancesTable.php b/src/Model/Table/StateBalancesTable.php
index 85b2f7837..2d00b3a96 100644
--- a/src/Model/Table/StateBalancesTable.php
+++ b/src/Model/Table/StateBalancesTable.php
@@ -6,6 +6,9 @@ use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
+use Cake\ORM\TableRegistry;
+use Cake\I18n\Date;
+
/**
* StateBalances Model
*
@@ -78,4 +81,100 @@ class StateBalancesTable extends Table
return $rules;
}
+
+ private function calculateStateBalance($previousStateBalance)
+ {
+ $entity = $this->newEntity();
+ $entity->state_user_id = $previousStateBalance->state_user_id;
+ $newDate = $previousStateBalance->record_date;
+ $newDate->day(1);
+ if($newDate->month > 12) {
+ $newDate->month($newDate->month + 1);
+ } else {
+ $newDate->month(1);
+ $newDate->year($newDate->year + 1);
+ }
+ $entity->record_date = $newDate;
+ $entity->amount = $previousStateBalance->partDecay($newDate);
+ if($this->save($entity)) {
+ return $entity;
+ }
+ return ['state' => 'error', 'msg' => 'couldn\'t save', 'details' => $entity->getErrors()];
+ }
+
+ // getting start balance for month, if exist else create and create all missing state_balances before, recursive
+ public function chooseForMonthAndUser($month, $year, $state_user_id)
+ {
+ //'created' => 'identifier'
+ $query = $this->find('all');
+ $query->select([
+ 'month' => $query->func()->month(['record_date' => 'identifier']),
+ 'year' => $query->func()->year(['record_date' => 'identifier'])
+ ])->select($this)
+ //->where(['month' => $month, 'year' => $year, 'state_user_id' => $state_user_id])
+ ->where(['state_user_id' => $state_user_id])
+ ->contain([]);
+ // TODO: fix query with correct month and year
+ //debug($query);
+ if($query->count() == 0)
+ {
+
+ // if any state balance for user exist, pick last one
+ $state_balances = $this->find('all')
+ ->where(['state_user_id' => $state_user_id])
+ ->limit(1)
+ ->order(['record_date' => 'DESC'])
+ ;
+ if($state_balances->count() == 1)
+ {
+ $current_state_balance = $state_balances->first();
+ while(true)
+ {
+ $new_state_balance = $this->calculateStateBalance($current_state_balance);
+ if(is_array($new_state_balance)) {
+ return ['state' => 'error', 'msg' => 'error calculate state balance', 'details' => $new_state_balance];
+ }
+ $record_date = $new_state_balance->record_date;
+ if($record_date->month === $month && $record_date->year === $year) {
+ return $new_state_balance;
+ }
+ $current_state_balance = $new_state_balance;
+ }
+ }
+ // else create one for first user transaction
+ else
+ {
+ $state_user_transactions_table = TableRegistry::getTableLocator()->get('StateUserTransactions');
+ $state_user_transaction = $state_user_transactions_table->find('all')
+ ->where(['state_user_id' => $state_user_id, 'StateUserTransactions.transaction_type_id <' => 3])
+ ->contain(['Transactions' => ['TransactionCreations', 'TransactionSendCoins']])
+ ->limit(1)
+ ->order(['transaction_id' => 'ASC'])
+ ->first()
+ ;
+ if(!$state_user_transaction) {
+ return null;
+ }
+ $entity = $this->newEntity();
+ $entity->state_user_id = $state_user_id;
+ if($state_user_transaction->transaction_type_id == 1) {
+ $creation = $state_user_transaction->transaction->transaction_creations[0];
+ $entity->amount = $creation->amount;
+ $entity->record_date = $creation->target_date;
+ } else if($state_user_transaction->transaction_type_id == 2) {
+ $transfer = $state_user_transaction->transaction->transaction_send_coins[0];
+ $entity->amount = $transfer->amount;
+ $entity->record_date = $state_user_transaction->transaction->received;
+ }
+ if(!$this->save($entity)) {
+ return ['state' => 'error', 'msg' => 'error by saving state balance', 'details' => $entity->getErrors()];
+ } else {
+ return $entity;
+ }
+ }
+
+
+ }
+ return $query->first();
+ }
}
diff --git a/src/Model/Transactions/Record.php b/src/Model/Transactions/Record.php
index e5d7f917e..ac378d56c 100644
--- a/src/Model/Transactions/Record.php
+++ b/src/Model/Transactions/Record.php
@@ -92,9 +92,9 @@ class GradidoModifieUserBalance
}
$entity->record_date = $recordDate;
$entity->amount = $newBalance;
- if(!$stateBalancesTable->save($entity)) {
+ /*if(!$stateBalancesTable->save($entity)) {
return ['state' => 'error', 'msg' => 'error saving state balance', 'details' => $entity->getErrors()];
- }
+ }*/
return true;
}
diff --git a/src/Template/StateBalances/overview.ctp b/src/Template/StateBalances/overview.ctp
index d9954ccfd..8ac2f769b 100644
--- a/src/Template/StateBalances/overview.ctp
+++ b/src/Template/StateBalances/overview.ctp
@@ -48,6 +48,8 @@ $this->assign('header', $header);
$cellColorClass = 'alert-color';
} else if($transaction['type'] == 'creation') {
$cellColorClass = 'orange-color';
+ } else if($transaction['type'] == 'decay') {
+ $cellColorClass = 'red-color';
}
?>