From a8bb5a5c7c4ae0c311b7cd62aee2829a11676511 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Tue, 5 May 2020 09:53:31 +0000 Subject: [PATCH] update craetion validation with target date --- .../TransactionCreationsController.php | 27 +++++++++- .../Transactions/TransactionCreation.php | 51 ++++++++++++++++--- .../TransactionCreations/create_multi.ctp | 11 ++-- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/Controller/TransactionCreationsController.php b/src/Controller/TransactionCreationsController.php index 910c73fcd..83f7dfe6a 100644 --- a/src/Controller/TransactionCreationsController.php +++ b/src/Controller/TransactionCreationsController.php @@ -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 . ' ' . $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')); diff --git a/src/Model/Transactions/TransactionCreation.php b/src/Model/Transactions/TransactionCreation.php index af640a95d..ddcd33816 100644 --- a/src/Model/Transactions/TransactionCreation.php +++ b/src/Model/Transactions/TransactionCreation.php @@ -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
"; //$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())); diff --git a/src/Template/TransactionCreations/create_multi.ctp b/src/Template/TransactionCreations/create_multi.ctp index 346326fee..76a57319e 100644 --- a/src/Template/TransactionCreations/create_multi.ctp +++ b/src/Template/TransactionCreations/create_multi.ctp @@ -73,8 +73,8 @@ use Cake\I18n\FrozenTime; Form->control('target_date', ['value' => $firstDayLastMonth]); ?> 20000000) { - $disable = 'disabled'; + if($activeUser['id'] == $possibleReceiver['id'] || $possibleReceiver['amount'] > 30000000) { + //$disable = 'disabled'; } ?>
@@ -104,7 +104,12 @@ use Cake\I18n\FrozenTime; - In diesem und letztem Monat bereits geschöpft: element('printGradido', ['number' => $possibleReceiver['amount']]);?> + In diesen und den letzten 2 Monaten bereits geschöpft (alte Berechnung): element('printGradido', ['number' => $possibleReceiver['amount']]);?> + + + 0) : ?> + + Im letzten Monat geschöpft (neue Berechnung): element('printGradido', ['number' => $possibleReceiver['amount2']]) ?>