2019-04-23 14:22:13 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +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-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;
|
2021-01-15 03:02:55 +01:00
|
|
|
use App\Models\Presenters\RecurringInvoicePresenter;
|
2020-09-24 02:57:44 +02:00
|
|
|
use App\Services\Recurring\RecurringService;
|
2019-11-05 23:52:57 +01:00
|
|
|
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;
|
2021-01-15 03:02:55 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2019-04-23 14:22:13 +02:00
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
/**
|
|
|
|
* Class for Recurring Invoices.
|
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 string|null $number
|
|
|
|
* @property float $discount
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property bool $is_amount_discount
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $po_number
|
|
|
|
* @property string|null $date
|
|
|
|
* @property string|null $due_date
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property bool $is_deleted
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property mixed $line_items
|
2023-03-08 08:33:42 +01:00
|
|
|
* @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
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property float $tax_rate1
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $tax_name2
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property float $tax_rate2
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $tax_name3
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property float $tax_rate3
|
|
|
|
* @property float $total_taxes
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $custom_value1
|
|
|
|
* @property string|null $custom_value2
|
|
|
|
* @property string|null $custom_value3
|
|
|
|
* @property string|null $custom_value4
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property float $amount
|
|
|
|
* @property float $balance
|
2023-04-26 11:25:33 +02:00
|
|
|
* @property float|null $partial
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $last_viewed
|
|
|
|
* @property int $frequency_id
|
|
|
|
* @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
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property bool $auto_bill_enabled
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int|null $design_id
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property bool $uses_inclusive_taxes
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $custom_surcharge1
|
|
|
|
* @property string|null $custom_surcharge2
|
|
|
|
* @property string|null $custom_surcharge3
|
|
|
|
* @property string|null $custom_surcharge4
|
2023-04-26 13:18:01 +02:00
|
|
|
* @property bool $custom_surcharge_tax1
|
|
|
|
* @property bool $custom_surcharge_tax2
|
|
|
|
* @property bool $custom_surcharge_tax3
|
|
|
|
* @property bool $custom_surcharge_tax4
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $due_date_days
|
|
|
|
* @property string|null $partial_due_date
|
|
|
|
* @property string $exchange_rate
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property float $paid_to_date
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int|null $subscription_id
|
|
|
|
* @property string|null $next_send_date_client
|
|
|
|
* @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
|
2023-08-11 06:18:58 +02:00
|
|
|
* @property \App\Models\RecurringInvoiceInvitation $invitation
|
2023-03-08 08:33:42 +01:00
|
|
|
* @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\RecurringInvoiceInvitation> $invitations
|
|
|
|
* @property-read int|null $invitations_count
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Invoice> $invoices
|
|
|
|
* @property-read int|null $invoices_count
|
|
|
|
* @property-read \App\Models\Project|null $project
|
|
|
|
* @property-read \App\Models\Subscription|null $subscription
|
|
|
|
* @property-read \App\Models\User $user
|
|
|
|
* @property-read \App\Models\Vendor|null $vendor
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
|
|
|
|
* @method static \Database\Factories\RecurringInvoiceFactory factory($count = null, $state = [])
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice filter(\App\Filters\QueryFilters $filters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|RecurringInvoice withoutTrashed()
|
2023-03-16 05:20:38 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @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\RecurringInvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Invoice> $invoices
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property bool $is_proforma
|
2023-03-08 08:33:42 +01:00
|
|
|
* @mixin \Eloquent
|
2019-05-03 00:29:04 +02:00
|
|
|
*/
|
2019-04-23 14:22:13 +02:00
|
|
|
class RecurringInvoice extends BaseModel
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use SoftDeletes;
|
|
|
|
use Filterable;
|
2019-11-05 23:52:57 +01:00
|
|
|
use MakesDates;
|
2020-09-14 14:13:15 +02:00
|
|
|
use HasRecurrence;
|
2021-01-15 03:02:55 +01:00
|
|
|
use PresentableTrait;
|
|
|
|
|
|
|
|
protected $presenter = RecurringInvoicePresenter::class;
|
2020-09-14 14:13:15 +02:00
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Statuses.
|
2019-05-03 00:29:04 +02:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public const STATUS_DRAFT = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const STATUS_ACTIVE = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const STATUS_PAUSED = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const STATUS_COMPLETED = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const STATUS_PENDING = -1;
|
2019-05-03 09:35:49 +02:00
|
|
|
|
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
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_DAILY = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_WEEKLY = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_TWO_WEEKS = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_FOUR_WEEKS = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_MONTHLY = 5;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_TWO_MONTHS = 6;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_THREE_MONTHS = 7;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_FOUR_MONTHS = 8;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_SIX_MONTHS = 9;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_ANNUALLY = 10;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_TWO_YEARS = 11;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const FREQUENCY_THREE_YEARS = 12;
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const RECURS_INDEFINITELY = -1;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
protected $fillable = [
|
|
|
|
'client_id',
|
2020-10-14 22:58:20 +02:00
|
|
|
'project_id',
|
2019-11-27 11:27:24 +01:00
|
|
|
'number',
|
2019-07-03 11:58:34 +02:00
|
|
|
'discount',
|
2019-06-26 06:04:10 +02:00
|
|
|
'is_amount_discount',
|
|
|
|
'po_number',
|
2019-11-27 11:27:24 +01:00
|
|
|
'date',
|
2019-06-26 06:04:10 +02:00
|
|
|
'due_date',
|
2020-09-13 12:20:11 +02:00
|
|
|
'due_date_days',
|
2019-06-26 06:04:10 +02:00
|
|
|
'line_items',
|
|
|
|
'footer',
|
2019-07-03 11:58:34 +02:00
|
|
|
'public_notes',
|
2019-06-26 06:04:10 +02:00
|
|
|
'private_notes',
|
|
|
|
'terms',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_name2',
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_name3',
|
2019-06-26 06:04:10 +02:00
|
|
|
'tax_rate1',
|
|
|
|
'tax_rate2',
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_rate3',
|
2019-06-26 06:04:10 +02:00
|
|
|
'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',
|
2021-01-11 22:57:48 +01:00
|
|
|
'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',
|
2022-02-08 11:43:42 +01:00
|
|
|
'vendor_id',
|
2022-06-02 05:49:29 +02:00
|
|
|
'next_send_date_client',
|
2022-11-15 11:27:39 +01:00
|
|
|
'uses_inclusive_taxes',
|
2019-12-30 22:59:12 +01:00
|
|
|
];
|
2019-04-23 14:22:13 +02:00
|
|
|
|
|
|
|
protected $casts = [
|
2019-05-24 11:23:38 +02:00
|
|
|
'settings' => 'object',
|
|
|
|
'line_items' => 'object',
|
2020-06-15 01:34:18 +02:00
|
|
|
'backup' => 'object',
|
2019-09-26 00:27:26 +02:00
|
|
|
'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',
|
2020-09-06 11:38:10 +02:00
|
|
|
'status',
|
2019-05-03 00:29:04 +02:00
|
|
|
];
|
|
|
|
|
2023-04-24 12:35:26 +02:00
|
|
|
protected $with = [
|
|
|
|
'client.company',
|
|
|
|
];
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDateAttribute($value)
|
|
|
|
{
|
2020-09-06 11:38:10 +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
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDueDateAttribute($value)
|
|
|
|
{
|
2020-09-06 11:38:10 +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
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getPartialDueDateAttribute($value)
|
|
|
|
{
|
2020-09-06 11:38:10 +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
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
return $value;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2022-02-08 11:43:42 +01:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Vendor::class);
|
|
|
|
}
|
|
|
|
|
2021-05-10 10:27:51 +02:00
|
|
|
public function activities()
|
|
|
|
{
|
2021-08-22 08:23:00 +02:00
|
|
|
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
|
2021-05-10 10:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function history()
|
|
|
|
{
|
|
|
|
return $this->hasManyThrough(Backup::class, Activity::class);
|
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-04-23 14:22:13 +02:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2021-02-26 21:48:49 +01:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Project::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-04-23 14:22:13 +02:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2019-04-23 14:22:13 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:28 +02:00
|
|
|
public function assigned_user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
2019-11-05 11:16:38 +01:00
|
|
|
{
|
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
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-08-02 11:47:26 +02:00
|
|
|
public function invoices(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2019-10-22 13:27:03 +02:00
|
|
|
{
|
2021-04-13 11:34:59 +02:00
|
|
|
return $this->hasMany(Invoice::class, 'recurring_id', 'id')->withTrashed();
|
2019-10-22 13:27:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:47:26 +02:00
|
|
|
public function invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2019-04-23 14:22:13 +02:00
|
|
|
{
|
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
|
|
|
|
2023-08-02 11:47:26 +02:00
|
|
|
public function documents(): \Illuminate\Database\Eloquent\Relations\MorphMany
|
2020-09-17 23:49:22 +02:00
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
2021-04-01 16:38:46 +02:00
|
|
|
|
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()) {
|
2020-09-06 11:38:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-10 22:41:39 +01:00
|
|
|
public function calculateStatus()
|
|
|
|
{
|
|
|
|
|
|
|
|
if($this->remaining_cycles == 0) {
|
|
|
|
return self::STATUS_COMPLETED;
|
2024-01-14 05:05:00 +01:00
|
|
|
} elseif ($this->status_id == self::STATUS_ACTIVE && Carbon::parse($this->next_send_date)->isFuture()) {
|
2024-01-10 22:41:39 +01:00
|
|
|
return self::STATUS_PENDING;
|
2024-01-14 05:05:00 +01:00
|
|
|
} else {
|
2024-01-10 22:41:39 +01:00
|
|
|
return $this->status_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function nextSendDate(): ?Carbon
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->next_send_date_client) {
|
2020-09-27 11:22:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-16 04:14:15 +01:00
|
|
|
|
2021-06-10 23:34:03 +02:00
|
|
|
$offset = $this->client->timezone_offset();
|
2020-09-27 11:22:34 +02:00
|
|
|
|
2022-05-17 09:47:55 +02:00
|
|
|
/* If this setting is enabled, the recurring invoice may be set in the past */
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->company->stop_on_unpaid_recurring) {
|
2023-02-16 02:36:09 +01:00
|
|
|
/* Lets set the next send date to now so we increment from today, rather than in the past*/
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Carbon::parse($this->next_send_date)->lt(now()->subDays(3))) {
|
|
|
|
$this->next_send_date_client = now()->format('Y-m-d');
|
2022-05-17 09:47:55 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
switch ($this->frequency_id) {
|
2021-01-02 00:01:33 +01:00
|
|
|
case self::FREQUENCY_DAILY:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addDay()->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_WEEKLY:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeek()->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_WEEKS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeeks(2)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_FOUR_WEEKS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeeks(4)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_MONTHLY:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthNoOverflow()->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_MONTHS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(2)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_THREE_MONTHS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(3)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_FOUR_MONTHS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(4)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_SIX_MONTHS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(6)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_ANNUALLY:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addYear()->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_YEARS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addYears(2)->addSeconds($offset);
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_THREE_YEARS:
|
2022-06-02 05:49:29 +02:00
|
|
|
return Carbon::parse($this->next_send_date_client)->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
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function nextSendDateClient(): ?Carbon
|
2022-06-02 05:49:29 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->next_send_date_client) {
|
2022-06-02 05:49:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this setting is enabled, the recurring invoice may be set in the past */
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->company->stop_on_unpaid_recurring) {
|
2023-02-16 02:36:09 +01:00
|
|
|
/* Lets set the next send date to now so we increment from today, rather than in the past*/
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Carbon::parse($this->next_send_date)->lt(now()->subDays(3))) {
|
|
|
|
$this->next_send_date_client = now()->format('Y-m-d');
|
2022-06-02 05:49:29 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-06-02 05:49:29 +02:00
|
|
|
|
|
|
|
switch ($this->frequency_id) {
|
|
|
|
case self::FREQUENCY_DAILY:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addDay();
|
|
|
|
case self::FREQUENCY_WEEKLY:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeek();
|
|
|
|
case self::FREQUENCY_TWO_WEEKS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeeks(2);
|
|
|
|
case self::FREQUENCY_FOUR_WEEKS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addWeeks(4);
|
|
|
|
case self::FREQUENCY_MONTHLY:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthNoOverflow();
|
|
|
|
case self::FREQUENCY_TWO_MONTHS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(2);
|
|
|
|
case self::FREQUENCY_THREE_MONTHS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(3);
|
|
|
|
case self::FREQUENCY_FOUR_MONTHS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(4);
|
|
|
|
case self::FREQUENCY_SIX_MONTHS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addMonthsNoOverflow(6);
|
|
|
|
case self::FREQUENCY_ANNUALLY:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addYear();
|
|
|
|
case self::FREQUENCY_TWO_YEARS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addYears(2);
|
|
|
|
case self::FREQUENCY_THREE_YEARS:
|
|
|
|
return Carbon::parse($this->next_send_date_client)->startOfDay()->addYears(3);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-06-02 05:49:29 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public function remainingCycles(): int
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01: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-12-30 22:59:12 +01:00
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01: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) {
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_DRAFT:
|
2019-08-15 13:10:02 +02:00
|
|
|
return '<h4><span class="badge badge-light">'.ctrans('texts.draft').'</span></h4>';
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_PENDING:
|
2020-09-23 02:16:19 +02:00
|
|
|
return '<h4><span class="badge badge-primary">'.ctrans('texts.pending').'</span></h4>';
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_ACTIVE:
|
2020-09-23 02:16:19 +02:00
|
|
|
return '<h4><span class="badge badge-primary">'.ctrans('texts.active').'</span></h4>';
|
2020-09-06 11:38:10 +02:00
|
|
|
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>';
|
2020-09-23 02:16:19 +02:00
|
|
|
case self::STATUS_PAUSED:
|
|
|
|
return '<h4><span class="badge badge-danger">'.ctrans('texts.paused').'</span></h4>';
|
2019-08-15 13:10:02 +02:00
|
|
|
default:
|
2023-04-26 13:18:01 +02:00
|
|
|
return '<h4><span class="badge badge-primary">'.ctrans('texts.pending').'</span></h4>';
|
|
|
|
|
2019-08-15 13:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
public static function stringStatus(int $status)
|
|
|
|
{
|
|
|
|
switch ($status) {
|
|
|
|
case self::STATUS_DRAFT:
|
|
|
|
return ctrans('texts.draft');
|
|
|
|
case self::STATUS_PENDING:
|
|
|
|
return ctrans('texts.pending');
|
|
|
|
case self::STATUS_ACTIVE:
|
|
|
|
return ctrans('texts.active');
|
|
|
|
case self::STATUS_COMPLETED:
|
|
|
|
return ctrans('texts.status_completed');
|
|
|
|
case self::STATUS_PAUSED:
|
|
|
|
return ctrans('texts.paused');
|
|
|
|
default:
|
2023-04-26 13:18:01 +02:00
|
|
|
return ctrans('texts.pending');
|
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public static function frequencyForKey(int $frequency_id): string
|
2019-08-15 13:10:02 +02:00
|
|
|
{
|
|
|
|
switch ($frequency_id) {
|
2021-01-03 08:56:42 +01:00
|
|
|
case self::FREQUENCY_DAILY:
|
|
|
|
return ctrans('texts.freq_daily');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_WEEKLY:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_weekly');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_WEEKS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_two_weeks');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_FOUR_WEEKS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_four_weeks');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_MONTHLY:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_monthly');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_MONTHS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_two_months');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_THREE_MONTHS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_three_months');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_FOUR_MONTHS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_four_months');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_SIX_MONTHS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_six_months');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_ANNUALLY:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_annually');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::FREQUENCY_TWO_YEARS:
|
2019-08-15 13:10:02 +02:00
|
|
|
return ctrans('texts.freq_two_years');
|
2022-03-28 23:04:04 +02:00
|
|
|
case self::FREQUENCY_THREE_YEARS:
|
|
|
|
return ctrans('texts.freq_three_years');
|
2019-08-15 13:10:02 +02:00
|
|
|
default:
|
2022-03-28 23:04:04 +02:00
|
|
|
return '';
|
2019-08-15 13:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-01 13:33:42 +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
|
2020-09-13 12:20:11 +02:00
|
|
|
{
|
|
|
|
$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-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 ||
|
2022-06-21 11:57:17 +02:00
|
|
|
! $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
|
|
|
}
|
2021-04-01 16:38:46 +02:00
|
|
|
|
2020-12-30 23:18:29 +01:00
|
|
|
$data = [];
|
2021-04-01 16:38:46 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! Carbon::parse($this->next_send_date_client)) {
|
2021-01-01 23:51:13 +01:00
|
|
|
return $data;
|
2021-01-04 13:38:00 +01:00
|
|
|
}
|
2020-09-12 12:33:12 +02:00
|
|
|
|
2022-06-02 05:49:29 +02:00
|
|
|
$next_send_date = Carbon::parse($this->next_send_date_client)->copy();
|
2021-01-01 23:52:42 +01:00
|
|
|
|
2022-06-21 11:57:17 +02: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') : '';
|
2021-04-01 16:38:46 +02:00
|
|
|
|
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'),
|
2022-06-21 11:57:17 +02:00
|
|
|
'due_date' => $next_due_date_string,
|
2020-09-13 12:20:11 +02:00
|
|
|
];
|
2020-09-12 12:33:12 +02:00
|
|
|
|
2021-06-25 03:57:17 +02:00
|
|
|
/* Fixes the timeshift in case the offset is negative which cause a infinite loop due to UTC +0*/
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->client->timezone_offset() < 0) {
|
2021-06-25 03:57:17 +02:00
|
|
|
$next_send_date = $this->nextDateByFrequency($next_send_date->addDay()->format('Y-m-d'));
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-06-25 03:57:17 +02:00
|
|
|
$next_send_date = $this->nextDateByFrequency($next_send_date->format('Y-m-d'));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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':
|
2022-02-16 03:24:10 +01:00
|
|
|
case '':
|
2022-12-02 01:02:04 +01:00
|
|
|
case '0':
|
2020-09-14 13:11:46 +02:00
|
|
|
return $this->calculateDateFromTerms($date);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-06-02 17:23:10 +02:00
|
|
|
case 'on_receipt':
|
2022-09-08 03:30:40 +02:00
|
|
|
return Carbon::parse($date)->copy();
|
2022-02-16 03:24:10 +01:00
|
|
|
|
2020-09-14 13:11:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2022-06-21 11:57:17 +02:00
|
|
|
* @return null|Carbon The date
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
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
|
|
|
/**
|
2023-03-15 06:17:24 +01:00
|
|
|
* service
|
|
|
|
*
|
|
|
|
* @return RecurringService
|
2020-09-24 02:57:44 +02:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function service(): RecurringService
|
2020-09-24 02:57:44 +02:00
|
|
|
{
|
|
|
|
return new RecurringService($this);
|
|
|
|
}
|
2021-04-01 16:38:46 +02:00
|
|
|
|
|
|
|
public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Subscription::class);
|
|
|
|
}
|
2022-04-06 02:38:01 +02:00
|
|
|
|
|
|
|
public function translate_entity()
|
|
|
|
{
|
|
|
|
return ctrans('texts.recurring_invoice');
|
|
|
|
}
|
2019-04-23 14:22:13 +02:00
|
|
|
}
|