1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Models/Client.php

515 lines
11 KiB
PHP
Raw Normal View History

2015-03-18 00:39:03 +01:00
<?php namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-10-13 17:44:01 +02:00
use Utils;
2015-03-31 20:50:58 +02:00
use DB;
2015-10-28 20:22:07 +01:00
use Carbon;
2015-11-12 21:36:28 +01:00
use Laracasts\Presenter\PresentableTrait;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 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',
];
/**
* @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';
2015-11-24 20:45:38 +01:00
/**
* @return array
*/
2015-11-24 20:45:38 +01:00
public static function getImportColumns()
{
return [
Client::$fieldName,
Client::$fieldPhone,
Client::$fieldAddress1,
Client::$fieldAddress2,
Client::$fieldCity,
Client::$fieldState,
Client::$fieldPostalCode,
Client::$fieldCountry,
Client::$fieldNotes,
Client::$fieldWebsite,
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',
'street2|address2' => 'address2',
'street|address|address1' => 'address1',
'city' => 'city',
'state|province' => 'state',
'zip|postal|code' => 'postal_code',
'country' => 'country',
'note' => 'notes',
'site|website' => 'website',
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
*/
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()
{
return $this->hasMany('App\Models\Expense','client_id','id')->withTrashed();
}
2016-02-03 04:21:13 +01:00
/**
* @param $data
* @param bool $isPrimary
* @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-06-20 16:14:43 +02:00
if (Utils::hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD) && $this->account->enable_portal_password){
2016-02-29 22:46:27 +01:00
if(!empty($data['password']) && $data['password']!='-%unchanged%-'){
$contact->password = bcrypt($data['password']);
} else if(empty($data['password'])){
$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)
{
if ($balanceAdjustment === 0 && $paidToDateAdjustment === 0) {
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
2015-10-28 20:22:07 +01:00
if ( ! count($this->contacts)) {
return '';
}
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
$contact = $this->contacts[0];
return $contact->getDisplayName() ?: trans('texts.unnamed_client');
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;
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;
}
/**
* @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
2016-06-20 16:14:43 +02: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;
}
2015-06-10 10:34:20 +02:00
if (!$this->account) {
$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;
}
if (!$this->account) {
$this->load('account');
}
return $this->account->currency ? $this->account->currency->code : 'USD';
}
/**
* @param $isQuote
* @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
*/
2016-05-09 22:29:02 +02:00
public function hasAutoBillConfigurableInvoices(){
return $this->invoices()->whereIn('auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT])->count() > 0;
}
2015-03-16 22:45:25 +01:00
}
2015-10-28 20:22:07 +01:00
Client::creating(function ($client) {
$client->setNullValues();
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
});