Skip to content

Commit

Permalink
Add ActivePaidAccessesMeasurement, fix measurements graph keys
Browse files Browse the repository at this point in the history
remp/novydenik#1241
  • Loading branch information
Matus Kalafut committed Jun 20, 2024
1 parent d1e86ae commit 608ee9d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/FamilyModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Crm\FamilyModule\Models\Scenarios\IsFamilyMasterCriteria;
use Crm\FamilyModule\Models\Scenarios\IsFamilySlaveCriteria;
use Crm\FamilyModule\Seeders\FamilySeeder;
use Crm\FamilyModule\Seeders\MeasurementsSeeder;
use Crm\FamilyModule\Seeders\SubscriptionExtensionMethodsSeeder;
use Crm\FamilyModule\Seeders\SubscriptionTypeNamesSeeder;
use Crm\PaymentsModule\Events\BeforeCreateRenewalPaymentEvent;
Expand Down Expand Up @@ -133,6 +134,7 @@ public function registerSeeders(SeederManager $seederManager)
$seederManager->addSeeder($this->getInstance(FamilySeeder::class));
$seederManager->addSeeder($this->getInstance(SubscriptionExtensionMethodsSeeder::class));
$seederManager->addSeeder($this->getInstance(SubscriptionTypeNamesSeeder::class));
$seederManager->addSeeder($this->getInstance(MeasurementsSeeder::class));
}

public function registerScenariosCriteria(ScenariosCriteriaStorage $scenariosCriteriaStorage)
Expand Down
91 changes: 91 additions & 0 deletions src/Measurements/ActivePaidAccessesMeasurement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Crm\FamilyModule\Measurements;

use Crm\ApplicationModule\Models\Measurements\BaseMeasurement;
use Crm\ApplicationModule\Models\Measurements\Criteria;
use Crm\ApplicationModule\Models\Measurements\Point;
use Crm\ApplicationModule\Models\Measurements\Series;

class ActivePaidAccessesMeasurement extends BaseMeasurement
{
public const CODE = 'subscriptions.active_paid_accesses';

public function calculate(Criteria $criteria): Series
{
$series = $criteria->getEmptySeries();

$date = clone $criteria->getFrom();
while ($date <= $criteria->getTo()) {
$next = $criteria->getAggregation()->nextDate($date);

$total = $this->countActivePayingSubscribers($date, $next) + $this->countUnusedFamilyRequests($date, $next);

$point = new Point($criteria->getAggregation(), $total, clone $date);
$series->setPoint($point);

$date = $next;
}
return $series;
}

private function countActivePayingSubscribers($date, $next): int
{
$query = "
SELECT is_paid, COUNT(DISTINCT subscriptions.user_id) AS count
FROM subscriptions
JOIN users ON users.id = subscriptions.user_id
WHERE ?
GROUP BY is_paid
";

$rows = $this->db()->query(
$query,
[
'start_time <' => $next,
'end_time >=' => $date,
'users.active' => 1,
'subscription_type_id NOT' => $this->db()::literal(
'IN (SELECT master_subscription_type_id FROM family_subscription_types)'
),
],
);

foreach ($rows as $row) {
if ($row->is_paid === 1) {
return $row->count;
}
}

return 0;
}

private function countUnusedFamilyRequests($date, $next): int
{
$query = "
SELECT COUNT(*) AS count
FROM family_requests
JOIN subscriptions ON family_requests.master_subscription_id = subscriptions.id
WHERE ?
";

$row = $this->db()->fetch(
$query,
[
'subscriptions.start_time <' => $next,
'subscriptions.end_time >=' => $date,
'family_requests.created_at <' => $next,
$this->db()::literal('?or', [
'accepted_at' => null,
'accepted_at >' => $next,
]),
$this->db()::literal('?or', [
'canceled_at' => null,
'canceled_at <' => $next,
]),
],
);

return $row->count;
}
}
31 changes: 31 additions & 0 deletions src/Seeders/MeasurementsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Crm\FamilyModule\Seeders;

use Crm\ApplicationModule\Repositories\MeasurementsRepository;
use Crm\ApplicationModule\Seeders\ISeeder;
use Crm\ApplicationModule\Seeders\MeasurementsTrait;
use Crm\FamilyModule\Measurements\ActivePaidAccessesMeasurement;
use Symfony\Component\Console\Output\OutputInterface;

class MeasurementsSeeder implements ISeeder
{
use MeasurementsTrait;

private MeasurementsRepository $measurementsRepository;

public function __construct(MeasurementsRepository $measurementsRepository)
{
$this->measurementsRepository = $measurementsRepository;
}

public function seed(OutputInterface $output)
{
$this->addMeasurement(
$output,
ActivePaidAccessesMeasurement::CODE,
'family.measurements.active_paid_accesses.title',
'family.measurements.active_paid_accesses.description',
);
}
}
5 changes: 5 additions & 0 deletions src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ services:
- Crm\FamilyModule\Seeders\FamilySeeder
- Crm\FamilyModule\Seeders\SubscriptionExtensionMethodsSeeder
- Crm\FamilyModule\Seeders\SubscriptionTypeNamesSeeder
- Crm\FamilyModule\Seeders\MeasurementsSeeder

# forms
- Crm\FamilyModule\Forms\RequestFormFactory
Expand All @@ -58,3 +59,7 @@ services:
setup:
# register subscription extension method for family subscription types
- registerExtension(Crm\FamilyModule\Models\Extension\ExtendFamilyExtension::METHOD_CODE, Crm\FamilyModule\Models\Extension\ExtendFamilyExtension)

measurementManager:
setup:
- register(Crm\FamilyModule\Measurements\ActivePaidAccessesMeasurement())
5 changes: 5 additions & 0 deletions src/lang/family.cs_CZ.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ admin:
label: Nákup bez DPH
description: Odečte od všech položek DPH. Zálohová i daňová faktura budou bez DPH. Používá se pouze pro zahraniční firmy s platným DIČ nebo firmy nakupující z mimo EU.
send: Pridej

measurements:
active_paid_accesses:
title: 'S placeným předplatným + neaktivované dceřiné'
description: 'Počet předplatitelů s aktivním placeným předplatným + počet neaktivovaných firemních/rodinných přístupů. Jeden uživatel je započítan jednou i když má více předplatných.'
5 changes: 5 additions & 0 deletions src/lang/family.en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ admin:
label: Use 0% VAT
description: Deducts VAT from all items. Both the advance and the tax invoice will be without VAT. It is supposed to be used only for foreign companies with a valid VAT number or companies purchasing from outside the EU.
send: Add

measurements:
active_paid_accesses:
title: 'Paying subscribers + unused company'
description: 'Number of active subscribers with paid subscription + number of unused company/family accesses. One user is counted once even if he has multiple subscriptions.'
5 changes: 5 additions & 0 deletions src/lang/family.sk_SK.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ admin:
label: Nákup bez DPH
description: Odpočíta od všetkých položiek DPH. Zálohová aj daňová faktúra budú bez DPH. Používa sa len pre zahraničné firmy s platným IČ DPH alebo firmy nakupujúce z mimo EU.
send: Pridaj

measurements:
active_paid_accesses:
title: 'S plateným predplatným + neaktivované dcérske'
description: 'Počet predplatiteľov s aktívnym plateným predplatným + počet neaktivovaných firemných/rodinných prístupov. Jeden používateľ je započítaný jeden krát aj keď má viac predplatných.'

0 comments on commit 608ee9d

Please sign in to comment.