mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
remove timestamp on_update because it didn't make sense. I don't know why I have make it so in the first place
This commit is contained in:
parent
a5e7b38f72
commit
a99f727eab
@ -4,7 +4,7 @@ CREATE TABLE `transactions` (
|
|||||||
`transaction_type_id` int(10) unsigned NOT NULL,
|
`transaction_type_id` int(10) unsigned NOT NULL,
|
||||||
`tx_hash` binary(48) DEFAULT NULL,
|
`tx_hash` binary(48) DEFAULT NULL,
|
||||||
`memo` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
`memo` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
`received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
`blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1,
|
`blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|||||||
@ -42,20 +42,21 @@ class StateBalancesController extends AppController
|
|||||||
$this->set(compact('stateBalances'));
|
$this->set(compact('stateBalances'));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateBalance($stateUserId)
|
private function updateBalances($stateUserId)
|
||||||
{
|
{
|
||||||
|
echo "stateUserId: $stateUserId\n";
|
||||||
$stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions');
|
$stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions');
|
||||||
$transactionsTable = TableRegistry::getTableLocator()->get('Transactions');
|
$transactionsTable = TableRegistry::getTableLocator()->get('Transactions');
|
||||||
// info: cakephp use lazy loading, query will be executed later only if needed
|
// info: cakephp use lazy loading, query will be executed later only if needed
|
||||||
$state_balances = $this->StateBalances->find('all')->where(['state_user_id' => $stateUserId]);
|
$state_balances = $this->StateBalances->find('all')->where(['state_user_id' => $stateUserId]);
|
||||||
$state_user_transactions = $stateUserTransactionsTable
|
$state_user_transactions = $stateUserTransactionsTable
|
||||||
->find('all')
|
->find()
|
||||||
->where(['state_user_id' => $stateUserId])
|
->where(['state_user_id' => $stateUserId])
|
||||||
->order(['transaction_id ASC'])
|
->order(['transaction_id ASC'])
|
||||||
->contain(false);
|
//->contain(false);
|
||||||
|
;
|
||||||
|
|
||||||
if(!$state_user_transactions || !$state_user_transactions->count()) {
|
if(!$state_user_transactions || !$state_user_transactions->count()) {
|
||||||
//debug($state_user_transactions);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,10 +67,12 @@ class StateBalancesController extends AppController
|
|||||||
$update_state_balance = false;
|
$update_state_balance = false;
|
||||||
if($state_balances->count() == 0) {
|
if($state_balances->count() == 0) {
|
||||||
$create_state_balance = true;
|
$create_state_balance = true;
|
||||||
|
$recalculate_state_user_transactions_balance = true;
|
||||||
}
|
}
|
||||||
if($state_balances->count() > 1) {
|
if($state_balances->count() > 1) {
|
||||||
$clear_state_balance = true;
|
$clear_state_balance = true;
|
||||||
$create_state_balance = true;
|
$create_state_balance = true;
|
||||||
|
$recalculate_state_user_transactions_balance = true;
|
||||||
}
|
}
|
||||||
if($state_balances->count() == 1) {
|
if($state_balances->count() == 1) {
|
||||||
if($state_user_transactions->count() == 0){
|
if($state_user_transactions->count() == 0){
|
||||||
@ -125,6 +128,7 @@ class StateBalancesController extends AppController
|
|||||||
|
|
||||||
$transaction_ids = [];
|
$transaction_ids = [];
|
||||||
if($recalculate_state_user_transactions_balance) {
|
if($recalculate_state_user_transactions_balance) {
|
||||||
|
echo "recalculate state user transaction balances \n";
|
||||||
$state_user_transactions_array = $state_user_transactions->toArray();
|
$state_user_transactions_array = $state_user_transactions->toArray();
|
||||||
foreach($state_user_transactions_array as $i => $state_user_transaction) {
|
foreach($state_user_transactions_array as $i => $state_user_transaction) {
|
||||||
$transaction_ids[$state_user_transaction->transaction_id] = $i;
|
$transaction_ids[$state_user_transaction->transaction_id] = $i;
|
||||||
@ -144,17 +148,23 @@ class StateBalancesController extends AppController
|
|||||||
$amount_date = null;
|
$amount_date = null;
|
||||||
$amount = 0;
|
$amount = 0;
|
||||||
|
|
||||||
if($transaction->transaction_type_id == 1) {
|
if($transaction->transaction_type_id == 1) { // creation
|
||||||
|
echo "creation\n";
|
||||||
$temp = $transaction->transaction_creations[0];
|
$temp = $transaction->transaction_creations[0];
|
||||||
|
echo "creation amount: " . $temp->amount . "\n";
|
||||||
|
echo "target date: " . $temp->target_date . "\n";
|
||||||
$balance_temp = $this->StateBalances->newEntity();
|
$balance_temp = $this->StateBalances->newEntity();
|
||||||
$balance_temp->amount = $temp->amount;
|
$balance_temp->amount = $temp->amount;
|
||||||
$balance_temp->record_date = $temp->target_date;
|
$balance_temp->record_date = $temp->target_date;
|
||||||
|
|
||||||
$amount = $balance_temp->partDecay($transaction->received);
|
$amount = $balance_temp->partDecay($transaction->received);
|
||||||
$amount_date = $transaction->received;
|
$amount_date = $transaction->received;
|
||||||
|
echo "amount in state_user_transactions: $amount\n";
|
||||||
|
echo "date in state_user_transactions: $amount_date\n";
|
||||||
|
echo "diff: " . ($temp->amount - $amount) . "\n";
|
||||||
//$amount_date =
|
//$amount_date =
|
||||||
} else if($transaction->transaction_type_id == 2) {
|
} else if($transaction->transaction_type_id == 2) { // transfer
|
||||||
|
echo "send coins\n";
|
||||||
$temp = $transaction->transaction_send_coins[0];
|
$temp = $transaction->transaction_send_coins[0];
|
||||||
$amount = intval($temp->amount);
|
$amount = intval($temp->amount);
|
||||||
// reverse if sender
|
// reverse if sender
|
||||||
@ -162,18 +172,23 @@ class StateBalancesController extends AppController
|
|||||||
$amount *= -1.0;
|
$amount *= -1.0;
|
||||||
}
|
}
|
||||||
$amount_date = $transaction->received;
|
$amount_date = $transaction->received;
|
||||||
|
echo "amount: " . $amount . "\n";
|
||||||
|
echo "date: " . $amount_date . "\n";
|
||||||
}
|
}
|
||||||
if($i == 0) {
|
if($i == 0) {
|
||||||
$balance_cursor->amount = $amount;
|
$balance_cursor->amount = $amount;
|
||||||
} else {
|
} else {
|
||||||
$balance_cursor->amount = $balance_cursor->partDecay($amount_date) + $amount;
|
$balance_cursor->amount = $balance_cursor->partDecay($amount_date) + $amount;
|
||||||
}
|
}
|
||||||
|
echo "balance cursor amount: " . $balance_cursor->amount . "\n";
|
||||||
$balance_cursor->record_date = $amount_date;
|
$balance_cursor->record_date = $amount_date;
|
||||||
$state_user_transaction_index = $transaction_ids[$transaction->id];
|
$state_user_transaction_index = $transaction_ids[$transaction->id];
|
||||||
$state_user_transactions_array[$state_user_transaction_index]->balance = $balance_cursor->amount;
|
$state_user_transactions_array[$state_user_transaction_index]->balance = $balance_cursor->amount;
|
||||||
$state_user_transactions_array[$state_user_transaction_index]->balance_date = $balance_cursor->record_date;
|
$state_user_transactions_array[$state_user_transaction_index]->balance_date = $balance_cursor->record_date;
|
||||||
$i++;
|
$i++;
|
||||||
|
echo "##########################\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = $stateUserTransactionsTable->saveMany($state_user_transactions_array);
|
$results = $stateUserTransactionsTable->saveMany($state_user_transactions_array);
|
||||||
$errors = [];
|
$errors = [];
|
||||||
foreach($results as $i => $result) {
|
foreach($results as $i => $result) {
|
||||||
@ -446,13 +461,29 @@ class StateBalancesController extends AppController
|
|||||||
}
|
}
|
||||||
$session = $this->getRequest()->getSession();
|
$session = $this->getRequest()->getSession();
|
||||||
$user = $session->read('StateUser');
|
$user = $session->read('StateUser');
|
||||||
|
|
||||||
$this->updateBalances($user['id']);
|
$this->updateBalances($user['id']);
|
||||||
|
|
||||||
|
|
||||||
|
echo "try to unhex: ". $user['public_hex'] . "\n";
|
||||||
$public_key_bin = hex2bin($user['public_hex']);
|
$public_key_bin = hex2bin($user['public_hex']);
|
||||||
|
//var_dump($public_key_bin);
|
||||||
$stateUserQuery = $this->StateBalances->StateUsers
|
$stateUserQuery = $this->StateBalances->StateUsers
|
||||||
->find('all')
|
->find('all')
|
||||||
->where(['public_key' => $public_key_bin])
|
//->where(['public_key' => $public_key_bin])
|
||||||
->contain(['StateBalances']);
|
->where(['id' => $user['id']])
|
||||||
|
//->contain(['StateBalances'])
|
||||||
|
;
|
||||||
|
$hex_from_db = bin2hex(stream_get_contents($stateUserQuery->first()->public_key));
|
||||||
|
echo "hex from db: $hex_from_db\n";
|
||||||
|
echo "state user query array: \n";
|
||||||
|
var_dump($stateUserQuery->toArray());
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
$state_balance = $this->StateBalances->find()->where(['state_user_id' => $user['id']]);
|
||||||
|
echo "state balance:\n";
|
||||||
|
var_dump($state_balance->toArray());
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
$result_user_count = $stateUserQuery->count();
|
$result_user_count = $stateUserQuery->count();
|
||||||
if($result_user_count < 1) {
|
if($result_user_count < 1) {
|
||||||
@ -465,16 +496,13 @@ class StateBalancesController extends AppController
|
|||||||
'details' => ['public_key' => $user['public_hex'], 'entry_count' => $result_count]
|
'details' => ['public_key' => $user['public_hex'], 'entry_count' => $result_count]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
$state_balances = $stateUserQuery->first()->state_balances;
|
//$state_balance = $stateUserQuery->first()->state_balances;
|
||||||
$state_balances_count = count($state_balances);
|
|
||||||
if($state_balances_count > 1) {
|
if(!$state_balance) {
|
||||||
return $this->returnJson(['state' => 'error', 'msg' => 'state balances count isn\'t as expected, expect 1 or 0', 'details' => $state_balances_count]);
|
|
||||||
}
|
|
||||||
if(!$state_balances_count) {
|
|
||||||
return $this->returnJson(['state' => 'success', 'balance' => 0]);
|
return $this->returnJson(['state' => 'success', 'balance' => 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->returnJson(['state' => 'success', 'balance' => $state_balances[0]->amount]);
|
return $this->returnJson(['state' => 'success', 'balance' => $state_balance->amount]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -44,7 +44,7 @@ class StateBalance extends Entity
|
|||||||
return $dateOrTime->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT);
|
return $dateOrTime->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT);
|
||||||
} else {
|
} else {
|
||||||
var_dump($dateOrTime);
|
var_dump($dateOrTime);
|
||||||
debug_print_backtrace(0, 4);
|
debug_print_backtrace(0, 6);
|
||||||
die("date or time unexpected object");
|
die("date or time unexpected object");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ class StateBalancesControllerTest extends TestCase
|
|||||||
'app.TransactionCreations',
|
'app.TransactionCreations',
|
||||||
'app.Transactions',
|
'app.Transactions',
|
||||||
'app.StateUsers',
|
'app.StateUsers',
|
||||||
|
'app.StateUserTransactions',
|
||||||
'app.StateErrors',
|
'app.StateErrors',
|
||||||
'app.TransactionSignatures',
|
'app.TransactionSignatures',
|
||||||
'app.TransactionSendCoins',
|
'app.TransactionSendCoins',
|
||||||
@ -73,7 +74,7 @@ class StateBalancesControllerTest extends TestCase
|
|||||||
'StateUser' => [
|
'StateUser' => [
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
'email_checked' => 1,
|
'email_checked' => 1,
|
||||||
'public_hex' => '8190bda585ee5f1d9fbf7d06e81e69ec18e13376104cff54b7457eb7d3ef710d'
|
'public_hex' => 'f7f4a49a4ac10379f8b9ddcb731c4d9ec495e6edd16075f52672cd25e3179f0f'
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
//echo "balance: $balance";
|
//echo "balance: $balance";
|
||||||
|
|||||||
@ -120,14 +120,5 @@ class StateBalancesTableTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->markTestIncomplete('Not implemented yet.');
|
$this->markTestIncomplete('Not implemented yet.');
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* calculate balance at end of month
|
|
||||||
* work only if state balance at begin of month exist
|
|
||||||
* use transaction_send_coins and transaction_creations
|
|
||||||
*/
|
|
||||||
//public function updateLastStateBalanceOfMonth($month, $year, $state_user_id)
|
|
||||||
public function testUpdateLastStateBalanceOfMonth()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user