mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
decay
This commit is contained in:
parent
6de55f5f39
commit
e56cd1501d
@ -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'];
|
||||
|
||||
@ -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()) . "<br>";
|
||||
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);
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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';
|
||||
}
|
||||
?>
|
||||
<div class="row">
|
||||
@ -60,7 +62,7 @@ $this->assign('header', $header);
|
||||
</a>
|
||||
<?php elseif(isset($transaction['name']) && $transaction['name'] != '') : ?>
|
||||
<small class="tx-email"><?= $transaction['name'] ?></small>
|
||||
<?php else : ?>
|
||||
<?php elseif(isset($transaction['pubkey'])) : ?>
|
||||
<small class="tx-email"><?= $transaction['pubkey'] ?></small>
|
||||
<?php endif; ?>
|
||||
<!-- noch ungeklärt - icon ist nicht aligned -->
|
||||
@ -75,6 +77,9 @@ $this->assign('header', $header);
|
||||
<?php elseif($transaction['type'] == 'receive') : ?>
|
||||
<i class="material-icons-outlined">arrow_forward</i>
|
||||
<?= __('Empfangen') ?>
|
||||
<?php elseif($transaction['type'] == 'decay') : ?>
|
||||
<i class="material-icons-outlined">minus_circle_multiple</i>
|
||||
<?= __('Vergangen') ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
@ -86,10 +91,18 @@ $this->assign('header', $header);
|
||||
<?= $transaction['memo'] ?>
|
||||
<?php endif;?>
|
||||
</div>
|
||||
<div class="cell c4"><?= $transaction['date']->nice() ?></div>
|
||||
<div class="cell c4">
|
||||
<?php if(isset($transaction['date'])) : ?>
|
||||
<?= $transaction['date']->nice() ?>
|
||||
<?php elseif(isset($transaction['decay_duration'])) : ?>
|
||||
<?= h($transaction['decay_duration']) ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="cell c3"><?= $this->element('printGradido', ['number' => $balance]) ?></div>
|
||||
<div class="cell c2">
|
||||
<?= $transaction['transaction_id'] ?>
|
||||
<?php if(isset($transaction['transaction_id'])) : ?>
|
||||
<?= $transaction['transaction_id'] ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user