@accounter/server 0.0.9-alpha-20251216161545-668306e40cf3aed663f444e0004246489fbec6d4 → 0.0.9-alpha-20251217093036-7168648b507d62946aa287af4ea690b73b077b2d
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/CHANGELOG.md +34 -5
- package/dist/green-invoice-graphql/src/mesh-artifacts/index.d.ts +1 -1
- package/dist/server/src/__generated__/types.d.ts +282 -301
- package/dist/server/src/__generated__/types.js.map +1 -1
- package/dist/server/src/modules/app-providers/green-invoice-client.d.ts +3 -3
- package/dist/server/src/modules/contracts/helpers/contracts.helper.d.ts +2 -0
- package/dist/server/src/modules/contracts/helpers/contracts.helper.js +20 -0
- package/dist/server/src/modules/contracts/helpers/contracts.helper.js.map +1 -1
- package/dist/server/src/modules/documents/__generated__/types.d.ts +121 -7
- package/dist/server/src/modules/documents/__generated__/types.js.map +1 -1
- package/dist/server/src/modules/documents/helpers/common.helper.d.ts +2 -0
- package/dist/server/src/modules/documents/helpers/common.helper.js +20 -0
- package/dist/server/src/modules/documents/helpers/common.helper.js.map +1 -1
- package/dist/server/src/modules/documents/helpers/issue-document.helper.d.ts +21 -0
- package/dist/server/src/modules/{green-invoice → documents}/helpers/issue-document.helper.js +61 -11
- package/dist/server/src/modules/documents/helpers/issue-document.helper.js.map +1 -0
- package/dist/server/src/modules/documents/index.js +9 -2
- package/dist/server/src/modules/documents/index.js.map +1 -1
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.d.ts +2 -0
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js +357 -0
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js.map +1 -0
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.d.ts +2 -0
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js +228 -0
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js.map +1 -0
- package/dist/server/src/modules/green-invoice/__generated__/types.d.ts +6 -135
- package/dist/server/src/modules/green-invoice/__generated__/types.js +0 -2
- package/dist/server/src/modules/green-invoice/__generated__/types.js.map +1 -1
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.d.ts +21 -27
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js +160 -151
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js.map +1 -1
- package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js +4 -347
- package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js.map +1 -1
- package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js +4 -512
- package/dist/server/src/modules/green-invoice/typeDefs/green-invoice.graphql.js.map +1 -1
- package/dist/shaam-uniform-format-generator/src/generator/records/b110.d.ts +35 -35
- package/dist/shaam-uniform-format-generator/src/types/enums.d.ts +71 -71
- package/package.json +2 -2
- package/src/__generated__/types.ts +327 -620
- package/src/modules/contracts/helpers/contracts.helper.ts +22 -0
- package/src/modules/documents/__generated__/types.ts +121 -7
- package/src/modules/documents/helpers/common.helper.ts +21 -0
- package/src/modules/{green-invoice → documents}/helpers/issue-document.helper.ts +104 -44
- package/src/modules/documents/index.ts +9 -2
- package/src/modules/documents/resolvers/documents-issuing.resolver.ts +554 -0
- package/src/modules/documents/typeDefs/documents-issuing.graphql.ts +228 -0
- package/src/modules/green-invoice/__generated__/types.ts +6 -137
- package/src/modules/green-invoice/helpers/green-invoice.helper.ts +195 -201
- package/src/modules/green-invoice/resolvers/green-invoice.resolvers.ts +5 -520
- package/src/modules/green-invoice/typeDefs/green-invoice.graphql.ts +4 -512
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.d.ts +0 -7
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js +0 -53
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js.map +0 -1
- package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.d.ts +0 -19
- package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js.map +0 -1
- package/src/modules/green-invoice/helpers/contract-to-draft.helper.ts +0 -69
|
@@ -1,471 +1,26 @@
|
|
|
1
|
-
import { addMonths, endOfMonth, format, startOfMonth, subMonths } from 'date-fns';
|
|
2
1
|
import { GraphQLError } from 'graphql';
|
|
3
2
|
import type { _DOLLAR_defs_Document } from '@accounter/green-invoice-graphql';
|
|
4
|
-
import type { BillingCycle, ResolversTypes } from '../../../__generated__/types.js';
|
|
5
|
-
import { Currency, DocumentType } from '../../../shared/enums.js';
|
|
6
|
-
import { dateToTimelessDateString } from '../../../shared/helpers/index.js';
|
|
7
3
|
import { GreenInvoiceClientProvider } from '../../app-providers/green-invoice-client.js';
|
|
8
|
-
import {
|
|
9
|
-
getChargeBusinesses,
|
|
10
|
-
getChargeDocumentsMeta,
|
|
11
|
-
} from '../../charges/helpers/common.helper.js';
|
|
12
|
-
import { ChargesProvider } from '../../charges/providers/charges.provider.js';
|
|
13
|
-
import { ContractsProvider } from '../../contracts/providers/contracts.provider.js';
|
|
14
|
-
import { IGetContractsByIdsResult } from '../../contracts/types.js';
|
|
15
|
-
import { DocumentsProvider } from '../../documents/providers/documents.provider.js';
|
|
16
4
|
import { IssuedDocumentsProvider } from '../../documents/providers/issued-documents.provider.js';
|
|
17
|
-
import { normalizeDocumentType } from '../../documents/resolvers/common.js';
|
|
18
5
|
import type {
|
|
19
6
|
IGetAllIssuedDocumentsResult,
|
|
20
|
-
IGetIssuedDocumentsByIdsResult,
|
|
21
7
|
IInsertDocumentsResult,
|
|
22
8
|
IUpdateIssuedDocumentParams,
|
|
23
9
|
} from '../../documents/types.js';
|
|
24
10
|
import { validateClientIntegrations } from '../../financial-entities/helpers/clients.helper.js';
|
|
25
|
-
import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js';
|
|
26
11
|
import { ClientsProvider } from '../../financial-entities/providers/clients.provider.js';
|
|
27
|
-
import { TransactionsProvider } from '../../transactions/providers/transactions.provider.js';
|
|
28
|
-
import { convertContractToDraft } from '../helpers/contract-to-draft.helper.js';
|
|
29
12
|
import {
|
|
30
|
-
|
|
31
|
-
convertGreenInvoiceDocumentToLocalDocumentInfo,
|
|
32
|
-
getGreenInvoiceDocumentNameFromType,
|
|
13
|
+
convertGreenInvoiceDocumentToDocumentDraft,
|
|
33
14
|
getGreenInvoiceDocuments,
|
|
34
15
|
getLinkedDocuments,
|
|
16
|
+
greenInvoiceCountryToCountryCode,
|
|
35
17
|
greenInvoiceToDocumentStatus,
|
|
36
18
|
insertNewDocumentFromGreenInvoice,
|
|
37
19
|
} from '../helpers/green-invoice.helper.js';
|
|
38
|
-
import {
|
|
39
|
-
deduceVatTypeFromBusiness,
|
|
40
|
-
executeDocumentIssue,
|
|
41
|
-
filterAndHandleSwiftTransactions,
|
|
42
|
-
getDocumentDateOutOfTransactions,
|
|
43
|
-
getIncomeFromDocuments,
|
|
44
|
-
getLinkedDocumentsAttributes,
|
|
45
|
-
getPaymentsFromTransactions,
|
|
46
|
-
getTypeFromDocumentsAndTransactions,
|
|
47
|
-
} from '../helpers/issue-document.helper.js';
|
|
48
20
|
import type { GreenInvoiceModule } from '../types.js';
|
|
49
21
|
|
|
50
22
|
export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
|
|
51
23
|
Query: {
|
|
52
|
-
newDocumentInfoDraftByCharge: async (
|
|
53
|
-
_,
|
|
54
|
-
{ chargeId },
|
|
55
|
-
{
|
|
56
|
-
injector,
|
|
57
|
-
adminContext: {
|
|
58
|
-
defaultCryptoConversionFiatCurrency,
|
|
59
|
-
financialAccounts: { swiftBusinessId },
|
|
60
|
-
locality,
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
) => {
|
|
64
|
-
if (!chargeId) {
|
|
65
|
-
throw new GraphQLError('Charge ID is required to fetch document draft');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const chargePromise = injector.get(ChargesProvider).getChargeByIdLoader.load(chargeId);
|
|
69
|
-
const documentsPromise = injector
|
|
70
|
-
.get(DocumentsProvider)
|
|
71
|
-
.getDocumentsByChargeIdLoader.load(chargeId);
|
|
72
|
-
const transactionsPromise = injector
|
|
73
|
-
.get(TransactionsProvider)
|
|
74
|
-
.transactionsByChargeIDLoader.load(chargeId)
|
|
75
|
-
.then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId));
|
|
76
|
-
|
|
77
|
-
const [charge, documents, transactions, { documentsCurrency }, { mainBusinessId }] =
|
|
78
|
-
await Promise.all([
|
|
79
|
-
chargePromise,
|
|
80
|
-
documentsPromise,
|
|
81
|
-
transactionsPromise,
|
|
82
|
-
getChargeDocumentsMeta(chargeId, injector),
|
|
83
|
-
getChargeBusinesses(chargeId, injector),
|
|
84
|
-
]);
|
|
85
|
-
|
|
86
|
-
if (!charge) {
|
|
87
|
-
throw new GraphQLError(`Charge with ID "${chargeId}" not found`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const businessMatchPromise = mainBusinessId
|
|
91
|
-
? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
|
|
92
|
-
: Promise.resolve(undefined);
|
|
93
|
-
|
|
94
|
-
const openIssuedDocumentsPromise = injector
|
|
95
|
-
.get(IssuedDocumentsProvider)
|
|
96
|
-
.getIssuedDocumentsByIdLoader.loadMany(documents.map(d => d.id))
|
|
97
|
-
.then(
|
|
98
|
-
res =>
|
|
99
|
-
res.filter(r => {
|
|
100
|
-
if (!r) return false;
|
|
101
|
-
|
|
102
|
-
if (r instanceof Error) {
|
|
103
|
-
console.error('Failed to fetch issued document', r);
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (r.status !== 'OPEN') {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return true;
|
|
112
|
-
}) as IGetIssuedDocumentsByIdsResult[],
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
const paymentPromise = getPaymentsFromTransactions(injector, transactions);
|
|
116
|
-
|
|
117
|
-
const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
|
|
118
|
-
|
|
119
|
-
const [businessMatch, openIssuedDocuments, payment, vatType] = await Promise.all([
|
|
120
|
-
businessMatchPromise,
|
|
121
|
-
openIssuedDocumentsPromise,
|
|
122
|
-
paymentPromise,
|
|
123
|
-
vatTypePromise,
|
|
124
|
-
]);
|
|
125
|
-
|
|
126
|
-
const greenInvoiceDocuments = await Promise.all(
|
|
127
|
-
openIssuedDocuments.map(doc =>
|
|
128
|
-
injector
|
|
129
|
-
.get(GreenInvoiceClientProvider)
|
|
130
|
-
.documentLoader.load(doc.external_id)
|
|
131
|
-
.then(res => {
|
|
132
|
-
if (!res) {
|
|
133
|
-
console.error('Failed to fetch document from Green Invoice', doc.external_id);
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
return res;
|
|
137
|
-
}),
|
|
138
|
-
),
|
|
139
|
-
).then(res => res.filter(Boolean) as _DOLLAR_defs_Document[]);
|
|
140
|
-
|
|
141
|
-
const greenInvoiceClientId = validateClientIntegrations(
|
|
142
|
-
businessMatch?.integrations ?? {},
|
|
143
|
-
).greenInvoiceId;
|
|
144
|
-
if (!greenInvoiceClientId) {
|
|
145
|
-
throw new GraphQLError(
|
|
146
|
-
`Green invoice integration missing for business ID="${mainBusinessId}"`,
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const income = getIncomeFromDocuments(
|
|
151
|
-
openIssuedDocuments.map(doc => ({
|
|
152
|
-
document: documents.find(d => d.id === doc.id)!,
|
|
153
|
-
issuedDocument: doc,
|
|
154
|
-
greenInvoiceDocument: greenInvoiceDocuments.find(gd => gd.id === doc.external_id)!,
|
|
155
|
-
})),
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
if (income.length === 0 && transactions.length > 0) {
|
|
159
|
-
income.push(
|
|
160
|
-
...transactions.map(transaction => ({
|
|
161
|
-
description: transaction.source_description ?? '',
|
|
162
|
-
quantity: 1,
|
|
163
|
-
price: Number(transaction.amount),
|
|
164
|
-
currency: transaction.currency as Currency,
|
|
165
|
-
currencyRate: undefined,
|
|
166
|
-
vatType,
|
|
167
|
-
})),
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const type = getTypeFromDocumentsAndTransactions(greenInvoiceDocuments, transactions);
|
|
172
|
-
|
|
173
|
-
const linkedDocsAttributes = getLinkedDocumentsAttributes(
|
|
174
|
-
openIssuedDocuments,
|
|
175
|
-
type === DocumentType.CreditInvoice,
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
let remarks = charge.user_description;
|
|
179
|
-
if (greenInvoiceDocuments.length) {
|
|
180
|
-
remarks = greenInvoiceDocuments.map(doc => doc.remarks).join(', ');
|
|
181
|
-
switch (type) {
|
|
182
|
-
case DocumentType.Receipt:
|
|
183
|
-
case DocumentType.InvoiceReceipt:
|
|
184
|
-
remarks = `${getGreenInvoiceDocumentNameFromType(type)} for ${greenInvoiceDocuments.map(doc => `${getGreenInvoiceDocumentNameFromType(doc.type)} ${doc.number}`).join(', ')}`;
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
} else if (businessMatch?.remark) {
|
|
188
|
-
remarks = businessMatch.remark;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const documentDate = getDocumentDateOutOfTransactions(transactions);
|
|
192
|
-
|
|
193
|
-
const transactionsCurrencies = Array.from(new Set(transactions.map(t => t.currency)));
|
|
194
|
-
const transactionsCurrency =
|
|
195
|
-
transactionsCurrencies.length === 1 ? transactionsCurrencies[0] : undefined;
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
remarks,
|
|
199
|
-
// description: ____,
|
|
200
|
-
// footer: ____,
|
|
201
|
-
type,
|
|
202
|
-
date: documentDate,
|
|
203
|
-
dueDate: dateToTimelessDateString(endOfMonth(new Date())),
|
|
204
|
-
lang: 'ENGLISH',
|
|
205
|
-
currency: (transactionsCurrency ||
|
|
206
|
-
documentsCurrency ||
|
|
207
|
-
defaultCryptoConversionFiatCurrency) as Currency,
|
|
208
|
-
vatType,
|
|
209
|
-
rounding: false,
|
|
210
|
-
signed: true,
|
|
211
|
-
client: greenInvoiceClientId,
|
|
212
|
-
income,
|
|
213
|
-
payment,
|
|
214
|
-
// linkedPaymentId: ____,
|
|
215
|
-
// maxPayments: _____,
|
|
216
|
-
// discount: _____,
|
|
217
|
-
...linkedDocsAttributes,
|
|
218
|
-
};
|
|
219
|
-
},
|
|
220
|
-
newDocumentInfoDraftByDocument: async (
|
|
221
|
-
_,
|
|
222
|
-
{ documentId },
|
|
223
|
-
{
|
|
224
|
-
injector,
|
|
225
|
-
adminContext: {
|
|
226
|
-
defaultCryptoConversionFiatCurrency,
|
|
227
|
-
financialAccounts: { swiftBusinessId },
|
|
228
|
-
locality,
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
) => {
|
|
232
|
-
if (!documentId) {
|
|
233
|
-
throw new GraphQLError('Document ID is required to fetch document draft');
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const document = await injector
|
|
237
|
-
.get(DocumentsProvider)
|
|
238
|
-
.getDocumentsByIdLoader.load(documentId);
|
|
239
|
-
|
|
240
|
-
if (!document) {
|
|
241
|
-
throw new GraphQLError(`Document with ID "${documentId}" not found`);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const chargePromise = document.charge_id
|
|
245
|
-
? injector.get(ChargesProvider).getChargeByIdLoader.load(document.charge_id)
|
|
246
|
-
: Promise.resolve(undefined);
|
|
247
|
-
const transactionsPromise = document.charge_id
|
|
248
|
-
? injector
|
|
249
|
-
.get(TransactionsProvider)
|
|
250
|
-
.transactionsByChargeIDLoader.load(document.charge_id)
|
|
251
|
-
.then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId))
|
|
252
|
-
: Promise.resolve(undefined);
|
|
253
|
-
const chargeBusinessIdPromise = document.charge_id
|
|
254
|
-
? getChargeBusinesses(document.charge_id, injector).then(res => res.mainBusinessId)
|
|
255
|
-
: Promise.resolve(undefined);
|
|
256
|
-
|
|
257
|
-
const [charge, transactions, chargeBusinessId] = await Promise.all([
|
|
258
|
-
chargePromise,
|
|
259
|
-
transactionsPromise,
|
|
260
|
-
chargeBusinessIdPromise,
|
|
261
|
-
]);
|
|
262
|
-
|
|
263
|
-
if (!charge) {
|
|
264
|
-
throw new GraphQLError(`Charge with ID "${document.charge_id}" not found`);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const businessMatchPromise = chargeBusinessId
|
|
268
|
-
? injector.get(ClientsProvider).getClientByIdLoader.load(chargeBusinessId)
|
|
269
|
-
: Promise.resolve(undefined);
|
|
270
|
-
|
|
271
|
-
const openIssuedDocumentPromise = injector
|
|
272
|
-
.get(IssuedDocumentsProvider)
|
|
273
|
-
.getIssuedDocumentsByIdLoader.load(document.id);
|
|
274
|
-
|
|
275
|
-
const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, chargeBusinessId);
|
|
276
|
-
|
|
277
|
-
const [businessMatch, openIssuedDocument, vatType] = await Promise.all([
|
|
278
|
-
businessMatchPromise,
|
|
279
|
-
openIssuedDocumentPromise,
|
|
280
|
-
vatTypePromise,
|
|
281
|
-
]);
|
|
282
|
-
|
|
283
|
-
if (!openIssuedDocument) {
|
|
284
|
-
throw new GraphQLError(
|
|
285
|
-
`Document with ID "${document.id}" doesn't seem like a document we issued`,
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (openIssuedDocument.status !== 'OPEN') {
|
|
290
|
-
throw new GraphQLError(`Document with ID "${document.id}" is closed`);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
const greenInvoiceDocumentPromise: Promise<_DOLLAR_defs_Document | null> = injector
|
|
294
|
-
.get(GreenInvoiceClientProvider)
|
|
295
|
-
.documentLoader.load(openIssuedDocument.external_id)
|
|
296
|
-
.then(res => {
|
|
297
|
-
if (!res) {
|
|
298
|
-
console.error(
|
|
299
|
-
'Failed to fetch document from Green Invoice',
|
|
300
|
-
openIssuedDocument.external_id,
|
|
301
|
-
);
|
|
302
|
-
return null;
|
|
303
|
-
}
|
|
304
|
-
return res;
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
const paymentPromise = getPaymentsFromTransactions(injector, transactions ?? []);
|
|
308
|
-
|
|
309
|
-
const greenInvoiceClientId = validateClientIntegrations(
|
|
310
|
-
businessMatch?.integrations ?? {},
|
|
311
|
-
).greenInvoiceId;
|
|
312
|
-
if (!greenInvoiceClientId) {
|
|
313
|
-
throw new GraphQLError(
|
|
314
|
-
`Green invoice integration missing for business ID="${chargeBusinessId}"`,
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
const [greenInvoiceDocument, payment, { documentsCurrency }] = await Promise.all([
|
|
319
|
-
greenInvoiceDocumentPromise,
|
|
320
|
-
paymentPromise,
|
|
321
|
-
getChargeDocumentsMeta(charge.id, injector),
|
|
322
|
-
]);
|
|
323
|
-
|
|
324
|
-
if (!greenInvoiceDocument) {
|
|
325
|
-
throw new GraphQLError(
|
|
326
|
-
`Document with ID "${document.id}" doesn't have a Green Invoice matching document`,
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const income = getIncomeFromDocuments([
|
|
331
|
-
{
|
|
332
|
-
document,
|
|
333
|
-
issuedDocument: openIssuedDocument,
|
|
334
|
-
greenInvoiceDocument,
|
|
335
|
-
},
|
|
336
|
-
]);
|
|
337
|
-
|
|
338
|
-
const type = getTypeFromDocumentsAndTransactions([greenInvoiceDocument], transactions ?? []);
|
|
339
|
-
|
|
340
|
-
const linkedDocsAttributes = getLinkedDocumentsAttributes(
|
|
341
|
-
[openIssuedDocument],
|
|
342
|
-
type === DocumentType.CreditInvoice,
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
let remarks = greenInvoiceDocument.remarks;
|
|
346
|
-
switch (type) {
|
|
347
|
-
case DocumentType.Receipt:
|
|
348
|
-
case DocumentType.InvoiceReceipt:
|
|
349
|
-
remarks = `${getGreenInvoiceDocumentNameFromType(type)} for ${getGreenInvoiceDocumentNameFromType(greenInvoiceDocument.type)} ${greenInvoiceDocument.number}`;
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
if (!remarks && businessMatch?.remark) {
|
|
354
|
-
remarks = businessMatch.remark;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const documentDate = getDocumentDateOutOfTransactions(transactions ?? []);
|
|
358
|
-
|
|
359
|
-
return {
|
|
360
|
-
remarks,
|
|
361
|
-
// description: ____,
|
|
362
|
-
// footer: ____,
|
|
363
|
-
type,
|
|
364
|
-
date: documentDate,
|
|
365
|
-
dueDate: dateToTimelessDateString(endOfMonth(new Date())),
|
|
366
|
-
lang: 'ENGLISH',
|
|
367
|
-
currency: (documentsCurrency || defaultCryptoConversionFiatCurrency) as Currency,
|
|
368
|
-
vatType,
|
|
369
|
-
rounding: false,
|
|
370
|
-
signed: true,
|
|
371
|
-
client: greenInvoiceClientId,
|
|
372
|
-
income,
|
|
373
|
-
payment,
|
|
374
|
-
// linkedPaymentId: ____,
|
|
375
|
-
// maxPayments: _____,
|
|
376
|
-
// discount: _____,
|
|
377
|
-
...linkedDocsAttributes,
|
|
378
|
-
};
|
|
379
|
-
},
|
|
380
|
-
clientMonthlyChargesDrafts: async (_, { issueMonth }, { injector }) => {
|
|
381
|
-
const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
|
|
382
|
-
const monthlyBillingCycle: BillingCycle = 'MONTHLY';
|
|
383
|
-
const monthlyContracts = openContracts.filter(
|
|
384
|
-
contract => contract.billing_cycle.toLocaleUpperCase() === monthlyBillingCycle,
|
|
385
|
-
);
|
|
386
|
-
const drafts = await Promise.all(
|
|
387
|
-
monthlyContracts.map(async contract =>
|
|
388
|
-
convertContractToDraft(contract, injector, issueMonth),
|
|
389
|
-
),
|
|
390
|
-
);
|
|
391
|
-
|
|
392
|
-
return drafts;
|
|
393
|
-
},
|
|
394
|
-
clientChargesDraftsByContracts: async (_, { issueMonth, contractIds }, { injector }) => {
|
|
395
|
-
const contracts = await injector
|
|
396
|
-
.get(ContractsProvider)
|
|
397
|
-
.getContractsByIdLoader.loadMany(contractIds)
|
|
398
|
-
.then(res => res.filter(c => !!c && !(c instanceof Error)) as IGetContractsByIdsResult[]);
|
|
399
|
-
|
|
400
|
-
const drafts = await Promise.all(
|
|
401
|
-
contracts.map(async contract => convertContractToDraft(contract, injector, issueMonth)),
|
|
402
|
-
);
|
|
403
|
-
|
|
404
|
-
return drafts;
|
|
405
|
-
},
|
|
406
|
-
clientMonthlyChargeDraft: async (_, { clientId, issueMonth }, { injector }) => {
|
|
407
|
-
const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
|
|
408
|
-
const contract = openContracts.find(openContract => openContract.client_id === clientId);
|
|
409
|
-
|
|
410
|
-
if (!contract) {
|
|
411
|
-
throw new GraphQLError(`Contract not found for client ID="${clientId}"`);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const businessPromise = injector
|
|
415
|
-
.get(BusinessesProvider)
|
|
416
|
-
.getBusinessByIdLoader.load(contract.client_id);
|
|
417
|
-
const clientPromise = injector
|
|
418
|
-
.get(ClientsProvider)
|
|
419
|
-
.getClientByIdLoader.load(contract.client_id);
|
|
420
|
-
const [business, client] = await Promise.all([businessPromise, clientPromise]);
|
|
421
|
-
|
|
422
|
-
if (!business) {
|
|
423
|
-
throw new GraphQLError(`Business ID="${contract.client_id}" not found`);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (!client) {
|
|
427
|
-
throw new GraphQLError(`Client not found for business ID="${contract.client_id}"`);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
const greenInvoiceId = validateClientIntegrations(client.integrations)?.greenInvoiceId;
|
|
431
|
-
|
|
432
|
-
if (!greenInvoiceId) {
|
|
433
|
-
throw new GraphQLError(
|
|
434
|
-
`Green invoice match not found for business ID="${contract.client_id}"`,
|
|
435
|
-
);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
const today = issueMonth ? addMonths(new Date(issueMonth), 1) : new Date();
|
|
439
|
-
const monthStart = dateToTimelessDateString(startOfMonth(today));
|
|
440
|
-
const monthEnd = dateToTimelessDateString(endOfMonth(today));
|
|
441
|
-
const year = today.getFullYear() + (today.getMonth() === 0 ? -1 : 0);
|
|
442
|
-
const month = format(subMonths(today, 1), 'MMMM');
|
|
443
|
-
|
|
444
|
-
const draft: ResolversTypes['NewDocumentInfo'] = {
|
|
445
|
-
remarks: `${contract.purchase_orders[0] ? `PO: ${contract.purchase_orders[0]}${contract.remarks ? ', ' : ''}` : ''}${contract.remarks ?? ''}`,
|
|
446
|
-
description: `GraphQL Hive Enterprise License - ${month} ${year}`,
|
|
447
|
-
type: normalizeDocumentType(contract.document_type),
|
|
448
|
-
date: monthStart,
|
|
449
|
-
dueDate: monthEnd,
|
|
450
|
-
lang: 'ENGLISH',
|
|
451
|
-
currency: contract.currency as Currency,
|
|
452
|
-
vatType: 'EXEMPT',
|
|
453
|
-
rounding: false,
|
|
454
|
-
signed: true,
|
|
455
|
-
client: greenInvoiceId,
|
|
456
|
-
income: [
|
|
457
|
-
{
|
|
458
|
-
description: `GraphQL Hive Enterprise License - ${month} ${year}`,
|
|
459
|
-
quantity: 1,
|
|
460
|
-
price: contract.amount,
|
|
461
|
-
currency: contract.currency as Currency,
|
|
462
|
-
vatType: 'EXEMPT',
|
|
463
|
-
},
|
|
464
|
-
],
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
return draft;
|
|
468
|
-
},
|
|
469
24
|
greenInvoiceClient: async (_, { clientId }, { injector }) => {
|
|
470
25
|
try {
|
|
471
26
|
const client = await injector.get(ClientsProvider).getClientByIdLoader.load(clientId);
|
|
@@ -487,7 +42,7 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
|
|
|
487
42
|
},
|
|
488
43
|
},
|
|
489
44
|
Mutation: {
|
|
490
|
-
|
|
45
|
+
syncGreenInvoiceDocuments: async (
|
|
491
46
|
_,
|
|
492
47
|
{ ownerId: inputOwnerId, singlePageLimit = true },
|
|
493
48
|
{ injector, adminContext: { defaultAdminBusinessId } },
|
|
@@ -591,74 +146,6 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
|
|
|
591
146
|
|
|
592
147
|
return addedDocs;
|
|
593
148
|
},
|
|
594
|
-
issueGreenInvoiceDocuments: async (
|
|
595
|
-
_,
|
|
596
|
-
{ generateDocumentsInfo },
|
|
597
|
-
{ injector, adminContext: { defaultAdminBusinessId } },
|
|
598
|
-
) => {
|
|
599
|
-
const errors: string[] = [];
|
|
600
|
-
|
|
601
|
-
await Promise.all(
|
|
602
|
-
generateDocumentsInfo.map(document => {
|
|
603
|
-
executeDocumentIssue(
|
|
604
|
-
injector,
|
|
605
|
-
defaultAdminBusinessId,
|
|
606
|
-
document,
|
|
607
|
-
undefined,
|
|
608
|
-
true,
|
|
609
|
-
undefined,
|
|
610
|
-
true,
|
|
611
|
-
).catch(e => {
|
|
612
|
-
console.error(e);
|
|
613
|
-
errors.push(
|
|
614
|
-
`${document.client?.name ?? document.client?.greenInvoiceId}: ${e.message}`,
|
|
615
|
-
);
|
|
616
|
-
});
|
|
617
|
-
}),
|
|
618
|
-
);
|
|
619
|
-
|
|
620
|
-
return {
|
|
621
|
-
success: true,
|
|
622
|
-
errors,
|
|
623
|
-
};
|
|
624
|
-
},
|
|
625
|
-
previewGreenInvoiceDocument: async (_, { input: initialInput }, { injector }) => {
|
|
626
|
-
const input = await convertDocumentInputIntoGreenInvoiceInput(initialInput, injector);
|
|
627
|
-
const document = await injector.get(GreenInvoiceClientProvider).previewDocuments({ input });
|
|
628
|
-
|
|
629
|
-
if (!document) {
|
|
630
|
-
throw new GraphQLError('Failed to generate document preview');
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
if ('errorMessage' in document) {
|
|
634
|
-
console.error('Failed to generate document preview', document);
|
|
635
|
-
throw new GraphQLError(
|
|
636
|
-
`Failed to generate document preview, Green Invoice returned: ${document.errorMessage}`,
|
|
637
|
-
);
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
if ('file' in document && document.file) {
|
|
641
|
-
return document.file;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
console.error('Document preview does not contain a file', document);
|
|
645
|
-
throw new GraphQLError('Document preview does not contain a file');
|
|
646
|
-
},
|
|
647
|
-
issueGreenInvoiceDocument: async (
|
|
648
|
-
_,
|
|
649
|
-
{ input: initialInput, emailContent, attachment, chargeId, sendEmail = false },
|
|
650
|
-
{ injector, adminContext: { defaultAdminBusinessId } },
|
|
651
|
-
) => {
|
|
652
|
-
return executeDocumentIssue(
|
|
653
|
-
injector,
|
|
654
|
-
defaultAdminBusinessId,
|
|
655
|
-
initialInput,
|
|
656
|
-
emailContent ?? undefined,
|
|
657
|
-
attachment ?? undefined,
|
|
658
|
-
chargeId ?? undefined,
|
|
659
|
-
sendEmail ?? false,
|
|
660
|
-
);
|
|
661
|
-
},
|
|
662
149
|
},
|
|
663
150
|
IssuedDocumentInfo: {
|
|
664
151
|
originalDocument: async (info, _, { injector }) => {
|
|
@@ -672,7 +159,7 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
|
|
|
672
159
|
if (!document) {
|
|
673
160
|
throw new GraphQLError('Original document not found');
|
|
674
161
|
}
|
|
675
|
-
return
|
|
162
|
+
return await convertGreenInvoiceDocumentToDocumentDraft(document, injector);
|
|
676
163
|
} catch (error) {
|
|
677
164
|
console.error('Error fetching original document:', error);
|
|
678
165
|
throw new GraphQLError('Error fetching original document');
|
|
@@ -700,9 +187,7 @@ export const greenInvoiceResolvers: GreenInvoiceModule.Resolvers = {
|
|
|
700
187
|
.clientLoader.load(clientId)
|
|
701
188
|
.then(client => {
|
|
702
189
|
if (client?.country) {
|
|
703
|
-
|
|
704
|
-
// greenInvoiceCountryToCountryCode(client.country)
|
|
705
|
-
return client.country;
|
|
190
|
+
return greenInvoiceCountryToCountryCode(client.country);
|
|
706
191
|
}
|
|
707
192
|
return null;
|
|
708
193
|
});
|