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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. ->timezone(CompanySettingsService::getDefaultTimezone()),
  81. Forms\Components\DateTimePicker::make('end_date')
  82. ->timezone(CompanySettingsService::getDefaultTimezone())
  83. ->after('start_date'),
  84. ])
  85. ->columns()
  86. ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
  87. ]);
  88. }
  89. public static function table(Table $table): Table
  90. {
  91. return $table
  92. ->columns([
  93. Tables\Columns\TextColumn::make('name')
  94. ->label('Name')
  95. ->sortable(),
  96. Tables\Columns\TextColumn::make('status')
  97. ->badge(),
  98. Tables\Columns\TextColumn::make('category')
  99. ->searchable(),
  100. Tables\Columns\TextColumn::make('type')
  101. ->searchable(),
  102. Tables\Columns\TextColumn::make('rate')
  103. ->localizeLabel()
  104. ->rate(static fn (Adjustment $record) => $record->computation->value)
  105. ->searchable()
  106. ->sortable(),
  107. Tables\Columns\TextColumn::make('paused_until')
  108. ->label('Auto-Resume Date')
  109. ->dateTime()
  110. ->sortable()
  111. ->toggleable(isToggledHiddenByDefault: true),
  112. Tables\Columns\TextColumn::make('start_date')
  113. ->dateTime()
  114. ->sortable()
  115. ->toggleable(isToggledHiddenByDefault: true),
  116. Tables\Columns\TextColumn::make('end_date')
  117. ->dateTime()
  118. ->sortable()
  119. ->toggleable(isToggledHiddenByDefault: true),
  120. ])
  121. ->filters([
  122. Tables\Filters\SelectFilter::make('status')
  123. ->label('Status')
  124. ->native(false)
  125. ->default('unarchived')
  126. ->options(
  127. collect(AdjustmentStatus::cases())
  128. ->mapWithKeys(fn (AdjustmentStatus $status) => [$status->value => $status->getLabel()])
  129. ->merge([
  130. 'unarchived' => 'Unarchived',
  131. ])
  132. ->toArray()
  133. )
  134. ->indicateUsing(function (Tables\Filters\SelectFilter $filter, array $state) {
  135. if (blank($state['value'] ?? null)) {
  136. return [];
  137. }
  138. $label = collect($filter->getOptions())
  139. ->mapWithKeys(fn (string | array $label, string $value): array => is_array($label) ? $label : [$value => $label])
  140. ->get($state['value']);
  141. if (blank($label)) {
  142. return [];
  143. }
  144. $indicator = $filter->getIndicator();
  145. if (! $indicator instanceof Indicator) {
  146. if ($state['value'] === 'unarchived') {
  147. $indicator = $label;
  148. } else {
  149. $indicator = Indicator::make("{$indicator}: {$label}");
  150. }
  151. }
  152. return [$indicator];
  153. })
  154. ->query(function (Builder $query, array $data): Builder {
  155. if (blank($data['value'] ?? null)) {
  156. return $query;
  157. }
  158. if ($data['value'] !== 'unarchived') {
  159. return $query->where('status', $data['value']);
  160. } else {
  161. return $query->where('status', '!=', AdjustmentStatus::Archived->value);
  162. }
  163. }),
  164. Tables\Filters\SelectFilter::make('category')
  165. ->label('Category')
  166. ->native(false)
  167. ->options(AdjustmentCategory::class),
  168. Tables\Filters\SelectFilter::make('type')
  169. ->label('Type')
  170. ->native(false)
  171. ->options(AdjustmentType::class),
  172. Tables\Filters\SelectFilter::make('computation')
  173. ->label('Computation')
  174. ->native(false)
  175. ->options(AdjustmentComputation::class),
  176. ])
  177. ->actions([
  178. Tables\Actions\ActionGroup::make([
  179. Tables\Actions\EditAction::make(),
  180. Tables\Actions\Action::make('pause')
  181. ->label('Pause')
  182. ->icon('heroicon-m-pause')
  183. ->form([
  184. Forms\Components\DateTimePicker::make('paused_until')
  185. ->label('Auto-resume date')
  186. ->timezone(CompanySettingsService::getDefaultTimezone())
  187. ->helperText('When should this adjustment automatically resume? Leave empty to keep paused indefinitely.')
  188. ->after('now'),
  189. Forms\Components\Textarea::make('status_reason')
  190. ->label('Reason for pausing')
  191. ->maxLength(255),
  192. ])
  193. ->databaseTransaction()
  194. ->successNotificationTitle('Adjustment paused')
  195. ->failureNotificationTitle('Failed to pause adjustment')
  196. ->visible(fn (Adjustment $record) => $record->canBePaused())
  197. ->action(function (Adjustment $record, array $data, Tables\Actions\Action $action) {
  198. $pausedUntil = $data['paused_until'] ?? null;
  199. $reason = $data['status_reason'] ?? null;
  200. $record->pause($reason, $pausedUntil);
  201. $action->success();
  202. }),
  203. Tables\Actions\Action::make('resume')
  204. ->label('Resume')
  205. ->icon('heroicon-m-play')
  206. ->requiresConfirmation()
  207. ->databaseTransaction()
  208. ->successNotificationTitle('Adjustment resumed')
  209. ->failureNotificationTitle('Failed to resume adjustment')
  210. ->visible(fn (Adjustment $record) => $record->canBeResumed())
  211. ->action(function (Adjustment $record, Tables\Actions\Action $action) {
  212. $record->resume();
  213. $action->success();
  214. }),
  215. Tables\Actions\Action::make('archive')
  216. ->label('Archive')
  217. ->icon('heroicon-m-archive-box')
  218. ->color('danger')
  219. ->form([
  220. Forms\Components\Textarea::make('status_reason')
  221. ->label('Reason for archiving')
  222. ->maxLength(255),
  223. ])
  224. ->databaseTransaction()
  225. ->successNotificationTitle('Adjustment archived')
  226. ->failureNotificationTitle('Failed to archive adjustment')
  227. ->visible(fn (Adjustment $record) => $record->canBeArchived())
  228. ->action(function (Adjustment $record, array $data, Tables\Actions\Action $action) {
  229. $reason = $data['status_reason'] ?? null;
  230. $record->archive($reason);
  231. $action->success();
  232. }),
  233. ]),
  234. ])
  235. ->bulkActions([
  236. Tables\Actions\BulkActionGroup::make([
  237. Tables\Actions\BulkAction::make('pause')
  238. ->label('Pause')
  239. ->icon('heroicon-m-pause')
  240. ->form([
  241. Forms\Components\DateTimePicker::make('paused_until')
  242. ->label('Auto-resume date')
  243. ->timezone(CompanySettingsService::getDefaultTimezone())
  244. ->helperText('When should these adjustments automatically resume? Leave empty to keep paused indefinitely.')
  245. ->after('now'),
  246. Forms\Components\Textarea::make('status_reason')
  247. ->label('Reason for pausing')
  248. ->maxLength(255),
  249. ])
  250. ->databaseTransaction()
  251. ->successNotificationTitle('Adjustments paused')
  252. ->failureNotificationTitle('Failed to pause adjustments')
  253. ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
  254. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBePaused());
  255. if ($isInvalid) {
  256. Notification::make()
  257. ->title('Pause failed')
  258. ->body('Only adjustments that are currently active can be paused. Please adjust your selection and try again.')
  259. ->persistent()
  260. ->danger()
  261. ->send();
  262. $action->cancel(true);
  263. }
  264. })
  265. ->deselectRecordsAfterCompletion()
  266. ->action(function (Collection $records, array $data, Tables\Actions\BulkAction $action) {
  267. $pausedUntil = $data['paused_until'] ?? null;
  268. $reason = $data['status_reason'] ?? null;
  269. $records->each(function (Adjustment $record) use ($reason, $pausedUntil) {
  270. $record->pause($reason, $pausedUntil);
  271. });
  272. $action->success();
  273. }),
  274. Tables\Actions\BulkAction::make('resume')
  275. ->label('Resume')
  276. ->icon('heroicon-m-play')
  277. ->databaseTransaction()
  278. ->requiresConfirmation()
  279. ->successNotificationTitle('Adjustments resumed')
  280. ->failureNotificationTitle('Failed to resume adjustments')
  281. ->before(function (Collection $records, Tables\Actions\BulkAction $action) {
  282. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBeResumed());
  283. if ($isInvalid) {
  284. Notification::make()
  285. ->title('Resume failed')
  286. ->body('Only adjustments that are currently paused can be resumed. Please adjust your selection and try again.')
  287. ->persistent()
  288. ->danger()
  289. ->send();
  290. $action->cancel(true);
  291. }
  292. })
  293. ->deselectRecordsAfterCompletion()
  294. ->action(function (Collection $records, Tables\Actions\BulkAction $action) {
  295. $records->each(function (Adjustment $record) {
  296. $record->resume();
  297. });
  298. $action->success();
  299. }),
  300. Tables\Actions\BulkAction::make('archive')
  301. ->label('Archive')
  302. ->icon('heroicon-m-archive-box')
  303. ->color('danger')
  304. ->form([
  305. Forms\Components\Textarea::make('status_reason')
  306. ->label('Reason for archiving')
  307. ->maxLength(255),
  308. ])
  309. ->databaseTransaction()
  310. ->successNotificationTitle('Adjustments archived')
  311. ->failureNotificationTitle('Failed to archive adjustments')
  312. ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
  313. $isInvalid = $records->contains(fn (Adjustment $record) => ! $record->canBeArchived());
  314. if ($isInvalid) {
  315. Notification::make()
  316. ->title('Archive failed')
  317. ->body('Only adjustments that are currently active or paused can be archived. Please adjust your selection and try again.')
  318. ->persistent()
  319. ->danger()
  320. ->send();
  321. $action->cancel(true);
  322. }
  323. })
  324. ->deselectRecordsAfterCompletion()
  325. ->action(function (Collection $records, array $data, Tables\Actions\BulkAction $action) {
  326. $reason = $data['status_reason'] ?? null;
  327. $records->each(function (Adjustment $record) use ($reason) {
  328. $record->archive($reason);
  329. });
  330. $action->success();
  331. }),
  332. ]),
  333. ]);
  334. }
  335. public static function getRelations(): array
  336. {
  337. return [
  338. //
  339. ];
  340. }
  341. public static function getPages(): array
  342. {
  343. return [
  344. 'index' => Pages\ListAdjustments::route('/'),
  345. 'create' => Pages\CreateAdjustment::route('/create'),
  346. 'edit' => Pages\EditAdjustment::route('/{record}/edit'),
  347. ];
  348. }
  349. }