1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Models/Invoice.php

344 lines
9.5 KiB
PHP
Raw Normal View History

<?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
*/
namespace App\Models;
2019-09-05 01:52:49 +02:00
use App\Events\Invoice\InvoiceWasUpdated;
2019-09-04 07:10:10 +02:00
use App\Helpers\Invoice\InvoiceCalc;
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-10-02 00:44:13 +02:00
use App\Models\PaymentTerm;
use App\Utils\Number;
2019-05-02 07:47:16 +02:00
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesInvoiceValues;
2019-05-02 07:47:16 +02:00
use App\Utils\Traits\NumberFormatter;
use Illuminate\Database\Eloquent\Model;
2019-04-04 03:38:17 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2019-05-29 13:15:42 +02:00
use Illuminate\Support\Carbon;
2019-09-04 03:45:53 +02:00
use Illuminate\Support\Facades\File;
2019-08-14 04:16:09 +02:00
use Illuminate\Support\Facades\Log;
2019-09-04 03:45:53 +02:00
use Illuminate\Support\Facades\Storage;
2019-08-29 06:07:04 +02:00
use Laracasts\Presenter\PresentableTrait;
class Invoice extends BaseModel
{
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-08-29 06:07:04 +02:00
use PresentableTrait;
use MakesInvoiceValues;
2019-09-05 07:04:52 +02:00
2019-08-29 06:07:04 +02:00
protected $presenter = 'App\Models\Presenters\InvoicePresenter';
2019-04-04 03:38:17 +02:00
2019-07-29 05:59:28 +02:00
protected $hidden = [
'id',
2019-07-30 00:28:38 +02:00
'private_notes',
'user_id',
'client_id',
'company_id',
'backup',
'settings',
2019-07-29 05:59:28 +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_value3',
'custom_value4',
2019-05-16 00:26:21 +02:00
'line_items',
'settings',
'client_id',
'footer',
2019-05-16 00:26:21 +02:00
];
protected $casts = [
'settings' => 'object',
'line_items' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
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-08-14 04:16:09 +02:00
protected $appends = [
'hashed_id',
'status'
];
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-08-14 04:16:09 +02:00
public function getStatusAttribute()
{
if($this->status_id == Invoice::STATUS_SENT && $this->due_date > Carbon::now())
return Invoice::STATUS_UNPAID;
else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date > Carbon::now())
return Invoice::STATUS_UNPAID;
else if($this->status_id == Invoice::STATUS_SENT && $this->due_date < Carbon::now())
return Invoice::STATUS_OVERDUE;
else if($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date < Carbon::now())
return Invoice::STATUS_OVERDUE;
else
return $this->status_id;
}
2019-04-04 01:17:15 +02:00
public function company()
{
2019-04-04 01:17:15 +02:00
return $this->belongsTo(Company::class);
}
2019-04-04 01:17:15 +02:00
public function user()
{
2019-04-04 01:17:15 +02:00
return $this->belongsTo(User::class);
}
2019-04-04 01:30:49 +02:00
public function invitations()
{
2019-04-24 02:22:02 +02:00
return $this->hasMany(InvoiceInvitation::class);
}
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
{
2019-09-11 07:32:47 +02:00
return $this->client->getSetting('lock_sent_invoices');
2019-05-02 07:47:16 +02:00
}
/**
* 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));
}
2019-05-29 13:15:42 +02:00
public function markViewed()
{
$this->last_viewed = Carbon::now()->format('Y-m-d H:i');
}
2019-05-02 07:47:16 +02:00
public function isPayable()
{
return ($this->status === Invoice::STATUS_UNPAID || $this->status === Invoice::STATUS_OVERDUE);
}
2019-08-14 04:16:09 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
case Invoice::STATUS_DRAFT:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-light">'.ctrans('texts.draft').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_SENT:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_PARTIAL:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-primary">'.ctrans('texts.partial').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_PAID:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-success">'.ctrans('texts.paid').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_CANCELLED:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-secondary">'.ctrans('texts.cancelled').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_OVERDUE:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-danger">'.ctrans('texts.overdue').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_UNPAID:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-warning">'.ctrans('texts.unpaid').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case Invoice::STATUS_REVERSED:
2019-08-20 00:29:19 +02:00
return '<h5><span class="badge badge-info">'.ctrans('texts.reversed').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
default:
# code...
break;
}
}
/**
* Returns the template for the invoice
*
2019-09-04 03:45:53 +02:00
* @return string Either the template view, OR the template HTML string
2019-09-05 01:52:49 +02:00
* @todo this needs attention, invoice->settings needs clarification
*/
public function design() :string
{
2019-09-04 03:45:53 +02:00
return File::exists(resource_path($this->settings->design)) ? File::get(resource_path($this->settings->design)) : File::get(resource_path('views/pdf/design1.blade.php'));
}
2019-09-04 07:10:10 +02:00
/**
* Access the invoice calculator object
*
* @return object The invoice calculator object getters
*/
public function calc()
{
2019-09-11 07:32:47 +02:00
$invoice_calc = new InvoiceCalc($this, $this->settings);
2019-09-04 07:10:10 +02:00
return $invoice_calc->build();
}
2019-09-05 01:52:49 +02:00
2019-10-01 03:56:48 +02:00
/** TODO// DOCUMENT THIS FUNCTIONALITY */
2019-09-05 01:52:49 +02:00
public function pdf_url()
{
$public_path = 'storage/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
$storage_path = 'public/' . $this->client->client_hash . '/invoices/'. $this->invoice_number . '.pdf';
if(!Storage::exists($storage_path)) {
event(new InvoiceWasUpdated($this));
}
return $public_path;
}
2019-10-01 03:56:48 +02:00
/**
* @param bool $save
*/
public function updatePaidStatus($paid = false, $save = true) : bool
{
$status_id = false;
if ($paid && $this->balance == 0) {
$status_id = self::STATUS_PAID;
} elseif ($paid && $this->balance > 0 && $this->balance < $this->amount) {
$status_id = self::STATUS_PARTIAL;
} elseif ($this->isPartial() && $this->balance > 0) {
$status_id = ($this->balance == $this->amount ? self::STATUS_SENT : self::STATUS_PARTIAL);
}
if ($status_id && $status_id != $this->status_id) {
$this->status_id = $status_id;
if ($save) {
$this->save();
}
}
}
/**
* @return bool
*/
public function isPartial() : bool
{
return ($this->partial && $this->partial > 0) === true;
//return $this->status_id >= self::STATUS_PARTIAL;
}
2019-10-02 00:44:13 +02:00
/**
* Clear partial fields
* @return void
*/
public function clearPartial() : void
{
$this->partial = null;
$this->partial_due_date = null;
$this->save();
}
2019-10-01 03:56:48 +02:00
/**
* @param float $balance_adjustment
*/
public function updateBalance($balance_adjustment)
{
2019-10-01 11:59:32 +02:00
\Log::error('updating invoice balance');
2019-10-01 03:56:48 +02:00
if ($this->is_deleted)
return;
$balance_adjustment = floatval($balance_adjustment);
$this->balance = $this->balance + $balance_adjustment;
if($this->balance == 0)
$this->status_id = self::STATUS_PAID;
$this->save();
2019-10-01 11:59:32 +02:00
\Log::error('finished updatingoice balance');
2019-10-01 03:56:48 +02:00
}
2019-10-02 00:44:13 +02:00
public function setDueDate()
{
$this->due_date = Carbon::now()->addDays(PaymentTerm::find($this->company->settings->payment_terms_id)->num_days);
$this->save();
}
2019-08-14 04:16:09 +02:00
}