2018-10-15 14:40:34 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. 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
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-01-04 11:33:37 +01:00
|
|
|
use App\Events\Payment\PaymentWasRefunded;
|
2020-02-19 21:44:12 +01:00
|
|
|
use App\Events\Payment\PaymentWasVoided;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Services\Ledger\LedgerService;
|
2020-02-22 03:25:49 +01:00
|
|
|
use App\Services\Payment\PaymentService;
|
2023-07-31 10:22:34 +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;
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Utils\Traits\Payment\Refundable;
|
2023-07-31 10:22:34 +02:00
|
|
|
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo;
|
2019-11-18 11:46:01 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2023-03-08 08:33:42 +01:00
|
|
|
/**
|
|
|
|
* App\Models\Payment
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $company_id
|
|
|
|
* @property int $client_id
|
|
|
|
* @property int|null $project_id
|
|
|
|
* @property int|null $vendor_id
|
|
|
|
* @property int|null $user_id
|
|
|
|
* @property int|null $assigned_user_id
|
|
|
|
* @property int|null $client_contact_id
|
|
|
|
* @property int|null $invitation_id
|
|
|
|
* @property int|null $company_gateway_id
|
|
|
|
* @property int|null $gateway_type_id
|
|
|
|
* @property int|null $type_id
|
|
|
|
* @property int $status_id
|
2023-04-26 14:38:08 +02:00
|
|
|
* @property float $amount
|
|
|
|
* @property float $refunded
|
|
|
|
* @property float $applied
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $date
|
|
|
|
* @property string|null $transaction_reference
|
|
|
|
* @property string|null $payer_id
|
|
|
|
* @property string|null $number
|
|
|
|
* @property string|null $private_notes
|
|
|
|
* @property int|null $created_at
|
|
|
|
* @property int|null $updated_at
|
|
|
|
* @property int|null $deleted_at
|
|
|
|
* @property bool $is_deleted
|
2023-04-26 14:38:08 +02:00
|
|
|
* @property bool $is_manual
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property float $exchange_rate
|
|
|
|
* @property int $currency_id
|
|
|
|
* @property int|null $exchange_currency_id
|
2023-08-06 09:56:26 +02:00
|
|
|
* @property \App\Models\Paymentable $paymentable
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property object|null $meta
|
|
|
|
* @property string|null $custom_value1
|
|
|
|
* @property string|null $custom_value2
|
|
|
|
* @property string|null $custom_value3
|
|
|
|
* @property string|null $custom_value4
|
|
|
|
* @property int|null $transaction_id
|
|
|
|
* @property string|null $idempotency_key
|
|
|
|
* @property-read \App\Models\User|null $assigned_user
|
|
|
|
* @property-read \App\Models\Client $client
|
|
|
|
* @property-read \App\Models\Company $company
|
|
|
|
* @property-read \App\Models\CompanyGateway|null $company_gateway
|
|
|
|
* @property-read \App\Models\ClientContact|null $contact
|
|
|
|
* @property-read \App\Models\Currency|null $currency
|
|
|
|
* @property-read \App\Models\Currency|null $exchange_currency
|
|
|
|
* @property-read \App\Models\GatewayType|null $gateway_type
|
|
|
|
* @property-read mixed $hashed_id
|
|
|
|
* @property-read \App\Models\Project|null $project
|
|
|
|
* @property-read \App\Models\PaymentType|null $type
|
|
|
|
* @property-read \App\Models\User|null $user
|
|
|
|
* @property-read \App\Models\Vendor|null $vendor
|
|
|
|
* @method static \Database\Factories\PaymentFactory factory($count = null, $state = [])
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment filter(\App\Filters\QueryFilters $filters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Payment withoutTrashed()
|
2023-03-16 05:20:38 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
2023-03-21 12:37:06 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Invoice> $invoices
|
2023-08-07 12:47:05 +02:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment>|\Illuminate\Support\Collection $paymentables
|
2023-03-08 08:33:42 +01:00
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
class Payment extends BaseModel
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2019-05-03 09:57:55 +02:00
|
|
|
use Filterable;
|
2019-10-04 00:06:38 +02:00
|
|
|
use MakesDates;
|
2019-11-18 11:46:01 +01:00
|
|
|
use SoftDeletes;
|
2020-01-29 03:03:47 +01:00
|
|
|
use Refundable;
|
2021-08-07 05:43:34 +02:00
|
|
|
use Inviteable;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
const STATUS_PENDING = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-07-06 13:22:36 +02:00
|
|
|
const STATUS_CANCELLED = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
const STATUS_FAILED = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
const STATUS_COMPLETED = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
const STATUS_PARTIALLY_REFUNDED = 5;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
const STATUS_REFUNDED = 6;
|
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_CREDIT_CARD = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_BANK_TRANSFER = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_PAYPAL = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-11-27 10:47:59 +01:00
|
|
|
const TYPE_CRYPTO = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_DWOLLA = 5;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_CUSTOM1 = 6;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_ALIPAY = 7;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_SOFORT = 8;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_SEPA = 9;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_GOCARDLESS = 10;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_APPLE_PAY = 11;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_CUSTOM2 = 12;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_CUSTOM3 = 13;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-08-19 08:50:33 +02:00
|
|
|
const TYPE_TOKEN = 'token';
|
|
|
|
|
2019-06-26 06:04:10 +02:00
|
|
|
protected $fillable = [
|
2020-06-26 00:29:24 +02:00
|
|
|
'assigned_user_id',
|
2019-12-30 22:59:12 +01:00
|
|
|
'client_id',
|
2019-12-16 12:34:38 +01:00
|
|
|
'type_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'amount',
|
2019-12-16 12:34:38 +01:00
|
|
|
'date',
|
2019-12-17 11:50:45 +01:00
|
|
|
'transaction_reference',
|
2020-02-03 11:33:07 +01:00
|
|
|
'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',
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2019-09-02 07:08:26 +02:00
|
|
|
protected $casts = [
|
2020-03-06 12:19:09 +01:00
|
|
|
'exchange_rate' => 'float',
|
2019-09-26 00:27:26 +02:00
|
|
|
'settings' => 'object',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-11-19 22:06:48 +01:00
|
|
|
'is_deleted' => 'bool',
|
2020-09-01 01:28:37 +02:00
|
|
|
'meta' => 'object',
|
2019-09-02 07:08:26 +02:00
|
|
|
];
|
|
|
|
|
2020-02-06 10:35:51 +01:00
|
|
|
protected $with = [
|
|
|
|
'paymentables',
|
|
|
|
];
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-05-03 09:57:55 +02:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2019-05-03 09:57:55 +02:00
|
|
|
}
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function company_gateway(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2020-06-27 02:05:31 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(CompanyGateway::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-05-03 09:57:55 +02:00
|
|
|
return $this->belongsTo(Company::class);
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2020-05-19 00:22:18 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(ClientContact::class);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
2019-04-28 07:31:32 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function assigned_user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-11-05 11:16:38 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
2019-11-05 11:16:38 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function documents(): \Illuminate\Database\Eloquent\Relations\MorphMany
|
2019-04-28 07:31:32 +02:00
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
/**
|
|
|
|
* @return MorphedByMany
|
|
|
|
*/
|
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();
|
2020-01-07 01:13:47 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
/**
|
|
|
|
* @return MorphedByMany
|
|
|
|
*/
|
2020-01-07 01:13:47 +01:00
|
|
|
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
|
|
|
|
2023-08-02 12:10:42 +02:00
|
|
|
public function company_ledger(): \Illuminate\Database\Eloquent\Relations\MorphMany
|
2019-05-15 07:03:18 +02:00
|
|
|
{
|
|
|
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
|
|
|
}
|
2019-08-16 07:20:28 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function type(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
2019-12-18 11:49:28 +01:00
|
|
|
return $this->belongsTo(PaymentType::class);
|
2019-12-16 12:34:38 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Currency::class);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function transaction(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2023-07-31 10:22:34 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(BankTransaction::class);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function exchange_currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Currency::class, 'exchange_currency_id', 'id');
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Vendor::class);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Project::class);
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:39:48 +01:00
|
|
|
public function translatedType()
|
|
|
|
{
|
2023-03-10 07:49:43 +01:00
|
|
|
if (! $this->type_id) {
|
2022-03-05 22:39:48 +01:00
|
|
|
return '';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-03-05 22:39:48 +01:00
|
|
|
|
2023-03-10 07:49:43 +01:00
|
|
|
$pt = new PaymentType();
|
|
|
|
|
|
|
|
return $pt->name($this->type_id);
|
2022-03-05 22:39:48 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function gateway_type(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-01-24 11:53:46 +01:00
|
|
|
{
|
|
|
|
return $this->belongsTo(GatewayType::class);
|
|
|
|
}
|
|
|
|
|
2023-08-01 15:01:48 +02:00
|
|
|
public function paymentables(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2019-12-16 12:34:38 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:45:34 +01:00
|
|
|
public function formatAmount(float $amount): string
|
|
|
|
{
|
|
|
|
return Number::formatMoney($amount, $this->client);
|
|
|
|
}
|
|
|
|
|
2019-10-04 00:06:38 +02:00
|
|
|
public function clientPaymentDate()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->date) {
|
2019-10-08 01:05:41 +02:00
|
|
|
return '';
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
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'));
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
return $this->createClientDate($this->date, $this->client->timezone()->name)->format($date_format->format);
|
2019-10-04 00:06:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:38:08 +02:00
|
|
|
public static function badgeForStatus(int $status): string
|
2019-08-16 07:20:28 +02:00
|
|
|
{
|
|
|
|
switch ($status) {
|
2019-08-19 08:50:33 +02:00
|
|
|
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>';
|
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>';
|
2019-08-19 08:50:33 +02:00
|
|
|
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>';
|
2019-08-19 08:50:33 +02:00
|
|
|
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>';
|
2019-08-19 08:50:33 +02:00
|
|
|
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-19 08:50:33 +02:00
|
|
|
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>';
|
2019-08-16 07:20:28 +02:00
|
|
|
default:
|
2023-04-26 14:38:08 +02:00
|
|
|
return '';
|
2019-08-16 07:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 05:41:02 +01:00
|
|
|
|
2023-04-26 14:38:08 +02:00
|
|
|
public static function stringStatus(int $status): string
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
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 '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:38:08 +02:00
|
|
|
public function ledger(): LedgerService
|
2020-02-20 22:05:01 +01:00
|
|
|
{
|
|
|
|
return new LedgerService($this);
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:38:08 +02:00
|
|
|
public function service(): PaymentService
|
2020-02-22 03:25:49 +01:00
|
|
|
{
|
|
|
|
return new PaymentService($this);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function refund(array $data) :self
|
2020-01-27 08:49:04 +01:00
|
|
|
{
|
2020-06-01 05:16:06 +02:00
|
|
|
return $this->service()->refundPayment($data);
|
2020-01-27 08:49:04 +01:00
|
|
|
}
|
2020-02-19 21:44:12 +01:00
|
|
|
|
|
|
|
/**
|
2023-04-26 14:38:08 +02:00
|
|
|
* @return float
|
2020-02-19 21:44:12 +01:00
|
|
|
*/
|
|
|
|
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)));
|
2020-02-19 21:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isVoided()
|
|
|
|
{
|
2020-07-06 13:22:36 +02:00
|
|
|
return $this->status_id == self::STATUS_CANCELLED;
|
2020-02-19 21:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:44:12 +01:00
|
|
|
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;
|
2020-02-19 21:44:12 +01:00
|
|
|
$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-02-19 21:44:12 +01:00
|
|
|
}
|
2020-11-09 03:39:42 +01:00
|
|
|
|
2021-08-30 14:04:51 +02:00
|
|
|
public function getLink() :string
|
2020-11-09 03:39:42 +01:00
|
|
|
{
|
2023-07-04 01:20:58 +02:00
|
|
|
// if (Ninja::isHosted()) {
|
|
|
|
// $domain = isset($this->company->portal_domain) ? $this->company->portal_domain : $this->company->domain();
|
|
|
|
// } else {
|
|
|
|
// $domain = config('ninja.app_url');
|
|
|
|
// }
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2023-07-04 01:20:58 +02:00
|
|
|
$domain = $this->company->domain();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-07-04 01:20:58 +02:00
|
|
|
$domain = strlen($this->company->portal_domain) > 5 ? $this->company->portal_domain : config('ninja.app_url');
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-08-30 14:04:51 +02:00
|
|
|
|
2022-06-21 11:57:17 +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 [
|
2022-06-21 11:57:17 +02:00
|
|
|
'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(),
|
2023-04-26 14:38:08 +02:00
|
|
|
'payment_request' => [],
|
2022-03-09 22:52:33 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function translate_entity(): string
|
2022-04-06 02:38:01 +02:00
|
|
|
{
|
|
|
|
return ctrans('texts.payment');
|
|
|
|
}
|
2023-06-07 08:25:41 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public function portalUrl($use_react_url): string
|
2023-06-07 08:25:41 +02:00
|
|
|
{
|
2023-06-11 08:09:16 +02:00
|
|
|
return $use_react_url ? config('ninja.react_url')."/#/payments/{$this->hashed_id}/edit" : config('ninja.app_url');
|
2023-06-07 08:25:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|