Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InvoiceResource.php 6.0KB

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