@tesouro/embedded-components-react 0.5.225 → 0.5.226

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.
Files changed (41) hide show
  1. package/README.md +21 -21
  2. package/dist/index.d.ts +23 -2
  3. package/dist/index.js +1 -1
  4. package/dist/lib/ReceivablesWidget.d.ts +23 -2
  5. package/dist/lib/ReceivablesWidget.js +1 -1
  6. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/data-access/dist/index.js +4 -4
  7. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/data-access/dist/index10.js +45 -31
  8. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index.js +1 -1
  9. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index12.js +51 -32
  10. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index13.js +20 -1
  11. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index15.js +17 -202
  12. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index16.js +220 -31
  13. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index17.js +32 -145
  14. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index18.js +126 -44
  15. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index19.js +145 -117
  16. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index20.js +46 -58
  17. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index21.js +113 -87
  18. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index22.js +58 -116
  19. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index23.js +98 -225
  20. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index24.js +111 -27
  21. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index25.js +225 -462
  22. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index26.js +34 -41
  23. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index27.js +627 -34
  24. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index28.js +39 -77
  25. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index29.js +34 -112
  26. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index30.js +75 -310
  27. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index31.js +124 -0
  28. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index32.js +324 -0
  29. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index6.js +20 -15
  30. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index8.js +56 -53
  31. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index9.js +33 -29
  32. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/ui/dist/index25.js +184 -162
  33. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/ui/dist/index4.js +1 -3
  34. package/dist/libs/tesouro-embedded-components-react/invoicing-widget/ui/dist/index6.js +78 -69
  35. package/dist/libs/tesouro-embedded-components-react/shared/counterpart-management/feature/dist/lib/CounterpartSelector.js +7 -6
  36. package/dist/libs/tesouro-embedded-components-react/shared/counterpart-management/ui/dist/index15.js +20 -19
  37. package/dist/libs/tesouro-embedded-components-react/shared/data-access/dist/index.js +39 -39
  38. package/dist/libs/tesouro-embedded-components-react/shared/data-access/dist/lib/rest/@tanstack/react-query.gen.js +210 -199
  39. package/dist/libs/tesouro-embedded-components-react/shared/data-access/dist/lib/rest/sdk.gen.js +60 -52
  40. package/dist/libs/tesouro-embedded-components-react/shared/feature/dist/lib/analytics/build-info.js +1 -1
  41. package/package.json +1 -1
@@ -2,42 +2,635 @@
2
2
  "use client";
3
3
  "use client";
4
4
  "use client";
5
- import { useReceivablesAnalyticsQuery as e } from "../../data-access/dist/index12.js";
5
+ import { toast as e } from "../../../../../packages/shared-ui-shadcn/dist/index.js";
6
+ import { getStateOptions as t } from "../../../shared/util/dist/lib/usStates.js";
7
+ import "../../../shared/ui/dist/index.js";
8
+ import { COUNTERPART_MESSAGE_LABELS_EN as n } from "../../../shared/counterpart-management/feature/dist/lib/labels.js";
9
+ import { useCounterpartAddressesQuery as r } from "../../../shared/counterpart-management/data-access/dist/index7.js";
10
+ import { useCounterpartForm as i } from "../../../shared/counterpart-management/feature/dist/lib/useCounterpartForm.js";
11
+ import "../../../shared/counterpart-management/feature/dist/index.js";
12
+ import "../../ui/dist/index10.js";
13
+ import "../../ui/dist/index.js";
14
+ import { buildLineItemControls as a, commitLineItemField as o, commitTaxRateInput as s, convertPriceInput as c, createLineItem as l, getCurrencySymbol as ee, lineItemFromProduct as te, reconcileLineItemIds as ne, reconcileMeasureUnits as re, sanitizeLineItemPatch as ie, sanitizeTaxRateInput as ae, stepLineItemQuantity as oe, stripBlankLineItems as se, summarizeLineItemErrors as ce, toSheetLineItem as le, validateLineItems as ue } from "./index6.js";
15
+ import { buildReceivableAttachmentsView as de } from "./index7.js";
16
+ import { buildCreateRecurrencePayload as fe, buildUpdateRecurrencePayload as pe, clearRecurrenceRecipients as me, countIssuedIterations as he, isRecurring as u, recurrenceToFormFields as ge, validateRecurrence as _e, willRecurrenceIssue as ve } from "./index8.js";
17
+ import { convertDiscountInput as ye, decorateDiscountInput as be, discountInputToMinorUnits as xe, sanitizeDiscountInput as Se } from "./index10.js";
18
+ import { buildFormTotalsView as Ce, calculateFormTotals as we, validateDiscount as Te, validateInvoiceTotals as Ee } from "./index11.js";
19
+ import { createEmptyReceivableFormValues as De, hasReceivableFormErrors as Oe, receivableFormValuesToCreatePayload as ke, receivableFormValuesToLineItemChanges as Ae, receivableFormValuesToUpdatePayload as je, receivableToFormValues as Me, submittedLineItemRows as Ne, validateReceivableForm as Pe } from "./index12.js";
20
+ import { getIssueReminderBlocker as Fe, shouldShowRemindersInitially as Ie } from "./index14.js";
21
+ import { useCreateReceivableMutation as Le, useIssueReceivableMutation as Re, useUpdateReceivableMutation as ze } from "../../data-access/dist/index7.js";
22
+ import { useCreateRecurrenceMutation as Be, useRecurrenceQuery as Ve, useUpdateRecurrenceMutation as He } from "../../data-access/dist/index11.js";
6
23
  import "../../data-access/dist/index.js";
7
- import { buildReceivableStatusCardsView as t } from "./index26.js";
8
- import { useMemo as n } from "react";
24
+ import { useReceivableIssuePreflight as Ue } from "./index15.js";
25
+ import { UNSUBMITTED_PAYLOAD_VARIANTS as We, apiValidationErrorsToFormErrors as Ge, getApiErrorMessage as Ke, parseApiValidationErrors as qe } from "./index18.js";
26
+ import { useReceivableAttachmentUploads as Je } from "./index19.js";
27
+ import { useReceivablePaymentTerms as Ye } from "./index20.js";
28
+ import { useReceivableRecurrence as Xe } from "./index21.js";
29
+ import { useReceivableBankAccount as Ze } from "./index23.js";
30
+ import { useReceivableReminders as Qe } from "./index24.js";
31
+ import { useReceivablePreview as $e } from "./index26.js";
32
+ import { getCurrencyOptions as et } from "../../../shared/product-catalog/feature/dist/lib/helpers.js";
33
+ import { useMeasureUnitsQuery as tt } from "../../../shared/product-catalog/data-access/dist/index9.js";
34
+ import { useMeasureUnits as nt } from "../../../shared/product-catalog/feature/dist/lib/useMeasureUnits.js";
35
+ import "../../../shared/product-catalog/feature/dist/index.js";
36
+ import { useCallback as d, useEffect as f, useMemo as p, useRef as m, useState as h } from "react";
9
37
  //#region ../../libs/tesouro-embedded-components-react/invoicing-widget/feature/dist/index27.js
10
- function r({ enabled: r, statusFilter: i, messages: a, statusLabels: o }) {
11
- let s = e({
12
- enabled: r,
13
- dimension: "status",
14
- metric: "id",
15
- aggregation_function: "count",
16
- type: "invoice"
17
- }), c = e({
18
- enabled: r,
19
- dimension: "status",
20
- metric: "total_amount",
21
- aggregation_function: "summary",
22
- type: "invoice"
23
- }), l = s.data?.data, u = c.data?.data, d = s.isPending || c.isPending;
24
- return n(() => {
25
- if (r) return d ? { kind: "loading" } : t({
26
- counts: l ?? [],
27
- amounts: u ?? [],
28
- statusFilter: i,
29
- statusLabels: o,
30
- fallback: a.notAvailable
31
- });
32
- }, [
33
- u,
34
- l,
35
- r,
36
- d,
37
- a.notAvailable,
38
- i,
39
- o
38
+ function g({ messages: g, counterpartMessageLabels: rt, embeddedBankName: it, onCreated: at }) {
39
+ let ot = Le(), st = ze(), ct = Be(), lt = He(), ut = Re(), _ = m("draft"), dt = m(!1), v = m(!1), y = m(!1), ft = m(null), b = m(void 0), x = m([]), pt = m(""), [S, C] = h(!1), [w, mt] = h("create"), [ht, gt] = h(), [T, _t] = h(), [E, D] = h(() => De()), [vt, O] = h({}), [yt, bt] = h("none"), xt = nt(), k = tt(), A = k.data?.data, { unitNamesById: j } = xt, [St, Ct] = h(), [M, wt] = h(!1), Tt = m(!1), N = m(!1), P = m(!1), F = m({
40
+ lines: !1,
41
+ discount: !1
42
+ }), [Et, I] = h(!1), Dt = p(() => t(), []), Ot = p(() => ({
43
+ ...n,
44
+ ...rt
45
+ }), [rt]), kt = r(E.customerId), At = kt.data?.data?.[0], L = kt.data?.data?.[0]?.id, jt = !!E.customerId && (kt.isLoading || kt.isFetching), R = Ue({
46
+ counterpartId: E.customerId,
47
+ billingAddressId: L,
48
+ billingCountry: At?.country
49
+ }), Mt = R.isLoading || jt, Nt = d((e) => {
50
+ D((t) => ({
51
+ ...t,
52
+ attachments: e(t.attachments)
53
+ }));
54
+ }, []), z = Je({
55
+ attachments: E.attachments,
56
+ setAttachments: Nt,
57
+ receivableId: ht
58
+ }), { reset: B, discardUploads: Pt, commitRemovals: Ft } = z, It = E.type === "invoice" ? de({
59
+ attachments: E.attachments,
60
+ pendingUploads: z.pendingUploads,
61
+ isCountLimitReached: z.isCountLimitReached,
62
+ isRecurring: u(E),
63
+ messages: g
64
+ }) : void 0, Lt = d((e) => {
65
+ D((t) => {
66
+ let n = e ?? "";
67
+ return n === t.customerId ? t : me({
68
+ ...t,
69
+ customerId: n
70
+ });
71
+ });
72
+ }, []), Rt = i({
73
+ view: yt,
74
+ setView: bt,
75
+ selectedId: E.customerId || void 0,
76
+ setSelectedId: Lt,
77
+ messages: Ot,
78
+ counterpartType: "customer",
79
+ counterpart: void 0,
80
+ primaryAddress: At
81
+ }), zt = d((e) => D((t) => {
82
+ let n = e({
83
+ paymentTermsId: t.paymentTermsId,
84
+ paymentTermsCustomDate: t.paymentTermsCustomDate
85
+ });
86
+ return {
87
+ ...t,
88
+ ...n,
89
+ ...n.paymentTermsId === "receipt_due" ? { paymentReminderId: "" } : {}
90
+ };
91
+ }), []), Bt = Ye({
92
+ paymentTermsId: E.paymentTermsId,
93
+ paymentTermsCustomDate: E.paymentTermsCustomDate,
94
+ issueDate: E.issueDate,
95
+ isRecurring: E.type === "invoice" && u(E),
96
+ messages: g,
97
+ onChange: zt
98
+ }), Vt = Bt.reset, Ht = d((e) => D((t) => ({
99
+ ...t,
100
+ ...e
101
+ })), []), V = Ze({
102
+ open: S,
103
+ bankAccountId: E.bankAccountId,
104
+ embeddedBankName: it,
105
+ messages: g,
106
+ onSelect: Ht
107
+ }), Ut = V.reset, Wt = Ve(T), H = T ? Wt.data : void 0, U = he(H), Gt = U > 0, W = !!T && Wt.isLoading, G = m(void 0);
108
+ f(() => {
109
+ H && G.current !== H.id && (G.current = H.id, D((e) => ({
110
+ ...e,
111
+ ...ge(H)
112
+ })));
113
+ }, [H]);
114
+ let Kt = d((e) => D(e), []), qt = d((e) => O((t) => {
115
+ if (!e.some((e) => e in t)) return t;
116
+ let n = { ...t };
117
+ return e.forEach((e) => delete n[e]), n;
118
+ }), []), Jt = m([]), Yt = d((e) => {
119
+ if (!M) {
120
+ qt(e);
121
+ return;
122
+ }
123
+ Jt.current.push(...e);
124
+ }, [qt, M]);
125
+ f(() => {
126
+ let e = Jt.current;
127
+ if (!e.length) return;
128
+ Jt.current = [];
129
+ let t = _e({
130
+ fields: E,
131
+ issuedCount: U,
132
+ messages: g
133
+ });
134
+ O((n) => {
135
+ let r = { ...n };
136
+ return e.forEach((e) => {
137
+ t[e] ? r[e] = t[e] : delete r[e];
138
+ }), r;
139
+ });
140
+ }, [
141
+ U,
142
+ g,
143
+ E
144
+ ]);
145
+ let Xt = Xe({
146
+ values: E,
147
+ errors: vt,
148
+ lockSchedule: Gt,
149
+ messages: g,
150
+ onChange: Kt,
151
+ onClearErrors: qt,
152
+ onRevalidateErrors: Yt
153
+ }), K = Xt.reset, Zt = d((e) => D((t) => ({
154
+ ...t,
155
+ ...e
156
+ })), []), Qt = ot.isPending || st.isPending || ct.isPending || lt.isPending || ut.isPending, $t = Qe({
157
+ open: S,
158
+ isInvoice: E.type === "invoice",
159
+ disabled: Qt,
160
+ paymentTermsId: E.paymentTermsId,
161
+ paymentTermsCustomDate: E.paymentTermsCustomDate,
162
+ paymentReminderId: E.paymentReminderId,
163
+ overdueReminderId: E.overdueReminderId,
164
+ messages: g,
165
+ onSelect: Zt
166
+ }), en = $t.reset, tn = d(() => {
167
+ x.current = [], Ct(void 0), mt("create"), gt(void 0), _t(void 0), G.current = void 0, b.current = void 0, _.current = "draft", v.current = !1, y.current = !1, D(De()), O({}), wt(!1), I(!1), F.current = {
168
+ lines: !1,
169
+ discount: !1
170
+ }, B(), K();
171
+ }, [B, K]), nn = d((e = "invoice") => {
172
+ Vt(), Ut(), K();
173
+ let t = {
174
+ ...De(),
175
+ type: e
176
+ };
177
+ en(Ie(t)), x.current = [], b.current = void 0, _.current = "draft", v.current = !1, y.current = !1, G.current = void 0, Ct(void 0), mt("create"), gt(void 0), _t(void 0), D(t), O({}), B(), wt(!1), I(!1), F.current = {
178
+ lines: !1,
179
+ discount: !1
180
+ }, C(!0);
181
+ }, [
182
+ B,
183
+ Vt,
184
+ Ut,
185
+ K,
186
+ en
187
+ ]), rn = d((e, { highlightForIssue: t = !1 } = {}) => {
188
+ if (e.type === "credit_note") return;
189
+ Vt(), Ut(), K();
190
+ let n = Me(e, A);
191
+ en(Ie(n)), x.current = e.line_items, pt.current = e.currency, b.current = void 0, _.current = t ? "issue" : "draft", v.current = !1, y.current = t, G.current = void 0, Ct(void 0), mt("edit"), gt(e.id), _t(e.type === "invoice" ? e.recurrence_id ?? void 0 : void 0), D(n), O({}), wt(!1), I(!1), F.current = {
192
+ lines: !1,
193
+ discount: !1
194
+ }, B(), C(!0);
195
+ }, [
196
+ B,
197
+ Vt,
198
+ Ut,
199
+ K,
200
+ en,
201
+ A
202
+ ]);
203
+ f(() => {
204
+ A?.length && D((e) => {
205
+ let t = re(e.lineItems, A);
206
+ return t === e.lineItems ? e : {
207
+ ...e,
208
+ lineItems: t
209
+ };
210
+ });
211
+ }, [A]);
212
+ let an = z.isUploading, on = d(() => {
213
+ an || (Pt(), C(!1), O({}));
214
+ }, [Pt, an]), sn = d((e) => {
215
+ let t = { ...e };
216
+ t.taxRate !== void 0 && (t.taxRate = ae(t.taxRate), P.current = !0), t.type !== void 0 && t.type !== "invoice" && Pt(), t.type !== void 0 && (_.current = "draft"), t.discount !== void 0 && M && (N.current = !0), t.discountType !== void 0 && (N.current = !0), t.currency !== void 0 && (Tt.current = !0), D((e) => {
217
+ let n = {
218
+ ...e,
219
+ ...t
220
+ };
221
+ return t.discount !== void 0 && (n.discount = Se(t.discount, n.discountType, n.currency)), n.currency !== e.currency && (n.lineItems = e.lineItems.map((t) => ({
222
+ ...t,
223
+ price: c(t.price, e.currency, n.currency)
224
+ }))), (n.currency !== e.currency || n.discountType !== e.discountType) && (n.discount = ye({
225
+ value: n.discount,
226
+ type: n.discountType,
227
+ currency: n.currency
228
+ })), n;
229
+ });
230
+ }, [Pt, M]), cn = d(() => I(!0), []), ln = d(() => {
231
+ I(!1), N.current = !0, D((e) => ({
232
+ ...e,
233
+ discount: ye({
234
+ value: e.discount,
235
+ type: e.discountType,
236
+ currency: e.currency
237
+ })
238
+ }));
239
+ }, []), un = d(() => {
240
+ P.current = !0, D((e) => {
241
+ let t = s(e.taxRate);
242
+ return t === e.taxRate ? e : {
243
+ ...e,
244
+ taxRate: t
245
+ };
246
+ });
247
+ }, []), q = d((e, { validate: t }) => {
248
+ t && (Tt.current = !0), D((t) => ({
249
+ ...t,
250
+ lineItems: e(t.lineItems)
251
+ }));
252
+ }, []);
253
+ f(() => {
254
+ let e = Tt.current, t = N.current;
255
+ if (!e && !t && !P.current) return;
256
+ Tt.current = !1, N.current = !1, P.current = !1, e && (F.current.lines = !0), t && (F.current.discount = !0);
257
+ let n = v.current || U > 0, r = e ? ue(E.lineItems, {
258
+ currency: E.currency,
259
+ messages: g,
260
+ requireComplete: n
261
+ }) : void 0;
262
+ O((t) => {
263
+ let i = { ...t };
264
+ return F.current.lines && (i.lineItems = e ? r : t.lineItems, i.lineItemsSummary = ce(i.lineItems, Ee(E, g), n && E.lineItems.length === 0 ? g.lineItemsRequired : void 0)), F.current.discount && (i.discount = Te(E, g)), i;
265
+ });
266
+ }, [
267
+ U,
268
+ g,
269
+ E
270
+ ]);
271
+ let dn = d((e, t) => {
272
+ let { unit: n, ...r } = t, i = ie(r);
273
+ i.name !== void 0 && (i.productId = ""), n !== void 0 && (i.measureUnitId = n, i.measureUnitName = ""), i.taxable !== void 0 && (P.current = !0), q((t) => t.map((t) => t.id === e ? {
274
+ ...t,
275
+ ...i
276
+ } : t), { validate: M });
277
+ }, [q, M]), fn = d((e, t) => {
278
+ q((n) => n.map((n) => n.id === e ? {
279
+ ...n,
280
+ ...te({
281
+ product: t,
282
+ currency: E.currency,
283
+ unitNamesById: j
284
+ })
285
+ } : n), { validate: !0 });
286
+ }, [
287
+ q,
288
+ j,
289
+ E.currency
290
+ ]), pn = d((e, t) => {
291
+ t && q((n) => t.kind === "remove" ? n.filter((t) => t.id !== e) : n.map((n) => n.id === e ? {
292
+ ...n,
293
+ ...t.patch
294
+ } : n), { validate: !0 });
295
+ }, [q]), mn = d((e, t) => {
296
+ let n = E.lineItems.find((t) => t.id === e);
297
+ n && pn(e, o({
298
+ lineItem: n,
299
+ field: t,
300
+ rowCount: E.lineItems.length,
301
+ currency: E.currency
302
+ }));
303
+ }, [
304
+ pn,
305
+ E.currency,
306
+ E.lineItems
307
+ ]), hn = d((e, t) => {
308
+ let n = E.lineItems.find((t) => t.id === e);
309
+ n && pn(e, oe({
310
+ lineItem: n,
311
+ step: t,
312
+ rowCount: E.lineItems.length
313
+ }));
314
+ }, [pn, E.lineItems]), gn = d(() => {
315
+ let e = l(E.currency);
316
+ Ct(e.id), D((t) => ({
317
+ ...t,
318
+ lineItems: [...t.lineItems, e]
319
+ }));
320
+ }, [E.currency]), _n = d((e) => {
321
+ q((t) => t.length <= 1 ? t : t.filter((t) => t.id !== e), { validate: !0 });
322
+ }, [q]), J = d((e, t) => e.type === "invoice" && (t === "issue" || ve(e) && U === 0), [U]), vn = d((e, t) => ({
323
+ ...Pe({
324
+ values: e,
325
+ billingAddressId: L,
326
+ messages: g,
327
+ willIssue: t,
328
+ hasStartedRecurrence: U > 0
329
+ }),
330
+ ...e.type === "invoice" && u(e) ? _e({
331
+ fields: e,
332
+ issuedCount: U,
333
+ messages: g
334
+ }) : {}
335
+ }), [
336
+ L,
337
+ U,
338
+ g
339
+ ]), yn = d(() => {
340
+ requestAnimationFrame(() => {
341
+ let e = ft.current, t = e?.querySelector("[aria-invalid=\"true\"]");
342
+ if (!e || !t) return;
343
+ let n = t.getBoundingClientRect().top - e.getBoundingClientRect().top + e.scrollTop;
344
+ e.scrollTo({
345
+ top: Math.max(0, n - e.clientHeight / 2 + t.offsetHeight / 2),
346
+ behavior: "smooth"
347
+ });
348
+ });
349
+ }, []), Y = d((e, t) => {
350
+ O(e), wt(!0), v.current = t, F.current = {
351
+ lines: !0,
352
+ discount: !0
353
+ }, yn();
354
+ }, [yn]), bn = !T || E.frequency !== "once" || Wt.isError;
355
+ f(() => {
356
+ if (!S || !y.current || jt || W || !bn) return;
357
+ y.current = !1;
358
+ let e = J(E, "issue");
359
+ Y(vn(E, e), e);
360
+ }, [
361
+ jt,
362
+ S,
363
+ bn,
364
+ W,
365
+ Y,
366
+ vn,
367
+ E,
368
+ J
369
+ ]);
370
+ let xn = m({
371
+ values: E,
372
+ billingAddressId: L
373
+ });
374
+ f(() => {
375
+ let e = xn.current;
376
+ if (xn.current = {
377
+ values: E,
378
+ billingAddressId: L
379
+ }, !M) return;
380
+ let t = [
381
+ "customerId",
382
+ "paymentTermsId",
383
+ "bankAccountId",
384
+ "openingNote",
385
+ "closingNote",
386
+ "taxRate"
387
+ ].filter((t) => e.values[t] !== E[t] || t === "customerId" && e.billingAddressId !== L || t === "paymentTermsId" && e.values.paymentTermsCustomDate !== E.paymentTermsCustomDate);
388
+ if (!t.length) return;
389
+ let n = Pe({
390
+ values: E,
391
+ billingAddressId: L,
392
+ messages: g,
393
+ willIssue: v.current
394
+ });
395
+ O((e) => {
396
+ let r = { ...e };
397
+ return t.forEach((e) => {
398
+ n[e] ? r[e] = n[e] : delete r[e];
399
+ }), r;
400
+ });
401
+ }, [
402
+ L,
403
+ g,
404
+ M,
405
+ E
40
406
  ]);
407
+ let Sn = d(async (t) => {
408
+ if (_.current = t, W) return;
409
+ if (an) {
410
+ e.error(g.attachmentsUploading);
411
+ return;
412
+ }
413
+ if (k.isLoading) {
414
+ e.error(g.lineItemsLoading);
415
+ return;
416
+ }
417
+ let n = se(E.lineItems), r = n.length === E.lineItems.length ? E : {
418
+ ...E,
419
+ lineItems: n
420
+ };
421
+ r !== E && D(r);
422
+ let i = r.type === "invoice" && u(r), a = J(r, t);
423
+ if (a && (Mt || V.isLoading)) {
424
+ e.error(g.issueChecksLoading);
425
+ return;
426
+ }
427
+ if (a && !V.canAddAccount && !r.bankAccountId && !V.hasAccounts) {
428
+ e.error(g.issueBankAccountUnavailable);
429
+ return;
430
+ }
431
+ let o = vn(r, a);
432
+ if (Oe(o)) {
433
+ Y(o, a);
434
+ return;
435
+ }
436
+ if (O({}), a && R.isTaxIdMissing) {
437
+ e.error(g.issueTaxIdRequired);
438
+ return;
439
+ }
440
+ if (a && (r.paymentReminderId || r.overdueReminderId)) {
441
+ let t = Fe(R.counterpart, g);
442
+ if (t) {
443
+ e.error(t);
444
+ return;
445
+ }
446
+ }
447
+ let s = w === "edit" ? ht : b.current, c, l, ee = {};
448
+ try {
449
+ if (s) {
450
+ let e = Ae({
451
+ values: r,
452
+ originalLineItems: x.current,
453
+ originalCurrency: pt.current,
454
+ unitNamesById: j,
455
+ units: A
456
+ });
457
+ ee = Ne({
458
+ values: r,
459
+ originalLineItems: x.current,
460
+ changes: e
461
+ });
462
+ let t = await st.mutateAsync({
463
+ path: { receivable_id: s },
464
+ body: je({
465
+ values: r,
466
+ billingAddressId: L ?? "",
467
+ lineItemChanges: e
468
+ })
469
+ });
470
+ Ft(), c = s, l = t?.type ?? r.type, i && t && (x.current = t.line_items, pt.current = t.currency, r = {
471
+ ...r,
472
+ lineItems: ne(r.lineItems, t.line_items)
473
+ }, D(r));
474
+ } else {
475
+ ee = Ne({ values: r });
476
+ let e = await ot.mutateAsync({ body: ke({
477
+ values: r,
478
+ billingAddressId: L ?? "",
479
+ unitNamesById: j
480
+ }) });
481
+ if (!e?.id) {
482
+ C(!1), tn();
483
+ return;
484
+ }
485
+ c = e.id, l = e.type, i && (b.current = e.id, x.current = e.line_items, pt.current = e.currency, r = {
486
+ ...r,
487
+ lineItems: ne(r.lineItems, e.line_items)
488
+ }, D(r));
489
+ }
490
+ } catch (t) {
491
+ let n = We[r.type], i = Ke(t, {
492
+ irrelevantVariants: n,
493
+ messages: g
494
+ });
495
+ e.error((s ? g.updateFailed : g.createFailed).replace("{errorMessage}", i));
496
+ let { errors: o, listError: c } = Ge(qe(t, n), ee), l = ce(o.lineItems, void 0, c);
497
+ Y({
498
+ ...o,
499
+ ...l ? { lineItemsSummary: l } : {}
500
+ }, a);
501
+ return;
502
+ }
503
+ if (i) try {
504
+ H ? await lt.mutateAsync({
505
+ path: { recurrence_id: H.id },
506
+ body: pe(r, { lockSchedule: Gt })
507
+ }) : await ct.mutateAsync({ body: fe(r, c) });
508
+ } catch (t) {
509
+ e.error(Ke(t, { messages: g }));
510
+ return;
511
+ }
512
+ else if (a) try {
513
+ await ut.mutateAsync({ path: { receivable_id: c } });
514
+ } catch {
515
+ e.error(g.issueFailedAfterSave);
516
+ }
517
+ let te = w === "create";
518
+ C(!1), tn(), te && at?.(c, l);
519
+ }, [
520
+ V.canAddAccount,
521
+ V.hasAccounts,
522
+ V.isLoading,
523
+ L,
524
+ Ft,
525
+ ot,
526
+ ct,
527
+ ht,
528
+ H,
529
+ an,
530
+ Mt,
531
+ ut,
532
+ Gt,
533
+ g,
534
+ w,
535
+ at,
536
+ R.counterpart,
537
+ R.isTaxIdMissing,
538
+ W,
539
+ tn,
540
+ Y,
541
+ A,
542
+ j,
543
+ k.isLoading,
544
+ st,
545
+ lt,
546
+ vn,
547
+ E,
548
+ J
549
+ ]), X = d(async (e = _.current) => {
550
+ if (!dt.current) {
551
+ dt.current = !0;
552
+ try {
553
+ await Sn(e);
554
+ } finally {
555
+ dt.current = !1;
556
+ }
557
+ }
558
+ }, [Sn]), Cn = p(() => ({
559
+ ...E,
560
+ discount: Et ? E.discount : be(xe(E.discount, E.discountType, E.currency), E.discountType, E.currency),
561
+ lineItems: E.lineItems.map(le)
562
+ }), [Et, E]), wn = p(() => a(E.lineItems), [E.lineItems]), Z = p(() => we(E), [E]), Q = E.type === "invoice", Tn = Q && u(E), En = Qt || k.isLoading || z.isUploading || W, Dn = w === "edit" ? g.updateDraftButton : g.saveDraftButton, On = Tn ? H ? g.recurrenceUpdateButton : g.recurrenceActivateButton : Q ? g.issueButton : Dn, $ = Q && !Tn ? "issue" : "draft", kn = d(() => X("draft"), [X]), An = d(() => X($), [$, X]), jn = d(() => X(), [X]), Mn = d(() => {
563
+ _.current = "draft";
564
+ }, []), Nn = d(() => {
565
+ _.current = $;
566
+ }, [$]), Pn = p(() => Ce(Z, E.currency), [Z, E.currency]), Fn = $e({
567
+ values: E,
568
+ totals: Z,
569
+ billingAddress: At,
570
+ unitNamesById: j,
571
+ messages: g,
572
+ enabled: S
573
+ });
574
+ return {
575
+ open: S,
576
+ setOpen: C,
577
+ mode: w,
578
+ values: E,
579
+ sheetValues: Cn,
580
+ errors: vt,
581
+ setValues: D,
582
+ updateValues: sn,
583
+ commitTaxRate: un,
584
+ focusDiscount: cn,
585
+ commitDiscount: ln,
586
+ updateLineItem: dn,
587
+ selectProduct: fn,
588
+ commitLineItem: mn,
589
+ stepLineItem: hn,
590
+ addLineItem: gn,
591
+ removeLineItem: _n,
592
+ attachmentsView: It,
593
+ attachmentsUploading: z.isUploading,
594
+ draftButtonLabel: Q && !Tn ? Dn : void 0,
595
+ draftDisabled: En,
596
+ primaryButtonLabel: On,
597
+ primaryDisabled: En || V.isLoading && J(E, $) || Q && (Mt || !Tn && R.isCounterpartLoading),
598
+ scrollContainerRef: ft,
599
+ selectAttachmentFiles: z.selectFiles,
600
+ setAttachmentIncludeInEmail: z.setIncludeInEmail,
601
+ removeAttachment: z.remove,
602
+ autoFocusLineItemId: St,
603
+ lineItemUnitOptions: xt.unitOptions,
604
+ lineItemsLoading: k.isLoading,
605
+ lineItemControls: wn,
606
+ totals: Z,
607
+ preview: Fn,
608
+ totalsView: Pn,
609
+ currencySymbol: ee(E.currency),
610
+ isSubmitting: Qt,
611
+ currencyOptions: et(E.currency),
612
+ paymentTerms: Bt,
613
+ showBillingSection: E.type === "invoice" || w === "edit",
614
+ recurrence: Xt,
615
+ showRecurrenceSection: E.type === "invoice",
616
+ recurrenceLoading: W,
617
+ showDueDateField: E.type === "quote",
618
+ openCreate: nn,
619
+ openEdit: rn,
620
+ close: on,
621
+ submit: jn,
622
+ submitDraft: kn,
623
+ submitPrimary: An,
624
+ selectDraftMode: Mn,
625
+ selectPrimaryMode: Nn,
626
+ units: xt,
627
+ counterpartView: yt,
628
+ counterpartForm: Rt,
629
+ stateOptions: Dt,
630
+ openCounterpartCreate: Rt.openCreate,
631
+ bankAccount: V,
632
+ reminders: $t
633
+ };
41
634
  }
42
635
  //#endregion
43
- export { r as useReceivableStatusCards };
636
+ export { g as useReceivableForm };