1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Models/RecurringInvoice.php

494 lines
15 KiB
PHP
Raw Normal View History

2019-04-23 14:22:13 +02:00
<?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
*
* @copyright Copyright (c) 2021. 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
*/
2019-04-23 14:22:13 +02:00
namespace App\Models;
2020-09-13 12:20:11 +02:00
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Models\Presenters\RecurringInvoicePresenter;
2020-09-24 02:57:44 +02:00
use App\Services\Recurring\RecurringService;
use App\Utils\Traits\MakesDates;
2019-04-23 14:22:13 +02:00
use App\Utils\Traits\MakesHash;
2020-09-14 14:13:15 +02:00
use App\Utils\Traits\Recurring\HasRecurrence;
2019-04-23 14:22:13 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2019-05-23 08:15:06 +02:00
use Illuminate\Support\Carbon;
use Laracasts\Presenter\PresentableTrait;
2019-04-23 14:22:13 +02:00
2019-05-03 00:29:04 +02:00
/**
* Class for Recurring Invoices.
*/
2019-04-23 14:22:13 +02:00
class RecurringInvoice extends BaseModel
{
use MakesHash;
use SoftDeletes;
use Filterable;
use MakesDates;
2020-09-14 14:13:15 +02:00
use HasRecurrence;
use PresentableTrait;
protected $presenter = RecurringInvoicePresenter::class;
2020-09-14 14:13:15 +02:00
2019-05-03 00:29:04 +02:00
/**
* Invoice Statuses.
2019-05-03 00:29:04 +02:00
*/
2020-09-23 02:16:19 +02:00
const STATUS_DRAFT = 1;
const STATUS_ACTIVE = 2;
const STATUS_PAUSED = 3;
const STATUS_COMPLETED = 4;
2019-05-03 09:35:49 +02:00
const STATUS_PENDING = -1;
2019-05-03 00:29:04 +02:00
/**
2020-10-07 02:20:28 +02:00
* Invoice Frequencies.
2019-05-03 00:29:04 +02:00
*/
const FREQUENCY_DAILY = 1;
const FREQUENCY_WEEKLY = 2;
const FREQUENCY_TWO_WEEKS = 3;
const FREQUENCY_FOUR_WEEKS = 4;
const FREQUENCY_MONTHLY = 5;
const FREQUENCY_TWO_MONTHS = 6;
const FREQUENCY_THREE_MONTHS = 7;
const FREQUENCY_FOUR_MONTHS = 8;
const FREQUENCY_SIX_MONTHS = 9;
const FREQUENCY_ANNUALLY = 10;
const FREQUENCY_TWO_YEARS = 11;
const FREQUENCY_THREE_YEARS = 12;
2019-05-03 00:29:04 +02:00
2019-05-05 02:49:01 +02:00
const RECURS_INDEFINITELY = -1;
protected $fillable = [
'client_id',
'project_id',
'number',
2019-07-03 11:58:34 +02:00
'discount',
'is_amount_discount',
'po_number',
'date',
'due_date',
2020-09-13 12:20:11 +02:00
'due_date_days',
'line_items',
'footer',
2019-07-03 11:58:34 +02:00
'public_notes',
'private_notes',
'terms',
'tax_name1',
'tax_name2',
'tax_name3',
'tax_rate1',
'tax_rate2',
'tax_rate3',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'amount',
2019-07-03 11:58:34 +02:00
'partial',
'frequency_id',
2020-09-16 12:08:51 +02:00
'next_send_date',
'remaining_cycles',
2020-09-23 02:16:19 +02:00
'auto_bill',
2020-09-24 05:40:13 +02:00
'auto_bill_enabled',
'design_id',
2021-02-23 02:17:46 +01:00
'custom_surcharge1',
'custom_surcharge2',
'custom_surcharge3',
'custom_surcharge4',
'custom_surcharge_tax1',
'custom_surcharge_tax2',
'custom_surcharge_tax3',
'custom_surcharge_tax4',
'design_id',
'assigned_user_id',
'exchange_rate',
];
2019-04-23 14:22:13 +02:00
protected $casts = [
'settings' => 'object',
'line_items' => 'object',
'backup' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
2019-04-23 14:22:13 +02:00
];
2019-08-15 13:10:02 +02:00
protected $appends = [
'hashed_id',
'status',
2019-05-03 00:29:04 +02:00
];
2020-07-23 05:55:11 +02:00
protected $touches = [];
public function getEntityType()
{
return self::class;
}
public function getDateAttribute($value)
{
if (! empty($value)) {
return (new Carbon($value))->format('Y-m-d');
}
return $value;
}
public function getDueDateAttribute($value)
{
if (! empty($value)) {
return (new Carbon($value))->format('Y-m-d');
}
return $value;
}
public function getPartialDueDateAttribute($value)
{
if (! empty($value)) {
return (new Carbon($value))->format('Y-m-d');
}
return $value;
}
2021-05-10 10:27:51 +02:00
public function activities()
{
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(300);
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);
}
2019-04-23 14:22:13 +02:00
public function company()
{
return $this->belongsTo(Company::class);
}
2019-05-03 00:29:04 +02:00
public function client()
{
return $this->belongsTo(Client::class)->withTrashed();
2019-05-03 00:29:04 +02:00
}
public function project()
{
return $this->belongsTo(Project::class)->withTrashed();
}
2019-04-23 14:22:13 +02:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
2019-04-23 14:22:13 +02:00
}
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
public function invoices()
{
2021-04-13 11:34:59 +02:00
return $this->hasMany(Invoice::class, 'recurring_id', 'id')->withTrashed();
}
2019-04-23 14:22:13 +02:00
public function invitations()
{
2020-09-18 09:04:28 +02:00
return $this->hasMany(RecurringInvoiceInvitation::class);
2019-04-23 14:22:13 +02:00
}
2019-05-23 07:08:31 +02:00
2020-09-17 23:49:22 +02:00
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
2019-08-15 13:10:02 +02:00
public function getStatusAttribute()
{
2020-11-25 15:19:52 +01:00
if ($this->status_id == self::STATUS_ACTIVE && Carbon::parse($this->next_send_date)->isFuture()) {
return self::STATUS_PENDING;
2020-11-25 15:19:52 +01:00
} else {
2019-08-15 13:10:02 +02:00
return $this->status_id;
2020-11-25 15:19:52 +01:00
}
2019-08-15 13:10:02 +02:00
}
2019-05-23 08:15:06 +02:00
public function nextSendDate() :?Carbon
2019-05-23 07:08:31 +02:00
{
2020-11-25 15:19:52 +01:00
if (!$this->next_send_date) {
return null;
}
2021-06-10 13:25:47 +02:00
2021-06-10 23:34:03 +02:00
$offset = $this->client->timezone_offset();
/*
As we are firing at UTC+0 if our offset is negative it is technically firing the day before so we always need
to add ON a day - a day = 86400 seconds
*/
if($offset < 0)
$offset += 86400;
switch ($this->frequency_id) {
2021-01-02 00:01:33 +01:00
case self::FREQUENCY_DAILY:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addDay()->addSeconds($offset);
case self::FREQUENCY_WEEKLY:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addWeek()->addSeconds($offset);
case self::FREQUENCY_TWO_WEEKS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addWeeks(2)->addSeconds($offset);
case self::FREQUENCY_FOUR_WEEKS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addWeeks(4)->addSeconds($offset);
case self::FREQUENCY_MONTHLY:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
case self::FREQUENCY_TWO_MONTHS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(2)->addSeconds($offset);
case self::FREQUENCY_THREE_MONTHS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
case self::FREQUENCY_FOUR_MONTHS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(4)->addSeconds($offset);
case self::FREQUENCY_SIX_MONTHS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(6)->addSeconds($offset);
case self::FREQUENCY_ANNUALLY:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addYear()->addSeconds($offset);
case self::FREQUENCY_TWO_YEARS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addYears(2)->addSeconds($offset);
case self::FREQUENCY_THREE_YEARS:
2021-06-10 13:25:47 +02:00
return Carbon::parse($this->next_send_date)->startOfDay()->addYears(3)->addSeconds($offset);
2019-05-23 07:08:31 +02:00
default:
2019-05-23 08:15:06 +02:00
return null;
}
2019-05-23 07:08:31 +02:00
}
2020-09-13 12:20:11 +02:00
public function nextDateByFrequency($date)
{
2021-06-11 00:20:46 +02:00
$offset = $this->client->timezone_offset();
2020-09-13 12:20:11 +02:00
switch ($this->frequency_id) {
2021-01-02 00:01:33 +01:00
case self::FREQUENCY_DAILY:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addDay()->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_WEEKLY:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addWeek()->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_TWO_WEEKS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addWeeks(2)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_FOUR_WEEKS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addWeeks(4)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_MONTHLY:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_TWO_MONTHS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(2)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_THREE_MONTHS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_FOUR_MONTHS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(4)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_SIX_MONTHS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->addMonthsNoOverflow(6)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_ANNUALLY:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addYear()->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_TWO_YEARS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addYears(2)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
case self::FREQUENCY_THREE_YEARS:
2021-06-11 00:20:46 +02:00
return Carbon::parse($date)->startOfDay()->addYears(3)->addSeconds($offset);
2020-09-13 12:20:11 +02:00
default:
return null;
}
}
2019-05-23 08:15:06 +02:00
public function remainingCycles() : int
2019-05-23 07:08:31 +02:00
{
if ($this->remaining_cycles == 0) {
2019-05-23 07:08:31 +02:00
return 0;
2020-11-25 15:19:52 +01:00
} elseif ($this->remaining_cycles == -1) {
2020-09-12 12:33:12 +02:00
return -1;
2020-11-25 15:19:52 +01:00
} else {
2019-05-23 07:08:31 +02:00
return $this->remaining_cycles - 1;
}
2019-05-23 07:08:31 +02:00
}
2019-05-23 08:15:06 +02:00
public function setCompleted() : void
2019-05-23 07:08:31 +02:00
{
$this->status_id = self::STATUS_COMPLETED;
$this->next_send_date = null;
2019-05-23 08:15:06 +02:00
$this->remaining_cycles = 0;
2019-05-23 07:08:31 +02:00
$this->save();
}
2019-08-15 13:10:02 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
2019-08-15 13:10:02 +02:00
return '<h4><span class="badge badge-light">'.ctrans('texts.draft').'</span></h4>';
break;
case self::STATUS_PENDING:
2020-09-23 02:16:19 +02:00
return '<h4><span class="badge badge-primary">'.ctrans('texts.pending').'</span></h4>';
2019-08-15 13:10:02 +02:00
break;
case self::STATUS_ACTIVE:
2020-09-23 02:16:19 +02:00
return '<h4><span class="badge badge-primary">'.ctrans('texts.active').'</span></h4>';
2019-08-15 13:10:02 +02:00
break;
case self::STATUS_COMPLETED:
2019-08-15 13:10:02 +02:00
return '<h4><span class="badge badge-success">'.ctrans('texts.status_completed').'</span></h4>';
break;
2020-09-23 02:16:19 +02:00
case self::STATUS_PAUSED:
return '<h4><span class="badge badge-danger">'.ctrans('texts.paused').'</span></h4>';
break;
2019-08-15 13:10:02 +02:00
default:
// code...
2019-08-15 13:10:02 +02:00
break;
}
}
public static function frequencyForKey(int $frequency_id) :string
{
switch ($frequency_id) {
case self::FREQUENCY_DAILY:
return ctrans('texts.freq_daily');
break;
case self::FREQUENCY_WEEKLY:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_weekly');
break;
case self::FREQUENCY_TWO_WEEKS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_two_weeks');
break;
case self::FREQUENCY_FOUR_WEEKS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_four_weeks');
break;
case self::FREQUENCY_MONTHLY:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_monthly');
break;
case self::FREQUENCY_TWO_MONTHS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_two_months');
break;
case self::FREQUENCY_THREE_MONTHS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_three_months');
break;
case self::FREQUENCY_FOUR_MONTHS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_four_months');
break;
case self::FREQUENCY_SIX_MONTHS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_six_months');
break;
case self::FREQUENCY_ANNUALLY:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_annually');
break;
case self::FREQUENCY_TWO_YEARS:
2019-08-15 13:10:02 +02:00
return ctrans('texts.freq_two_years');
break;
2019-08-15 13:10:02 +02:00
default:
// code...
2019-08-15 13:10:02 +02:00
break;
}
}
2020-07-01 13:33:42 +02:00
2020-09-13 12:20:11 +02:00
public function calc()
{
$invoice_calc = null;
if ($this->uses_inclusive_taxes) {
$invoice_calc = new InvoiceSumInclusive($this);
} else {
$invoice_calc = new InvoiceSum($this);
}
return $invoice_calc->build();
}
2020-11-25 15:19:52 +01:00
/*
* Important to note when playing with carbon dates - in order
2020-09-13 12:20:11 +02:00
* not to modify the original instance, always use a `->copy()`
2020-11-25 15:19:52 +01:00
*
2020-09-13 12:20:11 +02:00
*/
2020-07-01 13:33:42 +02:00
public function recurringDates()
{
2020-09-14 13:11:46 +02:00
2020-11-25 15:19:52 +01:00
/* Return early if nothing to send back! */
if ($this->status_id == self::STATUS_COMPLETED ||
2020-09-13 12:20:11 +02:00
$this->remaining_cycles == 0 ||
!$this->next_send_date) {
2020-09-12 12:33:12 +02:00
return [];
}
/* Endless - lets send 10 back*/
$iterations = $this->remaining_cycles;
2020-11-25 15:19:52 +01:00
if ($this->remaining_cycles == -1) {
2020-09-12 12:33:12 +02:00
$iterations = 10;
2020-11-25 15:19:52 +01:00
}
$data = [];
2021-01-04 13:38:00 +01:00
if (!Carbon::parse($this->next_send_date)) {
return $data;
2021-01-04 13:38:00 +01:00
}
2020-09-12 12:33:12 +02:00
$next_send_date = Carbon::parse($this->next_send_date)->copy();
2020-11-25 15:19:52 +01:00
for ($x=0; $x<$iterations; $x++) {
2020-09-24 13:30:46 +02:00
// we don't add the days... we calc the day of the month!!
$next_due_date = $this->calculateDueDate($next_send_date->copy()->format('Y-m-d'));
2020-10-06 03:36:25 +02:00
$next_due_date_string = $next_due_date ? $next_due_date->format('Y-m-d') : '';
2020-09-13 12:20:11 +02:00
$next_send_date = Carbon::parse($next_send_date);
2020-09-12 12:33:12 +02:00
2020-09-13 12:20:11 +02:00
$data[] = [
2020-11-25 15:19:52 +01:00
'send_date' => $next_send_date->format('Y-m-d'),
2020-10-06 03:36:25 +02:00
'due_date' => $next_due_date_string
2020-09-13 12:20:11 +02:00
];
2020-09-12 12:33:12 +02:00
/* Fixes the timeshift in case the offset is negative which cause a infinite loop due to UTC +0*/
if($this->client->timezone_offset() < 0){
$next_send_date = $this->nextDateByFrequency($next_send_date->addDay()->format('Y-m-d'));
}
else
$next_send_date = $this->nextDateByFrequency($next_send_date->format('Y-m-d'));
2020-09-13 12:20:11 +02:00
}
return $data;
2020-07-01 13:33:42 +02:00
}
2020-09-12 12:33:12 +02:00
2020-11-25 15:19:52 +01:00
public function calculateDueDate($date)
2020-09-14 13:11:46 +02:00
{
2020-09-14 14:13:15 +02:00
switch ($this->due_date_days) {
case 'terms':
2020-09-14 13:11:46 +02:00
return $this->calculateDateFromTerms($date);
break;
default:
2020-09-14 14:13:15 +02:00
return $this->setDayOfMonth($date, $this->due_date_days);
2020-09-14 13:11:46 +02:00
break;
}
}
/**
* Calculates a date based on the client payment terms.
2020-11-25 15:19:52 +01:00
*
2020-09-14 13:11:46 +02:00
* @param Carbon $date A given date
* @return NULL|Carbon The date
*/
2020-11-25 15:19:52 +01:00
public function calculateDateFromTerms($date)
2020-09-14 13:11:46 +02:00
{
2020-09-24 13:30:46 +02:00
$new_date = Carbon::parse($date);
2020-09-14 13:11:46 +02:00
$client_payment_terms = $this->client->getSetting('payment_terms');
2020-11-25 15:19:52 +01:00
if ($client_payment_terms == '') {//no due date! return null;
2020-09-14 13:11:46 +02:00
return null;
2020-11-25 15:19:52 +01:00
}
2020-09-14 13:11:46 +02:00
2020-09-24 13:30:46 +02:00
return $new_date->addDays($client_payment_terms); //add the number of days in the payment terms to the date
2020-09-14 13:11:46 +02:00
}
2020-09-24 02:57:44 +02:00
/**
* Service entry points.
*/
public function service() :RecurringService
{
return new RecurringService($this);
}
public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Subscription::class);
}
2019-04-23 14:22:13 +02:00
}