@fayz-ai/plugin-fiscal-br 0.15.12 → 0.16.0
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/InvoiceNotesButton-4L7SLYMA.js +6 -0
- package/dist/{InvoiceNotesButton-SC3G7FUR.js.map → InvoiceNotesButton-4L7SLYMA.js.map} +1 -1
- package/dist/NotasFiscaisView-LU4A65J7.js +6 -0
- package/dist/NotasFiscaisView-LU4A65J7.js.map +1 -0
- package/dist/OrderNoteCard-NZOVNTR3.js +5 -0
- package/dist/{OrderNoteCard-PDSK2AS3.js.map → OrderNoteCard-NZOVNTR3.js.map} +1 -1
- package/dist/UnitFiscalWidgets-XSED3WPQ.js +564 -0
- package/dist/UnitFiscalWidgets-XSED3WPQ.js.map +1 -0
- package/dist/{chunk-ODMEESFD.js → chunk-CWYTWKXW.js} +23 -3
- package/dist/chunk-CWYTWKXW.js.map +1 -0
- package/dist/chunk-E5TUJOGT.js +644 -0
- package/dist/chunk-E5TUJOGT.js.map +1 -0
- package/dist/{chunk-SSPDH22E.js → chunk-ITWWGLEV.js} +3 -3
- package/dist/{chunk-SSPDH22E.js.map → chunk-ITWWGLEV.js.map} +1 -1
- package/dist/chunk-U4TQSVWI.js +269 -0
- package/dist/chunk-U4TQSVWI.js.map +1 -0
- package/dist/chunk-YSRG5JQI.js +205 -0
- package/dist/chunk-YSRG5JQI.js.map +1 -0
- package/dist/data/issued.d.ts +97 -0
- package/dist/data/issued.d.ts.map +1 -1
- package/dist/data/registries.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +52 -20
- package/dist/index.js.map +1 -1
- package/dist/lib/notes.d.ts +35 -1
- package/dist/lib/notes.d.ts.map +1 -1
- package/dist/migrations/index.d.ts +4 -0
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/main.browser.js +5 -1
- package/dist/migrations/main.browser.js.map +1 -1
- package/dist/migrations/main.js +400 -2
- package/dist/migrations/main.js.map +1 -1
- package/dist/views/InvoiceNotesButton.d.ts.map +1 -1
- package/dist/views/NotasFiscaisView.d.ts +4 -0
- package/dist/views/NotasFiscaisView.d.ts.map +1 -0
- package/dist/views/UnitFiscalWidgets.d.ts +6 -4
- package/dist/views/UnitFiscalWidgets.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/InvoiceNotesButton-SC3G7FUR.js +0 -5
- package/dist/OrderNoteCard-PDSK2AS3.js +0 -5
- package/dist/UnitFiscalWidgets-K4PX3HWG.js +0 -503
- package/dist/UnitFiscalWidgets-K4PX3HWG.js.map +0 -1
- package/dist/chunk-F325US5P.js +0 -387
- package/dist/chunk-F325US5P.js.map +0 -1
- package/dist/chunk-ODMEESFD.js.map +0 -1
package/dist/migrations/main.js
CHANGED
|
@@ -5629,6 +5629,400 @@ END $function$;
|
|
|
5629
5629
|
REVOKE ALL ON FUNCTION public.fiscalbr_note_receipt_occurrences(uuid) FROM PUBLIC, anon;
|
|
5630
5630
|
GRANT EXECUTE ON FUNCTION public.fiscalbr_note_receipt_occurrences(uuid) TO authenticated, service_role;
|
|
5631
5631
|
`;
|
|
5632
|
+
var MIGRATION_035_THE_EMITTER_BORROWS_THE_UNIT_ADDRESS = `-- ============================================================================
|
|
5633
|
+
-- 035 \u2014 the emitter borrows the unit's fiscal address
|
|
5634
|
+
--
|
|
5635
|
+
-- The PlugNotas company registration needs a complete fiscal address (street,
|
|
5636
|
+
-- city, state, CEP). The address lives on the legal entity (company_legal_
|
|
5637
|
+
-- entities / addresses owner_type 'company_legal_entity'), but the UI only
|
|
5638
|
+
-- edits the UNIT's address (addresses owner_type 'unit'): a company created
|
|
5639
|
+
-- from a unit's CNPJ never got its own address, so the registration refused
|
|
5640
|
+
-- with \xABEndere\xE7o fiscal completo\xBB. fiscalbr_emitter_for_service now falls back
|
|
5641
|
+
-- to a unit of this CNPJ (the matriz's/first fiscal address) when the legal
|
|
5642
|
+
-- entity has none. Read-only shape change; CREATE OR REPLACE, idempotent.
|
|
5643
|
+
-- ============================================================================
|
|
5644
|
+
|
|
5645
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_emitter_for_service(p_tenant uuid, p_legal_entity uuid)
|
|
5646
|
+
RETURNS jsonb
|
|
5647
|
+
LANGUAGE sql
|
|
5648
|
+
STABLE SECURITY DEFINER
|
|
5649
|
+
SET search_path TO ''
|
|
5650
|
+
AS $function$
|
|
5651
|
+
SELECT jsonb_build_object(
|
|
5652
|
+
'legalEntityId', l.id,
|
|
5653
|
+
'tenantId', l.tenant_id,
|
|
5654
|
+
'cnpj', regexp_replace(coalesce(l.document_number, ''), '\\D', '', 'g'),
|
|
5655
|
+
'legalName', l.legal_name,
|
|
5656
|
+
'tradeName', l.trade_name,
|
|
5657
|
+
'stateRegistration', l.state_registration,
|
|
5658
|
+
'municipalRegistration', l.municipal_registration,
|
|
5659
|
+
'taxRegime', l.tax_regime,
|
|
5660
|
+
'taxRegimeCode', coalesce(s.tax_regime_code, CASE WHEN l.tax_regime = 'simples_nacional' THEN 1 END),
|
|
5661
|
+
'specialTaxRegimeCode', coalesce(s.special_tax_regime_code, 0),
|
|
5662
|
+
'cnae', l.cnae,
|
|
5663
|
+
'email', coalesce(nullif(l.fiscal_email, ''), l.email),
|
|
5664
|
+
'phone', l.phone,
|
|
5665
|
+
-- The fiscal address: the legal entity's own addresses row, else its retired
|
|
5666
|
+
-- columns, else a UNIT of this CNPJ (the UI edits the unit's address, not the
|
|
5667
|
+
-- PJ's, so a new company that never set its own address borrows the unit's).
|
|
5668
|
+
'address', coalesce(
|
|
5669
|
+
CASE WHEN a.id IS NOT NULL THEN jsonb_build_object(
|
|
5670
|
+
'street', a.street, 'number', a.number, 'complement', a.complement,
|
|
5671
|
+
'district', a.district, 'city', a.city, 'state', a.state, 'postalCode', a.postal_code,
|
|
5672
|
+
'ibgeCityCode', l.ibge_city_code, 'country', a.country) END,
|
|
5673
|
+
CASE WHEN nullif(l.address, '') IS NOT NULL THEN jsonb_build_object(
|
|
5674
|
+
'street', l.address, 'number', l.address_number, 'complement', l.address_complement,
|
|
5675
|
+
'district', l.district, 'city', l.city, 'state', l.state, 'postalCode', l.postal_code,
|
|
5676
|
+
'ibgeCityCode', l.ibge_city_code, 'country', l.country) END,
|
|
5677
|
+
CASE WHEN ua.id IS NOT NULL THEN jsonb_build_object(
|
|
5678
|
+
'street', ua.street, 'number', ua.number, 'complement', ua.complement,
|
|
5679
|
+
'district', ua.district, 'city', ua.city, 'state', ua.state, 'postalCode', ua.postal_code,
|
|
5680
|
+
'ibgeCityCode', l.ibge_city_code, 'country', ua.country) END,
|
|
5681
|
+
jsonb_build_object(
|
|
5682
|
+
'street', l.address, 'number', l.address_number, 'complement', l.address_complement,
|
|
5683
|
+
'district', l.district, 'city', l.city, 'state', l.state, 'postalCode', l.postal_code,
|
|
5684
|
+
'ibgeCityCode', l.ibge_city_code, 'country', l.country)
|
|
5685
|
+
),
|
|
5686
|
+
'settings', CASE WHEN s.id IS NULL THEN NULL ELSE jsonb_build_object(
|
|
5687
|
+
'nfe', jsonb_build_object('enabled', s.nfe_enabled, 'production', s.nfe_production, 'series', s.nfe_series, 'nextNumber', s.nfe_next_number),
|
|
5688
|
+
'nfce', jsonb_build_object('enabled', s.nfce_enabled, 'production', s.nfce_production, 'series', s.nfce_series, 'nextNumber', s.nfce_next_number,
|
|
5689
|
+
'cscId', s.nfce_csc_id, 'cscToken', s.nfce_csc_token, 'genericItem', s.nfce_generic_item),
|
|
5690
|
+
'certificate', jsonb_build_object('providerId', s.certificate_provider_id, 'holder', s.certificate_cn,
|
|
5691
|
+
'expiresAt', s.certificate_expires_at, 'uploadedAt', s.certificate_uploaded_at),
|
|
5692
|
+
'providerSyncedAt', s.provider_synced_at,
|
|
5693
|
+
'providerWebhookAt', s.provider_webhook_at
|
|
5694
|
+
) END
|
|
5695
|
+
)
|
|
5696
|
+
FROM public.company_legal_entities l
|
|
5697
|
+
LEFT JOIN public.plg_fiscalbr_emitter_settings s ON s.legal_entity_id = l.id AND s.tenant_id = l.tenant_id
|
|
5698
|
+
LEFT JOIN LATERAL (
|
|
5699
|
+
SELECT * FROM public.addresses x
|
|
5700
|
+
WHERE x.tenant_id = l.tenant_id AND x.owner_type = 'company_legal_entity' AND x.owner_id = l.id
|
|
5701
|
+
ORDER BY (x.kind = 'fiscal') DESC, x.is_default DESC, x.created_at
|
|
5702
|
+
LIMIT 1
|
|
5703
|
+
) a ON true
|
|
5704
|
+
LEFT JOIN LATERAL (
|
|
5705
|
+
SELECT x.* FROM public.addresses x
|
|
5706
|
+
JOIN app.units u2 ON u2.id = x.owner_id AND u2.company_legal_entity_id = l.id AND u2.tenant_id = l.tenant_id
|
|
5707
|
+
WHERE x.tenant_id = l.tenant_id AND x.owner_type = 'unit' AND nullif(x.street, '') IS NOT NULL
|
|
5708
|
+
ORDER BY (x.kind = 'fiscal') DESC, x.is_default DESC, x.created_at
|
|
5709
|
+
LIMIT 1
|
|
5710
|
+
) ua ON true
|
|
5711
|
+
WHERE l.id = p_legal_entity AND l.tenant_id = p_tenant
|
|
5712
|
+
$function$;
|
|
5713
|
+
`;
|
|
5714
|
+
var MIGRATION_036_THE_FISCAL_GROUP_NAMES_THE_SERVICE_ON_THE_NOTE = `-- The fiscal group can name its services on the NFS-e.
|
|
5715
|
+
--
|
|
5716
|
+
-- A service's own name is not always what the city-hall note should read:
|
|
5717
|
+
-- "Sess\xE3o de terapia capilar" often has to go out as "PRESTA\xC7\xC3O DE SERVI\xC7OS DE
|
|
5718
|
+
-- EST\xC9TICA". This column lets the fiscal group carry that text; when set, the
|
|
5719
|
+
-- note's discrimina\xE7\xE3o uses it in place of each service name (the services
|
|
5720
|
+
-- that inherit the group via plg_fiscalbr_product_tax_info.tax_group_id). NULL
|
|
5721
|
+
-- keeps the previous behaviour \u2014 the service's own name.
|
|
5722
|
+
--
|
|
5723
|
+
-- Additive and idempotent: the pool is live.
|
|
5724
|
+
|
|
5725
|
+
ALTER TABLE public.plg_fiscalbr_tax_groups
|
|
5726
|
+
ADD COLUMN IF NOT EXISTS nfse_description text;
|
|
5727
|
+
|
|
5728
|
+
COMMENT ON COLUMN public.plg_fiscalbr_tax_groups.nfse_description IS
|
|
5729
|
+
'Custom discrimina\xE7\xE3o for the NFS-e: when set, the note names this group''s services with this text instead of each service name.';
|
|
5730
|
+
`;
|
|
5731
|
+
var MIGRATION_037_THE_SERVICE_ISSUES_THE_RECEIVABLE_NOTE = `-- ============================================================================
|
|
5732
|
+
-- 037 \u2014 the integration service issues the RECEIVABLE's NFS-e
|
|
5733
|
+
--
|
|
5734
|
+
-- The counterpart of 031 for Contas a Receber: 031 gave the service its doors
|
|
5735
|
+
-- to an ORDER's note; this gives the same for a receivable (plg_financial_
|
|
5736
|
+
-- invoices), which is where an NFS-e of service is issued. The backend
|
|
5737
|
+
-- (control-sdk-backend) calls these; they were missing, so \xABEmitir NFS-e\xBB
|
|
5738
|
+
-- raised \xABfunction fiscalbr_invoice_for_note_for_service does not exist\xBB.
|
|
5739
|
+
--
|
|
5740
|
+
-- fiscalbr_invoice_for_note_for_service what the note is made of
|
|
5741
|
+
-- fiscalbr_nfse_open_for_service the note, opened + RPS number
|
|
5742
|
+
-- fiscalbr_notes_of_invoice_for_service a receivable's notes
|
|
5743
|
+
-- fiscalbr_invoices_to_issue_for_service receivables with no live NFS-e
|
|
5744
|
+
--
|
|
5745
|
+
-- Additive and idempotent: the pool is live.
|
|
5746
|
+
-- ============================================================================
|
|
5747
|
+
|
|
5748
|
+
-- A note issued straight from a receivable carries its invoice; and the emitter
|
|
5749
|
+
-- counts its own RPS numbers (PlugNotas is told numeracaoAutomatica, but the
|
|
5750
|
+
-- service stamps the n\xFAmero it will send).
|
|
5751
|
+
ALTER TABLE public.plg_fiscalbr_documents
|
|
5752
|
+
ADD COLUMN IF NOT EXISTS invoice_id uuid;
|
|
5753
|
+
CREATE INDEX IF NOT EXISTS plg_fiscalbr_documents_invoice_idx
|
|
5754
|
+
ON public.plg_fiscalbr_documents (tenant_id, invoice_id) WHERE invoice_id IS NOT NULL;
|
|
5755
|
+
ALTER TABLE public.plg_fiscalbr_emitter_settings
|
|
5756
|
+
ADD COLUMN IF NOT EXISTS nfse_next_number integer;
|
|
5757
|
+
|
|
5758
|
+
-- What the NFS-e of a receivable is made of: the invoice, its service lines,
|
|
5759
|
+
-- who is billed and where, and the notes it already carries. The service
|
|
5760
|
+
-- applies the rules; this only gathers.
|
|
5761
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_invoice_for_note_for_service(p_tenant uuid, p_invoice uuid)
|
|
5762
|
+
RETURNS jsonb
|
|
5763
|
+
LANGUAGE sql STABLE SECURITY DEFINER
|
|
5764
|
+
SET search_path TO ''
|
|
5765
|
+
AS $$
|
|
5766
|
+
SELECT jsonb_build_object(
|
|
5767
|
+
'invoiceId', i.id,
|
|
5768
|
+
'tenantId', i.tenant_id,
|
|
5769
|
+
'unitId', i.unit_id,
|
|
5770
|
+
'legalEntityId', u.company_legal_entity_id,
|
|
5771
|
+
'status', 'open',
|
|
5772
|
+
'total', i.total,
|
|
5773
|
+
'items', coalesce((
|
|
5774
|
+
SELECT jsonb_agg(jsonb_build_object(
|
|
5775
|
+
'id', l.item_id,
|
|
5776
|
+
'serviceId', coalesce(l.product_id, nullif(l.item_metadata ->> 'referenceId', '')::uuid),
|
|
5777
|
+
'description', l.name,
|
|
5778
|
+
'quantity', l.quantity,
|
|
5779
|
+
'total', l.total,
|
|
5780
|
+
'discount', coalesce(l.discount, 0),
|
|
5781
|
+
'fiscalOverride', NULL
|
|
5782
|
+
) ORDER BY l.name)
|
|
5783
|
+
FROM public.v_financial_invoice_lines l
|
|
5784
|
+
WHERE l.invoice_id = i.id AND l.tenant_id = i.tenant_id AND l.item_cancelled_at IS NULL
|
|
5785
|
+
), '[]'::jsonb),
|
|
5786
|
+
'customer', (
|
|
5787
|
+
SELECT jsonb_build_object(
|
|
5788
|
+
'name', pe.name,
|
|
5789
|
+
'document', pe.document_number,
|
|
5790
|
+
'email', pe.email,
|
|
5791
|
+
'stateRegistration', NULL,
|
|
5792
|
+
'address', (
|
|
5793
|
+
SELECT jsonb_build_object(
|
|
5794
|
+
'street', a.street, 'number', a.number, 'complement', a.complement,
|
|
5795
|
+
'district', a.district, 'city', a.city, 'state', a.state, 'postalCode', a.postal_code)
|
|
5796
|
+
FROM public.addresses a
|
|
5797
|
+
WHERE a.owner_type = 'person' AND a.owner_id = pe.id
|
|
5798
|
+
ORDER BY a.is_default DESC NULLS LAST
|
|
5799
|
+
LIMIT 1)
|
|
5800
|
+
)
|
|
5801
|
+
FROM public.people pe WHERE pe.id = i.counterparty_person_id
|
|
5802
|
+
),
|
|
5803
|
+
'notes', coalesce((
|
|
5804
|
+
SELECT jsonb_agg(jsonb_build_object('documentId', d.document_id, 'model', d.model, 'status', d.status))
|
|
5805
|
+
FROM public.plg_fiscalbr_documents d
|
|
5806
|
+
WHERE d.tenant_id = i.tenant_id AND d.invoice_id = i.id
|
|
5807
|
+
), '[]'::jsonb)
|
|
5808
|
+
)
|
|
5809
|
+
FROM public.plg_financial_invoices i
|
|
5810
|
+
LEFT JOIN app.units u ON u.id = i.unit_id AND u.tenant_id = i.tenant_id
|
|
5811
|
+
WHERE i.id = p_invoice AND i.tenant_id = p_tenant
|
|
5812
|
+
$$;
|
|
5813
|
+
|
|
5814
|
+
-- The note, opened before PlugNotas is called, with the idIntegracao and the
|
|
5815
|
+
-- RPS number it will carry. One live NFS-e per receivable: a second \xABEmitir\xBB
|
|
5816
|
+
-- while one processes or is authorized is refused (P0001 note_exists).
|
|
5817
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_nfse_open_for_service(
|
|
5818
|
+
p_tenant uuid, p_invoice uuid, p_input jsonb
|
|
5819
|
+
)
|
|
5820
|
+
RETURNS jsonb
|
|
5821
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
5822
|
+
SET search_path TO ''
|
|
5823
|
+
AS $$
|
|
5824
|
+
DECLARE
|
|
5825
|
+
v_inv public.plg_financial_invoices%ROWTYPE;
|
|
5826
|
+
v_legal uuid;
|
|
5827
|
+
v_doc uuid := gen_random_uuid();
|
|
5828
|
+
v_integration text;
|
|
5829
|
+
v_existing uuid;
|
|
5830
|
+
v_number integer;
|
|
5831
|
+
BEGIN
|
|
5832
|
+
SELECT * INTO v_inv FROM public.plg_financial_invoices WHERE id = p_invoice AND tenant_id = p_tenant FOR UPDATE;
|
|
5833
|
+
IF NOT FOUND THEN
|
|
5834
|
+
RAISE EXCEPTION 'fiscalbr_nfse_open: invoice % is not of tenant %', p_invoice, p_tenant USING ERRCODE = 'P0002';
|
|
5835
|
+
END IF;
|
|
5836
|
+
|
|
5837
|
+
SELECT u.company_legal_entity_id INTO v_legal FROM app.units u WHERE u.id = v_inv.unit_id AND u.tenant_id = p_tenant;
|
|
5838
|
+
|
|
5839
|
+
SELECT document_id INTO v_existing FROM public.plg_fiscalbr_documents
|
|
5840
|
+
WHERE tenant_id = p_tenant AND invoice_id = p_invoice AND model = 'nfse' AND status IN ('processing', 'authorized')
|
|
5841
|
+
LIMIT 1;
|
|
5842
|
+
IF v_existing IS NOT NULL THEN
|
|
5843
|
+
RAISE EXCEPTION 'note_exists' USING ERRCODE = 'P0001', DETAIL = v_existing::text;
|
|
5844
|
+
END IF;
|
|
5845
|
+
|
|
5846
|
+
-- Next RPS number for this emitter (legal entity), assigned atomically.
|
|
5847
|
+
IF v_legal IS NOT NULL THEN
|
|
5848
|
+
UPDATE public.plg_fiscalbr_emitter_settings
|
|
5849
|
+
SET nfse_next_number = coalesce(nfse_next_number, 1) + 1
|
|
5850
|
+
WHERE legal_entity_id = v_legal
|
|
5851
|
+
RETURNING coalesce(nfse_next_number, 1) - 1 INTO v_number;
|
|
5852
|
+
END IF;
|
|
5853
|
+
v_number := coalesce(v_number, 1);
|
|
5854
|
+
|
|
5855
|
+
v_integration := 'csb-' || v_doc::text;
|
|
5856
|
+
|
|
5857
|
+
INSERT INTO public.documents (id, tenant_id, kind, title, status, unit_id, subject_type, subject_id, created_by, metadata)
|
|
5858
|
+
VALUES (v_doc, p_tenant, 'fiscal_document', 'NFS-e da conta', 'processing', v_inv.unit_id,
|
|
5859
|
+
'invoice', p_invoice, nullif(p_input ->> 'userId', '')::uuid, jsonb_build_object('source', 'integration-service'));
|
|
5860
|
+
|
|
5861
|
+
INSERT INTO public.plg_fiscalbr_documents (
|
|
5862
|
+
document_id, tenant_id, unit_id, invoice_id, model, environment, status, series, number, provider,
|
|
5863
|
+
provider_integration_id, total_value, recipient_snapshot, request_payload, source_snapshot, emitted_by_user_id, metadata)
|
|
5864
|
+
VALUES (
|
|
5865
|
+
v_doc, p_tenant, v_inv.unit_id, p_invoice, 'nfse', p_input ->> 'environment', 'processing', p_input ->> 'series', v_number::text, 'plugnotas',
|
|
5866
|
+
v_integration, (p_input ->> 'total')::numeric, p_input -> 'recipient', p_input -> 'request', p_input -> 'source',
|
|
5867
|
+
nullif(p_input ->> 'userId', '')::uuid, jsonb_build_object('emitterCnpj', p_input ->> 'emitterCnpj'));
|
|
5868
|
+
|
|
5869
|
+
INSERT INTO public.plg_fiscalbr_document_events (tenant_id, unit_id, document_id, event_type, status, created_by_user_id)
|
|
5870
|
+
VALUES (p_tenant, v_inv.unit_id, v_doc, 'issue_requested', 'processing', nullif(p_input ->> 'userId', '')::uuid);
|
|
5871
|
+
|
|
5872
|
+
RETURN jsonb_build_object('documentId', v_doc, 'integrationId', v_integration, 'number', v_number);
|
|
5873
|
+
END
|
|
5874
|
+
$$;
|
|
5875
|
+
|
|
5876
|
+
-- A receivable's notes, newest first (reuses the single-note reader from 031).
|
|
5877
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_notes_of_invoice_for_service(p_tenant uuid, p_invoice uuid)
|
|
5878
|
+
RETURNS SETOF jsonb
|
|
5879
|
+
LANGUAGE sql STABLE SECURITY DEFINER
|
|
5880
|
+
SET search_path TO ''
|
|
5881
|
+
AS $$
|
|
5882
|
+
SELECT public.fiscalbr_note_for_service(p_tenant, d.document_id)
|
|
5883
|
+
FROM public.plg_fiscalbr_documents d
|
|
5884
|
+
WHERE d.tenant_id = p_tenant AND d.invoice_id = p_invoice
|
|
5885
|
+
ORDER BY d.created_at DESC
|
|
5886
|
+
$$;
|
|
5887
|
+
|
|
5888
|
+
-- The period's receivables with no live NFS-e yet (the V1's \xABSem nota\xBB).
|
|
5889
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_invoices_to_issue_for_service(p_tenant uuid, p_params jsonb)
|
|
5890
|
+
RETURNS SETOF jsonb
|
|
5891
|
+
LANGUAGE sql STABLE SECURITY DEFINER
|
|
5892
|
+
SET search_path TO ''
|
|
5893
|
+
AS $$
|
|
5894
|
+
SELECT jsonb_build_object(
|
|
5895
|
+
'invoiceId', i.id,
|
|
5896
|
+
'number', i.number,
|
|
5897
|
+
'customerName', pe.name,
|
|
5898
|
+
'date', i.issue_date,
|
|
5899
|
+
'total', i.total,
|
|
5900
|
+
'paid', 0,
|
|
5901
|
+
'issuable', (pe.document_number IS NOT NULL AND pe.document_number <> ''),
|
|
5902
|
+
'reason', CASE WHEN pe.document_number IS NULL OR pe.document_number = '' THEN 'o cliente n\xE3o tem CPF/CNPJ' ELSE NULL END)
|
|
5903
|
+
FROM public.plg_financial_invoices i
|
|
5904
|
+
LEFT JOIN public.people pe ON pe.id = i.counterparty_person_id
|
|
5905
|
+
WHERE i.tenant_id = p_tenant AND i.direction = 'receivable'
|
|
5906
|
+
AND NOT EXISTS (
|
|
5907
|
+
SELECT 1 FROM public.plg_fiscalbr_documents d
|
|
5908
|
+
WHERE d.tenant_id = i.tenant_id AND d.invoice_id = i.id AND d.model = 'nfse' AND d.status IN ('processing', 'authorized'))
|
|
5909
|
+
AND ((p_params ->> 'from') IS NULL OR i.issue_date >= (p_params ->> 'from')::date)
|
|
5910
|
+
AND ((p_params ->> 'to') IS NULL OR i.issue_date <= (p_params ->> 'to')::date)
|
|
5911
|
+
ORDER BY i.issue_date DESC NULLS LAST
|
|
5912
|
+
LIMIT greatest(1, least(coalesce((p_params ->> 'limit')::int, 300), 500))
|
|
5913
|
+
$$;
|
|
5914
|
+
|
|
5915
|
+
DO $$
|
|
5916
|
+
DECLARE
|
|
5917
|
+
f text;
|
|
5918
|
+
BEGIN
|
|
5919
|
+
FOREACH f IN ARRAY ARRAY[
|
|
5920
|
+
'public.fiscalbr_invoice_for_note_for_service(uuid, uuid)',
|
|
5921
|
+
'public.fiscalbr_nfse_open_for_service(uuid, uuid, jsonb)',
|
|
5922
|
+
'public.fiscalbr_notes_of_invoice_for_service(uuid, uuid)',
|
|
5923
|
+
'public.fiscalbr_invoices_to_issue_for_service(uuid, jsonb)'
|
|
5924
|
+
] LOOP
|
|
5925
|
+
EXECUTE format('REVOKE ALL ON FUNCTION %s FROM PUBLIC, anon, authenticated', f);
|
|
5926
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'fayz_integration') THEN
|
|
5927
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %s TO fayz_integration', f);
|
|
5928
|
+
END IF;
|
|
5929
|
+
END LOOP;
|
|
5930
|
+
END
|
|
5931
|
+
$$;
|
|
5932
|
+
`;
|
|
5933
|
+
var MIGRATION_038_THE_EMITTER_TELLS_ITS_NFSE_CONFIG = `-- ============================================================================
|
|
5934
|
+
-- 038 \u2014 the emitter reader returns its NFS-e config
|
|
5935
|
+
--
|
|
5936
|
+
-- fiscalbr_emitter_for_service built \`settings\` with nfe/nfce/certificate but
|
|
5937
|
+
-- no \`nfse\` block, so the integration service read the company's NFS-e config
|
|
5938
|
+
-- as empty and refused every issue with emitter_not_ready (nfse_enabled /
|
|
5939
|
+
-- service_code / iss "missing") even though the columns were filled. This adds
|
|
5940
|
+
-- the nfse block, mirroring what door.ts reads (settings.nfse.*). Read-only
|
|
5941
|
+
-- shape change; CREATE OR REPLACE, idempotent.
|
|
5942
|
+
-- ============================================================================
|
|
5943
|
+
|
|
5944
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_emitter_for_service(p_tenant uuid, p_legal_entity uuid)
|
|
5945
|
+
RETURNS jsonb
|
|
5946
|
+
LANGUAGE sql
|
|
5947
|
+
STABLE SECURITY DEFINER
|
|
5948
|
+
SET search_path TO ''
|
|
5949
|
+
AS $function$
|
|
5950
|
+
SELECT jsonb_build_object(
|
|
5951
|
+
'legalEntityId', l.id,
|
|
5952
|
+
'tenantId', l.tenant_id,
|
|
5953
|
+
'cnpj', regexp_replace(coalesce(l.document_number, ''), '\\D', '', 'g'),
|
|
5954
|
+
'legalName', l.legal_name,
|
|
5955
|
+
'tradeName', l.trade_name,
|
|
5956
|
+
'stateRegistration', l.state_registration,
|
|
5957
|
+
'municipalRegistration', l.municipal_registration,
|
|
5958
|
+
'taxRegime', l.tax_regime,
|
|
5959
|
+
'taxRegimeCode', coalesce(s.tax_regime_code, CASE WHEN l.tax_regime = 'simples_nacional' THEN 1 END),
|
|
5960
|
+
'specialTaxRegimeCode', coalesce(s.special_tax_regime_code, 0),
|
|
5961
|
+
'cnae', l.cnae,
|
|
5962
|
+
'email', coalesce(nullif(l.fiscal_email, ''), l.email),
|
|
5963
|
+
'phone', l.phone,
|
|
5964
|
+
'address', coalesce(
|
|
5965
|
+
CASE WHEN a.id IS NOT NULL THEN jsonb_build_object(
|
|
5966
|
+
'street', a.street, 'number', a.number, 'complement', a.complement,
|
|
5967
|
+
'district', a.district, 'city', a.city, 'state', a.state, 'postalCode', a.postal_code,
|
|
5968
|
+
'ibgeCityCode', l.ibge_city_code, 'country', a.country) END,
|
|
5969
|
+
CASE WHEN nullif(l.address, '') IS NOT NULL THEN jsonb_build_object(
|
|
5970
|
+
'street', l.address, 'number', l.address_number, 'complement', l.address_complement,
|
|
5971
|
+
'district', l.district, 'city', l.city, 'state', l.state, 'postalCode', l.postal_code,
|
|
5972
|
+
'ibgeCityCode', l.ibge_city_code, 'country', l.country) END,
|
|
5973
|
+
CASE WHEN ua.id IS NOT NULL THEN jsonb_build_object(
|
|
5974
|
+
'street', ua.street, 'number', ua.number, 'complement', ua.complement,
|
|
5975
|
+
'district', ua.district, 'city', ua.city, 'state', ua.state, 'postalCode', ua.postal_code,
|
|
5976
|
+
'ibgeCityCode', l.ibge_city_code, 'country', ua.country) END,
|
|
5977
|
+
jsonb_build_object(
|
|
5978
|
+
'street', l.address, 'number', l.address_number, 'complement', l.address_complement,
|
|
5979
|
+
'district', l.district, 'city', l.city, 'state', l.state, 'postalCode', l.postal_code,
|
|
5980
|
+
'ibgeCityCode', l.ibge_city_code, 'country', l.country)
|
|
5981
|
+
),
|
|
5982
|
+
'settings', CASE WHEN s.id IS NULL THEN NULL ELSE jsonb_build_object(
|
|
5983
|
+
'nfe', jsonb_build_object('enabled', s.nfe_enabled, 'production', s.nfe_production, 'series', s.nfe_series, 'nextNumber', s.nfe_next_number),
|
|
5984
|
+
'nfce', jsonb_build_object('enabled', s.nfce_enabled, 'production', s.nfce_production, 'series', s.nfce_series, 'nextNumber', s.nfce_next_number,
|
|
5985
|
+
'cscId', s.nfce_csc_id, 'cscToken', s.nfce_csc_token, 'genericItem', s.nfce_generic_item),
|
|
5986
|
+
-- The NFS-e config, under the keys the integration service reads (door.ts).
|
|
5987
|
+
'nfse', jsonb_build_object(
|
|
5988
|
+
'enabled', s.nfse_enabled, 'production', s.nfse_production, 'nacional', s.nfse_national,
|
|
5989
|
+
'nextNumber', s.nfse_next_number,
|
|
5990
|
+
'codigoTributacaoNacional', s.nfse_service_code_national,
|
|
5991
|
+
'codigoTributacaoMunicipio', s.nfse_service_code_municipal,
|
|
5992
|
+
'codigoNbs', s.nfse_nbs_code,
|
|
5993
|
+
'cnae', s.nfse_cnae,
|
|
5994
|
+
'issAliquota', s.nfse_iss_rate,
|
|
5995
|
+
'issTipoTributacao', s.nfse_iss_taxation,
|
|
5996
|
+
'issExigibilidade', s.nfse_iss_exigibility,
|
|
5997
|
+
'ibsCbsCst', s.nfse_ibscbs_cst,
|
|
5998
|
+
'ibsCbsClassificacao', s.nfse_ibscbs_classification,
|
|
5999
|
+
'regimeApuracaoSn', s.nfse_simples_regime,
|
|
6000
|
+
'valorLiquido', s.nfse_net_value,
|
|
6001
|
+
'informacoesComplementares', s.nfse_additional_info),
|
|
6002
|
+
'certificate', jsonb_build_object('providerId', s.certificate_provider_id, 'holder', s.certificate_cn,
|
|
6003
|
+
'expiresAt', s.certificate_expires_at, 'uploadedAt', s.certificate_uploaded_at),
|
|
6004
|
+
'providerSyncedAt', s.provider_synced_at,
|
|
6005
|
+
'providerWebhookAt', s.provider_webhook_at
|
|
6006
|
+
) END
|
|
6007
|
+
)
|
|
6008
|
+
FROM public.company_legal_entities l
|
|
6009
|
+
LEFT JOIN public.plg_fiscalbr_emitter_settings s ON s.legal_entity_id = l.id AND s.tenant_id = l.tenant_id
|
|
6010
|
+
LEFT JOIN LATERAL (
|
|
6011
|
+
SELECT * FROM public.addresses x
|
|
6012
|
+
WHERE x.tenant_id = l.tenant_id AND x.owner_type = 'company_legal_entity' AND x.owner_id = l.id
|
|
6013
|
+
ORDER BY (x.kind = 'fiscal') DESC, x.is_default DESC, x.created_at
|
|
6014
|
+
LIMIT 1
|
|
6015
|
+
) a ON true
|
|
6016
|
+
LEFT JOIN LATERAL (
|
|
6017
|
+
SELECT x.* FROM public.addresses x
|
|
6018
|
+
JOIN app.units u2 ON u2.id = x.owner_id AND u2.company_legal_entity_id = l.id AND u2.tenant_id = l.tenant_id
|
|
6019
|
+
WHERE x.tenant_id = l.tenant_id AND x.owner_type = 'unit' AND nullif(x.street, '') IS NOT NULL
|
|
6020
|
+
ORDER BY (x.kind = 'fiscal') DESC, x.is_default DESC, x.created_at
|
|
6021
|
+
LIMIT 1
|
|
6022
|
+
) ua ON true
|
|
6023
|
+
WHERE l.id = p_legal_entity AND l.tenant_id = p_tenant
|
|
6024
|
+
$function$;
|
|
6025
|
+
`;
|
|
5632
6026
|
var MIGRATIONS = [
|
|
5633
6027
|
{ id: "000_baseline", sql: MIGRATION_000_BASELINE },
|
|
5634
6028
|
{ id: "001_preserve_terminal_results_and_recover_leases", sql: MIGRATION_001_PRESERVE_TERMINAL_RESULTS_AND_RECOVER_LEASES },
|
|
@@ -5664,9 +6058,13 @@ var MIGRATIONS = [
|
|
|
5664
6058
|
{ id: "031_the_service_issues_the_note", sql: MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE },
|
|
5665
6059
|
{ id: "032_a_delivered_note_carries_where_it_goes", sql: MIGRATION_032_A_DELIVERED_NOTE_CARRIES_WHERE_IT_GOES },
|
|
5666
6060
|
{ id: "033_a_company_buyer_gets_its_nfe", sql: MIGRATION_033_A_COMPANY_BUYER_GETS_ITS_NFE },
|
|
5667
|
-
{ id: "034_only_what_arrived_enters_stock", sql: MIGRATION_034_ONLY_WHAT_ARRIVED_ENTERS_STOCK }
|
|
6061
|
+
{ id: "034_only_what_arrived_enters_stock", sql: MIGRATION_034_ONLY_WHAT_ARRIVED_ENTERS_STOCK },
|
|
6062
|
+
{ id: "035_the_emitter_borrows_the_unit_address", sql: MIGRATION_035_THE_EMITTER_BORROWS_THE_UNIT_ADDRESS },
|
|
6063
|
+
{ id: "036_the_fiscal_group_names_the_service_on_the_note", sql: MIGRATION_036_THE_FISCAL_GROUP_NAMES_THE_SERVICE_ON_THE_NOTE },
|
|
6064
|
+
{ id: "037_the_service_issues_the_receivable_note", sql: MIGRATION_037_THE_SERVICE_ISSUES_THE_RECEIVABLE_NOTE },
|
|
6065
|
+
{ id: "038_the_emitter_tells_its_nfse_config", sql: MIGRATION_038_THE_EMITTER_TELLS_ITS_NFSE_CONFIG }
|
|
5668
6066
|
];
|
|
5669
6067
|
|
|
5670
|
-
export { MIGRATIONS, MIGRATION_000_BASELINE, MIGRATION_001_PRESERVE_TERMINAL_RESULTS_AND_RECOVER_LEASES, MIGRATION_002_A_NOTA_QUE_CHEGA_TEM_ONDE_ESPERAR, MIGRATION_003_A_CAIXA_DE_ENTRADA_ATRAVESSA, MIGRATION_004_O_GRUPO_FISCAL_DO_V1_REPETE_NOME, MIGRATION_005_A_NOTA_EMITIDA_E_UM_DOCUMENTO, MIGRATION_006_O_GRUPO_FISCAL_GANHA_O_RAIL, MIGRATION_007_A_RLS_DO_DOCUMENTO_PASSA_A_SER_LEGIVEL, MIGRATION_008_A_NOTA_RECEBIDA_GUARDA_A_DECISAO_DE_ENTRADA, MIGRATION_009_ACEITAR_A_NOTA_ESCREVE_ESTOQUE_E_TITULO_DE_UMA_VEZ, MIGRATION_010_A_DATA_DE_EMISSAO_E_INSTANTE_E_A_COMPETENCIA_E_DIA, MIGRATION_011_O_IMPOSTO_DO_PRODUTO_E_DO_FISCAL, MIGRATION_012_QUEM_E_O_PRODUTO_SE_PERGUNTA_AO_INDICE, MIGRATION_013_A_SUGESTAO_SOBREVIVE_A_FECHAR_A_JANELA, MIGRATION_014_A_DESCRICAO_DA_NOTA_TEM_LIXO_NO_MEIO, MIGRATION_015_O_MOVIMENTO_DIZ_QUE_VEIO_DA_NOTA, MIGRATION_016_A_NOTA_GUARDA_O_PROPRIO_XML, MIGRATION_017_QUEM_EMITE_E_O_CNPJ_NAO_A_LOJA, MIGRATION_018_A_NOTA_DO_V1_ALCANCA_A_CONTA, MIGRATION_019_A_NOTA_EMITIDA_SE_CANCELA_PELO_V2, MIGRATION_020_A_NOTA_ACHA_O_FORNECEDOR_PELO_CNPJ, MIGRATION_021_USO_E_CONSUMO_ENTRA_NO_ESTOQUE, MIGRATION_022_A_NOTA_DO_V1_TRAZ_O_XML, MIGRATION_023_A_NOTA_ACEITA_SEM_LINHAS_RECEBE_AS_LINHAS, MIGRATION_024_A_NOTA_NAO_APONTA_PARA_CONTA_QUE_SUMIU, MIGRATION_025_A_CONTA_DA_NOTA_FECHA_COM_O_TOTAL, MIGRATION_026_O_RECEBIMENTO_PARADO_DIZ_POR_QUE, MIGRATION_027_A_NOTA_COM_CONTA_E_SEM_ESTOQUE_TERMINA_NO_V2, MIGRATION_028_A_LINHA_DE_ESTOQUE_TEM_UNIDADE, MIGRATION_029_THE_SERVICE_WRITES_THE_INTAKE_HEALTH, MIGRATION_030_THE_SERVICE_ONBOARDS_THE_EMITTER, MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE, MIGRATION_032_A_DELIVERED_NOTE_CARRIES_WHERE_IT_GOES, MIGRATION_033_A_COMPANY_BUYER_GETS_ITS_NFE, MIGRATION_034_ONLY_WHAT_ARRIVED_ENTERS_STOCK };
|
|
6068
|
+
export { MIGRATIONS, MIGRATION_000_BASELINE, MIGRATION_001_PRESERVE_TERMINAL_RESULTS_AND_RECOVER_LEASES, MIGRATION_002_A_NOTA_QUE_CHEGA_TEM_ONDE_ESPERAR, MIGRATION_003_A_CAIXA_DE_ENTRADA_ATRAVESSA, MIGRATION_004_O_GRUPO_FISCAL_DO_V1_REPETE_NOME, MIGRATION_005_A_NOTA_EMITIDA_E_UM_DOCUMENTO, MIGRATION_006_O_GRUPO_FISCAL_GANHA_O_RAIL, MIGRATION_007_A_RLS_DO_DOCUMENTO_PASSA_A_SER_LEGIVEL, MIGRATION_008_A_NOTA_RECEBIDA_GUARDA_A_DECISAO_DE_ENTRADA, MIGRATION_009_ACEITAR_A_NOTA_ESCREVE_ESTOQUE_E_TITULO_DE_UMA_VEZ, MIGRATION_010_A_DATA_DE_EMISSAO_E_INSTANTE_E_A_COMPETENCIA_E_DIA, MIGRATION_011_O_IMPOSTO_DO_PRODUTO_E_DO_FISCAL, MIGRATION_012_QUEM_E_O_PRODUTO_SE_PERGUNTA_AO_INDICE, MIGRATION_013_A_SUGESTAO_SOBREVIVE_A_FECHAR_A_JANELA, MIGRATION_014_A_DESCRICAO_DA_NOTA_TEM_LIXO_NO_MEIO, MIGRATION_015_O_MOVIMENTO_DIZ_QUE_VEIO_DA_NOTA, MIGRATION_016_A_NOTA_GUARDA_O_PROPRIO_XML, MIGRATION_017_QUEM_EMITE_E_O_CNPJ_NAO_A_LOJA, MIGRATION_018_A_NOTA_DO_V1_ALCANCA_A_CONTA, MIGRATION_019_A_NOTA_EMITIDA_SE_CANCELA_PELO_V2, MIGRATION_020_A_NOTA_ACHA_O_FORNECEDOR_PELO_CNPJ, MIGRATION_021_USO_E_CONSUMO_ENTRA_NO_ESTOQUE, MIGRATION_022_A_NOTA_DO_V1_TRAZ_O_XML, MIGRATION_023_A_NOTA_ACEITA_SEM_LINHAS_RECEBE_AS_LINHAS, MIGRATION_024_A_NOTA_NAO_APONTA_PARA_CONTA_QUE_SUMIU, MIGRATION_025_A_CONTA_DA_NOTA_FECHA_COM_O_TOTAL, MIGRATION_026_O_RECEBIMENTO_PARADO_DIZ_POR_QUE, MIGRATION_027_A_NOTA_COM_CONTA_E_SEM_ESTOQUE_TERMINA_NO_V2, MIGRATION_028_A_LINHA_DE_ESTOQUE_TEM_UNIDADE, MIGRATION_029_THE_SERVICE_WRITES_THE_INTAKE_HEALTH, MIGRATION_030_THE_SERVICE_ONBOARDS_THE_EMITTER, MIGRATION_031_THE_SERVICE_ISSUES_THE_NOTE, MIGRATION_032_A_DELIVERED_NOTE_CARRIES_WHERE_IT_GOES, MIGRATION_033_A_COMPANY_BUYER_GETS_ITS_NFE, MIGRATION_034_ONLY_WHAT_ARRIVED_ENTERS_STOCK, MIGRATION_035_THE_EMITTER_BORROWS_THE_UNIT_ADDRESS, MIGRATION_036_THE_FISCAL_GROUP_NAMES_THE_SERVICE_ON_THE_NOTE, MIGRATION_037_THE_SERVICE_ISSUES_THE_RECEIVABLE_NOTE, MIGRATION_038_THE_EMITTER_TELLS_ITS_NFSE_CONFIG };
|
|
5671
6069
|
//# sourceMappingURL=main.js.map
|
|
5672
6070
|
//# sourceMappingURL=main.js.map
|