Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

OfferingResource.php 7.8KB

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