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

307 lines
7.1 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;
use App\Events\ClientWasCreated;
use App\Events\ClientWasUpdated;
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;
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
protected $presenter = 'App\Ninja\Presenters\ClientPresenter';
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
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',
];
2015-11-24 20:45:38 +01:00
public static $fieldName = 'name';
public static $fieldPhone = 'work_phone';
public static $fieldAddress1 = 'address1';
public static $fieldAddress2 = 'address2';
public static $fieldCity = 'city';
public static $fieldState = 'state';
public static $fieldPostalCode = 'postal_code';
public static $fieldNotes = 'notes';
public static $fieldCountry = 'country';
public static function getImportColumns()
{
return [
Client::$fieldName,
Client::$fieldPhone,
Client::$fieldAddress1,
Client::$fieldAddress2,
Client::$fieldCity,
Client::$fieldState,
Client::$fieldPostalCode,
Client::$fieldCountry,
Client::$fieldNotes,
Contact::$fieldFirstName,
Contact::$fieldLastName,
Contact::$fieldPhone,
Contact::$fieldEmail,
];
}
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',
2015-11-24 20:45:38 +01:00
];
}
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
}
2015-10-22 20:48:12 +02:00
public function user()
{
return $this->belongsTo('App\Models\User');
}
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
}
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
}
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
}
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
}
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
}
public function language()
{
return $this->belongsTo('App\Models\Language');
}
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
}
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
}
2015-10-28 20:22:07 +01:00
public function addContact($data, $isPrimary = false)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if ($publicId && $publicId != '-1') {
$contact = Contact::scope($publicId)->firstOrFail();
} else {
$contact = Contact::createNew();
$contact->send_invoice = true;
}
$contact->fill($data);
$contact->is_primary = $isPrimary;
return $this->contacts()->save($contact);
}
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;
$this->save();
}
public function getRoute()
{
return "/clients/{$this->public_id}";
}
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');
}
public function getName()
{
2015-07-02 22:21:29 +02:00
return $this->name;
2015-03-16 22:45:25 +01:00
}
2015-07-29 21:55:12 +02:00
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->name) {
return $this->name;
}
2015-07-29 21:55:12 +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];
2015-03-16 22:45:25 +01:00
return $contact->getDisplayName();
}
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);
}
2015-03-16 22:45:25 +01:00
public function getEntityType()
{
return ENTITY_CLIENT;
}
public function hasAddress()
{
$fields = [
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
];
foreach ($fields as $field) {
if ($this->$field) {
return true;
}
}
return false;
}
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');
}
}
public function getGatewayToken()
{
$this->account->load('account_gateways');
if (!count($this->account->account_gateways)) {
return false;
}
$accountGateway = $this->account->getGatewayConfig(GATEWAY_STRIPE);
if (!$accountGateway) {
return false;
}
$token = AccountGatewayToken::where('client_id', '=', $this->id)
->where('account_gateway_id', '=', $accountGateway->id)->first();
return $token ? $token->token : false;
}
public function getGatewayLink()
{
$token = $this->getGatewayToken();
return $token ? "https://dashboard.stripe.com/customers/{$token}" : false;
}
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
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();
}
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::created(function ($client) {
event(new ClientWasCreated($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
});
2015-10-28 20:22:07 +01:00
Client::updated(function ($client) {
event(new ClientWasUpdated($client));
2015-03-16 22:45:25 +01:00
});