Skip to content

Example: General Journal (Grid + Totals)

A full-screen editable grid with live totals and a screen action.

php
<?php

namespace App\TwoWee\Resources;

use App\Models\GlAccount;
use TwoWee\Laravel\Actions\Action;
use TwoWee\Laravel\Actions\ActionResult;
use TwoWee\Laravel\Columns\DateColumn;
use TwoWee\Laravel\Columns\DecimalColumn;
use TwoWee\Laravel\Columns\OptionColumn;
use TwoWee\Laravel\Columns\TextColumn;
use TwoWee\Laravel\GridResource;

class JournalResource extends GridResource
{
    protected static string $model = \App\Models\JournalLine::class;

    protected static string $label = 'General Journal';

    public static function lineColumns(): array
    {
        return [
            DateColumn::make('posting_date')
                ->label('Posting Date')
                ->editable()
                ->width(12),
            OptionColumn::make('document_type')
                ->label('Document Type')
                ->editable()
                ->width(14)
                ->options(['Invoice', 'Credit Memo', 'Payment']),
            TextColumn::make('document_no')
                ->label('Document No.')
                ->editable()
                ->width(15),
            OptionColumn::make('account_type')
                ->label('Account Type')
                ->editable()
                ->width(14)
                ->options(['G/L Account', 'Customer', 'Vendor', 'Bank Account']),
            TextColumn::make('account_no')
                ->label('Account No.')
                ->editable()
                ->width(15)
                ->lookup(GlAccount::class, valueColumn: 'no')
                ->autofill(['name' => 'description']),
            TextColumn::make('description')
                ->label('Description')
                ->editable()
                ->width('fill'),
            DecimalColumn::make('amount')
                ->label('Amount')
                ->editable()
                ->width(15)
                ->align('right'),
        ];
    }

    public static function totals(?\Illuminate\Database\Eloquent\Model $model = null): array
    {
        return [
            [
                'label' => 'Balance',
                'value' => '0.00',
                'source_column' => 'amount',
                'aggregate' => 'sum',
            ],
        ];
    }

    public static function screenActions(?\Illuminate\Database\Eloquent\Model $model = null): array
    {
        return [
            Action::make('post')
                ->label('Post Journal')
                ->requiresConfirmation('Post all journal lines? This cannot be undone.')
                ->action(function () {
                    // JournalLine::query()->each(fn ($line) => $line->post());
                    // JournalLine::query()->delete();
                    return ActionResult::success('Journal posted successfully.');
                }),
        ];
    }
}

Key Features Demonstrated

  • GridResource — sets layout() to 'Grid' and provides an empty form(); implement lineColumns() only
  • totals() with source_column — the Balance total updates in real-time as the user enters amounts
  • ->lookup(GlAccount::class, valueColumn: 'no') — inline lookup with autofill into the description column
  • ->requiresConfirmation() — user must confirm before the post action executes