1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Models/Client.php

606 lines
14 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
use Carbon;
2017-01-30 20:40:43 +01:00
use DB;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2017-01-30 20:40:43 +01:00
use Laracasts\Presenter\PresentableTrait;
2018-04-09 13:45:38 +02:00
use App\Models\Traits\HasCustomMessages;
2017-01-30 20:40:43 +01:00
use Utils;
2015-03-31 11:38:24 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class Client.
*/
2015-03-16 22:45:25 +01:00
class Client extends EntityModel
{
2015-11-12 21:36:28 +01:00
use PresentableTrait;
2015-03-31 11:38:24 +02:00
use SoftDeletes;
2018-04-09 13:45:38 +02:00
use HasCustomMessages;
2015-11-12 21:36:28 +01:00
/**
* @var string
*/
2015-11-12 21:36:28 +01:00
protected $presenter = 'App\Ninja\Presenters\ClientPresenter';
/**
* @var array
*/
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
/**
* @var array
*/
2015-10-28 20:22:07 +01:00
protected $fillable = [
'name',
'id_number',
'vat_number',
'work_phone',
'custom_value1',
'custom_value2',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
'private_notes',
'size_id',
'industry_id',
'currency_id',
'language_id',
'payment_terms',
'website',
2017-03-29 10:46:52 +02:00
'invoice_number_counter',
'quote_number_counter',
2017-05-16 15:00:56 +02:00
'public_notes',
'task_rate',
2017-11-19 13:52:42 +01:00
'shipping_address1',
'shipping_address2',
'shipping_city',
'shipping_state',
'shipping_postal_code',
'shipping_country_id',
2017-11-21 08:35:28 +01:00
'show_tasks_in_portal',
'send_reminders',
2018-04-09 13:45:38 +02:00
'custom_messages',
2015-10-28 20:22:07 +01:00
];
/**
* @return array
*/
2015-11-24 20:45:38 +01:00
public static function getImportColumns()
{
return [
2017-08-03 19:07:16 +02:00
'name',
'work_phone',
'address1',
'address2',
'city',
'state',
'postal_code',
'public_notes',
'private_notes',
'country',
'website',
'currency',
'vat_number',
'id_number',
'custom1',
'custom2',
'contact_first_name',
'contact_last_name',
'contact_phone',
'contact_email',
'contact_custom1',
'contact_custom2',
2015-11-24 20:45:38 +01:00
];
}
/**
* @return array
*/
2015-11-24 20:45:38 +01:00
public static function getImportMap()
{
return [
2017-08-03 19:07:16 +02:00
'first' => 'contact_first_name',
'last^last4' => 'contact_last_name',
2017-08-03 19:07:16 +02:00
'email' => 'contact_email',
'work|office' => 'work_phone',
'mobile|phone' => 'contact_phone',
'name|organization|description^card' => 'name',
'apt|street2|address2|line2' => 'address2',
'street|address1|line1^avs' => 'address1',
2015-11-25 10:35:24 +01:00
'city' => 'city',
'state|province' => 'state',
'zip|postal|code^avs' => 'postal_code',
2015-11-25 10:35:24 +01:00
'country' => 'country',
2017-08-03 19:07:16 +02:00
'public' => 'public_notes',
'private|note' => 'private_notes',
'site|website' => 'website',
2017-08-03 19:07:16 +02:00
'currency' => 'currency',
'vat' => 'vat_number',
2017-03-27 22:05:18 +02:00
'number' => 'id_number',
2015-11-24 20:45:38 +01:00
];
}
2015-03-16 22:45:25 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function account()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Account');
2015-03-16 22:45:25 +01:00
}
/**
* @return mixed
*/
2015-10-22 20:48:12 +02:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2015-10-22 20:48:12 +02:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function invoices()
{
2015-03-31 11:38:24 +02:00
return $this->hasMany('App\Models\Invoice');
2015-03-16 22:45:25 +01:00
}
2016-09-23 11:02:48 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function quotes()
{
return $this->hasMany('App\Models\Invoice')->where('invoice_type_id', '=', INVOICE_TYPE_QUOTE);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function publicQuotes()
{
return $this->hasMany('App\Models\Invoice')->where('invoice_type_id', '=', INVOICE_TYPE_QUOTE)->whereIsPublic(true);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function payments()
{
2015-03-31 11:38:24 +02:00
return $this->hasMany('App\Models\Payment');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function contacts()
{
2015-03-31 11:38:24 +02:00
return $this->hasMany('App\Models\Contact');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function country()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Country');
2015-03-16 22:45:25 +01:00
}
2017-11-19 13:52:42 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function shipping_country()
{
return $this->belongsTo('App\Models\Country');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function currency()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Currency');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function language()
{
return $this->belongsTo('App\Models\Language');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function size()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Size');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function industry()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Industry');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-02-03 04:21:13 +01:00
public function credits()
{
return $this->hasMany('App\Models\Credit');
}
2016-09-23 11:02:48 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function creditsWithBalance()
{
return $this->hasMany('App\Models\Credit')->where('balance', '>', 0);
}
/**
* @return mixed
*/
2016-02-17 09:10:33 +01:00
public function expenses()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\Expense', 'client_id', 'id')->withTrashed();
2016-02-17 09:10:33 +01:00
}
2016-02-03 04:21:13 +01:00
/**
* @param $data
* @param bool $isPrimary
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Database\Eloquent\Model
*/
2015-10-28 20:22:07 +01:00
public function addContact($data, $isPrimary = false)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : (isset($data['id']) ? $data['id'] : false);
2015-10-28 20:22:07 +01:00
// check if this client wasRecentlyCreated to ensure a new contact is
// always created even if the request includes a contact id
2018-06-19 21:46:35 +02:00
if (! $this->wasRecentlyCreated && $publicId && intval($publicId) > 0) {
$contact = Contact::scope($publicId)->whereClientId($this->id)->firstOrFail();
2015-10-28 20:22:07 +01:00
} else {
$contact = Contact::createNew();
$contact->send_invoice = true;
2016-12-28 19:50:21 +01:00
if (isset($data['contact_key']) && $this->account->account_key == env('NINJA_LICENSE_ACCOUNT_KEY')) {
$contact->contact_key = $data['contact_key'];
} else {
2017-04-02 19:46:01 +02:00
$contact->contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
2016-12-28 19:50:21 +01:00
}
2015-10-28 20:22:07 +01:00
}
2016-06-20 16:14:43 +02:00
if ($this->account->isClientPortalPasswordEnabled()) {
2017-01-30 20:40:43 +01:00
if (! empty($data['password']) && $data['password'] != '-%unchanged%-') {
2016-02-29 22:46:27 +01:00
$contact->password = bcrypt($data['password']);
2017-01-30 17:05:31 +01:00
} elseif (empty($data['password'])) {
2016-02-29 22:46:27 +01:00
$contact->password = null;
}
}
2016-06-20 16:14:43 +02:00
2015-10-28 20:22:07 +01:00
$contact->fill($data);
$contact->is_primary = $isPrimary;
2017-07-02 19:55:21 +02:00
$contact->email = trim($contact->email);
2015-10-28 20:22:07 +01:00
return $this->contacts()->save($contact);
}
/**
* @param $balanceAdjustment
* @param $paidToDateAdjustment
*/
2015-10-28 20:22:07 +01:00
public function updateBalances($balanceAdjustment, $paidToDateAdjustment)
{
2017-03-10 15:36:47 +01:00
if ($balanceAdjustment == 0 && $paidToDateAdjustment == 0) {
2015-10-28 20:22:07 +01:00
return;
}
$this->balance = $this->balance + $balanceAdjustment;
$this->paid_to_date = $this->paid_to_date + $paidToDateAdjustment;
2016-06-20 16:14:43 +02:00
2015-10-28 20:22:07 +01:00
$this->save();
}
/**
* @return string
*/
2015-10-28 20:22:07 +01:00
public function getRoute()
{
return "/clients/{$this->public_id}";
}
/**
* @return float|int
*/
2015-03-16 22:45:25 +01:00
public function getTotalCredit()
{
return DB::table('credits')
->where('client_id', '=', $this->id)
->whereNull('deleted_at')
->sum('balance');
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getName()
{
2015-07-02 22:21:29 +02:00
return $this->name;
2015-03-16 22:45:25 +01:00
}
2016-06-20 16:14:43 +02:00
/**
* @return mixed
*/
2016-05-08 14:37:32 +02:00
public function getPrimaryContact()
{
2018-05-06 21:45:47 +02:00
if (! $this->relationLoaded('contacts')) {
$this->load('contacts');
}
foreach ($this->contacts as $contact) {
if ($contact->is_primary) {
return $contact;
}
}
return false;
2016-05-08 14:37:32 +02:00
}
2016-06-20 16:14:43 +02:00
/**
* @return mixed|string
*/
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->name) {
return $this->name;
2018-05-06 21:45:47 +02:00
} else if ($contact = $this->getPrimaryContact()) {
return $contact->getDisplayName();
2015-03-16 22:45:25 +01:00
}
}
/**
* @return string
*/
2015-10-13 17:44:01 +02:00
public function getCityState()
{
$swap = $this->country && $this->country->swap_postal_code;
2017-01-30 20:40:43 +01:00
2015-10-13 17:44:01 +02:00
return Utils::cityStateZip($this->city, $this->state, $this->postal_code, $swap);
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getEntityType()
{
return ENTITY_CLIENT;
}
2016-12-25 08:57:33 +01:00
/**
* @return bool
*/
public function showMap()
{
return $this->hasAddress() && env('GOOGLE_MAPS_ENABLED') !== false;
}
/**
* @return bool
*/
public function addressesMatch()
{
$fields = [
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
];
foreach ($fields as $field) {
if ($this->$field != $this->{'shipping_' . $field}) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function hasAddress($shipping = false)
{
$fields = [
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
];
foreach ($fields as $field) {
if ($shipping) {
$field = 'shipping_' . $field;
}
if ($this->$field) {
return true;
}
}
return false;
}
/**
* @return string
*/
2015-03-16 22:45:25 +01:00
public function getDateCreated()
{
if ($this->created_at == '0000-00-00 00:00:00') {
return '---';
} else {
return $this->created_at->format('m/d/y h:i a');
}
}
/**
* @return bool
*/
2016-06-20 16:14:43 +02:00
public function getGatewayToken()
2015-03-16 22:45:25 +01:00
{
2016-06-20 16:14:43 +02:00
$accountGateway = $this->account->getGatewayByType(GATEWAY_TYPE_TOKEN);
2015-03-16 22:45:25 +01:00
2017-01-30 17:05:31 +01:00
if (! $accountGateway) {
2015-03-16 22:45:25 +01:00
return false;
}
2016-06-20 16:14:43 +02:00
return AccountGatewayToken::clientAndGateway($this->id, $accountGateway->id)->first();
2015-03-16 22:45:25 +01:00
}
/**
* @return bool
*/
2016-06-26 11:59:42 +02:00
public function defaultPaymentMethod()
{
if ($token = $this->getGatewayToken()) {
return $token->default_payment_method;
}
return false;
}
/**
* @return bool
*/
2016-06-20 16:14:43 +02:00
public function autoBillLater()
2015-03-16 22:45:25 +01:00
{
2016-06-20 16:14:43 +02:00
if ($token = $this->getGatewayToken()) {
if ($this->account->auto_bill_on_due_date) {
return true;
}
2016-04-27 01:59:52 +02:00
2016-06-20 16:14:43 +02:00
return $token->autoBillLater();
2016-04-27 01:59:52 +02:00
}
2016-06-20 16:14:43 +02:00
return false;
2015-03-16 22:45:25 +01:00
}
2015-06-10 10:34:20 +02:00
/**
* @return mixed
*/
2016-02-23 22:32:39 +01:00
public function getAmount()
{
return $this->balance + $this->paid_to_date;
}
/**
* @return mixed
*/
2015-06-10 10:34:20 +02:00
public function getCurrencyId()
{
2015-07-29 21:55:12 +02:00
if ($this->currency_id) {
return $this->currency_id;
}
2017-01-30 20:40:43 +01:00
if (! $this->account) {
2015-06-10 10:34:20 +02:00
$this->load('account');
}
2015-07-29 21:55:12 +02:00
return $this->account->currency_id ?: DEFAULT_CURRENCY;
2015-06-10 10:34:20 +02:00
}
2015-10-22 20:48:12 +02:00
/**
* @return string
*/
2016-06-20 16:14:43 +02:00
public function getCurrencyCode()
{
if ($this->currency) {
return $this->currency->code;
}
2017-01-30 20:40:43 +01:00
if (! $this->account) {
2016-06-20 16:14:43 +02:00
$this->load('account');
}
return $this->account->currency ? $this->account->currency->code : 'USD';
}
2017-11-27 15:50:06 +01:00
public function getCountryCode()
{
if ($country = $this->country) {
return $country->iso_3166_2;
}
if (! $this->account) {
$this->load('account');
}
return $this->account->country ? $this->account->country->iso_3166_2 : 'US';
}
/**
* @param $isQuote
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-22 20:48:12 +02:00
public function getCounter($isQuote)
{
return $isQuote ? $this->quote_number_counter : $this->invoice_number_counter;
}
2015-10-28 20:22:07 +01:00
public function markLoggedIn()
{
$this->last_login = Carbon::now()->toDateTimeString();
$this->save();
}
/**
* @return bool
*/
2017-01-30 17:05:31 +01:00
public function hasAutoBillConfigurableInvoices()
{
return $this->invoices()->whereIsPublic(true)->whereIn('auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT])->count() > 0;
}
/**
* @return bool
*/
public function hasRecurringInvoices()
{
return $this->invoices()->whereIsPublic(true)->whereIsRecurring(true)->count() > 0;
}
2017-01-30 13:35:04 +01:00
public function defaultDaysDue()
{
return $this->payment_terms == -1 ? 0 : $this->payment_terms;
}
2017-06-21 22:36:59 +02:00
public function firstInvitationKey()
{
if ($invoice = $this->invoices->first()) {
if ($invitation = $invoice->invitations->first()) {
return $invitation->invitation_key;
}
}
}
2015-03-16 22:45:25 +01:00
}
2015-10-28 20:22:07 +01:00
Client::creating(function ($client) {
$client->setNullValues();
2017-01-04 09:11:32 +01:00
$client->account->incrementCounter($client);
2015-03-16 22:45:25 +01:00
});
2015-10-28 20:22:07 +01:00
Client::updating(function ($client) {
$client->setNullValues();
2015-03-16 22:45:25 +01:00
});