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.

OfferingResource.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Filament\Company\Resources\Common;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Enums\Accounting\AccountType;
  5. use App\Enums\Common\OfferingType;
  6. use App\Filament\Company\Resources\Common\OfferingResource\Pages;
  7. use App\Models\Accounting\Account;
  8. use App\Models\Common\Offering;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use Filament\Forms;
  11. use Filament\Forms\Form;
  12. use Filament\Resources\Resource;
  13. use Filament\Tables;
  14. use Filament\Tables\Table;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Support\Str;
  17. use JaOcero\RadioDeck\Forms\Components\RadioDeck;
  18. class OfferingResource extends Resource
  19. {
  20. protected static ?string $model = Offering::class;
  21. protected static ?string $modelLabel = 'Offering';
  22. protected static ?string $navigationIcon = 'heroicon-o-square-3-stack-3d';
  23. public static function form(Form $form): Form
  24. {
  25. return $form
  26. ->schema([
  27. Forms\Components\Section::make('General')
  28. ->schema([
  29. RadioDeck::make('type')
  30. ->options(OfferingType::class)
  31. ->default(OfferingType::Product)
  32. ->icons(OfferingType::class)
  33. ->color('primary')
  34. ->columns()
  35. ->required(),
  36. Forms\Components\TextInput::make('name')
  37. ->autofocus()
  38. ->required()
  39. ->columnStart(1)
  40. ->maxLength(255),
  41. Forms\Components\TextInput::make('price')
  42. ->required()
  43. ->money(),
  44. Forms\Components\Textarea::make('description')
  45. ->label('Description')
  46. ->columnSpan(2)
  47. ->rows(3),
  48. Forms\Components\CheckboxList::make('attributes')
  49. ->options([
  50. 'Sellable' => 'Sellable',
  51. 'Purchasable' => 'Purchasable',
  52. ])
  53. ->hiddenLabel()
  54. ->required()
  55. ->live()
  56. ->bulkToggleable()
  57. ->validationMessages([
  58. 'required' => 'The offering must be either sellable or purchasable.',
  59. ]),
  60. ])->columns(),
  61. // Sellable Section
  62. Forms\Components\Section::make('Sale Information')
  63. ->schema([
  64. Forms\Components\Select::make('income_account_id')
  65. ->label('Income Account')
  66. ->options(Account::query()
  67. ->where('category', AccountCategory::Revenue)
  68. ->where('type', AccountType::OperatingRevenue)
  69. ->pluck('name', 'id')
  70. ->toArray())
  71. ->searchable()
  72. ->preload()
  73. ->required()
  74. ->validationMessages([
  75. 'required' => 'The income account is required for sellable offerings.',
  76. ]),
  77. Forms\Components\Select::make('salesTaxes')
  78. ->label('Sales Tax')
  79. ->relationship('salesTaxes', 'name')
  80. ->preload()
  81. ->multiple(),
  82. Forms\Components\Select::make('salesDiscounts')
  83. ->label('Sales Discount')
  84. ->relationship('salesDiscounts', 'name')
  85. ->preload()
  86. ->multiple(),
  87. ])
  88. ->columns()
  89. ->visible(fn (Forms\Get $get) => in_array('Sellable', $get('attributes') ?? [])),
  90. // Purchasable Section
  91. Forms\Components\Section::make('Purchase Information')
  92. ->schema([
  93. Forms\Components\Select::make('expense_account_id')
  94. ->label('Expense Account')
  95. ->options(Account::query()
  96. ->where('category', AccountCategory::Expense)
  97. ->where('type', AccountType::OperatingExpense)
  98. ->orderBy('name')
  99. ->pluck('name', 'id')
  100. ->toArray())
  101. ->searchable()
  102. ->preload()
  103. ->required()
  104. ->validationMessages([
  105. 'required' => 'The expense account is required for purchasable offerings.',
  106. ]),
  107. Forms\Components\Select::make('purchaseTaxes')
  108. ->label('Purchase Tax')
  109. ->relationship('purchaseTaxes', 'name')
  110. ->preload()
  111. ->multiple(),
  112. Forms\Components\Select::make('purchaseDiscounts')
  113. ->label('Purchase Discount')
  114. ->relationship('purchaseDiscounts', 'name')
  115. ->preload()
  116. ->multiple(),
  117. ])
  118. ->columns()
  119. ->visible(fn (Forms\Get $get) => in_array('Purchasable', $get('attributes') ?? [])),
  120. ])->columns();
  121. }
  122. public static function table(Table $table): Table
  123. {
  124. return $table
  125. ->modifyQueryUsing(function (Builder $query) {
  126. $query->selectRaw("
  127. *,
  128. CONCAT_WS(' & ',
  129. CASE WHEN sellable THEN 'Sellable' END,
  130. CASE WHEN purchasable THEN 'Purchasable' END
  131. ) AS attributes
  132. ");
  133. })
  134. ->columns([
  135. Tables\Columns\TextColumn::make('name')
  136. ->label('Name'),
  137. Tables\Columns\TextColumn::make('attributes')
  138. ->label('Attributes')
  139. ->badge(),
  140. Tables\Columns\TextColumn::make('type')
  141. ->searchable(),
  142. Tables\Columns\TextColumn::make('price')
  143. ->currency(CurrencyAccessor::getDefaultCurrency(), true)
  144. ->sortable()
  145. ->description(function (Offering $record) {
  146. $adjustments = $record->adjustments()
  147. ->pluck('name')
  148. ->join(', ');
  149. if (empty($adjustments)) {
  150. return null;
  151. }
  152. $adjustmentsList = Str::of($adjustments)->limit(40);
  153. return "+ {$adjustmentsList}";
  154. }),
  155. ])
  156. ->filters([
  157. //
  158. ])
  159. ->actions([
  160. Tables\Actions\EditAction::make(),
  161. ])
  162. ->bulkActions([
  163. Tables\Actions\BulkActionGroup::make([
  164. Tables\Actions\DeleteBulkAction::make(),
  165. ]),
  166. ]);
  167. }
  168. public static function getRelations(): array
  169. {
  170. return [
  171. //
  172. ];
  173. }
  174. public static function getPages(): array
  175. {
  176. return [
  177. 'index' => Pages\ListOfferings::route('/'),
  178. 'create' => Pages\CreateOffering::route('/create'),
  179. 'edit' => Pages\EditOffering::route('/{record}/edit'),
  180. ];
  181. }
  182. }