Skip to content

Refactor Sale::save_value() result handling into a typed outcome #4653

Open

Description

@coderabbitai

Summary

app/Controllers/Sales.php::postComplete() repeats the handling of integer results from Sale::save_value() across invoice, standard sale, work order, and quote flows.

Create a small result abstraction to remove the repeated sentinel checks while preserving the current HTTP behavior.

Required changes

  1. Add a SaleSaveOutcome value object.
    • Represent successful saves with a sale ID.
    • Represent INSUFFICIENT_GIFTCARD_BALANCE.
    • Represent INSUFFICIENT_REWARD_POINTS.
    • Represent generic failure, including the NEW_ENTRY result.
  2. Add a SaleSaveStatus enum or an equivalent explicit status type.
  3. Add a thin SaleSaveService adapter around Sale::save_value().
    • The adapter must convert the current integer result into SaleSaveOutcome.
  4. Update Sales::postComplete() to use the outcome object.
    • Keep reload(), clear_all(), session operations, and view selection in Sales.
    • Preserve the existing error messages and success behavior.
  5. Consider extracting shared work-order and quote behavior only if it remains small and clear.

Rationale

The current controller checks the same save-result sentinels in several branches. A typed outcome makes success and failure states explicit. It also reduces the risk that a new branch handles payment failures but omits NEW_ENTRY, which can otherwise produce a false success response.

Affected areas

  • app/Controllers/Sales.php
  • app/Models/Sale.php
  • New classes under an appropriate application service or value-object namespace
  • Tests for save-result mapping and controller behavior, as applicable

Acceptance criteria

  • Sale::save_value() result values map to explicit outcome states.
  • All existing postComplete() flows show the same localized messages for insufficient gift-card balance, insufficient reward points, and generic transaction failure.
  • Work-order and quote flows do not set a suspended ID or render a success view when the save fails.
  • The controller keeps HTTP rendering and session state management.
  • Existing regression tests pass, and new tests cover the outcome mapping.

Backlinks

Activity

  1. objecttothis commented on Oct 22, 2026

    @objecttothis
    Member

    Claude Issue Checker Agent (cc @objecttothis)

    Investigated against current master.

    Status: Still present (refactor not done).

    No SaleSaveOutcome, SaleSaveStatus, or SaleSaveService classes exist anywhere in the codebase. Sales::postComplete() in app/Controllers/Sales.php still repeats the same raw sentinel check against $data['sale_id_num'] (INSUFFICIENT_GIFTCARD_BALANCE, INSUFFICIENT_REWARD_POINTS, NEW_ENTRY) independently in the invoice, work-order, quote, and POS/return branches — four duplicated copies of the same if/elseif chain, exactly as described in the 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