@fayz-ai/plugin-fiscal-br 0.16.5 → 0.17.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/{InboundNotesPage-IEPEEOY2.js → InboundNotesPage-MT4FL7JO.js} +4 -4
- package/dist/{InboundNotesPage-IEPEEOY2.js.map → InboundNotesPage-MT4FL7JO.js.map} +1 -1
- package/dist/NoteReconcileView-XRJGCGTD.js +6 -0
- package/dist/{NoteReconcileView-3MFYJZME.js.map → NoteReconcileView-XRJGCGTD.js.map} +1 -1
- package/dist/{chunk-VMZN3UYW.js → chunk-J6LXGXCA.js} +3 -3
- package/dist/{chunk-VMZN3UYW.js.map → chunk-J6LXGXCA.js.map} +1 -1
- package/dist/{chunk-GXI2FGDQ.js → chunk-X7X57O7P.js} +158 -37
- package/dist/chunk-X7X57O7P.js.map +1 -0
- package/dist/data/inbox.d.ts +18 -0
- package/dist/data/inbox.d.ts.map +1 -1
- package/dist/index.js +52 -8
- package/dist/index.js.map +1 -1
- package/dist/locales/en.d.ts.map +1 -1
- package/dist/locales/pt-BR.d.ts.map +1 -1
- package/dist/migrations/index.d.ts +1 -0
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/main.browser.js +2 -1
- package/dist/migrations/main.browser.js.map +1 -1
- package/dist/migrations/main.js +568 -2
- package/dist/migrations/main.js.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/views/NoteReconcileView.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/NoteReconcileView-3MFYJZME.js +0 -6
- package/dist/chunk-GXI2FGDQ.js.map +0 -1
package/dist/migrations/main.js
CHANGED
|
@@ -6023,6 +6023,571 @@ AS $function$
|
|
|
6023
6023
|
WHERE l.id = p_legal_entity AND l.tenant_id = p_tenant
|
|
6024
6024
|
$function$;
|
|
6025
6025
|
`;
|
|
6026
|
+
var MIGRATION_039_EACH_LINE_ENTERS_ITS_OWN_LOCATION = `-- ============================================================================
|
|
6027
|
+
-- 039 \u2014 each line of a received note enters stock where it belongs
|
|
6028
|
+
--
|
|
6029
|
+
-- Accepting a note put every line into the ONE location chosen on the note:
|
|
6030
|
+
-- the milk and the cleaning products of the same supplier landed on the same
|
|
6031
|
+
-- shelf, and the product's own \xABLocal padr\xE3o\xBB was never asked.
|
|
6032
|
+
--
|
|
6033
|
+
-- V1 contract (beautyplace src/components/inventory/DanfeStockEntryModal.tsx,
|
|
6034
|
+
-- danfeStockEntry.ts): every line has its OWN stock location, pre-filled with
|
|
6035
|
+
-- the product's default location when that location is valid and active,
|
|
6036
|
+
-- otherwise blank so the person picks one, editable before confirming; batch
|
|
6037
|
+
-- and expiry per line as the product is configured (require_batch_for_stock /
|
|
6038
|
+
-- require_expiry_for_stock block the entry by name).
|
|
6039
|
+
--
|
|
6040
|
+
-- This file, additive and replay-safe:
|
|
6041
|
+
--
|
|
6042
|
+
-- 1. three columns on the note line: stock_location_id, batch_number,
|
|
6043
|
+
-- expiration_date;
|
|
6044
|
+
-- 2. plg_fiscalbr_check_stock_location \u2014 the location exists in the tenant,
|
|
6045
|
+
-- is active, and belongs to the note's unit (inventory_receive then checks
|
|
6046
|
+
-- the caller's access and permission there, as before);
|
|
6047
|
+
-- 3. fiscalbr_product_stock_defaults \u2014 the product's default location for
|
|
6048
|
+
-- the note's unit (per-unit setting, then tenant-wide setting, then the
|
|
6049
|
+
-- product's own, as inventory/017 resolves it), only when it is valid,
|
|
6050
|
+
-- plus whether batch and expiry are required: what the screen pre-fills;
|
|
6051
|
+
-- 4. fiscalbr_save_inbox_decision (034's body) writes the three columns when
|
|
6052
|
+
-- the payload names them, validating the location;
|
|
6053
|
+
-- 5. fiscalbr_accept_inbox_document (034's body) receives each line into its
|
|
6054
|
+
-- own location \u2014 one inventory_receive per location \u2014 refuses a line with
|
|
6055
|
+
-- no location (none on the line and none on the note), an invalid one, or
|
|
6056
|
+
-- a missing required batch/expiry. A note saved by an older app (only the
|
|
6057
|
+
-- note-wide location) still enters exactly as before, under the same
|
|
6058
|
+
-- idempotency key.
|
|
6059
|
+
-- ============================================================================
|
|
6060
|
+
|
|
6061
|
+
-- \u2500\u2500 1. Where the line enters \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
6062
|
+
ALTER TABLE public.plg_fiscalbr_inbox_items
|
|
6063
|
+
ADD COLUMN IF NOT EXISTS stock_location_id uuid,
|
|
6064
|
+
ADD COLUMN IF NOT EXISTS batch_number text,
|
|
6065
|
+
ADD COLUMN IF NOT EXISTS expiration_date date;
|
|
6066
|
+
|
|
6067
|
+
COMMENT ON COLUMN public.plg_fiscalbr_inbox_items.stock_location_id IS
|
|
6068
|
+
'Where this line enters stock on accept. NULL falls back to the note''s default_location_id (apps before 039).';
|
|
6069
|
+
|
|
6070
|
+
-- \u2500\u2500 2. A location this note may enter stock into \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
6071
|
+
CREATE OR REPLACE FUNCTION public.plg_fiscalbr_check_stock_location(
|
|
6072
|
+
p_tenant uuid, p_location uuid, p_unit uuid, p_item integer)
|
|
6073
|
+
RETURNS void LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path TO '' AS $function$
|
|
6074
|
+
DECLARE v_loc record;
|
|
6075
|
+
BEGIN
|
|
6076
|
+
SELECT id, unit_id, is_active INTO v_loc FROM public.plg_inventory_stock_locations
|
|
6077
|
+
WHERE id = p_location AND tenant_id = p_tenant;
|
|
6078
|
+
IF v_loc.id IS NULL THEN
|
|
6079
|
+
RAISE EXCEPTION 'fiscalbr: item %: stock location % does not exist', coalesce(p_item::text, '\u2014'), p_location
|
|
6080
|
+
USING ERRCODE = '22023', HINT = 'STOCK_LOCATION_UNKNOWN';
|
|
6081
|
+
END IF;
|
|
6082
|
+
IF NOT v_loc.is_active THEN
|
|
6083
|
+
RAISE EXCEPTION 'fiscalbr: item %: stock location % is inactive', coalesce(p_item::text, '\u2014'), p_location
|
|
6084
|
+
USING ERRCODE = '22023', HINT = 'STOCK_LOCATION_INACTIVE';
|
|
6085
|
+
END IF;
|
|
6086
|
+
IF p_unit IS NOT NULL AND v_loc.unit_id IS DISTINCT FROM p_unit THEN
|
|
6087
|
+
RAISE EXCEPTION 'fiscalbr: item %: stock location % belongs to another unit', coalesce(p_item::text, '\u2014'), p_location
|
|
6088
|
+
USING ERRCODE = '22023', HINT = 'STOCK_LOCATION_OTHER_UNIT';
|
|
6089
|
+
END IF;
|
|
6090
|
+
END $function$;
|
|
6091
|
+
|
|
6092
|
+
REVOKE ALL ON FUNCTION public.plg_fiscalbr_check_stock_location(uuid, uuid, uuid, integer) FROM PUBLIC, anon, authenticated;
|
|
6093
|
+
|
|
6094
|
+
-- \u2500\u2500 3. What the screen pre-fills \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
6095
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_product_stock_defaults(p_document_id uuid, p_product_ids uuid[])
|
|
6096
|
+
RETURNS TABLE (product_id uuid, location_id uuid, require_batch boolean, require_expiry boolean)
|
|
6097
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path TO '' AS $function$
|
|
6098
|
+
DECLARE
|
|
6099
|
+
v_tenant uuid := public.plg_financial_require_tenant();
|
|
6100
|
+
v_unit uuid;
|
|
6101
|
+
BEGIN
|
|
6102
|
+
SELECT d.unit_id INTO v_unit FROM public.plg_fiscalbr_inbox_documents d
|
|
6103
|
+
WHERE d.tenant_id = v_tenant AND d.id = p_document_id;
|
|
6104
|
+
IF NOT FOUND THEN
|
|
6105
|
+
RAISE EXCEPTION 'fiscalbr: note % not found in this tenant', p_document_id USING ERRCODE = 'P0002';
|
|
6106
|
+
END IF;
|
|
6107
|
+
PERFORM public.plg_financial_require_permission('financial.read', v_unit);
|
|
6108
|
+
|
|
6109
|
+
RETURN QUERY
|
|
6110
|
+
SELECT p.id,
|
|
6111
|
+
(SELECT c.location_id
|
|
6112
|
+
FROM (
|
|
6113
|
+
SELECT s.default_location_id AS location_id,
|
|
6114
|
+
CASE WHEN s.unit_id IS NOT NULL THEN 1 ELSE 2 END AS priority
|
|
6115
|
+
FROM public.plg_inventory_product_settings s
|
|
6116
|
+
WHERE s.tenant_id = v_tenant AND s.product_id = p.id AND s.default_location_id IS NOT NULL
|
|
6117
|
+
AND (s.unit_id IS NULL OR s.unit_id = v_unit)
|
|
6118
|
+
UNION ALL
|
|
6119
|
+
SELECT dd.default_location_id, 3
|
|
6120
|
+
FROM public.plg_inventory_product_details dd
|
|
6121
|
+
WHERE dd.tenant_id = v_tenant AND dd.product_id = p.id AND dd.default_location_id IS NOT NULL
|
|
6122
|
+
) c
|
|
6123
|
+
JOIN public.plg_inventory_stock_locations l
|
|
6124
|
+
ON l.id = c.location_id AND l.tenant_id = v_tenant AND l.is_active
|
|
6125
|
+
AND (v_unit IS NULL OR l.unit_id = v_unit)
|
|
6126
|
+
ORDER BY c.priority
|
|
6127
|
+
LIMIT 1),
|
|
6128
|
+
coalesce(det.require_batch_for_stock, false),
|
|
6129
|
+
coalesce(det.require_expiry_for_stock, false)
|
|
6130
|
+
FROM unnest(coalesce(p_product_ids, '{}'::uuid[])) AS p(id)
|
|
6131
|
+
LEFT JOIN public.plg_inventory_product_details det ON det.tenant_id = v_tenant AND det.product_id = p.id;
|
|
6132
|
+
END $function$;
|
|
6133
|
+
|
|
6134
|
+
REVOKE ALL ON FUNCTION public.fiscalbr_product_stock_defaults(uuid, uuid[]) FROM PUBLIC, anon;
|
|
6135
|
+
GRANT EXECUTE ON FUNCTION public.fiscalbr_product_stock_defaults(uuid, uuid[]) TO authenticated;
|
|
6136
|
+
|
|
6137
|
+
-- \u2500\u2500 4. Saving the decision carries where each line enters \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
6138
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_save_inbox_decision(
|
|
6139
|
+
p_document_id uuid,
|
|
6140
|
+
p_items jsonb,
|
|
6141
|
+
p_default_chart_account_id uuid DEFAULT NULL,
|
|
6142
|
+
p_default_location_id uuid DEFAULT NULL,
|
|
6143
|
+
p_duplicates jsonb DEFAULT NULL)
|
|
6144
|
+
RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path TO '' AS $function$
|
|
6145
|
+
DECLARE
|
|
6146
|
+
v_tenant uuid := public.plg_financial_require_tenant();
|
|
6147
|
+
v_doc public.plg_fiscalbr_inbox_documents%ROWTYPE;
|
|
6148
|
+
v_row jsonb;
|
|
6149
|
+
v_done integer := 0;
|
|
6150
|
+
v_total integer;
|
|
6151
|
+
v_line public.plg_fiscalbr_inbox_items%ROWTYPE;
|
|
6152
|
+
v_missing numeric;
|
|
6153
|
+
v_reason text;
|
|
6154
|
+
v_sloc uuid;
|
|
6155
|
+
v_batch text;
|
|
6156
|
+
v_expiry date;
|
|
6157
|
+
BEGIN
|
|
6158
|
+
SELECT * INTO v_doc FROM public.plg_fiscalbr_inbox_documents
|
|
6159
|
+
WHERE tenant_id = v_tenant AND id = p_document_id;
|
|
6160
|
+
IF NOT FOUND THEN
|
|
6161
|
+
RAISE EXCEPTION 'fiscalbr: nota % n\xE3o encontrada neste tenant', p_document_id USING ERRCODE = 'P0002';
|
|
6162
|
+
END IF;
|
|
6163
|
+
PERFORM public.plg_financial_require_permission('financial.edit', v_doc.unit_id);
|
|
6164
|
+
|
|
6165
|
+
-- Settled = stocked, or a payable with no stock line left to receive (027).
|
|
6166
|
+
IF v_doc.stocked_at IS NOT NULL OR (v_doc.invoice_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM public.plg_fiscalbr_inbox_items x
|
|
6167
|
+
WHERE x.tenant_id = v_tenant AND x.inbox_document_id = v_doc.id
|
|
6168
|
+
AND x.stock_movement_id IS NULL
|
|
6169
|
+
AND (x.entry_type IS NULL OR x.entry_type = '' OR x.entry_type IN ('insumo','revenda','patrimonio','uso_consumo')))) THEN
|
|
6170
|
+
RAISE EXCEPTION 'fiscalbr: nota % j\xE1 foi aceita \u2014 a decis\xE3o n\xE3o muda depois do estoque', p_document_id
|
|
6171
|
+
USING ERRCODE = '23514';
|
|
6172
|
+
END IF;
|
|
6173
|
+
|
|
6174
|
+
-- The note-wide location is only the \xABapply to all\xBB shortcut now, but it is
|
|
6175
|
+
-- still a location this note may enter stock into.
|
|
6176
|
+
IF p_default_location_id IS NOT NULL THEN
|
|
6177
|
+
PERFORM public.plg_fiscalbr_check_stock_location(v_tenant, p_default_location_id, v_doc.unit_id, NULL);
|
|
6178
|
+
END IF;
|
|
6179
|
+
|
|
6180
|
+
UPDATE public.plg_fiscalbr_inbox_documents SET
|
|
6181
|
+
default_chart_account_id = coalesce(p_default_chart_account_id, default_chart_account_id),
|
|
6182
|
+
default_location_id = coalesce(p_default_location_id, default_location_id),
|
|
6183
|
+
reviewed_at = now(), updated_at = now()
|
|
6184
|
+
WHERE id = v_doc.id;
|
|
6185
|
+
|
|
6186
|
+
FOR v_row IN SELECT * FROM jsonb_array_elements(coalesce(p_items, '[]'::jsonb)) LOOP
|
|
6187
|
+
SELECT * INTO v_line FROM public.plg_fiscalbr_inbox_items
|
|
6188
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id AND id = (v_row->>'id')::uuid;
|
|
6189
|
+
CONTINUE WHEN NOT FOUND;
|
|
6190
|
+
|
|
6191
|
+
-- The receipt: only a payload that names \`missing_quantity\` (note units)
|
|
6192
|
+
-- writes it. Without the key it stays as it is.
|
|
6193
|
+
v_missing := CASE WHEN v_line.shortage_quantity > 0
|
|
6194
|
+
THEN least(v_line.shortage_quantity, v_line.quantity_commercial) END;
|
|
6195
|
+
v_reason := CASE WHEN v_missing IS NOT NULL THEN coalesce(v_line.shortage_reason, 'faltou') END;
|
|
6196
|
+
IF v_row ? 'missing_quantity' THEN
|
|
6197
|
+
BEGIN
|
|
6198
|
+
v_missing := nullif(v_row->>'missing_quantity', '')::numeric;
|
|
6199
|
+
EXCEPTION WHEN OTHERS THEN
|
|
6200
|
+
RAISE EXCEPTION 'fiscalbr: item %: missing quantity is not a number', v_line.item_number
|
|
6201
|
+
USING ERRCODE = '22023', HINT = 'RECEIPT_BAD_QUANTITY';
|
|
6202
|
+
END;
|
|
6203
|
+
IF v_missing IS NOT NULL AND v_missing <= 0 THEN v_missing := NULL; END IF;
|
|
6204
|
+
IF v_missing IS NOT NULL AND v_missing > coalesce(v_line.quantity_commercial, 0) THEN
|
|
6205
|
+
RAISE EXCEPTION 'fiscalbr: item %: % did not arrive, but the note bills only %',
|
|
6206
|
+
v_line.item_number, v_missing, v_line.quantity_commercial
|
|
6207
|
+
USING ERRCODE = '22023', HINT = 'RECEIPT_MORE_THAN_BILLED';
|
|
6208
|
+
END IF;
|
|
6209
|
+
v_reason := CASE WHEN v_missing IS NULL THEN NULL
|
|
6210
|
+
ELSE coalesce(nullif(v_row->>'shortage_reason', ''), 'faltou') END;
|
|
6211
|
+
IF v_reason IS NOT NULL AND v_reason NOT IN ('faltou', 'avariado', 'vencido', 'recusado', 'outro') THEN
|
|
6212
|
+
RAISE EXCEPTION 'fiscalbr: item %: unknown reason %', v_line.item_number, v_reason
|
|
6213
|
+
USING ERRCODE = '22023', HINT = 'RECEIPT_BAD_REASON';
|
|
6214
|
+
END IF;
|
|
6215
|
+
END IF;
|
|
6216
|
+
|
|
6217
|
+
-- Where this line enters stock, and with which batch and expiry (V1:
|
|
6218
|
+
-- one location per line). Only a payload that names the key writes it,
|
|
6219
|
+
-- so an app on an older plugin keeps what is saved.
|
|
6220
|
+
v_sloc := v_line.stock_location_id;
|
|
6221
|
+
v_batch := v_line.batch_number;
|
|
6222
|
+
v_expiry := v_line.expiration_date;
|
|
6223
|
+
IF v_row ? 'stock_location_id' THEN
|
|
6224
|
+
BEGIN
|
|
6225
|
+
v_sloc := nullif(v_row->>'stock_location_id', '')::uuid;
|
|
6226
|
+
EXCEPTION WHEN OTHERS THEN
|
|
6227
|
+
RAISE EXCEPTION 'fiscalbr: item %: % is not a stock location', v_line.item_number, v_row->>'stock_location_id'
|
|
6228
|
+
USING ERRCODE = '22023', HINT = 'STOCK_LOCATION_UNKNOWN';
|
|
6229
|
+
END;
|
|
6230
|
+
IF v_sloc IS NOT NULL THEN
|
|
6231
|
+
PERFORM public.plg_fiscalbr_check_stock_location(v_tenant, v_sloc, v_doc.unit_id, v_line.item_number);
|
|
6232
|
+
END IF;
|
|
6233
|
+
END IF;
|
|
6234
|
+
IF v_row ? 'batch_number' THEN
|
|
6235
|
+
v_batch := nullif(btrim(coalesce(v_row->>'batch_number', '')), '');
|
|
6236
|
+
END IF;
|
|
6237
|
+
IF v_row ? 'expiration_date' THEN
|
|
6238
|
+
BEGIN
|
|
6239
|
+
v_expiry := nullif(v_row->>'expiration_date', '')::date;
|
|
6240
|
+
EXCEPTION WHEN OTHERS THEN
|
|
6241
|
+
RAISE EXCEPTION 'fiscalbr: item %: expiry % is not a date', v_line.item_number, v_row->>'expiration_date'
|
|
6242
|
+
USING ERRCODE = '22023', HINT = 'EXPIRY_BAD_DATE';
|
|
6243
|
+
END;
|
|
6244
|
+
END IF;
|
|
6245
|
+
|
|
6246
|
+
UPDATE public.plg_fiscalbr_inbox_items i SET
|
|
6247
|
+
stock_location_id = v_sloc,
|
|
6248
|
+
batch_number = v_batch,
|
|
6249
|
+
expiration_date = v_expiry,
|
|
6250
|
+
product_id = nullif(v_row->>'product_id', '')::uuid,
|
|
6251
|
+
entry_type = nullif(v_row->>'entry_type', ''),
|
|
6252
|
+
chart_account_id = nullif(v_row->>'chart_account_id', '')::uuid,
|
|
6253
|
+
stock_unit_id = nullif(v_row->>'stock_unit_id', '')::uuid,
|
|
6254
|
+
conversion_factor = coalesce(nullif(v_row->>'conversion_factor', '')::numeric, 1),
|
|
6255
|
+
cost_mode = coalesce(nullif(v_row->>'cost_mode', ''), 'novo'),
|
|
6256
|
+
shortage_quantity = v_missing,
|
|
6257
|
+
shortage_reason = v_reason,
|
|
6258
|
+
shortage_note = CASE WHEN v_missing IS NULL THEN NULL
|
|
6259
|
+
WHEN v_row ? 'missing_quantity' THEN nullif(btrim(coalesce(v_row->>'shortage_note', '')), '')
|
|
6260
|
+
ELSE i.shortage_note END,
|
|
6261
|
+
-- Quem escreveu. Ausente na chave \xE9 NULL, que \xE9 \xABuma pessoa escolheu\xBB.
|
|
6262
|
+
suggested_by = nullif(v_row->>'suggested_by', ''),
|
|
6263
|
+
suggestion_confidence = nullif(v_row->>'suggestion_confidence', '')::numeric,
|
|
6264
|
+
-- What the note bills, in stock units. The receipt is applied on accept.
|
|
6265
|
+
stock_quantity = i.quantity_commercial * coalesce(nullif(v_row->>'conversion_factor', '')::numeric, 1),
|
|
6266
|
+
unit_cost_new = CASE WHEN coalesce(nullif(v_row->>'conversion_factor', '')::numeric, 1) > 0
|
|
6267
|
+
THEN i.unit_price_commercial / coalesce(nullif(v_row->>'conversion_factor', '')::numeric, 1) END,
|
|
6268
|
+
-- Uma linha est\xE1 conciliada quando sabe o que \xE9. Despesa e servi\xE7o n\xE3o
|
|
6269
|
+
-- pedem produto: elas nunca v\xE3o virar estoque.
|
|
6270
|
+
reconciled = CASE
|
|
6271
|
+
WHEN nullif(v_row->>'entry_type', '') IN ('despesa','servico') THEN true
|
|
6272
|
+
ELSE nullif(v_row->>'product_id', '') IS NOT NULL
|
|
6273
|
+
END,
|
|
6274
|
+
updated_at = now()
|
|
6275
|
+
WHERE i.tenant_id = v_tenant AND i.inbox_document_id = v_doc.id AND i.id = v_line.id;
|
|
6276
|
+
v_done := v_done + 1;
|
|
6277
|
+
END LOOP;
|
|
6278
|
+
|
|
6279
|
+
IF p_duplicates IS NOT NULL THEN
|
|
6280
|
+
DELETE FROM public.plg_fiscalbr_inbox_duplicates
|
|
6281
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id;
|
|
6282
|
+
INSERT INTO public.plg_fiscalbr_inbox_duplicates (
|
|
6283
|
+
tenant_id, inbox_document_id, duplicate_index, number, due_date, value,
|
|
6284
|
+
payment_type, barcode, pix_key, payment_method_id)
|
|
6285
|
+
SELECT v_tenant, v_doc.id, coalesce((d->>'index')::int, ord), d->>'number',
|
|
6286
|
+
nullif(d->>'due_date', '')::date, (d->>'value')::numeric,
|
|
6287
|
+
nullif(d->>'payment_type', ''), nullif(d->>'barcode', ''),
|
|
6288
|
+
nullif(d->>'pix_key', ''), nullif(d->>'payment_method_id', '')::uuid
|
|
6289
|
+
FROM jsonb_array_elements(p_duplicates) WITH ORDINALITY AS t(d, ord);
|
|
6290
|
+
END IF;
|
|
6291
|
+
|
|
6292
|
+
SELECT count(*) INTO v_total FROM public.plg_fiscalbr_inbox_items
|
|
6293
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id;
|
|
6294
|
+
|
|
6295
|
+
UPDATE public.plg_fiscalbr_inbox_documents SET status = CASE
|
|
6296
|
+
WHEN (SELECT count(*) FROM public.plg_fiscalbr_inbox_items
|
|
6297
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id AND reconciled) = v_total
|
|
6298
|
+
THEN 'accepted' ELSE 'partial' END
|
|
6299
|
+
WHERE id = v_doc.id;
|
|
6300
|
+
|
|
6301
|
+
RETURN jsonb_build_object('document_id', v_doc.id, 'saved', v_done, 'items', v_total,
|
|
6302
|
+
'reconciled', (SELECT count(*) FROM public.plg_fiscalbr_inbox_items
|
|
6303
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id AND reconciled));
|
|
6304
|
+
END $function$;
|
|
6305
|
+
|
|
6306
|
+
REVOKE ALL ON FUNCTION public.fiscalbr_save_inbox_decision(uuid, jsonb, uuid, uuid, jsonb) FROM PUBLIC;
|
|
6307
|
+
GRANT EXECUTE ON FUNCTION public.fiscalbr_save_inbox_decision(uuid, jsonb, uuid, uuid, jsonb) TO authenticated;
|
|
6308
|
+
|
|
6309
|
+
-- \u2500\u2500 5. Accepting receives each line where it belongs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
6310
|
+
CREATE OR REPLACE FUNCTION public.fiscalbr_accept_inbox_document(
|
|
6311
|
+
p_document_id uuid,
|
|
6312
|
+
p_location_id uuid DEFAULT NULL)
|
|
6313
|
+
RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER SET search_path TO '' AS $function$
|
|
6314
|
+
DECLARE
|
|
6315
|
+
v_tenant uuid := public.plg_financial_require_tenant();
|
|
6316
|
+
v_doc public.plg_fiscalbr_inbox_documents%ROWTYPE;
|
|
6317
|
+
v_loc uuid;
|
|
6318
|
+
v_lines jsonb := '[]'::jsonb;
|
|
6319
|
+
v_insts jsonb := '[]'::jsonb;
|
|
6320
|
+
v_recv jsonb;
|
|
6321
|
+
v_inv jsonb;
|
|
6322
|
+
v_it record;
|
|
6323
|
+
v_no integer := 0;
|
|
6324
|
+
v_bearing integer := 0;
|
|
6325
|
+
v_total numeric := 0;
|
|
6326
|
+
v_stock_n integer := 0;
|
|
6327
|
+
v_key text;
|
|
6328
|
+
v_invoice uuid;
|
|
6329
|
+
v_item_ids uuid[] := '{}';
|
|
6330
|
+
v_fraction numeric;
|
|
6331
|
+
v_qty numeric;
|
|
6332
|
+
v_short_n integer := 0;
|
|
6333
|
+
v_short_v numeric := 0;
|
|
6334
|
+
-- A compet\xEAncia: o DIA da emiss\xE3o, n\xE3o o instante.
|
|
6335
|
+
v_competence date;
|
|
6336
|
+
-- One receipt per stock location: the lines grouped by where they enter.
|
|
6337
|
+
v_line_loc uuid;
|
|
6338
|
+
v_k text;
|
|
6339
|
+
v_groups jsonb := '{}'::jsonb;
|
|
6340
|
+
v_gitems jsonb := '{}'::jsonb;
|
|
6341
|
+
v_nloc integer := 0;
|
|
6342
|
+
v_op_first uuid;
|
|
6343
|
+
v_movs jsonb := '[]'::jsonb;
|
|
6344
|
+
v_req_batch boolean;
|
|
6345
|
+
v_req_expiry boolean;
|
|
6346
|
+
BEGIN
|
|
6347
|
+
SELECT * INTO v_doc FROM public.plg_fiscalbr_inbox_documents
|
|
6348
|
+
WHERE tenant_id = v_tenant AND id = p_document_id;
|
|
6349
|
+
IF NOT FOUND THEN
|
|
6350
|
+
RAISE EXCEPTION 'fiscalbr: nota % n\xE3o encontrada neste tenant', p_document_id USING ERRCODE = 'P0002';
|
|
6351
|
+
END IF;
|
|
6352
|
+
PERFORM public.plg_financial_require_permission('financial.create', v_doc.unit_id);
|
|
6353
|
+
|
|
6354
|
+
-- Settled = stocked, or a payable with no stock line left to receive (027).
|
|
6355
|
+
IF v_doc.stocked_at IS NOT NULL OR (v_doc.invoice_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM public.plg_fiscalbr_inbox_items x
|
|
6356
|
+
WHERE x.tenant_id = v_tenant AND x.inbox_document_id = v_doc.id
|
|
6357
|
+
AND x.stock_movement_id IS NULL
|
|
6358
|
+
AND (x.entry_type IS NULL OR x.entry_type = '' OR x.entry_type IN ('insumo','revenda','patrimonio','uso_consumo')))) THEN
|
|
6359
|
+
RETURN jsonb_build_object('document_id', v_doc.id, 'already_accepted', true,
|
|
6360
|
+
'invoice_id', v_doc.invoice_id, 'operation_id', v_doc.inventory_operation_id);
|
|
6361
|
+
END IF;
|
|
6362
|
+
|
|
6363
|
+
v_loc := coalesce(p_location_id, v_doc.default_location_id);
|
|
6364
|
+
v_competence := (v_doc.issue_date)::date;
|
|
6365
|
+
|
|
6366
|
+
IF EXISTS (SELECT 1 FROM public.plg_fiscalbr_inbox_items
|
|
6367
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id
|
|
6368
|
+
AND (entry_type IS NULL OR entry_type = '')) THEN
|
|
6369
|
+
RAISE EXCEPTION 'fiscalbr: h\xE1 item sem tipo de registro \u2014 conciliar antes de aceitar'
|
|
6370
|
+
USING ERRCODE = '23514';
|
|
6371
|
+
END IF;
|
|
6372
|
+
|
|
6373
|
+
-- What did not arrive, on every line (stock-bearing or not): it is what the
|
|
6374
|
+
-- finance person will abate from the payable.
|
|
6375
|
+
SELECT count(*), coalesce(sum(round(coalesce(i.total_price, 0) * least(i.shortage_quantity, i.quantity_commercial)
|
|
6376
|
+
/ nullif(i.quantity_commercial, 0), 2)), 0)
|
|
6377
|
+
INTO v_short_n, v_short_v
|
|
6378
|
+
FROM public.plg_fiscalbr_inbox_items i
|
|
6379
|
+
WHERE i.tenant_id = v_tenant AND i.inbox_document_id = v_doc.id AND i.shortage_quantity > 0;
|
|
6380
|
+
|
|
6381
|
+
FOR v_it IN
|
|
6382
|
+
SELECT * FROM public.plg_fiscalbr_inbox_items
|
|
6383
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id
|
|
6384
|
+
AND entry_type IN ('insumo','revenda','patrimonio','uso_consumo')
|
|
6385
|
+
ORDER BY item_number
|
|
6386
|
+
LOOP
|
|
6387
|
+
v_bearing := v_bearing + 1;
|
|
6388
|
+
-- The received fraction: 1 = all of it, 0 = nothing came.
|
|
6389
|
+
v_fraction := CASE WHEN coalesce(v_it.quantity_commercial, 0) <= 0 THEN 1
|
|
6390
|
+
ELSE 1 - least(greatest(coalesce(v_it.shortage_quantity, 0), 0), v_it.quantity_commercial)
|
|
6391
|
+
/ v_it.quantity_commercial END;
|
|
6392
|
+
-- Nothing came: nothing enters, and there is nothing to name.
|
|
6393
|
+
CONTINUE WHEN v_fraction <= 0.000001;
|
|
6394
|
+
IF v_it.stock_unit_id IS NULL THEN
|
|
6395
|
+
RAISE EXCEPTION 'fiscalbr: item % entra no estoque sem unidade \u2014 escolha a unidade de estoque', v_it.item_number
|
|
6396
|
+
USING ERRCODE = '23514';
|
|
6397
|
+
END IF;
|
|
6398
|
+
IF v_it.product_id IS NULL THEN
|
|
6399
|
+
RAISE EXCEPTION 'fiscalbr: item % \xE9 % e n\xE3o tem produto vinculado', v_it.item_number, v_it.entry_type
|
|
6400
|
+
USING ERRCODE = '23514';
|
|
6401
|
+
END IF;
|
|
6402
|
+
v_qty := round(coalesce(v_it.stock_quantity, v_it.quantity_commercial * v_it.conversion_factor) * v_fraction, 4);
|
|
6403
|
+
CONTINUE WHEN v_qty <= 0;
|
|
6404
|
+
|
|
6405
|
+
-- The line's own location; the note-wide one only for a line saved by an
|
|
6406
|
+
-- app that did not send one. Validated here too: the save may be old.
|
|
6407
|
+
v_line_loc := coalesce(v_it.stock_location_id, v_loc);
|
|
6408
|
+
IF v_line_loc IS NULL THEN
|
|
6409
|
+
RAISE EXCEPTION 'fiscalbr: item % enters stock without a stock location \u2014 choose one', v_it.item_number
|
|
6410
|
+
USING ERRCODE = '23514', HINT = 'STOCK_LOCATION_MISSING';
|
|
6411
|
+
END IF;
|
|
6412
|
+
PERFORM public.plg_fiscalbr_check_stock_location(v_tenant, v_line_loc, v_doc.unit_id, v_it.item_number);
|
|
6413
|
+
|
|
6414
|
+
-- Batch and expiry as the product is configured (V1: blocked by name).
|
|
6415
|
+
SELECT d.require_batch_for_stock, d.require_expiry_for_stock INTO v_req_batch, v_req_expiry
|
|
6416
|
+
FROM public.plg_inventory_product_details d
|
|
6417
|
+
WHERE d.tenant_id = v_tenant AND d.product_id = v_it.product_id;
|
|
6418
|
+
IF coalesce(v_req_batch, false) AND v_it.batch_number IS NULL THEN
|
|
6419
|
+
RAISE EXCEPTION 'fiscalbr: item % needs a batch to enter stock', v_it.item_number
|
|
6420
|
+
USING ERRCODE = '23514', HINT = 'BATCH_REQUIRED';
|
|
6421
|
+
END IF;
|
|
6422
|
+
IF coalesce(v_req_expiry, false) AND v_it.expiration_date IS NULL THEN
|
|
6423
|
+
RAISE EXCEPTION 'fiscalbr: item % needs an expiry date to enter stock', v_it.item_number
|
|
6424
|
+
USING ERRCODE = '23514', HINT = 'EXPIRY_REQUIRED';
|
|
6425
|
+
END IF;
|
|
6426
|
+
|
|
6427
|
+
v_no := v_no + 1;
|
|
6428
|
+
v_k := v_line_loc::text;
|
|
6429
|
+
v_groups := jsonb_set(v_groups, ARRAY[v_k], coalesce(v_groups->v_k, '[]'::jsonb) || jsonb_build_array(
|
|
6430
|
+
jsonb_build_object(
|
|
6431
|
+
'product_id', v_it.product_id,
|
|
6432
|
+
'quantity', v_qty,
|
|
6433
|
+
'unit_cost', coalesce(v_it.unit_cost_new, v_it.unit_price_commercial / v_it.conversion_factor),
|
|
6434
|
+
'measurement_unit_id', v_it.stock_unit_id,
|
|
6435
|
+
'notes', v_it.description)
|
|
6436
|
+
|| jsonb_strip_nulls(jsonb_build_object('batch_number', v_it.batch_number, 'expiration_date', v_it.expiration_date))));
|
|
6437
|
+
v_gitems := jsonb_set(v_gitems, ARRAY[v_k], coalesce(v_gitems->v_k, '[]'::jsonb) || jsonb_build_array(v_it.id));
|
|
6438
|
+
END LOOP;
|
|
6439
|
+
v_stock_n := v_no;
|
|
6440
|
+
|
|
6441
|
+
IF v_stock_n > 0 THEN
|
|
6442
|
+
SELECT count(*) INTO v_nloc FROM jsonb_object_keys(v_groups);
|
|
6443
|
+
FOR v_k IN SELECT k FROM jsonb_object_keys(v_groups) AS t(k) ORDER BY k LOOP
|
|
6444
|
+
v_recv := public.inventory_receive(
|
|
6445
|
+
v_k::uuid,
|
|
6446
|
+
(v_groups->v_k)::jsonb,
|
|
6447
|
+
jsonb_build_object(
|
|
6448
|
+
'supplier_id', v_doc.supplier_party_id,
|
|
6449
|
+
-- O documento do estoque tamb\xE9m quer o DIA.
|
|
6450
|
+
'date', v_competence,
|
|
6451
|
+
'type', 'nfe',
|
|
6452
|
+
'ref', 'NF-e ' || coalesce(v_doc.nfe_number, '?'),
|
|
6453
|
+
'notes', 'Entrada via DANFE NF-e ' || coalesce(v_doc.nfe_number, '?'),
|
|
6454
|
+
'reason', 'Entrada via DANFE NF-e ' || coalesce(v_doc.nfe_number, '?')),
|
|
6455
|
+
-- A note into one location keeps the key it always had; each extra
|
|
6456
|
+
-- location is its own receipt, idempotent on its own.
|
|
6457
|
+
(CASE WHEN v_nloc = 1 THEN 'nfe:' || v_doc.access_key
|
|
6458
|
+
ELSE 'nfe:' || v_doc.access_key || ':' || v_k END)::text);
|
|
6459
|
+
v_op_first := coalesce(v_op_first, (v_recv->>'operation_id')::uuid);
|
|
6460
|
+
v_movs := v_movs || coalesce(v_recv->'movements', '[]'::jsonb);
|
|
6461
|
+
|
|
6462
|
+
UPDATE public.plg_fiscalbr_inbox_items i
|
|
6463
|
+
SET stock_movement_id = m.movement_id, updated_at = now()
|
|
6464
|
+
FROM (
|
|
6465
|
+
SELECT ((v_gitems->v_k)->>(((mov->>'line_no')::int) - 1))::uuid AS item_id, (mov->>'id')::uuid AS movement_id
|
|
6466
|
+
FROM jsonb_array_elements(coalesce(v_recv->'movements', '[]'::jsonb)) AS t(mov)
|
|
6467
|
+
WHERE (mov->>'line_no') IS NOT NULL
|
|
6468
|
+
) m
|
|
6469
|
+
WHERE i.tenant_id = v_tenant AND i.id = m.item_id;
|
|
6470
|
+
END LOOP;
|
|
6471
|
+
END IF;
|
|
6472
|
+
|
|
6473
|
+
IF v_doc.invoice_id IS NOT NULL THEN
|
|
6474
|
+
-- Half-done in the V1: the payable exists; only the stock is missing.
|
|
6475
|
+
v_invoice := v_doc.invoice_id;
|
|
6476
|
+
SELECT total INTO v_total FROM public.plg_financial_invoices WHERE id = v_invoice;
|
|
6477
|
+
v_insts := '[]'::jsonb;
|
|
6478
|
+
ELSE
|
|
6479
|
+
SELECT coalesce(sum(value), 0),
|
|
6480
|
+
coalesce(jsonb_agg(jsonb_build_object(
|
|
6481
|
+
'number', coalesce(duplicate_index, 1),
|
|
6482
|
+
'due_date', coalesce(due_date, v_competence, current_date),
|
|
6483
|
+
'amount', round(value, 2),
|
|
6484
|
+
'metadata', jsonb_strip_nulls(jsonb_build_object(
|
|
6485
|
+
'barcode', barcode, 'pixKey', pix_key, 'paymentType', payment_type,
|
|
6486
|
+
'danfeNumber', number))) ORDER BY duplicate_index), '[]'::jsonb)
|
|
6487
|
+
INTO v_total, v_insts
|
|
6488
|
+
FROM public.plg_fiscalbr_inbox_duplicates
|
|
6489
|
+
WHERE tenant_id = v_tenant AND inbox_document_id = v_doc.id;
|
|
6490
|
+
|
|
6491
|
+
IF jsonb_array_length(v_insts) = 0 THEN
|
|
6492
|
+
v_total := round(coalesce(v_doc.total_value, 0), 2);
|
|
6493
|
+
v_insts := jsonb_build_array(jsonb_build_object(
|
|
6494
|
+
'number', 1, 'due_date', coalesce(v_competence, current_date), 'amount', v_total));
|
|
6495
|
+
ELSE
|
|
6496
|
+
v_total := round(v_total, 2);
|
|
6497
|
+
END IF;
|
|
6498
|
+
|
|
6499
|
+
v_key := 'danfe:' || v_doc.access_key;
|
|
6500
|
+
|
|
6501
|
+
-- The payable is born at the note's full value (V1 rule): the supplier
|
|
6502
|
+
-- billed it, and what did not arrive is abated by finance.
|
|
6503
|
+
v_inv := public.financial_create_invoice(
|
|
6504
|
+
'payable'::text,
|
|
6505
|
+
v_total::numeric,
|
|
6506
|
+
v_doc.supplier_party_id::uuid,
|
|
6507
|
+
NULL::uuid,
|
|
6508
|
+
v_doc.unit_id::uuid,
|
|
6509
|
+
v_insts::jsonb,
|
|
6510
|
+
v_competence::date,
|
|
6511
|
+
('NF-e ' || coalesce(v_doc.nfe_number, '?') || ' \xB7 ' || coalesce(v_doc.supplier_name, 'fornecedor'))::text,
|
|
6512
|
+
NULL::text,
|
|
6513
|
+
jsonb_build_object(
|
|
6514
|
+
'source', 'danfe',
|
|
6515
|
+
'danfe', jsonb_strip_nulls(jsonb_build_object(
|
|
6516
|
+
'accessKey', v_doc.access_key, 'number', v_doc.nfe_number,
|
|
6517
|
+
'series', v_doc.nfe_series, 'supplierName', v_doc.supplier_name,
|
|
6518
|
+
'supplierCnpj', v_doc.supplier_cnpj,
|
|
6519
|
+
'noteTotal', v_doc.total_value,
|
|
6520
|
+
'inboxDocumentId', v_doc.id)))::jsonb,
|
|
6521
|
+
v_key::text);
|
|
6522
|
+
v_invoice := (v_inv->>'invoice_id')::uuid;
|
|
6523
|
+
|
|
6524
|
+
INSERT INTO public.items (tenant_id, invoice_id, product_id, chart_account_id, name, quantity, unit_price, total, sort_order, metadata)
|
|
6525
|
+
SELECT v_tenant, v_invoice, i.product_id, coalesce(i.chart_account_id, v_doc.default_chart_account_id),
|
|
6526
|
+
i.description, i.quantity_commercial, i.unit_price_commercial,
|
|
6527
|
+
round(coalesce(i.total_price, 0), 2), i.item_number,
|
|
6528
|
+
jsonb_strip_nulls(jsonb_build_object(
|
|
6529
|
+
'itemKind', CASE WHEN i.product_id IS NOT NULL THEN 'product' ELSE 'other' END,
|
|
6530
|
+
'referenceId', i.product_id, 'entryType', i.entry_type,
|
|
6531
|
+
'chartAccountId', coalesce(i.chart_account_id, v_doc.default_chart_account_id),
|
|
6532
|
+
'ncm', i.ncm, 'cfop', i.cfop,
|
|
6533
|
+
'supplierProductCode', i.supplier_product_code,
|
|
6534
|
+
-- The note line this payable line came from: the receipt check
|
|
6535
|
+
-- reaches the payable through it.
|
|
6536
|
+
'inboxItemId', i.id))
|
|
6537
|
+
FROM public.plg_fiscalbr_inbox_items i
|
|
6538
|
+
WHERE i.tenant_id = v_tenant AND i.inbox_document_id = v_doc.id
|
|
6539
|
+
ORDER BY i.item_number
|
|
6540
|
+
ON CONFLICT DO NOTHING;
|
|
6541
|
+
PERFORM public.plg_fiscalbr_note_charge_lines(v_tenant, v_doc.id, v_invoice, v_total);
|
|
6542
|
+
END IF;
|
|
6543
|
+
|
|
6544
|
+
UPDATE public.plg_fiscalbr_inbox_documents SET
|
|
6545
|
+
invoice_id = v_invoice,
|
|
6546
|
+
-- The first receipt; every line also points at its own movement.
|
|
6547
|
+
inventory_operation_id = coalesce(v_op_first, inventory_operation_id),
|
|
6548
|
+
-- A note whose stock lines all came short was still received: nothing is
|
|
6549
|
+
-- left to enter, so it is settled too.
|
|
6550
|
+
stocked_at = CASE WHEN v_bearing > 0 THEN now() ELSE stocked_at END,
|
|
6551
|
+
status = 'accepted', updated_at = now()
|
|
6552
|
+
WHERE id = v_doc.id;
|
|
6553
|
+
|
|
6554
|
+
INSERT INTO public.plg_fiscalbr_supplier_product_map (
|
|
6555
|
+
tenant_id, supplier_cnpj, supplier_product_code, product_id, entry_type,
|
|
6556
|
+
chart_account_id, stock_unit_id, conversion_factor, cost_mode, hit_count, last_used_at)
|
|
6557
|
+
SELECT v_tenant, v_doc.supplier_cnpj, i.supplier_product_code, i.product_id,
|
|
6558
|
+
i.entry_type, coalesce(i.chart_account_id, v_doc.default_chart_account_id),
|
|
6559
|
+
i.stock_unit_id, i.conversion_factor, i.cost_mode, 1, now()
|
|
6560
|
+
FROM public.plg_fiscalbr_inbox_items i
|
|
6561
|
+
WHERE i.tenant_id = v_tenant AND i.inbox_document_id = v_doc.id
|
|
6562
|
+
AND i.product_id IS NOT NULL
|
|
6563
|
+
AND coalesce(v_doc.supplier_cnpj, '') <> ''
|
|
6564
|
+
AND coalesce(i.supplier_product_code, '') <> ''
|
|
6565
|
+
ON CONFLICT (tenant_id, supplier_cnpj, supplier_product_code) DO UPDATE SET
|
|
6566
|
+
product_id = EXCLUDED.product_id,
|
|
6567
|
+
entry_type = EXCLUDED.entry_type,
|
|
6568
|
+
chart_account_id = coalesce(EXCLUDED.chart_account_id, public.plg_fiscalbr_supplier_product_map.chart_account_id),
|
|
6569
|
+
stock_unit_id = coalesce(EXCLUDED.stock_unit_id, public.plg_fiscalbr_supplier_product_map.stock_unit_id),
|
|
6570
|
+
conversion_factor = EXCLUDED.conversion_factor,
|
|
6571
|
+
cost_mode = EXCLUDED.cost_mode,
|
|
6572
|
+
hit_count = public.plg_fiscalbr_supplier_product_map.hit_count + 1,
|
|
6573
|
+
last_used_at = now(),
|
|
6574
|
+
updated_at = now();
|
|
6575
|
+
|
|
6576
|
+
RETURN jsonb_build_object(
|
|
6577
|
+
'document_id', v_doc.id, 'already_accepted', false,
|
|
6578
|
+
'invoice_id', v_invoice, 'invoice_total', v_total,
|
|
6579
|
+
'installments', jsonb_array_length(v_insts),
|
|
6580
|
+
'operation_id', v_op_first,
|
|
6581
|
+
'stock_locations', v_nloc,
|
|
6582
|
+
'stock_lines', v_stock_n,
|
|
6583
|
+
'shortage_lines', v_short_n,
|
|
6584
|
+
'shortage_value', v_short_v,
|
|
6585
|
+
'movements', v_movs);
|
|
6586
|
+
END $function$;
|
|
6587
|
+
|
|
6588
|
+
REVOKE ALL ON FUNCTION public.fiscalbr_accept_inbox_document(uuid, uuid) FROM PUBLIC;
|
|
6589
|
+
GRANT EXECUTE ON FUNCTION public.fiscalbr_accept_inbox_document(uuid, uuid) TO authenticated;
|
|
6590
|
+
`;
|
|
6026
6591
|
var MIGRATIONS = [
|
|
6027
6592
|
{ id: "000_baseline", sql: MIGRATION_000_BASELINE },
|
|
6028
6593
|
{ id: "001_preserve_terminal_results_and_recover_leases", sql: MIGRATION_001_PRESERVE_TERMINAL_RESULTS_AND_RECOVER_LEASES },
|
|
@@ -6062,9 +6627,10 @@ var MIGRATIONS = [
|
|
|
6062
6627
|
{ id: "035_the_emitter_borrows_the_unit_address", sql: MIGRATION_035_THE_EMITTER_BORROWS_THE_UNIT_ADDRESS },
|
|
6063
6628
|
{ id: "036_the_fiscal_group_names_the_service_on_the_note", sql: MIGRATION_036_THE_FISCAL_GROUP_NAMES_THE_SERVICE_ON_THE_NOTE },
|
|
6064
6629
|
{ 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 }
|
|
6630
|
+
{ id: "038_the_emitter_tells_its_nfse_config", sql: MIGRATION_038_THE_EMITTER_TELLS_ITS_NFSE_CONFIG },
|
|
6631
|
+
{ id: "039_each_line_enters_its_own_location", sql: MIGRATION_039_EACH_LINE_ENTERS_ITS_OWN_LOCATION }
|
|
6066
6632
|
];
|
|
6067
6633
|
|
|
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 };
|
|
6634
|
+
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, MIGRATION_039_EACH_LINE_ENTERS_ITS_OWN_LOCATION };
|
|
6069
6635
|
//# sourceMappingURL=main.js.map
|
|
6070
6636
|
//# sourceMappingURL=main.js.map
|