update craetion validation with target date

This commit is contained in:
Dario Rekowski on RockPI 2020-05-05 09:53:31 +00:00
parent 141cd6a112
commit a8bb5a5c7c
3 changed files with 78 additions and 11 deletions

View File

@ -208,6 +208,23 @@ class TransactionCreationsController extends AppController
foreach($transactionActiveMonth as $t) {
$transactionActiveMonthSortedById[$t['id']] = $t['received'];
}
$firstDayLastMonth = new FrozenDate();
$firstDayLastMonth = $firstDayLastMonth->day(1)->subMonth(1);
$transactionsLastMonthTargeDate = $this->TransactionCreations
->find('all')
//->select(['state_user_id', 'target_date', 'amount'])
->where(['EXTRACT(YEAR_MONTH FROM target_date) LIKE' => $firstDayLastMonth->format('Ym')])
->group(['state_user_id'])
->contain([]);
$transactionsLastMonthTargeDate->select([
'state_user_id',
'sum_amount' => $transactionsLastMonthTargeDate->func()->sum('amount')
]);
$transactionsLastMonthTargetDateSortedByStateUserId = [];
foreach($transactionsLastMonthTargeDate as $transactionCreation) {
$transactionsLastMonthTargetDateSortedByStateUserId[$transactionCreation->state_user_id] = $transactionCreation->sum_amount;
}
$stateUsers = $stateUserTable
->find('all')
@ -224,18 +241,25 @@ class TransactionCreationsController extends AppController
$possibleReceiver = [];
foreach($stateUsers as $stateUser) {
$sumAmount = 0;
$sumAmount2 = 0;
if(isset($transactionsLastMonthTargetDateSortedByStateUserId[$stateUser->id])) {
$sumAmount2 = $transactionsLastMonthTargetDateSortedByStateUserId[$stateUser->id];
}
foreach($stateUser->transaction_creations as $transactionCreation) {
//var_dump($transactionCreation);
if(isset($transactionActiveMonthSortedById[$transactionCreation->transaction_id])) {
$sumAmount += $transactionCreation->amount;
}
}
//if($sumAmount < 20000000) {
array_push($possibleReceiver, [
'name' => $stateUser->first_name . '&nbsp;' . $stateUser->last_name,
'id' => $stateUser->id,
'email' => $stateUser->email,
'amount' => $sumAmount
'amount' => $sumAmount,
'amount2' => $sumAmount2
]);
/*} else {
$this->Flash->error(__('Creation above 2.000 GDD for 2 last two month'));
@ -250,6 +274,7 @@ class TransactionCreationsController extends AppController
$timeUsed = microtime(true) - $startTime;
$this->set(compact('timeUsed', 'stateUsers', 'creationForm', 'possibleReceiver'));
$this->set('firstDayLastMonth', $firstDayLastMonth);
$this->set('activeUser', $user);
$this->set('creationForm', $creationForm);
$this->set('transactionExecutingCount', $session->read('Transaction.executing'));

View File

@ -7,7 +7,7 @@ namespace Model\Transactions;
use Cake\ORM\TableRegistry;
use Cake\Core\Configure;
use Cake\Mailer\Email;
use Cake\I18n\FrozenDate;
class TransactionCreation extends TransactionBase {
@ -113,11 +113,12 @@ class TransactionCreation extends TransactionBase {
// ident hash isn't collision ressistent, it is for speed up search
$identHashBin = pack('a32', $this->getIdentHash());
$q = $existingCreations = $this->transactionCreationsTable
////////// old validation not more than 3k GDD for 3 Month ///////////////
$existingCreations = $this->transactionCreationsTable
->find('all')
->select(['amount', 'state_user_id', 'target_date'])
->contain(['StateUsers' => ['fields' => ['StateUsers.public_key']]]);
->contain(['StateUsers' => ['fields' => ['StateUsers.public_key']]])
->where(['target_date' => NULL]);
//$targetDate = $this->protoTransactionCreation->getTargetDate();
//echo "choose existing transactions<br>";
//$existingCreations->where([$q->func()->extract('YEAR_MONTH', 'target_date') . ' LIKE ' . $q->func()->extract('YEAR_MONTH', $targetDate)]);
@ -125,7 +126,7 @@ class TransactionCreation extends TransactionBase {
// uncomment because ident hash didn't work at the moment
//->where(['ident_hash' => $identHashBin]);
//$existingCreations->select(['amount_sum' => $existingCreations->func()->sum('amount')]);
/* old validation not more than 3k GDD for 3 Month*/
$existingCreations->matching('Transactions', function ($q) {
return $q->where(
@ -147,11 +148,46 @@ class TransactionCreation extends TransactionBase {
$newSum += $creation->amount;
}
}
/*
if($newSum > 30000000) {
$this->addError('TransactionCreation::validate', 'Creation more than 1.000 GDD per Month (3 Month) not allowed');
return false;
}
//die("\n");
}*/
/////////////// new validation, not more than 1K GDD per month via target_date ///////////////////////////
$existingCreations2 = $this->transactionCreationsTable
->find('all')
->select(['amount', 'state_user_id', 'target_date'])
->contain(['StateUsers' => ['fields' => ['StateUsers.public_key']]]);
$q = $existingCreations2;
$targetDate = $this->protoTransactionCreation->getTargetDate();
$targetDateFrozen = new FrozenDate($targetDate->getSeconds());
$targetDateMonthYearConcat = $targetDateFrozen->format('Ym');
$existingCreations2->where([
'target_date IS NOT' => NULL,
'EXTRACT(YEAR_MONTH FROM target_date) LIKE ' => $targetDateMonthYearConcat
]);
$newSum2 = $this->getAmount();
foreach($existingCreations2 as $creation) {
$newSum2 += $creation->amount;
}
if(!$existingCreations2->count()) {
if($newSum > 30000000) {
$this->addError('TransactionCreation::validate', 'Creation more than 1.000 GDD per Month (3 Month) not allowed');
return false;
}
} else {
if($newSum2 > 10000000) {
$this->addError('TransactionCreation::validate', 'Creation more than 1.000 GDD per Month in target_date not allowed');
return false;
}
}
return true;
}
@ -171,6 +207,7 @@ class TransactionCreation extends TransactionBase {
$transactionCreationEntity->state_user_id = $receiverUserId;
$transactionCreationEntity->amount = $this->getAmount();
$transactionCreationEntity->ident_hash = $this->getIdentHash();
$transactionCreationEntity->target_date = $this->protoTransactionCreation->getTargetDate()->getSeconds();
if(!$this->transactionCreationsTable->save($transactionCreationEntity)) {
$this->addError('TransactionCreation::save', 'error saving transactionCreation with errors: ' . json_encode($transactionCreationEntity->getErrors()));

View File

@ -73,8 +73,8 @@ use Cake\I18n\FrozenTime;
<?= $this->Form->control('target_date', ['value' => $firstDayLastMonth]); ?>
<?php foreach($possibleReceiver as $possibleReceiver) :
$disable = null;
if($activeUser['id'] == $possibleReceiver['id'] || $possibleReceiver['amount'] > 20000000) {
$disable = 'disabled';
if($activeUser['id'] == $possibleReceiver['id'] || $possibleReceiver['amount'] > 30000000) {
//$disable = 'disabled';
}
?>
<div class="grd_big_checkbox">
@ -104,7 +104,12 @@ use Cake\I18n\FrozenTime;
<?php endif; ?>
<?php if($possibleReceiver['amount'] != 0) : ?>
<span class="grd_smaller">
In diesem und letztem Monat bereits geschöpft: <?= $this->element('printGradido', ['number' => $possibleReceiver['amount']]);?>
In diesen und den letzten 2 Monaten bereits geschöpft (alte Berechnung): <?= $this->element('printGradido', ['number' => $possibleReceiver['amount']]);?>
</span>
<?php endif; ?>
<?php if($possibleReceiver['amount2'] > 0) : ?>
<span class="grd_smaller">
Im letzten Monat geschöpft (neue Berechnung): <?= $this->element('printGradido', ['number' => $possibleReceiver['amount2']]) ?>
</span>
<?php endif; ?>
<?php if($disable != null) : ?>