1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/RecurringInvoice.php

260 lines
7.8 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)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-23 14:22:13 +02:00
namespace App\Models;
use App\Models\Filterable;
use App\Utils\Traits\MakesDates;
2019-04-23 14:22:13 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2019-05-23 08:15:06 +02:00
use Illuminate\Support\Carbon;
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;
2019-05-03 00:29:04 +02:00
/**
* Invoice Statuses
*/
2019-05-03 09:35:49 +02:00
const STATUS_DRAFT = 2;
const STATUS_ACTIVE = 3;
2019-08-15 13:10:02 +02:00
const STATUS_CANCELLED = 4;
2019-05-03 09:35:49 +02:00
const STATUS_PENDING = -1;
const STATUS_COMPLETED = -2;
2019-05-03 00:29:04 +02:00
/**
* Recurring intervals //todo MAP WHEN WE MIGRATE
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;
2019-05-03 00:29:04 +02:00
2019-05-05 02:49:01 +02:00
const RECURS_INDEFINITELY = -1;
2019-05-03 00:29:04 +02:00
protected $fillable = [
'client_id',
'invoice_number',
2019-07-03 11:58:34 +02:00
'discount',
'is_amount_discount',
'po_number',
'invoice_date',
'due_date',
'line_items',
'settings',
'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',
'start_date',
2019-04-23 14:22:13 +02:00
];
protected $casts = [
'settings' => 'object',
'line_items' => '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
];
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);
}
2019-04-23 14:22:13 +02:00
public function user()
{
return $this->belongsTo(User::class);
}
public function assigned_user()
{
return $this->belongsTo(User::class ,'assigned_user_id', 'id');
}
public function invoices()
{
return $this->hasMany(Invoice::class, "id", "recurring_invoice_id");
}
2019-04-23 14:22:13 +02:00
public function invitations()
{
2019-04-24 02:22:02 +02:00
$this->morphMany(RecurringInvoiceInvitation::class);
2019-04-23 14:22:13 +02:00
}
2019-05-23 07:08:31 +02:00
2019-08-15 13:10:02 +02:00
public function getStatusAttribute()
{
if($this->status_id == RecurringInvoice::STATUS_ACTIVE && $this->start_date > Carbon::now()) //marked as active, but yet to fire first cycle
return RecurringInvoice::STATUS_PENDING;
else if($this->status_id == RecurringInvoice::STATUS_ACTIVE && $this->next_send_date > Carbon::now())
return RecurringInvoice::STATUS_COMPLETED;
else
return $this->status_id;
}
2019-05-23 08:15:06 +02:00
public function nextSendDate() :?Carbon
2019-05-23 07:08:31 +02:00
{
2019-05-23 08:15:06 +02:00
switch ($this->frequency_id)
{
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_WEEKLY:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addWeek());
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addWeeks(2));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addWeeks(4));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_MONTHLY:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addMonth());
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addMonths(2));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addMonths(3));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addMonths(4));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addMonths(6));
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_ANNUALLY:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addYear());
2019-08-15 13:10:02 +02:00
case RecurringInvoice::FREQUENCY_TWO_YEARS:
2019-05-23 08:15:06 +02:00
return Carbon::parse($this->next_send_date->addYears(2));
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
}
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)
return 0;
else
return $this->remaining_cycles - 1;
}
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-05-23 08:15:06 +02:00
2019-05-23 07:08:31 +02:00
}
2019-08-15 13:10:02 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
case RecurringInvoice::STATUS_DRAFT:
return '<h4><span class="badge badge-light">'.ctrans('texts.draft').'</span></h4>';
break;
case RecurringInvoice::STATUS_PENDING:
return '<h4><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h4>';
break;
case RecurringInvoice::STATUS_ACTIVE:
return '<h4><span class="badge badge-primary">'.ctrans('texts.partial').'</span></h4>';
break;
case RecurringInvoice::STATUS_COMPLETED:
return '<h4><span class="badge badge-success">'.ctrans('texts.status_completed').'</span></h4>';
break;
case RecurringInvoice::STATUS_CANCELLED:
return '<h4><span class="badge badge-danger">'.ctrans('texts.overdue').'</span></h4>';
break;
default:
# code...
break;
}
}
public static function frequencyForKey(int $frequency_id) :string
{
switch ($frequency_id) {
case RecurringInvoice::FREQUENCY_WEEKLY:
return ctrans('texts.freq_weekly');
break;
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return ctrans('texts.freq_two_weeks');
break;
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return ctrans('texts.freq_four_weeks');
break;
case RecurringInvoice::FREQUENCY_MONTHLY:
return ctrans('texts.freq_monthly');
break;
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
return ctrans('texts.freq_two_months');
break;
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
return ctrans('texts.freq_three_months');
break;
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
return ctrans('texts.freq_four_months');
break;
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
return ctrans('texts.freq_six_months');
break;
case RecurringInvoice::FREQUENCY_ANNUALLY:
return ctrans('texts.freq_annually');
break;
case RecurringInvoice::FREQUENCY_TWO_YEARS:
return ctrans('texts.freq_two_years');
break;
case RecurringInvoice::RECURS_INDEFINITELY:
return ctrans('texts.freq_indefinitely');
break;
default:
# code...
break;
}
}
2019-04-23 14:22:13 +02:00
}