|
@@ -3,10 +3,22 @@
|
3
|
3
|
namespace App\Filament\Company\Resources\Purchases;
|
4
|
4
|
|
5
|
5
|
use App\Filament\Company\Resources\Purchases\BillResource\Pages;
|
|
6
|
+use App\Models\Accounting\Adjustment;
|
6
|
7
|
use App\Models\Accounting\Bill;
|
|
8
|
+use App\Models\Accounting\DocumentLineItem;
|
|
9
|
+use App\Models\Common\Offering;
|
|
10
|
+use App\Utilities\Currency\CurrencyAccessor;
|
|
11
|
+use Awcodes\TableRepeater\Components\TableRepeater;
|
|
12
|
+use Awcodes\TableRepeater\Header;
|
|
13
|
+use Carbon\CarbonInterface;
|
|
14
|
+use Filament\Forms;
|
7
|
15
|
use Filament\Forms\Form;
|
8
|
16
|
use Filament\Resources\Resource;
|
|
17
|
+use Filament\Tables;
|
9
|
18
|
use Filament\Tables\Table;
|
|
19
|
+use Illuminate\Database\Eloquent\Model;
|
|
20
|
+use Illuminate\Support\Carbon;
|
|
21
|
+use Illuminate\Support\Facades\Auth;
|
10
|
22
|
|
11
|
23
|
class BillResource extends Resource
|
12
|
24
|
{
|
|
@@ -16,9 +28,228 @@ class BillResource extends Resource
|
16
|
28
|
|
17
|
29
|
public static function form(Form $form): Form
|
18
|
30
|
{
|
|
31
|
+ $company = Auth::user()->currentCompany;
|
|
32
|
+
|
19
|
33
|
return $form
|
20
|
34
|
->schema([
|
21
|
|
- //
|
|
35
|
+ Forms\Components\Section::make('Bill Details')
|
|
36
|
+ ->schema([
|
|
37
|
+ Forms\Components\Split::make([
|
|
38
|
+ Forms\Components\Group::make([
|
|
39
|
+ Forms\Components\Select::make('vendor_id')
|
|
40
|
+ ->relationship('vendor', 'name')
|
|
41
|
+ ->preload()
|
|
42
|
+ ->searchable()
|
|
43
|
+ ->required(),
|
|
44
|
+ ]),
|
|
45
|
+ Forms\Components\Group::make([
|
|
46
|
+ Forms\Components\TextInput::make('bill_number')
|
|
47
|
+ ->label('Bill Number')
|
|
48
|
+ ->default(fn () => Bill::getNextDocumentNumber()),
|
|
49
|
+ Forms\Components\TextInput::make('order_number')
|
|
50
|
+ ->label('P.O/S.O Number'),
|
|
51
|
+ Forms\Components\DatePicker::make('date')
|
|
52
|
+ ->label('Bill Date')
|
|
53
|
+ ->default(now()),
|
|
54
|
+ Forms\Components\DatePicker::make('due_date')
|
|
55
|
+ ->label('Due Date')
|
|
56
|
+ ->default(function () use ($company) {
|
|
57
|
+ return now()->addDays($company->defaultBill->payment_terms->getDays());
|
|
58
|
+ }),
|
|
59
|
+ ])->grow(true),
|
|
60
|
+ ])->from('md'),
|
|
61
|
+ TableRepeater::make('lineItems')
|
|
62
|
+ ->relationship()
|
|
63
|
+ ->saveRelationshipsUsing(function (TableRepeater $component, Forms\Contracts\HasForms $livewire, ?array $state) {
|
|
64
|
+ if (! is_array($state)) {
|
|
65
|
+ $state = [];
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ $relationship = $component->getRelationship();
|
|
69
|
+
|
|
70
|
+ $existingRecords = $component->getCachedExistingRecords();
|
|
71
|
+
|
|
72
|
+ $recordsToDelete = [];
|
|
73
|
+
|
|
74
|
+ foreach ($existingRecords->pluck($relationship->getRelated()->getKeyName()) as $keyToCheckForDeletion) {
|
|
75
|
+ if (array_key_exists("record-{$keyToCheckForDeletion}", $state)) {
|
|
76
|
+ continue;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ $recordsToDelete[] = $keyToCheckForDeletion;
|
|
80
|
+ $existingRecords->forget("record-{$keyToCheckForDeletion}");
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ $relationship
|
|
84
|
+ ->whereKey($recordsToDelete)
|
|
85
|
+ ->get()
|
|
86
|
+ ->each(static fn (Model $record) => $record->delete());
|
|
87
|
+
|
|
88
|
+ $childComponentContainers = $component->getChildComponentContainers(
|
|
89
|
+ withHidden: $component->shouldSaveRelationshipsWhenHidden(),
|
|
90
|
+ );
|
|
91
|
+
|
|
92
|
+ $itemOrder = 1;
|
|
93
|
+ $orderColumn = $component->getOrderColumn();
|
|
94
|
+
|
|
95
|
+ $translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver();
|
|
96
|
+
|
|
97
|
+ foreach ($childComponentContainers as $itemKey => $item) {
|
|
98
|
+ $itemData = $item->getState(shouldCallHooksBefore: false);
|
|
99
|
+
|
|
100
|
+ if ($orderColumn) {
|
|
101
|
+ $itemData[$orderColumn] = $itemOrder;
|
|
102
|
+
|
|
103
|
+ $itemOrder++;
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ if ($record = ($existingRecords[$itemKey] ?? null)) {
|
|
107
|
+ $itemData = $component->mutateRelationshipDataBeforeSave($itemData, record: $record);
|
|
108
|
+
|
|
109
|
+ if ($itemData === null) {
|
|
110
|
+ continue;
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ $translatableContentDriver ?
|
|
114
|
+ $translatableContentDriver->updateRecord($record, $itemData) :
|
|
115
|
+ $record->fill($itemData)->save();
|
|
116
|
+
|
|
117
|
+ continue;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ $relatedModel = $component->getRelatedModel();
|
|
121
|
+
|
|
122
|
+ $itemData = $component->mutateRelationshipDataBeforeCreate($itemData);
|
|
123
|
+
|
|
124
|
+ if ($itemData === null) {
|
|
125
|
+ continue;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ if ($translatableContentDriver) {
|
|
129
|
+ $record = $translatableContentDriver->makeRecord($relatedModel, $itemData);
|
|
130
|
+ } else {
|
|
131
|
+ $record = new $relatedModel;
|
|
132
|
+ $record->fill($itemData);
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ $record = $relationship->save($record);
|
|
136
|
+ $item->model($record)->saveRelationships();
|
|
137
|
+ $existingRecords->push($record);
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ $component->getRecord()->setRelation($component->getRelationshipName(), $existingRecords);
|
|
141
|
+
|
|
142
|
+ /** @var Bill $bill */
|
|
143
|
+ $bill = $component->getRecord();
|
|
144
|
+
|
|
145
|
+ // Recalculate totals for line items
|
|
146
|
+ $bill->lineItems()->each(function (DocumentLineItem $lineItem) {
|
|
147
|
+ $lineItem->updateQuietly([
|
|
148
|
+ 'tax_total' => $lineItem->calculateTaxTotal()->getAmount(),
|
|
149
|
+ 'discount_total' => $lineItem->calculateDiscountTotal()->getAmount(),
|
|
150
|
+ ]);
|
|
151
|
+ });
|
|
152
|
+
|
|
153
|
+ $subtotal = $bill->lineItems()->sum('subtotal') / 100;
|
|
154
|
+ $taxTotal = $bill->lineItems()->sum('tax_total') / 100;
|
|
155
|
+ $discountTotal = $bill->lineItems()->sum('discount_total') / 100;
|
|
156
|
+ $grandTotal = $subtotal + $taxTotal - $discountTotal;
|
|
157
|
+
|
|
158
|
+ $bill->updateQuietly([
|
|
159
|
+ 'subtotal' => $subtotal,
|
|
160
|
+ 'tax_total' => $taxTotal,
|
|
161
|
+ 'discount_total' => $discountTotal,
|
|
162
|
+ 'total' => $grandTotal,
|
|
163
|
+ ]);
|
|
164
|
+ })
|
|
165
|
+ ->headers([
|
|
166
|
+ Header::make('Items')->width('15%'),
|
|
167
|
+ Header::make('Description')->width('25%'),
|
|
168
|
+ Header::make('Quantity')->width('10%'),
|
|
169
|
+ Header::make('Price')->width('10%'),
|
|
170
|
+ Header::make('Taxes')->width('15%'),
|
|
171
|
+ Header::make('Discounts')->width('15%'),
|
|
172
|
+ Header::make('Amount')->width('10%')->align('right'),
|
|
173
|
+ ])
|
|
174
|
+ ->schema([
|
|
175
|
+ Forms\Components\Select::make('offering_id')
|
|
176
|
+ ->relationship('purchasableOffering', 'name')
|
|
177
|
+ ->preload()
|
|
178
|
+ ->searchable()
|
|
179
|
+ ->required()
|
|
180
|
+ ->live()
|
|
181
|
+ ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
|
|
182
|
+ $offeringId = $state;
|
|
183
|
+ $offeringRecord = Offering::with('purchaseTaxes')->find($offeringId);
|
|
184
|
+
|
|
185
|
+ if ($offeringRecord) {
|
|
186
|
+ $set('description', $offeringRecord->description);
|
|
187
|
+ $set('unit_price', $offeringRecord->price);
|
|
188
|
+ $set('purchaseTaxes', $offeringRecord->purchaseTaxes->pluck('id')->toArray());
|
|
189
|
+ $set('purchaseDiscounts', $offeringRecord->purchaseDiscounts->pluck('id')->toArray());
|
|
190
|
+ }
|
|
191
|
+ }),
|
|
192
|
+ Forms\Components\TextInput::make('description'),
|
|
193
|
+ Forms\Components\TextInput::make('quantity')
|
|
194
|
+ ->required()
|
|
195
|
+ ->numeric()
|
|
196
|
+ ->live()
|
|
197
|
+ ->default(1),
|
|
198
|
+ Forms\Components\TextInput::make('unit_price')
|
|
199
|
+ ->hiddenLabel()
|
|
200
|
+ ->numeric()
|
|
201
|
+ ->live()
|
|
202
|
+ ->default(0),
|
|
203
|
+ Forms\Components\Select::make('purchaseTaxes')
|
|
204
|
+ ->relationship('purchaseTaxes', 'name')
|
|
205
|
+ ->preload()
|
|
206
|
+ ->multiple()
|
|
207
|
+ ->live()
|
|
208
|
+ ->searchable(),
|
|
209
|
+ Forms\Components\Select::make('purchaseDiscounts')
|
|
210
|
+ ->relationship('purchaseDiscounts', 'name')
|
|
211
|
+ ->preload()
|
|
212
|
+ ->multiple()
|
|
213
|
+ ->live()
|
|
214
|
+ ->searchable(),
|
|
215
|
+ Forms\Components\Placeholder::make('total')
|
|
216
|
+ ->hiddenLabel()
|
|
217
|
+ ->content(function (Forms\Get $get) {
|
|
218
|
+ $quantity = max((float) ($get('quantity') ?? 0), 0);
|
|
219
|
+ $unitPrice = max((float) ($get('unit_price') ?? 0), 0);
|
|
220
|
+ $purchaseTaxes = $get('purchaseTaxes') ?? [];
|
|
221
|
+ $purchaseDiscounts = $get('purchaseDiscounts') ?? [];
|
|
222
|
+
|
|
223
|
+ $subtotal = $quantity * $unitPrice;
|
|
224
|
+
|
|
225
|
+ // Calculate tax amount based on subtotal
|
|
226
|
+ $taxAmount = 0;
|
|
227
|
+ if (! empty($purchaseTaxes)) {
|
|
228
|
+ $taxRates = Adjustment::whereIn('id', $purchaseTaxes)->pluck('rate');
|
|
229
|
+ $taxAmount = collect($taxRates)->sum(fn ($rate) => $subtotal * ($rate / 100));
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ // Calculate discount amount based on subtotal
|
|
233
|
+ $discountAmount = 0;
|
|
234
|
+ if (! empty($purchaseDiscounts)) {
|
|
235
|
+ $discountRates = Adjustment::whereIn('id', $purchaseDiscounts)->pluck('rate');
|
|
236
|
+ $discountAmount = collect($discountRates)->sum(fn ($rate) => $subtotal * ($rate / 100));
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ // Final total
|
|
240
|
+ $total = $subtotal + ($taxAmount - $discountAmount);
|
|
241
|
+
|
|
242
|
+ return money($total, CurrencyAccessor::getDefaultCurrency(), true)->format();
|
|
243
|
+ }),
|
|
244
|
+ ]),
|
|
245
|
+ Forms\Components\Grid::make(6)
|
|
246
|
+ ->schema([
|
|
247
|
+ Forms\Components\ViewField::make('totals')
|
|
248
|
+ ->columnStart(5)
|
|
249
|
+ ->columnSpan(2)
|
|
250
|
+ ->view('filament.forms.components.bill-totals'),
|
|
251
|
+ ]),
|
|
252
|
+ ]),
|
22
|
253
|
]);
|
23
|
254
|
}
|
24
|
255
|
|
|
@@ -26,7 +257,44 @@ class BillResource extends Resource
|
26
|
257
|
{
|
27
|
258
|
return $table
|
28
|
259
|
->columns([
|
29
|
|
- //
|
|
260
|
+ Tables\Columns\TextColumn::make('status')
|
|
261
|
+ ->badge()
|
|
262
|
+ ->searchable(),
|
|
263
|
+ Tables\Columns\TextColumn::make('due_date')
|
|
264
|
+ ->label('Due')
|
|
265
|
+ ->formatStateUsing(function (Tables\Columns\TextColumn $column, mixed $state) {
|
|
266
|
+ if (blank($state)) {
|
|
267
|
+ return null;
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ $date = Carbon::parse($state)
|
|
271
|
+ ->setTimezone($timezone ?? $column->getTimezone());
|
|
272
|
+
|
|
273
|
+ if ($date->isToday()) {
|
|
274
|
+ return 'Today';
|
|
275
|
+ }
|
|
276
|
+
|
|
277
|
+ return $date->diffForHumans([
|
|
278
|
+ 'options' => CarbonInterface::ONE_DAY_WORDS,
|
|
279
|
+ ]);
|
|
280
|
+ })
|
|
281
|
+ ->sortable(),
|
|
282
|
+ Tables\Columns\TextColumn::make('date')
|
|
283
|
+ ->date()
|
|
284
|
+ ->sortable(),
|
|
285
|
+ Tables\Columns\TextColumn::make('bill_number')
|
|
286
|
+ ->label('Number')
|
|
287
|
+ ->searchable(),
|
|
288
|
+ Tables\Columns\TextColumn::make('vendor.name')
|
|
289
|
+ ->sortable(),
|
|
290
|
+ Tables\Columns\TextColumn::make('total')
|
|
291
|
+ ->currency(),
|
|
292
|
+ Tables\Columns\TextColumn::make('amount_paid')
|
|
293
|
+ ->label('Amount Paid')
|
|
294
|
+ ->currency(),
|
|
295
|
+ Tables\Columns\TextColumn::make('amount_due')
|
|
296
|
+ ->label('Amount Due')
|
|
297
|
+ ->currency(),
|
30
|
298
|
]);
|
31
|
299
|
}
|
32
|
300
|
|