belongsTo(Affiliate::class); } public function order(): BelongsTo { return $this->belongsTo(Order::class); } /** * Get the earnings made from this order * * @return string */ public function earnings(): Attribute { return Attribute::make( get: function (): array { $earnings = []; /** @var Collection */ $invoices = $this->order->invoices; $extension = ExtensionHelper::getExtension('other', 'Affiliates'); $reward_percentage = $this->affiliate->reward ?: $extension->config('default_reward'); $invoices->each(function ($invoice) use (&$earnings, $reward_percentage) { if ($invoice->status !== 'paid') { return; } if (!isset($earnings[$invoice->currency_code])) { $earnings[$invoice->currency_code] = 0; } $earnings[$invoice->currency_code] += $invoice->total * $reward_percentage / 100; }); foreach ($earnings as $currency => $total) { // Round to 2 decimal places $earnings[$currency] = round($total, 2); } return $earnings; }, ); } }