mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
update state balance if not found, recursive
This commit is contained in:
parent
e56cd1501d
commit
e8db92c63c
@ -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),
|
||||||
|
|||||||
@ -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])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user