PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Analytics / Services / SummaryService.php

SummaryService.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Analytics/Services/SummaryService.php

77 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore
2
3 /**
4 * SummaryService — assembles KPI data for the /analytics/summary endpoint.
5 *
6 * @package Disco
7 * @subpackage Disco\App\Analytics\Services
8 * @since 1.3.23
9 */
10
11 namespace Disco\App\Analytics\Services;
12
13 use Disco\App\Analytics\Queries\SummaryQuery;
14
15 /**
16 * Calls SummaryQuery for both periods and builds the summary KPI array.
17 *
18 * All metrics are scoped to completed orders only (wc-completed).
19 * No SQL in this class — pure composition and growth calculation.
20 */
21 class SummaryService {
22
23 /**
24 * Returns all six summary KPIs for the current and comparison periods.
25 *
26 * The active_campaigns metric is date-independent and carries no comparison data.
27 * All other KPIs include growth deltas against the comparison period.
28 *
29 * @param array $current Date range for current period { from: string Y-m-d, to: string Y-m-d }.
30 * @param array $compare Date range for comparison period { from: string Y-m-d, to: string Y-m-d }.
31 */
32 public static function get_summary( array $current, array $compare ): array {
33 $query = new SummaryQuery;
34
35 $active_campaigns = $query->get_active_campaign_count();
36
37 $current_all = $query->get_all_order_metrics( $current );
38 $current_disco = $query->get_disco_order_metrics( $current );
39 $current_cust = $query->get_disco_customer_count( $current );
40
41 $prev_all = $query->get_all_order_metrics( $compare );
42 $prev_disco = $query->get_disco_order_metrics( $compare );
43 $prev_cust = $query->get_disco_customer_count( $compare );
44
45 return array(
46 'active_campaigns' => array(
47 'current' => $active_campaigns,
48 ),
49 'net_sales' => self::build_kpi( $current_all['net_sales'], $prev_all['net_sales'] ),
50 'discount_sales' => self::build_kpi( $current_disco['revenue'], $prev_disco['revenue'] ),
51 'total_orders' => self::build_kpi( $current_all['total_orders'], $prev_all['total_orders'] ),
52 'disco_orders' => self::build_kpi( $current_disco['orders_count'], $prev_disco['orders_count'] ),
53 'customers' => self::build_kpi( $current_cust, $prev_cust ),
54 );
55 }
56
57 /**
58 * Builds a single KPI block with growth comparison.
59 *
60 * @param int|float $current Value in the current period.
61 * @param int|float $previous Value in the comparison period.
62 * @return array { current, previous, change, change_percent, trend }
63 */
64 private static function build_kpi( $current, $previous ): array {
65 $change = $current - $previous;
66
67 return array(
68 'current' => $current,
69 'previous' => $previous,
70 'change' => $change,
71 'change_percent' => 0 == $previous ? null : round( ( $change / $previous ) * 100, 2 ), //phpcs:ignore
72 'trend' => $change > 0 ? 'up' : ( $change < 0 ? 'down' : 'flat' ),
73 );
74 }
75
76 }
77