Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

InvoiceResource.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\InvoiceResource\Pages;
  4. use App\Filament\Resources\InvoiceResource\RelationManagers;
  5. use App\Models\Setting\Currency;
  6. use Illuminate\Support\Facades\Auth;
  7. use Wallo\FilamentSelectify\Components\ButtonGroup;
  8. use App\Models\Document\Document;
  9. use Filament\Forms;
  10. use Filament\Resources\Form;
  11. use Filament\Resources\Resource;
  12. use Filament\Resources\Table;
  13. use Filament\Tables;
  14. use Illuminate\Database\Eloquent\Builder;
  15. class InvoiceResource extends Resource
  16. {
  17. protected static ?string $model = Document::class;
  18. protected static ?string $navigationIcon = 'heroicon-o-document-text';
  19. protected static ?string $navigationGroup = 'Sales';
  20. protected static ?string $navigationLabel = 'Invoices';
  21. protected static ?string $modelLabel = 'invoice';
  22. public static function getEloquentQuery(): Builder
  23. {
  24. return parent::getEloquentQuery()
  25. ->where('type', 'invoice');
  26. }
  27. public static function form(Form $form): Form
  28. {
  29. return $form
  30. ->schema([
  31. Forms\Components\Section::make('Billing')
  32. ->schema([
  33. Forms\Components\Grid::make(3)
  34. ->schema([
  35. Forms\Components\Select::make('contact_id')
  36. ->label('Customer')
  37. ->preload()
  38. ->placeholder('Select a customer')
  39. ->relationship('contact', 'name', static fn (Builder $query) => $query->where('type', 'customer'))
  40. ->searchable()
  41. ->required()
  42. ->createOptionForm([
  43. ButtonGroup::make('contact.entity')
  44. ->label('Entity')
  45. ->options([
  46. 'company' => 'Company',
  47. 'individual' => 'Individual',
  48. ])
  49. ->default('company')
  50. ->required(),
  51. Forms\Components\TextInput::make('contact.name')
  52. ->label('Name')
  53. ->maxLength(100)
  54. ->required(),
  55. Forms\Components\TextInput::make('contact.email')
  56. ->label('Email')
  57. ->email()
  58. ->nullable(),
  59. Forms\Components\TextInput::make('contact.phone')
  60. ->label('Phone')
  61. ->tel()
  62. ->maxLength(20),
  63. Forms\Components\Select::make('contact.currency_code')
  64. ->label('Currency')
  65. ->relationship('currency', 'name')
  66. ->preload()
  67. ->default(Currency::getDefaultCurrency())
  68. ->searchable()
  69. ->reactive()
  70. ->required(),
  71. ])->columnSpan(1),
  72. Forms\Components\Grid::make(2)
  73. ->schema([
  74. Forms\Components\DatePicker::make('document_date')
  75. ->label('Invoice Date')
  76. ->default(now())
  77. ->format('Y-m-d')
  78. ->required(),
  79. Forms\Components\DatePicker::make('due_date')
  80. ->label('Due Date')
  81. ->default(now())
  82. ->format('Y-m-d')
  83. ->required(),
  84. Forms\Components\TextInput::make('document_number')
  85. ->label('Invoice Number')
  86. ->required(),
  87. Forms\Components\TextInput::make('order_number')
  88. ->label('Order Number')
  89. ->nullable(),
  90. ])->columnSpan(2),
  91. ])->columns(3),
  92. ])->columns(3),
  93. ]);
  94. }
  95. public static function table(Table $table): Table
  96. {
  97. return $table
  98. ->columns([
  99. //
  100. ])
  101. ->filters([
  102. //
  103. ])
  104. ->actions([
  105. Tables\Actions\EditAction::make(),
  106. ])
  107. ->bulkActions([
  108. Tables\Actions\DeleteBulkAction::make(),
  109. ]);
  110. }
  111. public static function getSlug(): string
  112. {
  113. return '{company}/sales/invoices';
  114. }
  115. public static function getUrl($name = 'index', $params = [], $isAbsolute = true): string
  116. {
  117. $routeBaseName = static::getRouteBaseName();
  118. return route("{$routeBaseName}.{$name}", [
  119. 'company' => Auth::user()->currentCompany,
  120. 'record' => $params['record'] ?? null,
  121. ], $isAbsolute);
  122. }
  123. public static function getRelations(): array
  124. {
  125. return [
  126. RelationManagers\DocumentItemsRelationManager::class,
  127. ];
  128. }
  129. public static function getPages(): array
  130. {
  131. return [
  132. 'index' => Pages\ListInvoices::route('/'),
  133. 'create' => Pages\CreateInvoice::route('/create'),
  134. 'edit' => Pages\EditInvoice::route('/{record}/edit'),
  135. ];
  136. }
  137. }