Skip to main content

Adding Previous Due Calculation to Classic Receipts

Credit

This solution is credited to dnull

Overview​

This guide provides a quick frontend solution to display "Previous Due" amounts in Ultimate POS receipts without modifying backend transaction utilities. The fix calculates and displays the previous due amount by subtracting the current total due from the all due amount.

Prerequisites​

  • Basic knowledge of Laravel Blade templates
  • Access to the codebase files

Problem Statement​

The receipt template doesn't show a breakdown of previous due amounts separately from the total due amount. This fix adds a "Previous Due" line item that shows the difference between total due (all sales) and current sale due.

Solution Overview​

The fix involves:

  1. Adding PHP calculation logic to compute previous due
  2. Displaying the previous due amount in the receipt template
  3. Using Laravel Blade syntax for conditional rendering

Step-by-Step Implementation​

Step 1: Locate the Receipt Template​

Navigate to the Classic receipt template file:

resources/views/sale_pos/receipts/classic.blade.php

Step 2: Find the Target Section​

Look for the section containing $receipt_details->total_due. This is typically found in the payment/due summary table around line 200-300.

You'll find a structure similar to:

<!-- Total Due-->
@if(!empty($receipt_details->total_due) && !empty($receipt_details->total_due_label))
<tr>
<th>
{!! $receipt_details->total_due_label !!}
</th>
<td class="text-right">
{{$receipt_details->total_due}}
</td>
</tr>
@endif

@if(!empty($receipt_details->all_due))
<tr>
<th>
{!! $receipt_details->all_bal_label !!}
</th>
<td class="text-right">
{{$receipt_details->all_due}}
</td>
</tr>
@endif

Step 3: Add the PHP Calculation Logic​

Insert the following PHP code block before the existing due amount sections:

@php
function as_decimal(string $currency): float
{
$uf = preg_replace('/[^\d\.\-]/', '', $currency);
return (float) $uf;
}
$all_due_uf = as_decimal($receipt_details->all_due);
$total_due_uf = as_decimal($receipt_details->total_due);
$previous_due = $all_due_uf - $total_due_uf;
$previous_due_formatted = number_format($previous_due, 2);
@endphp

Step 4: Add the Previous Due Display​

Add this HTML/Blade code between the "Total Due" and "All Due" blocks:

@if(!empty($receipt_details->total_due) && !empty($receipt_details->all_due))
<tr>
<th>
Previous Due :
{{-- Or you can add this lang string if you prefer multi-language --}}
{{-- @lang('lang_v1.previous_due') --}}
</th>
<td class="text-right">
<span class="display_currency" data-currency_symbol="true">{{ $previous_due_formatted }}</span>
</td>
</tr>
@endif

Step 5: Complete Implementation​

Your final code structure should look like this:

@php
function as_decimal(string $currency): float
{
$uf = preg_replace('/[^\d\.\-]/', '', $currency);
return (float) $uf;
}
$all_due_uf = as_decimal($receipt_details->all_due);
$total_due_uf = as_decimal($receipt_details->total_due);
$previous_due = $all_due_uf - $total_due_uf;
$previous_due_formatted = number_format($previous_due, 2);
@endphp

<!-- Total Due-->
@if(!empty($receipt_details->total_due) && !empty($receipt_details->total_due_label))
<tr>
<th>
{!! $receipt_details->total_due_label !!}
</th>
<td class="text-right">
{{$receipt_details->total_due}}
</td>
</tr>
@endif

<!-- Previous Due (New Addition) -->
@if(!empty($receipt_details->total_due) && !empty($receipt_details->all_due))
<tr>
<th>
Previous Due :
</th>
<td class="text-right">
<span class="display_currency" data-currency_symbol="true">{{ $previous_due_formatted }}</span>
</td>
</tr>
@endif

<!-- All Due -->
@if(!empty($receipt_details->all_due))
<tr>
<th>
{!! $receipt_details->all_bal_label !!}
</th>
<td class="text-right">
{{$receipt_details->all_due}}
</td>
</tr>
@endif

Previous Due Display Result
Receipt showing the Previous Due amount displayed between Total Due and All Due sections

Alternative: One-Liner Approach​

For a more concise implementation, you can use this simplified version:

@php
$previous_due = floatval(preg_replace('/[^\d\.\-]/', '', $receipt_details->all_due)) - floatval(preg_replace('/[^\d\.\-]/', '', $receipt_details->total_due));
@endphp
@if(!empty($receipt_details->total_due) && !empty($receipt_details->all_due))
<tr>
<th>Previous Due :</th>
<td class="text-right">
<span class="display_currency" data-currency_symbol="true">
{{ $previous_due }}
</span>
</td>
</tr>
@endif

Improved Version with Additional Validation​

Based on community feedback, here's an enhanced version with more robust validation:

@php
// This block will not execute itself until the "previous due" block executes
$previous_due = floatval(preg_replace('/[^\d\.\-]/', '', $receipt_details->all_due)) - floatval(preg_replace('/[^\d\.\-]/', '', $receipt_details->total_due));
@endphp
{{-- We have conditions here which prevents execution if Total due is empty --}}
@if(!empty($receipt_details->total_due) && !empty($receipt_details->total_due_label) && !empty($receipt_details->all_due))
<tr>
<th>Previous Due :</th>
<td class="text-right">
<span class="display_currency" data-currency_symbol="true">
{{ $previous_due }}
</span>
</td>
</tr>
@endif
Important Note

The PHP block doesn't execute by itself until called by the Blade template. The @if conditions already prevent execution when all_due or total_due are empty, so crashes are unlikely. However, adding !empty($receipt_details->total_due_label) provides additional safety.

tip

Choose the detailed approach (Step 5) for better readability and maintainability, or the one-liner approach for minimal code footprint.

Code Explanation​

PHP Function Breakdown​

function as_decimal(string $currency): float
{
$uf = preg_replace('/[^\d\.\-]/', '', $currency);
return (float) $uf;
}

This function:

  • Removes all non-numeric characters except dots and hyphens
  • Converts currency strings to float values for calculation

Calculation Logic​

$all_due_uf = as_decimal($receipt_details->all_due);
$total_due_uf = as_decimal($receipt_details->total_due);
$previous_due = $all_due_uf - $total_due_uf;
$previous_due_formatted = number_format($previous_due, 2);

This calculates:

  • $all_due_uf: Total amount due across all sales
  • $total_due_uf: Amount due for current sale
  • $previous_due: Previous due amount (difference)
  • $previous_due_formatted: Formatted for display

Expected Result​

After implementation, your receipt will display:

Total Due Current Sale    $ 12.50
Previous Due : $ 737.50
Total Due(All Sales) $ 750.00

Multi-Language Support (Optional)​

To add multi-language support:

  1. Add to language files (resources/lang/en/lang_v1.php):

    'previous_due' => 'Previous Due',
  2. Replace the hardcoded text:

    <th>
    @lang('lang_v1.previous_due') :
    </th>

Testing​

  1. Create a test scenario:

    • Customer with existing due amount
    • Make a new sale
    • Generate receipt
  2. Verify calculations:

    • Previous Due + Current Sale Due = Total Due (All Sales)
    • Numbers should match your expectations
  3. Test edge cases:

    • New customer (no previous due)
    • Fully paid invoices
    • Partial payments

Important Notes​

warning

This is a frontend-only solution that doesn't modify backend transaction utilities. For a complete backend solution, consider modifying the transaction utility classes.

tip

The fix only displays when both total_due and all_due values are present, preventing errors on receipts without due amounts.

Troubleshooting​

Common Issues​

  1. Previous Due showing as 0.00

    • Check if $receipt_details->all_due and $receipt_details->total_due have values
    • Verify the as_decimal() function is parsing currency correctly
  2. PHP syntax errors

    • Ensure proper @php and @endphp tags
    • Check for missing semicolons
  3. Display formatting issues

    • Verify display_currency class is available
    • Check CSS styling for table rows

Debugging Steps​

Add debug output to check values:

@php
// Debug output (remove in production)
echo "All Due: " . $receipt_details->all_due . "<br>";
echo "Total Due: " . $receipt_details->total_due . "<br>";
echo "Previous Due: " . $previous_due_formatted . "<br>";
@endphp

Alternative Backend Solution​

For a more robust solution, consider:

  1. Modifying TransactionUtil class
  2. Adding previous_due calculation in controller
  3. Updating receipt data structure

This would provide better performance and maintainability but requires more extensive changes.

Conclusion​

This quick fix provides an immediate solution for displaying previous due amounts in Ultimate POS receipts. While it's a frontend-only solution, it effectively addresses the requirement without extensive backend modifications.

The implementation is backward-compatible and won't affect existing functionality, making it a safe quick fix for immediate deployment.

💛 Support this project

Binance ID:

478036326
Premium Login