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 18KB

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