akaunting/app/Models/Sale/Invoice.php

269 lines
6.9 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Models\Sale;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
use App\Models\Banking\Transaction;
2018-10-27 17:57:40 +03:00
use App\Models\Setting\Currency;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2018-04-27 16:01:31 +03:00
use App\Traits\Media;
use App\Traits\Recurring;
2019-12-31 16:03:20 +03:00
use App\Traits\Sales;
2017-11-26 15:20:17 +03:00
use Bkwld\Cloner\Cloneable;
2018-07-11 14:30:25 +03:00
use Date;
2017-09-14 22:21:00 +03:00
class Invoice extends Model
{
2019-12-31 16:03:20 +03:00
use Cloneable, Currencies, DateTime, Media, Recurring, Sales;
2017-09-14 22:21:00 +03:00
protected $table = 'invoices';
2017-12-28 17:20:16 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
2018-12-22 14:59:58 +03:00
protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid'];
2017-12-28 17:20:16 +03:00
2017-09-14 22:21:00 +03:00
protected $dates = ['deleted_at', 'invoiced_at', 'due_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2019-11-16 10:21:14 +03:00
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'];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
2019-11-16 10:21:14 +03:00
public $sortable = ['invoice_number', 'contact_name', 'amount', 'status' , 'invoiced_at', 'due_at', 'invoice_status_code'];
2017-09-14 22:21:00 +03:00
2018-10-27 17:57:40 +03:00
protected $reconciled_amount = [];
2017-11-26 15:20:17 +03:00
/**
* Clonable relationships.
*
* @var array
*/
2018-05-01 19:00:33 +03:00
public $cloneable_relations = ['items', 'recurring', 'totals'];
2017-11-26 15:20:17 +03:00
2018-04-23 22:17:20 +03:00
public function category()
{
2019-11-16 10:21:14 +03:00
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
2018-04-23 22:17:20 +03:00
}
2019-11-16 10:21:14 +03:00
public function contact()
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function currency()
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
2017-09-14 22:21:00 +03:00
}
public function items()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceItem');
2017-09-14 22:21:00 +03:00
}
2019-01-07 18:38:34 +03:00
public function item_taxes()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceItemTax');
}
2018-04-26 02:17:55 +03:00
public function histories()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceHistory');
}
2017-09-14 22:21:00 +03:00
public function payments()
{
2019-11-16 10:21:14 +03:00
return $this->transactions();
2017-09-14 22:21:00 +03:00
}
2018-04-26 18:40:04 +03:00
public function recurring()
2017-09-14 22:21:00 +03:00
{
2018-04-27 17:42:45 +03:00
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
2018-04-26 02:17:55 +03:00
}
public function status()
{
2019-12-31 15:49:09 +03:00
return $this->belongsTo('App\Models\Sale\InvoiceStatus', 'invoice_status_code', 'code');
2018-04-26 02:17:55 +03:00
}
public function totals()
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\InvoiceTotal');
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction', 'document_id');
}
2017-11-07 05:11:03 +03:00
public function scopeDue($query, $date)
{
2018-12-05 14:50:36 +03:00
return $query->whereDate('due_at', '=', $date);
2017-11-07 05:11:03 +03:00
}
public function scopeLatest($query)
{
2019-11-16 10:21:14 +03:00
return $query->orderBy('invoiced_at', 'desc');
2017-11-07 05:11:03 +03:00
}
public function scopeAccrued($query)
{
2018-03-09 16:24:00 +03:00
return $query->where('invoice_status_code', '<>', 'draft');
2017-11-07 05:11:03 +03:00
}
2018-03-09 22:00:17 +03:00
public function scopePaid($query)
{
return $query->where('invoice_status_code', '=', 'paid');
}
public function scopeNotPaid($query)
{
return $query->where('invoice_status_code', '<>', 'paid');
}
2017-11-26 15:20:17 +03:00
public function onCloning($src, $child = null)
{
$this->invoice_status_code = 'draft';
2017-11-26 15:20:17 +03:00
$this->invoice_number = $this->getNextInvoiceNumber();
}
2017-10-04 01:25:03 +03:00
/**
2017-10-21 14:23:57 +03:00
* Convert amount to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
/**
2017-10-21 14:23:57 +03:00
* Convert currency rate to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
2017-10-21 14:23:57 +03:00
$this->attributes['currency_rate'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
2017-12-28 17:20:16 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
2017-12-28 17:20:16 +03:00
{
2018-01-10 18:21:12 +03:00
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
2018-01-10 18:21:12 +03:00
} elseif (!$this->hasMedia('attachment')) {
2017-12-28 17:20:16 +03:00
return false;
}
2017-12-29 19:56:56 +03:00
return $this->getMedia('attachment')->last();
2017-12-28 17:20:16 +03:00
}
2018-04-17 16:40:52 +03:00
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountAttribute()
{
2018-04-18 14:34:33 +03:00
$percent = 0;
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
$discount = $this->totals()->where('code', 'discount')->value('amount');
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
if ($discount) {
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
2018-04-17 16:40:52 +03:00
2018-04-18 14:34:33 +03:00
$percent = number_format((($discount * 100) / $sub_total), 0);
2018-04-17 16:40:52 +03:00
}
2018-04-18 14:34:33 +03:00
return $percent;
2018-04-17 16:40:52 +03:00
}
2018-10-27 17:57:40 +03:00
2018-12-22 14:59:58 +03:00
/**
* Get the amount without tax.
*
* @return string
*/
public function getAmountWithoutTaxAttribute()
{
$amount = $this->amount;
$this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) {
$amount -= $tax->amount;
});
return $amount;
}
2018-10-27 17:57:40 +03:00
/**
* Get the paid amount.
*
* @return string
*/
public function getPaidAttribute()
{
2018-11-06 17:55:31 +03:00
if (empty($this->amount)) {
return false;
}
2018-10-27 17:57:40 +03:00
$paid = 0;
$reconciled = $reconciled_amount = 0;
if ($this->transactions->count()) {
2018-10-27 17:57:40 +03:00
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
foreach ($this->transactions as $item) {
2018-10-27 17:57:40 +03:00
if ($this->currency_code == $item->currency_code) {
$amount = (double) $item->amount;
} else {
2019-11-16 10:21:14 +03:00
$default_model = new Transaction();
2018-10-27 17:57:40 +03:00
$default_model->default_currency_code = $this->currency_code;
$default_model->amount = $item->amount;
$default_model->currency_code = $item->currency_code;
$default_model->currency_rate = $currencies[$item->currency_code];
$default_amount = (double) $default_model->getDivideConvertedAmount();
2019-11-16 10:21:14 +03:00
$convert_model = new Transaction();
2018-10-27 17:57:40 +03:00
$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];
2019-11-16 10:21:14 +03:00
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
2018-10-27 17:57:40 +03:00
}
$paid += $amount;
if ($item->reconciled) {
$reconciled_amount = +$amount;
}
}
}
if ($this->amount == $reconciled_amount) {
$reconciled = 1;
}
$this->setAttribute('reconciled', $reconciled);
return $paid;
}
2017-09-14 22:21:00 +03:00
}