2015-03-18 00:39:03 +01:00
|
|
|
<?php namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-31 20:50:58 +02:00
|
|
|
use DB;
|
|
|
|
|
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-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public static $fieldName = 'Client - Name';
|
|
|
|
public static $fieldPhone = 'Client - Phone';
|
|
|
|
public static $fieldAddress1 = 'Client - Street';
|
|
|
|
public static $fieldAddress2 = 'Client - Apt/Floor';
|
|
|
|
public static $fieldCity = 'Client - City';
|
|
|
|
public static $fieldState = 'Client - State';
|
|
|
|
public static $fieldPostalCode = 'Client - Postal Code';
|
|
|
|
public static $fieldNotes = 'Client - Notes';
|
|
|
|
public static $fieldCountry = 'Client - Country';
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 projects()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->hasMany('App\Models\Project');
|
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 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
|
|
|
}
|
|
|
|
|
|
|
|
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-03-16 22:45:25 +01:00
|
|
|
$contact = $this->contacts()->first();
|
|
|
|
|
|
|
|
return $contact->getDisplayName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_CLIENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWebsite()
|
|
|
|
{
|
|
|
|
if (!$this->website) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$link = $this->website;
|
|
|
|
$title = $this->website;
|
|
|
|
$prefix = 'http://';
|
|
|
|
|
|
|
|
if (strlen($link) > 7 && substr($link, 0, 7) === $prefix) {
|
|
|
|
$title = substr($title, 7);
|
|
|
|
} else {
|
|
|
|
$link = $prefix.$link;
|
|
|
|
}
|
|
|
|
|
|
|
|
return link_to($link, $title, array('target' => '_blank'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Client::created(function($client)
|
|
|
|
{
|
|
|
|
Activity::createClient($client);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
Client::updating(function ($client) {
|
|
|
|
Activity::updateClient($client);
|
|
|
|
});
|
|
|
|
|
|
|
|
Client::deleting(function ($client) {
|
|
|
|
Activity::archiveClient($client);
|
|
|
|
});
|
|
|
|
|
2015-03-26 07:24:02 +01:00
|
|
|
/*Client::restoring(function ($client) {
|
2015-03-16 22:45:25 +01:00
|
|
|
Activity::restoreClient($client);
|
|
|
|
});
|
2015-03-26 07:24:02 +01:00
|
|
|
*/
|