Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

RecurringInvoice.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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\Month;
  15. use App\Enums\Accounting\RecurringInvoiceStatus;
  16. use App\Enums\Setting\PaymentTerms;
  17. use App\Filament\Forms\Components\CustomSection;
  18. use App\Models\Common\Client;
  19. use App\Models\Setting\CompanyProfile;
  20. use App\Observers\RecurringInvoiceObserver;
  21. use App\Utilities\Localization\Timezone;
  22. use Filament\Actions\Action;
  23. use Filament\Actions\MountableAction;
  24. use Filament\Forms;
  25. use Filament\Forms\Form;
  26. use Guava\FilamentClusters\Forms\Cluster;
  27. use Illuminate\Database\Eloquent\Attributes\CollectedBy;
  28. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  29. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  30. use Illuminate\Database\Eloquent\Relations\HasMany;
  31. use Illuminate\Support\Carbon;
  32. #[CollectedBy(DocumentCollection::class)]
  33. #[ObservedBy(RecurringInvoiceObserver::class)]
  34. class RecurringInvoice extends Document
  35. {
  36. protected $table = 'recurring_invoices';
  37. protected $fillable = [
  38. 'company_id',
  39. 'client_id',
  40. 'logo',
  41. 'header',
  42. 'subheader',
  43. 'order_number',
  44. 'payment_terms',
  45. 'approved_at',
  46. 'ended_at',
  47. 'frequency',
  48. 'interval_type',
  49. 'interval_value',
  50. 'month',
  51. 'day_of_month',
  52. 'day_of_week',
  53. 'start_date',
  54. 'end_type',
  55. 'max_occurrences',
  56. 'end_date',
  57. 'occurrences_count',
  58. 'timezone',
  59. 'next_date',
  60. 'last_date',
  61. 'auto_send',
  62. 'send_time',
  63. 'status',
  64. 'currency_code',
  65. 'discount_method',
  66. 'discount_computation',
  67. 'discount_rate',
  68. 'subtotal',
  69. 'tax_total',
  70. 'discount_total',
  71. 'total',
  72. 'terms',
  73. 'footer',
  74. 'created_by',
  75. 'updated_by',
  76. ];
  77. protected $casts = [
  78. 'approved_at' => 'datetime',
  79. 'ended_at' => 'datetime',
  80. 'start_date' => 'date',
  81. 'end_date' => 'date',
  82. 'next_date' => 'date',
  83. 'last_date' => 'date',
  84. 'auto_send' => 'boolean',
  85. 'send_time' => 'datetime:H:i',
  86. 'payment_terms' => PaymentTerms::class,
  87. 'frequency' => Frequency::class,
  88. 'interval_type' => IntervalType::class,
  89. 'month' => Month::class,
  90. 'day_of_month' => DayOfMonth::class,
  91. 'day_of_week' => DayOfWeek::class,
  92. 'end_type' => EndType::class,
  93. 'status' => RecurringInvoiceStatus::class,
  94. 'discount_method' => DocumentDiscountMethod::class,
  95. 'discount_computation' => AdjustmentComputation::class,
  96. 'discount_rate' => RateCast::class,
  97. 'subtotal' => MoneyCast::class,
  98. 'tax_total' => MoneyCast::class,
  99. 'discount_total' => MoneyCast::class,
  100. 'total' => MoneyCast::class,
  101. ];
  102. public function client(): BelongsTo
  103. {
  104. return $this->belongsTo(Client::class);
  105. }
  106. public function invoices(): HasMany
  107. {
  108. return $this->hasMany(Invoice::class, 'recurring_invoice_id');
  109. }
  110. public function documentType(): DocumentType
  111. {
  112. return DocumentType::RecurringInvoice;
  113. }
  114. public function documentNumber(): ?string
  115. {
  116. return 'Auto-generated';
  117. }
  118. public function documentDate(): ?string
  119. {
  120. return $this->calculateNextDate()?->toDefaultDateFormat() ?? 'Auto-generated';
  121. }
  122. public function dueDate(): ?string
  123. {
  124. return $this->calculateNextDueDate()?->toDefaultDateFormat() ?? 'Auto-generated';
  125. }
  126. public function referenceNumber(): ?string
  127. {
  128. return $this->order_number;
  129. }
  130. public function amountDue(): ?string
  131. {
  132. return $this->total;
  133. }
  134. public function isDraft(): bool
  135. {
  136. return $this->status === RecurringInvoiceStatus::Draft;
  137. }
  138. public function isActive(): bool
  139. {
  140. return $this->status === RecurringInvoiceStatus::Active;
  141. }
  142. public function wasApproved(): bool
  143. {
  144. return $this->approved_at !== null;
  145. }
  146. public function wasEnded(): bool
  147. {
  148. return $this->ended_at !== null;
  149. }
  150. public function isNeverEnding(): bool
  151. {
  152. return $this->end_type === EndType::Never;
  153. }
  154. public function canBeApproved(): bool
  155. {
  156. return $this->isDraft() && $this->hasSchedule() && ! $this->wasApproved();
  157. }
  158. public function canBeEnded(): bool
  159. {
  160. return $this->isActive() && ! $this->wasEnded();
  161. }
  162. public function hasSchedule(): bool
  163. {
  164. return $this->start_date !== null;
  165. }
  166. public function getScheduleDescription(): string
  167. {
  168. $frequency = $this->frequency;
  169. return match (true) {
  170. $frequency->isDaily() => 'Repeat daily',
  171. $frequency->isWeekly() && $this->day_of_week => "Repeat weekly every {$this->day_of_week->getLabel()}",
  172. $frequency->isMonthly() && $this->day_of_month => "Repeat monthly on the {$this->day_of_month->getLabel()} day",
  173. $frequency->isYearly() && $this->month && $this->day_of_month => "Repeat yearly on {$this->month->getLabel()} {$this->day_of_month->getLabel()}",
  174. $frequency->isCustom() => $this->getCustomScheduleDescription(),
  175. default => 'Not Configured',
  176. };
  177. }
  178. private function getCustomScheduleDescription(): string
  179. {
  180. $interval = $this->interval_value > 1
  181. ? "{$this->interval_value} {$this->interval_type->getPluralLabel()}"
  182. : $this->interval_type->getSingularLabel();
  183. $dayDescription = match (true) {
  184. $this->interval_type->isWeek() && $this->day_of_week => " on {$this->day_of_week->getLabel()}",
  185. $this->interval_type->isMonth() && $this->day_of_month => " on the {$this->day_of_month->getLabel()} day",
  186. $this->interval_type->isYear() && $this->month && $this->day_of_month => " on {$this->month->getLabel()} {$this->day_of_month->getLabel()}",
  187. default => ''
  188. };
  189. return "Repeat every {$interval}{$dayDescription}";
  190. }
  191. /**
  192. * Get a human-readable description of when the schedule ends.
  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. /**
  207. * Get the schedule timeline description.
  208. */
  209. public function getTimelineDescription(): string
  210. {
  211. $parts = [];
  212. if ($this->start_date) {
  213. $parts[] = 'First Invoice: ' . $this->start_date->toDefaultDateFormat();
  214. }
  215. if ($this->end_type) {
  216. $parts[] = 'Ends: ' . $this->getEndDescription();
  217. }
  218. return implode(', ', $parts);
  219. }
  220. /**
  221. * Get next occurrence date based on the schedule.
  222. */
  223. public function calculateNextDate(): ?Carbon
  224. {
  225. $lastDate = $this->last_date ?? $this->start_date;
  226. if (! $lastDate) {
  227. return null;
  228. }
  229. $nextDate = match (true) {
  230. $this->frequency->isDaily() => $lastDate->addDay(),
  231. $this->frequency->isWeekly() => $lastDate->addWeek(),
  232. $this->frequency->isMonthly() => $lastDate->addMonth(),
  233. $this->frequency->isYearly() => $lastDate->addYear(),
  234. $this->frequency->isCustom() => $this->calculateCustomNextDate($lastDate),
  235. default => null
  236. };
  237. // Check if we've reached the end
  238. if ($this->hasReachedEnd($nextDate)) {
  239. return null;
  240. }
  241. return $nextDate;
  242. }
  243. public function calculateNextDueDate(): ?Carbon
  244. {
  245. $nextDate = $this->calculateNextDate();
  246. if (! $nextDate) {
  247. return null;
  248. }
  249. $terms = $this->payment_terms;
  250. if (! $terms) {
  251. return $nextDate;
  252. }
  253. return $nextDate->addDays($terms->getDays());
  254. }
  255. /**
  256. * Calculate next date for custom intervals
  257. */
  258. protected function calculateCustomNextDate(Carbon $lastDate): ?\Carbon\Carbon
  259. {
  260. $value = $this->interval_value ?? 1;
  261. return match ($this->interval_type) {
  262. IntervalType::Day => $lastDate->addDays($value),
  263. IntervalType::Week => $lastDate->addWeeks($value),
  264. IntervalType::Month => $lastDate->addMonths($value),
  265. IntervalType::Year => $lastDate->addYears($value),
  266. default => null
  267. };
  268. }
  269. /**
  270. * Check if the schedule has reached its end
  271. */
  272. public function hasReachedEnd(?Carbon $nextDate = null): bool
  273. {
  274. if (! $this->end_type) {
  275. return false;
  276. }
  277. return match (true) {
  278. $this->end_type->isNever() => false,
  279. $this->end_type->isAfter() => ($this->occurrences_count ?? 0) >= ($this->max_occurrences ?? 0),
  280. $this->end_type->isOn() && $this->end_date && $nextDate => $nextDate->greaterThan($this->end_date),
  281. default => false
  282. };
  283. }
  284. public static function getUpdateScheduleAction(string $action = Action::class): MountableAction
  285. {
  286. return $action::make('updateSchedule')
  287. ->label(fn (self $record) => $record->hasSchedule() ? 'Update Schedule' : 'Set Schedule')
  288. ->icon('heroicon-o-calendar-date-range')
  289. ->slideOver()
  290. ->successNotificationTitle('Schedule Updated')
  291. ->mountUsing(function (self $record, Form $form) {
  292. $data = $record->attributesToArray();
  293. $data['day_of_month'] ??= DayOfMonth::First;
  294. $data['start_date'] ??= now()->addMonth()->startOfMonth();
  295. $form->fill($data);
  296. })
  297. ->form([
  298. CustomSection::make('Frequency')
  299. ->contained(false)
  300. ->schema([
  301. Forms\Components\Select::make('frequency')
  302. ->label('Repeats')
  303. ->options(Frequency::class)
  304. ->softRequired()
  305. ->live()
  306. ->afterStateUpdated(function (Forms\Set $set, $state) {
  307. $frequency = Frequency::parse($state);
  308. if ($frequency->isDaily()) {
  309. $set('interval_value', null);
  310. $set('interval_type', null);
  311. }
  312. if ($frequency->isWeekly()) {
  313. $currentDayOfWeek = now()->dayOfWeek;
  314. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  315. $set('day_of_week', $currentDayOfWeek);
  316. $set('interval_value', null);
  317. $set('interval_type', null);
  318. }
  319. if ($frequency->isMonthly()) {
  320. $set('day_of_month', DayOfMonth::First);
  321. $set('interval_value', null);
  322. $set('interval_type', null);
  323. }
  324. if ($frequency->isYearly()) {
  325. $currentMonth = now()->month;
  326. $currentMonth = Month::parse($currentMonth);
  327. $set('month', $currentMonth);
  328. $currentDay = now()->dayOfMonth;
  329. $currentDay = DayOfMonth::parse($currentDay);
  330. $set('day_of_month', $currentDay);
  331. $set('interval_value', null);
  332. $set('interval_type', null);
  333. }
  334. if ($frequency->isCustom()) {
  335. $set('interval_value', 1);
  336. $set('interval_type', IntervalType::Month);
  337. $currentDay = now()->dayOfMonth;
  338. $currentDay = DayOfMonth::parse($currentDay);
  339. $set('day_of_month', $currentDay);
  340. }
  341. }),
  342. // Custom frequency fields in a nested grid
  343. Cluster::make([
  344. Forms\Components\TextInput::make('interval_value')
  345. ->softRequired()
  346. ->numeric()
  347. ->default(1),
  348. Forms\Components\Select::make('interval_type')
  349. ->options(IntervalType::class)
  350. ->softRequired()
  351. ->default(IntervalType::Month)
  352. ->live()
  353. ->afterStateUpdated(function (Forms\Set $set, $state) {
  354. $intervalType = IntervalType::parse($state);
  355. if ($intervalType->isWeek()) {
  356. $currentDayOfWeek = now()->dayOfWeek;
  357. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  358. $set('day_of_week', $currentDayOfWeek);
  359. }
  360. if ($intervalType->isMonth()) {
  361. $currentDay = now()->dayOfMonth;
  362. $currentDay = DayOfMonth::parse($currentDay);
  363. $set('day_of_month', $currentDay);
  364. }
  365. if ($intervalType->isYear()) {
  366. $currentMonth = now()->month;
  367. $currentMonth = Month::parse($currentMonth);
  368. $set('month', $currentMonth);
  369. $currentDay = now()->dayOfMonth;
  370. $currentDay = DayOfMonth::parse($currentDay);
  371. $set('day_of_month', $currentDay);
  372. }
  373. }),
  374. ])
  375. ->live()
  376. ->label('Every')
  377. ->required()
  378. ->markAsRequired(false)
  379. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isCustom()),
  380. // Specific schedule details
  381. Forms\Components\Select::make('month')
  382. ->label('Month')
  383. ->options(Month::class)
  384. ->softRequired()
  385. ->visible(
  386. fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isYearly() ||
  387. IntervalType::parse($get('interval_type'))?->isYear()
  388. ),
  389. Forms\Components\Select::make('day_of_month')
  390. ->label('Day of Month')
  391. ->options(DayOfMonth::class)
  392. ->softRequired()
  393. ->visible(
  394. fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isMonthly() ||
  395. Frequency::parse($get('frequency'))?->isYearly() ||
  396. IntervalType::parse($get('interval_type'))?->isMonth() ||
  397. IntervalType::parse($get('interval_type'))?->isYear()
  398. ),
  399. Forms\Components\Select::make('day_of_week')
  400. ->label('Day of Week')
  401. ->options(DayOfWeek::class)
  402. ->softRequired()
  403. ->visible(
  404. fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isWeekly() ||
  405. IntervalType::parse($get('interval_type'))?->isWeek()
  406. ),
  407. ])->columns(2),
  408. CustomSection::make('Dates & Time')
  409. ->contained(false)
  410. ->schema([
  411. Forms\Components\DatePicker::make('start_date')
  412. ->label('First Invoice Date')
  413. ->softRequired(),
  414. Forms\Components\Group::make(function (Forms\Get $get) {
  415. $components = [];
  416. $components[] = Forms\Components\Select::make('end_type')
  417. ->label('End Schedule')
  418. ->options(EndType::class)
  419. ->softRequired()
  420. ->live()
  421. ->afterStateUpdated(function (Forms\Set $set, $state) {
  422. $endType = EndType::parse($state);
  423. if ($endType?->isNever()) {
  424. $set('max_occurrences', null);
  425. $set('end_date', null);
  426. }
  427. if ($endType?->isAfter()) {
  428. $set('max_occurrences', 1);
  429. $set('end_date', null);
  430. }
  431. if ($endType?->isOn()) {
  432. $set('max_occurrences', null);
  433. $set('end_date', now()->addMonth()->startOfMonth());
  434. }
  435. });
  436. $endType = EndType::parse($get('end_type'));
  437. if ($endType?->isAfter()) {
  438. $components[] = Forms\Components\TextInput::make('max_occurrences')
  439. ->numeric()
  440. ->suffix('invoices')
  441. ->live();
  442. }
  443. if ($endType?->isOn()) {
  444. $components[] = Forms\Components\DatePicker::make('end_date')
  445. ->live();
  446. }
  447. return [
  448. Cluster::make($components)
  449. ->label('Schedule Ends')
  450. ->required()
  451. ->markAsRequired(false),
  452. ];
  453. }),
  454. Forms\Components\Select::make('timezone')
  455. ->options(Timezone::getTimezoneOptions(CompanyProfile::first()->country))
  456. ->searchable()
  457. ->softRequired(),
  458. ])
  459. ->columns(2),
  460. ])
  461. ->action(function (self $record, array $data, MountableAction $action) {
  462. $record->update($data);
  463. $action->success();
  464. });
  465. }
  466. public static function getApproveDraftAction(string $action = Action::class): MountableAction
  467. {
  468. return $action::make('approveDraft')
  469. ->label('Approve')
  470. ->icon('heroicon-o-check-circle')
  471. ->visible(function (self $record) {
  472. return $record->canBeApproved();
  473. })
  474. ->databaseTransaction()
  475. ->successNotificationTitle('Recurring Invoice Approved')
  476. ->action(function (self $record, MountableAction $action) {
  477. $record->approveDraft();
  478. $action->success();
  479. });
  480. }
  481. public function approveDraft(?Carbon $approvedAt = null): void
  482. {
  483. if (! $this->isDraft()) {
  484. throw new \RuntimeException('Invoice is not in draft status.');
  485. }
  486. $approvedAt ??= now();
  487. $this->update([
  488. 'approved_at' => $approvedAt,
  489. 'status' => RecurringInvoiceStatus::Active,
  490. ]);
  491. }
  492. }