Skip to content

[FIX] sale_management: update global discount when order lines change#1297

Open
times-odoo wants to merge 3 commits into
odoo:19.0from
odoo-dev:19.0-fix-sale-discount-update-orderlines-times
Open

[FIX] sale_management: update global discount when order lines change#1297
times-odoo wants to merge 3 commits into
odoo:19.0from
odoo-dev:19.0-fix-sale-discount-update-orderlines-times

Conversation

@times-odoo
Copy link
Copy Markdown

@times-odoo times-odoo commented May 26, 2026

Ensure the global discount value dynamically updates or resets when order lines are modified, utilizing a centralized field synchronized with manual line updates.

The implementation covers:

  • Dynamic Updates & Synchronization: Syncs a new global_discount_percentage field with modifications made directly to the line description, re-triggering calculation via an @api.onchange decorator when the subtotal changes.
  • Regex Parsing & Strict Validation: Dynamically extracts percentages using regex (r"(\d+(?:.\d+)?)%"), and raises a strict UserError fallback if the format does not conform to a valid floating-point representation.
  • Localized Precision Formatting: Leverages decimal.precision configurations and float_repr inside localized translation strings (self.env._) to ensure the order line description matches system rounding guidelines perfectly.
  • Automatic Removal: Cleanly identifies and purges global discount lines from the order recordset if all standard merchant product lines are fully deleted.
  • Line Partitioning: Segregates product and discount collections via standard framework operators to prevent cascading dependency loops during matrix evaluation.

Task Reference: 6245598

@robodoo
Copy link
Copy Markdown

robodoo commented May 26, 2026

Pull request status dashboard

@times-odoo times-odoo requested a review from mash-odoo May 26, 2026 12:42
@times-odoo times-odoo force-pushed the 19.0-fix-sale-discount-update-orderlines-times branch from 6dc7b3a to d3b40e6 Compare May 27, 2026 05:21
When a global discount line is applied to a sale order and an order
line is removed, the discount amount was not recalculated, leaving
an incorrect discount value on the order.

Added an onchange on order_line that recomputes the discount line
amount based on the updated subtotal of remaining product lines.
If no product lines remain, the discount line is removed entirely.

task-6245598
@times-odoo times-odoo force-pushed the 19.0-fix-sale-discount-update-orderlines-times branch 3 times, most recently from a05267e to bebdee0 Compare June 1, 2026 06:18
…xt updates

Introduced a tracking field for global discount percentages and added logic
to dynamically update it based on modifications made directly to the
discount line description.

Rationale:
Users need a synchronized flow when modifying global discount values. By
introducing 'global_discount_percentage', the system preserves the raw
numerical float value for other computing methods while allowing changes
made inside the line item description text to drive the value update.
Additionally, extracting the decimal precision dynamically ensures that line
renaming remains visually clean, localized, and compliant with system-configured
currency rounding standards.

Technical choices:
- Added the 'global_discount_percentage' Float field to 'sale.order'.
- Updated '_onchange_update_global_discount' to extract and validate descriptions,
  raising a UserError if a proper float structure is missing.
- Implemented state-change detection to synchronize the field value only
  when 'discount_from_name' shifts.
- Leveraged 'decimal.precision' and 'float_repr' to construct dynamically
  formatted, localized description strings using Odoo's 'self.env._' method.
@times-odoo times-odoo force-pushed the 19.0-fix-sale-discount-update-orderlines-times branch from bebdee0 to 8e51626 Compare June 1, 2026 08:54
…ng notifications on invalid discount names

Refactored the onchange string validation to utilize non-blocking bus
notifications and safely fall back to the original database record state.

Rationale:
Raising a strict 'UserError' inside an '@api.onchange' method breaks the
user's input flow harshly and can cause unstable UI behaviors in the web
client. Replacing the error with a bus notification improves the user
experience by warning them gracefully while resetting the line description
back to its last saved valid state ('self.id.origin'). Additionally, fixed
the wizard mapping computation to correctly store the percentage value as a
whole float number (multiplied by 100) to remain consistent with backend
expectations.

Technical choices:
- Replaced 'UserError' with 'self.env.user._bus_send' to broadcast a
  non-blocking UI warning snippet.
- Leveraged 'self.id.origin' via a database browse to retrieve the last
  committed 'global_discount_percentage' value as a dependable fallback.
- Restructured string evaluation inside the onchange loop to immediately
  return after rebuilding the description string, stopping unexpected line mutation.
- Corrected the transient wizard assignment math by multiplying the scalar representation
  by 100 before writing it to the core sales document.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants