'object', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', 'is_deleted' => 'bool', ]; protected $with = [ 'paymentables', ]; public function client() { return $this->belongsTo(Client::class)->withTrashed(); } public function company() { return $this->belongsTo(Company::class); } public function user() { return $this->belongsTo(User::class)->withTrashed(); } public function assigned_user() { return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed(); } public function documents() { return $this->morphMany(Document::class, 'documentable'); } public function invoices() { return $this->morphedByMany(Invoice::class, 'paymentable')->withPivot('amount','refunded')->withTimestamps(); } public function credits() { return $this->morphedByMany(Credit::class, 'paymentable')->withPivot('amount','refunded')->withTimestamps(); } public function company_ledger() { return $this->morphMany(CompanyLedger::class, 'company_ledgerable'); } public function type() { return $this->belongsTo(PaymentType::class); } public function paymentables() { return $this->hasMany(Paymentable::class); } public function formattedAmount() { return Number::formatMoney($this->amount, $this->client); } public function clientPaymentDate() { if (!$this->date) { return ''; } $date_format = DateFormat::find($this->client->getSetting('date_format_id')); return $this->createClientDate($this->date, $this->client->timezone()->name)->format($date_format->format); } public static function badgeForStatus(int $status) { switch ($status) { case self::STATUS_PENDING: return '
'.ctrans('texts.payment_status_1').'
'; break; case self::STATUS_VOIDED: return '
'.ctrans('texts.payment_status_2').'
'; break; case self::STATUS_FAILED: return '
'.ctrans('texts.payment_status_3').'
'; break; case self::STATUS_COMPLETED: return '
'.ctrans('texts.payment_status_4').'
'; break; case self::STATUS_PARTIALLY_REFUNDED: return '
'.ctrans('texts.payment_status_5').'
'; break; case self::STATUS_REFUNDED: return '
'.ctrans('texts.payment_status_6').'
'; break; default: # code... break; } } public function ledger() { return new LedgerService($this); } public function service() { return new PaymentService($this); } public function resolveRouteBinding($value) { return $this ->withTrashed() ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); } public function refund(array $data) :Payment { return $this->processRefund($data); } /** * @return mixed */ public function getCompletedAmount() :float { return $this->amount - $this->refunded; } public function recordRefund($amount = null) { //do i need $this->isRefunded() here? if ($this->isVoided()) { return false; } //if no refund specified if (! $amount) { $amount = $this->amount; } $new_refund = min($this->amount, $this->refunded + $amount); $refund_change = $new_refund - $this->refunded; if ($refund_change) { $this->refunded = $new_refund; $this->status_id = $this->refunded == $this->amount ? self::STATUS_REFUNDED : self::STATUS_PARTIALLY_REFUNDED; $this->save(); event(new PaymentWasRefunded($this, $refund_change)); } return true; } public function isVoided() { return $this->status_id == self::STATUS_VOIDED; } public function isPartiallyRefunded() { return $this->status_id == self::STATUS_PARTIALLY_REFUNDED; } public function isRefunded() { return $this->status_id == self::STATUS_REFUNDED; } public function markVoided() { if ($this->isVoided() || $this->isPartiallyRefunded() || $this->isRefunded()) { return false; } $this->refunded = $this->amount; $this->status_id = self::STATUS_VOIDED; $this->save(); event(new PaymentWasVoided($this)); } }