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.

DocumentResource.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting;
  3. use App\Filament\Company\Resources\Accounting\DocumentResource\Pages;
  4. use App\Models\Accounting\Document;
  5. use App\Models\Common\Offering;
  6. use Awcodes\TableRepeater\Components\TableRepeater;
  7. use Awcodes\TableRepeater\Header;
  8. use Filament\Forms;
  9. use Filament\Forms\Components\FileUpload;
  10. use Filament\Forms\Form;
  11. use Filament\Resources\Resource;
  12. use Filament\Support\Enums\MaxWidth;
  13. use Filament\Tables;
  14. use Filament\Tables\Table;
  15. use Illuminate\Support\Facades\Auth;
  16. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  17. class DocumentResource extends Resource
  18. {
  19. protected static ?string $model = Document::class;
  20. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  21. public static function form(Form $form): Form
  22. {
  23. $company = Auth::user()->currentCompany;
  24. return $form
  25. ->schema([
  26. Forms\Components\Section::make('Invoice Header')
  27. ->collapsible()
  28. ->schema([
  29. Forms\Components\Split::make([
  30. Forms\Components\Group::make([
  31. FileUpload::make('logo')
  32. ->openable()
  33. ->maxSize(1024)
  34. ->localizeLabel()
  35. ->visibility('public')
  36. ->disk('public')
  37. ->directory('logos/document')
  38. ->imageResizeMode('contain')
  39. ->imageCropAspectRatio('3:2')
  40. ->panelAspectRatio('3:2')
  41. ->maxWidth(MaxWidth::ExtraSmall)
  42. ->panelLayout('integrated')
  43. ->removeUploadedFileButtonPosition('center bottom')
  44. ->uploadButtonPosition('center bottom')
  45. ->uploadProgressIndicatorPosition('center bottom')
  46. ->getUploadedFileNameForStorageUsing(
  47. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  48. ->prepend(Auth::user()->currentCompany->id . '_'),
  49. )
  50. ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif']),
  51. ]),
  52. Forms\Components\Group::make([
  53. Forms\Components\TextInput::make('header')
  54. ->default(fn () => $company->defaultInvoice->header),
  55. Forms\Components\TextInput::make('subheader')
  56. ->default(fn () => $company->defaultInvoice->subheader),
  57. Forms\Components\View::make('filament.forms.components.company-info')
  58. ->viewData([
  59. 'company_name' => $company->name,
  60. 'company_address' => $company->profile->address,
  61. 'company_city' => $company->profile->city?->name,
  62. 'company_state' => $company->profile->state?->name,
  63. 'company_zip' => $company->profile->zip_code,
  64. 'company_country' => $company->profile->state?->country->name,
  65. ]),
  66. ])->grow(true),
  67. ])->from('md'),
  68. ]),
  69. Forms\Components\Section::make('Invoice Details')
  70. ->schema([
  71. Forms\Components\Split::make([
  72. Forms\Components\Group::make([
  73. Forms\Components\Select::make('client_id')
  74. ->relationship('client', 'name'),
  75. ]),
  76. Forms\Components\Group::make([
  77. Forms\Components\TextInput::make('document_number')
  78. ->label('Invoice Number')
  79. ->default(fn () => $company->defaultInvoice->getNumberNext()),
  80. Forms\Components\TextInput::make('order_number')
  81. ->label('P.O/S.O Number'),
  82. Forms\Components\DatePicker::make('date')
  83. ->label('Invoice Date')
  84. ->default(now()),
  85. Forms\Components\DatePicker::make('due_date')
  86. ->label('Payment Due')
  87. ->default(function () use ($company) {
  88. return now()->addDays($company->defaultInvoice->payment_terms->getDays());
  89. }),
  90. ])->grow(true),
  91. ])->from('md'),
  92. TableRepeater::make('lineItems')
  93. ->relationship()
  94. ->headers([
  95. Header::make('Items'),
  96. Header::make('Description'),
  97. Header::make('Quantity'),
  98. Header::make('Price'),
  99. Header::make('Amount'),
  100. ])
  101. ->schema([
  102. Forms\Components\Select::make('offering_id')
  103. ->relationship('offering', 'name')
  104. ->preload()
  105. ->searchable()
  106. ->required()
  107. ->live()
  108. ->afterStateUpdated(function (Forms\Set $set, $state) {
  109. $offeringId = $state;
  110. $offeringRecord = Offering::find($offeringId);
  111. $set('description', $offeringRecord->description);
  112. $set('unit_price', $offeringRecord->price);
  113. $set('total', $offeringRecord->price);
  114. }),
  115. Forms\Components\TextInput::make('description')
  116. ->required(),
  117. Forms\Components\TextInput::make('quantity')
  118. ->required()
  119. ->numeric()
  120. ->live()
  121. ->default(1),
  122. Forms\Components\Group::make([
  123. Forms\Components\TextInput::make('unit_price')
  124. ->hiddenLabel()
  125. ->live()
  126. ->numeric()
  127. ->default(0),
  128. ]),
  129. Forms\Components\Placeholder::make('total')
  130. ->hiddenLabel()
  131. ->content(function (Forms\Get $get) {
  132. $quantity = $get('quantity');
  133. $unitPrice = $get('unit_price');
  134. if ($quantity && $unitPrice) {
  135. return $quantity * $unitPrice;
  136. }
  137. }),
  138. ]),
  139. Forms\Components\Textarea::make('terms')
  140. ->columnSpanFull(),
  141. ]),
  142. Forms\Components\Section::make('Invoice Footer')
  143. ->collapsible()
  144. ->schema([
  145. Forms\Components\Textarea::make('footer')
  146. ->columnSpanFull(),
  147. ]),
  148. ]);
  149. }
  150. public static function table(Table $table): Table
  151. {
  152. return $table
  153. ->columns([
  154. Tables\Columns\TextColumn::make('company.name')
  155. ->numeric()
  156. ->sortable(),
  157. Tables\Columns\TextColumn::make('client.name')
  158. ->numeric()
  159. ->sortable(),
  160. Tables\Columns\TextColumn::make('vendor.name')
  161. ->numeric()
  162. ->sortable(),
  163. Tables\Columns\TextColumn::make('type')
  164. ->searchable(),
  165. Tables\Columns\TextColumn::make('logo')
  166. ->searchable(),
  167. Tables\Columns\TextColumn::make('header')
  168. ->searchable(),
  169. Tables\Columns\TextColumn::make('subheader')
  170. ->searchable(),
  171. Tables\Columns\TextColumn::make('document_number')
  172. ->searchable(),
  173. Tables\Columns\TextColumn::make('order_number')
  174. ->searchable(),
  175. Tables\Columns\TextColumn::make('date')
  176. ->date()
  177. ->sortable(),
  178. Tables\Columns\TextColumn::make('due_date')
  179. ->date()
  180. ->sortable(),
  181. Tables\Columns\TextColumn::make('status')
  182. ->searchable(),
  183. Tables\Columns\TextColumn::make('currency_code')
  184. ->searchable(),
  185. Tables\Columns\TextColumn::make('subtotal')
  186. ->numeric()
  187. ->sortable(),
  188. Tables\Columns\TextColumn::make('tax_total')
  189. ->numeric()
  190. ->sortable(),
  191. Tables\Columns\TextColumn::make('discount_total')
  192. ->numeric()
  193. ->sortable(),
  194. Tables\Columns\TextColumn::make('total')
  195. ->numeric()
  196. ->sortable(),
  197. Tables\Columns\TextColumn::make('amount_paid')
  198. ->numeric()
  199. ->sortable(),
  200. Tables\Columns\TextColumn::make('amount_due')
  201. ->numeric()
  202. ->sortable(),
  203. Tables\Columns\TextColumn::make('created_by')
  204. ->numeric()
  205. ->sortable(),
  206. Tables\Columns\TextColumn::make('updated_by')
  207. ->numeric()
  208. ->sortable(),
  209. Tables\Columns\TextColumn::make('created_at')
  210. ->dateTime()
  211. ->sortable()
  212. ->toggleable(isToggledHiddenByDefault: true),
  213. Tables\Columns\TextColumn::make('updated_at')
  214. ->dateTime()
  215. ->sortable()
  216. ->toggleable(isToggledHiddenByDefault: true),
  217. ])
  218. ->filters([
  219. //
  220. ])
  221. ->actions([
  222. Tables\Actions\EditAction::make(),
  223. ])
  224. ->bulkActions([
  225. Tables\Actions\BulkActionGroup::make([
  226. Tables\Actions\DeleteBulkAction::make(),
  227. ]),
  228. ]);
  229. }
  230. public static function getRelations(): array
  231. {
  232. return [
  233. //
  234. ];
  235. }
  236. public static function getPages(): array
  237. {
  238. return [
  239. 'index' => Pages\ListDocuments::route('/'),
  240. 'create' => Pages\CreateDocument::route('/create'),
  241. 'edit' => Pages\EditDocument::route('/{record}/edit'),
  242. ];
  243. }
  244. }