1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Models/Invoice.php

626 lines
17 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Models;
use App\Events\Invoice\InvoiceReminderWasEmailed;
use App\Events\Invoice\InvoiceWasEmailed;
2019-09-05 01:52:49 +02:00
use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
2020-10-26 05:06:58 +01:00
use App\Jobs\Entity\CreateEntityPdf;
use App\Models\Expense;
2020-10-28 11:10:49 +01:00
use App\Models\Presenters\InvoicePresenter;
use App\Models\Task;
use App\Services\Invoice\InvoiceService;
use App\Services\Ledger\LedgerService;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2020-10-26 05:06:58 +01:00
use App\Utils\Traits\Invoice\ActionsInvoice;
2019-05-02 07:47:16 +02:00
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceValues;
use App\Utils\Traits\MakesReminders;
2019-05-02 07:47:16 +02:00
use App\Utils\Traits\NumberFormatter;
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\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;
use MakesReminders;
use ActionsInvoice;
2020-10-28 11:10:49 +01:00
protected $presenter = InvoicePresenter::class;
2019-04-04 03:38:17 +02:00
2020-07-23 05:55:11 +02:00
protected $touches = [];
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',
2019-07-29 05:59:28 +02:00
];
2019-05-16 00:26:21 +02:00
protected $fillable = [
'number',
2019-05-16 00:26:21 +02:00
'discount',
'po_number',
'date',
2019-05-16 00:26:21 +02:00
'due_date',
'terms',
'public_notes',
'private_notes',
'tax_name1',
'tax_rate1',
'tax_name2',
'tax_rate2',
'tax_name3',
'tax_rate3',
2019-05-16 00:26:21 +02:00
'is_amount_discount',
'partial',
'partial_due_date',
'project_id',
2019-05-16 00:26:21 +02:00
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
2019-05-16 00:26:21 +02:00
'line_items',
'client_id',
'footer',
'custom_surcharge1',
'custom_surcharge2',
'custom_surcharge3',
'custom_surcharge4',
'design_id',
2020-06-16 02:21:40 +02:00
'assigned_user_id',
'exchange_rate',
2021-03-10 01:08:58 +01:00
'subscription_id',
2021-05-21 14:08:56 +02:00
'auto_bill_enabled',
2021-07-17 12:36:51 +02:00
'uses_inclusive_taxes',
'vendor_id',
2019-05-16 00:26:21 +02:00
];
protected $casts = [
2020-08-14 14:21:46 +02:00
// 'date' => 'date:Y-m-d',
// 'due_date' => 'date:Y-m-d',
// 'partial_due_date' => 'date:Y-m-d',
'line_items' => 'object',
'backup' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
2022-01-05 05:32:07 +01:00
'is_deleted' => 'bool',
2022-02-27 07:49:49 +01:00
'is_amount_discount' => 'bool',
];
2020-08-14 14:21:46 +02:00
protected $with = [];
2019-04-25 09:16:41 +02:00
2019-08-14 04:16:09 +02:00
protected $appends = [
'hashed_id',
'status',
2019-08-14 04:16:09 +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;
2019-05-02 07:47:16 +02:00
const STATUS_PAID = 4;
2019-05-02 07:47:16 +02:00
const STATUS_CANCELLED = 5;
const STATUS_REVERSED = 6;
2019-04-23 06:16:41 +02:00
const STATUS_OVERDUE = -1; //status < 4 || < 3 && !is_deleted && !trashed() && due_date < now()
const STATUS_UNPAID = -2; //status < 4 || < 3 && !is_deleted && !trashed()
2019-04-04 01:30:49 +02:00
public function getEntityType()
{
return self::class;
}
public function getDateAttribute($value)
{
2020-08-15 00:03:29 +02:00
return $this->dateMutator($value);
}
public function getDueDateAttribute($value)
{
2020-08-15 00:03:29 +02:00
return $this->dateMutator($value);
}
public function getPartialDueDateAttribute($value)
{
2020-08-15 00:03:29 +02:00
return $this->dateMutator($value);
}
2019-04-04 01:17:15 +02:00
public function company()
{
2019-04-04 01:17:15 +02:00
return $this->belongsTo(Company::class);
}
2021-02-22 23:47:54 +01:00
public function project()
{
return $this->belongsTo(Project::class);
}
2022-02-07 10:52:11 +01:00
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
2021-02-22 23:47:54 +01:00
public function design()
{
return $this->belongsTo(Design::class);
}
2019-04-04 01:17:15 +02:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
2019-04-04 01:30:49 +02:00
2021-04-07 10:06:50 +02:00
public function recurring_invoice()
{
return $this->belongsTo(RecurringInvoice::class, 'recurring_id', 'id')->withTrashed();
2021-04-07 10:06:50 +02:00
}
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
2019-12-07 12:33:49 +01: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)->withTrashed();
}
2019-04-28 07:31:32 +02:00
2021-04-07 05:35:16 +02:00
public function subscription()
{
return $this->belongsTo(Subscription::class)->withTrashed();
}
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()
{
2020-11-25 10:21:26 +01:00
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
2019-05-13 08:18:46 +02:00
}
2019-05-15 06:47:07 +02:00
public function company_ledger()
{
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
}
public function activities()
{
2021-08-22 08:23:00 +02:00
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);
}
public function credits()
{
return $this->hasMany(Credit::class);
}
public function tasks()
{
return $this->hasMany(Task::class);
}
2022-03-14 07:17:26 +01:00
public function task()
{
return $this->hasOne(Task::class);
}
2022-03-12 00:30:55 +01:00
public function expenses()
{
return $this->hasMany(Expense::class);
}
2022-03-11 09:49:51 +01:00
public function expense()
{
2022-03-11 09:49:51 +01:00
return $this->hasOne(Expense::class);
}
/**
* Service entry points.
*/
public function service() :InvoiceService
{
return new InvoiceService($this);
}
public function ledger()
{
return new LedgerService($this);
}
2019-05-02 07:47:16 +02:00
/* ---------------- */
/* Settings getters */
/* ---------------- */
public function getStatusAttribute()
{
2021-04-21 14:27:08 +02:00
$due_date = $this->due_date ? Carbon::parse($this->due_date) : false;
$partial_due_date = $this->partial_due_Date ? Carbon::parse($this->partial_due_date) : false;
if ($this->status_id == self::STATUS_SENT && $due_date && $due_date->gt(now())) {
return self::STATUS_UNPAID;
2021-04-21 14:27:08 +02:00
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->gt(now())) {
return self::STATUS_PARTIAL;
} elseif ($this->status_id == self::STATUS_SENT && $due_date && $due_date->lt(now())) {
return self::STATUS_OVERDUE;
2021-04-21 14:27:08 +02:00
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->lt(now())) {
return self::STATUS_OVERDUE;
} else {
return $this->status_id;
}
}
public function isPayable(): bool
{
if ($this->status_id == self::STATUS_DRAFT && $this->is_deleted == false) {
2020-07-07 14:33:11 +02:00
return true;
} elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) {
2019-10-08 04:03:40 +02:00
return true;
} elseif ($this->status_id == self::STATUS_PARTIAL && $this->is_deleted == false) {
2019-10-08 04:03:40 +02:00
return true;
} elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) {
2019-10-08 04:03:40 +02:00
return true;
} elseif ($this->status_id == self::STATUS_DRAFT && $this->is_deleted == false) {
2019-10-08 04:03:40 +02:00
return true;
} else {
2019-10-08 04:03:40 +02:00
return false;
}
}
public function isRefundable(): bool
{
if ($this->is_deleted) {
return false;
}
if (($this->amount - $this->balance) == 0) {
return false;
}
return true;
}
/**
* @return bool
*/
public function isPartial(): bool
{
return $this->status_id >= self::STATUS_PARTIAL;
}
/**
* @return bool
*/
public function hasPartial(): bool
{
return ($this->partial && $this->partial > 0) === true;
}
2019-08-14 04:16:09 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
return '<h5><span class="badge badge-light">'.ctrans('texts.draft').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_SENT:
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_PARTIAL:
return '<h5><span class="badge badge-primary">'.ctrans('texts.partial').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_PAID:
return '<h5><span class="badge badge-success">'.ctrans('texts.paid').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_CANCELLED:
return '<h5><span class="badge badge-secondary">'.ctrans('texts.cancelled').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_OVERDUE:
return '<h5><span class="badge badge-danger">'.ctrans('texts.overdue').'</span></h5>';
2019-08-14 04:16:09 +02:00
break;
case self::STATUS_UNPAID:
2022-06-12 09:15:51 +02:00
return '<h5><span class="badge badge-warning text-white">'.ctrans('texts.unpaid').'</span></h5>';
2019-12-07 12:33:49 +01:00
break;
case self::STATUS_REVERSED:
return '<h5><span class="badge badge-info">'.ctrans('texts.reversed').'</span></h5>';
2019-12-07 12:33:49 +01:00
break;
2019-08-14 04:16:09 +02:00
default:
// code...
2019-08-14 04:16:09 +02:00
break;
}
}
public static function stringStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
return ctrans('texts.draft');
break;
case self::STATUS_SENT:
return ctrans('texts.sent');
break;
case self::STATUS_PARTIAL:
return ctrans('texts.partial');
break;
case self::STATUS_PAID:
return ctrans('texts.paid');
break;
case self::STATUS_CANCELLED:
return ctrans('texts.cancelled');
break;
case self::STATUS_OVERDUE:
return ctrans('texts.overdue');
break;
case self::STATUS_UNPAID:
return ctrans('texts.unpaid');
break;
case self::STATUS_REVERSED:
return ctrans('texts.reversed');
break;
default:
// code...
break;
}
}
2019-09-04 07:10:10 +02:00
/**
* Access the invoice calculator object.
2019-12-07 12:33:49 +01:00
*
* @return stdClass The invoice calculator object getters
2019-09-04 07:10:10 +02:00
*/
public function calc()
{
$invoice_calc = null;
if ($this->uses_inclusive_taxes) {
$invoice_calc = new InvoiceSumInclusive($this);
} else {
$invoice_calc = new InvoiceSum($this);
}
2019-12-07 12:33:49 +01:00
2019-09-04 07:10:10 +02:00
return $invoice_calc->build();
}
2019-09-05 01:52:49 +02:00
2021-05-16 01:41:12 +02:00
public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false)
2019-09-05 01:52:49 +02:00
{
if (! $invitation) {
if ($this->invitations()->exists()) {
2021-04-30 01:01:56 +02:00
$invitation = $this->invitations()->first();
} else {
2021-04-30 01:01:56 +02:00
$this->service()->createInvitations();
$invitation = $this->invitations()->first();
}
}
if (! $invitation) {
2021-04-30 01:01:56 +02:00
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
}
2021-04-30 01:01:56 +02:00
2021-06-12 13:50:01 +02:00
$file_path = $this->client->invoice_filepath($invitation).$this->numberFormatter().'.pdf';
$file_exists = false;
/* Flysystem throws an exception if the path is "corrupted" so lets wrap it in a try catch and return a bool 06/01/2022*/
try {
$file_exists = Storage::disk(config('filesystems.default'))->exists($file_path);
} catch (\Exception $e) {
nlog($e->getMessage());
}
if (Ninja::isHosted() && $portal && $file_exists) {
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
} elseif (Ninja::isHosted()) {
$file_path = (new CreateEntityPdf($invitation, config('filesystems.default')))->handle();
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
}
try {
$file_exists = Storage::disk('public')->exists($file_path);
} catch (\Exception $e) {
nlog($e->getMessage());
}
if ($file_exists) {
2021-05-16 01:41:12 +02:00
return Storage::disk('public')->{$type}($file_path);
}
2021-05-16 01:41:12 +02:00
$file_path = (new CreateEntityPdf($invitation))->handle();
return Storage::disk('public')->{$type}($file_path);
}
public function markInvitationsSent()
{
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
2022-04-20 03:55:33 +02:00
$invitation->load('invoice');
$invitation->sent_date = Carbon::now();
$invitation->save();
}
});
}
2020-07-28 06:29:56 +02:00
/**
* Filtering logic to determine
* whether an invoice is locked
* based on the current status of the invoice.
* @return bool [description]
2020-07-28 06:29:56 +02:00
*/
public function isLocked() :bool
{
$locked_status = $this->client->getSetting('lock_invoices');
switch ($locked_status) {
case 'off':
return false;
break;
case 'when_sent':
2021-07-28 04:29:51 +02:00
return $this->status_id == self::STATUS_SENT;
2020-07-28 06:29:56 +02:00
break;
case 'when_paid':
return $this->status_id == self::STATUS_PAID || $this->status_id == self::STATUS_PARTIAL;
break;
2020-07-28 06:29:56 +02:00
default:
return false;
break;
}
}
public function getBalanceDueAttribute()
{
return $this->balance;
}
public function getTotalAttribute()
{
return $this->calc()->getTotal();
}
2022-02-28 00:15:36 +01:00
public function getPayableAmount()
{
if ($this->partial > 0) {
2022-02-28 00:15:36 +01:00
return $this->partial;
}
2022-02-28 00:15:36 +01:00
if ($this->balance > 0) {
2022-02-28 00:15:36 +01:00
return $this->balance;
}
2022-02-28 00:15:36 +01:00
if ($this->status_id = 1) {
2022-02-28 00:15:36 +01:00
return $this->amount;
}
2022-02-28 00:15:36 +01:00
return 0;
}
public function entityEmailEvent($invitation, $reminder_template, $template)
{
switch ($reminder_template) {
case 'invoice':
2021-07-26 11:55:54 +02:00
event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
break;
case 'reminder1':
2021-07-26 11:55:54 +02:00
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER1_SENT));
break;
case 'reminder2':
2021-07-26 11:55:54 +02:00
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER2_SENT));
break;
case 'reminder3':
2021-07-26 11:55:54 +02:00
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER3_SENT));
break;
case 'reminder_endless':
2021-07-26 11:55:54 +02:00
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER_ENDLESS_SENT));
break;
default:
// code...
break;
}
}
2022-03-09 22:52:33 +01:00
public function transaction_event()
{
2022-03-10 02:17:05 +01:00
$invoice = $this->fresh();
2022-03-10 01:32:04 +01:00
2022-03-09 22:52:33 +01:00
return [
'invoice_id' => $invoice->id,
'invoice_amount' => $invoice->amount ?: 0,
'invoice_partial' => $invoice->partial ?: 0,
'invoice_balance' => $invoice->balance ?: 0,
2022-03-10 02:17:05 +01:00
'invoice_paid_to_date' => $invoice->paid_to_date ?: 0,
'invoice_status' => $invoice->status_id ?: 1,
2022-03-09 22:52:33 +01:00
];
}
2022-04-06 02:38:01 +02:00
public function expense_documents()
{
$line_items = $this->line_items;
$expense_ids = [];
foreach($line_items as $item)
{
if(property_exists($item, 'expense_id'))
{
$expense_ids[] = $item->expense_id;
}
}
return Expense::whereIn('id', $this->transformKeys($expense_ids))
->where('invoice_documents', 1)
->where('company_id', $this->company_id)
->cursor();
}
public function task_documents()
{
$line_items = $this->line_items;
$task_ids = [];
foreach($line_items as $item)
{
if(property_exists($item, 'task_id'))
{
$task_ids[] = $item->task_id;
}
}
return Task::whereIn('id', $this->transformKeys($task_ids))
->whereHas('company', function($query){
$query->where('invoice_task_documents', 1);
})
->where('company_id', $this->company_id)
->cursor();
}
2022-04-06 02:38:01 +02:00
public function translate_entity()
{
return ctrans('texts.invoice');
}
2019-12-07 12:33:49 +01:00
}