@fayz-ai/plugin-fiscal-br 0.13.2 → 0.13.4

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/dist/index.js CHANGED
@@ -4980,6 +4980,7 @@ var MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE = `-- ============================
4980
4980
  -- fiscalbr_notes_for_service an order's notes
4981
4981
  -- fiscalbr_note_for_service one note
4982
4982
  -- fiscalbr_notes_to_follow_for_service notes still waiting on PlugNotas
4983
+ -- fiscalbr_note_by_integration_for_service where a webhook belongs
4983
4984
  --
4984
4985
  -- A note is opened BEFORE PlugNotas is called, with the idIntegracao it will
4985
4986
  -- carry, so a call whose answer is lost is found again by that id instead of
@@ -5236,6 +5237,18 @@ AS $$
5236
5237
  SELECT d.xml FROM public.plg_fiscalbr_documents d WHERE d.document_id = p_document AND d.tenant_id = p_tenant
5237
5238
  $$;
5238
5239
 
5240
+ -- Where a PlugNotas webhook belongs: our idIntegracao names the note.
5241
+ CREATE OR REPLACE FUNCTION public.fiscalbr_note_by_integration_for_service(p_integration text)
5242
+ RETURNS jsonb
5243
+ LANGUAGE sql STABLE SECURITY DEFINER
5244
+ SET search_path TO ''
5245
+ AS $$
5246
+ SELECT jsonb_build_object('tenantId', d.tenant_id, 'documentId', d.document_id)
5247
+ FROM public.plg_fiscalbr_documents d
5248
+ WHERE d.provider_integration_id = p_integration AND d.provider = 'plugnotas'
5249
+ LIMIT 1
5250
+ $$;
5251
+
5239
5252
  -- Notes this service sent that PlugNotas has not settled: the fallback when a
5240
5253
  -- webhook does not arrive. Oldest poll first.
5241
5254
  CREATE OR REPLACE FUNCTION public.fiscalbr_notes_to_follow_for_service(p_limit integer DEFAULT 25)
@@ -5262,7 +5275,8 @@ BEGIN
5262
5275
  'public.fiscalbr_note_for_service(uuid, uuid)',
5263
5276
  'public.fiscalbr_notes_for_service(uuid, uuid)',
5264
5277
  'public.fiscalbr_note_xml_for_service(uuid, uuid)',
5265
- 'public.fiscalbr_notes_to_follow_for_service(integer)'
5278
+ 'public.fiscalbr_notes_to_follow_for_service(integer)',
5279
+ 'public.fiscalbr_note_by_integration_for_service(text)'
5266
5280
  ] LOOP
5267
5281
  EXECUTE format('REVOKE ALL ON FUNCTION %s FROM PUBLIC, anon, authenticated', f);
5268
5282
  IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'fayz_integration') THEN
@@ -5272,6 +5286,131 @@ BEGIN
5272
5286
  END
5273
5287
  $$;
5274
5288
  `;
5289
+ var MIGRATION_032_A_DELIVERED_NOTE_CARRIES_WHERE_IT_GOES = `-- ============================================================================
5290
+ -- 032 \u2014 a home-delivery NFC-e carries where it goes
5291
+ --
5292
+ -- The SEFAZ rejected Artorius' NFC-e delivered at home with "NFC-e de entrega
5293
+ -- a domicilio sem o endereco do destinatario": indPres 4 requires the
5294
+ -- recipient's address. The order's source for its note now carries the
5295
+ -- checkout's delivery address and the customer's email; the integration
5296
+ -- service adds the city's IBGE code (control-sdk-backend ADR 0008).
5297
+ -- ============================================================================
5298
+
5299
+ CREATE OR REPLACE FUNCTION public.fiscalbr_order_for_note_for_service(p_tenant uuid, p_order uuid)
5300
+ RETURNS jsonb
5301
+ LANGUAGE plpgsql STABLE SECURITY DEFINER
5302
+ SET search_path TO ''
5303
+ AS $$
5304
+ DECLARE
5305
+ v_checkout jsonb;
5306
+ BEGIN
5307
+ -- The shop is optional: a site order says how it was paid on its checkout,
5308
+ -- read only where the shop's table exists.
5309
+ IF to_regclass('public.plg_shop_checkout_sessions') IS NOT NULL THEN
5310
+ EXECUTE $q$
5311
+ SELECT jsonb_build_object(
5312
+ 'paymentMethod', s.payment_method, 'provider', s.provider, 'total', s.total, 'status', s.status,
5313
+ 'customerName', s.customer_name, 'customerDocument', s.customer_document,
5314
+ 'destinationState', s.shipping_address ->> 'state', 'destinationCity', s.shipping_address ->> 'city',
5315
+ 'customerEmail', s.customer_email,
5316
+ 'shippingAddress', CASE WHEN s.shipping_address IS NULL THEN NULL ELSE jsonb_build_object(
5317
+ 'street', s.shipping_address ->> 'street', 'number', s.shipping_address ->> 'number',
5318
+ 'complement', s.shipping_address ->> 'complement', 'district', s.shipping_address ->> 'district',
5319
+ 'city', s.shipping_address ->> 'city', 'state', s.shipping_address ->> 'state',
5320
+ 'postalCode', coalesce(s.shipping_address ->> 'postalCode', s.shipping_address ->> 'postal_code')) END)
5321
+ FROM public.plg_shop_checkout_sessions s
5322
+ WHERE s.order_id = $1 AND s.tenant_id = $2 AND s.status = 'paid'
5323
+ ORDER BY s.updated_at DESC LIMIT 1 $q$
5324
+ INTO v_checkout USING p_order, p_tenant;
5325
+ END IF;
5326
+
5327
+ RETURN (SELECT jsonb_build_object(
5328
+ 'orderId', o.id,
5329
+ 'tenantId', o.tenant_id,
5330
+ 'unitId', o.unit_id,
5331
+ 'kind', o.kind,
5332
+ 'status', o.status,
5333
+ 'channel', o.channel,
5334
+ 'createdAt', o.created_at,
5335
+ 'subtotal', o.subtotal,
5336
+ 'discount', o.discount,
5337
+ 'shipping', o.shipping,
5338
+ 'total', o.total,
5339
+ 'legalEntityId', u.company_legal_entity_id,
5340
+ 'items', coalesce((
5341
+ SELECT jsonb_agg(jsonb_build_object(
5342
+ 'itemId', i.id,
5343
+ 'productId', i.product_id,
5344
+ 'code', coalesce(nullif(p.sku, ''), i.product_id::text, i.id::text),
5345
+ 'name', i.name,
5346
+ 'quantity', i.quantity,
5347
+ 'unitPrice', i.unit_price,
5348
+ 'discount', i.discount,
5349
+ 'total', i.total,
5350
+ 'cancelled', i.cancelled_at IS NOT NULL,
5351
+ -- The product's own value wins, field by field, over its group's (V1).
5352
+ 'tax', CASE WHEN t.product_id IS NULL AND g.id IS NULL THEN NULL ELSE jsonb_build_object(
5353
+ 'ncm', t.ncm,
5354
+ 'cest', coalesce(t.cest, g.cest),
5355
+ 'cfop', coalesce(nullif(t.cfop, ''), g.cfop),
5356
+ 'cst', coalesce(nullif(t.cst, ''), g.cst),
5357
+ 'csosn', g.csosn,
5358
+ 'origin', coalesce(nullif(t.icms_origin, ''), g.icms_origin),
5359
+ 'icmsRate', CASE WHEN coalesce(t.icms_rate, 0) > 0 THEN t.icms_rate ELSE g.icms_rate END,
5360
+ 'icmsBaseMode', g.icms_bc_mode,
5361
+ 'pisSituation', coalesce(nullif(t.pis_situation, ''), g.pis_situation),
5362
+ 'pisRate', CASE WHEN coalesce(t.pis_rate, 0) > 0 THEN t.pis_rate ELSE g.pis_rate END,
5363
+ 'cofinsSituation', coalesce(nullif(t.cofins_situation, ''), g.cofins_situation),
5364
+ 'cofinsRate', CASE WHEN coalesce(t.cofins_rate, 0) > 0 THEN t.cofins_rate ELSE g.cofins_rate END,
5365
+ 'ibsCbsSituation', coalesce(nullif(t.ibs_cbs_situation, ''), g.ibs_cbs_situation),
5366
+ 'ibsCbsClassification', coalesce(nullif(t.ibs_cbs_classification, ''), g.ibs_cbs_classification),
5367
+ 'group', g.name
5368
+ ) END
5369
+ ) ORDER BY i.sort_order, i.created_at)
5370
+ FROM public.items i
5371
+ LEFT JOIN public.products p ON p.id = i.product_id AND p.tenant_id = i.tenant_id
5372
+ LEFT JOIN public.plg_fiscalbr_product_tax_info t ON t.product_id = i.product_id AND t.tenant_id = i.tenant_id
5373
+ LEFT JOIN public.plg_fiscalbr_tax_groups g ON g.id = t.tax_group_id AND g.tenant_id = i.tenant_id
5374
+ WHERE i.order_id = o.id AND i.tenant_id = o.tenant_id
5375
+ ), '[]'::jsonb),
5376
+ -- Anything that is not a site order is paid through the movements the
5377
+ -- order received, net of reversals, by method.
5378
+ 'checkout', v_checkout,
5379
+ 'payments', coalesce((
5380
+ SELECT jsonb_agg(jsonb_build_object('method', x.method, 'methodType', x.method_type, 'amount', x.amount))
5381
+ FROM (
5382
+ SELECT pm.name AS method, pt.name AS method_type,
5383
+ sum(CASE WHEN m.direction = 'credit' THEN m.paid_amount ELSE -m.paid_amount END) AS amount
5384
+ FROM public.plg_financial_movements m
5385
+ LEFT JOIN public.plg_financial_payment_methods pm ON pm.id = m.payment_method_id
5386
+ LEFT JOIN public.plg_financial_payment_method_types pt ON pt.id = pm.payment_method_type_id
5387
+ WHERE m.tenant_id = o.tenant_id AND m.movement_kind = 'payment' AND m.status = 'paid'
5388
+ AND (m.invoice_id = o.id OR m.financial_invoice_id IN (
5389
+ SELECT fi.id FROM public.plg_financial_invoices fi WHERE fi.order_id = o.id AND fi.tenant_id = o.tenant_id))
5390
+ GROUP BY pm.name, pt.name
5391
+ ) x WHERE x.amount <> 0
5392
+ ), '[]'::jsonb),
5393
+ 'notes', coalesce((
5394
+ SELECT jsonb_agg(jsonb_build_object('documentId', d.document_id, 'model', d.model, 'status', d.status))
5395
+ FROM public.plg_fiscalbr_documents d
5396
+ WHERE d.tenant_id = o.tenant_id AND d.order_id = o.id
5397
+ ), '[]'::jsonb)
5398
+ )
5399
+ FROM public.orders o
5400
+ LEFT JOIN app.units u ON u.id = o.unit_id AND u.tenant_id = o.tenant_id
5401
+ WHERE o.id = p_order AND o.tenant_id = p_tenant);
5402
+ END
5403
+ $$;
5404
+
5405
+ REVOKE ALL ON FUNCTION public.fiscalbr_order_for_note_for_service(uuid, uuid) FROM PUBLIC, anon, authenticated;
5406
+ DO $$
5407
+ BEGIN
5408
+ IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'fayz_integration') THEN
5409
+ GRANT EXECUTE ON FUNCTION public.fiscalbr_order_for_note_for_service(uuid, uuid) TO fayz_integration;
5410
+ END IF;
5411
+ END
5412
+ $$;
5413
+ `;
5275
5414
  var MIGRATIONS = [
5276
5415
  { id: "000_baseline", sql: MIGRATION_000_BASELINE },
5277
5416
  { id: "001_preserve_terminal_results_and_recover_leases", sql: MIGRATION_001_PRESERVE_TERMINAL_RESULTS_AND_RECOVER_LEASES },
@@ -5304,7 +5443,8 @@ var MIGRATIONS = [
5304
5443
  { id: "028_a_linha_de_estoque_tem_unidade", sql: MIGRATION_028_A_LINHA_DE_ESTOQUE_TEM_UNIDADE },
5305
5444
  { id: "029_the_service_writes_the_intake_health", sql: MIGRATION_029_THE_SERVICE_WRITES_THE_INTAKE_HEALTH },
5306
5445
  { id: "030_the_service_onboards_the_emitter", sql: MIGRATION_030_THE_SERVICE_ONBOARDS_THE_EMITTER },
5307
- { id: "031_the_service_issues_the_note", sql: MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE }
5446
+ { id: "031_the_service_issues_the_note", sql: MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE },
5447
+ { id: "032_a_delivered_note_carries_where_it_goes", sql: MIGRATION_032_A_DELIVERED_NOTE_CARRIES_WHERE_IT_GOES }
5308
5448
  ];
5309
5449
  var taxGroupEntity = {
5310
5450
  name: "Grupo fiscal",