1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Models/Client.php

944 lines
31 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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;
2024-09-22 06:49:43 +02:00
use Laravel\Scout\Searchable;
2024-10-03 06:21:47 +02:00
use App\DataMapper\ClientSync;
2024-09-22 06:49:43 +02:00
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesDates;
2023-10-26 04:57:44 +02:00
use App\DataMapper\FeesAndLimits;
use App\Models\Traits\Excludable;
2024-09-22 06:49:43 +02:00
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
2024-10-03 06:21:47 +02:00
use Illuminate\Support\Facades\App;
2023-10-26 04:57:44 +02:00
use App\Services\Client\ClientService;
use App\Utils\Traits\GeneratesCounter;
use Laracasts\Presenter\PresentableTrait;
2024-09-22 06:49:43 +02:00
use App\Models\Presenters\ClientPresenter;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Utils\Traits\ClientGroupSettingsSaver;
use App\Libraries\Currency\Conversion\CurrencyApi;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Contracts\Translation\HasLocalePreference;
2023-03-08 08:33:42 +01:00
/**
* App\Models\Client
*
* @property int $id
* @property int $company_id
* @property int $user_id
* @property int|null $assigned_user_id
* @property string|null $name
* @property string|null $website
* @property string|null $private_notes
* @property string|null $public_notes
* @property string|null $client_hash
* @property string|null $logo
* @property string|null $phone
2023-04-26 09:41:30 +02:00
* @property string|null $routing_id
2023-08-06 09:56:26 +02:00
* @property float $balance
* @property float $paid_to_date
* @property float $credit_balance
2023-03-08 08:33:42 +01:00
* @property int|null $last_login
* @property int|null $industry_id
* @property int|null $size_id
2024-08-30 07:44:01 +02:00
* @property object|array|null $e_invoice
2023-03-08 08:33:42 +01:00
* @property string|null $address1
* @property string|null $address2
* @property string|null $city
* @property string|null $state
* @property string|null $postal_code
2023-08-09 02:26:47 +02:00
* @property int|null $country_id
2023-03-08 08:33:42 +01:00
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
* @property string|null $custom_value4
* @property string|null $shipping_address1
* @property string|null $shipping_address2
* @property string|null $shipping_city
* @property string|null $shipping_state
* @property string|null $shipping_postal_code
* @property int|null $shipping_country_id
* @property object|null $settings
2023-07-24 10:57:02 +02:00
* @property object|null $group_settings
2024-09-22 11:27:34 +02:00
* @property object|null $sync
2023-03-08 08:33:42 +01:00
* @property bool $is_deleted
* @property int|null $group_settings_id
* @property string|null $vat_number
* @property string|null $number
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $deleted_at
* @property string|null $id_number
2023-08-06 09:35:19 +02:00
* @property-read mixed $hashed_id
2023-03-08 08:33:42 +01:00
* @property-read \App\Models\User|null $assigned_user
2023-08-06 09:35:19 +02:00
* @property-read \App\Models\User $user
2023-03-08 08:33:42 +01:00
* @property-read \App\Models\Company $company
2023-08-06 09:35:19 +02:00
* @property-read \App\Models\Country|null $country
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
2023-03-08 08:33:42 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientContact> $contacts
* @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\Expense> $expenses
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientGatewayToken> $gateway_tokens
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Invoice> $invoices
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $ledger
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientContact> $primary_contact
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Project> $projects
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Quote> $quotes
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringExpense> $recurring_expenses
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringInvoice> $recurring_invoices
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\SystemLog> $system_logs
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
2023-07-05 00:10:11 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringInvoice> $recurring_invoices
2023-03-08 08:33:42 +01:00
* @method static \Illuminate\Database\Eloquent\Builder|Client exclude($columns)
* @method static \Database\Factories\ClientFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|Client filter(\App\Filters\QueryFilters $filters)
2023-08-06 09:35:19 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|Client without()
2023-08-07 06:50:08 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|Client find()
2023-08-07 00:23:13 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|Client select()
2023-03-16 05:20:38 +01:00
* @property string $payment_balance
2023-03-21 12:37:06 +01:00
* @property mixed $tax_data
2024-10-04 01:56:31 +02:00
* @property bool $is_tax_exempt
2024-06-16 06:35:56 +02:00
* @property bool $has_valid_vat_number
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
*/
class Client extends BaseModel implements HasLocalePreference
{
use PresentableTrait;
use MakesHash;
2019-04-30 08:02:39 +02:00
use MakesDates;
use SoftDeletes;
2019-03-28 11:07:45 +01:00
use Filterable;
2019-06-03 07:31:20 +02:00
use GeneratesCounter;
2021-06-10 05:56:02 +02:00
use AppSetup;
2022-04-03 11:39:55 +02:00
use ClientGroupSettingsSaver;
2023-01-21 06:52:24 +01:00
use Excludable;
2024-09-22 06:49:43 +02:00
use Searchable;
2020-10-28 11:10:49 +01:00
protected $presenter = ClientPresenter::class;
2019-07-30 00:28:38 +02:00
protected $hidden = [
'id',
2019-07-30 00:28:38 +02:00
'private_notes',
'user_id',
'company_id',
'last_login',
];
protected $fillable = [
'assigned_user_id',
'name',
'website',
'private_notes',
'industry_id',
'size_id',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'shipping_address1',
'shipping_address2',
'shipping_city',
'shipping_state',
'shipping_postal_code',
'shipping_country_id',
'settings',
'vat_number',
'id_number',
2019-10-07 22:49:16 +02:00
'group_settings_id',
'public_notes',
2021-01-25 11:34:12 +01:00
'phone',
'number',
2023-04-17 09:24:16 +02:00
'routing_id',
2023-05-18 00:31:21 +02:00
'is_tax_exempt',
2023-05-31 06:36:35 +02:00
'has_valid_vat_number',
'classification',
];
2019-04-29 02:54:26 +02:00
protected $with = [
2021-10-10 11:37:57 +02:00
'gateway_tokens',
'documents',
'contacts.company',
2019-04-29 02:54:26 +02:00
];
protected $casts = [
'is_deleted' => 'boolean',
'country_id' => 'string',
'settings' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
2023-03-04 01:00:52 +01:00
'last_login' => 'timestamp',
2023-03-21 12:37:06 +01:00
'tax_data' => 'object',
2024-06-02 10:37:56 +02:00
'e_invoice' => 'object',
2024-09-22 11:27:34 +02:00
'sync' => ClientSync::class,
];
2020-07-23 05:55:11 +02:00
protected $touches = [];
/**
* Whitelisted fields for using from query parameters on subscriptions request.
*
* @var string[]
*/
public static $subscriptions_fillable = [
'assigned_user_id',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'shipping_address1',
'shipping_address2',
'shipping_city',
'shipping_state',
'shipping_postal_code',
'shipping_country_id',
'payment_terms',
'vat_number',
'id_number',
'public_notes',
'phone',
2023-04-17 09:24:16 +02:00
'routing_id',
];
2024-04-25 08:31:15 +02:00
public static array $bulk_update_columns = [
'public_notes',
'industry_id',
'size_id',
'country_id',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
];
2023-01-21 06:52:24 +01:00
2024-09-22 06:49:43 +02:00
public function toSearchableArray()
{
2024-10-03 06:21:47 +02:00
$locale = $this->locale();
App::setLocale($locale);
$name = ctrans('texts.client') . " | " . $this->present()->name();
if(strlen($this->vat_number ?? '') > 1)
$name .= " | ". $this->vat_number;
2024-09-22 06:49:43 +02:00
return [
2024-10-03 06:21:47 +02:00
'name' => $name,
2024-09-22 06:49:43 +02:00
'is_deleted' => $this->is_deleted,
'hashed_id' => $this->hashed_id,
'number' => $this->number,
'id_number' => $this->id_number,
'vat_number' => $this->vat_number,
'balance' => $this->balance,
'paid_to_date' => $this->paid_to_date,
'phone' => $this->phone,
'address1' => $this->address1,
'address2' => $this->address2,
'city' => $this->city,
'state' => $this->state,
'postal_code' => $this->postal_code,
'website' => $this->website,
'private_notes' => $this->private_notes,
'public_notes' => $this->public_notes,
'shipping_address1' => $this->shipping_address1,
'shipping_address2' => $this->shipping_address2,
'shipping_city' => $this->shipping_city,
'shipping_state' => $this->shipping_state,
'shipping_postal_code' => $this->shipping_postal_code,
'custom_value1' => $this->custom_value1,
'custom_value2' => $this->custom_value2,
'custom_value3' => $this->custom_value3,
'custom_value4' => $this->custom_value4,
'company_key' => $this->company->company_key,
];
}
2023-01-21 06:52:24 +01:00
2024-10-03 06:21:47 +02:00
public function getScoutKey()
{
return $this->hashed_id;
}
public function getEntityType()
{
return self::class;
}
2023-08-06 09:41:40 +02:00
public function ledger(): \Illuminate\Database\Eloquent\Relations\HasMany
{
2020-06-20 01:23:41 +02:00
return $this->hasMany(CompanyLedger::class)->orderBy('id', 'desc');
}
2023-08-11 09:58:43 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<CompanyLedger>
*/
public function company_ledger(): \Illuminate\Database\Eloquent\Relations\MorphMany
2020-07-01 07:51:19 +02:00
{
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
}
2023-08-06 09:41:40 +02:00
public function gateway_tokens(): \Illuminate\Database\Eloquent\Relations\HasMany
2019-09-08 12:39:13 +02:00
{
return $this->hasMany(ClientGatewayToken::class)->orderBy('is_default', 'DESC');
2019-09-08 12:39:13 +02:00
}
2023-08-06 09:41:40 +02:00
public function expenses(): \Illuminate\Database\Eloquent\Relations\HasMany
2021-08-01 07:46:40 +02:00
{
return $this->hasMany(Expense::class)->withTrashed();
}
2023-08-06 09:41:40 +02:00
public function projects(): \Illuminate\Database\Eloquent\Relations\HasMany
2021-08-01 07:46:40 +02:00
{
return $this->hasMany(Project::class)->withTrashed();
}
2019-09-13 00:33:48 +02:00
/**
* Retrieves the specific payment token per
* gateway - per payment method.
2019-09-13 00:33:48 +02:00
*
* Allows the storage of multiple tokens
* per client per gateway per payment_method
*
2019-09-25 04:07:33 +02:00
* @param int $company_gateway_id The company gateway ID
2019-09-13 00:33:48 +02:00
* @param int $payment_method_id The payment method ID
* @return ClientGatewayToken The client token record
*/
2019-09-25 04:07:33 +02:00
public function gateway_token($company_gateway_id, $payment_method_id)
2019-09-13 00:33:48 +02:00
{
2019-09-25 04:07:33 +02:00
return $this->gateway_tokens()
->whereCompanyGatewayId($company_gateway_id)
->whereGatewayTypeId($payment_method_id)
2019-09-13 00:33:48 +02:00
->first();
}
2023-08-06 09:41:40 +02:00
public function credits(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Credit::class)->withTrashed();
}
2023-08-06 09:41:40 +02:00
public function purgeable_activities(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Activity::class);
}
2023-08-06 09:41:40 +02:00
public function activities(): \Illuminate\Database\Eloquent\Relations\HasMany
{
2024-09-29 07:34:13 +02:00
return $this->hasMany(Activity::class)->where('company_id', $this->company_id)->take(50)->orderBy('id', 'desc');
}
2024-01-14 05:05:00 +01:00
public function contacts(): HasMany
{
return $this->hasMany(ClientContact::class)->orderBy('is_primary', 'desc');
}
2024-01-14 05:05:00 +01:00
public function primary_contact(): HasMany
{
return $this->hasMany(ClientContact::class)->where('is_primary', true);
}
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
2024-01-14 05:05:00 +01:00
public function user(): BelongsTo
{
2021-09-04 05:35:53 +02:00
return $this->belongsTo(User::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function assigned_user(): BelongsTo
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function country(): BelongsTo
2019-01-26 10:34:38 +01:00
{
return $this->belongsTo(Country::class);
}
2024-01-14 05:05:00 +01:00
public function invoices(): HasMany
{
2020-07-07 09:54:21 +02:00
return $this->hasMany(Invoice::class)->withTrashed();
2019-01-26 10:34:38 +01:00
}
2024-01-14 05:05:00 +01:00
public function quotes(): HasMany
2021-08-01 07:46:40 +02:00
{
return $this->hasMany(Quote::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function tasks(): HasMany
2021-08-01 07:46:40 +02:00
{
return $this->hasMany(Task::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function payments(): HasMany
{
return $this->hasMany(Payment::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function recurring_invoices(): HasMany
2021-07-02 08:36:14 +02:00
{
return $this->hasMany(RecurringInvoice::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function recurring_expenses(): HasMany
2022-01-30 00:46:39 +01:00
{
return $this->hasMany(RecurringExpense::class)->withTrashed();
}
2024-01-14 05:05:00 +01:00
public function shipping_country(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-01-26 10:34:38 +01:00
{
return $this->belongsTo(Country::class, 'shipping_country_id', 'id');
}
2024-01-14 05:05:00 +01:00
public function system_logs(): HasMany
2020-08-24 04:45:53 +02:00
{
2022-05-12 23:52:02 +02:00
return $this->hasMany(SystemLog::class)->take(50)->orderBy('id', 'desc');
2020-08-24 04:45:53 +02:00
}
2024-01-14 05:05:00 +01:00
public function timezone(): Timezone
2019-04-24 02:22:02 +02:00
{
2019-09-11 07:32:47 +02:00
return Timezone::find($this->getSetting('timezone_id'));
2019-04-24 02:22:02 +02:00
}
public function language()
{
/** @var \Illuminate\Support\Collection<\App\Models\Language> */
$languages = app('languages');
2021-06-10 05:56:02 +02:00
return $languages->first(function ($item) {
return $item->id == $this->getSetting('language_id');
});
}
2024-01-14 05:05:00 +01:00
public function industry(): BelongsTo
2023-04-26 09:41:30 +02:00
{
return $this->belongsTo(Industry::class);
}
2024-01-14 05:05:00 +01:00
public function size(): BelongsTo
2023-07-05 08:07:36 +02:00
{
return $this->belongsTo(Size::class);
}
2024-01-14 05:05:00 +01:00
public function locale(): string
{
if (! $this->language()) {
2022-01-17 08:53:39 +01:00
return 'en';
}
2022-01-17 08:53:39 +01:00
return $this->language()->locale ?: 'en';
}
public function date_format()
{
/** @var \Illuminate\Support\Collection<DateFormat> */
2024-06-03 02:19:37 +02:00
$date_formats = app('date_formats');
return $date_formats->first(function ($item) {
return $item->id == $this->getSetting('date_format_id');
})->format;
}
2019-08-28 04:36:53 +02:00
public function currency()
{
2024-06-14 09:09:44 +02:00
/** @var \Illuminate\Support\Collection<Currency> */
$currencies = app('currencies');
2021-06-10 05:56:02 +02:00
return $currencies->first(function ($item) {
return $item->id == $this->getSetting('currency_id');
});
2019-08-28 04:36:53 +02:00
}
2024-01-14 05:05:00 +01:00
public function service(): ClientService
{
return new ClientService($this);
}
2024-01-14 05:05:00 +01:00
public function updateBalance($amount): ClientService
{
return $this->service()->updateBalance($amount);
}
2019-09-12 05:23:44 +02:00
/**
* Returns the entire filtered set
2019-09-12 05:23:44 +02:00
* of settings which have been merged from
* Client > Group > Company levels.
*
2023-03-15 04:22:12 +01:00
* @return \stdClass stdClass object of settings
2019-09-12 05:23:44 +02:00
*/
2024-01-14 05:05:00 +01:00
public function getMergedSettings(): object
2019-04-29 02:54:26 +02:00
{
if ($this->group_settings !== null) {
2019-09-19 07:50:05 +02:00
$group_settings = ClientSettings::buildClientSettings($this->group_settings->settings, $this->settings);
2019-09-11 07:32:47 +02:00
2019-09-19 07:50:05 +02:00
return ClientSettings::buildClientSettings($this->company->settings, $group_settings);
2019-09-11 07:32:47 +02:00
}
2023-03-15 04:35:54 +01:00
return CompanySettings::setProperties(ClientSettings::buildClientSettings($this->company->settings, $this->settings));
}
2019-09-12 05:23:44 +02:00
/**
* Returns a single setting
* which cascades from
* Client > Group > Company.
*
2019-09-12 05:23:44 +02:00
* @param string $setting The Setting parameter
* @return mixed The setting requested
*/
2024-01-14 05:05:00 +01:00
public function getSetting($setting): mixed
2019-09-10 12:25:29 +02:00
{
/*Client Settings*/
if ($this->settings && property_exists($this->settings, $setting) && isset($this->settings->{$setting})) {
/*need to catch empty string here*/
if (is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >= 1)) {
2019-09-19 07:50:05 +02:00
return $this->settings->{$setting};
} elseif (is_bool($this->settings->{$setting})) {
2021-09-11 00:43:40 +02:00
return $this->settings->{$setting};
2023-10-26 04:57:44 +02:00
} elseif (is_int($this->settings->{$setting})) {
2022-08-10 05:35:47 +02:00
return $this->settings->{$setting};
2023-10-26 04:57:44 +02:00
} elseif(is_float($this->settings->{$setting})) {
2023-05-27 14:26:59 +02:00
return $this->settings->{$setting};
2021-09-11 00:43:40 +02:00
}
2019-09-19 07:50:05 +02:00
}
2019-09-10 12:25:29 +02:00
2019-10-08 13:14:23 +02:00
/*Group Settings*/
if ($this->group_settings && (property_exists($this->group_settings->settings, $setting) !== false) && (isset($this->group_settings->settings->{$setting}) !== false)) {
return $this->group_settings->settings->{$setting};
2019-09-19 07:50:05 +02:00
}
2019-10-08 13:14:23 +02:00
/*Company Settings*/
2024-03-16 02:36:40 +01:00
elseif ((property_exists($this->company->settings, $setting) !== false) && (isset($this->company->settings->{$setting}) !== false)) {
2019-09-11 05:46:23 +02:00
return $this->company->settings->{$setting};
} elseif (property_exists(CompanySettings::defaults(), $setting)) {
return CompanySettings::defaults()->{$setting};
}
return '';
2019-10-08 13:14:23 +02:00
}
public function getSettingEntity($setting)
{
/*Client Settings*/
if ($this->settings && (property_exists($this->settings, $setting) !== false) && (isset($this->settings->{$setting}) !== false)) {
2019-10-08 13:14:23 +02:00
/*need to catch empty string here*/
if (is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >= 1)) {
2019-10-08 13:14:23 +02:00
return $this;
}
}
/*Group Settings*/
if ($this->group_settings && (property_exists($this->group_settings->settings, $setting) !== false) && (isset($this->group_settings->settings->{$setting}) !== false)) {
return $this->group_settings;
2019-10-08 13:14:23 +02:00
}
/*Company Settings*/
if ((property_exists($this->company->settings, $setting) != false) && (isset($this->company->settings->{$setting}) !== false)) {
2019-10-08 13:14:23 +02:00
return $this->company;
}
2023-01-21 06:52:24 +01:00
throw new \Exception('Could not find a settings object', 1);
2019-09-10 12:25:29 +02:00
}
2023-08-11 09:58:43 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<Document>
*/
2024-01-14 05:05:00 +01:00
public function documents(): \Illuminate\Database\Eloquent\Relations\MorphMany
2019-04-28 07:31:32 +02:00
{
return $this->morphMany(Document::class, 'documentable');
}
2024-01-14 05:05:00 +01:00
public function group_settings(): BelongsTo
2019-09-10 04:30:43 +02:00
{
return $this->belongsTo(GroupSetting::class);
}
2019-09-13 07:52:01 +02:00
/**
* Returns the first Credit Card Gateway.
*
* @return null|CompanyGateway The Priority Credit Card gateway
2019-09-13 07:52:01 +02:00
*/
2024-01-14 05:05:00 +01:00
public function getCreditCardGateway(): ?CompanyGateway
2019-09-13 07:52:01 +02:00
{
$pms = $this->service()->getPaymentMethods(-1);
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::CREDIT_CARD) {
2024-01-14 05:05:00 +01:00
2023-08-08 10:56:31 +02:00
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
2024-01-14 05:05:00 +01:00
if($cg->gateway_key == '80af24a6a691230bbec33e930ab40666') { //ensure we don't attempt to authorize paypal platform - yet.
2024-01-05 07:33:11 +01:00
continue;
2024-01-14 05:05:00 +01:00
}
2024-01-05 07:33:11 +01:00
2024-01-05 07:21:40 +01:00
if ($cg && is_object($cg->fees_and_limits) && ! property_exists($cg->fees_and_limits, strval(GatewayType::CREDIT_CARD))) {
$fees_and_limits = $cg->fees_and_limits;
2024-01-14 05:05:00 +01:00
$fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits();
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
}
2019-09-13 07:52:01 +02:00
2024-01-14 05:05:00 +01:00
if ($cg && is_object($cg->fees_and_limits) && $cg->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
return $cg;
}
}
}
2019-09-13 07:52:01 +02:00
return null;
2019-09-13 07:52:01 +02:00
}
2023-12-18 05:25:16 +01:00
2024-01-14 05:05:00 +01:00
public function getBACSGateway(): ?CompanyGateway
2022-12-16 11:35:35 +01:00
{
$pms = $this->service()->getPaymentMethods(-1);
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::BACS) {
2023-08-08 10:56:31 +02:00
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
2022-12-16 11:35:35 +01:00
2024-07-14 13:28:54 +02:00
if ($cg && ! property_exists($cg->fees_and_limits, GatewayType::BACS)) { //@phpstan-ignore-line
2022-12-16 11:35:35 +01:00
$fees_and_limits = $cg->fees_and_limits;
2024-01-14 05:05:00 +01:00
$fees_and_limits->{GatewayType::BACS} = new FeesAndLimits();
2022-12-16 11:35:35 +01:00
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
}
if ($cg && $cg->fees_and_limits->{GatewayType::BACS}->is_enabled) {
return $cg;
}
}
}
return null;
}
2019-09-13 07:52:01 +02:00
2024-01-14 05:05:00 +01:00
public function getACSSGateway(): ?CompanyGateway
2023-12-18 05:25:16 +01:00
{
$pms = $this->service()->getPaymentMethods(-1);
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::ACSS) {
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
2024-07-14 13:28:54 +02:00
if ($cg && ! property_exists($cg->fees_and_limits, GatewayType::ACSS)) { //@phpstan-ignore-line
2023-12-18 05:25:16 +01:00
$fees_and_limits = $cg->fees_and_limits;
2024-01-14 05:05:00 +01:00
$fees_and_limits->{GatewayType::ACSS} = new FeesAndLimits();
2023-12-18 05:25:16 +01:00
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
}
if ($cg && $cg->fees_and_limits->{GatewayType::ACSS}->is_enabled) {
return $cg;
}
}
}
return null;
}
//todo refactor this - it is only searching for existing tokens
2024-01-14 05:05:00 +01:00
public function getBankTransferGateway(): ?CompanyGateway
2019-09-25 04:07:33 +02:00
{
$pms = $this->service()->getPaymentMethods(-1);
2020-07-03 10:14:15 +02:00
if ($this->currency()->code == 'USD' && in_array(GatewayType::BANK_TRANSFER, array_column($pms, 'gateway_type_id'))) {
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::BANK_TRANSFER) {
2023-08-08 10:56:31 +02:00
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
2024-07-14 13:28:54 +02:00
if ($cg && ! property_exists($cg->fees_and_limits, GatewayType::BANK_TRANSFER)) { //@phpstan-ignore-line
$fees_and_limits = $cg->fees_and_limits;
2024-01-14 05:05:00 +01:00
$fees_and_limits->{GatewayType::BANK_TRANSFER} = new FeesAndLimits();
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
}
if ($cg && $cg->fees_and_limits->{GatewayType::BANK_TRANSFER}->is_enabled) {
return $cg;
}
}
2020-07-03 10:14:15 +02:00
}
}
if ($this->currency()->code == 'EUR' && (in_array(GatewayType::BANK_TRANSFER, array_column($pms, 'gateway_type_id')) || in_array(GatewayType::SEPA, array_column($pms, 'gateway_type_id')))) {
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::SEPA) {
2023-08-08 10:56:31 +02:00
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
if ($cg && $cg->fees_and_limits->{GatewayType::SEPA}->is_enabled) {
return $cg;
}
}
2020-07-03 10:14:15 +02:00
}
}
2023-03-17 23:56:03 +01:00
if (in_array(GatewayType::DIRECT_DEBIT, array_column($pms, 'gateway_type_id'))) {
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::DIRECT_DEBIT) {
2023-08-08 10:56:31 +02:00
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
if ($cg && $cg->fees_and_limits->{GatewayType::DIRECT_DEBIT}->is_enabled) {
return $cg;
}
}
}
}
if (in_array($this->currency()->code, ['CAD','USD']) && in_array(GatewayType::ACSS, array_column($pms, 'gateway_type_id'))) {
// if ($this->currency()->code == 'CAD' && in_array(GatewayType::ACSS, array_column($pms, 'gateway_type_id'))) {
2023-12-18 05:25:16 +01:00
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::ACSS) {
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
if ($cg && $cg->fees_and_limits->{GatewayType::ACSS}->is_enabled) {
return $cg;
}
}
}
}
if (in_array($this->currency()->code, ['GBP']) && in_array(GatewayType::BACS, array_column($pms, 'gateway_type_id'))) {
// if ($this->currency()->code == 'CAD' && in_array(GatewayType::ACSS, array_column($pms, 'gateway_type_id'))) {
foreach ($pms as $pm) {
if ($pm['gateway_type_id'] == GatewayType::BACS) {
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
if ($cg && $cg->fees_and_limits->{GatewayType::BACS}->is_enabled) {
return $cg;
}
}
}
}
2023-12-18 05:25:16 +01:00
2020-07-03 10:14:15 +02:00
return null;
2019-09-25 04:07:33 +02:00
}
2020-07-03 02:56:36 +02:00
2020-07-03 14:39:29 +02:00
public function getBankTransferMethodType()
{
if ($this->currency()->code == 'USD') {
return GatewayType::BANK_TRANSFER;
}
if ($this->currency()->code == 'EUR') {
return GatewayType::SEPA;
}
//Special handler for GoCardless
if($this->currency()->code == 'CAD' && ($this->getBankTransferGateway()->gateway_key == 'b9886f9257f0c6ee7c302f1c74475f6c') ?? false) {
return GatewayType::DIRECT_DEBIT;
}
2023-03-17 23:56:03 +01:00
if (in_array($this->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD','USD'])) {
return GatewayType::DIRECT_DEBIT;
}
2023-12-18 05:25:16 +01:00
if(in_array($this->currency()->code, ['CAD'])) {
return GatewayType::ACSS;
}
2020-07-03 14:39:29 +02:00
}
2023-07-25 12:04:04 +02:00
public function getCurrencyCode(): string
2020-07-03 02:56:36 +02:00
{
2020-07-03 10:14:15 +02:00
if ($this->currency()) {
return $this->currency()->code;
}
2020-07-03 02:56:36 +02:00
2020-07-03 10:14:15 +02:00
return 'USD';
2020-07-03 02:56:36 +02:00
}
2024-01-14 05:05:00 +01:00
public function validGatewayForAmount($fees_and_limits_for_payment_type, $amount): bool
2020-10-12 06:10:34 +02:00
{
2020-11-25 15:19:52 +01:00
if (isset($fees_and_limits_for_payment_type)) {
$fees_and_limits = $fees_and_limits_for_payment_type;
} else {
return true;
}
2020-10-12 06:10:34 +02:00
2020-11-25 15:19:52 +01:00
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && $amount < $fees_and_limits->min_limit) {
return false;
}
2020-10-12 06:10:34 +02:00
2020-11-25 15:19:52 +01:00
if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $fees_and_limits->max_limit != -1 && $amount > $fees_and_limits->max_limit) {
return false;
}
2020-10-12 06:10:34 +02:00
2020-11-25 15:19:52 +01:00
return true;
2020-10-12 06:10:34 +02:00
}
public function preferredLocale()
{
$this->language()->locale ?? 'en';
}
2024-01-14 05:05:00 +01:00
public function backup_path(): string
2021-10-20 05:05:46 +02:00
{
return $this->company->company_key.'/'.$this->client_hash.'/backups';
}
2024-01-14 05:05:00 +01:00
public function invoice_filepath($invitation): string
{
2021-06-12 13:50:01 +02:00
$contact_key = $invitation->contact->contact_key;
2021-06-12 13:50:01 +02:00
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/invoices/';
}
public function e_document_filepath($invitation): string
2023-04-03 17:55:40 +02:00
{
$contact_key = $invitation->contact->contact_key;
2023-04-17 09:24:16 +02:00
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/e_invoice/';
2023-04-03 17:55:40 +02:00
}
2024-01-14 05:05:00 +01:00
public function quote_filepath($invitation): string
{
2021-06-12 13:50:01 +02:00
$contact_key = $invitation->contact->contact_key;
2021-06-12 13:50:01 +02:00
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/quotes/';
}
2024-01-14 05:05:00 +01:00
public function credit_filepath($invitation): string
{
2021-06-12 13:50:01 +02:00
$contact_key = $invitation->contact->contact_key;
2021-06-12 13:50:01 +02:00
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/credits/';
}
2024-01-14 05:05:00 +01:00
public function recurring_invoice_filepath($invitation): string
{
2021-06-12 13:50:01 +02:00
$contact_key = $invitation->contact->contact_key;
2021-06-12 13:50:01 +02:00
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/recurring_invoices/';
}
2024-01-14 05:05:00 +01:00
public function company_filepath(): string
{
return $this->company->company_key.'/';
}
2024-01-14 05:05:00 +01:00
public function document_filepath(): string
{
return $this->company->company_key.'/documents/';
}
2024-01-14 05:05:00 +01:00
public function setCompanyDefaults($data, $entity_name): array
{
$defaults = [];
2024-03-26 04:12:32 +01:00
$terms = &$data['terms'];
$footer = &$data['footer'];
2024-10-08 01:11:43 +02:00
if(!$terms || empty($terms)) {
$defaults['terms'] = $this->getSetting($entity_name.'_terms');
2024-03-26 04:12:32 +01:00
} elseif ($terms) {
$defaults['terms'] = $data['terms'];
}
2024-10-08 01:11:43 +02:00
if(!$footer || empty($footer)) {
$defaults['footer'] = $this->getSetting($entity_name.'_footer');
2024-03-26 04:12:32 +01:00
} elseif ($footer) {
$defaults['footer'] = $data['footer'];
}
2024-10-08 01:11:43 +02:00
if (strlen($this->public_notes ?? '') >= 1) {
$defaults['public_notes'] = $this->public_notes;
}
2023-07-24 07:22:12 +02:00
$exchange_rate = new CurrencyApi();
2024-01-14 05:05:00 +01:00
$defaults['exchange_rate'] = 1 / $exchange_rate->exchangeRate($this->getSetting('currency_id'), $this->company->settings->currency_id);
2023-07-24 07:22:12 +02:00
return $defaults;
}
2020-06-12 14:48:54 +02:00
2024-01-09 11:49:29 +01:00
public function setExchangeRate()
{
2024-01-14 05:05:00 +01:00
2024-01-09 11:49:29 +01:00
$converter = new CurrencyApi();
2024-01-14 05:05:00 +01:00
return 1 / $converter->convert(1, $this->currency()->id, $this->company->settings->currency_id);
2024-01-09 11:49:29 +01:00
}
2024-07-14 00:16:29 +02:00
public function utc_offset(): int
{
$offset = 0;
$timezone = $this->timezone();
date_default_timezone_set('GMT');
$date = new \DateTime("now", new \DateTimeZone($timezone->name));
$offset = $date->getOffset();
return $offset;
}
2024-01-14 05:05:00 +01:00
public function timezone_offset(): int
2021-06-10 23:34:03 +02:00
{
2021-06-11 23:41:02 +02:00
$offset = 0;
2021-06-10 23:34:03 +02:00
$entity_send_time = $this->getSetting('entity_send_time');
if ($entity_send_time == 0) {
2021-06-10 23:34:03 +02:00
return 0;
}
2021-06-10 23:34:03 +02:00
2023-11-15 05:36:24 +01:00
$offset -= $this->company->utc_offset();
2021-06-10 23:34:03 +02:00
2021-06-11 23:41:02 +02:00
$offset += ($entity_send_time * 3600);
2021-06-10 23:34:03 +02:00
return $offset;
}
2022-03-10 01:32:04 +01:00
public function transaction_event()
{
2022-03-10 02:17:05 +01:00
$client = $this->fresh();
2022-03-10 01:32:04 +01:00
return [
'client_id' => $client->id,
'client_balance' => $client->balance ?: 0,
2022-03-10 02:17:05 +01:00
'client_paid_to_date' => $client->paid_to_date ?: 0,
'client_credit_balance' => $client->credit_balance ?: 0,
2022-03-10 01:32:04 +01:00
];
}
2024-01-14 05:05:00 +01:00
public function translate_entity(): string
2022-04-06 02:38:01 +02:00
{
return ctrans('texts.client');
}
2023-06-07 08:25:41 +02:00
public function portalUrl(bool $use_react_url): string
{
2024-01-14 05:05:00 +01:00
return $use_react_url ? config('ninja.react_url'). "/#/clients/{$this->hashed_id}" : config('ninja.app_url');
2023-06-07 08:25:41 +02:00
}
}