# Task P.4 - PDFMe Product Item Table Template Refactoring

## Objective

Redesign the PDFMe quotation template to support dynamic Product Item tables by introducing a dedicated Product Item page between the existing Customer Detail page and Condition & Signature page.

The implementation **must not modify or replace the existing template**. A completely new template version shall be created while maintaining full backward compatibility.

---

# Mandatory Discovery Phase (Required)

Before implementing anything, inspect the actual project structure and produce a discovery report.

Do NOT make architectural assumptions.

The implementation may only begin after the discovery phase is completed.

---

# Discovery Scope

Inspect the current implementation under the CRM Template module.

At minimum review:

## 1. CRM Template Module

Inspect everything related to

```
/dashboard/crm/settings/templates
```

including

* pages
* components
* dialogs
* APIs
* services
* repositories
* database schema
* template versioning
* template preview
* template import/export
* template activation flow

Document how the template lifecycle currently works.

---

## 2. PDF Generation Runtime

Inspect the entire rendering pipeline.

Including

* quotation-preview.tsx
* PDF preview flow
* PDF download flow
* pdfme template loader
* template selector
* font loader
* plugin registration
* input builder
* layout engine
* topic engine
* schema builder
* adapter layer

Produce a dependency graph showing how a quotation becomes a PDF.

---

## 3. Existing PDF Templates

Locate every existing PDFMe template.

Document

* current page count
* page purpose
* schema layout
* static schema
* dynamic schema
* reusable components

Do not assume the current template structure.

---

## 4. Product Item Flow

Locate where quotation product items originate.

Trace

Quotation

↓

Export Payload

↓

Preview Model

↓

Input Builder

↓

PDF Template

↓

PDF Generator

Document every transformation layer.

Do NOT duplicate DTOs or adapters if an existing one already provides the required data.

---

## 5. Layout Engine

Inspect the existing layout implementation.

Determine whether it already supports

* page insertion
* page cloning
* page replacement
* page splitting
* dynamic page generation
* table pagination
* automatic page break

If not supported,

design the extension before implementation.

---

# Discovery Output

Before coding, provide

1. Existing architecture

2. Dependency graph

3. Current template lifecycle

4. Runtime rendering flow

5. Files that will be modified

6. New files that will be created

7. Backward compatibility analysis

8. Risk assessment

9. Migration strategy

Implementation may only begin after this report.

---

# Functional Requirements

Current document

```
Page 1
Customer Detail

Page 2
Condition + Signature
```

shall become

```
Page 1
Customer Detail

Page 2
Product Item Detail Table

Page 3
Condition & Signature
```

---

# Product Item Page

The Product Item page shall contain

* quotation items
* item number
* product code
* description
* specification
* quantity
* unit
* unit price
* discount
* amount
* remark (if available)

The table shall support

* unlimited rows
* automatic page break
* repeated table header
* automatic page numbering if multiple product pages are required

No fixed maximum row count.

---

# PDFMe Table

Use the official PDFMe Table implementation.

Do not create a custom HTML rendering solution.

Support

* automatic pagination
* repeated headers
* dynamic row height
* variable content length

---

# Template Versioning

Do NOT modify

* existing template JSON
* existing production template
* existing preview template

Create a new version.

Example

```
ALLA_template_pdfme_product_v1.json

ONVALLA_template_pdfme_product_v1.json
```

The existing templates must continue working without any behavior changes.

---

# Runtime Compatibility

The preview system must support both

Legacy Template

and

Product Item Template

through template selection.

No breaking changes.

---

# CRM Template Module Compatibility

The implementation must integrate with the existing

```
/dashboard/crm/settings/templates
```

workflow.

Do not bypass

* template storage
* template CRUD
* version management
* preview
* activation

The new template shall behave exactly like existing templates.

---

# Non-Functional Requirements

* No breaking changes
* No duplicated rendering pipeline
* No duplicated adapters
* No duplicated DTOs
* No duplicated services
* Reuse existing layout engine whenever possible
* Extend existing architecture instead of replacing it

---

# Acceptance Criteria

* Discovery report completed before implementation.
* Existing templates remain unchanged.
* New Product Item template added as a separate version.
* Product Item page inserted between Customer Detail and Condition page.
* Unlimited Product Item rows supported.
* Automatic page break works correctly.
* Table headers repeat across pages.
* Preview works correctly.
* PDF download works correctly.
* Existing templates continue functioning without modification.
* `/dashboard/crm/settings/templates` fully supports the new template without regression.
