2018-10-15 07:00:48 +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
|
|
|
|
*
|
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
|
|
|
*/
|
2018-10-15 07:00:48 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-03-02 22:44:08 +01:00
|
|
|
use App\DataMapper\ClientSettings;
|
2019-04-29 02:54:26 +02:00
|
|
|
use App\DataMapper\CompanySettings;
|
2021-07-09 01:46:18 +02:00
|
|
|
use App\DataMapper\FeesAndLimits;
|
2021-07-04 05:16:12 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2021-08-01 07:46:40 +02:00
|
|
|
use App\Models\Expense;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Presenters\ClientPresenter;
|
2021-08-01 07:46:40 +02:00
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Models\Task;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Services\Client\ClientService;
|
2021-06-10 05:56:02 +02:00
|
|
|
use App\Utils\Traits\AppSetup;
|
2022-04-03 11:39:55 +02:00
|
|
|
use App\Utils\Traits\ClientGroupSettingsSaver;
|
2019-06-03 07:31:20 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2019-04-30 08:02:39 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2018-11-10 14:24:36 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Exception;
|
2020-02-10 10:53:02 +01:00
|
|
|
use Illuminate\Contracts\Translation\HasLocalePreference;
|
2018-11-12 08:52:20 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2019-11-05 23:52:57 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-01-25 11:47:23 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-15 07:00:48 +02:00
|
|
|
|
2020-02-10 10:53:02 +01:00
|
|
|
class Client extends BaseModel implements HasLocalePreference
|
2018-10-15 07:00:48 +02:00
|
|
|
{
|
2018-11-02 11:54:46 +01:00
|
|
|
use PresentableTrait;
|
2018-11-10 14:24:36 +01:00
|
|
|
use MakesHash;
|
2019-04-30 08:02:39 +02:00
|
|
|
use MakesDates;
|
2018-11-12 08:52:20 +01:00
|
|
|
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;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
protected $presenter = ClientPresenter::class;
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2019-07-30 00:28:38 +02:00
|
|
|
protected $hidden = [
|
2018-11-27 07:59:16 +01:00
|
|
|
'id',
|
2019-07-30 00:28:38 +02:00
|
|
|
'private_notes',
|
|
|
|
'user_id',
|
|
|
|
'company_id',
|
|
|
|
'last_login',
|
2018-11-10 14:24:36 +01:00
|
|
|
];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-06-26 06:04:10 +02:00
|
|
|
protected $fillable = [
|
2020-06-21 23:23:32 +02:00
|
|
|
'assigned_user_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'name',
|
|
|
|
'website',
|
|
|
|
'private_notes',
|
|
|
|
'industry_id',
|
|
|
|
'size_id',
|
|
|
|
'address1',
|
|
|
|
'address2',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'postal_code',
|
|
|
|
'country_id',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_value3',
|
2019-12-16 12:34:38 +01:00
|
|
|
'custom_value4',
|
2019-06-26 06:04:10 +02:00
|
|
|
'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',
|
2020-09-06 11:38:10 +02:00
|
|
|
'public_notes',
|
2021-01-25 11:34:12 +01:00
|
|
|
'phone',
|
|
|
|
'number',
|
2019-06-26 06:04:10 +02:00
|
|
|
];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
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
|
|
|
];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-02-17 11:34:46 +01:00
|
|
|
protected $casts = [
|
2019-12-30 22:59:12 +01:00
|
|
|
'is_deleted' => 'boolean',
|
2019-11-10 13:06:30 +01:00
|
|
|
'country_id' => 'string',
|
2019-09-26 00:27:26 +02:00
|
|
|
'settings' => 'object',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-02-17 11:34:46 +01:00
|
|
|
];
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2021-04-05 10:43:25 +02:00
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-11 13:19:05 +02:00
|
|
|
public function ledger()
|
|
|
|
{
|
2020-06-20 01:23:41 +02:00
|
|
|
return $this->hasMany(CompanyLedger::class)->orderBy('id', 'desc');
|
2020-04-11 13:19:05 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 07:51:19 +02:00
|
|
|
public function company_ledger()
|
|
|
|
{
|
|
|
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
|
|
|
}
|
|
|
|
|
2019-09-08 12:39:13 +02:00
|
|
|
public function gateway_tokens()
|
|
|
|
{
|
2022-03-16 06:29:39 +01:00
|
|
|
return $this->hasMany(ClientGatewayToken::class)->orderBy('is_default', 'DESC');
|
2019-09-08 12:39:13 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 07:46:40 +02:00
|
|
|
public function expenses()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Expense::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function projects()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Project::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:33:48 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the specific payment token per
|
2020-09-06 11:38:10 +02:00
|
|
|
* 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-12-30 22:59:12 +01:00
|
|
|
*
|
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();
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
public function credits()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Credit::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2019-11-12 05:41:02 +01:00
|
|
|
public function activities()
|
|
|
|
{
|
2022-04-27 08:52:29 +02:00
|
|
|
return $this->hasMany(Activity::class)->take(50)->orderBy('id', 'desc');
|
2019-11-12 05:41:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 04:16:17 +01:00
|
|
|
public function contacts()
|
|
|
|
{
|
2019-01-25 11:47:23 +01:00
|
|
|
return $this->hasMany(ClientContact::class)->orderBy('is_primary', 'desc');
|
2018-10-29 04:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function primary_contact()
|
|
|
|
{
|
2020-04-19 12:29:58 +02:00
|
|
|
return $this->hasMany(ClientContact::class)->where('is_primary', true);
|
2018-10-29 04:16:17 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
2019-09-25 07:55:52 +02:00
|
|
|
public function user()
|
|
|
|
{
|
2021-09-04 05:35:53 +02:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2019-09-25 07:55:52 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:16:38 +01:00
|
|
|
public function assigned_user()
|
|
|
|
{
|
2021-09-08 01:29:20 +02:00
|
|
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
2019-11-05 11:16:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 10:34:38 +01:00
|
|
|
public function country()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Country::class);
|
2020-05-26 14:37:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invoices()
|
|
|
|
{
|
2020-07-07 09:54:21 +02:00
|
|
|
return $this->hasMany(Invoice::class)->withTrashed();
|
2019-01-26 10:34:38 +01:00
|
|
|
}
|
|
|
|
|
2021-08-01 07:46:40 +02:00
|
|
|
public function quotes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Quote::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tasks()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:36:14 +02:00
|
|
|
public function recurring_invoices()
|
|
|
|
{
|
|
|
|
return $this->hasMany(RecurringInvoice::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:46:39 +01:00
|
|
|
public function recurring_expenses()
|
|
|
|
{
|
|
|
|
return $this->hasMany(RecurringExpense::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2019-01-26 10:34:38 +01:00
|
|
|
public function shipping_country()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Country::class, 'shipping_country_id', 'id');
|
|
|
|
}
|
|
|
|
|
2020-08-24 04:45:53 +02:00
|
|
|
public function system_logs()
|
|
|
|
{
|
2021-07-27 01:02:14 +02:00
|
|
|
return $this->hasMany(SystemLog::class)->orderBy('id', 'desc');
|
2020-08-24 04:45:53 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-04-24 02:22:02 +02:00
|
|
|
public function timezone()
|
|
|
|
{
|
2019-09-11 07:32:47 +02:00
|
|
|
return Timezone::find($this->getSetting('timezone_id'));
|
2019-04-24 02:22:02 +02:00
|
|
|
}
|
|
|
|
|
2019-12-10 21:25:54 +01:00
|
|
|
public function language()
|
|
|
|
{
|
2020-04-19 12:29:58 +02:00
|
|
|
|
|
|
|
$languages = Cache::get('languages');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-06-10 05:56:02 +02:00
|
|
|
if(!$languages)
|
|
|
|
$this->buildCache(true);
|
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
return $languages->filter(function ($item) {
|
|
|
|
return $item->id == $this->getSetting('language_id');
|
|
|
|
})->first();
|
2021-06-10 04:24:32 +02:00
|
|
|
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function locale()
|
|
|
|
{
|
2022-01-17 08:53:39 +01:00
|
|
|
if(!$this->language())
|
|
|
|
return 'en';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return $this->language()->locale ?: 'en';
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:13:10 +02:00
|
|
|
public function date_format()
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-11-05 23:52:57 +01:00
|
|
|
$date_formats = Cache::get('date_formats');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-12-09 06:34:23 +01:00
|
|
|
if(!$date_formats)
|
|
|
|
$this->buildCache(true);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $date_formats->filter(function ($item) {
|
2019-11-05 23:52:57 +01:00
|
|
|
return $item->id == $this->getSetting('date_format_id');
|
|
|
|
})->first()->format;
|
2019-08-28 03:13:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 04:36:53 +02:00
|
|
|
public function currency()
|
|
|
|
{
|
2019-11-05 23:52:57 +01:00
|
|
|
$currencies = Cache::get('currencies');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-06-10 05:56:02 +02:00
|
|
|
if(!$currencies)
|
|
|
|
$this->buildCache(true);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $currencies->filter(function ($item) {
|
2019-11-05 23:52:57 +01:00
|
|
|
return $item->id == $this->getSetting('currency_id');
|
|
|
|
})->first();
|
2019-08-28 04:36:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
public function service() :ClientService
|
|
|
|
{
|
|
|
|
return new ClientService($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateBalance($amount) :ClientService
|
|
|
|
{
|
|
|
|
return $this->service()->updateBalance($amount);
|
|
|
|
}
|
2020-02-05 05:06:03 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/**
|
|
|
|
* Adjusts client "balances" when a client
|
|
|
|
* makes a payment that goes on file, but does
|
2020-09-06 11:38:10 +02:00
|
|
|
* not effect the client.balance record.
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-02-03 11:33:07 +01:00
|
|
|
* @param float $amount Adjustment amount
|
2020-03-21 06:37:30 +01:00
|
|
|
* @return Client
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
2020-06-05 05:52:53 +02:00
|
|
|
// public function processUnappliedPayment($amount) :Client
|
|
|
|
// {
|
|
|
|
// return $this->service()->updatePaidToDate($amount)
|
|
|
|
// ->adjustCreditBalance($amount)
|
|
|
|
// ->save();
|
|
|
|
// }
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2019-09-12 05:23:44 +02:00
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* Returns the entire filtered set
|
2019-09-12 05:23:44 +02:00
|
|
|
* of settings which have been merged from
|
2020-09-06 11:38:10 +02:00
|
|
|
* Client > Group > Company levels.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2020-11-01 06:09:09 +01:00
|
|
|
* @return stdClass stdClass object of settings
|
2019-09-12 05:23:44 +02:00
|
|
|
*/
|
2019-09-21 06:09:25 +02:00
|
|
|
public function getMergedSettings() :object
|
2019-04-29 02:54:26 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01: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
|
|
|
}
|
|
|
|
|
2019-09-21 06:09:25 +02:00
|
|
|
return CompanySettings::setProperties(ClientSettings::buildClientSettings($this->company->settings, $this->settings));
|
2019-04-29 07:50:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 05:23:44 +02:00
|
|
|
/**
|
|
|
|
* Returns a single setting
|
|
|
|
* which cascades from
|
2020-09-06 11:38:10 +02:00
|
|
|
* Client > Group > Company.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-09-12 05:23:44 +02:00
|
|
|
* @param string $setting The Setting parameter
|
|
|
|
* @return mixed The setting requested
|
|
|
|
*/
|
2019-09-10 12:25:29 +02:00
|
|
|
public function getSetting($setting)
|
|
|
|
{
|
2019-09-19 07:50:05 +02:00
|
|
|
|
2020-03-08 10:06:21 +01:00
|
|
|
/*Client Settings*/
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->settings && property_exists($this->settings, $setting) && isset($this->settings->{$setting})) {
|
|
|
|
/*need to catch empty string here*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if (is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >= 1)) {
|
2019-09-19 07:50:05 +02:00
|
|
|
return $this->settings->{$setting};
|
|
|
|
}
|
2021-09-11 00:43:40 +02:00
|
|
|
elseif(is_bool($this->settings->{$setting})){
|
|
|
|
return $this->settings->{$setting};
|
|
|
|
}
|
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*/
|
2019-12-30 22:59:12 +01:00
|
|
|
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-12-22 11:28:41 +01:00
|
|
|
|
2019-10-08 13:14:23 +02:00
|
|
|
/*Company Settings*/
|
2020-03-21 06:37:30 +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};
|
2019-09-19 07:50:05 +02:00
|
|
|
}
|
2019-10-05 03:48:00 +02:00
|
|
|
|
2021-03-17 10:28:44 +01:00
|
|
|
elseif( property_exists(CompanySettings::defaults(), $setting) ) {
|
|
|
|
return CompanySettings::defaults()->{$setting};
|
|
|
|
}
|
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
return '';
|
|
|
|
|
|
|
|
// throw new \Exception("Settings corrupted", 1);
|
2019-10-08 13:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSettingEntity($setting)
|
|
|
|
{
|
|
|
|
/*Client Settings*/
|
2019-12-30 22:59:12 +01:00
|
|
|
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*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if (is_string($this->settings->{$setting}) && (iconv_strlen($this->settings->{$setting}) >= 1)) {
|
2019-10-08 13:14:23 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Group Settings*/
|
2019-12-30 22:59:12 +01:00
|
|
|
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*/
|
2019-12-30 22:59:12 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
throw new Exception('Could not find a settings object', 1);
|
2019-09-10 12:25:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 07:31:32 +02:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
|
|
|
|
2019-09-10 04:30:43 +02:00
|
|
|
public function group_settings()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(GroupSetting::class);
|
|
|
|
}
|
|
|
|
|
2019-09-13 07:52:01 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the first Credit Card Gateway.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2020-09-06 11:38:10 +02:00
|
|
|
* @return null|CompanyGateway The Priority Credit Card gateway
|
2019-09-13 07:52:01 +02:00
|
|
|
*/
|
|
|
|
public function getCreditCardGateway() :?CompanyGateway
|
|
|
|
{
|
2021-07-07 00:46:03 +02:00
|
|
|
|
|
|
|
$pms = $this->service()->getPaymentMethods(0);
|
|
|
|
|
|
|
|
foreach($pms as $pm)
|
|
|
|
{
|
|
|
|
|
|
|
|
if($pm['gateway_type_id'] == GatewayType::CREDIT_CARD)
|
|
|
|
{
|
|
|
|
$cg = CompanyGateway::find($pm['company_gateway_id']);
|
|
|
|
|
2021-07-09 01:46:18 +02:00
|
|
|
if($cg && !property_exists($cg->fees_and_limits, GatewayType::CREDIT_CARD)){
|
|
|
|
$fees_and_limits = $cg->fees_and_limits;
|
|
|
|
$fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits;
|
|
|
|
$cg->fees_and_limits = $fees_and_limits;
|
|
|
|
$cg->save();
|
|
|
|
}
|
|
|
|
|
2021-07-07 00:46:03 +02:00
|
|
|
if($cg && $cg->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled)
|
|
|
|
return $cg;
|
2019-09-13 07:52:01 +02:00
|
|
|
|
2021-07-07 00:46:03 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-09-13 07:52:01 +02:00
|
|
|
|
2021-07-07 00:46:03 +02:00
|
|
|
return null;
|
|
|
|
|
2019-09-13 07:52:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 00:46:03 +02:00
|
|
|
//todo refactor this - it is only searching for existing tokens
|
2020-07-03 10:14:15 +02:00
|
|
|
public function getBankTransferGateway() :?CompanyGateway
|
2019-09-25 04:07:33 +02:00
|
|
|
{
|
2021-07-04 05:16:12 +02:00
|
|
|
$pms = $this->service()->getPaymentMethods(0);
|
2020-07-03 10:14:15 +02:00
|
|
|
|
2021-07-04 05:16:12 +02:00
|
|
|
if($this->currency()->code == 'USD' && in_array(GatewayType::BANK_TRANSFER, array_column($pms, 'gateway_type_id'))){
|
2019-09-25 04:07:33 +02:00
|
|
|
|
2021-07-04 05:16:12 +02:00
|
|
|
foreach($pms as $pm){
|
2021-07-07 00:46:03 +02:00
|
|
|
|
2021-07-04 05:16:12 +02:00
|
|
|
if($pm['gateway_type_id'] == GatewayType::BANK_TRANSFER)
|
2021-07-07 00:46:03 +02:00
|
|
|
{
|
|
|
|
$cg = CompanyGateway::find($pm['company_gateway_id']);
|
|
|
|
|
2021-07-09 07:50:16 +02:00
|
|
|
if($cg && !property_exists($cg->fees_and_limits, GatewayType::BANK_TRANSFER)){
|
2021-07-09 01:46:18 +02:00
|
|
|
$fees_and_limits = $cg->fees_and_limits;
|
|
|
|
$fees_and_limits->{GatewayType::BANK_TRANSFER} = new FeesAndLimits;
|
|
|
|
$cg->fees_and_limits = $fees_and_limits;
|
|
|
|
$cg->save();
|
|
|
|
}
|
|
|
|
|
2021-07-07 00:46:03 +02:00
|
|
|
if($cg && $cg->fees_and_limits->{GatewayType::BANK_TRANSFER}->is_enabled)
|
|
|
|
return $cg;
|
|
|
|
}
|
2020-07-03 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 05:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($this->currency()->code == 'EUR' && in_array(GatewayType::BANK_TRANSFER, array_column($pms, 'gateway_type_id'))){
|
|
|
|
|
|
|
|
foreach($pms as $pm){
|
2021-07-07 00:46:03 +02:00
|
|
|
|
2021-07-04 05:16:12 +02:00
|
|
|
if($pm['gateway_type_id'] == GatewayType::SEPA)
|
2021-07-07 00:46:03 +02:00
|
|
|
{
|
|
|
|
$cg = CompanyGateway::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
|
|
|
}
|
2021-07-04 05:16:12 +02:00
|
|
|
|
2020-07-03 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-29 07:07:40 +02:00
|
|
|
// if ($this->currency()->code == 'EUR' && in_array(GatewayType::SEPA, array_column($pms, 'gateway_type_id'))) {
|
|
|
|
// foreach ($pms as $pm) {
|
|
|
|
// if ($pm['gateway_type_id'] == GatewayType::SEPA) {
|
|
|
|
// $cg = CompanyGateway::find($pm['company_gateway_id']);
|
|
|
|
|
|
|
|
// if ($cg && $cg->fees_and_limits->{GatewayType::SEPA}->is_enabled) {
|
|
|
|
// return $cg;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2021-10-19 18:05:27 +02:00
|
|
|
|
2021-11-03 01:55:45 +01:00
|
|
|
if ($this->country && $this->country->iso_3166_3 == 'GBR' && in_array(GatewayType::DIRECT_DEBIT, array_column($pms, 'gateway_type_id'))) {
|
2021-10-19 15:37:23 +02:00
|
|
|
foreach ($pms as $pm) {
|
|
|
|
if ($pm['gateway_type_id'] == GatewayType::DIRECT_DEBIT) {
|
|
|
|
$cg = CompanyGateway::find($pm['company_gateway_id']);
|
|
|
|
|
|
|
|
if ($cg && $cg->fees_and_limits->{GatewayType::DIRECT_DEBIT}->is_enabled) {
|
|
|
|
return $cg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:14:15 +02:00
|
|
|
return null;
|
2021-07-04 05:16:12 +02:00
|
|
|
|
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()
|
|
|
|
{
|
2021-07-04 05:16:12 +02:00
|
|
|
|
2020-07-03 14:39:29 +02:00
|
|
|
if ($this->currency()->code == 'USD') {
|
|
|
|
return GatewayType::BANK_TRANSFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->currency()->code == 'EUR') {
|
|
|
|
return GatewayType::SEPA;
|
|
|
|
}
|
2021-10-19 15:37:23 +02:00
|
|
|
|
|
|
|
if ($this->currency()->code == 'GBP') {
|
|
|
|
return GatewayType::DIRECT_DEBIT;
|
|
|
|
}
|
2020-07-03 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 10:14:15 +02:00
|
|
|
public function getCurrencyCode()
|
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
|
|
|
}
|
|
|
|
|
2020-10-12 13:02:02 +02: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
|
|
|
}
|
|
|
|
|
2020-02-10 10:53:02 +01:00
|
|
|
public function preferredLocale()
|
|
|
|
{
|
|
|
|
$languages = Cache::get('languages');
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-06-10 05:56:02 +02:00
|
|
|
if(!$languages)
|
|
|
|
$this->buildCache(true);
|
|
|
|
|
2020-02-10 10:53:02 +01:00
|
|
|
return $languages->filter(function ($item) {
|
2020-02-17 10:37:44 +01:00
|
|
|
return $item->id == $this->getSetting('language_id');
|
2020-02-10 10:53:02 +01:00
|
|
|
})->first()->locale;
|
|
|
|
}
|
|
|
|
|
2021-10-20 05:05:46 +02:00
|
|
|
public function backup_path()
|
|
|
|
{
|
|
|
|
return $this->company->company_key.'/'.$this->client_hash.'/backups';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
public function invoice_filepath($invitation)
|
|
|
|
{
|
|
|
|
$contact_key = $invitation->contact->contact_key;
|
|
|
|
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/invoices/';
|
2020-02-15 10:06:30 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
public function quote_filepath($invitation)
|
2020-02-19 21:44:12 +01:00
|
|
|
{
|
2021-06-12 13:50:01 +02:00
|
|
|
$contact_key = $invitation->contact->contact_key;
|
|
|
|
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/quotes/';
|
2020-02-19 21:44:12 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
public function credit_filepath($invitation)
|
2020-02-19 21:44:12 +01:00
|
|
|
{
|
2021-06-12 13:50:01 +02:00
|
|
|
$contact_key = $invitation->contact->contact_key;
|
|
|
|
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/credits/';
|
2020-02-19 21:44:12 +01:00
|
|
|
}
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
public function recurring_invoice_filepath($invitation)
|
2021-01-15 03:02:55 +01:00
|
|
|
{
|
2021-06-12 13:50:01 +02:00
|
|
|
$contact_key = $invitation->contact->contact_key;
|
|
|
|
return $this->company->company_key.'/'.$this->client_hash.'/'.$contact_key.'/recurring_invoices/';
|
2021-01-15 03:02:55 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
public function company_filepath()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return $this->company->company_key.'/';
|
2020-03-05 08:14:57 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
public function document_filepath()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return $this->company->company_key.'/documents/';
|
2020-04-10 07:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 07:45:24 +01:00
|
|
|
public function setCompanyDefaults($data, $entity_name) :array
|
2020-03-03 10:44:26 +01:00
|
|
|
{
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults = [];
|
2020-03-08 10:06:21 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! (array_key_exists('terms', $data) && strlen($data['terms']) > 1)) {
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults['terms'] = $this->getSetting($entity_name.'_terms');
|
2020-03-21 06:37:30 +01:00
|
|
|
} elseif (array_key_exists('terms', $data)) {
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults['terms'] = $data['terms'];
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-06 23:48:03 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! (array_key_exists('footer', $data) && strlen($data['footer']) > 1)) {
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults['footer'] = $this->getSetting($entity_name.'_footer');
|
2020-03-21 06:37:30 +01:00
|
|
|
} elseif (array_key_exists('footer', $data)) {
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults['footer'] = $data['footer'];
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-06 23:48:03 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (strlen($this->public_notes) >= 1) {
|
2020-03-10 07:45:24 +01:00
|
|
|
$defaults['public_notes'] = $this->public_notes;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-06 12:57:11 +01:00
|
|
|
|
2020-03-10 07:45:24 +01:00
|
|
|
return $defaults;
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|
2020-06-12 14:48:54 +02:00
|
|
|
|
|
|
|
public function payments()
|
|
|
|
{
|
2021-08-01 07:46:40 +02:00
|
|
|
return $this->hasMany(Payment::class)->withTrashed();
|
2020-06-12 14:48:54 +02:00
|
|
|
}
|
2021-06-10 23:34:03 +02:00
|
|
|
|
|
|
|
public function timezone_offset()
|
|
|
|
{
|
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)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
$timezone = $this->company->timezone();
|
|
|
|
|
2021-06-11 23:41:02 +02:00
|
|
|
$offset -= $timezone->utc_offset;
|
|
|
|
$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 [
|
2022-03-10 02:17:05 +01:00
|
|
|
'client_id' => $client->id,
|
|
|
|
'client_balance' => $client->balance ?: 0,
|
|
|
|
'client_paid_to_date' => $client->paid_to_date ?: 0,
|
|
|
|
'client_credit_balance' => $client->credit_balance ?: 0
|
2022-03-10 01:32:04 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-04-06 02:38:01 +02:00
|
|
|
public function translate_entity()
|
|
|
|
{
|
|
|
|
return ctrans('texts.client');
|
|
|
|
}
|
2018-10-15 07:00:48 +02:00
|
|
|
}
|