Skip to main content

bg4h-discount-api

Typed Python pricing engine for BaseGes articles. It resolves article discounts, customer discount tables, promotions, and customer exceptions while preserving BaseGes priority rules.

Installation

python -m pip install bg4h-discount-api

bg4h>=1.0.70 is installed automatically as a required dependency.

Features

  • ✅ Typed pricing models based on dataclasses
  • ✅ Single-article and true batch calculations
  • ✅ BaseGes SQL Server repository included
  • ✅ In-memory reuse of customer and rule data during each operation
  • ✅ Article, family, subfamily, group, key, customer, and customer-type rules
  • ✅ Quantity and minimum-amount eligibility
  • ✅ Deterministic priority resolution and BaseGes tie-breaking
  • ✅ JSON-ready result serialization
  • ✅ No dependency on Frappe or a web framework

Public API

Class or method Description
DiscountCalculator.calculate() Calculate one article or a collection
DiscountCalculator.calculate_many() Calculate a batch with per-item quantities
DiscountCalculator.calculate_dicts() Return JSON-ready dictionaries
DiscountCalculator.clear_cache() Clear compatibility cache state
BaseGesDiscountCalculator.calculate_article() Calculate one article using BaseGes
BaseGesDiscountCalculator.calculate_articles() Calculate a batch using BaseGes
BaseGesPricingRepository.get_product(s) Read one or many articles
BaseGesPricingRepository.get_client() Read customer data
BaseGesPricingRepository.get_discount_*() Read discount-table data
BaseGesPricingRepository.get_active_promotions() Read active promotions
BaseGesPricingRepository.get_promotion_*() Read promotion rules
BaseGesPricingRepository.get_exception_*() Read customer exceptions

Quick start

from datetime import date
import pymssql
from bg4h_discount_api import BaseGesDiscountCalculator

connection = pymssql.connect(server="server", user="user", password="password", database="company")
try:
    calculator = BaseGesDiscountCalculator(connection)
    result = calculator.calculate_article("ART-001", "000123", 5, price_date=date.today())
    print(result.final_unit_price, result.final_amount)
finally:
    connection.close()

Batch pricing

results = calculator.calculate_articles(
    [{"reference": "ART-001", "quantity": 1}, {"reference": "ART-002", "quantity": 3}],
    client_code="000123",
)

Batch methods preserve input order, repeated references, individual quantities, and missing articles while grouping compatible reads.

Examples for every public method

from datetime import date
from decimal import Decimal
from bg4h_discount_api import (
    ArticleRequest, ClientData, DiscountCalculator, DiscountLine, ProductData,
    Promotion, PromotionInfo, BaseGesPricingRepository,
    matching_discount, matching_specificity, parse_quantity,
    promotion_audience_specificity,
)

calculator = DiscountCalculator(my_repository)
one = calculator.calculate("ART-001", "000123", 2)
many = calculator.calculate_many(["ART-001", {"reference": "ART-002", "quantity": 4}], "000123")
payload = calculator.calculate_dicts("ART-001", "000123")
calculator.clear_cache()

repository = BaseGesPricingRepository(connection)
article = repository.get_product("ART-001")
articles = repository.get_products(["ART-001", "ART-002"])
customer = repository.get_client("000123")
table = repository.get_discount_header("D1")
table_lines = repository.get_discount_lines("D1")
promotions = repository.get_active_promotions(date.today())
promotion_lines = repository.get_promotion_lines("P1")
promotion_lines_many = repository.get_promotion_lines_many(["P1", "P2"])
exception = repository.get_exception_header("000123")
exception_lines = repository.get_exception_lines("E1")

request = ArticleRequest.from_value({"reference": "ART-001", "quantity": "5"})
quantity = parse_quantity("2.5")
product = ProductData("ART-001", "Test article", Decimal("100"), family="F1")
line = DiscountLine(Decimal("10"), family="F1")
specificity = matching_specificity(line, product)
discount = matching_discount([line], product, quantity)
customer_data = ClientData("000123", client_type="TYPE-1")
promotion = Promotion("P1", "Offer", date.today(), date.today(), "TYPE-1")
audience_level = promotion_audience_specificity(promotion, customer_data)
info = PromotionInfo("P1", "Offer", "2026-01-01", "2026-12-31", Decimal("1"), Decimal("0"), Decimal("10"), None, True, True, True)
info_payload = info.to_dict()
result_payload = one.to_dict()
article_has_price = one.found

resolve_priority() is an internal engine function invoked by the calculator because it uses internal candidate objects.

Pricing result and priority rules

PricingResult exposes original_price, price_before_discount, discount_percentage, final_unit_price, final_amount, origin, promotion/table/exception codes, and promotions. to_dict() converts Decimal values to JSON-compatible numbers.

The existing BaseGes priority order is preserved: customer exceptions (E/E5), promotions (P/P14), discount tables (D/D4), and article discounts (A) as fallback. Validity, audience, quantity, minimum amount, net prices, and creation-order tie-breaking follow the ERP rules. The IMP-04 case is outside the implemented reference logic.

Custom data sources

Implement PricingRepository and inject it into DiscountCalculator to use another database or test data. Return exported models such as ProductData, ClientData, DiscountLine, and PromotionLine.

Development

python -m pytest --cov=bg4h_discount_api
python -m ruff check src tests
python -m mypy src tests

Metadata

Release files for bg4h-discount-api 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bg4h-discount-api 0.1.1
File Size Uploaded Provenance Actions
bg4h_discount_api-0.1.1.tar.gz 23.5 kB Without provenance Details for bg4h_discount_api-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bg4h-discount-api 0.1.1
File Interpreter ABI Platform Actions
bg4h_discount_api-0.1.1-py3-none-any.whl Python 3 none any Details for bg4h_discount_api-0.1.1-py3-none-any.whl

Total release size: 40.5 kB

Release files / bg4h_discount_api-0.1.1.tar.gz

Download URL bg4h_discount_api-0.1.1.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1c2289af30ce2611dbcff68edef66792cf2e79c24c70cb03bcfc1bf3ad8fcc1c
BLAKE2b-256 checksum
How to use checksums
78dad646ceb9f4e8d9523458339ec4dadb1d46eeaef5223012ace7404dc4f317
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release files / bg4h_discount_api-0.1.1-py3-none-any.whl

Download URL bg4h_discount_api-0.1.1-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2efd30266645c8060b98e871ad838df7dca42e566d4275d0d9b9cc82108648f4
BLAKE2b-256 checksum
How to use checksums
4362ef2d96b42cefada9a7427bafb7105363f6c33958b10a29a090456632605c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Security for bg4h-discount-api 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page