Skip to content

Commit

Permalink
Merge pull request #90 from khalidmaquilang/feature/si-100-reference-…
Browse files Browse the repository at this point in the history
…number-on-sales

SI-100 | Feature | add reference number
  • Loading branch information
khalidmaquilang authored Jun 18, 2024
2 parents e316ca8 + f6f3216 commit f8eeadd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 40 deletions.
90 changes: 53 additions & 37 deletions app/Filament/Resources/SaleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ function ($component) {
->label('VAT (Value-Added Tax)')
->suffix('%')
->lazy()
->default(0)
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get) {
self::updateTotalAmount($get, $set);
})
Expand Down Expand Up @@ -183,27 +184,32 @@ function ($component) {
->label('Total amount')
->suffix($currency)
->disabled(),
Forms\Components\Select::make('payment_type_id')
->relationship('paymentType', 'name')
->required(),
Forms\Components\Group::make([
Forms\Components\Select::make('payment_type_id')
->relationship('paymentType', 'name')
->required(),
Forms\Components\TextInput::make('reference_number'),
])
->columns(2),
Forms\Components\Group::make([
Forms\Components\TextInput::make('paid_amount')
->suffix($currency)
->default(0)
->required()
->minValue(0)
->maxValue(fn ($get) => floatval(str_replace(',', '', $get('total_amount'))) ?? 0)
->maxValue(fn($get) => floatval(str_replace(',', '', $get('total_amount'))) ?? 0)
->numeric(),
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('pay_full')
->label('Pay in full')
->color('success')
->action(
fn ($set, $get) => $set(
fn($set, $get) => $set(
'paid_amount',
str_replace(',', '', $get('total_amount'))
)
)
->visible(fn ($operation) => $operation === 'create'),
->visible(fn($operation) => $operation === 'create'),
]),
]),
]),
Expand All @@ -215,17 +221,20 @@ public static function table(Table $table): Table
return $table
->columns([
Tables\Columns\TextColumn::make('invoice_number')
->searchable(),
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('sale_date')
->date()
->sortable(),
Tables\Columns\TextColumn::make('pay_until')
->label('Due Date')
->formatStateUsing(fn ($state) => now()->addDays($state)->format('M d, Y')),
->formatStateUsing(fn($state) => now()->addDays($state)->format('M d, Y'))
->sortable(),
Tables\Columns\TextColumn::make('total_amount')
->money(fn ($record) => $record->company->getCurrency()),
->money(fn($record) => $record->company->getCurrency())
->sortable(),
Tables\Columns\TextColumn::make('remaining_amount')
->money(fn ($record) => $record->company->getCurrency())
->money(fn($record) => $record->company->getCurrency())
->sortable(),
Tables\Columns\TextColumn::make('customer.name')
->numeric()
Expand All @@ -234,7 +243,8 @@ public static function table(Table $table): Table
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('user.name')
->label('Created By'),
->label('Created By')
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
Expand All @@ -256,35 +266,40 @@ public static function table(Table $table): Table
Tables\Actions\ActionGroup::make([
Tables\Actions\Action::make('Pay Amount')
->form([
TextInput::make('paid_amount')
->hint(function ($record) {
return 'You need to pay '.$record->formatted_remaining_amount;
})
->minValue(1)
->maxValue(function ($record): float {
return $record->remaining_amount;
})
->numeric()
->required()
->hintAction(
Forms\Components\Actions\Action::make('pay_in_full')
->icon('heroicon-m-arrow-down-tray')
->action(function (Forms\Set $set, $state, $record) {
$set('paid_amount', $record->remaining_amount);
})
),
Forms\Components\Group::make([
TextInput::make('paid_amount')
->hint(function ($record) {
return 'You need to pay '.$record->formatted_remaining_amount;
})
->minValue(1)
->maxValue(function ($record): float {
return $record->remaining_amount;
})
->numeric()
->required()
->hintAction(
Forms\Components\Actions\Action::make('pay_in_full')
->icon('heroicon-m-arrow-down-tray')
->action(function (Forms\Set $set, $state, $record) {
$set('paid_amount', $record->remaining_amount);
})
),
TextInput::make('reference_number'),
])
->columns(2),
])
->color('info')
->icon('heroicon-m-banknotes')
->visible(fn ($record) => $record->remaining_amount > 0)
->visible(fn($record) => $record->remaining_amount > 0)
->action(function ($record, array $data) {
$record->paid_amount += $data['paid_amount'];
$record->reference_number = $data['reference_number'];
$record->save();
}),
Tables\Actions\Action::make('Download Invoice')
->icon('heroicon-o-document-arrow-down')
->color('success')
->url(fn (Sale $record) => route('app.sales.generate-invoice', [
->url(fn(Sale $record) => route('app.sales.generate-invoice', [
'company' => session('company_id'),
'sale' => $record,
]))
Expand All @@ -295,7 +310,8 @@ public static function table(Table $table): Table
Tables\Actions\BulkActionGroup::make([
ExportBulkAction::make(),
]),
]);
])
->defaultSort('sale_date', 'desc');
}

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

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

// Update the state with the new values
Expand All @@ -353,21 +369,21 @@ public static function updateSubTotal(Forms\Get $get, Forms\Set $set): void
*/
public static function updateTotalAmount(Forms\Get $get, Forms\Set $set): void
{
$subTotal = (float) str_replace(',', '', $get('sub_total'));
$subTotal = (float)str_replace(',', '', $get('sub_total'));
$vatField = $get('vat');
$discount = (float) str_replace(',', '', $get('discount'));
$discount = (float)str_replace(',', '', $get('discount'));
$discountType = $get('discount_type');

if (empty($subTotal)) {
$subTotal = 0;
}

if (! empty($discount)) {
if (!empty($discount)) {
$subTotal = self::calculateAfterDiscount($subTotal, $discount, $discountType);
}

$vat = 0;
if (! empty($vatField)) {
if (!empty($vatField)) {
$vat = $subTotal * ($vatField / 100);
}

Expand Down
7 changes: 4 additions & 3 deletions app/Filament/Resources/SaleResource/Pages/ViewSales.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ public function infolist(Infolist $infolist): Infolist
->date(),
TextEntry::make('pay_until')
->label('Due Date')
->formatStateUsing(fn ($state) => now()->addDays($state)->format('M d, Y')),
->formatStateUsing(fn($state) => now()->addDays($state)->format('M d, Y')),
TextEntry::make('customer.name'),
Fieldset::make('Payment Information')
->schema([
TextEntry::make('vat'),
TextEntry::make('formatted_discount')
->label('Discount'),
TextEntry::make('total_amount')
->formatStateUsing(fn ($state) => number_format($state, 2).' '.$currency),
->formatStateUsing(fn($state) => number_format($state, 2).' '.$currency),
TextEntry::make('paid_amount')
->formatStateUsing(fn ($state) => number_format($state, 2).' '.$currency),
->formatStateUsing(fn($state) => number_format($state, 2).' '.$currency),
TextEntry::make('paymentType.name'),
TextEntry::make('reference_number'),
]),
TextEntry::make('notes'),
]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('sales', function (Blueprint $table) {
$table->string('reference_number')->nullable()->after('payment_type_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('sales', function (Blueprint $table) {
$table->dropColumn('reference_number');
});
}
};

0 comments on commit f8eeadd

Please sign in to comment.