@zeniai/client-epic-state 5.2.88 → 5.2.89-beta0KB
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/lib/entity/invoicing/invoicingCommonPayload.d.ts +9 -0
- package/lib/entity/invoicing/invoicingCommonPayload.js +8 -1
- package/lib/entity/snackbar/snackbarTypes.d.ts +1 -1
- package/lib/entity/snackbar/snackbarTypes.js +1 -0
- package/lib/entity/tenant/tenantPayload.d.ts +1 -0
- package/lib/entity/tenant/tenantReducer.js +1 -0
- package/lib/entity/tenant/tenantState.d.ts +1 -0
- package/lib/entity/transaction/stateTypes/paymentType.d.ts +2 -2
- package/lib/entity/transaction/stateTypes/paymentType.js +9 -1
- package/lib/entity/transaction/stateTypes/printStatus.d.ts +4 -0
- package/lib/entity/transaction/stateTypes/printStatus.js +11 -0
- package/lib/epic.d.ts +5 -1
- package/lib/epic.js +8 -0
- package/lib/esm/entity/invoicing/invoicingCommonPayload.js +7 -0
- package/lib/esm/entity/snackbar/snackbarTypes.js +1 -0
- package/lib/esm/entity/tenant/tenantReducer.js +1 -0
- package/lib/esm/entity/transaction/stateTypes/paymentType.js +9 -1
- package/lib/esm/entity/transaction/stateTypes/printStatus.js +7 -0
- package/lib/esm/epic.js +8 -0
- package/lib/esm/index.js +5 -2
- package/lib/esm/view/invoicing/invoicingQboView/invoicingQboViewSelector.js +20 -12
- package/lib/esm/view/invoicing/settingsView/invoicingQboSettingsFormConfig.js +6 -1
- package/lib/esm/view/invoicing/settingsView/settingsViewReducer.js +19 -1
- package/lib/esm/view/invoicing/settingsView/settingsViewSelector.js +15 -1
- package/lib/esm/view/transactionDetail/createTransaction/createTransactionLocalDataHelper.js +157 -0
- package/lib/esm/view/transactionDetail/createTransaction/createTransactionPayload.js +164 -0
- package/lib/esm/view/transactionDetail/createTransaction/createTransactionSelector.js +47 -0
- package/lib/esm/view/transactionDetail/createTransaction/createTransactionState.js +11 -0
- package/lib/esm/view/transactionDetail/createTransaction/epics/createTransactionAttachmentUploadFailureEpic.js +19 -0
- package/lib/esm/view/transactionDetail/createTransaction/epics/createTransactionEpic.js +81 -0
- package/lib/esm/view/transactionDetail/createTransaction/epics/fetchVendorRecommendationsForCreateTransactionEpic.js +53 -0
- package/lib/esm/view/transactionDetail/createTransaction/epics/initializeCreateTransactionLocalDataEpic.js +10 -0
- package/lib/esm/view/transactionDetail/transactionDetailReducer.js +196 -1
- package/lib/index.d.ts +6 -2
- package/lib/index.js +58 -37
- package/lib/view/expenseAutomationView/helpers/transactionCategorizationLocalDataHelper.d.ts +1 -1
- package/lib/view/invoicing/invoicingQboView/invoicingQboViewSelector.d.ts +6 -1
- package/lib/view/invoicing/invoicingQboView/invoicingQboViewSelector.js +20 -12
- package/lib/view/invoicing/settingsView/invoicingQboSettingsFormConfig.js +5 -0
- package/lib/view/invoicing/settingsView/settingsViewReducer.js +19 -1
- package/lib/view/invoicing/settingsView/settingsViewSelector.d.ts +3 -1
- package/lib/view/invoicing/settingsView/settingsViewSelector.js +15 -1
- package/lib/view/transactionDetail/createTransaction/createTransactionLocalDataHelper.d.ts +56 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionLocalDataHelper.js +172 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionPayload.d.ts +95 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionPayload.js +169 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionSelector.d.ts +44 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionSelector.js +53 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionState.d.ts +82 -0
- package/lib/view/transactionDetail/createTransaction/createTransactionState.js +14 -0
- package/lib/view/transactionDetail/createTransaction/epics/createTransactionAttachmentUploadFailureEpic.d.ts +10 -0
- package/lib/view/transactionDetail/createTransaction/epics/createTransactionAttachmentUploadFailureEpic.js +23 -0
- package/lib/view/transactionDetail/createTransaction/epics/createTransactionEpic.d.ts +9 -0
- package/lib/view/transactionDetail/createTransaction/epics/createTransactionEpic.js +85 -0
- package/lib/view/transactionDetail/createTransaction/epics/fetchVendorRecommendationsForCreateTransactionEpic.d.ts +20 -0
- package/lib/view/transactionDetail/createTransaction/epics/fetchVendorRecommendationsForCreateTransactionEpic.js +57 -0
- package/lib/view/transactionDetail/createTransaction/epics/initializeCreateTransactionLocalDataEpic.d.ts +12 -0
- package/lib/view/transactionDetail/createTransaction/epics/initializeCreateTransactionLocalDataEpic.js +14 -0
- package/lib/view/transactionDetail/transactionDetailReducer.d.ts +39 -1
- package/lib/view/transactionDetail/transactionDetailReducer.js +198 -2
- package/lib/view/transactionDetail/transactionDetailState.d.ts +12 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createSlice } from '@reduxjs/toolkit';
|
|
2
|
+
import { INVOICING_QBO_SALES_TAX_POSTING_ACCOUNT_SLOT, } from '../../../entity/invoicing/invoicingCommonPayload';
|
|
2
3
|
import { clearAll } from '../../../rootActions';
|
|
3
4
|
import { entityToInvoicingQboSettingsLocalData, } from './invoicingQboSettingsFormConfig';
|
|
4
5
|
export const initialState = {
|
|
@@ -40,7 +41,24 @@ const settingsView = createSlice({
|
|
|
40
41
|
saveInvoicingSettings(draft, action) {
|
|
41
42
|
draft.saveFetchState = { fetchState: 'In-Progress', error: undefined };
|
|
42
43
|
if (draft.settings != null) {
|
|
43
|
-
|
|
44
|
+
// The display-only sales tax posting account is never in a save body,
|
|
45
|
+
// so carry it over rather than blank it until the response lands.
|
|
46
|
+
const postingAccount = draft.settings.qbo?.account_mappings?.[INVOICING_QBO_SALES_TAX_POSTING_ACCOUNT_SLOT];
|
|
47
|
+
const settings = { ...draft.settings, ...action.payload.settings };
|
|
48
|
+
const accountMappings = settings.qbo?.account_mappings;
|
|
49
|
+
draft.settings =
|
|
50
|
+
postingAccount != null && accountMappings != null
|
|
51
|
+
? {
|
|
52
|
+
...settings,
|
|
53
|
+
qbo: {
|
|
54
|
+
...settings.qbo,
|
|
55
|
+
account_mappings: {
|
|
56
|
+
...accountMappings,
|
|
57
|
+
[INVOICING_QBO_SALES_TAX_POSTING_ACCOUNT_SLOT]: postingAccount,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
: settings;
|
|
44
62
|
}
|
|
45
63
|
},
|
|
46
64
|
saveInvoicingSettingsSuccess(draft, action) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getInvoicingQboAccountMappingId, } from '../../../entity/invoicing/invoicingCommonPayload';
|
|
2
1
|
import { getAddressByAddressId } from '../../../entity/address/addressSelector';
|
|
3
2
|
import { getFileByFileId } from '../../../entity/file/fileSelector';
|
|
3
|
+
import { INVOICING_QBO_SALES_TAX_POSTING_ACCOUNT_SLOT, getInvoicingQboAccountMappingId, } from '../../../entity/invoicing/invoicingCommonPayload';
|
|
4
4
|
import { getFormattedAddress } from '../../addressView/addressViewSelector';
|
|
5
5
|
import { getInvoicingConfigView, invoicingFieldHelpForGroup, } from '../invoicingConfigView/invoicingConfigViewSelector';
|
|
6
6
|
import { getInvoicingQboView, isInvoicingAccountingPickerDataLoading, } from '../invoicingQboView/invoicingQboViewSelector';
|
|
@@ -102,6 +102,19 @@ export const getInvoicingSettingsHubView = (state) => {
|
|
|
102
102
|
status,
|
|
103
103
|
};
|
|
104
104
|
};
|
|
105
|
+
/**
|
|
106
|
+
* The name of the account the service posts sales tax to. Read from the whole
|
|
107
|
+
* account the settings carry, or looked up by id in the account list for a
|
|
108
|
+
* payload that sends only the id.
|
|
109
|
+
*/
|
|
110
|
+
const invoicingSalesTaxPostingAccountName = (accountMappings, accounts) => {
|
|
111
|
+
const slot = accountMappings?.[INVOICING_QBO_SALES_TAX_POSTING_ACCOUNT_SLOT];
|
|
112
|
+
const name = typeof slot === 'string'
|
|
113
|
+
? accounts.find((account) => account.id === slot)?.name
|
|
114
|
+
: slot?.account_name;
|
|
115
|
+
const trimmed = name?.trim() ?? '';
|
|
116
|
+
return trimmed !== '' ? trimmed : undefined;
|
|
117
|
+
};
|
|
105
118
|
/**
|
|
106
119
|
* Bundled view for the QBO configuration page: the draft it edits, the accounts
|
|
107
120
|
* and option lists its dropdowns render, the connection and sync health it
|
|
@@ -135,6 +148,7 @@ export const getInvoicingQboSettingsFormView = (state) => {
|
|
|
135
148
|
localData: qboDraft ?? initialInvoicingQboSettingsLocalData,
|
|
136
149
|
nestedAccountHierarchy: qboView.nestedAccountHierarchy,
|
|
137
150
|
retryFetchStateByTaskId: qboView.retryFetchStateByTaskId,
|
|
151
|
+
salesTaxPostingAccountName: invoicingSalesTaxPostingAccountName(settings?.qbo?.account_mappings, qboView.accounts),
|
|
138
152
|
saveState: saveFetchState.fetchState === 'Not-Started' && error != null
|
|
139
153
|
? { fetchState, error }
|
|
140
154
|
: saveFetchState,
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { dateNow } from '../../../zeniDayJS';
|
|
2
|
+
import { ALL_CREATABLE_TRANSACTION_TYPES, } from './createTransactionState';
|
|
3
|
+
/**
|
|
4
|
+
* Monotonic counter for line row keys. These never leave the client -- the payload builder
|
|
5
|
+
* drops them -- so a counter is enough, and it keeps reducer tests deterministic in a way
|
|
6
|
+
* randomUUID would not.
|
|
7
|
+
*/
|
|
8
|
+
let lineIdCounter = 0;
|
|
9
|
+
export const nextCreateTransactionLineId = () => `create-transaction-line-${lineIdCounter++}`;
|
|
10
|
+
/** Test-only: makes line ids predictable from the start of each case. */
|
|
11
|
+
export const resetCreateTransactionLineIdCounter = () => {
|
|
12
|
+
lineIdCounter = 0;
|
|
13
|
+
};
|
|
14
|
+
export const createEmptyCreateTransactionLine = (postingType) => ({
|
|
15
|
+
id: nextCreateTransactionLineId(),
|
|
16
|
+
postingType,
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* The lines a freshly-opened form starts with. A journal entry is invalid with fewer than one
|
|
20
|
+
* debit and one credit, so it opens with both rather than making the user add the second row.
|
|
21
|
+
* Transfer has no lines at all.
|
|
22
|
+
*/
|
|
23
|
+
export const getDefaultCreateTransactionLines = (transactionType) => {
|
|
24
|
+
if (transactionType === 'transfer') {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
if (transactionType === 'journal_entry') {
|
|
28
|
+
return [
|
|
29
|
+
createEmptyCreateTransactionLine('debit'),
|
|
30
|
+
createEmptyCreateTransactionLine('credit'),
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
return [createEmptyCreateTransactionLine()];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A blank working copy for one type. Note this does NOT seed the date -- the reducer's initial
|
|
37
|
+
* state has to stay a pure constant so `clearAll` restores something stable. The date is seeded
|
|
38
|
+
* by initializeCreateTransactionLocalDataEpic, which runs when the form opens.
|
|
39
|
+
*/
|
|
40
|
+
export const getEmptyCreateTransactionLocalData = (transactionType) => ({
|
|
41
|
+
lines: [],
|
|
42
|
+
transactionType,
|
|
43
|
+
});
|
|
44
|
+
export const getInitialCreateTransactionLocalDataByType = () => ALL_CREATABLE_TRANSACTION_TYPES.reduce((acc, transactionType) => {
|
|
45
|
+
acc[transactionType] =
|
|
46
|
+
getEmptyCreateTransactionLocalData(transactionType);
|
|
47
|
+
return acc;
|
|
48
|
+
}, {});
|
|
49
|
+
/**
|
|
50
|
+
* The payment_type the backend insists on for some types: a check needs `check`, and a
|
|
51
|
+
* credit_card_credit (sent as expense + is_credit) needs a card type -- qbo_purchase.py raises
|
|
52
|
+
* without them.
|
|
53
|
+
*/
|
|
54
|
+
const SEEDED_PAYMENT_TYPE = {
|
|
55
|
+
check: 'check',
|
|
56
|
+
credit_card_credit: 'credit_card',
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* What the form opens with: a blank copy plus today's date, the default line set, and the
|
|
60
|
+
* payment_type the backend insists on for the type.
|
|
61
|
+
*/
|
|
62
|
+
export const getSeededCreateTransactionLocalData = (transactionType) => ({
|
|
63
|
+
...getEmptyCreateTransactionLocalData(transactionType),
|
|
64
|
+
lines: getDefaultCreateTransactionLines(transactionType),
|
|
65
|
+
paymentType: SEEDED_PAYMENT_TYPE[transactionType],
|
|
66
|
+
transactionDate: dateNow(),
|
|
67
|
+
});
|
|
68
|
+
/**
|
|
69
|
+
* Merges a partial edit into the working copy. Lines are replaced wholesale when present --
|
|
70
|
+
* a partial line array would silently truncate the form.
|
|
71
|
+
*/
|
|
72
|
+
export const mergeCreateTransactionLocalData = (current, update) => ({
|
|
73
|
+
...current,
|
|
74
|
+
...update,
|
|
75
|
+
lines: update.lines ?? current.lines,
|
|
76
|
+
// The type is keyed by the slice, never edited through a merge.
|
|
77
|
+
transactionType: current.transactionType,
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* A user edit to a prefilled account or class makes it theirs, so its flag is dropped and a
|
|
81
|
+
* later vendor switch no longer clears it.
|
|
82
|
+
*/
|
|
83
|
+
export const updateCreateTransactionLine = (lines, lineId, update) => lines.map((line) => line.id === lineId
|
|
84
|
+
? {
|
|
85
|
+
...line,
|
|
86
|
+
...update,
|
|
87
|
+
id: line.id,
|
|
88
|
+
isAccountPreFilled: 'accountQboId' in update ? undefined : line.isAccountPreFilled,
|
|
89
|
+
isClassPreFilled: 'classId' in update ? undefined : line.isClassPreFilled,
|
|
90
|
+
}
|
|
91
|
+
: line);
|
|
92
|
+
/**
|
|
93
|
+
* Recommendation -> RecommendationWithCOT. Chain-of-thought and recommendation feedback both
|
|
94
|
+
* need an existing transaction, so cot/reasoning are empty here by design; the dropdown only
|
|
95
|
+
* needs the entities to build its "Recommended" group.
|
|
96
|
+
*/
|
|
97
|
+
export const toRecommendationsWithCOT = (recommendations) => recommendations.map((recommendation) => ({
|
|
98
|
+
account: recommendation.account != null
|
|
99
|
+
? { ...recommendation.account, cot: [], reasoning: '' }
|
|
100
|
+
: undefined,
|
|
101
|
+
class: recommendation.class != null
|
|
102
|
+
? { ...recommendation.class, cot: [], reasoning: '' }
|
|
103
|
+
: undefined,
|
|
104
|
+
vendor: recommendation.vendor != null
|
|
105
|
+
? { ...recommendation.vendor, cot: [], reasoning: '' }
|
|
106
|
+
: undefined,
|
|
107
|
+
}));
|
|
108
|
+
/**
|
|
109
|
+
* Applies a vendor's recommendations to one line: always refreshes the dropdown's "Recommended"
|
|
110
|
+
* group, but only prefills account/class when the field is still empty. Callers switching
|
|
111
|
+
* vendors clear the previous prefill first, so "empty" includes what the last vendor filled. Anything the user
|
|
112
|
+
* picked wins -- this follows the transaction-categorization helper, which prefills, rather
|
|
113
|
+
* than the detail-page helper, which only suggests.
|
|
114
|
+
*/
|
|
115
|
+
export const applyRecommendationsToCreateTransactionLine = (line, recommendations, isAccountingClassesEnabled) => {
|
|
116
|
+
const top = recommendations[0];
|
|
117
|
+
const updated = {
|
|
118
|
+
...line,
|
|
119
|
+
recommendationsWithCOT: toRecommendationsWithCOT(recommendations),
|
|
120
|
+
};
|
|
121
|
+
if (line.accountQboId == null && top?.account?.qboId != null) {
|
|
122
|
+
updated.accountQboId = top.account.qboId;
|
|
123
|
+
updated.accountName = top.account.accountName;
|
|
124
|
+
updated.isAccountPreFilled = true;
|
|
125
|
+
}
|
|
126
|
+
if (isAccountingClassesEnabled &&
|
|
127
|
+
line.classId == null &&
|
|
128
|
+
top?.class?.classId != null) {
|
|
129
|
+
updated.classId = top.class.classId;
|
|
130
|
+
updated.className = top.class.className;
|
|
131
|
+
updated.isClassPreFilled = true;
|
|
132
|
+
}
|
|
133
|
+
return updated;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Undoes a prefill when the vendor is cleared. Only the fields the prefill wrote are removed --
|
|
137
|
+
* the per-field flags are what make the user's own choices distinguishable from ours, which is
|
|
138
|
+
* why they are not a single line-level boolean: a hand-picked account alongside a prefilled
|
|
139
|
+
* class is an ordinary state, and clearing must not take the account with it.
|
|
140
|
+
*/
|
|
141
|
+
export const clearRecommendationsFromCreateTransactionLine = (line) => {
|
|
142
|
+
const cleared = {
|
|
143
|
+
...line,
|
|
144
|
+
recommendationsWithCOT: undefined,
|
|
145
|
+
};
|
|
146
|
+
if (line.isAccountPreFilled === true) {
|
|
147
|
+
cleared.accountQboId = undefined;
|
|
148
|
+
cleared.accountName = undefined;
|
|
149
|
+
cleared.isAccountPreFilled = undefined;
|
|
150
|
+
}
|
|
151
|
+
if (line.isClassPreFilled === true) {
|
|
152
|
+
cleared.classId = undefined;
|
|
153
|
+
cleared.className = undefined;
|
|
154
|
+
cleared.isClassPreFilled = undefined;
|
|
155
|
+
}
|
|
156
|
+
return cleared;
|
|
157
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { DEFAULT_DATE_FORMAT } from '../../../commonStateTypes/timePeriod';
|
|
2
|
+
import { getAccountBaseForTransferAccounts } from '../../../entity/account/accountSelector';
|
|
3
|
+
import { getClassById } from '../../../entity/class/classSelector';
|
|
4
|
+
import { toProjectBasePayload, } from '../../../entity/project/projectPayload';
|
|
5
|
+
import { getProjectById } from '../../../entity/project/projectSelector';
|
|
6
|
+
import { getVendorByQboId, getVendorByVendorId, } from '../../../entity/vendor/vendorSelector';
|
|
7
|
+
const toRemitToAddressPayload = (address) => {
|
|
8
|
+
if (address == null) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
line1: address.line1,
|
|
13
|
+
city: address.city,
|
|
14
|
+
state: address.state,
|
|
15
|
+
postal_code: address.postalCode,
|
|
16
|
+
country: address.country,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const toLinePayload = (state, line, transactionType) => {
|
|
20
|
+
const lineDetail = {};
|
|
21
|
+
if (line.accountQboId != null) {
|
|
22
|
+
lineDetail.account = {
|
|
23
|
+
qbo_id: line.accountQboId,
|
|
24
|
+
account_name: line.accountName,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// An empty object is what the confirmed payloads send when no class is chosen; sending the
|
|
28
|
+
// key with a qbo_id only once one is picked.
|
|
29
|
+
const classQboId = toQboClassId(state, line.classId);
|
|
30
|
+
lineDetail.class = classQboId != null ? { qbo_id: classQboId } : {};
|
|
31
|
+
if (line.projectId != null) {
|
|
32
|
+
lineDetail.project = toProjectBasePayload(getProjectById(state.projectState, line.projectId) ?? {
|
|
33
|
+
projectId: line.projectId,
|
|
34
|
+
projectName: '',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (transactionType === 'journal_entry') {
|
|
38
|
+
lineDetail.posting_type = line.postingType;
|
|
39
|
+
const vendorQboId = toQboLineVendorId(state, line.vendorQboId);
|
|
40
|
+
if (vendorQboId != null) {
|
|
41
|
+
lineDetail.vendor = { qbo_id: vendorQboId };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
// The backend rejects a bool here, so this is always coerced to a number.
|
|
46
|
+
amount: line.amount?.amount ?? 0,
|
|
47
|
+
line_description: line.lineDescription,
|
|
48
|
+
line_detail_type: transactionType === 'journal_entry'
|
|
49
|
+
? 'journal_entry_line_detail'
|
|
50
|
+
: 'account_based_expense_line_detail',
|
|
51
|
+
line_detail: lineDetail,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Thrown when a picked id has no QBO id to send. Sending the Zeni id instead would reach QBO as
|
|
56
|
+
* an unknown reference and come back as an opaque error, so the create is stopped here and the
|
|
57
|
+
* epic shows this message instead.
|
|
58
|
+
*/
|
|
59
|
+
export class UnresolvedCreateTransactionIdError extends Error {
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Resolves a transfer account (the Zeni account id from the transfer pools) to its QBO id. The
|
|
63
|
+
* header and line account dropdowns already hold the QBO id, so they are sent as-is.
|
|
64
|
+
*/
|
|
65
|
+
const toQboTransferAccountId = (state, accountId) => {
|
|
66
|
+
if (accountId == null) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const qboId = getAccountBaseForTransferAccounts(state.accountState, accountId)?.qboId;
|
|
70
|
+
if (qboId == null || qboId === '') {
|
|
71
|
+
throw new UnresolvedCreateTransactionIdError('The selected transfer account could not be found in QuickBooks.');
|
|
72
|
+
}
|
|
73
|
+
return qboId;
|
|
74
|
+
};
|
|
75
|
+
/** Resolves a Zeni class id (`class_<uuid>`) to the QBO id `line_detail.class.qbo_id` expects. */
|
|
76
|
+
const toQboClassId = (state, classId) => {
|
|
77
|
+
if (classId == null) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const qboId = getClassById(state.classState, {
|
|
81
|
+
classId,
|
|
82
|
+
reportId: 'class_list',
|
|
83
|
+
})?.qboId;
|
|
84
|
+
if (qboId == null || qboId === '') {
|
|
85
|
+
throw new UnresolvedCreateTransactionIdError('The selected class could not be found in QuickBooks.');
|
|
86
|
+
}
|
|
87
|
+
return qboId;
|
|
88
|
+
};
|
|
89
|
+
/** A vendor not yet synced to QBO carries `qboId === name`, which QBO would reject. */
|
|
90
|
+
const toSyncedVendorQboId = (vendor) => {
|
|
91
|
+
if (vendor == null || vendor.qboId === '' || vendor.qboId === vendor.name) {
|
|
92
|
+
throw new UnresolvedCreateTransactionIdError('The selected vendor is not synced to QuickBooks yet.');
|
|
93
|
+
}
|
|
94
|
+
return vendor.qboId;
|
|
95
|
+
};
|
|
96
|
+
/** Resolves a Zeni vendor id to its QBO id -- `vendor_id` on the wire is a QBO id. */
|
|
97
|
+
const toQboVendorId = (state, vendorId) => vendorId == null
|
|
98
|
+
? undefined
|
|
99
|
+
: toSyncedVendorQboId(getVendorByVendorId(state.vendorState, vendorId));
|
|
100
|
+
/** A journal entry line holds the QBO id already, so it is only checked against the store. */
|
|
101
|
+
const toQboLineVendorId = (state, vendorQboId) => vendorQboId == null
|
|
102
|
+
? undefined
|
|
103
|
+
: toSyncedVendorQboId(getVendorByQboId(state.vendorState, vendorQboId));
|
|
104
|
+
/**
|
|
105
|
+
* Builds the request body for one transaction type. One branch per type rather than a shared
|
|
106
|
+
* body with optional keys, because the types genuinely diverge -- transfer carries no lines at
|
|
107
|
+
* all, and journal_entry carries no header account.
|
|
108
|
+
*
|
|
109
|
+
* Throws UnresolvedCreateTransactionIdError when a picked vendor (header or journal entry line),
|
|
110
|
+
* class or transfer account has no QBO id.
|
|
111
|
+
*/
|
|
112
|
+
export const prepareCreateTransactionPayload = (state, localData) => {
|
|
113
|
+
const { transactionType } = localData;
|
|
114
|
+
const base = {
|
|
115
|
+
transaction_type: transactionType,
|
|
116
|
+
transaction_date: localData.transactionDate?.format(DEFAULT_DATE_FORMAT) ?? '',
|
|
117
|
+
transaction_memo: localData.transactionMemo,
|
|
118
|
+
};
|
|
119
|
+
if (transactionType === 'transfer') {
|
|
120
|
+
return {
|
|
121
|
+
...base,
|
|
122
|
+
from_account_id: toQboTransferAccountId(state, localData.fromAccountId),
|
|
123
|
+
to_account_id: toQboTransferAccountId(state, localData.toAccountId),
|
|
124
|
+
total_amount: localData.totalAmount?.amount ?? 0,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const lines = localData.lines.map((line) => toLinePayload(state, line, transactionType));
|
|
128
|
+
if (transactionType === 'journal_entry') {
|
|
129
|
+
return {
|
|
130
|
+
...base,
|
|
131
|
+
document_id: localData.documentId,
|
|
132
|
+
lines,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const purchaseBase = {
|
|
136
|
+
...base,
|
|
137
|
+
payment_type: localData.paymentType,
|
|
138
|
+
qbo_account_id: localData.qboAccountId,
|
|
139
|
+
vendor_id: toQboVendorId(state, localData.vendorId),
|
|
140
|
+
lines,
|
|
141
|
+
};
|
|
142
|
+
if (transactionType === 'check') {
|
|
143
|
+
return {
|
|
144
|
+
...purchaseBase,
|
|
145
|
+
document_number: localData.documentNumber,
|
|
146
|
+
print_status: localData.printStatus,
|
|
147
|
+
remit_to_address: toRemitToAddressPayload(localData.remitToAddress),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (transactionType === 'credit_card_credit') {
|
|
151
|
+
// credit_card_credit is not in the backend's SUPPORTED_TRANSACTION_WRITE_TYPES. QBO models
|
|
152
|
+
// it as a Purchase with Credit: true and qbo_purchase.py already honours is_credit for
|
|
153
|
+
// card payment types, so this sends `expense` + is_credit rather than a type the validator
|
|
154
|
+
// rejects outright. Confirm with backend before enabling the menu item. The seed sets
|
|
155
|
+
// payment_type to credit_card, since qbo_purchase.py raises on is_credit without a card type.
|
|
156
|
+
return {
|
|
157
|
+
...purchaseBase,
|
|
158
|
+
transaction_type: 'expense',
|
|
159
|
+
is_credit: true,
|
|
160
|
+
document_number: localData.documentNumber,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
return purchaseBase;
|
|
164
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createSelector } from '@reduxjs/toolkit';
|
|
2
|
+
import { getNestedAccountListHierarchy } from '../../accountList/accountListSelector';
|
|
3
|
+
import { getNestedClassListHierarchy } from '../../classList/classListSelector';
|
|
4
|
+
import { getResolvedTransferAccounts } from '../../createTransferEntry/createTransferEntrySelector';
|
|
5
|
+
import { getRecommendationsForEntity } from '../../recommendation/recommendationSelector';
|
|
6
|
+
export const getCreateTransactionLocalData = (state, transactionType) => state.transactionDetailState.createTransactionsState.newTransactionState[transactionType];
|
|
7
|
+
export const getCreateTransactionStatus = (state) => state.transactionDetailState.createTransactionsState.createStatus;
|
|
8
|
+
/**
|
|
9
|
+
* The vendor recommendations backing the category/class dropdowns on the create page.
|
|
10
|
+
*
|
|
11
|
+
* No new state field is needed for this: getRecommendationsForEntity already returns
|
|
12
|
+
* fetchState alongside recommendations, so composing it here is enough for the page to show a
|
|
13
|
+
* line skeleton and hold Save while a fetch is in flight.
|
|
14
|
+
*/
|
|
15
|
+
export const getCreateTransactionVendorRecommendations = (state, vendorQboId) => {
|
|
16
|
+
if (vendorQboId == null || vendorQboId === '') {
|
|
17
|
+
return { fetchState: 'Not-Started' };
|
|
18
|
+
}
|
|
19
|
+
const view = getRecommendationsForEntity(state, vendorQboId, 'vendor');
|
|
20
|
+
return {
|
|
21
|
+
fetchState: view.fetchState,
|
|
22
|
+
recommendations: view.recommendations,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Memoized: app-ui reads this straight from useSelector, and the vendor list and hierarchies are
|
|
27
|
+
* rebuilt on every call, so an unmemoized version re-renders the page on every store action.
|
|
28
|
+
*/
|
|
29
|
+
export const getCreateTransactionView = createSelector([
|
|
30
|
+
(state) => state.transactionDetailState.createTransactionsState,
|
|
31
|
+
(state) => state.vendorState.vendorByID,
|
|
32
|
+
(state) => state.accountListState,
|
|
33
|
+
(state) => state.classListState,
|
|
34
|
+
(state) => getResolvedTransferAccounts(state).transferAccountsList,
|
|
35
|
+
(_state, transactionType) => transactionType,
|
|
36
|
+
], (createTransactionsState, vendorByID, accountListState, classListState, transferAccountsList, transactionType) => ({
|
|
37
|
+
transactionType,
|
|
38
|
+
localData: createTransactionsState.newTransactionState[transactionType],
|
|
39
|
+
createStatus: createTransactionsState.createStatus,
|
|
40
|
+
createdTransactionId: createTransactionsState.createdTransactionId,
|
|
41
|
+
vendorList: Object.values(vendorByID),
|
|
42
|
+
accountListNestedAccountHierarchy: getNestedAccountListHierarchy(accountListState, 'accountList'),
|
|
43
|
+
classListNestedAccountHierarchy: getNestedClassListHierarchy(classListState),
|
|
44
|
+
transferAccountsList: transactionType === 'transfer' ? transferAccountsList : [],
|
|
45
|
+
fetchState: createTransactionsState.createStatus.fetchState,
|
|
46
|
+
error: createTransactionsState.createStatus.error,
|
|
47
|
+
}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transaction types the Create Transaction flow can originate. A narrow subset of
|
|
3
|
+
* TransactionType -- everything else is read-only and arrives via sync.
|
|
4
|
+
*/
|
|
5
|
+
export const ALL_CREATABLE_TRANSACTION_TYPES = [
|
|
6
|
+
'expense',
|
|
7
|
+
'check',
|
|
8
|
+
'credit_card_credit',
|
|
9
|
+
'transfer',
|
|
10
|
+
'journal_entry',
|
|
11
|
+
];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { filter, map } from 'rxjs/operators';
|
|
2
|
+
import { openSnackbar } from '../../../../entity/snackbar/snackbarReducer';
|
|
3
|
+
import { createTransactionAttachmentUploadFailure } from '../../transactionDetailReducer';
|
|
4
|
+
/**
|
|
5
|
+
* Attachments upload from the page after the create succeeds, so a failure is reported back here
|
|
6
|
+
* rather than by createTransactionEpic.
|
|
7
|
+
*/
|
|
8
|
+
export const createTransactionAttachmentUploadFailureEpic = (actions$) => actions$.pipe(filter(createTransactionAttachmentUploadFailure.match), filter((action) => action.payload.failedAttachmentFileNames.length > 0), map((action) => openSnackbar({
|
|
9
|
+
messageSection: 'missing_receipts_attachment',
|
|
10
|
+
messageText: 'failed',
|
|
11
|
+
type: 'error',
|
|
12
|
+
showStatusIcon: true,
|
|
13
|
+
variables: [
|
|
14
|
+
{
|
|
15
|
+
variableName: '__message__',
|
|
16
|
+
variableValue: ` - ${action.payload.failedAttachmentFileNames.join(', ')},`,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
})));
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { defer, from } from 'rxjs';
|
|
2
|
+
import { catchError, exhaustMap, filter, mergeMap } from 'rxjs/operators';
|
|
3
|
+
import { openSnackbar } from '../../../../entity/snackbar/snackbarReducer';
|
|
4
|
+
import { toTransactionID } from '../../../../entity/transaction/payloadTypes/transactionIDPayload';
|
|
5
|
+
import { updateTransaction } from '../../../../entity/transaction/transactionReducer';
|
|
6
|
+
import { createZeniAPIStatus, isSuccessResponse, } from '../../../../responsePayload';
|
|
7
|
+
import { createTransaction, createTransactionSuccessOrFailure, initializeTransactionDetailLocalData, } from '../../transactionDetailReducer';
|
|
8
|
+
import { getTransactionDetailKey } from '../../transactionDetailState';
|
|
9
|
+
import { UnresolvedCreateTransactionIdError, prepareCreateTransactionPayload, } from '../createTransactionPayload';
|
|
10
|
+
export const createTransactionEpic = (actions$, state$, zeniAPI) => actions$.pipe(filter(createTransaction.match),
|
|
11
|
+
// exhaustMap, not mergeMap: a second submit while one is in flight is dropped here rather
|
|
12
|
+
// than relying only on the disabled Save button, so it cannot create a duplicate in QBO.
|
|
13
|
+
exhaustMap((action) => {
|
|
14
|
+
const { transactionType } = action.payload;
|
|
15
|
+
const state = state$.value;
|
|
16
|
+
const localData = state.transactionDetailState.createTransactionsState
|
|
17
|
+
.newTransactionState[transactionType];
|
|
18
|
+
// Deferred so a payload that cannot be built (an id with no QBO id) lands in catchError
|
|
19
|
+
// below as a failure, instead of throwing out of the epic.
|
|
20
|
+
return defer(() => zeniAPI.postAndGetJSON(`${zeniAPI.apiEndPoints.accountMicroServiceBaseUrl}/1.0/transactions/manage`, prepareCreateTransactionPayload(state, localData))).pipe(mergeMap((response) => {
|
|
21
|
+
const created = isSuccessResponse(response)
|
|
22
|
+
? response.data?.transactions[0]
|
|
23
|
+
: undefined;
|
|
24
|
+
if (created == null) {
|
|
25
|
+
const status = isSuccessResponse(response)
|
|
26
|
+
? createZeniAPIStatus('Unexpected Error', 'Transaction was created but returned no details.')
|
|
27
|
+
: response.status;
|
|
28
|
+
return from([
|
|
29
|
+
createTransactionSuccessOrFailure('Error', undefined, status),
|
|
30
|
+
openSnackbar({
|
|
31
|
+
messageSection: 'create_transaction',
|
|
32
|
+
messageText: 'failed',
|
|
33
|
+
type: 'error',
|
|
34
|
+
// This variable is how the server's own message reaches the user. Per-type
|
|
35
|
+
// required fields live in the QBO entity classes, so a rejection comes back as a
|
|
36
|
+
// QBO error with no field name -- the message text is the only useful content.
|
|
37
|
+
variables: [
|
|
38
|
+
{ variableName: '_api-error_', variableValue: status.message },
|
|
39
|
+
],
|
|
40
|
+
}),
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
const newTransactionId = toTransactionID(created);
|
|
44
|
+
// The write is QBO-only: transaction_write_handler.py touches no local storage, and
|
|
45
|
+
// Zeni's mirror is populated by the delta sync that runs afterwards. Refetching here
|
|
46
|
+
// would come back without the new row, so the 200 body is inserted optimistically
|
|
47
|
+
// instead -- the same approach createTransferEntryEpic takes.
|
|
48
|
+
return from([
|
|
49
|
+
updateTransaction(getTransactionDetailKey(newTransactionId), created),
|
|
50
|
+
// No reset here: it would clear createdTransactionId in the same tick, before the
|
|
51
|
+
// screen renders it to upload pending attachments and navigate. The screen resets
|
|
52
|
+
// the working copy on unmount instead.
|
|
53
|
+
// Before the local-data init: it seeds the detail entry that init writes into.
|
|
54
|
+
createTransactionSuccessOrFailure('Completed', newTransactionId),
|
|
55
|
+
initializeTransactionDetailLocalData({
|
|
56
|
+
transactionId: newTransactionId,
|
|
57
|
+
vendorId: created.vendor_id ?? undefined,
|
|
58
|
+
}),
|
|
59
|
+
openSnackbar({
|
|
60
|
+
messageSection: 'create_transaction',
|
|
61
|
+
messageText: 'success',
|
|
62
|
+
type: 'success',
|
|
63
|
+
}),
|
|
64
|
+
]);
|
|
65
|
+
}), catchError((error) => {
|
|
66
|
+
const message = error instanceof UnresolvedCreateTransactionIdError
|
|
67
|
+
? error.message
|
|
68
|
+
: JSON.stringify(error);
|
|
69
|
+
return from([
|
|
70
|
+
createTransactionSuccessOrFailure('Error', undefined, createZeniAPIStatus('Unexpected Error', `Create Transaction REST API call errored out ${message}`)),
|
|
71
|
+
openSnackbar({
|
|
72
|
+
messageSection: 'create_transaction',
|
|
73
|
+
messageText: 'failed',
|
|
74
|
+
type: 'error',
|
|
75
|
+
variables: [
|
|
76
|
+
{ variableName: '_api-error_', variableValue: message },
|
|
77
|
+
],
|
|
78
|
+
}),
|
|
79
|
+
]);
|
|
80
|
+
}));
|
|
81
|
+
}));
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { concat, from, of } from 'rxjs';
|
|
2
|
+
import { filter, mergeMap, switchMap, take } from 'rxjs/operators';
|
|
3
|
+
import { getIsAccountingClassesEnabled } from '../../../../entity/tenant/tenantSelector';
|
|
4
|
+
import { fetchRecommendationByEntityId, fetchRecommendationByEntityName, indicateReadyToInitializeRecommendation, } from '../../../recommendation/recommendationReducer';
|
|
5
|
+
import { getRecommendationsForEntity } from '../../../recommendation/recommendationSelector';
|
|
6
|
+
import { clearRecommendationsForCreateTransactionLines, fetchVendorRecommendationsForCreateTransaction, setRecommendationsForCreateTransactionLines, } from '../../transactionDetailReducer';
|
|
7
|
+
/**
|
|
8
|
+
* The entity-level recommendation fetch, keyed by whatever identifier the endpoint can resolve.
|
|
9
|
+
*
|
|
10
|
+
* A vendor that has not synced to QBO carries `qboId === name`; the id endpoint has nothing to
|
|
11
|
+
* look up in that case, so the by-name variant is used instead.
|
|
12
|
+
*/
|
|
13
|
+
const toEntityIdOrName = (entity) => entity.qboId && entity.qboId !== entity.name ? entity.qboId : entity.name;
|
|
14
|
+
const isUnsyncedVendor = (entity) => !entity.qboId || entity.qboId === entity.name;
|
|
15
|
+
/**
|
|
16
|
+
* On vendor select, populates the category/class dropdowns from that vendor's recommendations.
|
|
17
|
+
*
|
|
18
|
+
* Deliberately NOT the detail page's fetchEntityRecommendationsForLineUpdate: that one sends
|
|
19
|
+
* transaction_id / line_ids and writes into transactionDetailById[key], which no-ops when the
|
|
20
|
+
* entry does not exist. A transaction being created has neither.
|
|
21
|
+
*
|
|
22
|
+
* Two differences from the reimbursement epic this is modelled on:
|
|
23
|
+
* 1. the wait filters on the matching entityIdOrName before take(1), so an unrelated
|
|
24
|
+
* recommendation landing first cannot satisfy this request, and
|
|
25
|
+
* 2. switchMap, so changing the vendor again cancels the earlier wait rather than letting a
|
|
26
|
+
* stale result overwrite the newer vendor's prefill.
|
|
27
|
+
*/
|
|
28
|
+
export const fetchVendorRecommendationsForCreateTransactionEpic = (actions$, state$) => actions$.pipe(filter(fetchVendorRecommendationsForCreateTransaction.match), switchMap((action) => {
|
|
29
|
+
const { transactionType, entity, lineIds } = action.payload;
|
|
30
|
+
if (entity == null) {
|
|
31
|
+
return of(clearRecommendationsForCreateTransactionLines(transactionType, lineIds));
|
|
32
|
+
}
|
|
33
|
+
const entityIdOrName = toEntityIdOrName(entity);
|
|
34
|
+
const state = state$.value;
|
|
35
|
+
const isAccountingClassesEnabled = getIsAccountingClassesEnabled(state);
|
|
36
|
+
const cached = getRecommendationsForEntity(state, entityIdOrName, 'vendor');
|
|
37
|
+
// Cache hit: apply straight away, no request and no wait.
|
|
38
|
+
if (cached.fetchState === 'Completed') {
|
|
39
|
+
return of(setRecommendationsForCreateTransactionLines(transactionType, lineIds, cached.recommendations ?? [], isAccountingClassesEnabled));
|
|
40
|
+
}
|
|
41
|
+
const waitThenApply = actions$.pipe(filter(indicateReadyToInitializeRecommendation.match), filter((ready) => ready.payload.entityType === 'vendor' &&
|
|
42
|
+
ready.payload.entityIdOrName === entityIdOrName), take(1), // don't listen forever! IMPORTANT!
|
|
43
|
+
mergeMap(() => from([
|
|
44
|
+
setRecommendationsForCreateTransactionLines(transactionType, lineIds, getRecommendationsForEntity(state$.value, entityIdOrName, 'vendor').recommendations ?? [], getIsAccountingClassesEnabled(state$.value)),
|
|
45
|
+
])));
|
|
46
|
+
// A fetch already in flight needs no second request, only the wait.
|
|
47
|
+
if (cached.fetchState === 'In-Progress') {
|
|
48
|
+
return waitThenApply;
|
|
49
|
+
}
|
|
50
|
+
return concat(of((isUnsyncedVendor(entity)
|
|
51
|
+
? fetchRecommendationByEntityName(entityIdOrName, 'vendor')
|
|
52
|
+
: fetchRecommendationByEntityId(entityIdOrName, 'vendor'))), waitThenApply);
|
|
53
|
+
}));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { filter, map } from 'rxjs/operators';
|
|
2
|
+
import { initializeCreateTransactionLocalData, resetCreateTransactionLocalData, } from '../../transactionDetailReducer';
|
|
3
|
+
/**
|
|
4
|
+
* Seeds the working copy when the Create form opens: today's date, the default line set for the
|
|
5
|
+
* type, and the payment_type the backend insists on.
|
|
6
|
+
*
|
|
7
|
+
* The seed replaces the whole working copy rather than merging into it, so a second initialize
|
|
8
|
+
* for the same type can't leave the previous draft's vendor, memo or account behind.
|
|
9
|
+
*/
|
|
10
|
+
export const initializeCreateTransactionLocalDataEpic = (actions$) => actions$.pipe(filter(initializeCreateTransactionLocalData.match), map((action) => resetCreateTransactionLocalData(action.payload.transactionType)));
|