diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php
index a0b3142e0..c87a3032b 100644
--- a/src/Controller/StateBalancesController.php
+++ b/src/Controller/StateBalancesController.php
@@ -228,6 +228,7 @@ class StateBalancesController extends AppController
$decay_duration = $current_state_balance->decayDuration($date);
$current_state_balance->amount = $current_state_balance->partDecay($date);
+ echo "amount: ". ($current_state_balance->amount / 10000) . ", duration: " . $decay_duration . "
";
$decay_transaction = [
'type' => 'decay',
'balance' => -($prev_amount - $current_state_balance->amount),
@@ -239,11 +240,17 @@ class StateBalancesController extends AppController
$cursor++;
}
+ 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->record_date = $date;
- $current_state_balance->amount += $transaction['balance'];
}
- echo "amount: ". $current_state_balance->amount . ", duration: " . $current_state_balance->decayDuration(Time::now()) . "
";
+ echo "amount: ". ($current_state_balance->amount / 10000) . ", duration: " . $current_state_balance->decayDuration(Time::now()) . "
";
array_push($transactions_reversed, [
'type' => 'decay',
'balance' => -($current_state_balance->amount - $current_state_balance->decay),
diff --git a/src/Model/Table/StateBalancesTable.php b/src/Model/Table/StateBalancesTable.php
index 2d00b3a96..64606e941 100644
--- a/src/Model/Table/StateBalancesTable.php
+++ b/src/Model/Table/StateBalancesTable.php
@@ -8,6 +8,7 @@ use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
use Cake\I18n\Date;
+use Cake\I18n\Time;
/**
* StateBalances Model
@@ -102,23 +103,33 @@ class StateBalancesTable extends Table
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)
{
//'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() . "
";
$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])
+ ->where(['AND' => [
+ 'state_user_id' => $state_user_id,
+ 'record_date >=' => $first_of_month,
+ 'record_date <=' => $last_of_month
+ ]
+ ])
+ ->order(['record_date' => 'ASC'])
->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])