$company_ledger * @property-read \Illuminate\Database\Eloquent\Collection $credits * @property-read \Illuminate\Database\Eloquent\Collection $documents * @property-read \Illuminate\Database\Eloquent\Collection $invoices * @property \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $paymentables * @mixin \Eloquent */ class Payment extends BaseModel { use MakesHash; use Filterable; use MakesDates; use SoftDeletes; use Refundable; use Inviteable; public const STATUS_PENDING = 1; public const STATUS_CANCELLED = 2; public const STATUS_FAILED = 3; public const STATUS_COMPLETED = 4; public const STATUS_PARTIALLY_REFUNDED = 5; public const STATUS_REFUNDED = 6; public const TYPE_CREDIT_CARD = 1; public const TYPE_BANK_TRANSFER = 2; public const TYPE_PAYPAL = 3; public const TYPE_CRYPTO = 4; public const TYPE_DWOLLA = 5; public const TYPE_CUSTOM1 = 6; public const TYPE_ALIPAY = 7; public const TYPE_SOFORT = 8; public const TYPE_SEPA = 9; public const TYPE_GOCARDLESS = 10; public const TYPE_APPLE_PAY = 11; public const TYPE_CUSTOM2 = 12; public const TYPE_CUSTOM3 = 13; public const TYPE_TOKEN = 'token'; protected $fillable = [ 'assigned_user_id', 'client_id', 'type_id', 'amount', 'date', 'transaction_reference', 'number', 'exchange_currency_id', 'exchange_rate', 'private_notes', 'custom_value1', 'custom_value2', 'custom_value3', 'custom_value4', 'category_id', 'idempotency_key', ]; protected $casts = [ 'exchange_rate' => 'float', 'settings' => 'object', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', 'is_deleted' => 'bool', 'meta' => 'object', 'refund_meta' => 'array', ]; protected $with = [ 'paymentables', ]; protected $touches = []; public function getEntityType() { return self::class; } public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Client::class)->withTrashed(); } public function company_gateway(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CompanyGateway::class)->withTrashed(); } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Company::class); } public function contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(ClientContact::class); } public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class)->withTrashed(); } public function assigned_user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed(); } public function documents(): \Illuminate\Database\Eloquent\Relations\MorphMany { return $this->morphMany(Document::class, 'documentable'); } /** * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function invoices(): \Illuminate\Database\Eloquent\Relations\MorphToMany { return $this->morphedByMany(Invoice::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps(); } /** * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function credits(): \Illuminate\Database\Eloquent\Relations\MorphToMany { return $this->morphedByMany(Credit::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps(); } /** * @return \Illuminate\Database\Eloquent\Relations\MorphMany */ public function company_ledger(): \Illuminate\Database\Eloquent\Relations\MorphMany { return $this->morphMany(CompanyLedger::class, 'company_ledgerable'); } public function type(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(PaymentType::class); } public function currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Currency::class); } public function transaction(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(BankTransaction::class)->withTrashed(); } public function exchange_currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Currency::class, 'exchange_currency_id', 'id'); } public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Vendor::class); } public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Project::class); } public function translatedType(): string { if (! $this->type_id) { return ''; } $pt = new PaymentType(); return $pt->name($this->type_id); } public function gateway_type(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(GatewayType::class); } public function paymentables(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Paymentable::class); } public function formattedAmount() { return Number::formatMoney($this->amount, $this->client); } public function formatAmount(float $amount): string { return Number::formatMoney($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 function badgeForStatus(): string { switch ($this->status_id) { case self::STATUS_PENDING: return '
'.ctrans('texts.payment_status_1').'
'; case self::STATUS_CANCELLED: return '
'.ctrans('texts.payment_status_2').'
'; case self::STATUS_FAILED: return '
'.ctrans('texts.payment_status_3').'
'; case self::STATUS_COMPLETED: if($this->amount > $this->applied) { return '
' . ctrans('texts.partially_unapplied') . '
'; } return '
'.ctrans('texts.payment_status_4').'
'; case self::STATUS_PARTIALLY_REFUNDED: return '
'.ctrans('texts.payment_status_5').'
'; case self::STATUS_REFUNDED: return '
'.ctrans('texts.payment_status_6').'
'; default: return ''; } } public static function stringStatus(int $status): string { switch ($status) { case self::STATUS_PENDING: return ctrans('texts.payment_status_1'); case self::STATUS_CANCELLED: return ctrans('texts.payment_status_2'); case self::STATUS_FAILED: return ctrans('texts.payment_status_3'); case self::STATUS_COMPLETED: return ctrans('texts.payment_status_4'); case self::STATUS_PARTIALLY_REFUNDED: return ctrans('texts.payment_status_5'); case self::STATUS_REFUNDED: return ctrans('texts.payment_status_6'); default: return ''; } } public function ledger(): LedgerService { return new LedgerService($this); } public function service(): PaymentService { return new PaymentService($this); } /** * $data = [ 'id' => $payment->id, 'amount' => 10, 'invoices' => [ [ 'invoice_id' => $invoice->id, 'amount' => 10, ], ], 'date' => '2020/12/12', 'gateway_refund' => false, 'email_receipt' => false, ]; * * @param array $data * @return self */ public function refund(array $data): self { return $this->service()->refundPayment($data); } /** * @return float */ 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, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } return true; } public function isVoided() { return $this->status_id == self::STATUS_CANCELLED; } public function isPartiallyRefunded() { return $this->status_id == self::STATUS_PARTIALLY_REFUNDED; } public function isRefunded() { return $this->status_id == self::STATUS_REFUNDED; } public function setStatus($status) { $this->status_id = $status; $this->save(); } public function markVoided() { if ($this->isVoided() || $this->isPartiallyRefunded() || $this->isRefunded()) { return false; } $this->refunded = $this->amount; $this->status_id = self::STATUS_CANCELLED; $this->save(); event(new PaymentWasVoided($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } public function getLink(): string { if (Ninja::isHosted()) { $domain = $this->company->domain(); } else { $domain = strlen($this->company->portal_domain) > 5 ? $this->company->portal_domain : config('ninja.app_url'); } return $domain.'/client/payment/'.$this->client->contacts()->first()->contact_key.'/'.$this->hashed_id.'?next=/client/payments/'.$this->hashed_id; } public function transaction_event() { $payment = $this->fresh(); return [ 'payment_id' => $payment->id, 'payment_amount' => $payment->amount ?: 0, 'payment_applied' => $payment->applied ?: 0, 'payment_refunded' => $payment->refunded ?: 0, 'payment_status' => $payment->status_id ?: 1, 'paymentables' => $payment->paymentables->toArray(), 'payment_request' => [], ]; } public function translate_entity(): string { return ctrans('texts.payment'); } public function portalUrl($use_react_url): string { return $use_react_url ? config('ninja.react_url')."/#/payments/{$this->hashed_id}/edit" : config('ninja.app_url'); } public function setRefundMeta(array $data) { $tmp_meta = $this->refund_meta ?? []; $tmp_meta[] = $data; $this->refund_meta = $tmp_meta; } }