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

571 lines
12 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;
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;
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',
2015-10-28 20:22:07 +01:00
];
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldName = 'name';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldPhone = 'work_phone';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldAddress1 = 'address1';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldAddress2 = 'address2';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldCity = 'city';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldState = 'state';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldPostalCode = 'postal_code';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldNotes = 'notes';
/**
* @var string
*/
2015-11-24 20:45:38 +01:00
public static $fieldCountry = 'country';
/**
* @var string
*/
public static $fieldWebsite = 'website';
/**
* @var string
*/
public static $fieldVatNumber = 'vat_number';
/**
* @var string
*/
public static $fieldIdNumber = 'id_number';
2015-11-24 20:45:38 +01:00
/**
* @return array
*/
2015-11-24 20:45:38 +01:00
public static function getImportColumns()
{
return [
2017-01-30 20:40:43 +01:00
self::$fieldName,
self::$fieldPhone,
self::$fieldAddress1,
self::$fieldAddress2,
self::$fieldCity,
self::$fieldState,
self::$fieldPostalCode,
self::$fieldCountry,
self::$fieldNotes,
self::$fieldWebsite,
self::$fieldVatNumber,
self::$fieldIdNumber,
2015-11-24 20:45:38 +01:00
Contact::$fieldFirstName,
Contact::$fieldLastName,
Contact::$fieldPhone,
Contact::$fieldEmail,
];
}
/**
* @return array
*/
2015-11-24 20:45:38 +01:00
public static function getImportMap()
{
return [
2015-11-25 10:35:24 +01:00
'first' => 'first_name',
'last' => 'last_name',
'email' => 'email',
'mobile|phone' => 'phone',
'name|organization' => 'name',
2017-03-27 22:05:18 +02:00
'apt|street2|address2' => 'address2',
2015-11-25 10:35:24 +01:00
'street|address|address1' => 'address1',
'city' => 'city',
'state|province' => 'state',
'zip|postal|code' => 'postal_code',
'country' => 'country',
'note' => 'notes',
'site|website' => 'website',
'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
}
/**
* @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
if ($publicId && $publicId != '-1') {
$contact = Contact::scope($publicId)->firstOrFail();
} 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;
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()
{
return $this->contacts()
->whereIsPrimary(true)
->first();
}
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;
}
2016-06-20 16:14:43 +02:00
2017-01-30 17:05:31 +01:00
if (! count($this->contacts)) {
2015-10-28 20:22:07 +01:00
return '';
}
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
$contact = $this->contacts[0];
2016-10-31 15:08:00 +01:00
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 hasAddress()
{
$fields = [
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
];
foreach ($fields as $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';
}
/**
* @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;
}
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
});