Skip to content

ERPNext appears to apply pricing rules even if they're expired #50122

@engahmed1190

Description

@engahmed1190

Information about bug

Summary

Pricing rules with expired validity dates continue to be applied to new sales invoices, even when the invoice posting date is after the
pricing rule's valid_upto date.

Steps to Reproduce

  1. Create a Pricing Rule (e.g., PRLE-0001 "All Customers this week 5%")
    - Set valid_from: 2025-10-06
    - Set valid_upto: 2025-10-13
    - Apply to Item Group: "Demo Item Group"
    - Discount: 5%
    - Status: Active (disable = 0)
  2. Create a Sales Invoice via POS on 2025-10-15 (after expiry date)
    - Add items from "Demo Item Group" (e.g., Camera, Backpack)
    - Complete the sale
  3. Check the invoice items

Expected Behavior

The expired pricing rule (PRLE-0001) should NOT be applied to invoices created after 2025-10-13. Items should be priced without the 5%
discount.

Actual Behavior

The expired pricing rule IS applied to the invoice:

  • Camera: ج.م 900.00 → ج.م 855.00 (5% discount applied)
  • Backpack: ج.م 500.00 → ج.م 475.00 (5% discount applied)
  • Invoice shows: discount_percentage = 5%, discount_amount = 45.00/25.00

Database Evidence

-- Pricing Rule Status
SELECT name, title, valid_from, valid_upto, disable
FROM tabPricing Rule
WHERE name = 'PRLE-0001';

-- Result:
-- PRLE-0001 | All Customers this week 5% | 2025-10-06 | 2025-10-13 | 0

-- Invoice created AFTER expiry
SELECT name, posting_date, creation
FROM tabSales Invoice
WHERE name = 'ACC-SINV-2025-00040';

-- Result:
-- ACC-SINV-2025-00040 | 2025-10-15 | 2025-10-15 22:24:51

-- Discount still applied
SELECT item_code, rate, price_list_rate, discount_percentage, discount_amount
FROM tabSales Invoice Item
WHERE parent = 'ACC-SINV-2025-00040';

-- Result:
-- SKU008 | 475.00 | 500.00 | 5.0 | 25.00

Root Cause

ERPNext Core Behavior: The apply_pricing_rule function in ERPNext's core (erpnext.accounts.doctype.pricing_rule.pricing_rule) does NOT
enforce valid_upto date validation when applying pricing rules during invoice creation.

Code Flow:

  1. Calling ERPNext erpnext_apply_pricing_rule
    pricing_results = erpnext_apply_pricing_rule(pricing_args) or []
  2. The pricing_args includes the correct posting date:
    "posting_date": invoice.get("posting_date") or nowdate(), # 2025-10-15
  3. ERPNext's pricing engine matches rules based on:
    - Item/Item Group/Brand ✓
    - Customer/Customer Group ✓
    - Company ✓
    - BUT ignores valid_upto validation ✗

Impact

  • User Confusion: Customers receive unexpected discounts on expired promotions
  • Revenue Loss: Business loses revenue from unintended discounts
  • Inventory Issues: Expired promotional pricing affects profit margins
  • UI Inconsistency: Expired rules don't appear in POS Next offers dialog (after my fixes), but are still applied by ERPNext core

Module

accounts, selling

Version

frappe 15.75.0 version-15
erpnext 15.72.2 version-15

Installation method

None

Relevant log output / Stack trace / Full Error Message.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions