How do I add a lens to an existing report?
You can use the addLens() method to add a lens to an existing report:
\Aero\Admin\Reports\Implementations\SalesReport::extend(function (\Aero\Admin\Reports\Implementations\SalesReport $report, \Closure $next) {
$report->addLens(\Aerocargo\Acme\Lenses\TotalRevenueLens::class);
return $next($report);
});
class TotalRevenueLens extends \Aero\Admin\Reports\ReportLens
{
protected function value(): \Aero\Cart\CartPrice
{
$totalRevenue = \DB::query()
->fromSub($this->query, 'results')
->selectRaw('SUM(results.revenue + results.tax) as total')
->value('total');
$currency = request()->store()->currency;
return new \Aero\Cart\CartPrice($currency, $totalRevenue * 100);
}
}
Where the lens added extends \Aero\Admin\Reports\ReportLens:
class TotalRevenueLens extends \Aero\Admin\Reports\ReportLens
{
protected function value(): \Aero\Cart\CartPrice
{
$totalRevenue = \DB::query()
->fromSub($this->query, 'results')
->selectRaw('SUM(results.revenue + results.tax) as total')
->value('total');
$currency = request()->store()->currency;
return new \Aero\Cart\CartPrice($currency, $totalRevenue * 100);
}
}
$this->query is a copy of the report query, the value method must return a string (or stringable).