akaunting/app/Models/Sale/Invoice.php

119 lines
2.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
2020-02-11 21:55:13 +03:00
use App\Abstracts\DocumentModel;
2019-12-31 16:03:20 +03:00
use App\Traits\Sales;
2017-09-14 22:21:00 +03:00
2020-02-11 21:55:13 +03:00
class Invoice extends DocumentModel
2017-09-14 22:21:00 +03:00
{
2020-02-11 21:55:13 +03:00
use 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
*/
2020-01-11 16:57:32 +03:00
protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid', 'status_label'];
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
*/
2020-01-11 16:57:32 +03:00
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'status', '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
*/
2020-01-11 16:57:32 +03:00
public $sortable = ['invoice_number', 'contact_name', 'amount', 'status' , 'invoiced_at', 'due_at'];
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 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()
{
2020-01-16 17:08:26 +03:00
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'income');
2019-11-16 10:21:14 +03:00
}
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
}
2020-01-20 22:58:49 +03:00
public function scopeNumber($query, $number)
{
return $query->where('invoice_number', '=', $number);
}
2017-11-26 15:20:17 +03:00
public function onCloning($src, $child = null)
{
2020-01-11 16:57:32 +03:00
$this->status = 'draft';
2017-11-26 15:20:17 +03:00
$this->invoice_number = $this->getNextInvoiceNumber();
}
public function getSentAtAttribute($value)
{
$sent = $this->histories()->where('status', 'sent')->first();
return ($sent) ? $sent->created_at : null;
}
2017-09-14 22:21:00 +03:00
}