Files
tenant-registry/internal/store/products.go
T
sharangandSharang Parnerkar 1332ad7bc5
ci / shared (push) Successful in 15s
ci / test (push) Successful in 26m35s
ci / image (push) Successful in 53s
feat(erpsetup): catalogue for Leistungsbeschreibung 1.0.0, opt-in -sync-catalog (#115)
Brings the ERPNext sales catalogue in line with the frozen Leistungsbeschreibungen 1.0.0. This PR changes code and tests only. No ERP was called; the live run is yours, using the erpsetup CLI.

## Dry run first (with the flag; only this path prints `field old → new`)

```sh
ERP_BASE_URL=https://erp.breakpilot.com ERP_API_KEY=… ERP_API_SECRET=… ERP_WEBHOOK_SECRET=… \
  go run ./cmd/erpsetup -webhook-url https://registry.breakpilot.com/v1/internal/erp/webhook -sync-catalog -dry-run
```

(`ERP_LEAD_DOCTYPE` is optional, as before.) Leave out `-dry-run` for the real run.

## What a run does with and without `-sync-catalog`

| | without the flag (as today) | with `-sync-catalog` |
|---|---|---|
| New items, unit "Prüftag", their list prices | created | created |
| Item **description** | **updated (this already happens on every run today)** | updated |
| Item `item_name`, `disabled` | untouched | updated when they differ |
| Standard Selling `price_list_rate` (rows without a customer) | set once, never changed | updated when it differs |
| Customer-specific Item Prices | untouched | untouched |
| Quote print format (Mehrverbrauch row) | updated | updated |

**A plain run is not inert.** It creates the 5 new items, the new unit and 5 new prices, rewrites every changed description, and updates the quote template. Only names, the disabled flags and price changes wait for the flag. Nothing is ever deleted. Only the doctypes Item, Item Price and UOM are touched beyond what erpsetup already manages.

## Catalogue changes

| Code | Status | Price old → new (EUR) |
|---|---|---|
| PLT-CORE-CLD-STD/BUS/ENT | keep (name "Breakpilot …", new text) | 290 / 790 / 1990 (unchanged) |
| PLT-BNDL-CLD-STD | **new** Starter-Modulpaket | 990 |
| PLT-BNDL-CLD-ADD | **new** Weiteres Modul | 499 |
| CRA-CSA-CLD-STD | keep | **1490 → 490** |
| CRA-CSA-CLD-BUS | keep | **3990 → 1290** |
| CRA-CSA-CLD-ENT | keep | **9900 → 0** (Preis nach Vereinbarung: a quote shows 0,00 € unless the rate is entered by hand) |
| CRA-CSA-CLD-TGT | **new** extra Prüfobjekt / Monat | 5.99 |
| CRA-CSA-CLD-DAY | **new** Prüftag (unit Prüftag) | 9.99 |
| CRA-CSA-CLD-SCAN | **disable** | 12 (unchanged) |
| CRA-WBK-CLD-STD/BUS/ENT | **disable** | unchanged |
| DEV-WP-CLD-STD | keep ("Werkpilot Cloud") | 59 |
| DEV-WP-CLD-BUS/ENT | **disable** | unchanged |
| DEV-WP-OPR-*, DEV-FWK-OPR-*, INF-GIT-OPR-*, INF-CI-OPR-* | keep, new texts | unchanged |
| DEV-FWK-CLD-STD | keep ("Firmwerk Cloud") | 89 |
| DEV-FWK-CLD-BUS/ENT | **disable** | unchanged |
| INF-GIT-CLD-STD/BUS/ENT | keep, new texts | 190 / 390 / 790 |
| INF-GIT-CLD-USR | **new** extra user | 19 |
| INF-CI-CLD-STD/BUS/ENT | keep, new texts | 149 / 290 / 590 |

The item group stays "BreakPilot Abonnements".

Correction to the spec, applied here: PLT-CORE-CLD-STD no longer mentions "Rechnungen", because LB Plattform §2(3) says the portal provides no invoices. The text is now "Arbeitsbereich mit Anmeldung und Team für bis 25 Nutzer, Standard-Support. Module werden einzeln gebucht." No other item text mentions invoices, and a test now checks that none does.

## Other changes

- `sku.Disabled` is sent to ERPNext as the Item field `disabled: 1`. Disabled items stay in ERPNext so old documents keep working. Since the disabled items already exist on prod, their flag only changes with `-sync-catalog`.
- **Quote, Mehrverbrauch row:** "Zusätzliche Prüfobjekte werden mit {TGT} netto je Monat berechnet, Prüftage nach verbrauchtem Monatskontingent mit {DAY} netto (Leistungsbeschreibung Prüfwerk, Abschnitt 7)." If a rate is missing, the text says "gemäß der jeweils gültigen Preisliste" instead, per rate. If both are missing, one combined sentence is printed. Quotes with INF-GIT-CLD-STD/BUS/ENT or PLT-BNDL-* add the sentence on extra users (Leistungsbeschreibung Breakpilot Plattform, Abschnitt 4). The Jinja was parsed and rendered locally with jinja2 against mocks for eight item mixes.
- **Migration 0026** adds CRA-CSA-CLD-TGT and -DAY to the `pruefwerk` product. SCAN stays so old orders still map. The up is guarded so it can run twice; the down removes the two SKUs. `DefaultProducts` and the ERP mock's SKU list follow. **Run `migrate-up` with the deploy.** Without it, TGT/DAY lines on an order log "sales order item without catalog product".
- **0 € module lines:** nothing needed fixing. Neither the webhook path nor reconcile reads the rate: the webhook template sends only `item_code` and `qty`, and `SubscriptionPlan` has no rate. A new test covers both paths: a bundle order with modules at rate 0 and qty 15 gives an active tenant, plan standard, and seats 15 per module.

## Decisions the spec did not cover

1. **No Prüfwerk Mehrverbrauch sentence on Enterprise quotes.** CRA-CSA-CLD-ENT is "ohne Begrenzung". Edge case: a quote with both -STD and -ENT also prints no Prüfwerk sentence. The extra-users sentence is not affected.
2. The run without the flag still writes descriptions, as `Run` has done until now (see the table).
3. `-sync-catalog` only changes Item Price rows without a `customer`. If there are several such rows for an item, the run reports "not synced". If there are none, it creates the list price.
4. The quote template's Item Price lookup still has no customer filter (unchanged from before).
5. INF-GIT-OPR texts: STD uses the spec text exactly, so "kein Quellcode verlässt das Haus" is gone. BUS keeps "für mittelgroße Teams" and loses "schnellerer Patch-Rhythmus", like CI. ENT says "für große Entwicklungsorganisationen".
6. INF-CI-OPR texts differ only by size ("mindestens 2 vCPU / 4 GB" etc.); "für größere/umfangreiche Builds" was dropped.
7. DEV-WP-OPR and DEV-FWK-OPR texts: the tier phrase sits after "im eigenen Netz", e.g. "Werkpilot im eigenen Netz für mittlere Teams, mit einem vom Kunden betriebenen KI-Modell; …".
8. TGT/DAY quantities on an order or subscription count toward Prüfwerk `seats`, exactly as SCAN already does. Not changed here.
9. Bundle revenue goes to cost center Plattform (PLT-*). Module lines at 0 € book nothing on Prüfwerk, Firmwerk or Werkpilot. Whether to allocate it is an accounting decision for you.
10. The dry run prints texts in full (no truncation), so the wording can be read before it goes live.
11. `TestEverySKUHasAListPrice` now allows exactly one item at 0: CRA-CSA-CLD-ENT.
12. The ERP mock's `DefaultSKUs` now also lists PLT-BNDL-* and CRA-CSA-CLD-TGT/DAY.

## Tests

`go build ./... && go vet ./... && go test ./...` all pass. The Postgres testcontainers suites ran (podman socket) with no skips: server 463 s, migrations 32 s. New tests:

- catalogue consistency: unique codes, units, disabled set, prices including 5.99/9.99, brand spelling
- `TestSyncCatalog`: the dry run writes nothing and names every change; the run updates name, description, disabled and price and keeps the customer price; a second run makes no updates; without the flag only the description changes
- quote template phrases
- `TestMigrate_productsSeedMatchesDefaults`: seed equals `DefaultProducts`, 0026 applied twice, down
- `TestErp_zeroRateModuleLinesEntitle`: webhook and reconcile, memory and Postgres

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com>
Reviewed-on: #115
2026-11-06 16:20:22 +00:00

109 lines
4.7 KiB
Go

package store
import (
"fmt"
"strconv"
"strings"
"time"
)
// DefaultProducts mirrors the seed rows of migrations/0003_products.up.sql,
// 0014_werkmeister_product.up.sql and 0026_pruefwerk_tgt_day_skus.up.sql
// for the Memory store. Keep the two in sync.
func DefaultProducts() []Product {
now := time.Now().UTC()
plans := []string{"standard", "business", "enterprise"}
return []Product{
{
Key: "pruefwerk", Name: "Prüfwerk", Type: "standalone",
Description: "Compliance scanner for CRA / IEC 62443 style assessments: SBOM, DAST, pentest orchestration, PLC provisioning tests.",
LaunchURLs: map[string]string{"stage": "https://pruefwerk-stage.breakpilot.com", "prod": "https://pruefwerk.breakpilot.com"},
ProvisionURLs: map[string]string{},
// SCAN is no longer sold (TGT and DAY replace it) but old orders still map.
SKUs: []string{"CRA-CSA-CLD-STD", "CRA-CSA-CLD-BUS", "CRA-CSA-CLD-ENT", "CRA-CSA-CLD-SCAN", "CRA-CSA-CLD-TGT", "CRA-CSA-CLD-DAY"},
Addons: []ProductAddon{{ID: "werkbank", Name: "Werkbank runner", LimitKey: "runners", SKUs: []string{"CRA-WBK-CLD-STD", "CRA-WBK-CLD-BUS", "CRA-WBK-CLD-ENT"}}},
PlansRequired: plans, SupportsTrial: true, TrialDays: 14, Enabled: true, SortOrder: 10, CreatedAt: now, UpdatedAt: now,
},
{
Key: "werkpilot", Name: "Werkpilot", Type: "cli",
Description: "On-prem AI coding agent for machine and plant manufacturers (CLI). Licensed per named user; talks to llm.breakpilot.com.",
LaunchURLs: map[string]string{"stage": "https://git.breakpilot.com/platform/werkpilot/releases", "prod": "https://git.breakpilot.com/platform/werkpilot/releases"},
ProvisionURLs: map[string]string{},
SKUs: []string{"DEV-WP-CLD-STD", "DEV-WP-CLD-BUS", "DEV-WP-CLD-ENT", "DEV-WP-OPR-STD", "DEV-WP-OPR-BUS", "DEV-WP-OPR-ENT"},
Addons: []ProductAddon{},
PlansRequired: plans, SupportsTrial: true, TrialDays: 14, Enabled: true, SortOrder: 20, CreatedAt: now, UpdatedAt: now,
},
{
Key: "firmwerk", Name: "Firmwerk", Type: "cli",
Description: "Reproducible firmware builds with SBOM / VEX / attestation (CLI + cloud control plane).",
LaunchURLs: map[string]string{"stage": "https://firmwerk-stage.breakpilot.com", "prod": "https://firmwerk.breakpilot.com"},
ProvisionURLs: map[string]string{},
SKUs: []string{"DEV-FWK-CLD-STD", "DEV-FWK-CLD-BUS", "DEV-FWK-CLD-ENT", "DEV-FWK-OPR-STD", "DEV-FWK-OPR-BUS", "DEV-FWK-OPR-ENT"},
Addons: []ProductAddon{},
PlansRequired: plans, SupportsTrial: true, TrialDays: 14, Enabled: true, SortOrder: 30, CreatedAt: now, UpdatedAt: now,
},
{
// Draft, not for sale (platform/werkmeister ADR 0001): no SKUs until pricing.
Key: "werkmeister", Name: "Werkmeister", Type: "standalone",
Description: "Visual workflow builder: blocks for the booked BreakPilot products, composable and versioned.",
LaunchURLs: map[string]string{"stage": "https://werkmeister-stage.breakpilot.com", "prod": "https://werkmeister.breakpilot.com"},
ProvisionURLs: map[string]string{},
SKUs: []string{}, Addons: []ProductAddon{},
PlansRequired: plans, SupportsTrial: true, TrialDays: 14, Enabled: false, SortOrder: 40, CreatedAt: now, UpdatedAt: now,
},
{
Key: "compliance", Name: "Compliance", Type: "standalone",
Description: "DSFA / TOM / VVT generation; evidence capture.",
LaunchURLs: map[string]string{}, ProvisionURLs: map[string]string{},
SKUs: []string{}, Addons: []ProductAddon{},
PlansRequired: plans, SupportsTrial: true, TrialDays: 14, Enabled: false, SortOrder: 90, CreatedAt: now, UpdatedAt: now,
},
}
}
// Catalog projects a Product onto the /v1/catalog shape for one environment.
func (p Product) Catalog(env string) CatalogEntry {
return CatalogEntry{
Key: p.Key, Name: p.Name, Description: p.Description, Type: p.Type,
LaunchURL: p.LaunchURLs[env], SKUs: nonNil(p.SKUs), Addons: p.Addons,
PlansRequired: nonNil(p.PlansRequired), SupportsTrial: p.SupportsTrial, TrialDays: p.TrialDays,
}
}
// HasAddon reports whether the product sells the named add-on.
func (p Product) HasAddon(id string) bool {
for _, a := range p.Addons {
if a.ID == id {
return true
}
}
return false
}
func nonNil(s []string) []string {
if s == nil {
return []string{}
}
return s
}
// tenant list cursor: "<unix-nanos>_<id>" — keyset on (created_at, id).
func encodeTenantCursor(t Tenant) string {
return fmt.Sprintf("%d_%s", t.CreatedAt.UnixNano(), t.ID)
}
func decodeTenantCursor(c string) (time.Time, string, bool) {
if c == "" {
return time.Time{}, "", true
}
i := strings.IndexByte(c, '_')
if i <= 0 {
return time.Time{}, "", false
}
n, err := strconv.ParseInt(c[:i], 10, 64)
if err != nil {
return time.Time{}, "", false
}
return time.Unix(0, n).UTC(), c[i+1:], true
}