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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Events\Invoice\InvoiceWasMarkedSent;
|
|
|
|
use App\Events\Invoice\InvoiceWasPaid;
|
2019-09-05 01:52:49 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasUpdated;
|
2019-10-22 04:07:00 +02:00
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
|
|
|
use App\Helpers\Invoice\InvoiceSumInclusive;
|
2019-11-19 22:06:48 +01:00
|
|
|
use App\Jobs\Client\UpdateClientBalance;
|
2019-12-29 07:28:57 +01:00
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
2019-11-05 23:52:57 +01:00
|
|
|
use App\Jobs\Invoice\CreateInvoicePdf;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Models\CompanyLedger;
|
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;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Services\Invoice\InvoiceService;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Services\Ledger\LedgerService;
|
2019-08-30 08:16:39 +02:00
|
|
|
use App\Utils\Number;
|
2020-04-28 01:50:54 +02:00
|
|
|
use App\Utils\Traits\Archivable;
|
2019-12-16 12:34:38 +01:00
|
|
|
use App\Utils\Traits\InvoiceEmailBuilder;
|
2020-04-08 12:48:31 +02:00
|
|
|
use App\Utils\Traits\Invoice\ActionsInvoice;
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2019-09-02 07:08:26 +02:00
|
|
|
use App\Utils\Traits\MakesInvoiceValues;
|
2019-12-18 03:45:18 +01:00
|
|
|
use App\Utils\Traits\MakesReminders;
|
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;
|
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;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-08-29 06:07:04 +02:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
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
|
|
|
{
|
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;
|
2019-09-02 07:08:26 +02:00
|
|
|
use MakesInvoiceValues;
|
2019-12-16 12:34:38 +01:00
|
|
|
use InvoiceEmailBuilder;
|
2019-12-18 03:45:18 +01:00
|
|
|
use MakesReminders;
|
2020-04-08 12:48:31 +02:00
|
|
|
use ActionsInvoice;
|
2019-12-18 03:45:18 +01: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',
|
2019-07-29 05:59:28 +02:00
|
|
|
];
|
|
|
|
|
2019-05-16 00:26:21 +02:00
|
|
|
protected $fillable = [
|
2019-11-27 11:27:24 +01:00
|
|
|
'number',
|
2019-05-16 00:26:21 +02:00
|
|
|
'discount',
|
|
|
|
'po_number',
|
2019-11-27 11:27:24 +01:00
|
|
|
'date',
|
2019-05-16 00:26:21 +02:00
|
|
|
'due_date',
|
|
|
|
'terms',
|
|
|
|
'public_notes',
|
|
|
|
'private_notes',
|
|
|
|
'invoice_type_id',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_rate1',
|
|
|
|
'tax_name2',
|
|
|
|
'tax_rate2',
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_name3',
|
|
|
|
'tax_rate3',
|
2019-05-16 00:26:21 +02:00
|
|
|
'is_amount_discount',
|
2019-10-11 00:11:36 +02:00
|
|
|
'footer',
|
2019-05-16 00:26:21 +02:00
|
|
|
'partial',
|
|
|
|
'partial_due_date',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
2019-06-26 06:04:10 +02:00
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
2019-05-16 00:26:21 +02:00
|
|
|
'line_items',
|
|
|
|
'client_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'footer',
|
2020-03-05 08:14:57 +01:00
|
|
|
'custom_surcharge1',
|
|
|
|
'custom_surcharge2',
|
|
|
|
'custom_surcharge3',
|
|
|
|
'custom_surcharge4',
|
|
|
|
'custom_surcharge_tax1',
|
|
|
|
'custom_surcharge_tax2',
|
|
|
|
'custom_surcharge_tax3',
|
|
|
|
'custom_surcharge_tax4',
|
2020-03-07 23:15:11 +01:00
|
|
|
'design_id',
|
2019-05-16 00:26:21 +02:00
|
|
|
];
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2019-04-17 08:20:32 +02:00
|
|
|
protected $casts = [
|
2019-09-26 00:27:26 +02:00
|
|
|
'line_items' => 'object',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-04-17 08:20:32 +02:00
|
|
|
];
|
|
|
|
|
2019-04-25 09:16:41 +02:00
|
|
|
protected $with = [
|
2020-02-06 10:35:51 +01:00
|
|
|
// 'company',
|
|
|
|
// 'client',
|
2019-04-25 09:16:41 +02:00
|
|
|
];
|
|
|
|
|
2019-08-14 04:16:09 +02:00
|
|
|
protected $appends = [
|
|
|
|
'hashed_id',
|
|
|
|
'status'
|
|
|
|
];
|
|
|
|
|
2020-03-23 18:10:42 +01:00
|
|
|
protected $dates = [
|
|
|
|
'date',
|
|
|
|
];
|
|
|
|
|
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;
|
2020-04-08 12:48:31 +02:00
|
|
|
const STATUS_REVERSED = 6;
|
2019-04-23 06:16:41 +02:00
|
|
|
|
2020-04-08 12:48:31 +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
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return Invoice::class;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDateAttribute($value)
|
|
|
|
{
|
2020-03-29 23:29:00 +02:00
|
|
|
if (!empty($value)) {
|
2020-04-04 12:32:42 +02:00
|
|
|
return (new Carbon($value))->format('Y-m-d');
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDueDateAttribute($value)
|
|
|
|
{
|
2020-03-29 23:29:00 +02:00
|
|
|
if (!empty($value)) {
|
2020-04-04 12:32:42 +02:00
|
|
|
return (new Carbon($value))->format('Y-m-d');
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getPartialDueDateAttribute($value)
|
|
|
|
{
|
2020-03-29 23:29:00 +02:00
|
|
|
if (!empty($value)) {
|
2020-04-04 12:32:42 +02:00
|
|
|
return (new Carbon($value))->format('Y-m-d');
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
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-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
2019-04-04 01:30:49 +02:00
|
|
|
|
2019-11-05 11:16:38 +01:00
|
|
|
public function assigned_user()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
2019-11-05 11:16:38 +01:00
|
|
|
}
|
2019-12-07 12:33:49 +01: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()
|
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2019-04-24 07:18:48 +02:00
|
|
|
}
|
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-03-21 06:37:30 +01:00
|
|
|
return $this->morphToMany(Payment::class, 'paymentable')->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');
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
public function activities()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Activity::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function history()
|
|
|
|
{
|
|
|
|
$this->activities->with('backup');
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
// public function credits()
|
|
|
|
// {
|
|
|
|
// return $this->belongsToMany(Credit::class)->using(Paymentable::class)->withPivot(
|
|
|
|
// 'amount',
|
|
|
|
// 'refunded'
|
|
|
|
// )->withTimestamps();
|
|
|
|
// }
|
2020-01-03 09:49:59 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/**
|
|
|
|
* Service entry points
|
|
|
|
*/
|
2020-02-20 22:05:01 +01:00
|
|
|
public function service() :InvoiceService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return new InvoiceService($this);
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function ledger()
|
2020-02-20 22:05:01 +01:00
|
|
|
{
|
|
|
|
return new LedgerService($this);
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
/* ---------------- */
|
|
|
|
/* Settings getters */
|
|
|
|
/* ---------------- */
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
public function getStatusAttribute()
|
|
|
|
{
|
|
|
|
if ($this->status_id == Invoice::STATUS_SENT && $this->due_date > Carbon::now()) {
|
|
|
|
return Invoice::STATUS_UNPAID;
|
|
|
|
} elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date > Carbon::now()) {
|
|
|
|
return Invoice::STATUS_UNPAID;
|
|
|
|
} elseif ($this->status_id == Invoice::STATUS_SENT && $this->due_date < Carbon::now()) {
|
|
|
|
return Invoice::STATUS_OVERDUE;
|
|
|
|
} elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->partial_due_date < Carbon::now()) {
|
|
|
|
return Invoice::STATUS_OVERDUE;
|
|
|
|
} else {
|
|
|
|
return $this->status_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
/**
|
2019-12-07 12:33:49 +01:00
|
|
|
* If True, prevents an invoice from being
|
2019-05-02 07:47:16 +02:00
|
|
|
* modified once it has been marked as sent
|
2019-12-07 12:33:49 +01:00
|
|
|
*
|
2019-05-02 07:47:16 +02:00
|
|
|
* @return boolean isLocked
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isLocked(): bool
|
2019-05-02 07:47:16 +02:00
|
|
|
{
|
2019-09-11 07:32:47 +02:00
|
|
|
return $this->client->getSetting('lock_sent_invoices');
|
2019-05-02 07:47:16 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isPayable(): bool
|
2019-08-14 07:40:22 +02:00
|
|
|
{
|
2020-01-15 10:43:40 +01:00
|
|
|
if ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-01-15 10:43:40 +01:00
|
|
|
} elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-01-15 10:43:40 +01:00
|
|
|
} elseif ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-01-15 10:43:40 +01:00
|
|
|
} elseif ($this->status_id == Invoice::STATUS_DRAFT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-10-08 04:03:40 +02:00
|
|
|
return false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-08-14 07:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isRefundable(): bool
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
2020-02-15 10:01:15 +01:00
|
|
|
if ($this->is_deleted) {
|
2020-01-29 05:25:08 +01:00
|
|
|
return false;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
if (($this->amount - $this->balance) == 0) {
|
2020-01-30 05:50:45 +01:00
|
|
|
return false;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isPartial(): bool
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return $this->status_id >= self::STATUS_PARTIAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function hasPartial(): bool
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return ($this->partial && $this->partial > 0) === true;
|
|
|
|
}
|
|
|
|
|
2019-08-14 04:16:09 +02:00
|
|
|
public static function badgeForStatus(int $status)
|
|
|
|
{
|
|
|
|
switch ($status) {
|
|
|
|
case Invoice::STATUS_DRAFT:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01: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:
|
2020-02-15 10:01:15 +01:00
|
|
|
return '<h5><span class="badge badge-warning">' . ctrans('texts.unpaid') . '</span></h5>';
|
2019-12-07 12:33:49 +01:00
|
|
|
break;
|
2019-08-14 04:16:09 +02:00
|
|
|
case Invoice::STATUS_REVERSED:
|
2020-02-15 10:01:15 +01:00
|
|
|
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...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 14:47:45 +02:00
|
|
|
|
2019-12-14 06:49:48 +01:00
|
|
|
public static function stringStatus(int $status)
|
|
|
|
{
|
|
|
|
switch ($status) {
|
|
|
|
case Invoice::STATUS_DRAFT:
|
|
|
|
return ctrans('texts.draft');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_SENT:
|
|
|
|
return ctrans('texts.sent');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_PARTIAL:
|
|
|
|
return ctrans('texts.partial');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_PAID:
|
|
|
|
return ctrans('texts.paid');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_CANCELLED:
|
|
|
|
return ctrans('texts.cancelled');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_OVERDUE:
|
|
|
|
return ctrans('texts.overdue');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_UNPAID:
|
|
|
|
return ctrans('texts.unpaid');
|
|
|
|
break;
|
|
|
|
case Invoice::STATUS_REVERSED:
|
|
|
|
return ctrans('texts.reversed');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2019-09-04 07:10:10 +02:00
|
|
|
/**
|
|
|
|
* Access the invoice calculator object
|
2019-12-07 12:33:49 +01:00
|
|
|
*
|
2019-09-04 07:10:10 +02:00
|
|
|
* @return object The invoice calculator object getters
|
|
|
|
*/
|
|
|
|
public function calc()
|
|
|
|
{
|
2019-10-22 04:07:00 +02:00
|
|
|
$invoice_calc = null;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->uses_inclusive_taxes) {
|
2019-11-10 22:12:21 +01:00
|
|
|
$invoice_calc = new InvoiceSumInclusive($this);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-12-04 02:06:14 +01:00
|
|
|
$invoice_calc = new InvoiceSum($this);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
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
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
public function pdf_file_path($invitation = null)
|
2019-09-05 01:52:49 +02:00
|
|
|
{
|
2020-04-16 10:41:25 +02:00
|
|
|
if(!$invitation)
|
|
|
|
$invitation = $this->invitations->first();
|
2020-02-12 01:41:17 +01:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
$storage_path = Storage::url($this->client->invoice_filepath() . $this->number . '.pdf');
|
2019-09-05 01:52:49 +02:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
if (!Storage::exists($this->client->invoice_filepath() . $this->number . '.pdf')) {
|
2019-12-27 01:28:36 +01:00
|
|
|
event(new InvoiceWasUpdated($this, $this->company));
|
2020-04-16 10:41:25 +02:00
|
|
|
CreateInvoicePdf::dispatchNow($invitation);
|
2019-11-05 23:52:57 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 12:33:49 +01:00
|
|
|
return $storage_path;
|
2019-11-05 23:52:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 22:06:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates Invites to SENT
|
|
|
|
*
|
|
|
|
*/
|
2020-02-03 11:33:07 +01:00
|
|
|
public function markInvitationsSent()
|
2019-11-19 22:06:48 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->invitations->each(function ($invitation) {
|
|
|
|
if (!isset($invitation->sent_date)) {
|
2019-11-19 22:06:48 +01:00
|
|
|
$invitation->sent_date = Carbon::now();
|
|
|
|
$invitation->save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
/* Graveyard */
|
2020-02-03 11:33:07 +01: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));
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $save
|
|
|
|
*
|
|
|
|
* Has this been dragged from V1?
|
|
|
|
*/
|
|
|
|
// 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->hasPartial() && $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();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2019-12-07 12:33:49 +01:00
|
|
|
}
|