1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Models/Payment.php

406 lines
10 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Models;
2022-01-04 11:33:37 +01:00
use App\Events\Payment\PaymentWasRefunded;
use App\Events\Payment\PaymentWasVoided;
2022-01-24 11:53:46 +01:00
use App\Models\GatewayType;
use App\Services\Ledger\LedgerService;
use App\Services\Payment\PaymentService;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2019-10-03 23:51:54 +02:00
use App\Utils\Number;
2021-08-07 05:43:34 +02:00
use App\Utils\Traits\Inviteable;
2019-10-04 00:06:38 +02:00
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Payment\Refundable;
use Illuminate\Database\Eloquent\SoftDeletes;
2022-03-05 22:39:48 +01:00
use Illuminate\Support\Facades\Cache;
class Payment extends BaseModel
{
use MakesHash;
2019-05-03 09:57:55 +02:00
use Filterable;
2019-10-04 00:06:38 +02:00
use MakesDates;
use SoftDeletes;
use Refundable;
2021-08-07 05:43:34 +02:00
use Inviteable;
2019-05-13 08:18:46 +02:00
const STATUS_PENDING = 1;
2020-07-06 13:22:36 +02:00
const STATUS_CANCELLED = 2;
2019-05-13 08:18:46 +02:00
const STATUS_FAILED = 3;
2019-05-13 08:18:46 +02:00
const STATUS_COMPLETED = 4;
2019-05-13 08:18:46 +02:00
const STATUS_PARTIALLY_REFUNDED = 5;
2019-05-13 08:18:46 +02:00
const STATUS_REFUNDED = 6;
const TYPE_CREDIT_CARD = 1;
const TYPE_BANK_TRANSFER = 2;
const TYPE_PAYPAL = 3;
const TYPE_CRYPTO = 4;
const TYPE_DWOLLA = 5;
const TYPE_CUSTOM1 = 6;
const TYPE_ALIPAY = 7;
const TYPE_SOFORT = 8;
const TYPE_SEPA = 9;
const TYPE_GOCARDLESS = 10;
const TYPE_APPLE_PAY = 11;
const TYPE_CUSTOM2 = 12;
const TYPE_CUSTOM3 = 13;
const TYPE_TOKEN = 'token';
protected $fillable = [
2020-06-26 00:29:24 +02:00
'assigned_user_id',
'client_id',
'type_id',
'amount',
'date',
'transaction_reference',
'number',
2021-06-20 22:51:42 +02:00
'exchange_currency_id',
'exchange_rate',
2021-01-13 22:16:07 +01:00
// 'is_manual',
2020-07-22 09:53:14 +02:00
'private_notes',
2020-11-08 22:21:52 +01:00
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
];
protected $casts = [
'exchange_rate' => 'float',
'settings' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'is_deleted' => 'bool',
2020-09-01 01:28:37 +02:00
'meta' => 'object',
];
protected $with = [
'paymentables',
];
2020-07-23 05:55:11 +02:00
protected $touches = [];
public function getEntityType()
{
return self::class;
}
2019-05-03 09:57:55 +02:00
public function client()
{
return $this->belongsTo(Client::class)->withTrashed();
2019-05-03 09:57:55 +02:00
}
2020-06-27 02:05:31 +02:00
public function company_gateway()
{
return $this->belongsTo(CompanyGateway::class)->withTrashed();
}
2019-05-03 09:57:55 +02:00
public function company()
{
2019-05-03 09:57:55 +02:00
return $this->belongsTo(Company::class);
}
public function contact()
{
return $this->belongsTo(ClientContact::class);
}
2019-05-03 09:57:55 +02:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
2019-04-28 07:31:32 +02:00
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
2019-04-28 07:31:32 +02:00
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
2019-05-13 08:18:46 +02:00
public function invoices()
{
2020-11-25 01:23:39 +01:00
return $this->morphedByMany(Invoice::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
}
public function credits()
{
2020-11-25 01:23:39 +01:00
return $this->morphedByMany(Credit::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
2019-05-13 08:18:46 +02:00
}
2019-05-15 07:03:18 +02:00
public function company_ledger()
{
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
}
2019-08-16 07:20:28 +02:00
public function type()
2019-08-16 07:20:28 +02:00
{
return $this->belongsTo(PaymentType::class);
}
2022-04-27 10:05:54 +02:00
public function currency()
{
return $this->belongsTo(Currency::class);
}
public function exchange_currency()
{
return $this->belongsTo(Currency::class, 'exchange_currency_id', 'id');
}
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
2022-03-05 22:39:48 +01:00
public function translatedType()
{
if (! $this->type) {
2022-03-05 22:39:48 +01:00
return '';
}
2022-03-05 22:39:48 +01:00
return ctrans('texts.payment_type_'.$this->type->name);
}
2022-01-24 11:53:46 +01:00
public function gateway_type()
{
return $this->belongsTo(GatewayType::class);
}
public function paymentables()
{
return $this->hasMany(Paymentable::class);
2019-08-16 07:20:28 +02:00
}
2019-10-03 23:51:54 +02:00
public function formattedAmount()
{
return Number::formatMoney($this->amount, $this->client);
}
2019-10-04 00:06:38 +02:00
public function clientPaymentDate()
{
if (! $this->date) {
2019-10-08 01:05:41 +02:00
return '';
}
2019-10-08 01:05:41 +02:00
2019-10-04 00:06:38 +02:00
$date_format = DateFormat::find($this->client->getSetting('date_format_id'));
return $this->createClientDate($this->date, $this->client->timezone()->name)->format($date_format->format);
2019-10-04 00:06:38 +02:00
}
2019-08-16 07:20:28 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
case self::STATUS_PENDING:
2019-08-20 00:29:19 +02:00
return '<h6><span class="badge badge-secondary">'.ctrans('texts.payment_status_1').'</span></h6>';
break;
2020-07-06 13:22:36 +02:00
case self::STATUS_CANCELLED:
2022-06-12 09:15:51 +02:00
return '<h6><span class="badge badge-warning text-white">'.ctrans('texts.payment_status_2').'</span></h6>';
break;
case self::STATUS_FAILED:
2019-08-20 00:29:19 +02:00
return '<h6><span class="badge badge-danger">'.ctrans('texts.payment_status_3').'</span></h6>';
break;
case self::STATUS_COMPLETED:
2019-08-20 00:29:19 +02:00
return '<h6><span class="badge badge-info">'.ctrans('texts.payment_status_4').'</span></h6>';
break;
case self::STATUS_PARTIALLY_REFUNDED:
2019-08-20 00:29:19 +02:00
return '<h6><span class="badge badge-success">'.ctrans('texts.payment_status_5').'</span></h6>';
2019-08-16 07:20:28 +02:00
break;
case self::STATUS_REFUNDED:
2019-08-20 00:29:19 +02:00
return '<h6><span class="badge badge-primary">'.ctrans('texts.payment_status_6').'</span></h6>';
break;
2019-08-16 07:20:28 +02:00
default:
// code...
2019-08-16 07:20:28 +02:00
break;
}
}
2022-04-27 10:05:54 +02:00
public static function stringStatus(int $status)
{
switch ($status) {
case self::STATUS_PENDING:
return ctrans('texts.payment_status_1');
break;
case self::STATUS_CANCELLED:
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:
return '';
break;
}
}
public function ledger()
{
return new LedgerService($this);
}
public function service()
{
return new PaymentService($this);
}
2020-11-25 15:19:52 +01:00
public function resolveRouteBinding($value, $field = null)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
public function refund(array $data) :self
{
2020-06-01 05:16:06 +02:00
return $this->service()->refundPayment($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();
2021-05-06 23:12:07 +02:00
event(new PaymentWasRefunded($this, $refund_change, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
}
return true;
}
public function isVoided()
{
2020-07-06 13:22:36 +02:00
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;
}
2020-06-28 05:05:58 +02:00
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;
2020-07-06 13:22:36 +02:00
$this->status_id = self::STATUS_CANCELLED;
$this->save();
2021-05-06 23:12:07 +02:00
event(new PaymentWasVoided($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
}
2020-11-09 03:39:42 +01:00
2021-08-30 14:04:51 +02:00
// public function getLink()
// {
// return route('client.payments.show', $this->hashed_id);
// }
public function getLink() :string
2020-11-09 03:39:42 +01:00
{
if (Ninja::isHosted()) {
2021-08-30 14:04:51 +02:00
$domain = isset($this->company->portal_domain) ? $this->company->portal_domain : $this->company->domain();
} else {
2021-08-30 14:04:51 +02:00
$domain = config('ninja.app_url');
}
2021-08-30 14:04:51 +02:00
return $domain.'/client/payment/'.$this->client->contacts()->first()->contact_key.'/'.$this->hashed_id.'?next=/client/payments/'.$this->hashed_id;
2020-11-09 03:39:42 +01:00
}
2021-08-30 14:04:51 +02:00
2022-03-09 22:52:33 +01:00
public function transaction_event()
{
2022-03-10 02:17:05 +01:00
$payment = $this->fresh();
2022-03-10 01:32:04 +01:00
2022-03-09 22:52:33 +01:00
return [
'payment_id' => $payment->id,
'payment_amount' => $payment->amount ?: 0,
'payment_applied' => $payment->applied ?: 0,
'payment_refunded' => $payment->refunded ?: 0,
2022-03-10 02:17:05 +01:00
'payment_status' => $payment->status_id ?: 1,
'paymentables' => $payment->paymentables->toArray(),
2022-03-10 01:32:04 +01:00
'payment_request' => request() ? request()->all() : [],
2022-03-09 22:52:33 +01:00
];
}
2022-04-06 02:38:01 +02:00
public function translate_entity()
{
return ctrans('texts.payment');
}
}