v2 first commit
This commit is contained in:
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models\Expense;
|
||||
|
||||
use App\Models\Model;
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Currencies;
|
||||
|
||||
class BillItemTax extends Model
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 .= ')';
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user