1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Models/Vendor.php

369 lines
7.7 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2016-01-06 15:23:58 +01:00
use App\Events\VendorWasCreated;
use App\Events\VendorWasDeleted;
2017-01-30 20:40:43 +01:00
use App\Events\VendorWasUpdated;
use DB;
2016-01-06 15:23:58 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
2017-01-30 20:40:43 +01:00
use Laracasts\Presenter\PresentableTrait;
use Utils;
2016-01-06 15:23:58 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class Vendor.
*/
2016-01-08 19:01:00 +01:00
class Vendor extends EntityModel
{
2016-01-06 15:23:58 +01:00
use PresentableTrait;
use SoftDeletes;
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
protected $presenter = 'App\Ninja\Presenters\VendorPresenter';
/**
* @var array
*/
2017-01-30 20:40:43 +01:00
protected $dates = ['deleted_at'];
/**
* @var array
*/
2017-01-30 20:40:43 +01:00
protected $fillable = [
2016-01-06 15:23:58 +01:00
'name',
'id_number',
'vat_number',
'work_phone',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
'private_notes',
'currency_id',
'website',
2016-01-26 21:22:33 +01:00
'transaction_name',
2016-01-06 15:23:58 +01:00
];
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldName = 'name';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldPhone = 'work_phone';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldAddress1 = 'address1';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldAddress2 = 'address2';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldCity = 'city';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldState = 'state';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldPostalCode = 'postal_code';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldNotes = 'notes';
/**
* @var string
*/
2017-01-30 20:40:43 +01:00
public static $fieldCountry = 'country';
2016-01-06 15:23:58 +01:00
/**
* @return array
*/
2016-01-06 15:23:58 +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,
2016-01-06 15:23:58 +01:00
VendorContact::$fieldFirstName,
VendorContact::$fieldLastName,
VendorContact::$fieldPhone,
VendorContact::$fieldEmail,
];
}
/**
* @return array
*/
2016-01-06 15:23:58 +01:00
public static function getImportMap()
{
return [
'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',
];
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
/**
* @return mixed
*/
2016-01-06 15:23:58 +01:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2016-01-06 15:23:58 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-01-06 15:23:58 +01:00
public function payments()
{
return $this->hasMany('App\Models\Payment');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function vendor_contacts()
2016-01-06 15:23:58 +01:00
{
return $this->hasMany('App\Models\VendorContact');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function country()
{
return $this->belongsTo('App\Models\Country');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function currency()
{
return $this->belongsTo('App\Models\Currency');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function language()
{
return $this->belongsTo('App\Models\Language');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function size()
{
return $this->belongsTo('App\Models\Size');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 15:23:58 +01:00
public function industry()
{
return $this->belongsTo('App\Models\Industry');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function expenses()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\Expense', 'vendor_id', 'id');
}
/**
* @param $data
* @param bool $isPrimary
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Database\Eloquent\Model
*/
2016-01-06 15:23:58 +01:00
public function addVendorContact($data, $isPrimary = false)
{
2016-10-18 11:32:58 +02:00
$publicId = isset($data['public_id']) ? $data['public_id'] : (isset($data['id']) ? $data['id'] : false);
2016-01-06 15:23:58 +01:00
if ($publicId && $publicId != '-1') {
$contact = VendorContact::scope($publicId)->firstOrFail();
} else {
$contact = VendorContact::createNew();
}
$contact->fill($data);
$contact->is_primary = $isPrimary;
return $this->vendor_contacts()->save($contact);
2016-01-06 15:23:58 +01:00
}
/**
* @return string
*/
2016-01-06 15:23:58 +01:00
public function getRoute()
{
return "/vendors/{$this->public_id}";
}
/**
* @return mixed
*/
2016-01-06 15:23:58 +01:00
public function getName()
{
return $this->name;
}
/**
* @return mixed
*/
2016-01-06 15:23:58 +01:00
public function getDisplayName()
{
2016-01-19 14:01:19 +01:00
return $this->getName();
2016-01-06 15:23:58 +01:00
}
/**
* @return string
*/
2016-01-06 15:23:58 +01:00
public function getCityState()
{
$swap = $this->country && $this->country->swap_postal_code;
2017-01-30 20:40:43 +01:00
2016-01-06 15:23:58 +01:00
return Utils::cityStateZip($this->city, $this->state, $this->postal_code, $swap);
}
/**
* @return string
*/
2016-01-06 15:23:58 +01:00
public function getEntityType()
{
return 'vendor';
}
2016-12-25 08:57:33 +01:00
/**
* @return bool
*/
public function showMap()
{
return $this->hasAddress() && env('GOOGLE_MAPS_ENABLED') !== false;
}
/**
* @return bool
*/
2016-01-06 15:23:58 +01:00
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
*/
2016-01-06 15:23:58 +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 mixed
*/
2016-01-06 15:23:58 +01:00
public function getCurrencyId()
{
if ($this->currency_id) {
return $this->currency_id;
}
2017-01-30 20:40:43 +01:00
if (! $this->account) {
2016-01-06 15:23:58 +01:00
$this->load('account');
}
return $this->account->currency_id ?: DEFAULT_CURRENCY;
}
2016-01-19 14:01:19 +01:00
/**
* @return float|int
*/
public function getTotalExpenses()
2016-01-19 14:01:19 +01:00
{
return DB::table('expenses')
->select('expense_currency_id', DB::raw('SUM(amount) as amount'))
->whereVendorId($this->id)
->whereIsDeleted(false)
->groupBy('expense_currency_id')
->get();
2016-01-19 14:01:19 +01:00
}
2016-01-06 15:23:58 +01:00
}
Vendor::creating(function ($vendor) {
$vendor->setNullValues();
});
Vendor::created(function ($vendor) {
event(new VendorWasCreated($vendor));
});
Vendor::updating(function ($vendor) {
$vendor->setNullValues();
});
Vendor::updated(function ($vendor) {
event(new VendorWasUpdated($vendor));
});
Vendor::deleting(function ($vendor) {
$vendor->setNullValues();
});
Vendor::deleted(function ($vendor) {
event(new VendorWasDeleted($vendor));
});