Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RecurringInvoice.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  4. use App\Casts\RateCast;
  5. use App\Collections\Accounting\DocumentCollection;
  6. use App\Enums\Accounting\AdjustmentComputation;
  7. use App\Enums\Accounting\DayOfMonth;
  8. use App\Enums\Accounting\DayOfWeek;
  9. use App\Enums\Accounting\DocumentDiscountMethod;
  10. use App\Enums\Accounting\DocumentType;
  11. use App\Enums\Accounting\EndType;
  12. use App\Enums\Accounting\Frequency;
  13. use App\Enums\Accounting\IntervalType;
  14. use App\Enums\Accounting\InvoiceStatus;
  15. use App\Enums\Accounting\Month;
  16. use App\Enums\Accounting\RecurringInvoiceStatus;
  17. use App\Enums\Setting\PaymentTerms;
  18. use App\Filament\Forms\Components\CustomSection;
  19. use App\Models\Common\Client;
  20. use App\Models\Setting\CompanyProfile;
  21. use App\Observers\RecurringInvoiceObserver;
  22. use App\Utilities\Localization\Timezone;
  23. use Filament\Actions\Action;
  24. use Filament\Actions\MountableAction;
  25. use Filament\Forms;
  26. use Filament\Forms\Form;
  27. use Guava\FilamentClusters\Forms\Cluster;
  28. use Illuminate\Database\Eloquent\Attributes\CollectedBy;
  29. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  30. use Illuminate\Database\Eloquent\Model;
  31. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  32. use Illuminate\Database\Eloquent\Relations\HasMany;
  33. use Illuminate\Support\Carbon;
  34. #[CollectedBy(DocumentCollection::class)]
  35. #[ObservedBy(RecurringInvoiceObserver::class)]
  36. class RecurringInvoice extends Document
  37. {
  38. protected $table = 'recurring_invoices';
  39. protected $fillable = [
  40. 'company_id',
  41. 'client_id',
  42. 'logo',
  43. 'header',
  44. 'subheader',
  45. 'order_number',
  46. 'payment_terms',
  47. 'approved_at',
  48. 'ended_at',
  49. 'frequency',
  50. 'interval_type',
  51. 'interval_value',
  52. 'month',
  53. 'day_of_month',
  54. 'day_of_week',
  55. 'start_date',
  56. 'end_type',
  57. 'max_occurrences',
  58. 'end_date',
  59. 'occurrences_count',
  60. 'timezone',
  61. 'next_date',
  62. 'last_date',
  63. 'auto_send',
  64. 'send_time',
  65. 'status',
  66. 'currency_code',
  67. 'discount_method',
  68. 'discount_computation',
  69. 'discount_rate',
  70. 'subtotal',
  71. 'tax_total',
  72. 'discount_total',
  73. 'total',
  74. 'terms',
  75. 'footer',
  76. 'created_by',
  77. 'updated_by',
  78. ];
  79. protected $casts = [
  80. 'approved_at' => 'datetime',
  81. 'ended_at' => 'datetime',
  82. 'start_date' => 'date',
  83. 'end_date' => 'date',
  84. 'next_date' => 'date',
  85. 'last_date' => 'date',
  86. 'auto_send' => 'boolean',
  87. 'send_time' => 'datetime:H:i',
  88. 'payment_terms' => PaymentTerms::class,
  89. 'frequency' => Frequency::class,
  90. 'interval_type' => IntervalType::class,
  91. 'interval_value' => 'integer',
  92. 'month' => Month::class,
  93. 'day_of_month' => DayOfMonth::class,
  94. 'day_of_week' => DayOfWeek::class,
  95. 'end_type' => EndType::class,
  96. 'status' => RecurringInvoiceStatus::class,
  97. 'discount_method' => DocumentDiscountMethod::class,
  98. 'discount_computation' => AdjustmentComputation::class,
  99. 'discount_rate' => RateCast::class,
  100. 'subtotal' => MoneyCast::class,
  101. 'tax_total' => MoneyCast::class,
  102. 'discount_total' => MoneyCast::class,
  103. 'total' => MoneyCast::class,
  104. ];
  105. public function client(): BelongsTo
  106. {
  107. return $this->belongsTo(Client::class);
  108. }
  109. public function invoices(): HasMany
  110. {
  111. return $this->hasMany(Invoice::class, 'recurring_invoice_id');
  112. }
  113. public function documentType(): DocumentType
  114. {
  115. return DocumentType::RecurringInvoice;
  116. }
  117. public function documentNumber(): ?string
  118. {
  119. return 'Auto-generated';
  120. }
  121. public function documentDate(): ?string
  122. {
  123. return $this->calculateNextDate()?->toDefaultDateFormat() ?? 'Auto-generated';
  124. }
  125. public function dueDate(): ?string
  126. {
  127. return $this->calculateNextDueDate()?->toDefaultDateFormat() ?? 'Auto-generated';
  128. }
  129. public function referenceNumber(): ?string
  130. {
  131. return $this->order_number;
  132. }
  133. public function amountDue(): ?string
  134. {
  135. return $this->total;
  136. }
  137. public function isDraft(): bool
  138. {
  139. return $this->status === RecurringInvoiceStatus::Draft;
  140. }
  141. public function isActive(): bool
  142. {
  143. return $this->status === RecurringInvoiceStatus::Active;
  144. }
  145. public function wasApproved(): bool
  146. {
  147. return $this->approved_at !== null;
  148. }
  149. public function wasEnded(): bool
  150. {
  151. return $this->ended_at !== null;
  152. }
  153. public function isNeverEnding(): bool
  154. {
  155. return $this->end_type === EndType::Never;
  156. }
  157. public function canBeApproved(): bool
  158. {
  159. return $this->isDraft() && $this->hasSchedule() && ! $this->wasApproved();
  160. }
  161. public function canBeEnded(): bool
  162. {
  163. return $this->isActive() && ! $this->wasEnded();
  164. }
  165. public function hasSchedule(): bool
  166. {
  167. return $this->start_date !== null;
  168. }
  169. public function getScheduleDescription(): string
  170. {
  171. $frequency = $this->frequency;
  172. return match (true) {
  173. $frequency->isDaily() => 'Repeat daily',
  174. $frequency->isWeekly() && $this->day_of_week => "Repeat weekly every {$this->day_of_week->getLabel()}",
  175. $frequency->isMonthly() && $this->day_of_month => "Repeat monthly on the {$this->day_of_month->getLabel()} day",
  176. $frequency->isYearly() && $this->month && $this->day_of_month => "Repeat yearly on {$this->month->getLabel()} {$this->day_of_month->getLabel()}",
  177. $frequency->isCustom() => $this->getCustomScheduleDescription(),
  178. default => 'Not Configured',
  179. };
  180. }
  181. private function getCustomScheduleDescription(): string
  182. {
  183. $interval = $this->interval_value > 1
  184. ? "{$this->interval_value} {$this->interval_type->getPluralLabel()}"
  185. : $this->interval_type->getSingularLabel();
  186. $dayDescription = match (true) {
  187. $this->interval_type->isWeek() && $this->day_of_week => " on {$this->day_of_week->getLabel()}",
  188. $this->interval_type->isMonth() && $this->day_of_month => " on the {$this->day_of_month->getLabel()} day",
  189. $this->interval_type->isYear() && $this->month && $this->day_of_month => " on {$this->month->getLabel()} {$this->day_of_month->getLabel()}",
  190. default => ''
  191. };
  192. return "Repeat every {$interval}{$dayDescription}";
  193. }
  194. public function getEndDescription(): string
  195. {
  196. if (! $this->end_type) {
  197. return 'Not configured';
  198. }
  199. return match (true) {
  200. $this->end_type->isNever() => 'Never',
  201. $this->end_type->isAfter() && $this->max_occurrences => "After {$this->max_occurrences} " . str($this->max_occurrences === 1 ? 'invoice' : 'invoices'),
  202. $this->end_type->isOn() && $this->end_date => 'On ' . $this->end_date->toDefaultDateFormat(),
  203. default => 'Not configured'
  204. };
  205. }
  206. public function getTimelineDescription(): string
  207. {
  208. $parts = [];
  209. if ($this->start_date) {
  210. $parts[] = 'First Invoice: ' . $this->start_date->toDefaultDateFormat();
  211. }
  212. if ($this->end_type) {
  213. $parts[] = 'Ends: ' . $this->getEndDescription();
  214. }
  215. return implode(', ', $parts);
  216. }
  217. public function calculateNextDate(?Carbon $lastDate = null): ?Carbon
  218. {
  219. $lastDate ??= $this->last_date;
  220. if (! $lastDate && $this->start_date) {
  221. return $this->start_date;
  222. }
  223. if (! $lastDate) {
  224. return null;
  225. }
  226. $nextDate = match (true) {
  227. $this->frequency->isDaily() => $lastDate->addDay(),
  228. $this->frequency->isWeekly() => $this->calculateNextWeeklyDate($lastDate),
  229. $this->frequency->isMonthly() => $this->calculateNextMonthlyDate($lastDate),
  230. $this->frequency->isYearly() => $this->calculateNextYearlyDate($lastDate),
  231. $this->frequency->isCustom() => $this->calculateCustomNextDate($lastDate),
  232. default => null
  233. };
  234. if (! $nextDate || $this->hasReachedEnd($nextDate)) {
  235. return null;
  236. }
  237. return $nextDate;
  238. }
  239. public function calculateNextWeeklyDate(Carbon $lastDate): ?Carbon
  240. {
  241. return $lastDate->copy()->next($this->day_of_week->value);
  242. }
  243. public function calculateNextMonthlyDate(Carbon $lastDate): ?Carbon
  244. {
  245. return match (true) {
  246. $lastDate->equalTo($this->start_date) => $lastDate->copy()->day(
  247. min($this->day_of_month->value, $lastDate->daysInMonth)
  248. ),
  249. default => $lastDate->copy()->addMonth()->day(
  250. min($this->day_of_month->value, $lastDate->copy()->addMonth()->daysInMonth)
  251. ),
  252. };
  253. }
  254. public function calculateNextYearlyDate(Carbon $lastDate): ?Carbon
  255. {
  256. return match (true) {
  257. $lastDate->equalTo($this->start_date) => $lastDate->copy()
  258. ->month($this->month->value)
  259. ->day(min($this->day_of_month->value, $lastDate->daysInMonth)),
  260. default => $lastDate->copy()
  261. ->addYear()
  262. ->month($this->month->value)
  263. ->day(min($this->day_of_month->value, $lastDate->copy()->addYear()->month($this->month->value)->daysInMonth))
  264. };
  265. }
  266. protected function calculateCustomNextDate(Carbon $lastDate): ?Carbon
  267. {
  268. $interval = $this->interval_value ?? 1;
  269. return match ($this->interval_type) {
  270. IntervalType::Day => $lastDate->copy()->addDays($interval),
  271. IntervalType::Week => match (true) {
  272. $lastDate->equalTo($this->start_date) => $lastDate->copy()->next($this->day_of_week->value),
  273. $lastDate->dayOfWeek === $this->day_of_week->value => $lastDate->copy()->addWeeks($interval),
  274. default => $lastDate->copy()->next($this->day_of_week->value),
  275. },
  276. IntervalType::Month => match (true) {
  277. $lastDate->equalTo($this->start_date) => $lastDate->copy()->day(
  278. min($this->day_of_month->value, $lastDate->daysInMonth)
  279. ),
  280. default => $lastDate->copy()->addMonths($interval)->day(
  281. min($this->day_of_month->value, $lastDate->copy()->addMonths($interval)->daysInMonth)
  282. ),
  283. },
  284. IntervalType::Year => match (true) {
  285. $lastDate->equalTo($this->start_date) => $lastDate->copy()
  286. ->month($this->month->value)
  287. ->day(min($this->day_of_month->value, $lastDate->daysInMonth)),
  288. default => $lastDate->copy()
  289. ->addYears($interval)
  290. ->month($this->month->value)
  291. ->day(min($this->day_of_month->value, $lastDate->copy()->addYears($interval)->month($this->month->value)->daysInMonth))
  292. },
  293. default => null
  294. };
  295. }
  296. public function calculateNextDueDate(): ?Carbon
  297. {
  298. if (! $nextDate = $this->calculateNextDate()) {
  299. return null;
  300. }
  301. if (! $terms = $this->payment_terms) {
  302. return $nextDate;
  303. }
  304. return $nextDate->copy()->addDays($terms->getDays());
  305. }
  306. public function hasReachedEnd(?Carbon $nextDate = null): bool
  307. {
  308. if (! $this->end_type) {
  309. return false;
  310. }
  311. return match (true) {
  312. $this->end_type->isNever() => false,
  313. $this->end_type->isAfter() => ($this->occurrences_count ?? 0) >= ($this->max_occurrences ?? 0),
  314. $this->end_type->isOn() && $this->end_date && $nextDate => $nextDate->greaterThan($this->end_date),
  315. default => false
  316. };
  317. }
  318. public static function getUpdateScheduleAction(string $action = Action::class): MountableAction
  319. {
  320. return $action::make('updateSchedule')
  321. ->label(fn (self $record) => $record->hasSchedule() ? 'Update Schedule' : 'Set Schedule')
  322. ->icon('heroicon-o-calendar-date-range')
  323. ->slideOver()
  324. ->successNotificationTitle('Schedule Updated')
  325. ->mountUsing(function (self $record, Form $form) {
  326. $data = $record->attributesToArray();
  327. $data['day_of_month'] ??= DayOfMonth::First;
  328. $data['start_date'] ??= now()->addMonth()->startOfMonth();
  329. $form->fill($data);
  330. })
  331. ->form([
  332. CustomSection::make('Frequency')
  333. ->contained(false)
  334. ->schema([
  335. Forms\Components\Select::make('frequency')
  336. ->label('Repeats')
  337. ->options(Frequency::class)
  338. ->softRequired()
  339. ->live()
  340. ->afterStateUpdated(function (Forms\Set $set, $state) {
  341. $frequency = Frequency::parse($state);
  342. if ($frequency->isDaily()) {
  343. $set('interval_value', null);
  344. $set('interval_type', null);
  345. }
  346. if ($frequency->isWeekly()) {
  347. $currentDayOfWeek = now()->dayOfWeek;
  348. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  349. $set('day_of_week', $currentDayOfWeek);
  350. $set('interval_value', null);
  351. $set('interval_type', null);
  352. }
  353. if ($frequency->isMonthly()) {
  354. $set('day_of_month', DayOfMonth::First);
  355. $set('interval_value', null);
  356. $set('interval_type', null);
  357. }
  358. if ($frequency->isYearly()) {
  359. $currentMonth = now()->month;
  360. $currentMonth = Month::parse($currentMonth);
  361. $set('month', $currentMonth);
  362. $currentDay = now()->dayOfMonth;
  363. $currentDay = DayOfMonth::parse($currentDay);
  364. $set('day_of_month', $currentDay);
  365. $set('interval_value', null);
  366. $set('interval_type', null);
  367. }
  368. if ($frequency->isCustom()) {
  369. $set('interval_value', 1);
  370. $set('interval_type', IntervalType::Month);
  371. $currentDay = now()->dayOfMonth;
  372. $currentDay = DayOfMonth::parse($currentDay);
  373. $set('day_of_month', $currentDay);
  374. }
  375. }),
  376. // Custom frequency fields in a nested grid
  377. Cluster::make([
  378. Forms\Components\TextInput::make('interval_value')
  379. ->softRequired()
  380. ->numeric()
  381. ->default(1),
  382. Forms\Components\Select::make('interval_type')
  383. ->options(IntervalType::class)
  384. ->softRequired()
  385. ->default(IntervalType::Month)
  386. ->live()
  387. ->afterStateUpdated(function (Forms\Set $set, $state) {
  388. $intervalType = IntervalType::parse($state);
  389. if ($intervalType->isWeek()) {
  390. $currentDayOfWeek = now()->dayOfWeek;
  391. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  392. $set('day_of_week', $currentDayOfWeek);
  393. }
  394. if ($intervalType->isMonth()) {
  395. $currentDay = now()->dayOfMonth;
  396. $currentDay = DayOfMonth::parse($currentDay);
  397. $set('day_of_month', $currentDay);
  398. }
  399. if ($intervalType->isYear()) {
  400. $currentMonth = now()->month;
  401. $currentMonth = Month::parse($currentMonth);
  402. $set('month', $currentMonth);
  403. $currentDay = now()->dayOfMonth;
  404. $currentDay = DayOfMonth::parse($currentDay);
  405. $set('day_of_month', $currentDay);
  406. }
  407. }),
  408. ])
  409. ->live()
  410. ->label('Every')
  411. ->required()
  412. ->markAsRequired(false)
  413. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isCustom()),
  414. // Specific schedule details
  415. Forms\Components\Select::make('month')
  416. ->label('Month')
  417. ->options(Month::class)
  418. ->softRequired()
  419. ->visible(
  420. fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isYearly() ||
  421. IntervalType::parse($get('interval_type'))?->isYear()
  422. )
  423. ->live()
  424. ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
  425. $dayOfMonth = DayOfMonth::parse($get('day_of_month'));
  426. $frequency = Frequency::parse($get('frequency'));
  427. $intervalType = IntervalType::parse($get('interval_type'));
  428. $month = Month::parse($state);
  429. if (($frequency->isYearly() || $intervalType?->isYear()) && $month && $dayOfMonth) {
  430. $date = $dayOfMonth->resolveDate(today()->month($month->value))->toImmutable();
  431. $adjustedStartDate = $date->lt(today())
  432. ? $dayOfMonth->resolveDate($date->addYear()->month($month->value))
  433. : $dayOfMonth->resolveDate($date->month($month->value));
  434. $adjustedDay = min($dayOfMonth->value, $adjustedStartDate->daysInMonth);
  435. $set('day_of_month', $adjustedDay);
  436. $set('start_date', $adjustedStartDate);
  437. }
  438. }),
  439. Forms\Components\Select::make('day_of_month')
  440. ->label('Day of Month')
  441. ->options(function (Forms\Get $get) {
  442. $month = Month::parse($get('month')) ?? Month::January;
  443. $daysInMonth = Carbon::createFromDate(null, $month->value)->daysInMonth;
  444. return collect(DayOfMonth::cases())
  445. ->filter(static fn (DayOfMonth $dayOfMonth) => $dayOfMonth->value <= $daysInMonth || $dayOfMonth->isLast())
  446. ->mapWithKeys(fn (DayOfMonth $dayOfMonth) => [$dayOfMonth->value => $dayOfMonth->getLabel()]);
  447. })
  448. ->softRequired()
  449. ->visible(
  450. fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isMonthly() ||
  451. Frequency::parse($get('frequency'))?->isYearly() ||
  452. IntervalType::parse($get('interval_type'))?->isMonth() ||
  453. IntervalType::parse($get('interval_type'))?->isYear()
  454. )
  455. ->live()
  456. ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
  457. $dayOfMonth = DayOfMonth::parse($state);
  458. $frequency = Frequency::parse($get('frequency'));
  459. $intervalType = IntervalType::parse($get('interval_type'));
  460. $month = Month::parse($get('month'));
  461. if (($frequency->isMonthly() || $intervalType?->isMonth()) && $dayOfMonth) {
  462. $date = $dayOfMonth->resolveDate(today())->toImmutable();
  463. $adjustedStartDate = $date->lt(today())
  464. ? $dayOfMonth->resolveDate($date->addMonth())
  465. : $dayOfMonth->resolveDate($date);
  466. $set('start_date', $adjustedStartDate);
  467. }
  468. if (($frequency->isYearly() || $intervalType?->isYear()) && $month && $dayOfMonth) {
  469. $date = $dayOfMonth->resolveDate(today()->month($month->value))->toImmutable();
  470. $adjustedStartDate = $date->lt(today())
  471. ? $dayOfMonth->resolveDate($date->addYear()->month($month->value))
  472. : $dayOfMonth->resolveDate($date->month($month->value));
  473. $set('start_date', $adjustedStartDate);
  474. }
  475. }),
  476. Forms\Components\Select::make('day_of_week')
  477. ->label('Day of Week')
  478. ->options(DayOfWeek::class)
  479. ->softRequired()
  480. ->visible(
  481. fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isWeekly() ||
  482. IntervalType::parse($get('interval_type'))?->isWeek()
  483. )
  484. ->live()
  485. ->afterStateUpdated(function (Forms\Set $set, $state) {
  486. $dayOfWeek = DayOfWeek::parse($state);
  487. $adjustedStartDate = today()->is($dayOfWeek->name)
  488. ? today()
  489. : today()->next($dayOfWeek->name);
  490. $set('start_date', $adjustedStartDate);
  491. }),
  492. ])->columns(2),
  493. CustomSection::make('Dates & Time')
  494. ->contained(false)
  495. ->schema([
  496. Forms\Components\DatePicker::make('start_date')
  497. ->label('First Invoice Date')
  498. ->softRequired()
  499. ->live()
  500. ->minDate(today())
  501. ->closeOnDateSelection()
  502. ->afterStateUpdated(function (Forms\Set $set, $state) {
  503. $startDate = Carbon::parse($state);
  504. $dayOfWeek = DayOfWeek::parse($startDate->dayOfWeek);
  505. $set('day_of_week', $dayOfWeek);
  506. }),
  507. Forms\Components\Group::make(function (Forms\Get $get) {
  508. $components = [];
  509. $components[] = Forms\Components\Select::make('end_type')
  510. ->label('End Schedule')
  511. ->options(EndType::class)
  512. ->softRequired()
  513. ->live()
  514. ->afterStateUpdated(function (Forms\Set $set, $state) {
  515. $endType = EndType::parse($state);
  516. if ($endType?->isNever()) {
  517. $set('max_occurrences', null);
  518. $set('end_date', null);
  519. }
  520. if ($endType?->isAfter()) {
  521. $set('max_occurrences', 1);
  522. $set('end_date', null);
  523. }
  524. if ($endType?->isOn()) {
  525. $set('max_occurrences', null);
  526. $set('end_date', now()->addMonth()->startOfMonth());
  527. }
  528. });
  529. $endType = EndType::parse($get('end_type'));
  530. if ($endType?->isAfter()) {
  531. $components[] = Forms\Components\TextInput::make('max_occurrences')
  532. ->numeric()
  533. ->suffix('invoices')
  534. ->live();
  535. }
  536. if ($endType?->isOn()) {
  537. $components[] = Forms\Components\DatePicker::make('end_date')
  538. ->live();
  539. }
  540. return [
  541. Cluster::make($components)
  542. ->label('Schedule Ends')
  543. ->required()
  544. ->markAsRequired(false),
  545. ];
  546. }),
  547. Forms\Components\Select::make('timezone')
  548. ->options(Timezone::getTimezoneOptions(CompanyProfile::first()->country))
  549. ->searchable()
  550. ->softRequired(),
  551. ])
  552. ->columns(2),
  553. ])
  554. ->action(function (self $record, array $data, MountableAction $action) {
  555. $record->update($data);
  556. $action->success();
  557. });
  558. }
  559. public static function getApproveDraftAction(string $action = Action::class): MountableAction
  560. {
  561. return $action::make('approveDraft')
  562. ->label('Approve')
  563. ->icon('heroicon-o-check-circle')
  564. ->visible(function (self $record) {
  565. return $record->canBeApproved();
  566. })
  567. ->databaseTransaction()
  568. ->successNotificationTitle('Recurring Invoice Approved')
  569. ->action(function (self $record, MountableAction $action) {
  570. $record->approveDraft();
  571. $action->success();
  572. });
  573. }
  574. public function approveDraft(?Carbon $approvedAt = null): void
  575. {
  576. if (! $this->isDraft()) {
  577. throw new \RuntimeException('Invoice is not in draft status.');
  578. }
  579. $approvedAt ??= now();
  580. $this->update([
  581. 'approved_at' => $approvedAt,
  582. 'status' => RecurringInvoiceStatus::Active,
  583. ]);
  584. }
  585. public function generateInvoice(): ?Invoice
  586. {
  587. if (! $this->shouldGenerateInvoice()) {
  588. return null;
  589. }
  590. $nextDate = $this->next_date ?? $this->calculateNextDate();
  591. if (! $nextDate) {
  592. return null;
  593. }
  594. $dueDate = $this->calculateNextDueDate();
  595. $invoice = $this->invoices()->create([
  596. 'company_id' => $this->company_id,
  597. 'client_id' => $this->client_id,
  598. 'logo' => $this->logo,
  599. 'header' => $this->header,
  600. 'subheader' => $this->subheader,
  601. 'invoice_number' => Invoice::getNextDocumentNumber($this->company),
  602. 'date' => $nextDate,
  603. 'due_date' => $dueDate,
  604. 'status' => InvoiceStatus::Draft,
  605. 'currency_code' => $this->currency_code,
  606. 'discount_method' => $this->discount_method,
  607. 'discount_computation' => $this->discount_computation,
  608. 'discount_rate' => $this->discount_rate,
  609. 'subtotal' => $this->subtotal,
  610. 'tax_total' => $this->tax_total,
  611. 'discount_total' => $this->discount_total,
  612. 'total' => $this->total,
  613. 'terms' => $this->terms,
  614. 'footer' => $this->footer,
  615. 'created_by' => auth()->id(),
  616. 'updated_by' => auth()->id(),
  617. ]);
  618. $this->replicateLineItems($invoice);
  619. $this->update([
  620. 'last_date' => $nextDate,
  621. 'next_date' => $this->calculateNextDate($nextDate),
  622. 'occurrences_count' => ($this->occurrences_count ?? 0) + 1,
  623. ]);
  624. return $invoice;
  625. }
  626. public function replicateLineItems(Model $target): void
  627. {
  628. $this->lineItems->each(function (DocumentLineItem $lineItem) use ($target) {
  629. $replica = $lineItem->replicate([
  630. 'documentable_id',
  631. 'documentable_type',
  632. 'subtotal',
  633. 'total',
  634. 'created_by',
  635. 'updated_by',
  636. 'created_at',
  637. 'updated_at',
  638. ]);
  639. $replica->documentable_id = $target->id;
  640. $replica->documentable_type = $target->getMorphClass();
  641. $replica->save();
  642. $replica->adjustments()->sync($lineItem->adjustments->pluck('id'));
  643. });
  644. }
  645. public function shouldGenerateInvoice(): bool
  646. {
  647. if (! $this->isActive() || $this->hasReachedEnd()) {
  648. return false;
  649. }
  650. $nextDate = $this->calculateNextDate();
  651. if (! $nextDate || $nextDate->startOfDay()->isFuture()) {
  652. return false;
  653. }
  654. return true;
  655. }
  656. public function generateDueInvoices(): void
  657. {
  658. $maxIterations = 100;
  659. for ($i = 0; $i < $maxIterations; $i++) {
  660. $result = $this->generateInvoice();
  661. if (! $result) {
  662. break;
  663. }
  664. $this->refresh();
  665. }
  666. }
  667. }