forlogic-core 2.4.12 → 2.4.13

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.
@@ -0,0 +1,219 @@
1
+ -- =============================================================
2
+ -- Procedures de importação — Tipo de Fornecimento
3
+ -- Banco: qualiex_common (SQL Server)
4
+ -- Padrão: espelha import.validate_suppliers_categories.
5
+ -- Dispatch dinâmico: import.process_batch resolve pelo table_name
6
+ -- (@tablename = 'supply_types').
7
+ --
8
+ -- Tabela destino: suppliers.supply_types
9
+ -- Colunas relevantes:
10
+ -- id char(8) NOT NULL -- default new_id()
11
+ -- id_company char(8) NOT NULL
12
+ -- title varchar(200) NOT NULL
13
+ -- description nvarchar(max) NULL -- HTML/richtext
14
+ -- description_text nvarchar(max) NULL -- texto puro (fallback = description)
15
+ -- is_active bit NOT NULL -- default 1
16
+ -- id_form char(8) NULL -- FK dbo.forms (informado pelo usuário)
17
+ -- id_creation_user char(8) NOT NULL
18
+ -- id_last_modified_user char(8) NOT NULL
19
+ -- creation_date datetimeoffset(7) NOT NULL default SYSDATETIMEOFFSET()
20
+ -- last_modified datetimeoffset(7) NOT NULL default SYSDATETIMEOFFSET()
21
+ -- removed bit NOT NULL -- default 0
22
+ --
23
+ -- Colunas esperadas no CSV (ordem do template):
24
+ -- 1) Título -> title (obrigatório)
25
+ -- 2) Descrição -> description (opcional)
26
+ -- 3) Situação (Ativo/Inativo) -> is_active (opcional, default Ativo)
27
+ -- 4) Emissor (lookup usuários) -> id_creation_user (obrigatório)
28
+ -- 5) Formulário do tipo de fornecimento -> id_form (opcional, char(8))
29
+ --
30
+ -- Sem coluna `code` (tabela não possui).
31
+ -- id_company é resolvido pelo backend a partir do contexto da unidade.
32
+ -- =============================================================
33
+
34
+ USE [qualiex_common]
35
+ GO
36
+
37
+ -- =============================================================
38
+ -- Cleanup — versões anteriores (idempotência)
39
+ -- =============================================================
40
+ IF OBJECT_ID('import.get_errors_supply_types', 'P') IS NOT NULL
41
+ DROP PROCEDURE import.get_errors_supply_types;
42
+ GO
43
+ IF OBJECT_ID('import.validate_supply_types', 'P') IS NOT NULL
44
+ DROP PROCEDURE import.validate_supply_types;
45
+ GO
46
+
47
+ -- =============================================================
48
+ -- Staging table
49
+ -- =============================================================
50
+ IF EXISTS (SELECT * FROM sys.objects
51
+ WHERE object_id = OBJECT_ID(N'[import].[supply_types_staging]')
52
+ AND type IN (N'U'))
53
+ DROP TABLE [import].[supply_types_staging]
54
+ GO
55
+
56
+ SET ANSI_NULLS ON
57
+ GO
58
+ SET QUOTED_IDENTIFIER ON
59
+ GO
60
+
61
+ CREATE TABLE [import].[supply_types_staging](
62
+ [id_batch] [uniqueidentifier] NOT NULL,
63
+ [row_num] [int] NOT NULL,
64
+ [title] [varchar](200) NOT NULL,
65
+ [description] [nvarchar](max) NULL,
66
+ [is_active] [varchar](20) NULL, -- "Ativo" / "Inativo" vindo do CSV
67
+ [id_form] [char](8) NULL,
68
+ [id_creation_user] [char](8) NOT NULL,
69
+ [id_company] [char](8) NOT NULL,
70
+ CONSTRAINT [pk_supply_types_staging] PRIMARY KEY CLUSTERED
71
+ (
72
+ [id_batch] ASC,
73
+ [row_num] ASC
74
+ ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
75
+ ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
76
+ OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
77
+ ) ON [PRIMARY]
78
+ GO
79
+
80
+
81
+ -- =============================================================
82
+ -- import.get_errors_supply_types
83
+ -- Retorna erros consolidados por linha para exibição no frontend.
84
+ -- =============================================================
85
+ CREATE OR ALTER PROCEDURE [import].[get_errors_supply_types]
86
+ (
87
+ @id_batch UNIQUEIDENTIFIER
88
+ )
89
+ AS
90
+ BEGIN
91
+ SET NOCOUNT ON;
92
+
93
+ SELECT
94
+ STRING_AGG(
95
+ CAST(e.error_msg AS NVARCHAR(MAX)),
96
+ '; '
97
+ ) WITHIN GROUP (ORDER BY e.error_msg) AS error_msg,
98
+
99
+ e.row_num,
100
+
101
+ s.title,
102
+ s.id_creation_user
103
+
104
+ FROM import.errors e
105
+ JOIN import.supply_types_staging s
106
+ ON s.row_num = e.row_num
107
+ AND s.id_batch = e.id_batch
108
+ WHERE e.id_batch = @id_batch
109
+ GROUP BY
110
+ e.row_num,
111
+ s.title,
112
+ s.id_creation_user
113
+ ORDER BY e.row_num;
114
+ END
115
+ GO
116
+
117
+
118
+ -- =============================================================
119
+ -- import.validate_supply_types
120
+ -- 1) Título duplicado no arquivo
121
+ -- 2) Título já existente na base (mesma unidade, removed = 0)
122
+ -- 3) Situação inválida (aceita EXCLUSIVAMENTE 'Ativo' ou 'Inativo',
123
+ -- espelhando os valores do dropdown do template .xlsx —
124
+ -- comparação case/acento-insensitive para tolerar variações do Excel)
125
+ -- 4) INSERT das linhas sem erro em suppliers.supply_types
126
+ -- =============================================================
127
+ CREATE OR ALTER PROCEDURE [import].[validate_supply_types]
128
+ @id_batch UNIQUEIDENTIFIER
129
+ AS
130
+ BEGIN
131
+ SET NOCOUNT ON;
132
+
133
+ -- ---------------------------------------------------------
134
+ -- VALIDAÇÃO 1: Título duplicado dentro do próprio lote
135
+ -- ---------------------------------------------------------
136
+ INSERT INTO import.errors (id_batch, row_num, error_msg)
137
+ SELECT @id_batch, s.row_num, 'Título duplicado no arquivo'
138
+ FROM (
139
+ SELECT *, COUNT(*) OVER (PARTITION BY title) AS cnt
140
+ FROM import.supply_types_staging WITH (NOLOCK)
141
+ WHERE id_batch = @id_batch
142
+ ) s
143
+ WHERE s.cnt > 1;
144
+
145
+ -- ---------------------------------------------------------
146
+ -- VALIDAÇÃO 2: Título já existente na produção (mesma unidade)
147
+ -- ---------------------------------------------------------
148
+ INSERT INTO import.errors (id_batch, row_num, error_msg)
149
+ SELECT @id_batch, s.row_num, 'Título já existe na base'
150
+ FROM import.supply_types_staging s WITH (NOLOCK)
151
+ INNER JOIN suppliers.supply_types t WITH (NOLOCK)
152
+ ON t.title = s.title COLLATE Latin1_General_CS_AS
153
+ AND t.removed = 0
154
+ AND t.id_company = s.id_company COLLATE Latin1_General_CS_AS
155
+ WHERE s.id_batch = @id_batch;
156
+
157
+ -- ---------------------------------------------------------
158
+ -- VALIDAÇÃO 3: Situação inválida
159
+ -- Aceita EXCLUSIVAMENTE 'Ativo' ou 'Inativo' (case/acento-insensitive).
160
+ -- Campo é obrigatório — vazio também é considerado inválido.
161
+ -- ---------------------------------------------------------
162
+ INSERT INTO import.errors (id_batch, row_num, error_msg)
163
+ SELECT @id_batch, s.row_num, 'Situação inválida (use Ativo ou Inativo)'
164
+ FROM import.supply_types_staging s WITH (NOLOCK)
165
+ WHERE s.id_batch = @id_batch
166
+ AND LTRIM(RTRIM(ISNULL(s.is_active, ''))) COLLATE Latin1_General_CI_AI
167
+ NOT IN ('ativo','inativo');
168
+
169
+
170
+ -- ---------------------------------------------------------
171
+ -- INSERÇÃO: apenas registros sem erro
172
+ -- ---------------------------------------------------------
173
+ INSERT INTO suppliers.supply_types
174
+ (
175
+ title,
176
+ description,
177
+ description_text,
178
+ is_active,
179
+ id_form,
180
+ id_company,
181
+ id_creation_user,
182
+ id_last_modified_user,
183
+ creation_date,
184
+ last_modified,
185
+ removed
186
+ )
187
+ SELECT
188
+ s.title,
189
+ NULLIF(s.description, '') AS description,
190
+ -- Fallback: description_text recebe o mesmo valor de description.
191
+ -- Caso o backend .NET possua helper para extrair texto puro do HTML,
192
+ -- substituir este valor antes/depois do INSERT.
193
+ NULLIF(s.description, '') AS description_text,
194
+ CASE
195
+ WHEN LTRIM(RTRIM(ISNULL(s.is_active, ''))) COLLATE Latin1_General_CI_AI
196
+ = 'inativo' THEN 0
197
+ ELSE 1
198
+ END AS is_active,
199
+
200
+ NULLIF(s.id_form, '') AS id_form,
201
+ s.id_company,
202
+ s.id_creation_user,
203
+ s.id_creation_user AS id_last_modified_user,
204
+ SYSDATETIMEOFFSET() AS creation_date,
205
+ SYSDATETIMEOFFSET() AS last_modified,
206
+ 0 AS removed
207
+ FROM import.supply_types_staging s WITH (NOLOCK)
208
+ WHERE s.id_batch = @id_batch
209
+ AND NOT EXISTS (
210
+ SELECT 1
211
+ FROM import.errors e
212
+ WHERE e.id_batch = s.id_batch
213
+ AND e.row_num = s.row_num
214
+ );
215
+
216
+ -- Observação:
217
+ -- `id` (char(8)) é gerado pelo DEFAULT dbo.new_id() da tabela — não incluir no INSERT.
218
+ END
219
+ GO
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forlogic-core",
3
- "version": "2.4.12",
3
+ "version": "2.4.13",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",