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

589 lines
20 KiB
PHP
Raw Normal View History

2019-05-05 02:49:01 +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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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-05-05 02:49:01 +02:00
namespace App\Models;
2021-08-24 04:57:46 +02:00
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Models\Presenters\RecurringQuotePresenter;
use App\Services\Recurring\RecurringService;
use App\Utils\Traits\MakesDates;
2019-05-05 02:49:01 +02:00
use App\Utils\Traits\MakesHash;
2021-08-24 04:57:46 +02:00
use App\Utils\Traits\Recurring\HasRecurrence;
2019-05-05 02:49:01 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
2021-08-24 04:57:46 +02:00
use Laracasts\Presenter\PresentableTrait;
2019-05-05 02:49:01 +02:00
/**
2021-08-24 04:57:46 +02:00
* Class for Recurring Quotes.
2023-03-08 08:33:42 +01:00
*
* @property int $id
* @property int $client_id
* @property int $user_id
* @property int|null $assigned_user_id
* @property int $company_id
* @property int|null $project_id
* @property int|null $vendor_id
* @property int $status_id
* @property float $discount
* @property int $is_amount_discount
* @property string|null $number
* @property string|null $po_number
* @property string|null $date
* @property string|null $due_date
* @property int $is_deleted
* @property object|null $line_items
* @property object|null $backup
* @property string|null $footer
* @property string|null $public_notes
* @property string|null $private_notes
* @property string|null $terms
* @property string|null $tax_name1
* @property string $tax_rate1
* @property string|null $tax_name2
* @property string $tax_rate2
* @property string|null $tax_name3
* @property string $tax_rate3
* @property string $total_taxes
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
* @property string|null $custom_value4
* @property string $amount
* @property string $balance
* @property string|null $last_viewed
* @property int $frequency_id
2023-08-02 11:47:26 +02:00
* @property int $design_id
2023-03-08 08:33:42 +01:00
* @property string|null $last_sent_date
* @property string|null $next_send_date
* @property int|null $remaining_cycles
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $deleted_at
* @property string $auto_bill
* @property int $auto_bill_enabled
* @property string $paid_to_date
* @property string|null $custom_surcharge1
* @property string|null $custom_surcharge2
* @property string|null $custom_surcharge3
* @property string|null $custom_surcharge4
* @property int $custom_surcharge_tax1
* @property int $custom_surcharge_tax2
* @property int $custom_surcharge_tax3
* @property int $custom_surcharge_tax4
* @property string|null $due_date_days
* @property string $exchange_rate
2023-04-26 11:25:33 +02:00
* @property float|null $partial
2023-03-08 08:33:42 +01:00
* @property string|null $partial_due_date
* @property int|null $subscription_id
* @property int $uses_inclusive_taxes
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
* @property-read int|null $activities_count
* @property-read \App\Models\User|null $assigned_user
* @property-read \App\Models\Client $client
* @property-read \App\Models\Company $company
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
* @property-read int|null $documents_count
* @property-read mixed $hashed_id
* @property-read mixed $status
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
* @property-read int|null $history_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringQuoteInvitation> $invitations
* @property-read int|null $invitations_count
* @property-read \App\Models\Project|null $project
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Quote> $quotes
* @property-read int|null $quotes_count
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
* @method static \Database\Factories\RecurringQuoteFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote filter(\App\Filters\QueryFilters $filters)
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote query()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|RecurringQuote withoutTrashed()
2023-03-16 05:20:38 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
2023-03-24 08:02:34 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringQuoteInvitation> $invitations
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Quote> $quotes
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
2019-05-05 02:49:01 +02:00
*/
class RecurringQuote extends BaseModel
{
use MakesHash;
use SoftDeletes;
use Filterable;
2021-08-24 04:57:46 +02:00
use MakesDates;
use HasRecurrence;
use PresentableTrait;
protected $presenter = RecurringQuotePresenter::class;
2019-05-05 02:49:01 +02:00
/**
2021-08-24 04:57:46 +02:00
* Quote Statuses.
2019-05-05 02:49:01 +02:00
*/
2021-08-24 04:57:46 +02:00
const STATUS_DRAFT = 1;
2021-08-24 04:57:46 +02:00
const STATUS_ACTIVE = 2;
2021-08-24 04:57:46 +02:00
const STATUS_PAUSED = 3;
2021-08-24 04:57:46 +02:00
const STATUS_COMPLETED = 4;
2019-05-05 02:49:01 +02:00
const STATUS_PENDING = -1;
/**
2021-08-24 04:57:46 +02:00
* Quote Frequencies.
2019-05-05 02:49:01 +02:00
*/
2021-08-24 04:57:46 +02:00
const FREQUENCY_DAILY = 1;
2021-08-24 04:57:46 +02:00
const FREQUENCY_WEEKLY = 2;
2021-08-24 04:57:46 +02:00
const FREQUENCY_TWO_WEEKS = 3;
2021-08-24 04:57:46 +02:00
const FREQUENCY_FOUR_WEEKS = 4;
2021-08-24 04:57:46 +02:00
const FREQUENCY_MONTHLY = 5;
2021-08-24 04:57:46 +02:00
const FREQUENCY_TWO_MONTHS = 6;
2021-08-24 04:57:46 +02:00
const FREQUENCY_THREE_MONTHS = 7;
2021-08-24 04:57:46 +02:00
const FREQUENCY_FOUR_MONTHS = 8;
2021-08-24 04:57:46 +02:00
const FREQUENCY_SIX_MONTHS = 9;
2021-08-24 04:57:46 +02:00
const FREQUENCY_ANNUALLY = 10;
2021-08-24 04:57:46 +02:00
const FREQUENCY_TWO_YEARS = 11;
2021-08-24 04:57:46 +02:00
const FREQUENCY_THREE_YEARS = 12;
2019-05-05 02:49:01 +02:00
const RECURS_INDEFINITELY = -1;
protected $fillable = [
'client_id',
2021-08-24 04:57:46 +02:00
'project_id',
'number',
'discount',
'is_amount_discount',
'po_number',
2021-08-24 04:57:46 +02:00
'date',
'due_date',
'due_date_days',
'line_items',
'footer',
2021-08-24 04:57:46 +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',
2021-08-24 04:57:46 +02:00
'partial',
'frequency_id',
2021-08-24 04:57:46 +02:00
'next_send_date',
'remaining_cycles',
'auto_bill',
'auto_bill_enabled',
'design_id',
'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-05-05 02:49:01 +02:00
protected $casts = [
2021-08-24 04:57:46 +02:00
'settings' => 'object',
'line_items' => 'object',
'backup' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
2019-05-05 02:49:01 +02:00
];
2021-08-24 04:57:46 +02:00
protected $appends = [
'hashed_id',
'status',
2019-05-05 02:49:01 +02:00
];
2021-08-24 04:57:46 +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-08-24 04:57:46 +02:00
public function activities()
{
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);
}
2019-05-05 02:49:01 +02:00
public function company()
{
return $this->belongsTo(Company::class);
}
public function client()
{
return $this->belongsTo(Client::class)->withTrashed();
2019-05-05 02:49:01 +02:00
}
2021-08-24 04:57:46 +02:00
public function project()
{
return $this->belongsTo(Project::class)->withTrashed();
}
2019-05-05 02:49:01 +02:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
2019-05-05 02:49:01 +02:00
}
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
2021-08-24 04:57:46 +02:00
public function quotes()
{
return $this->hasMany(Quote::class, 'recurring_id', 'id')->withTrashed();
}
2019-05-05 02:49:01 +02:00
public function invitations()
{
2021-08-24 04:57:46 +02:00
return $this->hasMany(RecurringQuoteInvitation::class);
}
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
public function getStatusAttribute()
{
if ($this->status_id == self::STATUS_ACTIVE && Carbon::parse($this->next_send_date)->isFuture()) {
return self::STATUS_PENDING;
} else {
return $this->status_id;
}
}
public function nextSendDate() :?Carbon
{
if (! $this->next_send_date) {
2021-08-24 04:57:46 +02:00
return null;
}
2021-08-24 04:57:46 +02:00
$offset = $this->client->timezone_offset();
/*
2021-08-24 04:57:46 +02:00
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) {
2021-08-24 04:57:46 +02:00
$offset += 86400;
}
2021-08-24 04:57:46 +02:00
switch ($this->frequency_id) {
case self::FREQUENCY_DAILY:
return Carbon::parse($this->next_send_date)->startOfDay()->addDay()->addSeconds($offset);
case self::FREQUENCY_WEEKLY:
return Carbon::parse($this->next_send_date)->startOfDay()->addWeek()->addSeconds($offset);
case self::FREQUENCY_TWO_WEEKS:
return Carbon::parse($this->next_send_date)->startOfDay()->addWeeks(2)->addSeconds($offset);
case self::FREQUENCY_FOUR_WEEKS:
return Carbon::parse($this->next_send_date)->startOfDay()->addWeeks(4)->addSeconds($offset);
case self::FREQUENCY_MONTHLY:
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
case self::FREQUENCY_TWO_MONTHS:
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(2)->addSeconds($offset);
case self::FREQUENCY_THREE_MONTHS:
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
case self::FREQUENCY_FOUR_MONTHS:
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(4)->addSeconds($offset);
case self::FREQUENCY_SIX_MONTHS:
return Carbon::parse($this->next_send_date)->startOfDay()->addMonthsNoOverflow(6)->addSeconds($offset);
case self::FREQUENCY_ANNUALLY:
return Carbon::parse($this->next_send_date)->startOfDay()->addYear()->addSeconds($offset);
case self::FREQUENCY_TWO_YEARS:
return Carbon::parse($this->next_send_date)->startOfDay()->addYears(2)->addSeconds($offset);
case self::FREQUENCY_THREE_YEARS:
return Carbon::parse($this->next_send_date)->startOfDay()->addYears(3)->addSeconds($offset);
default:
return null;
}
}
public function nextDateByFrequency($date)
{
$offset = $this->client->timezone_offset();
switch ($this->frequency_id) {
case self::FREQUENCY_DAILY:
return Carbon::parse($date)->startOfDay()->addDay()->addSeconds($offset);
case self::FREQUENCY_WEEKLY:
return Carbon::parse($date)->startOfDay()->addWeek()->addSeconds($offset);
case self::FREQUENCY_TWO_WEEKS:
return Carbon::parse($date)->startOfDay()->addWeeks(2)->addSeconds($offset);
case self::FREQUENCY_FOUR_WEEKS:
return Carbon::parse($date)->startOfDay()->addWeeks(4)->addSeconds($offset);
case self::FREQUENCY_MONTHLY:
return Carbon::parse($date)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
case self::FREQUENCY_TWO_MONTHS:
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(2)->addSeconds($offset);
case self::FREQUENCY_THREE_MONTHS:
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
case self::FREQUENCY_FOUR_MONTHS:
return Carbon::parse($date)->startOfDay()->addMonthsNoOverflow(4)->addSeconds($offset);
case self::FREQUENCY_SIX_MONTHS:
return Carbon::parse($date)->addMonthsNoOverflow(6)->addSeconds($offset);
case self::FREQUENCY_ANNUALLY:
return Carbon::parse($date)->startOfDay()->addYear()->addSeconds($offset);
case self::FREQUENCY_TWO_YEARS:
return Carbon::parse($date)->startOfDay()->addYears(2)->addSeconds($offset);
case self::FREQUENCY_THREE_YEARS:
return Carbon::parse($date)->startOfDay()->addYears(3)->addSeconds($offset);
default:
return null;
}
}
public function remainingCycles() : int
{
if ($this->remaining_cycles == 0) {
return 0;
} elseif ($this->remaining_cycles == -1) {
return -1;
} else {
return $this->remaining_cycles - 1;
}
}
public function setCompleted() : void
{
$this->status_id = self::STATUS_COMPLETED;
$this->next_send_date = null;
$this->remaining_cycles = 0;
$this->save();
}
public static function badgeForStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
return '<h4><span class="badge badge-light">'.ctrans('texts.draft').'</span></h4>';
case self::STATUS_PENDING:
return '<h4><span class="badge badge-primary">'.ctrans('texts.pending').'</span></h4>';
case self::STATUS_ACTIVE:
return '<h4><span class="badge badge-primary">'.ctrans('texts.active').'</span></h4>';
case self::STATUS_COMPLETED:
return '<h4><span class="badge badge-success">'.ctrans('texts.status_completed').'</span></h4>';
case self::STATUS_PAUSED:
return '<h4><span class="badge badge-danger">'.ctrans('texts.paused').'</span></h4>';
default:
2023-04-26 13:18:01 +02:00
return '<h4><span class="badge badge-primary">'.ctrans('texts.pending').'</span></h4>';
2021-08-24 04:57:46 +02:00
}
2019-05-05 02:49:01 +02:00
}
2021-08-24 04:57:46 +02:00
public static function frequencyForKey(int $frequency_id) :string
{
switch ($frequency_id) {
case self::FREQUENCY_DAILY:
return ctrans('texts.freq_daily');
case self::FREQUENCY_WEEKLY:
return ctrans('texts.freq_weekly');
case self::FREQUENCY_TWO_WEEKS:
return ctrans('texts.freq_two_weeks');
case self::FREQUENCY_FOUR_WEEKS:
return ctrans('texts.freq_four_weeks');
case self::FREQUENCY_MONTHLY:
return ctrans('texts.freq_monthly');
case self::FREQUENCY_TWO_MONTHS:
return ctrans('texts.freq_two_months');
case self::FREQUENCY_THREE_MONTHS:
return ctrans('texts.freq_three_months');
case self::FREQUENCY_FOUR_MONTHS:
return ctrans('texts.freq_four_months');
case self::FREQUENCY_SIX_MONTHS:
return ctrans('texts.freq_six_months');
case self::FREQUENCY_ANNUALLY:
return ctrans('texts.freq_annually');
case self::FREQUENCY_TWO_YEARS:
return ctrans('texts.freq_two_years');
default:
2023-04-26 13:18:01 +02:00
return ctrans('texts.freq_weekly');
2021-08-24 04:57:46 +02:00
}
}
2023-04-26 12:57:30 +02:00
/**
* Access the invoice calculator object.
* @return InvoiceSumInclusive | InvoiceSum The invoice calculator object getters
*/
public function calc(): InvoiceSumInclusive | InvoiceSum
2021-08-24 04:57:46 +02:00
{
$invoice_calc = null;
2023-04-26 13:18:01 +02:00
if ($this->uses_inclusive_taxes) {
2021-08-24 04:57:46 +02:00
$invoice_calc = new InvoiceSumInclusive($this);
2023-04-26 13:18:01 +02:00
} else {
2021-08-24 04:57:46 +02:00
$invoice_calc = new InvoiceSum($this);
}
return $invoice_calc->build();
}
/*
* Important to note when playing with carbon dates - in order
* not to modify the original instance, always use a `->copy()`
*
*/
public function recurringDates()
{
/* Return early if nothing to send back! */
if ($this->status_id == self::STATUS_COMPLETED ||
$this->remaining_cycles == 0 ||
! $this->next_send_date) {
2021-08-24 04:57:46 +02:00
return [];
}
/* Endless - lets send 10 back*/
$iterations = $this->remaining_cycles;
if ($this->remaining_cycles == -1) {
$iterations = 10;
}
$data = [];
if (! Carbon::parse($this->next_send_date)) {
2021-08-24 04:57:46 +02:00
return $data;
}
$next_send_date = Carbon::parse($this->next_send_date)->copy();
for ($x = 0; $x < $iterations; $x++) {
2021-08-24 04:57: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'));
$next_due_date_string = $next_due_date ? $next_due_date->format('Y-m-d') : '';
$next_send_date = Carbon::parse($next_send_date);
$data[] = [
'send_date' => $next_send_date->format('Y-m-d'),
'due_date' => $next_due_date_string,
2021-08-24 04:57:46 +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) {
2021-08-24 04:57:46 +02:00
$next_send_date = $this->nextDateByFrequency($next_send_date->addDay()->format('Y-m-d'));
} else {
2021-08-24 04:57:46 +02:00
$next_send_date = $this->nextDateByFrequency($next_send_date->format('Y-m-d'));
}
2021-08-24 04:57:46 +02:00
}
return $data;
}
public function calculateDueDate($date)
{
switch ($this->due_date_days) {
case 'terms':
return $this->calculateDateFromTerms($date);
default:
return $this->setDayOfMonth($date, $this->due_date_days);
}
}
/**
* Calculates a date based on the client payment terms.
*
* @param Carbon $date A given date
* @return null|Carbon The date
2021-08-24 04:57:46 +02:00
*/
public function calculateDateFromTerms($date)
{
$new_date = Carbon::parse($date);
$client_payment_terms = $this->client->getSetting('payment_terms');
if ($client_payment_terms == '') {//no due date! return null;
return null;
}
return $new_date->addDays($client_payment_terms); //add the number of days in the payment terms to the date
}
/**
* Service entry points.
2023-04-26 13:18:01 +02:00
* @return RecurringService
2021-08-24 04:57:46 +02:00
*/
public function service() :RecurringService
{
return new RecurringService($this);
}
2022-04-06 02:38:01 +02:00
public function translate_entity()
{
return ctrans('texts.recurring_quote');
}
2019-05-05 02:49:01 +02:00
}