2018-10-15 14:40:34 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Models\Currency;
|
2019-04-04 03:38:17 +02:00
|
|
|
use App\Models\Filterable;
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2018-10-15 14:40:34 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-04-04 03:38:17 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
class Invoice extends BaseModel
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2019-04-04 03:38:17 +02:00
|
|
|
use SoftDeletes;
|
|
|
|
use Filterable;
|
2019-05-02 07:47:16 +02:00
|
|
|
use NumberFormatter;
|
|
|
|
use MakesDates;
|
2019-04-04 03:38:17 +02:00
|
|
|
|
2019-05-16 00:26:21 +02:00
|
|
|
protected $fillable = [
|
|
|
|
'invoice_number',
|
|
|
|
'discount',
|
|
|
|
'po_number',
|
|
|
|
'invoice_date',
|
|
|
|
'due_date',
|
|
|
|
'terms',
|
|
|
|
'public_notes',
|
|
|
|
'private_notes',
|
|
|
|
'invoice_type_id',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_rate1',
|
|
|
|
'tax_name2',
|
|
|
|
'tax_rate2',
|
|
|
|
'is_amount_discount',
|
|
|
|
'invoice_footer',
|
|
|
|
'partial',
|
|
|
|
'partial_due_date',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_taxes1',
|
|
|
|
'custom_taxes2',
|
|
|
|
'custom_text_value1',
|
|
|
|
'custom_text_value2',
|
|
|
|
'line_items',
|
|
|
|
'settings',
|
|
|
|
'client_id',
|
|
|
|
];
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2019-04-17 08:20:32 +02:00
|
|
|
protected $casts = [
|
2019-05-14 06:05:05 +02:00
|
|
|
'settings' => 'object',
|
|
|
|
'line_items' => 'object'
|
2019-04-17 08:20:32 +02:00
|
|
|
];
|
|
|
|
|
2019-04-25 09:16:41 +02:00
|
|
|
protected $with = [
|
2019-05-02 07:47:16 +02:00
|
|
|
'company',
|
|
|
|
'client',
|
2019-04-25 09:16:41 +02:00
|
|
|
];
|
|
|
|
|
2019-04-04 01:30:49 +02:00
|
|
|
const STATUS_DRAFT = 1;
|
2019-04-28 12:25:18 +02:00
|
|
|
const STATUS_SENT = 2;
|
2019-05-02 07:47:16 +02:00
|
|
|
const STATUS_PARTIAL = 3;
|
|
|
|
const STATUS_PAID = 4;
|
|
|
|
const STATUS_CANCELLED = 5;
|
2019-04-23 06:16:41 +02:00
|
|
|
|
2019-04-04 01:30:49 +02:00
|
|
|
const STATUS_OVERDUE = -1;
|
|
|
|
const STATUS_UNPAID = -2;
|
2019-05-02 07:47:16 +02:00
|
|
|
const STATUS_REVERSED = -3;
|
2019-04-04 01:30:49 +02:00
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
public function company()
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-04-04 01:17:15 +02:00
|
|
|
return $this->belongsTo(Company::class);
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
public function user()
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-04-04 01:17:15 +02:00
|
|
|
return $this->belongsTo(User::class);
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
2019-04-04 01:30:49 +02:00
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
public function invitations()
|
|
|
|
{
|
2019-04-24 02:22:02 +02:00
|
|
|
return $this->hasMany(InvoiceInvitation::class);
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
public function client()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Client::class);
|
|
|
|
}
|
2019-04-28 07:31:32 +02:00
|
|
|
|
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
2019-05-02 07:47:16 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->morphToMany(Payment::class, 'paymentable');
|
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:07 +02:00
|
|
|
public function company_ledger()
|
|
|
|
{
|
|
|
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
/* ---------------- */
|
|
|
|
/* Settings getters */
|
|
|
|
/* ---------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If True, prevents an invoice from being
|
|
|
|
* modified once it has been marked as sent
|
|
|
|
*
|
|
|
|
* @return boolean isLocked
|
|
|
|
*/
|
|
|
|
public function isLocked() : bool
|
|
|
|
{
|
|
|
|
return $this->client->getMergedSettings()->lock_sent_invoices;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the currency from the settings object.
|
|
|
|
*
|
|
|
|
* @return Eloquent Model The currency.
|
|
|
|
*/
|
|
|
|
public function getCurrency()
|
|
|
|
{
|
|
|
|
return Currency::find($this->settings->currency_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if invoice overdue.
|
|
|
|
*
|
|
|
|
* @param float $balance The balance
|
|
|
|
* @param date. $due_date The due date
|
|
|
|
*
|
|
|
|
* @return boolean True if overdue, False otherwise.
|
|
|
|
*/
|
|
|
|
public static function isOverdue($balance, $due_date)
|
|
|
|
{
|
|
|
|
if (! $this->formatValue($balance,2) > 0 || ! $due_date) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it isn't considered overdue until the end of the day
|
|
|
|
return strtotime($this->createClientDate(date(), $this->client->timezone()->name)) > (strtotime($due_date) + (60 * 60 * 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|