Skip to content

Refactor: replace duplicated sale-mode if/else in Sales.php with polymorphism #4645

Open

Description

@objecttothis

Context

In app/Controllers/Sales.php (around lines 780-970), the sale-completion flow branches on sale mode (is_invoice_mode(), is_work_order_mode(), is_quote_mode(), else/POS) using a large if/elseif/else chain. Each branch:

  1. Does mode-specific setup (numbers, sale_status, sale_type)
  2. Calls $this->sale->save_value(...)
  3. Repeats an identical error-check block on the result:
    if ($data['sale_id_num'] === INSUFFICIENT_GIFTCARD_BALANCE) {
        $data['error_message'] = lang('Sales.insufficient_giftcard_balance');
        return $this->reload($data);
    } elseif ($data['sale_id_num'] === INSUFFICIENT_REWARD_POINTS) {
        $data['error_message'] = lang('Sales.insufficient_reward_points');
        return $this->reload($data);
    } elseif ($data['sale_id_num'] == NEW_ENTRY) {
        $data['error_message'] = lang('Sales.transaction_failed');
        return $this->reload($data);
    }
    This block is duplicated 4 times (invoice ~L849-857, work order ~L889-898, quote ~L929-938, POS/return ~L963+).
  4. Renders a mode-specific view.

This came up in PR review (comments on L849-862): "might be for the future but this big if/else feels like this should have been solved with polymorphism" and "if using polymorphism we can call the class method and then after that this code can be shared and does not need to be duplicated."

Proposed fix

Introduce a SaleModeHandler interface with one implementation per mode (InvoiceSaleHandler, WorkOrderSaleHandler, QuoteSaleHandler, PosSaleHandler), each responsible for:

  • mode-specific pre-save setup (generating/validating invoice, work order, or quote numbers; setting sale_status/sale_type)
  • the mode-specific view name to render on success

The controller then:

  1. Selects the handler for the current mode (small factory/map based on the existing sale_lib mode checks)
  2. Calls the handler's setup method
  3. Calls the shared $this->sale->save_value(...)
  4. Runs the error-check block once (not duplicated)
  5. Calls the handler's render step

This preserves current behavior (same error messages, same view names, same order of operations) while removing the 4x duplicated error-check block and collapsing mode-specific logic into per-mode classes.

Scope / risk notes

  • Sales.php is a large controller and this is the core POS checkout path — needs solid test coverage before/after to guarantee no behavior regression.
  • Should be scoped as its own refactor PR, not bundled with unrelated changes.
  • Early-return semantics (return $this->reload($data)) inside each branch need to map cleanly onto the new handler-based flow.

Prompt for AI coding agents

```
Refactor app/Controllers/Sales.php to eliminate duplicated sale-mode branching logic using polymorphism.

Context: the sale-completion method branches on sale mode via $this->sale_lib->is_invoice_mode(), is_work_order_mode(), is_quote_mode(), and an else/default (POS/return) branch. Each branch does mode-specific setup, calls $this->sale->save_value(...), then repeats an identical error-check block against the returned $data['sale_id_num'] (checking against INSUFFICIENT_GIFTCARD_BALANCE, INSUFFICIENT_REWARD_POINTS, and NEW_ENTRY constants), then renders a mode-specific view.

Task:

  1. Define a SaleModeHandler interface (or abstract class) with methods for: preparing mode-specific sale data prior to save (invoice/work order/quote number generation and validation, setting sale_status and sale_type), and rendering the mode-specific view/result after a successful save.
  2. Implement one handler per mode: InvoiceSaleHandler, WorkOrderSaleHandler, QuoteSaleHandler, PosSaleHandler (covering both POS and return sale types).
  3. In Sales.php, replace the if/elseif/else chain with: select the correct handler for the current mode, call its pre-save setup, call the existing $this->sale->save_value(...) once, run ONE shared copy of the error-check block, then call the handler's post-save render step.
  4. Preserve exact current behavior: identical error messages (via lang()), identical view names rendered, identical order of side effects (e.g. $this->sale_lib->set_suspended_id(...), sort_and_filter_cart(...), clear_all(), barcode generation), and identical early-return behavior on validation failures (duplicate invoice/work order/quote number checks stay before save_value is called).
  5. Do not change the public route/controller method signatures or any view templates.
  6. Add or update tests covering all four modes (invoice, work order, quote, POS/return) including the three error paths (insufficient giftcard balance, insufficient reward points, failed save) to confirm behavior is unchanged before and after the refactor.
    ```

References

  • File: app/Controllers/Sales.php

Activity

  1. objecttothis commented on Oct 22, 2026

    @objecttothis
    MemberAuthor

    Claude Issue Checker Agent (cc @objecttothis)

    Investigated against current master.

    Status: Still present (refactor not done).

    No SaleModeHandler interface or per-mode handler classes (InvoiceSaleHandler, WorkOrderSaleHandler, QuoteSaleHandler, PosSaleHandler) exist in the codebase. Sales::postComplete() in app/Controllers/Sales.php is still one large method branching with if ($this->sale_lib->is_invoice_mode()) {...} elseif ($this->sale_lib->is_work_order_mode()) {...} elseif ($this->sale_lib->is_quote_mode()) {...} else {...}, each branch duplicating its own setup, save_value() call, and identical error-check block, matching the pre-refactor state described in this issue.

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