You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdjustmentResource.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Enums\Accounting\AdjustmentCategory;
  4. use App\Enums\Accounting\AdjustmentComputation;
  5. use App\Enums\Accounting\AdjustmentScope;
  6. use App\Enums\Accounting\AdjustmentStatus;
  7. use App\Enums\Accounting\AdjustmentType;
  8. use App\Filament\Company\Clusters\Settings;
  9. use App\Filament\Company\Clusters\Settings\Resources\AdjustmentResource\Pages;
  10. use App\Models\Accounting\Adjustment;
  11. use Filament\Forms;
  12. use Filament\Forms\Form;
  13. use Filament\Notifications\Notification;
  14. use Filament\Resources\Resource;
  15. use Filament\Tables;
  16. use Filament\Tables\Table;
  17. use Illuminate\Database\Eloquent\Collection;
  18. class AdjustmentResource extends Resource
  19. {
  20. protected static ?string $model = Adjustment::class;
  21. protected static ?string $cluster = Settings::class;
  22. public static function form(Form $form): Form
  23. {
  24. return $form
  25. ->schema([
  26. Forms\Components\Section::make('General')
  27. ->schema([
  28. Forms\Components\TextInput::make('name')
  29. ->autofocus()
  30. ->required()
  31. ->maxLength(255),
  32. Forms\Components\Textarea::make('description')
  33. ->label('Description'),
  34. ]),
  35. Forms\Components\Section::make('Configuration')
  36. ->schema([
  37. Forms\Components\Select::make('category')
  38. ->localizeLabel()
  39. ->options(AdjustmentCategory::class)
  40. ->default(AdjustmentCategory::Tax)
  41. ->live()
  42. ->required(),
  43. Forms\Components\Select::make('type')
  44. ->localizeLabel()
  45. ->options(AdjustmentType::class)
  46. ->default(AdjustmentType::Sales)
  47. ->live()
  48. ->required(),
  49. Forms\Components\Checkbox::make('recoverable')
  50. ->label('Recoverable')
  51. ->default(false)
  52. ->helperText('When enabled, tax is tracked separately as claimable from the government. Non-recoverable taxes are treated as part of the expense.')
  53. ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isTax() && AdjustmentType::parse($get('type'))->isPurchase()),
  54. ])
  55. ->columns()
  56. ->visibleOn('create'),
  57. Forms\Components\Section::make('Adjustment Details')
  58. ->schema([
  59. Forms\Components\Select::make('computation')
  60. ->localizeLabel()
  61. ->options(AdjustmentComputation::class)
  62. ->default(AdjustmentComputation::Percentage)
  63. ->live()
  64. ->required(),
  65. Forms\Components\TextInput::make('rate')
  66. ->localizeLabel()
  67. ->rate(static fn (Forms\Get $get) => $get('computation'))
  68. ->required(),
  69. Forms\Components\Select::make('scope')
  70. ->localizeLabel()
  71. ->options(AdjustmentScope::class),
  72. ])
  73. ->columns(),
  74. Forms\Components\Section::make('Dates')
  75. ->schema([
  76. Forms\Components\DateTimePicker::make('start_date'),
  77. Forms\Components\DateTimePicker::make('end_date')
  78. ->after('start_date'),
  79. ])
  80. ->columns()
  81. ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
  82. ]);
  83. }
  84. public static function table(Table $table): Table
  85. {
  86. return $table
  87. ->columns([
  88. Tables\Columns\TextColumn::make('name')
  89. ->label('Name')
  90. ->sortable(),
  91. Tables\Columns\TextColumn::make('status')
  92. ->badge(),
  93. Tables\Columns\TextColumn::make('category')
  94. ->searchable(),
  95. Tables\Columns\TextColumn::make('type')
  96. ->searchable(),
  97. Tables\Columns\TextColumn::make('rate')
  98. ->localizeLabel()
  99. ->rate(static fn (Adjustment $record) => $record->computation->value)
  100. ->searchable()
  101. ->sortable(),
  102. Tables\Columns\TextColumn::make('paused_until')
  103. ->label('Auto-Resume Date')
  104. ->dateTime()
  105. ->sortable()
  106. ->toggleable(isToggledHiddenByDefault: true),
  107. Tables\Columns\TextColumn::make('start_date')
  108. ->dateTime()
  109. ->sortable()
  110. ->toggleable(isToggledHiddenByDefault: true),
  111. Tables\Columns\TextColumn::make('end_date')
  112. ->dateTime()
  113. ->sortable()
  114. ->toggleable(isToggledHiddenByDefault: true),
  115. ])
  116. ->filters([
  117. Tables\Filters\TernaryFilter::make('archived')
  118. ->label('Archived')
  119. ->default(false)
  120. ->attribute('archived_at')
  121. ->nullable(),
  122. Tables\Filters\SelectFilter::make('status')
  123. ->label('Status')
  124. ->native(false)
  125. ->options(AdjustmentStatus::class),
  126. Tables\Filters\SelectFilter::make('category')
  127. ->label('Category')
  128. ->native(false)
  129. ->options(AdjustmentCategory::class),
  130. Tables\Filters\SelectFilter::make('type')
  131. ->label('Type')
  132. ->native(false)
  133. ->options(AdjustmentType::class),
  134. Tables\Filters\SelectFilter::make('computation')
  135. ->label('Computation')
  136. ->native(false)
  137. ->options(AdjustmentComputation::class),
  138. ])
  139. ->actions([
  140. Tables\Actions\ActionGroup::make([
  141. Tables\Actions\EditAction::make(),
  142. Tables\Actions\Action::make('pause')
  143. ->label('Pause')
  144. ->icon('heroicon-m-pause')
  145. ->form([
  146. Forms\Components\DateTimePicker::make('paused_until')
  147. ->label('Auto-resume date')
  148. ->helperText('When should this adjustment automatically resume? Leave empty to keep paused indefinitely.')
  149. ->after('now'),
  150. Forms\Components\Textarea::make('status_reason')
  151. ->label('Reason for pausing')
  152. ->maxLength(255),
  153. ])
  154. ->visible(fn (Adjustment $record) => $record->canBePaused())
  155. ->action(function (Adjustment $record, array $data) {
  156. $pausedUntil = $data['paused_until'] ?? null;
  157. $reason = $data['status_reason'] ?? null;
  158. $record->pause($reason, $pausedUntil);
  159. }),
  160. Tables\Actions\Action::make('resume')
  161. ->label('Resume')
  162. ->icon('heroicon-m-play')
  163. ->requiresConfirmation()
  164. ->visible(fn (Adjustment $record) => $record->canBeResumed())
  165. ->action(fn (Adjustment $record) => $record->resume()),
  166. Tables\Actions\Action::make('archive')
  167. ->label('Archive')
  168. ->icon('heroicon-m-archive-box')
  169. ->color('danger')
  170. ->form([
  171. Forms\Components\Textarea::make('status_reason')
  172. ->label('Reason for archiving')
  173. ->maxLength(255),
  174. ])
  175. ->visible(fn (Adjustment $record) => $record->canBeArchived())
  176. ->action(function (Adjustment $record, array $data) {
  177. $reason = $data['status_reason'] ?? null;
  178. $record->archive($reason);
  179. }),
  180. ]),
  181. ])
  182. ->bulkActions([
  183. Tables\Actions\BulkActionGroup::make([
  184. Tables\Actions\BulkAction::make('pause')
  185. ->label('Pause')
  186. ->icon('heroicon-m-pause')
  187. ->form([
  188. Forms\Components\DateTimePicker::make('paused_until')
  189. ->label('Auto-resume date')
  190. ->helperText('When should these adjustments automatically resume? Leave empty to keep paused indefinitely.')
  191. ->after('now'),
  192. Forms\Components\Textarea::make('status_reason')
  193. ->label('Reason for pausing')
  194. ->maxLength(255),
  195. ])
  196. ->databaseTransaction()
  197. ->successNotificationTitle('Adjustments paused')
  198. ->failureNotificationTitle('Failed to pause adjustments')
  199. ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
  200. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBePaused());
  201. if ($isInvalid) {
  202. Notification::make()
  203. ->title('Pause failed')
  204. ->body('Only adjustments that are currently active can be paused. Please adjust your selection and try again.')
  205. ->persistent()
  206. ->danger()
  207. ->send();
  208. $action->cancel(true);
  209. }
  210. })
  211. ->deselectRecordsAfterCompletion()
  212. ->action(function (Collection $records, array $data, Tables\Actions\BulkAction $action) {
  213. $pausedUntil = $data['paused_until'] ?? null;
  214. $reason = $data['status_reason'] ?? null;
  215. $records->each(function (Adjustment $record) use ($reason, $pausedUntil) {
  216. $record->pause($reason, $pausedUntil);
  217. });
  218. $action->success();
  219. }),
  220. Tables\Actions\BulkAction::make('resume')
  221. ->label('Resume')
  222. ->icon('heroicon-m-play')
  223. ->databaseTransaction()
  224. ->requiresConfirmation()
  225. ->successNotificationTitle('Adjustments resumed')
  226. ->failureNotificationTitle('Failed to resume adjustments')
  227. ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
  228. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBeResumed());
  229. if ($isInvalid) {
  230. Notification::make()
  231. ->title('Resume failed')
  232. ->body('Only adjustments that are currently paused can be resumed. Please adjust your selection and try again.')
  233. ->persistent()
  234. ->danger()
  235. ->send();
  236. $action->cancel(true);
  237. }
  238. })
  239. ->deselectRecordsAfterCompletion()
  240. ->action(function (Collection $records, Tables\Actions\BulkAction $action) {
  241. $records->each(function (Adjustment $record) {
  242. $record->resume();
  243. });
  244. $action->success();
  245. }),
  246. Tables\Actions\BulkAction::make('archive')
  247. ->label('Archive')
  248. ->icon('heroicon-m-archive-box')
  249. ->color('danger')
  250. ->form([
  251. Forms\Components\Textarea::make('status_reason')
  252. ->label('Reason for archiving')
  253. ->maxLength(255),
  254. ])
  255. ->databaseTransaction()
  256. ->successNotificationTitle('Adjustments archived')
  257. ->failureNotificationTitle('Failed to archive adjustments')
  258. ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
  259. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBeArchived());
  260. if ($isInvalid) {
  261. Notification::make()
  262. ->title('Archive failed')
  263. ->body('Only adjustments that are currently active or paused can be archived. Please adjust your selection and try again.')
  264. ->persistent()
  265. ->danger()
  266. ->send();
  267. $action->cancel(true);
  268. }
  269. })
  270. ->deselectRecordsAfterCompletion()
  271. ->action(function (Collection $records, array $data, Tables\Actions\BulkAction $action) {
  272. $reason = $data['status_reason'] ?? null;
  273. $records->each(function (Adjustment $record) use ($reason) {
  274. $record->archive($reason);
  275. });
  276. $action->success();
  277. }),
  278. ]),
  279. ]);
  280. }
  281. public static function getRelations(): array
  282. {
  283. return [
  284. //
  285. ];
  286. }
  287. public static function getPages(): array
  288. {
  289. return [
  290. 'index' => Pages\ListAdjustments::route('/'),
  291. 'create' => Pages\CreateAdjustment::route('/create'),
  292. 'edit' => Pages\EditAdjustment::route('/{record}/edit'),
  293. ];
  294. }
  295. }