Skip to content

Commit f8eeadd

Browse files
Merge pull request #90 from khalidmaquilang/feature/si-100-reference-number-on-sales
SI-100 | Feature | add reference number
2 parents e316ca8 + f6f3216 commit f8eeadd

File tree

3 files changed

+85
-40
lines changed

3 files changed

+85
-40
lines changed

app/Filament/Resources/SaleResource.php

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function ($component) {
155155
->label('VAT (Value-Added Tax)')
156156
->suffix('%')
157157
->lazy()
158+
->default(0)
158159
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get) {
159160
self::updateTotalAmount($get, $set);
160161
})
@@ -183,27 +184,32 @@ function ($component) {
183184
->label('Total amount')
184185
->suffix($currency)
185186
->disabled(),
186-
Forms\Components\Select::make('payment_type_id')
187-
->relationship('paymentType', 'name')
188-
->required(),
187+
Forms\Components\Group::make([
188+
Forms\Components\Select::make('payment_type_id')
189+
->relationship('paymentType', 'name')
190+
->required(),
191+
Forms\Components\TextInput::make('reference_number'),
192+
])
193+
->columns(2),
189194
Forms\Components\Group::make([
190195
Forms\Components\TextInput::make('paid_amount')
191196
->suffix($currency)
197+
->default(0)
192198
->required()
193199
->minValue(0)
194-
->maxValue(fn ($get) => floatval(str_replace(',', '', $get('total_amount'))) ?? 0)
200+
->maxValue(fn($get) => floatval(str_replace(',', '', $get('total_amount'))) ?? 0)
195201
->numeric(),
196202
Forms\Components\Actions::make([
197203
Forms\Components\Actions\Action::make('pay_full')
198204
->label('Pay in full')
199205
->color('success')
200206
->action(
201-
fn ($set, $get) => $set(
207+
fn($set, $get) => $set(
202208
'paid_amount',
203209
str_replace(',', '', $get('total_amount'))
204210
)
205211
)
206-
->visible(fn ($operation) => $operation === 'create'),
212+
->visible(fn($operation) => $operation === 'create'),
207213
]),
208214
]),
209215
]),
@@ -215,17 +221,20 @@ public static function table(Table $table): Table
215221
return $table
216222
->columns([
217223
Tables\Columns\TextColumn::make('invoice_number')
218-
->searchable(),
224+
->searchable()
225+
->sortable(),
219226
Tables\Columns\TextColumn::make('sale_date')
220227
->date()
221228
->sortable(),
222229
Tables\Columns\TextColumn::make('pay_until')
223230
->label('Due Date')
224-
->formatStateUsing(fn ($state) => now()->addDays($state)->format('M d, Y')),
231+
->formatStateUsing(fn($state) => now()->addDays($state)->format('M d, Y'))
232+
->sortable(),
225233
Tables\Columns\TextColumn::make('total_amount')
226-
->money(fn ($record) => $record->company->getCurrency()),
234+
->money(fn($record) => $record->company->getCurrency())
235+
->sortable(),
227236
Tables\Columns\TextColumn::make('remaining_amount')
228-
->money(fn ($record) => $record->company->getCurrency())
237+
->money(fn($record) => $record->company->getCurrency())
229238
->sortable(),
230239
Tables\Columns\TextColumn::make('customer.name')
231240
->numeric()
@@ -234,7 +243,8 @@ public static function table(Table $table): Table
234243
->numeric()
235244
->sortable(),
236245
Tables\Columns\TextColumn::make('user.name')
237-
->label('Created By'),
246+
->label('Created By')
247+
->sortable(),
238248
Tables\Columns\TextColumn::make('created_at')
239249
->dateTime()
240250
->sortable()
@@ -256,35 +266,40 @@ public static function table(Table $table): Table
256266
Tables\Actions\ActionGroup::make([
257267
Tables\Actions\Action::make('Pay Amount')
258268
->form([
259-
TextInput::make('paid_amount')
260-
->hint(function ($record) {
261-
return 'You need to pay '.$record->formatted_remaining_amount;
262-
})
263-
->minValue(1)
264-
->maxValue(function ($record): float {
265-
return $record->remaining_amount;
266-
})
267-
->numeric()
268-
->required()
269-
->hintAction(
270-
Forms\Components\Actions\Action::make('pay_in_full')
271-
->icon('heroicon-m-arrow-down-tray')
272-
->action(function (Forms\Set $set, $state, $record) {
273-
$set('paid_amount', $record->remaining_amount);
274-
})
275-
),
269+
Forms\Components\Group::make([
270+
TextInput::make('paid_amount')
271+
->hint(function ($record) {
272+
return 'You need to pay '.$record->formatted_remaining_amount;
273+
})
274+
->minValue(1)
275+
->maxValue(function ($record): float {
276+
return $record->remaining_amount;
277+
})
278+
->numeric()
279+
->required()
280+
->hintAction(
281+
Forms\Components\Actions\Action::make('pay_in_full')
282+
->icon('heroicon-m-arrow-down-tray')
283+
->action(function (Forms\Set $set, $state, $record) {
284+
$set('paid_amount', $record->remaining_amount);
285+
})
286+
),
287+
TextInput::make('reference_number'),
288+
])
289+
->columns(2),
276290
])
277291
->color('info')
278292
->icon('heroicon-m-banknotes')
279-
->visible(fn ($record) => $record->remaining_amount > 0)
293+
->visible(fn($record) => $record->remaining_amount > 0)
280294
->action(function ($record, array $data) {
281295
$record->paid_amount += $data['paid_amount'];
296+
$record->reference_number = $data['reference_number'];
282297
$record->save();
283298
}),
284299
Tables\Actions\Action::make('Download Invoice')
285300
->icon('heroicon-o-document-arrow-down')
286301
->color('success')
287-
->url(fn (Sale $record) => route('app.sales.generate-invoice', [
302+
->url(fn(Sale $record) => route('app.sales.generate-invoice', [
288303
'company' => session('company_id'),
289304
'sale' => $record,
290305
]))
@@ -295,7 +310,8 @@ public static function table(Table $table): Table
295310
Tables\Actions\BulkActionGroup::make([
296311
ExportBulkAction::make(),
297312
]),
298-
]);
313+
])
314+
->defaultSort('sale_date', 'desc');
299315
}
300316

301317
public static function getRelations(): array
@@ -333,12 +349,12 @@ public static function updateSubTotal(Forms\Get $get, Forms\Set $set): void
333349
{
334350
// Retrieve all selected products and remove empty rows
335351
$selectedProducts = collect($get('saleItems'))->filter(
336-
fn ($item) => ! empty($item['product_id']) && ! empty($item['quantity'])
352+
fn($item) => !empty($item['product_id']) && !empty($item['quantity'])
337353
);
338354

339355
// Calculate subtotal based on the selected products and quantities
340356
$subtotal = $selectedProducts->reduce(function ($subtotal, $product) {
341-
return $subtotal + ((float) $product['unit_cost'] * (float) $product['quantity']);
357+
return $subtotal + ((float)$product['unit_cost'] * (float)$product['quantity']);
342358
}, 0);
343359

344360
// Update the state with the new values
@@ -353,21 +369,21 @@ public static function updateSubTotal(Forms\Get $get, Forms\Set $set): void
353369
*/
354370
public static function updateTotalAmount(Forms\Get $get, Forms\Set $set): void
355371
{
356-
$subTotal = (float) str_replace(',', '', $get('sub_total'));
372+
$subTotal = (float)str_replace(',', '', $get('sub_total'));
357373
$vatField = $get('vat');
358-
$discount = (float) str_replace(',', '', $get('discount'));
374+
$discount = (float)str_replace(',', '', $get('discount'));
359375
$discountType = $get('discount_type');
360376

361377
if (empty($subTotal)) {
362378
$subTotal = 0;
363379
}
364380

365-
if (! empty($discount)) {
381+
if (!empty($discount)) {
366382
$subTotal = self::calculateAfterDiscount($subTotal, $discount, $discountType);
367383
}
368384

369385
$vat = 0;
370-
if (! empty($vatField)) {
386+
if (!empty($vatField)) {
371387
$vat = $subTotal * ($vatField / 100);
372388
}
373389

app/Filament/Resources/SaleResource/Pages/ViewSales.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ public function infolist(Infolist $infolist): Infolist
3131
->date(),
3232
TextEntry::make('pay_until')
3333
->label('Due Date')
34-
->formatStateUsing(fn ($state) => now()->addDays($state)->format('M d, Y')),
34+
->formatStateUsing(fn($state) => now()->addDays($state)->format('M d, Y')),
3535
TextEntry::make('customer.name'),
3636
Fieldset::make('Payment Information')
3737
->schema([
3838
TextEntry::make('vat'),
3939
TextEntry::make('formatted_discount')
4040
->label('Discount'),
4141
TextEntry::make('total_amount')
42-
->formatStateUsing(fn ($state) => number_format($state, 2).' '.$currency),
42+
->formatStateUsing(fn($state) => number_format($state, 2).' '.$currency),
4343
TextEntry::make('paid_amount')
44-
->formatStateUsing(fn ($state) => number_format($state, 2).' '.$currency),
44+
->formatStateUsing(fn($state) => number_format($state, 2).' '.$currency),
4545
TextEntry::make('paymentType.name'),
46+
TextEntry::make('reference_number'),
4647
]),
4748
TextEntry::make('notes'),
4849
]),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('sales', function (Blueprint $table) {
15+
$table->string('reference_number')->nullable()->after('payment_type_id');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('sales', function (Blueprint $table) {
25+
$table->dropColumn('reference_number');
26+
});
27+
}
28+
};

0 commit comments

Comments
 (0)