Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yonka's avatar

The best way to create customer statement (Paid and balance amounts)

I want to create customer statement and send to my customers, So I have these invoices

  1. Order Invoices: All orders customer have made
class OrderInvoice extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['order_id','invoice_no','reference_no','issue_date','due_date','sub_total','discount','total_amount','balance','invoice_status','customer_id'];

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function orderInvoiceReceipts()
    {
        return $this->hasMany(OrderInvoiceReceipt::class);
    }

    public function customer() :BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }
}

2.Sales Invoice: all sales customer have made

class SalesInvoice extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['sold_inventory_id','customer_id','invoice_no','reference_no','issue_date','sub_total','vat','discount','total_amount','balance','invoice_status'];

    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function salesInvoiceReceipts()
    {
        return $this->hasMany(SalesInvoiceReceipt::class);
    }

    public function customer() :BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }
}

3.OrderInvoiceReceipts: all orders customer has made payments

class OrderInvoiceReceipt extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['order_invoice_id','company_account_id','reference_no','receipt_no','paid_amount','balance','paid_date','status','description'];

    public function orderInvoice()
    {
        return $this->belongsTo(OrderInvoice::class);
    }

    public function companyAccount(): BelongsTo
    {
        return $this->belongsTo(CompanyAccount::class);
    }

    public function companyAccountTransaction(): HasOne
    {
        return $this->hasOne(CompanyAccountTransaction::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

4.SalesInvoiceReceipts: all sales customer has made payments

class SalesInvoiceReceipt extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['sales_invoice_id','company_account_id','customer_id','receipt_no','reference_no','paid_amount','paid_date','balance','status','description'];

    public function companyAccount()
    {
        return $this->belongsTo(CompanyAccount::class);
    }

    public function salesInvoice()
    {
        return $this->belongsTo(SalesInvoice::class);
    }

    public function companyAccountTransaction(): HasOne
    {
        return $this->hasOne(CompanyAccountTransaction::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

In my system, Orders and Sales are different, Orders are the things company will manufacture and has special invoice while sales are the raw materials company is selling. So I want to create customer statement, In order to send customer full report of the balances that the company is owed to the customer with detailed report. for instance I want to read all orderInvoices and salesInvoices that has balances and also want to read all orderInvoiceReceipts and SalesInvoiceReceipts of these oderInvoice and salesInvoices respectively, I want to read Date, Reference no/ InvoiceNo/ Receipt No, Description ( to tell weather this is order or sales), total Amount charged if it is orderInvoice or salesInvoice, payments (orderInvoiceReceipts/SalesInvoiceReceipts) and Balance. From date to Date, if there is balance before from_date, I want to forward and add it. I am using Laravel and livewire

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here’s a structured approach to generate a customer statement in Laravel (with Livewire), including all invoices (orders/sales), receipts, balances, and a running total, with support for a date range and opening balance.


1. Define the Statement Query Logic

You want to:

  • Get all OrderInvoices and SalesInvoices for a customer within a date range, with their receipts.
  • Get all OrderInvoiceReceipts and SalesInvoiceReceipts for that customer within the same range.
  • Calculate the opening balance (before from_date).
  • Show a running balance.

2. Sample Livewire Component Method


3. How to Use in Livewire/View

You can call this method in your Livewire component and pass the result to your view for display as a table.

public $customerId, $fromDate, $toDate, $statement;

public function mount($customerId)
{
    $this->customerId = $customerId;
    $this->fromDate = now()->subMonth()->toDateString();
    $this->toDate = now()->toDateString();
    $this->loadStatement();
}

public function loadStatement()
{
    $this->statement = $this->getCustomerStatement($this->customerId, $this->fromDate, $this->toDate);
}

And in your Blade view:

<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Reference</th>
            <th>Description</th>
            <th>Amount</th>
            <th>Payment</th>
            <th>Balance</th>
        </tr>
    </thead>
    <tbody>
        @foreach($statement as $row)
            <tr>
                <td>{{ $row['date'] }}</td>
                <td>{{ $row['reference'] }}</td>
                <td>{{ $row['description'] }}</td>
                <td>{{ $row['amount'] ?: '' }}</td>
                <td>{{ $row['payment'] ?: '' }}</td>
                <td>{{ $row['balance'] }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

4. Notes

  • You can add more fields (like invoice status, etc.) as needed.
  • This approach is flexible: you can add filters, export to PDF, etc.
  • For performance, consider eager loading relationships if you have a lot of data.

Summary:
This solution gives you a full, date-ranged customer statement with opening balance, all invoices, all receipts, and a running balance, ready to display or send to your customers.

Please or to participate in this conversation.