Skip to content

Commit

Permalink
Merge pull request #3 from makasim/user-created-at
Browse files Browse the repository at this point in the history
User created at
  • Loading branch information
makasim authored May 20, 2019
2 parents 2406d66 + 754ede3 commit 76c3fda
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Behavior/RegistrationBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private function createUser(Update $update): User
$user->setEnabled(false);
$user->setRoles([User::ROLE_USER]);
$user->setPassword($this->passwordEncoder->encodePassword($user, uniqid('pass')));
$user->setCreatedDate(new \DateTime('now'));

if ($contact = $update->getMessage()->getContact()) {
$user->setName(trim($contact->getFirstName().' '.$contact->getLastName()));
Expand Down
4 changes: 2 additions & 2 deletions src/Behavior/SetReminderDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute(Token $token)
/** @var Order $order */
$order = $orders[0];
$reminderDate = clone $order->getUpdatedAt();
$reminderDate->setTime(0, 0, 0);
$reminderDate->setTime(13, 0, 0);
$reminderDate->modify('+7 days');
} elseif ($count >= 2) {
// set reminder date only if time between orders is in range >= 5days <= 14days
Expand All @@ -65,7 +65,7 @@ public function execute(Token $token)

if ($diff >= $_5Days && $diff <= $_14Days) {
$reminderDate = clone $orderA->getUpdatedAt();
$reminderDate->setTime(0, 0, 0);
$reminderDate->setTime(13, 0, 0);
$reminderDate->add(new \DateInterval(sprintf('PT%sS', $diff)));
}
}
Expand Down
92 changes: 85 additions & 7 deletions src/Controller/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public function __construct(OrderStorage $orderStorage, UserStorage $userStorage
*/
public function __invoke(): Response
{
$threeMonthsAgo = time() - 60*60*24*90;
$threeMonthsAgo = new \DateTime('now - 90 days');
$threeMonthsAgo->setTime(0, 0,0);

$now = new \DateTime('now');
$now->setTime(0, 0,0);

return new Response($this->twig->render('stats.html.twig', [
'finishedOrdersCountSum' => $this->calcFinishedOrdersSumStats($threeMonthsAgo),
'finishedOrdersCountStats' => $this->calcFinishedOrdersCount($threeMonthsAgo),
'canceledOrdersCountStats' => $this->calcCanceledOrdersCount($threeMonthsAgo),
'finishedOrdersCountSum' => $this->calcFinishedOrdersSumStats($threeMonthsAgo->getTimestamp()),
'finishedOrdersCountStats' => $this->calcFinishedOrdersCount($threeMonthsAgo->getTimestamp()),
'canceledOrdersCountStats' => $this->calcCanceledOrdersCount($threeMonthsAgo->getTimestamp()),
'userRegisteredSumStats' => $this->calcUserRegisteredSumStats($threeMonthsAgo->getTimestamp()),
'threeMonthsAgo' => $threeMonthsAgo,
'now' => $now,
]));
}

Expand Down Expand Up @@ -226,19 +233,90 @@ private function calcFinishedOrdersSumStats(int $from): array
$options = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
];
//echo '<pre>';

$data = [];
foreach ($this->orderStorage->getCollection()->aggregate($pipeline, $options) as $row) {
if (false == $row['date']) {
continue;
}
//var_dump($row);

$data[] = [
'x' => $row['date'],
'y' => round($row['sum'] / 100),
];
}
//echo '<pre />';

return $data;
}

private function calcUserRegisteredSumStats(int $from): array
{
// createdAt field was introduced.
$createdAtFeatureIntroduced = 1558224001;

$from = max($from, $createdAtFeatureIntroduced);

$pipeline = [
[
'$match' => [
'createdDate.unix' => ['$gte' => $from],
],
],
[
'$project' => [
'createdDate' => [
'$toDate' => [
'$multiply' => [1000, '$createdDate.unix'],
],
],
],
],
[
'$group' => [
'_id' => [
'day' => [
'$dayOfMonth' => '$createdDate',
],
'month' => [
'$month' => '$createdDate',
],
'year' => [
'$year' => '$createdDate',
],
],
'date' => [
'$first' => [
'$dateToString' => ['format' => '%Y-%m-%d', 'date' => '$createdDate'],
],
],
'count' => ['$sum' => 1],
],
],
[
'$sort' => [
'_id.year' => 1,
'_id.month' => 1,
'_id.day' => 1,
],
],
];

$options = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
];

$data = [];
foreach ($this->userStorage->getCollection()->aggregate($pipeline, $options) as $row) {
if (false == $row['date']) {
continue;
}

$data[] = [
'x' => $row['date'],
'y' => $row['count'],
];
}

return $data;
}
}
1 change: 1 addition & 0 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function create(
/** @var User $user */
$user = $form->getData();
$user->setId(Uuid::generate());
$user->setCreatedDate(new \DateTime('now'));

if ($user->isAdmin() || $user->isOperator()) {
$user->setEnabled(true);
Expand Down
2 changes: 1 addition & 1 deletion src/CustomAction/UserReminderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function handle(string $action, Update $update): void

$reminderDate = new \DateTime('today');
$reminderDate->add(new \DateInterval(sprintf('P%sD', $callbackData->getValue('i'))));
$reminderDate->setTime(0, 0, 0);
$reminderDate->setTime(13, 0, 0);

$user->setReminderDate($reminderDate);

Expand Down
3 changes: 3 additions & 0 deletions src/DataFixtures/YADM/LoadUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function load(ObjectManager $manager)
$user->setRoles([User::ROLE_ADMIN]);
$user->setPassword($password);
$user->setEnabled(true);
$user->setCreatedDate(new \DateTime('now'));

$manager->persist($user);
$manager->flush();
Expand All @@ -37,6 +38,7 @@ public function load(ObjectManager $manager)
$user->setRoles([User::ROLE_OPERATOR]);
$user->setPassword($password);
$user->setEnabled(true);
$user->setCreatedDate(new \DateTime('now'));

$manager->persist($user);
$manager->flush();
Expand All @@ -50,6 +52,7 @@ public function load(ObjectManager $manager)
$user->setRoles([User::ROLE_USER]);
$user->setPassword($password);
$user->setEnabled(true);
$user->setCreatedDate(new \DateTime('now'));

$manager->persist($user);
$manager->flush();
Expand Down
10 changes: 10 additions & 0 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public function getReminderDate(): ?\DateTime
return get_value($this, 'reminderDate', null, \DateTime::class);
}

public function setCreatedDate(\DateTime $date): void
{
set_value($this, 'createdDate', $date);
}

public function getCreatedDate(): \DateTime
{
return get_value($this, 'createdDate', null, \DateTime::class);
}

public function setNotifyNewOrder(bool $notify): void
{
set_value($this, 'notifyNewOrder', $notify);
Expand Down
51 changes: 48 additions & 3 deletions templates/stats.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<canvas id="finishedOrdersSumStats" width="400" height="200"></canvas>
<canvas id="finishedOrdersCountStats" width="400" height="200"></canvas>
<canvas id="canceledOrdersCountStats" width="400" height="200"></canvas>
<canvas id="userRegisteredSumStats" width="400" height="200"></canvas>
<script>
window.onload = function() {
moment.locale('ru');
Expand All @@ -38,7 +39,11 @@
}],
xAxes: [{
type: 'time',
parser: 'YYYY-MM-DD'
parser: 'YYYY-MM-DD',
time: {
min: {{ threeMonthsAgo|date('U') }}000,
max: {{ now|date('U') }}000
}
}]
}
}
Expand Down Expand Up @@ -66,7 +71,11 @@
}],
xAxes: [{
type: 'time',
parser: 'YYYY-MM-DD'
parser: 'YYYY-MM-DD',
time: {
min: {{ threeMonthsAgo|date('U') }}000,
max: {{ now|date('U') }}000
}
}]
}
}
Expand Down Expand Up @@ -94,7 +103,43 @@
}],
xAxes: [{
type: 'time',
parser: 'YYYY-MM-DD'
parser: 'YYYY-MM-DD',
time: {
min: {{ threeMonthsAgo|date('U') }}000,
max: {{ now|date('U') }}000
}
}]
}
}
});
new Chart(document.getElementById('userRegisteredSumStats'), {
type: 'line',
data: {
datasets: [{
"label": "Клиентов зарегистрированно",
data: {{ userRegisteredSumStats|json_encode|raw }},
backgroundColor: 'rgba(255, 159, 64, 0.2)',
borderColor: 'rgba(255, 159, 64, 1)',
borderWidth: 1,
fill: true,
lineTension: 0.4
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
type: 'time',
parser: 'YYYY-MM-DD',
time: {
min: {{ threeMonthsAgo|date('U') }}000,
max: {{ now|date('U') }}000
}
}]
}
}
Expand Down
37 changes: 24 additions & 13 deletions templates/users/user-edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
{% block content %}
<h2>Редактирование пользователя</h2>

{% if not isAdmin %}
<table class="table">
{% if not isAdmin %}

<tr>
<th>Телефон</th>
<td align="left">
{{ user.phone }}
{% if user.telegram %}
<a href="http://t.me/{{ user.telegram.username }}" class="btn btn-primary btn-sm" target="_blank">Telegram</a>
{% endif %}
</td>
</tr>
<tr>
<th>Имя</th>
<td>{{ user.name }}</td>
</tr>
{% endif %}
{% if user.reminderDate %}
<tr>
<th>Дата напоминания</th>
<td>{{ user.reminderDate|ldate('dd-MM-yyyy HH:mm::ss') }}</td>
</tr>
{% endif %}
<tr>
<th>Телефон</th>
<td align="left">
{{ user.phone }}
{% if user.telegram %}
<a href="http://t.me/{{ user.telegram.username }}" class="btn btn-primary btn-sm" target="_blank">Telegram</a>
{% endif %}
</td>
</tr>
<tr>
<th>Имя</th>
<td>{{ user.name }}</td>
<th>Дата регистрации</th>
<td>{{ user.createdDate|ldate('dd-MM-yyyy HH:mm::ss') }}</td>
</tr>
</table>
{% endif %}

{{ form_start(form) }}
{{ form_errors(form) }}
Expand Down

0 comments on commit 76c3fda

Please sign in to comment.