Commit 811866d6 authored by okhin's avatar okhin 🚴

Adding a recompute total functions in the admin page

parent 094f8ebf
Pipeline #2293 canceled with stages
......@@ -10,6 +10,7 @@ use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationCreateCommand;
use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
class Admin extends Controller
{
......@@ -46,12 +47,21 @@ class Admin extends Controller
99 => "résilié",
100 => "non applicable",
));
// Test si il y a des messages
if (!$f3->exists('SESSION.message')) {
$f3->push('SESSION.message', '');
}
if (!$f3->exists('SESSION.error')) {
$f3->push('SESSION.error', '');
}
if (!$f3->get('SESSION.admin')) {
$mapper = new\DB\SQL\Mapper($f3->get('DB'), 'admins');
$auth = new \Auth($mapper, array('id'=>'user_id', 'pw'=>'password'));
$success = $auth->basic('\Controller\Admin::hash_password');
if (!$success) {
$f3->push('SESSION.error', 'Accès non autorisé');
$f3->error(401);
} else {
$f3->set('SESSION.admin', true);
......@@ -61,6 +71,23 @@ class Admin extends Controller
$f3->set('PIPLOME_URL', PIPLOME_URL);
}
public function afterRoute($f3, $args)
{
// Rendu de la page
if ($this->template!='') {
echo \Template::instance()->render($this->template);
}
// Une fois que tout est affiché, on peut supprimer les notifications
$f3->clear('SESSION.message');
$f3->clear('SESSION.error');
}
public static function recompute($f3, $args)
{
$f3->get('container')['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$f3->set('block_content', 'backend/dons.html');
}
public static function hash_password($password)
{
return hash('sha256', $password);
......@@ -140,6 +167,7 @@ class Admin extends Controller
$cumul = $user_amount['total'] + $result['somme'];
}
if ($error != '') {
$f3->push('SESSION.error', $error);
} else {
$db->query(
"UPDATE dons
......@@ -190,6 +218,7 @@ class Admin extends Controller
}
}
if ($error != '') {
$f3->push('SESSION.error', $error);
} else {
$db->query(
"UPDATE dons
......@@ -990,8 +1019,8 @@ class Admin extends Controller
}
}
$f3->set('result', $result);
$f3->set('error', $error);
$f3->set('message', $message);
$f3->push('SESSION.error', $error);
$f3->push('SESSION.message', $message);
$f3->set('block_content', 'backend/banque.html');
}
......
......@@ -29,6 +29,7 @@ GET @edit_support:/admin/support/edit/@id=Controller\Admin->support
POST @modify_support:/admin/support/edit=Controller\Admin->support
GET @invalidate_support:/admin/support/invalidate/@id=Controller\Admin->invalidate
GET @validate_support:/admin/support/validate/@id=Controller\Admin->validate
GET @recompute:/admin/recompute=Controlle\Admin->recompute
GET|POST @admin_accounts:/admin/accounts=Controller\Admin->accounts
......
......@@ -2,8 +2,8 @@
<h2>Gestion banque</h2>
<check if="{{@result}}"><p class="result">{{@result | raw}}</p></check>
<check if="{{@error}}"><p class="error">{{@error | raw}}</p></check>
<check if="{{@message}}"><p class="message">{{@message | raw}}</p></check>
<check if="{{@SESSION.error}}"><message messages="{{@SESSION.error}}" class="alert alert-danger"/></check>
<check if="{{@SESSION.message}}"><message messages="{{@SESSION.message}}" class="alert alert-danger" /></p></check>
<section id="cbs">
<h3 >Mise à jour des dates d'expiration des CB</h3>
......
......@@ -52,9 +52,8 @@
<input type="submit" value="Voir ce don" />
</form>
<br />
<check if="{{ @msg }}">
<span class="error">{{ @msg }}</span>
</check>
<check if="{{ @SESSION.error }}"> <message messages="{{ @SESSION.error }}" class="alert alert-danger" /> </check>
<check if="{{ @SESSION.message }}"> <message messages="{{ @SESSION.message }}" class="alert alert-default" /> </check>
</section>
<script type="text/javascript">
......
......@@ -45,6 +45,8 @@
<input type="submit" value="Filtrer" />
</form>
<a href="{{ 'recompute' | alias }}">Recalculer les totaux et cumuls de toute la base de donnée</a>
<br />
<check if="{{ @row_count }}"><b>{{ @row_count }} résultats</b>
<br />
<table cellspacing="0" cellpadding="0">
......
......@@ -154,7 +154,7 @@
</nav>
</div>
<section id="messages">
<message messages="@SESSION.message" class="alert alert-default">
<message messages="@SESSION.error" class="alert alert-danger">
<message messages="{{ @SESSION.message }}" class="alert alert-default">
<message messages="{{ @SESSION.error }}" class="alert alert-danger">
</section>
<?php
namespace LQDN\Command;
class AdminUpdateTotalUsersCommand
{
// There is no parameters for this command.
}
......@@ -7,6 +7,7 @@ use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
class UserHandler
{
......@@ -79,4 +80,23 @@ class UserHandler
]
);
}
/**
* Get through all the users and databases to recompute all total from users.
*
* @param AdminUpdateTotalUsersCommand $command
*/
public function handleAdminUpdateTotalUsersCommand(AdminUpdateTotalUsersCommand $command)
{
// Let's get all users first
$user_ids = $this->connection->executeQuery('SELECT id FROM users')->fetchAll(\PDO::FETCH_ASSOC);
foreach ($user_ids as $user_id) {
$user_id = (int) $user_id;
$total = $this->connection->executeQuery('SELECT SUM(somme) AS total FROM dons WHERE user_id = :user_id AND STATUS IN (1, 102)', [
'user_id' => $user_id
])->fetch(\PDO::FETCH_ASSOC);
$this->connection->executeUpdate('UPDATE users SET total = :total WHERE id = :user_id', ['total' => $total['total'], 'user_id' => $user_id]);
}
}
}
......@@ -6,10 +6,17 @@ use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\UserCreateCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
use LQDN\Tests\Functional\FunctionalTest;
class UserHandlerTest extends FunctionalTest
{
public function testAdminUpdateTotalUsersCommand()
{
$this->container['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$this->assertSame(1000, (int) $this->getUser(1)['total']);
}
public function testUserUpdateFromAdmin()
{
$user = $this->getUser(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment