@netoalmanca/advpl-sensei 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netoalmanca/advpl-sensei",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "MCP Server for ADVPL/TLPP Protheus ecosystem",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -19,7 +19,18 @@ Patterns and templates for generating clean, standardized ADVPL and TLPP code fo
19
19
  - Creating SOAP Web Services
20
20
  - Any new .prw or .tlpp file creation
21
21
 
22
+ ## Critical Rules (MANDATORY)
23
+
24
+ 1. **Anti-Hallucination**: Never use functions not found in the TDN or the local Function Registry (e.g., `HttpServer`).
25
+ 2. **ExecAuto vs RecLock**:
26
+ - **NEVER** use `RecLock` to create or update records in core business tables (SC5, SC6, SC7, SD1, SD2, SF1, SF2, SE1, SE2, SB1).
27
+ - **ALWAYS** use `MSExecAuto` (e.g., `MATA410` for Sales Orders) for these entities to ensure field validation, triggers, and data integrity.
28
+ - Refer to `patterns-execauto.md` for implementation details.
29
+ 3. **Variable Scope**: Always use `Local`. Never use `Public`. Avoid `Private` except for ExecAuto control variables (`lMsErroAuto`).
30
+ 4. **Hungarian Notation**: Strictly follow the naming conventions below.
31
+
22
32
  ## Naming Conventions
33
+ ... (rest of the file)
23
34
 
24
35
  | Element | Convention | Example |
25
36
  |---------|-----------|---------|
@@ -0,0 +1,116 @@
1
+ # Protheus ExecAuto Patterns
2
+
3
+ Manual for implementing automated routines (ExecAuto) in TOTVS Protheus. This is the **mandatory** pattern for complex business entities like Sales Orders, Purchase Orders, and Invoices.
4
+
5
+ ---
6
+
7
+ ## 1. Why use ExecAuto?
8
+
9
+ Directly using `RecLock` on core tables (SC5, SC6, SC7, SD1, SD2, etc.) bypasses:
10
+ - **Field Validations** (VLDUSER, CNIVEL, etc.)
11
+ - **Automatic Triggers** (SX7)
12
+ - **Integrity Checks** (SX9)
13
+ - **Standard Business Rules** (Calculations, stock updates, financial entries)
14
+
15
+ **MANDATORY:** Always use `MSExecAuto` for entities that have a standard Protheus routine.
16
+
17
+ ---
18
+
19
+ ## 2. Sales Order (MATA410)
20
+
21
+ The most common ExecAuto. It handles header (SC5) and items (SC6) simultaneously.
22
+
23
+ ### 2.1 Basic Structure
24
+
25
+ ```advpl
26
+ #Include "TOTVS.CH"
27
+
28
+ /*/{Protheus.doc} MySalesOrder
29
+ Example of Sales Order creation via ExecAuto
30
+ @type function
31
+ /*/
32
+ User Function MySalesOrder(oRequest)
33
+ Local aCabec := {}
34
+ Local aItens := {}
35
+ Local aLinha := {}
36
+ Local i := 0
37
+ Local lOk := .T.
38
+
39
+ // MSExecAuto global control variables
40
+ Private lMsErroAuto := .F.
41
+ Private lMsHelpAuto := .T.
42
+
43
+ // 1. Prepare Header (SC5)
44
+ // Format: { FieldName, Value, Nil }
45
+ aAdd(aCabec, {"C5_TIPO", "N", Nil})
46
+ aAdd(aCabec, {"C5_CLIENTE", oRequest["client"], Nil})
47
+ aAdd(aCabec, {"C5_LOJACLI", oRequest["store"], Nil})
48
+ aAdd(aCabec, {"C5_CONDPAG", "001", Nil})
49
+ aAdd(aCabec, {"C5_TABELA", "01", Nil})
50
+ aAdd(aCabec, {"C5_EMISSAO", dDataBase, Nil})
51
+
52
+ // 2. Prepare Items (SC6)
53
+ // Format: Array of arrays, where each inner array is a line
54
+ For i := 1 To Len(oRequest["items"])
55
+ aLinha := {}
56
+ aAdd(aLinha, {"C6_ITEM", StrZero(i, 2), Nil})
57
+ aAdd(aLinha, {"C6_PRODUTO", oRequest["items"][i]["product"], Nil})
58
+ aAdd(aLinha, {"C6_QTD", oRequest["items"][i]["quantity"], Nil})
59
+ aAdd(aLinha, {"C6_PRECO", oRequest["items"][i]["price"], Nil})
60
+ aAdd(aLinha, {"C6_TES", "501", Nil})
61
+ aAdd(aItens, aLinha)
62
+ Next i
63
+
64
+ // 3. Execute MATA410 (3 = Inclusion, 4 = Alteration, 5 = Deletion)
65
+ MSExecAuto({|x,y,z| MATA410(x,y,z)}, aCabec, aItens, 3)
66
+
67
+ // 4. Handle Errors
68
+ If lMsErroAuto
69
+ lOk := .F.
70
+ Conout("Error in MATA410 ExecAuto")
71
+ MostraErro() // Log or show errors
72
+ Else
73
+ Conout("Sales Order created: " + SC5->C5_NUM)
74
+ EndIf
75
+
76
+ Return lOk
77
+ ```
78
+
79
+ ---
80
+
81
+ ## 3. Operations Reference
82
+
83
+ | Operation | Value | Description |
84
+ |-----------|-------|-------------|
85
+ | Inclusion | 3 | Create new record |
86
+ | Alteration | 4 | Update existing record (requires positioning) |
87
+ | Deletion | 5 | Remove record (requires positioning) |
88
+
89
+ ---
90
+
91
+ ## 4. Error Handling Pattern for APIs
92
+
93
+ When using ExecAuto inside a REST API, you should capture the error log into a string to return in the JSON response.
94
+
95
+ ```advpl
96
+ If lMsErroAuto
97
+ cError := GetAutoGRLog() // Captures the error log
98
+ SetRestFault(500, cError)
99
+ EndIf
100
+ ```
101
+
102
+ ---
103
+
104
+ ## 5. Common ExecAuto Routines
105
+
106
+ | Entity | Routine | Table |
107
+ |--------|---------|-------|
108
+ | Sales Order | `MATA410` | SC5/SC6 |
109
+ | Purchase Order | `MATA120` | SC7 |
110
+ | Products | `MATA010` | SB1 |
111
+ | Customers | `CRMA980` | SA1 |
112
+ | Invoices (Inbound) | `MATA103` | SF1/SD1 |
113
+ | Invoices (Outbound) | `MATA461` | SF2/SD2 |
114
+ | Inventory Adjustments | `MATA241` | SD3 |
115
+ | Accounts Payable | `FINA050` | SE2 |
116
+ | Accounts Receivable | `FINA040` | SE1 |
@@ -97,11 +97,13 @@ WsMethod GET WsReceive nPage, nPageSize WsService CUSTOMERS
97
97
 
98
98
  Return lRet
99
99
  ```
100
-
101
100
  ### 1.3 POST Method - Create
102
101
 
102
+ **IMPORTANT:** While the example below uses `RecLock` for simple entities like `SA1`, you MUST use `MSExecAuto` for complex business entities (Sales Orders, Purchase Orders, Invoices). Refer to `patterns-execauto.md` for details.
103
+
103
104
  ```advpl
104
105
  WsMethod POST WsService CUSTOMERS
106
+ ```
105
107
  Local lRet := .T.
106
108
  Local oJson := JsonObject():New()
107
109
  Local oResp := Nil