選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ViewRecurringInvoice.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Filament\Company\Resources\Sales\RecurringInvoiceResource\Pages;
  3. use App\Enums\Accounting\DayOfMonth;
  4. use App\Enums\Accounting\DayOfWeek;
  5. use App\Enums\Accounting\EndType;
  6. use App\Enums\Accounting\Frequency;
  7. use App\Enums\Accounting\IntervalType;
  8. use App\Enums\Accounting\Month;
  9. use App\Filament\Company\Resources\Sales\RecurringInvoiceResource;
  10. use App\Filament\Forms\Components\LabeledField;
  11. use App\Models\Setting\CompanyProfile;
  12. use App\Utilities\Localization\Timezone;
  13. use Filament\Forms;
  14. use Filament\Forms\Form;
  15. use Filament\Resources\Pages\ViewRecord;
  16. use Filament\Support\Enums\MaxWidth;
  17. use Guava\FilamentClusters\Forms\Cluster;
  18. class ViewRecurringInvoice extends ViewRecord
  19. {
  20. protected static string $resource = RecurringInvoiceResource::class;
  21. protected function mutateFormDataBeforeFill(array $data): array
  22. {
  23. $data['day_of_month'] ??= DayOfMonth::First;
  24. $data['start_date'] ??= now()->addMonth()->startOfMonth();
  25. return $data;
  26. }
  27. public function getMaxContentWidth(): MaxWidth | string | null
  28. {
  29. return MaxWidth::SixExtraLarge;
  30. }
  31. public function form(Form $form): Form
  32. {
  33. return $form
  34. ->disabled(false)
  35. ->schema([
  36. Forms\Components\Section::make('Scheduling')
  37. ->schema([
  38. Forms\Components\Group::make([
  39. Forms\Components\Select::make('frequency')
  40. ->label('Repeat this invoice')
  41. ->inlineLabel()
  42. ->options(Frequency::class)
  43. ->softRequired()
  44. ->live()
  45. ->afterStateUpdated(function (Forms\Set $set, $state) {
  46. $frequency = Frequency::parse($state);
  47. if ($frequency->isDaily()) {
  48. $set('interval_value', null);
  49. $set('interval_type', null);
  50. }
  51. if ($frequency->isWeekly()) {
  52. $currentDayOfWeek = now()->dayOfWeek;
  53. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  54. $set('day_of_week', $currentDayOfWeek);
  55. $set('interval_value', null);
  56. $set('interval_type', null);
  57. }
  58. if ($frequency->isMonthly()) {
  59. $set('day_of_month', DayOfMonth::First);
  60. $set('interval_value', null);
  61. $set('interval_type', null);
  62. }
  63. if ($frequency->isYearly()) {
  64. $currentMonth = now()->month;
  65. $currentMonth = Month::parse($currentMonth);
  66. $set('month', $currentMonth);
  67. $currentDay = now()->dayOfMonth;
  68. $currentDay = DayOfMonth::parse($currentDay);
  69. $set('day_of_month', $currentDay);
  70. $set('interval_value', null);
  71. $set('interval_type', null);
  72. }
  73. if ($frequency->isCustom()) {
  74. $set('interval_value', 1);
  75. $set('interval_type', IntervalType::Month);
  76. $currentDay = now()->dayOfMonth;
  77. $currentDay = DayOfMonth::parse($currentDay);
  78. $set('day_of_month', $currentDay);
  79. }
  80. }),
  81. // Custom frequency fields
  82. LabeledField::make()
  83. ->prefix('every')
  84. ->schema([
  85. Cluster::make([
  86. Forms\Components\TextInput::make('interval_value')
  87. ->label('every')
  88. ->numeric()
  89. ->default(1),
  90. Forms\Components\Select::make('interval_type')
  91. ->label('Interval Type')
  92. ->options(IntervalType::class)
  93. ->softRequired()
  94. ->default(IntervalType::Month)
  95. ->live()
  96. ->afterStateUpdated(function (Forms\Set $set, $state) {
  97. $intervalType = IntervalType::parse($state);
  98. if ($intervalType->isWeek()) {
  99. $currentDayOfWeek = now()->dayOfWeek;
  100. $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
  101. $set('day_of_week', $currentDayOfWeek);
  102. }
  103. if ($intervalType->isMonth()) {
  104. $currentDay = now()->dayOfMonth;
  105. $currentDay = DayOfMonth::parse($currentDay);
  106. $set('day_of_month', $currentDay);
  107. }
  108. if ($intervalType->isYear()) {
  109. $currentMonth = now()->month;
  110. $currentMonth = Month::parse($currentMonth);
  111. $set('month', $currentMonth);
  112. $currentDay = now()->dayOfMonth;
  113. $currentDay = DayOfMonth::parse($currentDay);
  114. $set('day_of_month', $currentDay);
  115. }
  116. }),
  117. ])
  118. ->live()
  119. ->hiddenLabel(),
  120. ])
  121. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isCustom()),
  122. LabeledField::make()
  123. ->prefix(function (Forms\Get $get) {
  124. $frequency = Frequency::parse($get('frequency'));
  125. $intervalType = IntervalType::parse($get('interval_type'));
  126. if ($frequency->isYearly()) {
  127. return 'every';
  128. }
  129. if ($frequency->isCustom() && $intervalType?->isYear()) {
  130. return 'in';
  131. }
  132. return null;
  133. })
  134. ->schema([
  135. Forms\Components\Select::make('month')
  136. ->hiddenLabel()
  137. ->options(Month::class)
  138. ->live()
  139. ->softRequired(),
  140. ])
  141. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isYearly() || IntervalType::parse($get('interval_type'))?->isYear()),
  142. LabeledField::make()
  143. ->prefix('on the')
  144. ->suffix(function (Forms\Get $get) {
  145. $frequency = Frequency::parse($get('frequency'));
  146. $intervalType = IntervalType::parse($get('interval_type'));
  147. if ($frequency->isMonthly()) {
  148. return 'day of every month';
  149. }
  150. if ($frequency->isYearly() || ($frequency->isCustom() && $intervalType->isMonth()) || ($frequency->isCustom() && $intervalType->isYear())) {
  151. return 'day of the month';
  152. }
  153. return null;
  154. })
  155. ->schema([
  156. Forms\Components\Select::make('day_of_month')
  157. ->hiddenLabel()
  158. ->inlineLabel()
  159. ->options(DayOfMonth::class)
  160. ->live()
  161. ->softRequired(),
  162. ])
  163. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isMonthly() || Frequency::parse($get('frequency'))?->isYearly() || IntervalType::parse($get('interval_type'))?->isMonth() || IntervalType::parse($get('interval_type'))?->isYear()),
  164. LabeledField::make()
  165. ->prefix(function (Forms\Get $get) {
  166. $frequency = Frequency::parse($get('frequency'));
  167. $intervalType = IntervalType::parse($get('interval_type'));
  168. if ($frequency->isWeekly()) {
  169. return 'every';
  170. }
  171. if ($frequency->isCustom() && $intervalType->isWeek()) {
  172. return 'on';
  173. }
  174. return null;
  175. })
  176. ->schema([
  177. Forms\Components\Select::make('day_of_week')
  178. ->hiddenLabel()
  179. ->options(DayOfWeek::class)
  180. ->live()
  181. ->softRequired(),
  182. ])
  183. ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isWeekly() || IntervalType::parse($get('interval_type'))?->isWeek()),
  184. ])->columns(2),
  185. Forms\Components\Group::make([
  186. Forms\Components\DatePicker::make('start_date')
  187. ->label('Create first invoice on')
  188. ->inlineLabel()
  189. ->softRequired(),
  190. LabeledField::make()
  191. ->prefix('and end')
  192. ->suffix(function (Forms\Get $get) {
  193. $endType = EndType::parse($get('end_type'));
  194. if ($endType->isAfter()) {
  195. return 'invoices';
  196. }
  197. return null;
  198. })
  199. ->schema(function (Forms\Get $get) {
  200. $components = [];
  201. $components[] = Forms\Components\Select::make('end_type')
  202. ->hiddenLabel()
  203. ->options(EndType::class)
  204. ->softRequired()
  205. ->live()
  206. ->afterStateUpdated(function (Forms\Set $set, $state) {
  207. $endType = EndType::parse($state);
  208. if ($endType->isNever()) {
  209. $set('max_occurrences', null);
  210. $set('end_date', null);
  211. }
  212. if ($endType->isAfter()) {
  213. $set('max_occurrences', 1);
  214. $set('end_date', null);
  215. }
  216. if ($endType->isOn()) {
  217. $set('max_occurrences', null);
  218. $set('end_date', now()->addMonth()->startOfMonth());
  219. }
  220. });
  221. $endType = EndType::parse($get('end_type'));
  222. if ($endType->isAfter()) {
  223. $components[] = Forms\Components\TextInput::make('max_occurrences')
  224. ->numeric()
  225. ->live();
  226. }
  227. if ($endType->isOn()) {
  228. $components[] = Forms\Components\DatePicker::make('end_date')
  229. ->live();
  230. }
  231. return [
  232. Cluster::make($components)
  233. ->hiddenLabel(),
  234. ];
  235. }),
  236. ])->columns(2),
  237. Forms\Components\Group::make([
  238. LabeledField::make()
  239. ->prefix('Create in')
  240. ->suffix('time zone')
  241. ->schema([
  242. Forms\Components\Select::make('timezone')
  243. ->softRequired()
  244. ->hiddenLabel()
  245. ->options(Timezone::getTimezoneOptions(CompanyProfile::first()->country))
  246. ->searchable(),
  247. ])
  248. ->columns(1),
  249. ])->columns(2),
  250. ]),
  251. ]);
  252. }
  253. }