diff --git a/app/modules/_template/README.md b/app/modules/_template/README.md
index 7c2ea43..a2e75e3 100644
--- a/app/modules/_template/README.md
+++ b/app/modules/_template/README.md
@@ -32,7 +32,7 @@ Alle tabeller SKAL bruge `table_prefix` fra module.json:
```sql
-- Hvis table_prefix = "mymod_"
-CREATE TABLE mymod_customers (
+CREATE TABLE mymod_items (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);
@@ -40,6 +40,43 @@ CREATE TABLE mymod_customers (
Dette sikrer at moduler ikke kolliderer med core eller andre moduler.
+### Customer Linking (Hvis nødvendigt)
+
+Hvis dit modul skal have sin egen kunde-tabel (f.eks. ved sync fra eksternt system):
+
+**SKAL altid linke til core customers:**
+
+```sql
+CREATE TABLE mymod_customers (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ external_id VARCHAR(100), -- ID fra eksternt system
+ hub_customer_id INTEGER REFERENCES customers(id), -- VIGTIG!
+ active BOOLEAN DEFAULT TRUE,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+-- Auto-link trigger (se migrations/001_init.sql for komplet eksempel)
+CREATE TRIGGER trigger_auto_link_mymod_customer
+ BEFORE INSERT OR UPDATE OF name
+ ON mymod_customers
+ FOR EACH ROW
+ EXECUTE FUNCTION auto_link_mymod_customer();
+```
+
+**Hvorfor?** Dette sikrer at:
+- ✅ E-conomic export virker automatisk
+- ✅ Billing integration fungerer
+- ✅ Ingen manuel linking nødvendig
+
+**Alternativ:** Hvis modulet kun har simple kunde-relationer, brug direkte FK:
+```sql
+CREATE TABLE mymod_orders (
+ id SERIAL PRIMARY KEY,
+ customer_id INTEGER REFERENCES customers(id) -- Direkte link
+);
+```
+
## Konfiguration
Modul-specifikke miljøvariable følger mønsteret:
diff --git a/app/modules/_template/migrations/001_init.sql b/app/modules/_template/migrations/001_init.sql
index d266086..88a95d0 100644
--- a/app/modules/_template/migrations/001_init.sql
+++ b/app/modules/_template/migrations/001_init.sql
@@ -11,6 +11,19 @@ CREATE TABLE IF NOT EXISTS template_items (
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
+-- Optional: Customers tabel hvis modulet har egne kunder (f.eks. sync fra eksternt system)
+-- Kun nødvendigt hvis modulet har mange custom felter eller external sync
+-- Ellers brug direkte foreign key til customers.id
+CREATE TABLE IF NOT EXISTS template_customers (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ external_id VARCHAR(100), -- ID fra eksternt system hvis relevant
+ hub_customer_id INTEGER REFERENCES customers(id), -- VIGTIG: Link til core customers
+ active BOOLEAN DEFAULT TRUE,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
-- Index for performance
CREATE INDEX IF NOT EXISTS idx_template_items_active ON template_items(active);
CREATE INDEX IF NOT EXISTS idx_template_items_created ON template_items(created_at DESC);
@@ -29,6 +42,39 @@ BEFORE UPDATE ON template_items
FOR EACH ROW
EXECUTE FUNCTION update_template_items_updated_at();
+-- Trigger for auto-linking customers (hvis template_customers tabel oprettes)
+-- Dette linker automatisk nye kunder til core customers baseret på navn match
+CREATE OR REPLACE FUNCTION auto_link_template_customer()
+RETURNS TRIGGER AS $$
+DECLARE
+ matched_hub_id INTEGER;
+BEGIN
+ -- Hvis hub_customer_id allerede er sat, skip
+ IF NEW.hub_customer_id IS NOT NULL THEN
+ RETURN NEW;
+ END IF;
+
+ -- Find matching hub customer baseret på navn
+ SELECT id INTO matched_hub_id
+ FROM customers
+ WHERE LOWER(TRIM(name)) = LOWER(TRIM(NEW.name))
+ LIMIT 1;
+
+ -- Hvis match fundet, sæt hub_customer_id
+ IF matched_hub_id IS NOT NULL THEN
+ NEW.hub_customer_id := matched_hub_id;
+ END IF;
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER trigger_auto_link_template_customer
+ BEFORE INSERT OR UPDATE OF name
+ ON template_customers
+ FOR EACH ROW
+ EXECUTE FUNCTION auto_link_template_customer();
+
-- Indsæt test data (optional)
INSERT INTO template_items (name, description)
VALUES
diff --git a/app/shared/frontend/base.html b/app/shared/frontend/base.html
index e118dd2..29585ee 100644
--- a/app/shared/frontend/base.html
+++ b/app/shared/frontend/base.html
@@ -502,6 +502,7 @@
{% endblock %}
+
+
+