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.

InvoiceResource.php 6.1KB

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