| 1 |
<?php //phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* IntentPerformanceController — assembles the /analytics/intents-performance response. |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @subpackage Disco\App\Analytics\Controllers |
| 8 |
* @since 1.3.23 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Disco\App\Analytics\Controllers; |
| 12 |
|
| 13 |
use Disco\App\Analytics\Queries\IntentQuery; |
| 14 |
|
| 15 |
/** |
| 16 |
* Returns intent-wise sales for the current period only (no comparison). |
| 17 |
* |
| 18 |
* Reuses IntentQuery; keeps data flat: intent, sales, orders, percentage. |
| 19 |
*/ |
| 20 |
class IntentPerformanceController extends BaseController { |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns intent performance data for the given request args. |
| 24 |
* |
| 25 |
* @param array $args Request args with date_from and date_to as Y-m-d strings (optional). |
| 26 |
*/ |
| 27 |
public static function get_intent_performance( array $args ): array { |
| 28 |
$current = self::resolve_current_period( $args ); |
| 29 |
$compare = self::resolve_compare_period( $current ); |
| 30 |
|
| 31 |
$rows = ( new IntentQuery )->get_intent_sales( $current ); |
| 32 |
$total = array_sum( array_column( $rows, 'revenue' ) ); |
| 33 |
|
| 34 |
$data = array_map( |
| 35 |
function ( $row ) use ( $total ) { |
| 36 |
return array( |
| 37 |
'intent' => $row['intent'], |
| 38 |
'revenue' => $row['revenue'], |
| 39 |
'orders' => $row['orders'], |
| 40 |
'percentage' => $total > 0 ? round( $row['revenue'] / $total * 100, 2 ) : 0, |
| 41 |
); |
| 42 |
}, |
| 43 |
$rows |
| 44 |
); |
| 45 |
|
| 46 |
return array( |
| 47 |
'current_period' => array( 'from' => $current['from'], 'to' => $current['to'] ), |
| 48 |
'compare_period' => array( 'from' => $compare['from'], 'to' => $compare['to'] ), |
| 49 |
'total_revenue' => round( $total, 2 ), |
| 50 |
'data' => $data, |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|