feat: document engine with kinds, states, atomic numbering and fulfilment (#47)

One engine for sales, purchases and warehouse documents (ADR 0032). Each document
type now carries a kind that decides its family of tables, life cycle, effects,
accepted lines, the kinds it fulfils and its permissions, so company-defined types
work without code.

- Six families of header and line tables plus document_links; document_types gets
  document_kind (seeded types filled by the migration and the Italian template),
  document_numerators gets last_date.
- DocumentStateMachine with the order, invoice and simple life cycles and the
  actions each state admits; issued invoices are immutable and are cancelled with
  a reason only before the file reaches the exchange system.
- DocumentNumberingService numbers at confirmation inside the transaction under
  the numerator lock (advisory lock on PostgreSQL, write lock on SQLite): no
  duplicates, a rollback gives the number back, out-of-sequence dates refused on
  gap-free numerators, the new year's numerator created on first use, gap report.
- DocumentEngine, the generic DocumentService<THeader, TLine> and
  DocumentCopyService: save, confirm or issue, cancel, close, reopen, delete,
  duplicate and transform; links are recorded at confirmation and removed at
  cancellation, and FulfillmentCalculator recomputes quantities and states.
- Tests: rules in Core, concurrent numbering on SQLite and PostgreSQL (data tests,
  run on PostgreSQL 18 locally), order to delivery notes to deferred invoice on a
  real instance. Wiki (Vendite, Tabelle azienda), CHANGELOG, STATUS, DATA-MODEL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 15:30:24 +01:00
1 parent fa9304834a
commit ddd2273f7e
33 files changed
+4081 -37

No files matched your search

+679
View File
@@ -729,6 +729,7 @@ Migrazioni applicate in ordine:
- `core` 20261111015 `create_code_sequences` (M20261111_015_CreateCodeSequences)
- `core` 20261112001 `create_parties` (M20261112_001_CreateParties)
- `core` 20261112002 `create_items` (M20261112_002_CreateItems)
- `core` 20261112003 `create_documents` (M20261112_003_CreateDocuments)
#### `attachments`
@@ -990,6 +991,28 @@ Migrazioni applicate in ordine:
- indice unico `ux_discount_policies_code` su `code`
#### `document_links`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `target_family` | String(40) | no | | |
| `target_document_id` | Int64 | no | | |
| `target_line_id` | Int64 | no | | |
| `source_family` | String(40) | no | | |
| `source_document_id` | Int64 | no | | |
| `source_line_id` | Int64 | no | | |
| `quantity` | Decimal(19,6) | no | | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_document_links_source` su `source_family`, `source_line_id`
- indice `ix_document_links_source_document` su `source_family`, `source_document_id`
- indice `ix_document_links_target` su `target_family`, `target_document_id`
#### `document_numerators`
| Colonna | Tipo | Null | Default | Note |
@@ -1011,6 +1034,7 @@ Migrazioni applicate in ordine:
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
| `last_date` | Date | sì | | |
- indice unico `ux_document_numerators_code_year` su `code`, `year`
@@ -1035,6 +1059,7 @@ Migrazioni applicate in ordine:
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
| `document_kind` | String(40) | sì | | |
- indice unico `ux_document_types_code` su `code`
@@ -1112,6 +1137,115 @@ Migrazioni applicate in ordine:
- indice unico `ux_goods_appearances_code` su `code`
#### `inventory_document_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `inventory_documents` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_inventory_document_lines_document` su `document_id`
- indice `ix_inventory_document_lines_item` su `item_id`
- indice `ix_inventory_document_lines_source` su `source_family`, `source_line_id`
#### `inventory_documents`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_inventory_documents_date` su `document_date`
- indice `ix_inventory_documents_party` su `party_id`
- indice `ix_inventory_documents_status` su `status`
- indice unico `ux_inventory_documents_number` su `numerator_id`, `number`
#### `inventory_reasons`
| Colonna | Tipo | Null | Default | Note |
@@ -1710,6 +1844,224 @@ Migrazioni applicate in ordine:
- indice unico `ux_provinces_code` su `code`
#### `purchase_document_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `purchase_documents` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_purchase_document_lines_document` su `document_id`
- indice `ix_purchase_document_lines_item` su `item_id`
- indice `ix_purchase_document_lines_source` su `source_family`, `source_line_id`
#### `purchase_documents`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_purchase_documents_date` su `document_date`
- indice `ix_purchase_documents_party` su `party_id`
- indice `ix_purchase_documents_status` su `status`
- indice unico `ux_purchase_documents_number` su `numerator_id`, `number`
#### `purchase_order_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `purchase_orders` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_purchase_order_lines_document` su `document_id`
- indice `ix_purchase_order_lines_item` su `item_id`
- indice `ix_purchase_order_lines_source` su `source_family`, `source_line_id`
#### `purchase_orders`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_purchase_orders_date` su `document_date`
- indice `ix_purchase_orders_party` su `party_id`
- indice `ix_purchase_orders_status` su `status`
- indice unico `ux_purchase_orders_number` su `numerator_id`, `number`
#### `role_permissions`
| Colonna | Tipo | Null | Default | Note |
@@ -1743,6 +2095,333 @@ Migrazioni applicate in ordine:
- indice unico `ux_roles_code` su `code`
#### `sale_document_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `sale_documents` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_document_lines_document` su `document_id`
- indice `ix_sale_document_lines_item` su `item_id`
- indice `ix_sale_document_lines_source` su `source_family`, `source_line_id`
#### `sale_documents`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_documents_date` su `document_date`
- indice `ix_sale_documents_party` su `party_id`
- indice `ix_sale_documents_status` su `status`
- indice unico `ux_sale_documents_number` su `numerator_id`, `number`
#### `sale_order_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `sale_orders` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_order_lines_document` su `document_id`
- indice `ix_sale_order_lines_item` su `item_id`
- indice `ix_sale_order_lines_source` su `source_family`, `source_line_id`
#### `sale_orders`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_orders_date` su `document_date`
- indice `ix_sale_orders_party` su `party_id`
- indice `ix_sale_orders_status` su `status`
- indice unico `ux_sale_orders_number` su `numerator_id`, `number`
#### `sale_quotation_lines`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_id` | Int64 | no | | -> `sale_quotations` (Cascade) |
| `line_number` | Int32 | no | | |
| `kind` | String(40) | no | | |
| `item_id` | Int64 | sì | | -> `items` (Restrict) |
| `description` | String(1000) | no | | |
| `unit_of_measure_id` | Int64 | sì | | -> `units_of_measure` (Restrict) |
| `quantity` | Decimal(19,6) | no | `0` | |
| `price` | Decimal(19,6) | no | `0` | |
| `price_source` | String(40) | no | | |
| `discount_formula` | String(30) | sì | | |
| `tax_code_id` | Int64 | sì | | -> `tax_codes` (Restrict) |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `delivery_date` | Date | sì | | |
| `fulfilled_quantity` | Decimal(19,6) | no | `0` | |
| `fulfillment_closed` | Bool | no | `false` | |
| `source_family` | String(40) | sì | | |
| `source_line_id` | Int64 | sì | | |
| `account_code` | String(20) | sì | | |
| `cost_center_id` | Int64 | sì | | -> `cost_centers` (Restrict) |
| `job_id` | Int64 | sì | | -> `jobs` (Restrict) |
| `notes` | Text | sì | | |
| `sort_order` | Int32 | no | `0` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_quotation_lines_document` su `document_id`
- indice `ix_sale_quotation_lines_item` su `item_id`
- indice `ix_sale_quotation_lines_source` su `source_family`, `source_line_id`
#### `sale_quotations`
| Colonna | Tipo | Null | Default | Note |
|---|---|---|---|---|
| `id` | Int64 | no | | chiave primaria, identity |
| `document_type_id` | Int64 | no | | -> `document_types` (Restrict) |
| `kind` | String(40) | no | | |
| `status` | String(40) | no | | |
| `numerator_id` | Int64 | sì | | -> `document_numerators` (Restrict) |
| `section` | String(10) | sì | | |
| `number` | Int64 | sì | | |
| `document_number` | String(30) | sì | | |
| `document_date` | Date | no | | |
| `year` | Int32 | no | | |
| `party_id` | Int64 | sì | | -> `parties` (Restrict) |
| `destination_id` | Int64 | sì | | -> `party_addresses` (Restrict) |
| `external_reference` | String(40) | sì | | |
| `external_date` | Date | sì | | |
| `currency_code` | String(3) | no | | |
| `exchange_rate` | Decimal(19,6) | no | `1` | |
| `price_list_id` | Int64 | sì | | -> `price_lists` (Restrict) |
| `payment_term_id` | Int64 | sì | | -> `payment_terms` (Restrict) |
| `payment_method_id` | Int64 | sì | | -> `payment_methods` (Restrict) |
| `bank_account_id` | Int64 | sì | | -> `bank_accounts` (Restrict) |
| `salesperson_id` | Int64 | sì | | -> `salespeople` (Restrict) |
| `warehouse_id` | Int64 | sì | | -> `warehouses` (Restrict) |
| `inventory_reason_id` | Int64 | sì | | -> `inventory_reasons` (Restrict) |
| `transport_reason_id` | Int64 | sì | | -> `transport_reasons` (Restrict) |
| `shipping_term_id` | Int64 | sì | | -> `shipping_terms` (Restrict) |
| `carrier_id` | Int64 | sì | | -> `carriers` (Restrict) |
| `goods_appearance_id` | Int64 | sì | | -> `goods_appearances` (Restrict) |
| `packages` | Int32 | sì | | |
| `gross_weight` | Decimal(19,6) | sì | | |
| `net_weight` | Decimal(19,6) | sì | | |
| `transport_start` | DateTimeUtc | sì | | |
| `valid_until` | Date | sì | | |
| `delivery_date` | Date | sì | | |
| `revision` | Int32 | no | `0` | |
| `printed` | Bool | no | `false` | |
| `sent` | Bool | no | `false` | |
| `posted` | Bool | no | `false` | |
| `installments_generated` | Bool | no | `false` | |
| `xml_generated` | Bool | no | `false` | |
| `taxable_amount` | Decimal(19,6) | no | `0` | |
| `tax_amount` | Decimal(19,6) | no | `0` | |
| `total_amount` | Decimal(19,6) | no | `0` | |
| `footer_discount` | String(30) | sì | | |
| `expenses_amount` | Decimal(19,6) | no | `0` | |
| `stamp_duty` | Decimal(19,6) | no | `0` | |
| `withholding_amount` | Decimal(19,6) | no | `0` | |
| `social_security_amount` | Decimal(19,6) | no | `0` | |
| `net_amount` | Decimal(19,6) | no | `0` | |
| `notes` | Text | sì | | |
| `gl_entry_id` | Int64 | sì | | |
| `vat_protocol` | Int64 | sì | | |
| `confirmed_at` | DateTimeUtc | sì | | |
| `confirmed_by` | String(100) | sì | | |
| `cancelled_at` | DateTimeUtc | sì | | |
| `cancel_reason` | String(200) | sì | | |
| `is_demo` | Bool | no | `false` | |
| `created_at` | DateTimeUtc | no | | |
| `created_by` | String(100) | sì | | |
| `updated_at` | DateTimeUtc | sì | | |
| `updated_by` | String(100) | sì | | |
| `row_version` | Int64 | no | `1` | |
- indice `ix_sale_quotations_date` su `document_date`
- indice `ix_sale_quotations_party` su `party_id`
- indice `ix_sale_quotations_status` su `status`
- indice unico `ux_sale_quotations_number` su `numerator_id`, `number`
#### `salespeople`
| Colonna | Tipo | Null | Default | Note |
+7 -2
View File
@@ -5,7 +5,8 @@
**Ultimo aggiornamento:** 2026-11-12
**Milestone corrente:** M5 — Motore documenti e Vendite (issue #47-#54, nel progetto in *Da fare*). M4 chiusa:
PR #121 in `main`, tag `v0.4.0`, immagine e release.
**Prossimo passo:** M5, a partire dal motore documenti (#47) e dal calcolo dei totali (#48). #35 (PNG ad alta
**Prossimo passo:** M5: il motore documenti (#47) è sul ramo `feat/47-motore-documenti` in attesa di revisione;
poi il calcolo dei totali (#48) e le maschere (#49-#51). #35 (PNG ad alta
risoluzione) resta aperta e non blocca; #45 resta aperta solo per le banche ABI/CAB (manca una fonte con licenza
compatibile).
Da recuperare: profilo dell'azienda demo (indirizzo, partita IVA) prima dei documenti di M5; misure di riferimento su PostgreSQL in `docs/DATA-MODEL.md` §6.1; indicatore di robustezza della
@@ -15,7 +16,7 @@ filtro predefinito nei radar.
## Fatto
- Specifica normativa in `docs/SPEC.md`; `CLAUDE.md` con emendamenti E1-E7.
- ADR 0001-0031.
- ADR 0001-0032.
- Mockup in `docs/mockups/` (22 in alta risoluzione + galleria, 88 miniature, `mockup.css`, indice).
- Icone in `assets/`; deploy in `deploy/` (compose Unraid, `.env.example`, template Unraid).
- Documentazione tecnica: `ARCHITECTURE.md`, `DATA-MODEL.md` (provvisorio, sarà generato), `MODULES.md`, `API.md`,
@@ -59,6 +60,10 @@ filtro predefinito nei radar.
aggiornamento massivo (ADR 0030); import/export guidato CSV/XLSX/JSON con tracciato del piano dei conti (ADR 0031);
21 anagrafiche e 120 articoli di esempio; screenshot reali nella wiki. Test: 631 verdi in locale, 21 di PostgreSQL
saltati; immagine costruita in locale e avviata da zero con SQLite e dati di esempio.
- **M5 (in corso):** motore documenti (#47, ADR 0032): generi e definizioni dei tipi documento, sei famiglie di
tabelle con le righe e `document_links`, stati con azioni ammesse, numerazione atomica alla conferma (provata in
concorrenza su SQLite e su PostgreSQL 18 in locale), copia e trasformazione con evasione riga per riga. Test: 664
verdi in locale, PostgreSQL compreso.
- Su Gitea: etichette, milestone M0-M14, 114 issue con checklist, progetti *Khaemhat 1.0 — Sviluppo* (M0-M13)
e *Khaemhat 1.1+ — Fase 2* (M14). Numerazione: M0 #1-#6 e #114, M1 #7-#14, M2 #15-#24, M3 #25-#36,
M4 #37-#46, M5 #47-#54, M6 #55-#61, M7 #62-#68, M8 #69-#73, M9 #74-#81, M10 #82-#87, M11 #88-#91,
+65
View File
@@ -0,0 +1,65 @@
# ADR 0032 - Motore documenti: comportamento per genere, tabelle per famiglia, numerazione alla conferma
- **Stato:** accettata
- **Data:** 2026-11-12
- **Modifica:** SPEC §11.3; milestone M5, issue #47
## Contesto
SPEC §11.3 vuole un solo motore per tutti i documenti (vendita, acquisto, magazzino), guidato da definizioni
dichiarative, con tabelle per famiglia (`sale_quotations`, `sale_orders`, `sale_documents`, `purchase_orders`,
`purchase_documents`, `inventory_documents`, ognuna con le righe), `document_links` per l'evasione, gli stati
`Draft → Confirmed → (PartiallyFulfilled | Fulfilled) → Closed`, `Cancelled` e per le fatture
`Draft → Issued → SdiGenerated → SdiSent → (SdiDelivered | SdiRejected | SdiNotDelivered)`, la numerazione alla
conferma con la data non anteriore all'ultimo numero del sezionale. Restavano aperti: dove sta il comportamento di un
tipo documento (la tabella `document_types` è modificabile dall'utente e il suo `kind` dice solo la famiglia di
numerazione), come si numera per anno quando il numeratore è per anno, quando nasce un legame di evasione, che cosa
fa la copia, quali tabelle servono già adesso.
## Decisione
1. **Genere e definizione.** Il comportamento sta in codice, per **genere** (`DocumentKind`: preventivo, ordine
cliente, DDT, fattura immediata, accompagnatoria, differita, di acconto, nota di credito, nota di debito, parcella,
pro forma, autofattura, ordine fornitore, DDT fornitore, fattura fornitore, nota di credito fornitore, movimento di
magazzino). `DocumentBehaviors` dà per ogni genere: famiglia (tabella), direzione, ciclo di vita, effetti
(magazzino, impegnato, ordinato, contabilità, scadenze, e-fattura), tipi di riga ammessi, generi da cui evade
(sorgenti) e permessi. La tabella `document_types` riceve la colonna `document_kind`: ogni tipo dell'utente
sceglie un genere e aggiunge numeratore, causale di magazzino, TD SDI, causale contabile, registro IVA, modello di
stampa. `DocumentDefinition` è l'unione dei due (tipo + comportamento) e si legge con `DocumentDefinitionService`.
I tipi seminati prendono il genere dalla migrazione e dal modello del pacchetto paese.
2. **Tre cicli di vita.** `DocumentStateMachine` conosce tre cicli: *ordine* (preventivi, ordini, DDT, pro forma:
bozza, confermato, evaso parzialmente, evaso, chiuso, annullato), *fattura* (fatture e autofatture di vendita:
bozza, emessa, XML generato, inviata, consegnata, scartata, non consegnata, annullata) e *semplice* (fatture
fornitore, movimenti: bozza, confermato, annullato). Per ogni stato dice le azioni ammesse; un'azione non ammessa
è rifiutata con `document.action.not-allowed`. Si modifica solo una bozza, oltre a ordini e preventivi confermati
non evasi; la fattura emessa è immutabile: si annulla con un motivo finché l'XML non è inviato (o se scartato),
altrimenti si fa una nota di credito. Gli stati di evasione non sono azioni: li calcola `FulfillmentCalculator`.
3. **Numerazione alla conferma.** `DocumentNumberingService` numera nella stessa transazione della conferma, sotto il
lock `numerator:<codice>:<anno>` (advisory lock di transazione su PostgreSQL, lock di scrittura su SQLite): un
rollback restituisce il numero, quindi non nascono buchi. Il numeratore del tipo vale per il suo codice; per
l'anno della data si usa la riga (codice, anno) e, se manca, si crea copiando formato, prefisso e sezionale
dell'ultima. Sui numeratori **senza buchi** (fatture, autofatture, protocollo acquisti) la data non può
precedere l'ultima numerata (`document_numerators.last_date`) e la conferma chiede il periodo aperto quando il
genere ha effetto contabile. `FindGapsAsync` elenca i numeri mancanti di un numeratore. Un documento numerato non
si elimina: si annulla e il numero resta.
4. **Tabelle.** Sei testate e sei tabelle di righe con le stesse colonne (classi base `DocumentHeader` e
`DocumentLine`), più `document_links` (riga destinazione → riga sorgente, quantità). Le tabelle del castelletto IVA
e delle rate arrivano con il calcolo dei totali (#48), i campi di trasporto sono già nella testata.
5. **Evasione.** Una riga copiata da un documento evadibile tiene la riga d'origine (`source_family`,
`source_line_id`). Il legame in `document_links` nasce alla **conferma** del documento destinazione e sparisce al
suo annullamento; dopo ogni cambio `FulfillmentCalculator` ricalcola la quantità evasa delle righe sorgente e lo
stato del documento sorgente (confermato, evaso parzialmente, evaso). Le righe con saldo forzato contano come
evase; un documento chiuso o annullato non cambia stato.
6. **Copia.** `DocumentCopyService` crea una bozza da un documento esistente verso lo stesso tipo (duplicazione) o un
altro della stessa direzione (trasformazione). Se il genere destinazione evade il genere sorgente si copiano le
quantità residue e le righe tengono l'origine, altrimenti si copiano le quantità intere senza legame.
7. **Servizio generico.** `DocumentService<THeader, TLine>` (uno per famiglia) espone crea, leggi, salva, conferma,
annulla, chiudi, riapri, elimina e copia; il lavoro su più famiglie (evasione, copia) passa da `DocumentEngine`, che
tratta testate e righe tramite le classi base.
## Conseguenze
- Un tipo documento nuovo dell'utente funziona senza codice se usa un genere esistente; un genere nuovo è codice.
- Gli effetti sono dichiarati ma si applicano con i moduli: magazzino in M6, contabilità in M7, scadenze in M8,
e-fattura in M8. Il calcolo dei totali e delle rate è #48, le maschere sono #49-#51.
- La numerazione concorrente è provata su SQLite e PostgreSQL (test dei dati, eseguiti su PostgreSQL 16 e 17 ai tag).
+9 -2
View File
@@ -113,7 +113,7 @@ periodo (vedi [IVA](IVA)) e la chiusura dell'esercizio lo rende di sola lettura
| **Categorie controparte** | classificazione di clienti e fornitori |
| **Listini** | intestazioni dei listini: valuta, IVA inclusa, validità, **Listino base** (uno solo, in partenza `BASE`), **Derivato da** con ricarico % e arrotondamento (i prezzi sono in [Articoli e listini](Articoli-e-listini)) |
| **Politiche di sconto** | sconti in cascata con un codice, come formula (`10+5`), da richiamare nelle condizioni |
| **Tipi documento** | catalogo dei documenti con numeratore, causale di magazzino, causale contabile, registro IVA, codice TD, modello di stampa |
| **Tipi documento** | catalogo dei documenti con **genere** (obbligatorio), numeratore, causale di magazzino, causale contabile, registro IVA, codice TD, modello di stampa |
| **Centri di costo** | per l'analisi dei costi e dei ricavi |
| **Commesse** | progetti o cantieri a cui imputare costi e ricavi |
@@ -156,7 +156,14 @@ clienti `OC-{year}-{number:D4}` (`OC-2026-0042`), autofatture `{number}/AF/{year
Il numero è assegnato in modo atomico **alla conferma** del documento (da M5); per fatture, note di credito,
protocollo acquisti e registrazioni contabili **non sono ammessi buchi** nella numerazione (opzione *Ammessi buchi di numerazione*
spenta). I numeratori sono per anno: il caricamento di base crea quelli dell'anno in corso.
spenta) e la data di un documento non può precedere la **data dell'ultimo numero** del numeratore. I numeratori sono
per anno: il caricamento di base crea quelli dell'anno in corso e il primo documento di un anno nuovo crea da solo il
numeratore di quell'anno, con le stesse regole e dal numero 1.
Il **genere** di un tipo documento dice come si comportano i suoi documenti: famiglia, ciclo di vita, effetti, righe
ammesse e documenti che può evadere (vedi [Vendite](Vendite#il-motore-dei-documenti-in-sintesi)). I tipi di base hanno
già il genere; un tipo nuovo dell'azienda (ad esempio una seconda fattura immediata con un altro sezionale) sceglie uno
dei generi esistenti e funziona senza altre impostazioni.
## Preferenze e codici automatici
+54 -11
View File
@@ -1,6 +1,6 @@
# Vendite
> **Stato:** bozza · **Ultima revisione:** 2026-11-10 · **Versione software:** non ancora rilasciata (milestone M5)
> **Stato:** bozza (motore documenti pronto, maschere in arrivo) · **Ultima revisione:** 2026-11-12 · **Versione software:** non ancora rilasciata (milestone M5)
> **Modulo di licenza:** CORE (XML SDI con EINV, contabilizzazione con ACCT, partite con FIN) · **Permessi principali:** `sales.invoice.issue`, `sales.deliverynote.reopen`, famiglia `sales.*` · **Specifica:** SPEC §11.3, §11.4
Il modulo **Vendite** gestisce l'intero ciclo attivo: **preventivi**, **ordini clienti**, **DDT**, **fatture**
@@ -14,14 +14,35 @@ Menu **Vendite** → *Preventivi*, *Ordini clienti*, *DDT*, *Fatture*, *Note di
## Il motore dei documenti in sintesi
Ogni tipo di documento è descritto da una definizione che stabilisce: numeratore e sezionale, causale di magazzino
(se il documento carica o scarica merce), **effetti** (impegno, scarico, partite, registrazione contabile), da quali
Ogni tipo di documento (tabella *Tipi documento*, vedi [Tabelle azienda](Tabelle-azienda)) ha un **genere** e
insieme formano la sua definizione: numeratore e sezionale, causale di magazzino (se il documento carica o scarica
merce), **effetti** (impegno, scarico, partite, registrazione contabile, fattura elettronica), righe ammesse, da quali
documenti può essere **evaso** (ad esempio un DDT da un ordine), tipo documento SDI (TD), causale contabile, registro
IVA e modello di stampa.
| Genere | Famiglia | Ciclo di vita | Evade |
|---|---|---|---|
| Preventivo | preventivi | documento | |
| Ordine cliente | ordini clienti | documento | preventivo |
| DDT di vendita | documenti di vendita | documento | ordine cliente |
| Fattura immediata, accompagnatoria, parcella | documenti di vendita | fattura | ordine cliente, pro forma |
| Fattura differita | documenti di vendita | fattura | DDT di vendita |
| Fattura di acconto | documenti di vendita | fattura | ordine cliente |
| Nota di credito, nota di debito | documenti di vendita | fattura | fatture |
| Pro forma | documenti di vendita | documento | ordine cliente |
| Autofattura o integrazione | documenti di vendita | fattura | fattura fornitore |
| Ordine fornitore | ordini fornitori | documento | |
| DDT fornitore | documenti di acquisto | documento | ordine fornitore |
| Fattura fornitore | documenti di acquisto | semplice | ordine fornitore, DDT fornitore |
| Nota di credito fornitore | documenti di acquisto | semplice | fattura fornitore |
| Movimento di magazzino | movimenti di magazzino | semplice | |
I DDT non accettano righe di spesa o di sconto; i movimenti di magazzino solo articoli e righe descrittive.
I documenti sono **collegati riga per riga**: quando un DDT evade un ordine, ogni riga del DDT ricorda la riga
d'ordine di origine e la quantità evasa. Così Khaemhat calcola i **residui**, permette di riaprirli e scrive nel file
XML della fattura i riferimenti all'ordine (*DatiOrdineAcquisto*) e ai DDT (*DatiDDT*).
XML della fattura i riferimenti all'ordine (*DatiOrdineAcquisto*) e ai DDT (*DatiDDT*). Il collegamento vale dalla
**conferma** del documento che evade: una bozza non toglie nulla al residuo, un documento annullato lo restituisce.
```
Preventivo ──trasforma──► Ordine cliente ──evadi──► DDT ──fattura differita──► Fattura TD24
@@ -214,14 +235,34 @@ La fattura differita va emessa entro il giorno 15 del mese successivo a quello d
Altri badge indicano lo stato contabile (*Non contabilizzata*), del magazzino (*Scaricato*) o della fatturazione
(*Da fatturare*).
Le fatture fornitore e i movimenti di magazzino hanno solo *Bozza*, *Confermato* e *Annullato*.
Che cosa si può fare in ogni stato:
- Si **modifica** una bozza; di ordini, preventivi, DDT e pro forma confermati si possono ancora cambiare le righe
finché non sono evasi del tutto, ma una quantità non scende sotto quella già evasa e una riga evasa non si toglie.
Numero, data di un documento numerato e tipo non cambiano più.
- Si **elimina** solo una bozza, che non ha mai avuto un numero.
- Si **annulla** un documento confermato non ancora evaso: il numero resta e le quantità che aveva evaso tornano ai
documenti d'origine. Una fattura si annulla **con un motivo** finché il file non è stato inviato allo SDI (o dopo
uno scarto); dopo, si fa una nota di credito. Non si annulla un documento che altri hanno già evaso.
- Si **chiude** il residuo di un documento evaso in parte (o anche del tutto); **Riapri** lo riporta allo stato che
dicono le sue righe.
- Si **duplica** o **trasforma** un documento in qualunque stato.
## Numerazione
- Formato `{prefisso}/{anno}/{numero a 6 cifre}`, ad esempio `FT/2026/000418`, per **sezionale** (FT, NC, DDT…) e
anno, configurato nei numeratori (vedi [Tabelle azienda](Tabelle-azienda)).
- Il numero è assegnato **alla conferma**, in modo atomico: le bozze non consumano numeri.
- Il formato è quello del numeratore del tipo documento, per **sezionale** e anno (vedi
[Tabelle azienda](Tabelle-azienda#numeratori)): ad esempio `{number}/{year}` per fatture e note (`418/2026`),
`OC-{year}-{number:D4}` per gli ordini (`OC-2026-0042`).
- Il numero è assegnato **alla conferma**, in modo atomico: le bozze non consumano numeri, due conferme contemporanee
non ricevono mai lo stesso numero e una conferma non riuscita restituisce il numero. Fatture, note di credito e note
di debito condividono il numeratore delle fatture.
- Per fatture e note di credito **non sono ammessi buchi** e la data non può essere anteriore a quella dell'ultimo
documento numerato nello stesso sezionale.
- La data deve cadere in un **periodo fiscale aperto** (vedi esercizi e periodi in [Tabelle azienda](Tabelle-azienda)).
documento numerato nello stesso numeratore.
- La data dei documenti con effetto contabile deve cadere in un **periodo fiscale aperto** (vedi esercizi e periodi in
[Tabelle azienda](Tabelle-azienda)).
- Con il primo documento di un anno nuovo la numerazione riparte da 1.
## Calcolo dei totali
@@ -258,8 +299,10 @@ e-mail dell'azienda; il canale preferito è nelle condizioni del cliente.
- **Collegamenti**: la scheda omonima mostra i documenti di origine e quelli generati (ordine → DDT → fattura) con
le quantità; l'icona *Collegamenti* della toolbar apre lo stesso elenco.
- **Duplica** crea un nuovo documento in bozza con le stesse righe; la **trasformazione** cambia tipo mantenendo i
collegamenti (preventivo → ordine).
- **Duplica** crea un nuovo documento in bozza con le stesse righe e la data di oggi; la **trasformazione** crea un
documento di un altro tipo della stessa direzione (vendita o acquisto). Se il nuovo tipo evade il vecchio
(preventivo → ordine, ordine → DDT, DDT → fattura differita) le righe prendono solo le **quantità residue** e
restano collegate; le righe che il nuovo tipo non accetta (ad esempio le spese in un DDT) non si copiano.
- **Corrispettivi**: le vendite al dettaglio si registrano a mano come corrispettivi giornalieri.
## Report