update state balance if not found, recursive

This commit is contained in:
Dario Rekowski on RockPI 2021-01-11 09:44:46 +00:00 committed by Ulf Gebhardt
parent e56cd1501d
commit e8db92c63c
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 26 additions and 8 deletions

View File

@ -228,6 +228,7 @@ class StateBalancesController extends AppController
$decay_duration = $current_state_balance->decayDuration($date); $decay_duration = $current_state_balance->decayDuration($date);
$current_state_balance->amount = $current_state_balance->partDecay($date); $current_state_balance->amount = $current_state_balance->partDecay($date);
echo "amount: ". ($current_state_balance->amount / 10000) . ", duration: " . $decay_duration . "<br>";
$decay_transaction = [ $decay_transaction = [
'type' => 'decay', 'type' => 'decay',
'balance' => -($prev_amount - $current_state_balance->amount), 'balance' => -($prev_amount - $current_state_balance->amount),
@ -239,11 +240,17 @@ class StateBalancesController extends AppController
$cursor++; $cursor++;
} }
$current_state_balance->record_date = $date; if($current_state_balance->record_date != $date) {
if($transaction['type'] == 'send') {
$current_state_balance->amount -= $transaction['balance'];
} else {
$current_state_balance->amount += $transaction['balance']; $current_state_balance->amount += $transaction['balance'];
}
}
$current_state_balance->record_date = $date;
} }
echo "amount: ". $current_state_balance->amount . ", duration: " . $current_state_balance->decayDuration(Time::now()) . "<br>"; echo "amount: ". ($current_state_balance->amount / 10000) . ", duration: " . $current_state_balance->decayDuration(Time::now()) . "<br>";
array_push($transactions_reversed, [ array_push($transactions_reversed, [
'type' => 'decay', 'type' => 'decay',
'balance' => -($current_state_balance->amount - $current_state_balance->decay), 'balance' => -($current_state_balance->amount - $current_state_balance->decay),

View File

@ -8,6 +8,7 @@ use Cake\Validation\Validator;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\I18n\Date; use Cake\I18n\Date;
use Cake\I18n\Time;
/** /**
* StateBalances Model * StateBalances Model
@ -102,23 +103,33 @@ class StateBalancesTable extends Table
return ['state' => 'error', 'msg' => 'couldn\'t save', 'details' => $entity->getErrors()]; 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 // getting start balance for month, if exist else create and create all missing state_balances before, in while loop
public function chooseForMonthAndUser($month, $year, $state_user_id) public function chooseForMonthAndUser($month, $year, $state_user_id)
{ {
//'created' => 'identifier' //'created' => 'identifier'
$first_of_month = new Time("$year-$month-01 00:00");
$last_of_month = new Time($first_of_month);
$last_of_month->addMonth(1);
$last_of_month->subSecond(1);
echo "first of month: " . $first_of_month->i18nFormat() . ", last of month: " . $last_of_month->i18nFormat() . "<br>";
$query = $this->find('all'); $query = $this->find('all');
$query->select([ $query->select([
'month' => $query->func()->month(['record_date' => 'identifier']), 'month' => $query->func()->month(['record_date' => 'identifier']),
'year' => $query->func()->year(['record_date' => 'identifier']) 'year' => $query->func()->year(['record_date' => 'identifier'])
])->select($this) ])->select($this)
//->where(['month' => $month, 'year' => $year, 'state_user_id' => $state_user_id]) //->where(['month' => $month, 'year' => $year, 'state_user_id' => $state_user_id])
->where(['state_user_id' => $state_user_id]) ->where(['AND' => [
'state_user_id' => $state_user_id,
'record_date >=' => $first_of_month,
'record_date <=' => $last_of_month
]
])
->order(['record_date' => 'ASC'])
->contain([]); ->contain([]);
// TODO: fix query with correct month and year
//debug($query);
if($query->count() == 0) if($query->count() == 0)
{ {
// if any state balance for user exist, pick last one // if any state balance for user exist, pick last one
$state_balances = $this->find('all') $state_balances = $this->find('all')
->where(['state_user_id' => $state_user_id]) ->where(['state_user_id' => $state_user_id])