v2 first commit
This commit is contained in:
@ -2,16 +2,15 @@
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use Auth;
|
||||
use EloquentFilter\Filterable;
|
||||
use App\Traits\Media;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Kyslik\ColumnSortable\Sortable;
|
||||
use App\Traits\Media;
|
||||
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
|
||||
|
||||
class Company extends Eloquent
|
||||
{
|
||||
use Filterable, SoftDeletes, Sortable, Media;
|
||||
use Media, SearchString, SoftDeletes, Sortable;
|
||||
|
||||
protected $table = 'companies';
|
||||
|
||||
@ -26,6 +25,19 @@ class Company extends Eloquent
|
||||
*/
|
||||
public $sortable = ['name', 'domain', 'email', 'enabled', 'created_at'];
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::retrieved(function($model) {
|
||||
$model->setSettings();
|
||||
});
|
||||
|
||||
static::saving(function($model) {
|
||||
$model->unsetSettings();
|
||||
});
|
||||
}
|
||||
|
||||
public function accounts()
|
||||
{
|
||||
return $this->hasMany('App\Models\Banking\Account');
|
||||
@ -41,11 +53,6 @@ class Company extends Eloquent
|
||||
return $this->hasMany('App\Models\Expense\BillItem');
|
||||
}
|
||||
|
||||
public function bill_payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\BillPayment');
|
||||
}
|
||||
|
||||
public function bill_statuses()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\BillStatus');
|
||||
@ -61,6 +68,11 @@ class Company extends Eloquent
|
||||
return $this->hasMany('App\Models\Setting\Category');
|
||||
}
|
||||
|
||||
public function contacts()
|
||||
{
|
||||
return $this->hasMany('App\Models\Common\Contact');
|
||||
}
|
||||
|
||||
public function currencies()
|
||||
{
|
||||
return $this->hasMany('App\Models\Setting\Currency');
|
||||
@ -68,7 +80,12 @@ class Company extends Eloquent
|
||||
|
||||
public function customers()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\Customer');
|
||||
return $this->contacts()->where('type', 'customer');
|
||||
}
|
||||
|
||||
public function dashboards()
|
||||
{
|
||||
return $this->hasMany('App\Models\Common\Dashboard');
|
||||
}
|
||||
|
||||
public function invoice_histories()
|
||||
@ -81,11 +98,6 @@ class Company extends Eloquent
|
||||
return $this->hasMany('App\Models\Income\InvoiceItem');
|
||||
}
|
||||
|
||||
public function invoice_payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\InvoicePayment');
|
||||
}
|
||||
|
||||
public function invoice_statuses()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\InvoiceStatus');
|
||||
@ -103,7 +115,7 @@ class Company extends Eloquent
|
||||
|
||||
public function payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\Payment');
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
}
|
||||
|
||||
public function recurring()
|
||||
@ -113,7 +125,7 @@ class Company extends Eloquent
|
||||
|
||||
public function revenues()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\Revenue');
|
||||
return $this->transactions()->where('type', 'income');
|
||||
}
|
||||
|
||||
public function settings()
|
||||
@ -126,6 +138,11 @@ class Company extends Eloquent
|
||||
return $this->hasMany('App\Models\Setting\Tax');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany('App\Models\Banking\Transaction');
|
||||
}
|
||||
|
||||
public function transfers()
|
||||
{
|
||||
return $this->hasMany('App\Models\Banking\Transfer');
|
||||
@ -138,24 +155,29 @@ class Company extends Eloquent
|
||||
|
||||
public function vendors()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\Vendor');
|
||||
return $this->contacts()->where('type', 'vendor');
|
||||
}
|
||||
|
||||
public function setSettings()
|
||||
{
|
||||
$settings = $this->settings;
|
||||
|
||||
$groups = [
|
||||
'company',
|
||||
'default'
|
||||
];
|
||||
|
||||
foreach ($settings as $setting) {
|
||||
list($group, $key) = explode('.', $setting->getAttribute('key'));
|
||||
|
||||
// Load only general settings
|
||||
if ($group != 'general') {
|
||||
if (!in_array($group, $groups)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $setting->getAttribute('value');
|
||||
|
||||
if (($key == 'company_logo') && empty($value)) {
|
||||
if (($key == 'logo') && empty($value)) {
|
||||
$value = 'public/img/company.png';
|
||||
}
|
||||
|
||||
@ -163,27 +185,32 @@ class Company extends Eloquent
|
||||
}
|
||||
|
||||
// Set default default company logo if empty
|
||||
if ($this->getAttribute('company_logo') == '') {
|
||||
$this->setAttribute('company_logo', 'public/img/company.png');
|
||||
if ($this->getAttribute('logo') == '') {
|
||||
$this->setAttribute('logo', 'public/img/company.png');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the filter provider globally.
|
||||
*
|
||||
* @return ModelFilter
|
||||
*/
|
||||
public function modelFilter()
|
||||
public function unsetSettings()
|
||||
{
|
||||
list($folder, $file) = explode('/', \Route::current()->uri());
|
||||
$settings = $this->settings;
|
||||
|
||||
if (empty($folder) || empty($file)) {
|
||||
return $this->provideFilter();
|
||||
$groups = [
|
||||
'company',
|
||||
'default'
|
||||
];
|
||||
|
||||
foreach ($settings as $setting) {
|
||||
list($group, $key) = explode('.', $setting->getAttribute('key'));
|
||||
|
||||
// Load only general settings
|
||||
if (!in_array($group, $groups)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->offsetUnset($key);
|
||||
}
|
||||
|
||||
$class = '\App\Filters\\' . ucfirst($folder) .'\\' . ucfirst($file);
|
||||
|
||||
return $this->provideFilter($class);
|
||||
$this->offsetUnset('logo');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,10 +225,10 @@ class Company extends Eloquent
|
||||
{
|
||||
$request = request();
|
||||
|
||||
$input = $request->input();
|
||||
$limit = $request->get('limit', setting('general.list_limit', '25'));
|
||||
$search = $request->get('search');
|
||||
$limit = $request->get('limit', setting('default.list_limit', '25'));
|
||||
|
||||
return Auth::user()->companies()->filter($input)->sortable($sort)->paginate($limit);
|
||||
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,7 +254,7 @@ class Company extends Eloquent
|
||||
public function nameSortable($query, $direction)
|
||||
{
|
||||
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
|
||||
->where('key', 'general.company_name')
|
||||
->where('key', 'company.name')
|
||||
->orderBy('value', $direction)
|
||||
->select('companies.*');
|
||||
}
|
||||
|
117
app/Models/Common/Contact.php
Normal file
117
app/Models/Common/Contact.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\Media;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class Contact extends Model
|
||||
{
|
||||
use Cloneable, Currencies, Media, Notifiable;
|
||||
|
||||
protected $table = 'contacts';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'type', 'name', 'email', 'user_id', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sortable = ['name', 'email', 'phone', 'enabled'];
|
||||
|
||||
public function bills()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\Bill');
|
||||
}
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
||||
}
|
||||
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\Invoice');
|
||||
}
|
||||
|
||||
public function payments()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
}
|
||||
|
||||
public function revenues()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany('App\Models\Banking\Transaction');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to only include contacts of a given type.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param mixed $types
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeType($query, $types)
|
||||
{
|
||||
if (empty($types)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->whereIn('type', (array) $types);
|
||||
}
|
||||
|
||||
public function onCloning($src, $child = null)
|
||||
{
|
||||
$this->user_id = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current balance.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLogoAttribute($value)
|
||||
{
|
||||
if (!empty($value) && !$this->hasMedia('logo')) {
|
||||
return $value;
|
||||
} elseif (!$this->hasMedia('logo')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getMedia('logo')->last();
|
||||
}
|
||||
|
||||
public function getUnpaidAttribute()
|
||||
{
|
||||
$amount = 0;
|
||||
|
||||
$collection = ($this->type == 'customer') ? 'invoices' : 'bills';
|
||||
|
||||
$this->$collection()->accrued()->notPaid()->each(function ($item) use (&$amount) {
|
||||
$unpaid = $item->amount - $item->paid;
|
||||
|
||||
$amount += $this->convertFromDefault($unpaid, $item->currency_code, $item->currency_rate, false);
|
||||
});
|
||||
|
||||
return $amount;
|
||||
}
|
||||
}
|
42
app/Models/Common/Dashboard.php
Normal file
42
app/Models/Common/Dashboard.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
|
||||
class Dashboard extends Model
|
||||
{
|
||||
use Cloneable;
|
||||
|
||||
protected $table = 'dashboards';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'user_id', 'name', 'enabled'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sortable = ['name', 'enabled'];
|
||||
|
||||
public function widgets()
|
||||
{
|
||||
return $this->hasMany('App\Models\Common\DashboardWidget')->orderBy('sort', 'asc');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
|
||||
}
|
||||
|
||||
public function scopeGetByUser($query, $user_id)
|
||||
{
|
||||
return $query->where('user_id', $user_id)->get();
|
||||
}
|
||||
}
|
66
app/Models/Common/DashboardWidget.php
Normal file
66
app/Models/Common/DashboardWidget.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
|
||||
class DashboardWidget extends Model
|
||||
{
|
||||
|
||||
protected $table = 'dashboard_widgets';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'user_id', 'dashboard_id', 'widget_id', 'name', 'settings', 'sort'];
|
||||
|
||||
/**
|
||||
* The attributes that should be casted to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'settings' => 'array'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
|
||||
}
|
||||
|
||||
public function dashboard()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Common\Dashboard');
|
||||
}
|
||||
|
||||
public function widget()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Common\Widget');
|
||||
}
|
||||
|
||||
public function getNameAttribute($value)
|
||||
{
|
||||
if ($value) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $this->widget->name;
|
||||
}
|
||||
|
||||
public function getSettingsAttribute($value)
|
||||
{
|
||||
if (!empty($value)) {
|
||||
$value = json_decode($value, true);
|
||||
|
||||
$value['widget'] = $this;
|
||||
} else {
|
||||
$value = [
|
||||
'widget' => $this
|
||||
];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
30
app/Models/Common/EmailTemplate.php
Normal file
30
app/Models/Common/EmailTemplate.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
|
||||
class EmailTemplate extends Model
|
||||
{
|
||||
|
||||
protected $table = 'email_templates';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'alias', 'subject', 'body', 'params'];
|
||||
|
||||
/**
|
||||
* Scope to only include contacts of a given type.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param mixed $alias
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeAlias($query, $alias)
|
||||
{
|
||||
return $query->where('alias', $alias);
|
||||
}
|
||||
}
|
@ -2,15 +2,14 @@
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Models\Model;
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Currencies;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
use Sofa\Eloquence\Eloquence;
|
||||
use App\Traits\Media;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
|
||||
class Item extends Model
|
||||
{
|
||||
use Cloneable, Currencies, Eloquence, Media;
|
||||
use Cloneable, Currencies, Media;
|
||||
|
||||
protected $table = 'items';
|
||||
|
||||
@ -26,25 +25,14 @@ class Item extends Model
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'name', 'sku', 'description', 'sale_price', 'purchase_price', 'quantity', 'category_id', 'tax_id', 'enabled'];
|
||||
protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'tax_id', 'enabled'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $sortable = ['name', 'category', 'quantity', 'sale_price', 'purchase_price', 'enabled'];
|
||||
|
||||
/**
|
||||
* Searchable rules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $searchableColumns = [
|
||||
'name' => 10,
|
||||
'sku' => 5,
|
||||
'description' => 2,
|
||||
];
|
||||
protected $sortable = ['name', 'category', 'sale_price', 'purchase_price', 'enabled'];
|
||||
|
||||
public function category()
|
||||
{
|
||||
@ -114,17 +102,6 @@ class Item extends Model
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope quantity.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeQuantity($query)
|
||||
{
|
||||
return $query->where('quantity', '>', '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort by category name
|
||||
*
|
||||
|
@ -3,12 +3,11 @@
|
||||
namespace App\Models\Common;
|
||||
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Plank\Mediable\Media as PMedia;
|
||||
use Plank\Mediable\Media as BaseMedia;
|
||||
|
||||
class Media extends PMedia
|
||||
class Media extends BaseMedia
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Models\Model;
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Recurring as RecurringTrait;
|
||||
|
||||
class Recurring extends Model
|
||||
|
18
app/Models/Common/Report.php
Normal file
18
app/Models/Common/Report.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
|
||||
class Report extends Model
|
||||
{
|
||||
|
||||
protected $table = 'reports';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'name', 'description', 'class', 'group', 'period', 'basis', 'chart', 'enabled'];
|
||||
}
|
46
app/Models/Common/Widget.php
Normal file
46
app/Models/Common/Widget.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
|
||||
class Widget extends Model
|
||||
{
|
||||
use Cloneable;
|
||||
|
||||
protected $table = 'widgets';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'name', 'alias', 'settings', 'enabled'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sortable = ['name', 'alias', 'enabled'];
|
||||
|
||||
/**
|
||||
* The attributes that should be casted to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'settings' => 'array'
|
||||
];
|
||||
|
||||
public function dashboard_widgets()
|
||||
{
|
||||
return $this->hasMany('App\Models\Common\DashboardWidget');
|
||||
}
|
||||
|
||||
public function dashboard_widget()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Common\DashboardWidget', 'id', 'widget_id')->where('dashboard_id', 1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user