Skip to content

Refactor: Store payment types using a foreign key instead of localized strings #4548

Open

Description

@coderabbitai

Summary

Payment types (e.g. Cash, Debit Card, Bank Transfer, Wallet, etc.) are currently stored in the sales table as the string representation of the payment type label in whatever language the logged-in employee was using at the time of sale.

This creates a data integrity problem: a sale recorded by an English-speaking employee is stored as "Cash", while the same payment type recorded by a Spanish-speaking employee is stored as "Efectivo". Querying, filtering, and reporting across these records becomes inconsistent and unreliable.

Proposed Solution

  1. Create a new static payment_types table with at minimum the following columns:

    • id (primary key, integer)
    • name (canonical/internal identifier, e.g. cash, debit_card, bank_transfer, wallet)
  2. Update the sales (and receiving) tables to store the payment_type_id (foreign key → payment_types.id) instead of the localized string.

  3. Update all queries, filters, and reports that currently match on the localized string (e.g. LIKE lang('Sales.bank_transfer')) to instead use the numeric foreign key.

  4. Keep the language files for display/UI purposes only — translation happens at the presentation layer, not at the storage layer.

Impact

  • app/Models/Sale.php — get_payments_summary() and add_filters_to_query() use LIKE lang('Sales.*') conditions that are locale-dependent.
  • app/Controllers/Reports.php — get_payment_type() returns localized labels used for filtering.
  • app/Controllers/Sales.php — filter keys map to locale-dependent query conditions.
  • app/Helpers/locale_helper.php — get_payment_options() builds options from language strings.
  • app/Models/Receiving.php — get_payment_options() similarly builds options from language strings.

Related PR

This issue was identified during the review of #4547 (Bank Transfer and Wallet payment options), raised by @objecttothis.

Activity

  1. objecttothis commented on Jun 30, 2026

    @objecttothis
    Member

    Additional impact: app/Views/sales/register.php

    The view's check_payment_type() JavaScript function contains hardcoded comparisons against localized payment type strings (PHP-rendered into the JS via <?= lang(...) ?>):

    if (paymentType == "<?= lang('Sales.giftcard') ?>") { ... }
    else if (isCash && cash_mode == '1') { ... }
    else {
        // shows reference code — intended for Credit/Debit only,
        // but catches bank_transfer, wallet, upi, rewards too
        $(".reference-code-input").show();
    }

    Because the else branch caught all non-cash/non-giftcard types, adding Bank Transfer, Wallet, and UPI payment options (PR #4547) caused the Payment Reference Code field to incorrectly appear for those types.

    Interim fix applied: A helper get_reference_code_payment_types(): array was added to locale_helper.php as a single source of truth, used by both the controller validation (postAddPayment()) and the view (JSON-encoded into the JS as referenceCodePaymentTypes). The JS now uses referenceCodePaymentTypes.indexOf(paymentType) !== -1 instead of hardcoded strings.

    This is still locale-string-based. The full FK migration proposed here would allow this JS check to use a stable requires_reference_code boolean flag on the payment type record instead of matching against translated strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions