v2 first commit

This commit is contained in:
denisdulici
2019-11-16 10:21:14 +03:00
parent 5b23e9c2c4
commit 6d50fa8442
3075 changed files with 3451681 additions and 65594 deletions

View File

@ -2,19 +2,14 @@
namespace App\Models\Auth;
use EloquentFilter\Filterable;
use Laratrust\LaratrustPermission;
use Laratrust\Models\LaratrustPermission;
use Laratrust\Traits\LaratrustPermissionTrait;
use Kyslik\ColumnSortable\Sortable;
use Request;
use Route;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
class Permission extends LaratrustPermission
{
use LaratrustPermissionTrait;
use Filterable;
use Sortable;
use LaratrustPermissionTrait, SearchString, Sortable;
protected $table = 'permissions';
@ -25,31 +20,6 @@ class Permission extends LaratrustPermission
*/
protected $fillable = ['name', 'display_name', 'description'];
/**
* Define the filter provider globally.
*
* @return ModelFilter
*/
public function modelFilter()
{
// Check if is api or web
if (Request::is('api/*')) {
$arr = array_reverse(explode('\\', explode('@', app()['api.router']->currentRouteAction())[0]));
$folder = $arr[1];
$file = $arr[0];
} else {
list($folder, $file) = explode('/', Route::current()->uri());
}
if (empty($folder) || empty($file)) {
return $this->provideFilter();
}
$class = '\App\Filters\\' . ucfirst($folder) .'\\' . ucfirst($file);
return $this->provideFilter($class);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
@ -62,9 +32,9 @@ class Permission extends LaratrustPermission
{
$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 $query->filter($input)->sortable($sort)->paginate($limit);
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
}
}

View File

@ -2,19 +2,15 @@
namespace App\Models\Auth;
use EloquentFilter\Filterable;
use Laratrust\LaratrustRole;
use Laratrust\Models\LaratrustRole;
use Laratrust\Traits\LaratrustRoleTrait;
use Kyslik\ColumnSortable\Sortable;
use Request;
use Route;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
class Role extends LaratrustRole
{
use LaratrustRoleTrait;
use Filterable;
use Sortable;
use LaratrustRoleTrait, SearchString, Sortable;
protected $table = 'roles';
/**
@ -24,31 +20,6 @@ class Role extends LaratrustRole
*/
protected $fillable = ['name', 'display_name', 'description'];
/**
* Define the filter provider globally.
*
* @return ModelFilter
*/
public function modelFilter()
{
// Check if is api or web
if (Request::is('api/*')) {
$arr = array_reverse(explode('\\', explode('@', app()['api.router']->currentRouteAction())[0]));
$folder = $arr[1];
$file = $arr[0];
} else {
list($folder, $file) = explode('/', Route::current()->uri());
}
if (empty($folder) || empty($file)) {
return $this->provideFilter();
}
$class = '\App\Filters\\' . ucfirst($folder) .'\\' . ucfirst($file);
return $this->provideFilter($class);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
@ -61,9 +32,9 @@ class Role extends LaratrustRole
{
$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 $query->filter($input)->sortable($sort)->paginate($limit);
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
}
}

View File

@ -3,21 +3,19 @@
namespace App\Models\Auth;
use App\Notifications\Auth\Reset;
use App\Traits\Media;
use Date;
use EloquentFilter\Filterable;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
use Kyslik\ColumnSortable\Sortable;
use App\Traits\Media;
use Request;
use Route;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
class User extends Authenticatable
{
use Filterable, LaratrustUserTrait, Notifiable, SoftDeletes, Sortable, Media;
use LaratrustUserTrait, Notifiable, SearchString, SoftDeletes, Sortable, Media;
protected $table = 'users';
@ -54,9 +52,14 @@ class User extends Authenticatable
return $this->morphToMany('App\Models\Common\Company', 'user', 'user_companies', 'user_id', 'company_id');
}
public function customer()
public function contact()
{
return $this->hasOne('App\Models\Income\Customer', 'user_id', 'id');
return $this->hasOne('App\Models\Common\Contact', 'user_id', 'id');
}
public function dashboards()
{
return $this->hasMany('App\Models\Common\Dashboard');
}
/**
@ -73,7 +76,7 @@ class User extends Authenticatable
public function getPictureAttribute($value)
{
// Check if we should use gravatar
if (setting('general.use_gravatar', '0') == '1') {
if (setting('default.use_gravatar', '0') == '1') {
try {
// Check for gravatar
$url = 'https://www.gravatar.com/avatar/' . md5(strtolower($this->getAttribute('email'))).'?size=90&d=404';
@ -135,33 +138,6 @@ class User extends Authenticatable
$this->attributes['password'] = bcrypt($value);
}
/**
* Define the filter provider globally.
*
* @return ModelFilter
*/
public function modelFilter()
{
// Check if is api or web
if (Request::is('api/*')) {
$arr = array_reverse(explode('\\', explode('@', app()['api.router']->currentRouteAction())[0]));
$folder = $arr[1];
$file = $arr[0];
} else {
list($folder, $file) = explode('/', Route::current()->uri());
}
if (empty($folder) || empty($file)) {
return $this->provideFilter();
}
//$class = '\App\Filters\Auth\Users';
$class = '\App\Filters\\' . ucfirst($folder) . '\\' . ucfirst($file);
return $this->provideFilter($class);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
@ -174,10 +150,10 @@ class User extends Authenticatable
{
$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 $query->filter($input)->sortable($sort)->paginate($limit);
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
}
/**

View File

@ -2,13 +2,10 @@
namespace App\Models\Banking;
use App\Models\Model;
use Sofa\Eloquence\Eloquence;
use App\Abstracts\Model;
class Account extends Model
{
use Eloquence;
protected $table = 'accounts';
/**
@ -32,42 +29,24 @@ class Account extends Model
*/
public $sortable = ['name', 'number', 'opening_balance', 'enabled'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'number' => 10,
'bank_name' => 10,
'bank_phone' => 5,
'bank_address' => 2,
];
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function invoice_payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue');
}
public function bill_payments()
{
return $this->hasMany('App\Models\Expense\BillPayment');
return $this->transactions()->where('type', 'income');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment');
return $this->transactions()->where('type', 'expense');
}
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction');
}
/**
@ -92,10 +71,10 @@ class Account extends Model
$total = $this->opening_balance;
// Sum Incomes
$total += $this->invoice_payments()->sum('amount') + $this->revenues()->sum('amount');
$total += $this->revenues->sum('amount');
// Subtract Expenses
$total -= $this->bill_payments()->sum('amount') + $this->payments()->sum('amount');
$total -= $this->payments->sum('amount');
return $total;
}

View File

@ -2,13 +2,10 @@
namespace App\Models\Banking;
use App\Models\Model;
use Sofa\Eloquence\Eloquence;
use App\Abstracts\Model;
class Reconciliation extends Model
{
use Eloquence;
protected $table = 'reconciliations';
protected $dates = ['deleted_at', 'started_at', 'ended_at'];

View File

@ -2,91 +2,212 @@
namespace App\Models\Banking;
use App\Models\Expense\Bill;
use App\Models\Expense\Payment;
use App\Models\Income\Invoice;
use App\Models\Income\Revenue;
use Illuminate\Database\Eloquent\Model;
use App\Abstracts\Model;
use App\Models\Setting\Category;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Date;
class Transaction extends Model
{
public static function getUserTransactions($user_id, $type)
use Cloneable, Currencies, DateTime, Media, Recurring;
protected $table = 'transactions';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'type', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'document_id', 'contact_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['paid_at', 'amount','category.name', 'account.name'];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['recurring'];
public function account()
{
$transactions = array();
return $this->belongsTo('App\Models\Banking\Account')->withDefault(['name' => trans('general.na')]);
}
switch ($type) {
case 'payments':
$bills = Bill::where('vendor_id', $user_id)->get();
public function bill()
{
return $this->belongsTo('App\Models\Expense\Bill', 'document_id');
}
foreach ($bills as $bill) {
$bill_payments = $bill->payments;
public function category()
{
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
}
if ($bill_payments) {
foreach ($bill_payments as $bill_payment) {
$transactions[] = (object) [
'date' => $bill_payment->paid_at,
'account' => $bill_payment->account->name,
'type' => trans('invoices.status.partial'),
'category' => trans_choice('general.invoices', 1),
'description' => $bill_payment->description,
'amount' => $bill_payment->amount,
'currency_code' => $bill_payment->currency_code,
];
}
}
}
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
}
$payments = Payment::where('vendor_id', $user_id)->get();
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
foreach ($payments as $payment) {
$transactions[] = (object) [
'date' => $payment->paid_at,
'account' => $payment->account->name,
'type' => 'Expense',
'category' => $payment->category->name,
'description' => $payment->description,
'amount' => $payment->amount,
'currency_code' => $payment->currency_code,
];
}
break;
case 'revenues':
$invoices = Invoice::where('customer_id', $user_id)->get();
public function invoice()
{
return $this->belongsTo('App\Models\Income\Invoice', 'document_id');
}
foreach ($invoices as $invoice) {
$invoice_payments = $invoice->payments;
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
if ($invoice_payments) {
foreach ($invoice_payments as $invoice_payment) {
$transactions[] = (object) [
'date' => $invoice_payment->paid_at,
'account' => $invoice_payment->account->name,
'type' => trans('invoices.status.partial'),
'category' => trans_choice('general.invoices', 1),
'description' => $invoice_payment->description,
'amount' => $invoice_payment->amount,
'currency_code' => $invoice_payment->currency_code,
];
}
}
}
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
$revenues = Revenue::where('customer_id', $user_id)->get();
public function user()
{
return $this->belongsTo('App\Models\Auth\User', 'contact_id', 'id');
}
foreach ($revenues as $revenue) {
$transactions[] = (object) [
'date' => $revenue->paid_at,
'account' => $revenue->account->name,
'type' => trans_choice('general.payments', 1),
'category' => $revenue->category->name,
'description' => $revenue->description,
'amount' => $revenue->amount,
'currency_code' => $revenue->currency_code,
];
}
break;
/**
* 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 $transactions;
return $query->whereIn('type', (array) $types);
}
/**
* Get only transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsTransfer($query)
{
return $query->where('category_id', '=', Category::transfer());
}
/**
* Skip transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotTransfer($query)
{
return $query->where('category_id', '<>', Category::transfer());
}
/**
* Get only documents (invoice/bill).
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsDocument($query)
{
return $query->where('document_id', '<>', '');
}
/**
* Get by document (invoice/bill).
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $document_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDocument($query, $document_id)
{
return $query->where('document_id', '=', $document_id);
}
/**
* Order by paid date.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Scope paid invoice.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePaid($query)
{
return $query->sum('amount');
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Banking;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
class Transfer extends Model
@ -16,47 +16,32 @@ class Transfer extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'payment_id', 'revenue_id'];
protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['payment.paid_at', 'payment.amount', 'payment.name', 'revenue.name'];
public $sortable = ['expense.paid_at', 'expense.amount', 'expense.name', 'income.name'];
public function payment()
public function expense_transaction()
{
return $this->belongsTo('App\Models\Expense\Payment');
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id');
}
public function paymentAccount()
public function expense_account()
{
return $this->belongsTo('App\Models\Banking\Account', 'payment.account_id', 'id');
return $this->belongsTo('App\Models\Banking\Account', 'expense_transaction.account_id', 'id');
}
public function revenue()
public function income_transaction()
{
return $this->belongsTo('App\Models\Income\Revenue');
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id');
}
public function revenueAccount()
public function income_account()
{
return $this->belongsTo('App\Models\Banking\Account', 'revenue.account_id', 'id');
}
public function getDynamicConvertedAmount($format = false)
{
return $this->dynamicConvert($this->default_currency_code, $this->amount, $this->currency_code, $this->currency_rate, $format);
}
public function getReverseConvertedAmount($format = false)
{
return $this->reverseConvert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
return $this->belongsTo('App\Models\Banking\Account', 'income_transaction.account_id', 'id');
}
}

View File

@ -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.*');
}

View 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;
}
}

View 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();
}
}

View 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;
}
}

View 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);
}
}

View File

@ -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
*

View File

@ -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'];
}

View File

@ -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

View 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'];
}

View 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);
}
}

View File

@ -1,8 +0,0 @@
<?php
namespace App\Models\Company;
/**
* @deprecated since 1.2.7 version. use Common\Company instead.
*/
class Company extends \App\Models\Common\Company {}

View File

@ -2,19 +2,19 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
use App\Models\Banking\Transaction;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Date;
class Bill extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
use Cloneable, Currencies, DateTime, Media, Recurring;
protected $table = 'bills';
@ -32,29 +32,14 @@ class Bill extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'category_id', 'parent_id'];
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'contact_id', 'contact_name', 'contact_email', 'contact_tax_number', 'contact_phone', 'contact_address', 'notes', 'category_id', 'parent_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['bill_number', 'vendor_name', 'amount', 'status.name', 'billed_at', 'due_at', 'bill_status_code'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'bill_number' => 10,
'order_number' => 10,
'vendor_name' => 10,
'vendor_email' => 5,
'vendor_phone' => 2,
'vendor_address' => 1,
'notes' => 2,
];
public $sortable = ['bill_number', 'contact_name', 'amount', 'status.name', 'billed_at', 'due_at', 'bill_status_code'];
/**
* Clonable relationships.
@ -65,7 +50,12 @@ class Bill extends Model
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
}
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
}
public function currency()
@ -88,11 +78,6 @@ class Bill extends Model
return $this->hasMany('App\Models\Expense\BillItemTax');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\BillPayment');
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
@ -108,9 +93,9 @@ class Bill extends Model
return $this->hasMany('App\Models\Expense\BillTotal');
}
public function vendor()
public function transactions()
{
return $this->belongsTo('App\Models\Expense\Vendor');
return $this->hasMany('App\Models\Banking\Transaction', 'document_id');
}
public function scopeDue($query, $date)
@ -120,7 +105,7 @@ class Bill extends Model
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
return $query->orderBy('billed_at', 'desc');
}
public function scopeAccrued($query)
@ -238,7 +223,7 @@ class Bill extends Model
if ($this->currency_code == $item->currency_code) {
$amount = (double) $item->amount;
} else {
$default_model = new BillPayment();
$default_model = new Transaction();
$default_model->default_currency_code = $this->currency_code;
$default_model->amount = $item->amount;
$default_model->currency_code = $item->currency_code;
@ -246,13 +231,13 @@ class Bill extends Model
$default_amount = (double) $default_model->getDivideConvertedAmount();
$convert_model = new BillPayment();
$convert_model = new Transaction();
$convert_model->default_currency_code = $item->currency_code;
$convert_model->amount = $default_amount;
$convert_model->currency_code = $this->currency_code;
$convert_model->currency_rate = $currencies[$this->currency_code];
$amount = (double) $convert_model->getDynamicConvertedAmount();
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
}
$paid += $amount;

View File

@ -2,7 +2,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
class BillHistory extends Model
@ -24,28 +24,8 @@ class BillHistory extends Model
return $this->belongsTo('App\Models\Expense\Bill');
}
public function item()
{
return $this->belongsTo('App\Models\Common\Item');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
}
public function payment()
{
return $this->belongsTo('App\Models\Setting\Payment');
}
public function status()
{
return $this->belongsTo('App\Models\Expense\BillStatus', 'status_code', 'code');
}
public function getConvertedAmount($format = false)
{
return $this->convert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

View File

@ -2,13 +2,14 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
use Bkwld\Cloner\Cloneable;
class BillItem extends Model
{
use Currencies;
use Cloneable, Currencies;
protected $table = 'bill_items';
@ -17,7 +18,14 @@ class BillItem extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax'];
protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'price', 'total', 'tax'];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['taxes'];
public function bill()
{

View File

@ -2,7 +2,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
class BillItemTax extends Model

View File

@ -1,109 +0,0 @@
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use Date;
class BillPayment extends Model
{
use Currencies, DateTime, Media;
protected $table = 'bill_payments';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function bill()
{
return $this->belongsTo('App\Models\Expense\Bill');
}
public function item()
{
return $this->belongsTo('App\Models\Common\Item');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Scope paid invoice.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePaid($query)
{
return $query->sum('amount');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
class BillStatus extends Model
{
@ -32,17 +32,17 @@ class BillStatus extends Model
{
switch ($this->code) {
case 'paid':
$label = 'label-success';
$label = 'success';
break;
case 'delete':
$label = 'label-danger';
$label = 'danger';
break;
case 'partial':
case 'received':
$label = 'label-warning';
$label = 'warning';
break;
default:
$label = 'bg-aqua';
$label = 'info';
break;
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Abstracts\Model;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
@ -53,6 +53,8 @@ class BillTotal extends Model
$percent = 0;
$tax = null;
switch ($this->code) {
case 'discount':
$title = trans($title);
@ -60,22 +62,23 @@ class BillTotal extends Model
break;
case 'tax':
$rate = Tax::where('name', $title)->value('rate');
$tax = Tax::where('name', $title)->first();
if (!empty($rate)) {
$percent = $rate;
if (!empty($tax->rate)) {
$percent = $tax->rate;
}
break;
}
if (!empty($percent)) {
$title .= ' (';
if (setting('general.percent_position', 'after') == 'after') {
$title .= $percent . '%';
if (setting('localisation.percent_position', 'after') == 'after') {
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : $percent . '%';
} else {
$title .= '%' . $percent;
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : '%' . $percent;
}
$title .= ')';

View File

@ -1,150 +0,0 @@
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Models\Setting\Category;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Date;
class Payment extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
protected $table = 'payments';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['paid_at', 'amount', 'category.name', 'account.name'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'accounts.name',
'categories.name',
'vendors.name' ,
'description' ,
];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['recurring'];
public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
public function vendor()
{
return $this->belongsTo('App\Models\Expense\Vendor');
}
/**
* Get only transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsTransfer($query)
{
return $query->where('category_id', '=', Category::transfer());
}
/**
* Skip transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotTransfer($query)
{
return $query->where('category_id', '<>', Category::transfer());
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
public static function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -1,89 +0,0 @@
<?php
namespace App\Models\Expense;
use App\Models\Model;
use Bkwld\Cloner\Cloneable;
use App\Traits\Currencies;
use Sofa\Eloquence\Eloquence;
use App\Traits\Media;
class Vendor extends Model
{
use Cloneable, Currencies, Eloquence, Media;
protected $table = 'vendors';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'email', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'email', 'phone', 'enabled'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'email' => 5,
'phone' => 2,
'website' => 2,
'address' => 1,
];
public function bills()
{
return $this->hasMany('App\Models\Expense\Bill');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
/**
* 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;
$bills = $this->bills()->accrued()->notPaid()->get();
foreach ($bills as $bill) {
$bill_amount = $bill->amount - $bill->paid;
$amount += $this->dynamicConvert(setting('general.default_currency'), $bill_amount, $bill->currency_code, $bill->currency_rate, false);
}
return $amount;
}
}

View File

@ -1,83 +0,0 @@
<?php
namespace App\Models\Income;
use App\Models\Model;
use Bkwld\Cloner\Cloneable;
use App\Traits\Currencies;
use Illuminate\Notifications\Notifiable;
use Sofa\Eloquence\Eloquence;
class Customer extends Model
{
use Cloneable, Currencies, Eloquence, Notifiable;
protected $table = 'customers';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'user_id', 'name', 'email', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'email', 'phone', 'enabled'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'email' => 5,
'phone' => 2,
'website' => 2,
'address' => 1,
];
public function invoices()
{
return $this->hasMany('App\Models\Income\Invoice');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function user()
{
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
}
public function onCloning($src, $child = null)
{
$this->user_id = null;
}
public function getUnpaidAttribute()
{
$amount = 0;
$invoices = $this->invoices()->accrued()->notPaid()->get();
foreach ($invoices as $invoice) {
$invoice_amount = $invoice->amount - $invoice->paid;
$amount += $this->dynamicConvert(setting('general.default_currency'), $invoice_amount, $invoice->currency_code, $invoice->currency_rate, false);
}
return $amount;
}
}

View File

@ -2,7 +2,8 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
use App\Models\Banking\Transaction;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
use App\Traits\DateTime;
@ -10,12 +11,11 @@ use App\Traits\Incomes;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Date;
class Invoice extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Incomes, Media, Recurring;
use Cloneable, Currencies, DateTime, Incomes, Media, Recurring;
protected $table = 'invoices';
@ -33,29 +33,14 @@ class Invoice extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes', 'category_id', 'parent_id'];
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'contact_id', 'contact_name', 'contact_email', 'contact_tax_number', 'contact_phone', 'contact_address', 'notes', 'category_id', 'parent_id', 'footer'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['invoice_number', 'customer_name', 'amount', 'status' , 'invoiced_at', 'due_at', 'invoice_status_code'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'invoice_number' => 10,
'order_number' => 10,
'customer_name' => 10,
'customer_email' => 5,
'customer_phone' => 2,
'customer_address' => 1,
'notes' => 2,
];
public $sortable = ['invoice_number', 'contact_name', 'amount', 'status' , 'invoiced_at', 'due_at', 'invoice_status_code'];
protected $reconciled_amount = [];
@ -68,7 +53,12 @@ class Invoice extends Model
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
}
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
}
public function currency()
@ -76,11 +66,6 @@ class Invoice extends Model
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function customer()
{
return $this->belongsTo('App\Models\Income\Customer');
}
public function items()
{
return $this->hasMany('App\Models\Income\InvoiceItem');
@ -98,7 +83,7 @@ class Invoice extends Model
public function payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment');
return $this->transactions();
}
public function recurring()
@ -116,6 +101,11 @@ class Invoice extends Model
return $this->hasMany('App\Models\Income\InvoiceTotal');
}
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction', 'document_id');
}
public function scopeDue($query, $date)
{
return $query->whereDate('due_at', '=', $date);
@ -123,7 +113,7 @@ class Invoice extends Model
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
return $query->orderBy('invoiced_at', 'desc');
}
public function scopeAccrued($query)
@ -242,7 +232,7 @@ class Invoice extends Model
if ($this->currency_code == $item->currency_code) {
$amount = (double) $item->amount;
} else {
$default_model = new InvoicePayment();
$default_model = new Transaction();
$default_model->default_currency_code = $this->currency_code;
$default_model->amount = $item->amount;
$default_model->currency_code = $item->currency_code;
@ -250,13 +240,13 @@ class Invoice extends Model
$default_amount = (double) $default_model->getDivideConvertedAmount();
$convert_model = new InvoicePayment();
$convert_model = new Transaction();
$convert_model->default_currency_code = $item->currency_code;
$convert_model->amount = $default_amount;
$convert_model->currency_code = $this->currency_code;
$convert_model->currency_rate = $currencies[$this->currency_code];
$amount = (double) $convert_model->getDynamicConvertedAmount();
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
}
$paid += $amount;

View File

@ -2,7 +2,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
class InvoiceHistory extends Model
@ -24,28 +24,8 @@ class InvoiceHistory extends Model
return $this->belongsTo('App\Models\Income\Invoice');
}
public function item()
{
return $this->belongsTo('App\Models\Common\Item');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
}
public function payment()
{
return $this->belongsTo('App\Models\Setting\Payment');
}
public function status()
{
return $this->belongsTo('App\Models\Income\InvoiceStatus', 'status_code', 'code');
}
public function getConvertedAmount($format = false)
{
return $this->convert($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

View File

@ -2,13 +2,14 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
use Bkwld\Cloner\Cloneable;
class InvoiceItem extends Model
{
use Currencies;
use Cloneable, Currencies;
protected $table = 'invoice_items';
@ -17,7 +18,14 @@ class InvoiceItem extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax'];
protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax'];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['taxes'];
public function invoice()
{

View File

@ -2,7 +2,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
use App\Traits\Currencies;
class InvoiceItemTax extends Model

View File

@ -1,109 +0,0 @@
<?php
namespace App\Models\Income;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use Date;
class InvoicePayment extends Model
{
use Currencies, DateTime, Media;
protected $table = 'invoice_payments';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function invoice()
{
return $this->belongsTo('App\Models\Income\Invoice');
}
public function item()
{
return $this->belongsTo('App\Models\Common\Item');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Scope paid invoice.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePaid($query)
{
return $query->sum('amount');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
public function getDivideConvertedAmount($format = false)
{
return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
}
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
class InvoiceStatus extends Model
{
@ -32,17 +32,17 @@ class InvoiceStatus extends Model
{
switch ($this->code) {
case 'paid':
$label = 'label-success';
$label = 'success';
break;
case 'delete':
$label = 'label-danger';
$label = 'danger';
break;
case 'partial':
case 'sent':
$label = 'label-warning';
$label = 'warning';
break;
default:
$label = 'bg-aqua';
$label = 'info';
break;
}

View File

@ -2,7 +2,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Abstracts\Model;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
@ -34,12 +34,12 @@ class InvoiceTotal extends Model
/**
* Convert amount to double.
*
* @param string $value
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
$this->attributes['amount'] = (double)$value;
}
/**
@ -53,6 +53,8 @@ class InvoiceTotal extends Model
$percent = 0;
$tax = null;
switch ($this->code) {
case 'discount':
$title = trans($title);
@ -60,22 +62,23 @@ class InvoiceTotal extends Model
break;
case 'tax':
$rate = Tax::where('name', $title)->value('rate');
$tax = Tax::where('name', $title)->first();
if (!empty($rate)) {
$percent = $rate;
if (!empty($tax->rate)) {
$percent = $tax->rate;
}
break;
}
if (!empty($percent)) {
$title .= ' (';
if (setting('general.percent_position', 'after') == 'after') {
$title .= $percent . '%';
if (setting('localisation.percent_position', 'after') == 'after') {
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : $percent . '%';
} else {
$title .= '%' . $percent;
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : '%' . $percent;
}
$title .= ')';

View File

@ -1,156 +0,0 @@
<?php
namespace App\Models\Income;
use App\Models\Model;
use App\Models\Setting\Category;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Date;
class Revenue extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
protected $table = 'revenues';
protected $dates = ['deleted_at', 'paid_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['paid_at', 'amount','category_id', 'account', 'payment_method'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'invoice_number' => 10,
'order_number' => 10,
'customer_name' => 10,
'customer_email' => 5,
'notes' => 2,
];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['recurring'];
public function user()
{
return $this->belongsTo('App\Models\Auth\User', 'customer_id', 'id');
}
public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function customer()
{
return $this->belongsTo('App\Models\Income\Customer');
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
/**
* Get only transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsTransfer($query)
{
return $query->where('category_id', '=', Category::transfer());
}
/**
* Skip transfers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsNotTransfer($query)
{
return $query->where('category_id', '<>', Category::transfer());
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -1,8 +0,0 @@
<?php
namespace App\Models\Item;
/**
* @deprecated since 1.2.7 version. use Common\Item instead.
*/
class Item extends \App\Models\Common\Item {}

View File

@ -1,157 +0,0 @@
<?php
namespace App\Models;
use App\Scopes\Company;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use Request;
use Route;
class Model extends Eloquent
{
use Filterable, SoftDeletes, Sortable;
protected $dates = ['deleted_at'];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new Company);
}
/**
* Global company relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->belongsTo('App\Models\Common\Company');
}
/**
* Define the filter provider globally.
*
* @return ModelFilter
*/
public function modelFilter()
{
// Check if is api or web
if (Request::is('api/*')) {
$arr = array_reverse(explode('\\', explode('@', app()['api.router']->currentRouteAction())[0]));
$folder = $arr[1];
$file = $arr[0];
} else {
list($folder, $file) = explode('/', Route::current()->uri());
}
if (empty($folder) || empty($file)) {
return $this->provideFilter();
}
$class = '\App\Filters\\' . ucfirst($folder) . '\\' . ucfirst($file);
return $this->provideFilter($class);
}
/**
* Scope to only include company data.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $company_id
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCompanyId($query, $company_id)
{
return $query->where($this->table . '.company_id', '=', $company_id);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'name')
{
$request = request();
$input = $request->input();
$limit = $request->get('limit', setting('general.list_limit', '25'));
return $query->filter($input)->sortable($sort)->paginate($limit);
}
/**
* Scope to only include active models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query)
{
return $query->where('enabled', 1);
}
/**
* Scope to only include passive models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDisabled($query)
{
return $query->where('enabled', 0);
}
/**
* Scope to only include reconciled models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeReconciled($query, $value = 1)
{
return $query->where('reconciled', $value);
}
public function scopeAccount($query, $accounts)
{
if (empty($accounts)) {
return;
}
return $query->whereIn('account_id', (array) $accounts);
}
public function scopeCustomer($query, $customers)
{
if (empty($customers)) {
return;
}
return $query->whereIn('customer_id', (array) $customers);
}
public function scopeVendor($query, $vendors)
{
if (empty($vendors)) {
return;
}
return $query->whereIn('vendor_id', (array) $vendors);
}
}

View File

@ -2,13 +2,10 @@
namespace App\Models\Module;
use App\Models\Model;
use Sofa\Eloquence\Eloquence;
use App\Abstracts\Model;
class Module extends Model
{
use Eloquence;
protected $table = 'modules';
/**
@ -16,7 +13,7 @@ class Module extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'alias', 'status'];
protected $fillable = ['company_id', 'alias', 'enabled'];
/**
* Scope alias.

View File

@ -2,7 +2,7 @@
namespace App\Models\Module;
use App\Models\Model;
use App\Abstracts\Model;
class ModuleHistory extends Model
{

View File

@ -2,7 +2,7 @@
namespace App\Models\Setting;
use App\Models\Model;
use App\Abstracts\Model;
class Category extends Model
{
@ -39,24 +39,33 @@ class Category extends Model
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment');
return $this->transactions()->where('type', 'expense');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue');
return $this->transactions()->where('type', 'income');
}
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction');
}
/**
* Scope to only include categories of a given type.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $type
* @param mixed $types
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeType($query, $type)
public function scopeType($query, $types)
{
return $query->whereIn('type', (array) $type);
if (empty($types)) {
return $query;
}
return $query->whereIn('type', (array) $types);
}
/**

View File

@ -2,7 +2,7 @@
namespace App\Models\Setting;
use App\Models\Model;
use App\Abstracts\Model;
class Currency extends Model
{
@ -28,9 +28,19 @@ class Currency extends Model
return $this->hasMany('App\Models\Banking\Account', 'currency_code', 'code');
}
public function bills()
{
return $this->hasMany('App\Models\Expense\Bill', 'currency_code', 'code');
}
public function contacts()
{
return $this->hasMany('App\Models\Common\Contact', 'currency_code', 'code');
}
public function customers()
{
return $this->hasMany('App\Models\Income\Customer', 'currency_code', 'code');
return $this->contacts()->where('type', 'customer');
}
public function invoices()
@ -38,29 +48,24 @@ class Currency extends Model
return $this->hasMany('App\Models\Income\Invoice', 'currency_code', 'code');
}
public function invoice_payments()
public function payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment', 'currency_code', 'code');
return $this->transactions()->where('type', 'expense');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue', 'currency_code', 'code');
return $this->transactions()->where('type', 'income');
}
public function bills()
public function transactions()
{
return $this->hasMany('App\Models\Expense\Bill', 'currency_code', 'code');
return $this->hasMany('App\Models\Banking\Transaction', 'currency_code', 'code');
}
public function bill_payments()
public function vendors()
{
return $this->hasMany('App\Models\Expense\BillPayment', 'currency_code', 'code');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment', 'currency_code', 'code');
return $this->contacts()->where('type', 'vendor');
}
/**

View File

@ -3,9 +3,9 @@
namespace App\Models\Setting;
use App\Scopes\Company;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Setting extends Model
class Setting extends Eloquent
{
protected $table = 'settings';

View File

@ -2,7 +2,7 @@
namespace App\Models\Setting;
use App\Models\Model;
use App\Abstracts\Model;
class Tax extends Model
{
@ -65,12 +65,11 @@ class Tax extends Model
{
$title = $this->name . ' (';
if (setting('general.percent_position', 'after') == 'after') {
$title .= $this->rate . '%';
if (setting('localisation.percent_position', 'after') == 'after') {
$title .= $this->getAttribute('type') == 'fixed' ? $this->rate : $this->rate . '%';
} else {
$title .= '%' . $this->rate;
$title .= $this->getAttribute('type') == 'fixed' ? $this->rate : '%' . $this->rate;
}
$title .= ')';
return $title;