add stats about credit in use account and update dates for home graph

This commit is contained in:
osaajani 2021-06-14 19:48:42 +02:00
parent 03ae69b82a
commit c3637ab3ab
9 changed files with 90 additions and 31 deletions

View File

@ -84,10 +84,9 @@ class Quota extends StandardController
{
$result = $this->get_model()->consume_credit($id_user, $quantity);
//Enqueue verifications for quotas alerting
$queue = msg_get_queue(QUEUE_ID_QUOTA);
$message = ['id_user' => $id_user];
msg_send($queue, QUEUE_TYPE_QUOTA, $message, true, true);
//Write event
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'QUOTA_CONSUME', 'Consume ' . $quantity . ' credits of SMS quota.');
return $result;
}
@ -260,7 +259,7 @@ class Quota extends StandardController
}
echo "Update quota : " . $quota['id'] . "\n";
$internal_event->create($quota['id_user'], 'QUOTA_RENEWAL', 'Renew quota ' . $quota['id'] . ' report ' . $report . ' credits.');
$internal_event->create($quota['id_user'], 'QUOTA_RENEWAL', 'Renew quota and report ' . $report . ' credits.');
}
}

View File

@ -222,7 +222,7 @@ namespace controllers\internals;
//If we reached our max quota, do not send the message
$internal_quota = new Quota($this->bdd);
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
if ($internal_quota->has_enough_credit($id_user, $nb_credits))
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
{
$return['error'] = false;
$return['error_message'] = 'Not enough credit to send message.';

View File

@ -14,11 +14,13 @@ namespace controllers\publics;
class Account extends \descartes\Controller
{
public $internal_user;
public $internal_quota;
public function __construct()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_user = new \controllers\internals\User($bdd);
$this->internal_quota = new \controllers\internals\Quota($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -28,7 +30,9 @@ namespace controllers\publics;
*/
public function show()
{
$this->render('account/show');
$quota = $this->internal_quota->get_user_quota($_SESSION['user']['id']);
$quota_percent = $this->internal_quota->get_usage_percentage($_SESSION['user']['id']);
$this->render('account/show', ['quota' => $quota, 'quota_percent' => $quota_percent]);
}
/**

View File

@ -22,6 +22,7 @@ namespace controllers\publics;
private $internal_group;
private $internal_scheduled;
private $internal_event;
private $internal_quota;
/**
* Cette fonction est appelée avant toute les autres :
@ -39,6 +40,7 @@ namespace controllers\publics;
$this->internal_group = new \controllers\internals\Group($bdd);
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
$this->internal_quota = new \controllers\internals\Quota($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -60,40 +62,50 @@ namespace controllers\publics;
$nb_sendeds = $this->internal_sended->count_for_user($id_user);
$nb_receiveds = $this->internal_received->count_for_user($id_user);
//Création de la date d'il y a une semaine
$now = new \DateTime();
$one_week = new \DateInterval('P7D');
$date = $now->sub($one_week);
$formated_date = $date->format('Y-m-d');
//Récupération des 10 derniers Sms envoyés, Sms reçus et evenements enregistrés. Par date.
$sendeds = $this->internal_sended->get_lasts_by_date_for_user($id_user, 10);
$receiveds = $this->internal_received->get_lasts_by_date_for_user($id_user, 10);
$events = $this->internal_event->get_lasts_by_date_for_user($id_user, 10);
//Récupération du nombre de Sms envoyés et reçus depuis les 7 derniers jours
$nb_sendeds_by_day = $this->internal_sended->count_by_day_since_for_user($id_user, $formated_date);
$nb_receiveds_by_day = $this->internal_received->count_by_day_since_for_user($id_user, $formated_date);
//Récupération du nombre de Sms envoyés et reçus depuis 1 mois jours ou depuis le début du quota si il existe
//Création de la date d'il y a 30 jours
$now = new \DateTime();
$one_month = new \DateInterval('P1M');
$stats_start_date = clone $now;
$stats_start_date->sub($one_month);
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
//If user have a quota and the quota start before today, use quota start date instead
$quota = $this->internal_quota->get_user_quota($id_user);
if ($quota && (new \DateTime($quota['start_date']) <= $now) && (new \DateTime($quota['expiration_date']) > $now))
{
$stats_start_date = new \DateTime($quota['start_date']);
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
}
$nb_sendeds_by_day = $this->internal_sended->count_by_day_since_for_user($id_user, $stats_start_date_formated);
$nb_receiveds_by_day = $this->internal_received->count_by_day_since_for_user($id_user, $stats_start_date_formated);
//On va traduire ces données pour les afficher en graphique
$array_area_chart = [];
$today_less_7_day = new \DateTime();
$today_less_7_day->sub(new \DateInterval('P7D'));
$increment_day = new \DateInterval('P1D');
$date = clone $stats_start_date;
$one_day = new \DateInterval('P1D');
$i = 0;
//On va construire un tableau avec la date en clef, et les données pour chaque date
while ($i < 7)
while ($date <= $now)
{
$today_less_7_day->add($increment_day);
++$i;
$date_f = $today_less_7_day->format('Y-m-d');
$date_f = $date->format('Y-m-d');
$array_area_chart[$date_f] = [
'period' => $date_f,
'sendeds' => 0,
'receiveds' => 0,
];
$date->add($one_day);
}
$total_sendeds = 0;
@ -112,8 +124,9 @@ namespace controllers\publics;
$total_receiveds += $nb_received;
}
$avg_sendeds = round($total_sendeds / 7, 2);
$avg_receiveds = round($total_receiveds / 7, 2);
$nb_days = $stats_start_date->diff($now)->days;
$avg_sendeds = round($total_sendeds / $nb_days, 2);
$avg_receiveds = round($total_receiveds / $nb_days, 2);
$array_area_chart = array_values($array_area_chart);

View File

@ -58,6 +58,17 @@ class User extends \descartes\Controller
{
$quota_percentage = $this->internal_quota->get_usage_percentage($entity['id']);
$entity['quota_percentage'] = $quota_percentage * 100;
$quota = $this->internal_quota->get_user_quota($entity['id']);
if (!$quota)
{
continue;
}
if (new \DateTime() > new \DateTime($quota['expiration_date']))
{
$entity['quota_expired_at'] = $quota['expiration_date'];
}
}
header('Content-Type: application/json');
@ -300,6 +311,7 @@ class User extends \descartes\Controller
return $this->redirect(\descartes\Router::url('User', 'add'));
}
$nb_update = 0;
$users = $_POST['users'] ?? [];
foreach ($users as $id_user => $user)
{
@ -331,6 +343,7 @@ class User extends \descartes\Controller
return $this->redirect(\descartes\Router::url('User', 'add'));
}
//Forge quota for user if needed
$quota = false;
if ($quota_enable)
@ -384,13 +397,22 @@ class User extends \descartes\Controller
if (!$success)
{
\FlashMessage\FlashMessage::push('danger', 'L\'utilisateur #' . (int) $id_user . ' n\'as pas pu être mis à jour.');
continue;
}
return $this->redirect(\descartes\Router::url('User', 'list'));
$nb_update++;
}
if ($nb_update != count($users))
{
\FlashMessage\FlashMessage::push('danger', 'Certains utilisateurs n\'ont pas pu être mis à jour.');
return $this->redirect(\descartes\Router::url('User', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Tous les utilisateurs ont bien été mis à jour.');
return $this->redirect(\descartes\Router::url('User', 'list'));
}
}

View File

@ -39,7 +39,7 @@ namespace models;
FROM quota
WHERE id_user = :id_user
AND start_date <= :at
AND end_date > :at';
AND expiration_date > :at';
$params = [
'id_user' => $id_user,

View File

@ -81,7 +81,22 @@
</div>
<?php } ?>
</div>
<div class="col-xs-12 col-md-6">
<div class="col-xs-12 col-md-6">
<?php if ($quota) { ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-area-chart fa-fw"></i> Quota de SMS</h4>
</div>
<div class="panel-body">
<strong>Crédit de base :</strong> <?php $this->s($quota['credit']); ?><br/>
<strong>Crédit additionel :</strong> <?php $this->s($quota['additional']); ?><br/>
<strong>Crédit consommés :</strong> <?php $this->s($quota['consumed']); ?> (<?= $quota_percent * 100; ?>%)<br/>
<strong>Renouvellement automatique :</strong> <?php $this->s(($quota['auto_renew'] ? 'Oui, renouvellement le ' : 'Non, fin le ') . $quota['expiration_date']); ?><br/>
<strong>Report des crédits non utilisés :</strong> <?= $quota['report_unused'] ? 'Oui' : 'Non'; ?><br/>
<strong>Report des crédits additionels non utilisés :</strong> <?= $quota['report_unused_additional'] ? 'Oui' : 'Non'; ?><br/>
</div>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-at fa-fw"></i> Modifier e-mail</h4>

View File

@ -62,6 +62,9 @@
</div>
<fieldset>
<legend>Quota de SMS</legend>
<?php if (($user['quota']['expiration_date'] ?? false) && (new \DateTime() > new \DateTime($user['quota']['expiration_date']))) { ?>
<div class="alert alert-danger text-left">Le quota de cet utilisateur est expiré depuis le <b><?php $this->s($user['quota']['expiration_date']); ?></b> est n'as pas été renouvelé, il n'est donc plus appliqué !</div>
<?php } ?>
<div class="form-group">
<label>Définir un quota pour cet utilisateur : </label>

View File

@ -95,8 +95,11 @@ jQuery(document).ready(function ()
{
data: 'quota_percentage',
render: function (data, type, row, meta) {
return jQuery.fn.dataTable.render.text().display(data) + "%";
return '<input name="user_ids[]" type="checkbox" value="' + data + '">';
var html = jQuery.fn.dataTable.render.text().display(data) + "%";
if (row['quota_expired_at'] !== undefined) {
html += ' - <span class="danger">Quota expiré le ' + jQuery.fn.dataTable.render.text().display(row['quota_expired_at']) + '</span>';
}
return html;
},
},
{