@accounter/server 0.0.9-alpha-20251216144137-dc7316f892e1fcf0e350cfbbaa9ca0746e1c6c50 → 0.0.9-alpha-20251216161545-668306e40cf3aed663f444e0004246489fbec6d4
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 +5 -28
- package/dist/green-invoice-graphql/src/mesh-artifacts/index.d.ts +1 -1
- package/dist/server/src/__generated__/types.d.ts +301 -282
- 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/documents/__generated__/types.d.ts +7 -121
- package/dist/server/src/modules/documents/__generated__/types.js.map +1 -1
- package/dist/server/src/modules/documents/helpers/common.helper.d.ts +0 -2
- package/dist/server/src/modules/documents/helpers/common.helper.js +0 -20
- package/dist/server/src/modules/documents/helpers/common.helper.js.map +1 -1
- package/dist/server/src/modules/documents/index.js +2 -9
- package/dist/server/src/modules/documents/index.js.map +1 -1
- package/dist/server/src/modules/green-invoice/__generated__/types.d.ts +135 -6
- package/dist/server/src/modules/green-invoice/__generated__/types.js +2 -0
- package/dist/server/src/modules/green-invoice/__generated__/types.js.map +1 -1
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.d.ts +7 -0
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js +53 -0
- package/dist/server/src/modules/green-invoice/helpers/contract-to-draft.helper.js.map +1 -0
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.d.ts +27 -21
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js +146 -151
- package/dist/server/src/modules/green-invoice/helpers/green-invoice.helper.js.map +1 -1
- package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.d.ts +19 -0
- package/dist/server/src/modules/{documents → green-invoice}/helpers/issue-document.helper.js +11 -59
- package/dist/server/src/modules/green-invoice/helpers/issue-document.helper.js.map +1 -0
- package/dist/server/src/modules/green-invoice/resolvers/green-invoice.resolvers.js +347 -4
- 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 +512 -4
- 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 +1 -1
- package/src/__generated__/types.ts +620 -327
- package/src/modules/documents/__generated__/types.ts +7 -121
- package/src/modules/documents/helpers/common.helper.ts +0 -21
- package/src/modules/documents/index.ts +2 -9
- package/src/modules/green-invoice/__generated__/types.ts +137 -6
- package/src/modules/green-invoice/helpers/contract-to-draft.helper.ts +69 -0
- package/src/modules/green-invoice/helpers/green-invoice.helper.ts +199 -183
- package/src/modules/{documents → green-invoice}/helpers/issue-document.helper.ts +44 -96
- package/src/modules/green-invoice/resolvers/green-invoice.resolvers.ts +520 -5
- package/src/modules/green-invoice/typeDefs/green-invoice.graphql.ts +512 -4
- package/dist/server/src/modules/documents/helpers/issue-document.helper.d.ts +0 -21
- package/dist/server/src/modules/documents/helpers/issue-document.helper.js.map +0 -1
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.d.ts +0 -2
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js +0 -346
- package/dist/server/src/modules/documents/resolvers/documents-issuing.resolver.js.map +0 -1
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.d.ts +0 -2
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js +0 -228
- package/dist/server/src/modules/documents/typeDefs/documents-issuing.graphql.js.map +0 -1
- package/src/modules/documents/resolvers/documents-issuing.resolver.ts +0 -529
- package/src/modules/documents/typeDefs/documents-issuing.graphql.ts +0 -228
|
@@ -1,529 +0,0 @@
|
|
|
1
|
-
import { addMonths, endOfMonth, format, startOfMonth, subMonths } from 'date-fns';
|
|
2
|
-
import { GraphQLError } from 'graphql';
|
|
3
|
-
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
|
-
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 { validateClientIntegrations } from '../../financial-entities/helpers/clients.helper.js';
|
|
16
|
-
import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js';
|
|
17
|
-
import { ClientsProvider } from '../../financial-entities/providers/clients.provider.js';
|
|
18
|
-
import {
|
|
19
|
-
convertDocumentInputIntoGreenInvoiceInput,
|
|
20
|
-
getDocumentNameFromGreenInvoiceType,
|
|
21
|
-
normalizeGreenInvoiceDocumentType,
|
|
22
|
-
} from '../../green-invoice/helpers/green-invoice.helper.js';
|
|
23
|
-
import { TransactionsProvider } from '../../transactions/providers/transactions.provider.js';
|
|
24
|
-
import { getDocumentNameFromType } from '../helpers/common.helper.js';
|
|
25
|
-
import {
|
|
26
|
-
convertContractToDraft,
|
|
27
|
-
deduceVatTypeFromBusiness,
|
|
28
|
-
executeDocumentIssue,
|
|
29
|
-
filterAndHandleSwiftTransactions,
|
|
30
|
-
getDocumentDateOutOfTransactions,
|
|
31
|
-
getIncomeRecordsFromDocuments,
|
|
32
|
-
getLinkedDocumentsAttributes,
|
|
33
|
-
getPaymentsFromTransactions,
|
|
34
|
-
getTypeFromDocumentsAndTransactions,
|
|
35
|
-
} from '../helpers/issue-document.helper.js';
|
|
36
|
-
import { DocumentsProvider } from '../providers/documents.provider.js';
|
|
37
|
-
import { IssuedDocumentsProvider } from '../providers/issued-documents.provider.js';
|
|
38
|
-
import type { DocumentsModule, IGetIssuedDocumentsByIdsResult } from '../types.js';
|
|
39
|
-
import { normalizeDocumentType } from './common.js';
|
|
40
|
-
|
|
41
|
-
export const documentsIssuingResolvers: DocumentsModule.Resolvers = {
|
|
42
|
-
Query: {
|
|
43
|
-
newDocumentDraftByCharge: async (
|
|
44
|
-
_,
|
|
45
|
-
{ chargeId },
|
|
46
|
-
{
|
|
47
|
-
injector,
|
|
48
|
-
adminContext: {
|
|
49
|
-
defaultCryptoConversionFiatCurrency,
|
|
50
|
-
financialAccounts: { swiftBusinessId },
|
|
51
|
-
locality,
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
) => {
|
|
55
|
-
if (!chargeId) {
|
|
56
|
-
throw new GraphQLError('Charge ID is required to fetch document draft');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const chargePromise = injector.get(ChargesProvider).getChargeByIdLoader.load(chargeId);
|
|
60
|
-
const documentsPromise = injector
|
|
61
|
-
.get(DocumentsProvider)
|
|
62
|
-
.getDocumentsByChargeIdLoader.load(chargeId);
|
|
63
|
-
const transactionsPromise = injector
|
|
64
|
-
.get(TransactionsProvider)
|
|
65
|
-
.transactionsByChargeIDLoader.load(chargeId)
|
|
66
|
-
.then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId));
|
|
67
|
-
|
|
68
|
-
const [charge, documents, transactions, { documentsCurrency }, { mainBusinessId }] =
|
|
69
|
-
await Promise.all([
|
|
70
|
-
chargePromise,
|
|
71
|
-
documentsPromise,
|
|
72
|
-
transactionsPromise,
|
|
73
|
-
getChargeDocumentsMeta(chargeId, injector),
|
|
74
|
-
getChargeBusinesses(chargeId, injector),
|
|
75
|
-
]);
|
|
76
|
-
|
|
77
|
-
if (!charge) {
|
|
78
|
-
throw new GraphQLError(`Charge with ID "${chargeId}" not found`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const clientPromise = mainBusinessId
|
|
82
|
-
? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
|
|
83
|
-
: Promise.resolve(undefined);
|
|
84
|
-
|
|
85
|
-
const openIssuedDocumentsPromise = injector
|
|
86
|
-
.get(IssuedDocumentsProvider)
|
|
87
|
-
.getIssuedDocumentsByIdLoader.loadMany(documents.map(d => d.id))
|
|
88
|
-
.then(
|
|
89
|
-
res =>
|
|
90
|
-
res.filter(r => {
|
|
91
|
-
if (!r) return false;
|
|
92
|
-
|
|
93
|
-
if (r instanceof Error) {
|
|
94
|
-
console.error('Failed to fetch issued document', r);
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (r.status !== 'OPEN') {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return true;
|
|
103
|
-
}) as IGetIssuedDocumentsByIdsResult[],
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
const paymentPromise = getPaymentsFromTransactions(injector, transactions);
|
|
107
|
-
|
|
108
|
-
const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
|
|
109
|
-
|
|
110
|
-
const [client, openIssuedDocuments, payment, vatType] = await Promise.all([
|
|
111
|
-
clientPromise,
|
|
112
|
-
openIssuedDocumentsPromise,
|
|
113
|
-
paymentPromise,
|
|
114
|
-
vatTypePromise,
|
|
115
|
-
]);
|
|
116
|
-
|
|
117
|
-
const greenInvoiceDocuments = await Promise.all(
|
|
118
|
-
openIssuedDocuments.map(doc =>
|
|
119
|
-
injector
|
|
120
|
-
.get(GreenInvoiceClientProvider)
|
|
121
|
-
.documentLoader.load(doc.external_id)
|
|
122
|
-
.then(res => {
|
|
123
|
-
if (!res) {
|
|
124
|
-
console.error('Failed to fetch document from Green Invoice', doc.external_id);
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
return res;
|
|
128
|
-
}),
|
|
129
|
-
),
|
|
130
|
-
).then(res => res.filter(Boolean) as _DOLLAR_defs_Document[]);
|
|
131
|
-
|
|
132
|
-
const greenInvoiceClientId = validateClientIntegrations(
|
|
133
|
-
client?.integrations ?? {},
|
|
134
|
-
).greenInvoiceId;
|
|
135
|
-
if (!greenInvoiceClientId) {
|
|
136
|
-
throw new GraphQLError(
|
|
137
|
-
`Green invoice integration missing for business ID="${mainBusinessId}"`,
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const income = getIncomeRecordsFromDocuments(
|
|
142
|
-
openIssuedDocuments.map(
|
|
143
|
-
doc => greenInvoiceDocuments.find(gd => gd.id === doc.external_id)!,
|
|
144
|
-
),
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
if (income.length === 0 && transactions.length > 0) {
|
|
148
|
-
income.push(
|
|
149
|
-
...transactions.map(transaction => ({
|
|
150
|
-
description: transaction.source_description ?? '',
|
|
151
|
-
quantity: 1,
|
|
152
|
-
price: Number(transaction.amount),
|
|
153
|
-
currency: transaction.currency as Currency,
|
|
154
|
-
currencyRate: undefined,
|
|
155
|
-
vatType,
|
|
156
|
-
})),
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const type = getTypeFromDocumentsAndTransactions(
|
|
161
|
-
greenInvoiceDocuments.map(doc => ({ type: normalizeGreenInvoiceDocumentType(doc.type) })),
|
|
162
|
-
transactions,
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
const linkedDocsAttributes = getLinkedDocumentsAttributes(
|
|
166
|
-
openIssuedDocuments,
|
|
167
|
-
type === DocumentType.CreditInvoice,
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
let remarks = charge.user_description;
|
|
171
|
-
if (greenInvoiceDocuments.length) {
|
|
172
|
-
remarks = greenInvoiceDocuments.map(doc => doc.remarks).join(', ');
|
|
173
|
-
switch (type) {
|
|
174
|
-
case DocumentType.Receipt:
|
|
175
|
-
case DocumentType.InvoiceReceipt:
|
|
176
|
-
remarks = `${getDocumentNameFromType(type)} for ${greenInvoiceDocuments.map(doc => `${getDocumentNameFromGreenInvoiceType(doc.type)} ${doc.number}`).join(', ')}`;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
} else if (client?.remark) {
|
|
180
|
-
remarks = client.remark;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const documentDate = getDocumentDateOutOfTransactions(transactions);
|
|
184
|
-
|
|
185
|
-
const transactionsCurrencies = Array.from(new Set(transactions.map(t => t.currency)));
|
|
186
|
-
const transactionsCurrency =
|
|
187
|
-
transactionsCurrencies.length === 1 ? transactionsCurrencies[0] : undefined;
|
|
188
|
-
|
|
189
|
-
const draft: ResolversTypes['DocumentDraft'] = {
|
|
190
|
-
remarks,
|
|
191
|
-
// description: ____,
|
|
192
|
-
// footer: ____,
|
|
193
|
-
type,
|
|
194
|
-
date: documentDate,
|
|
195
|
-
dueDate: dateToTimelessDateString(endOfMonth(new Date())),
|
|
196
|
-
language: 'ENGLISH',
|
|
197
|
-
currency: (transactionsCurrency ||
|
|
198
|
-
documentsCurrency ||
|
|
199
|
-
defaultCryptoConversionFiatCurrency) as Currency,
|
|
200
|
-
vatType,
|
|
201
|
-
rounding: false,
|
|
202
|
-
signed: true,
|
|
203
|
-
client,
|
|
204
|
-
income,
|
|
205
|
-
payment,
|
|
206
|
-
// linkedPaymentId: ____,
|
|
207
|
-
// maxPayments: _____,
|
|
208
|
-
// discount: _____,
|
|
209
|
-
...linkedDocsAttributes,
|
|
210
|
-
};
|
|
211
|
-
return draft;
|
|
212
|
-
},
|
|
213
|
-
newDocumentDraftByDocument: async (
|
|
214
|
-
_,
|
|
215
|
-
{ documentId },
|
|
216
|
-
{
|
|
217
|
-
injector,
|
|
218
|
-
adminContext: {
|
|
219
|
-
defaultCryptoConversionFiatCurrency,
|
|
220
|
-
financialAccounts: { swiftBusinessId },
|
|
221
|
-
locality,
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
) => {
|
|
225
|
-
if (!documentId) {
|
|
226
|
-
throw new GraphQLError('Document ID is required to fetch document draft');
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const document = await injector
|
|
230
|
-
.get(DocumentsProvider)
|
|
231
|
-
.getDocumentsByIdLoader.load(documentId);
|
|
232
|
-
|
|
233
|
-
if (!document) {
|
|
234
|
-
throw new GraphQLError(`Document with ID "${documentId}" not found`);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const chargePromise = document.charge_id
|
|
238
|
-
? injector.get(ChargesProvider).getChargeByIdLoader.load(document.charge_id)
|
|
239
|
-
: Promise.resolve(undefined);
|
|
240
|
-
const transactionsPromise = document.charge_id
|
|
241
|
-
? injector
|
|
242
|
-
.get(TransactionsProvider)
|
|
243
|
-
.transactionsByChargeIDLoader.load(document.charge_id)
|
|
244
|
-
.then(res => filterAndHandleSwiftTransactions(res, swiftBusinessId))
|
|
245
|
-
: Promise.resolve(undefined);
|
|
246
|
-
const mainBusinessIdPromise = document.charge_id
|
|
247
|
-
? getChargeBusinesses(document.charge_id, injector).then(res => res.mainBusinessId)
|
|
248
|
-
: Promise.resolve(undefined);
|
|
249
|
-
|
|
250
|
-
const [charge, transactions, mainBusinessId] = await Promise.all([
|
|
251
|
-
chargePromise,
|
|
252
|
-
transactionsPromise,
|
|
253
|
-
mainBusinessIdPromise,
|
|
254
|
-
]);
|
|
255
|
-
|
|
256
|
-
if (!charge) {
|
|
257
|
-
throw new GraphQLError(`Charge with ID "${document.charge_id}" not found`);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const clientPromise = mainBusinessId
|
|
261
|
-
? injector.get(ClientsProvider).getClientByIdLoader.load(mainBusinessId)
|
|
262
|
-
: Promise.resolve(undefined);
|
|
263
|
-
|
|
264
|
-
const openIssuedDocumentPromise = injector
|
|
265
|
-
.get(IssuedDocumentsProvider)
|
|
266
|
-
.getIssuedDocumentsByIdLoader.load(document.id);
|
|
267
|
-
|
|
268
|
-
const vatTypePromise = deduceVatTypeFromBusiness(injector, locality, mainBusinessId);
|
|
269
|
-
|
|
270
|
-
const [client, openIssuedDocument, vatType] = await Promise.all([
|
|
271
|
-
clientPromise,
|
|
272
|
-
openIssuedDocumentPromise,
|
|
273
|
-
vatTypePromise,
|
|
274
|
-
]);
|
|
275
|
-
|
|
276
|
-
if (!openIssuedDocument) {
|
|
277
|
-
throw new GraphQLError(
|
|
278
|
-
`Document with ID "${document.id}" doesn't seem like a document we issued`,
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (openIssuedDocument.status !== 'OPEN') {
|
|
283
|
-
throw new GraphQLError(`Document with ID "${document.id}" is closed`);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const greenInvoiceDocumentPromise: Promise<_DOLLAR_defs_Document | null> = injector
|
|
287
|
-
.get(GreenInvoiceClientProvider)
|
|
288
|
-
.documentLoader.load(openIssuedDocument.external_id)
|
|
289
|
-
.then(res => {
|
|
290
|
-
if (!res) {
|
|
291
|
-
console.error(
|
|
292
|
-
'Failed to fetch document from Green Invoice',
|
|
293
|
-
openIssuedDocument.external_id,
|
|
294
|
-
);
|
|
295
|
-
return null;
|
|
296
|
-
}
|
|
297
|
-
return res;
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
const paymentPromise = getPaymentsFromTransactions(injector, transactions ?? []);
|
|
301
|
-
|
|
302
|
-
const greenInvoiceClientId = validateClientIntegrations(
|
|
303
|
-
client?.integrations ?? {},
|
|
304
|
-
).greenInvoiceId;
|
|
305
|
-
if (!greenInvoiceClientId) {
|
|
306
|
-
throw new GraphQLError(
|
|
307
|
-
`Green invoice integration missing for business ID="${mainBusinessId}"`,
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const [greenInvoiceDocument, payment, { documentsCurrency }] = await Promise.all([
|
|
312
|
-
greenInvoiceDocumentPromise,
|
|
313
|
-
paymentPromise,
|
|
314
|
-
getChargeDocumentsMeta(charge.id, injector),
|
|
315
|
-
]);
|
|
316
|
-
|
|
317
|
-
if (!greenInvoiceDocument) {
|
|
318
|
-
throw new GraphQLError(
|
|
319
|
-
`Document with ID "${document.id}" doesn't have a Green Invoice matching document`,
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const income = getIncomeRecordsFromDocuments([greenInvoiceDocument]);
|
|
324
|
-
|
|
325
|
-
const type = getTypeFromDocumentsAndTransactions(
|
|
326
|
-
[{ type: normalizeDocumentType(document.type) }],
|
|
327
|
-
transactions ?? [],
|
|
328
|
-
);
|
|
329
|
-
|
|
330
|
-
const linkedDocsAttributes = getLinkedDocumentsAttributes(
|
|
331
|
-
[openIssuedDocument],
|
|
332
|
-
type === DocumentType.CreditInvoice,
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
let remarks = greenInvoiceDocument.remarks;
|
|
336
|
-
switch (type) {
|
|
337
|
-
case DocumentType.Receipt:
|
|
338
|
-
case DocumentType.InvoiceReceipt:
|
|
339
|
-
remarks = `${getDocumentNameFromType(type)} for ${getDocumentNameFromGreenInvoiceType(greenInvoiceDocument.type)} ${greenInvoiceDocument.number}`;
|
|
340
|
-
break;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (!remarks && client?.remark) {
|
|
344
|
-
remarks = client.remark;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const documentDate = getDocumentDateOutOfTransactions(transactions ?? []);
|
|
348
|
-
|
|
349
|
-
const draft: ResolversTypes['DocumentDraft'] = {
|
|
350
|
-
remarks,
|
|
351
|
-
// description: ____,
|
|
352
|
-
// footer: ____,
|
|
353
|
-
type,
|
|
354
|
-
date: documentDate,
|
|
355
|
-
dueDate: dateToTimelessDateString(endOfMonth(new Date())),
|
|
356
|
-
language: 'ENGLISH',
|
|
357
|
-
currency: (documentsCurrency || defaultCryptoConversionFiatCurrency) as Currency,
|
|
358
|
-
vatType,
|
|
359
|
-
rounding: false,
|
|
360
|
-
signed: true,
|
|
361
|
-
client,
|
|
362
|
-
income,
|
|
363
|
-
payment,
|
|
364
|
-
// linkedPaymentId: ____,
|
|
365
|
-
// maxPayments: _____,
|
|
366
|
-
// discount: _____,
|
|
367
|
-
...linkedDocsAttributes,
|
|
368
|
-
};
|
|
369
|
-
return draft;
|
|
370
|
-
},
|
|
371
|
-
periodicalDocumentDrafts: async (_, { issueMonth }, { injector }) => {
|
|
372
|
-
const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
|
|
373
|
-
const monthlyBillingCycle: BillingCycle = 'MONTHLY';
|
|
374
|
-
const monthlyContracts = openContracts.filter(
|
|
375
|
-
contract => contract.billing_cycle.toLocaleUpperCase() === monthlyBillingCycle,
|
|
376
|
-
);
|
|
377
|
-
const drafts = await Promise.all(
|
|
378
|
-
monthlyContracts.map(async contract =>
|
|
379
|
-
convertContractToDraft(contract, injector, issueMonth),
|
|
380
|
-
),
|
|
381
|
-
);
|
|
382
|
-
|
|
383
|
-
return drafts;
|
|
384
|
-
},
|
|
385
|
-
periodicalDocumentDraftsByContracts: async (_, { issueMonth, contractIds }, { injector }) => {
|
|
386
|
-
const contracts = await injector
|
|
387
|
-
.get(ContractsProvider)
|
|
388
|
-
.getContractsByIdLoader.loadMany(contractIds)
|
|
389
|
-
.then(res => res.filter(c => !!c && !(c instanceof Error)) as IGetContractsByIdsResult[]);
|
|
390
|
-
|
|
391
|
-
const drafts = await Promise.all(
|
|
392
|
-
contracts.map(async contract => convertContractToDraft(contract, injector, issueMonth)),
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
return drafts;
|
|
396
|
-
},
|
|
397
|
-
clientMonthlyChargeDraft: async (_, { clientId, issueMonth }, { injector }) => {
|
|
398
|
-
const openContracts = await injector.get(ContractsProvider).getAllOpenContracts();
|
|
399
|
-
const contract = openContracts.find(openContract => openContract.client_id === clientId);
|
|
400
|
-
|
|
401
|
-
if (!contract) {
|
|
402
|
-
throw new GraphQLError(`Contract not found for client ID="${clientId}"`);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const businessPromise = injector
|
|
406
|
-
.get(BusinessesProvider)
|
|
407
|
-
.getBusinessByIdLoader.load(contract.client_id);
|
|
408
|
-
const clientPromise = injector
|
|
409
|
-
.get(ClientsProvider)
|
|
410
|
-
.getClientByIdLoader.load(contract.client_id);
|
|
411
|
-
const [business, client] = await Promise.all([businessPromise, clientPromise]);
|
|
412
|
-
|
|
413
|
-
if (!business) {
|
|
414
|
-
throw new GraphQLError(`Business ID="${contract.client_id}" not found`);
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (!client) {
|
|
418
|
-
throw new GraphQLError(`Client not found for business ID="${contract.client_id}"`);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
const greenInvoiceId = validateClientIntegrations(client.integrations)?.greenInvoiceId;
|
|
422
|
-
|
|
423
|
-
if (!greenInvoiceId) {
|
|
424
|
-
throw new GraphQLError(
|
|
425
|
-
`Green invoice match not found for business ID="${contract.client_id}"`,
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
const today = issueMonth ? addMonths(new Date(issueMonth), 1) : new Date();
|
|
430
|
-
const monthStart = dateToTimelessDateString(startOfMonth(today));
|
|
431
|
-
const monthEnd = dateToTimelessDateString(endOfMonth(today));
|
|
432
|
-
const year = today.getFullYear() + (today.getMonth() === 0 ? -1 : 0);
|
|
433
|
-
const month = format(subMonths(today, 1), 'MMMM');
|
|
434
|
-
|
|
435
|
-
const draft: ResolversTypes['DocumentDraft'] = {
|
|
436
|
-
remarks: `${contract.purchase_orders[0] ? `PO: ${contract.purchase_orders[0]}${contract.remarks ? ', ' : ''}` : ''}${contract.remarks ?? ''}`,
|
|
437
|
-
description: `GraphQL Hive Enterprise License - ${month} ${year}`,
|
|
438
|
-
type: normalizeDocumentType(contract.document_type),
|
|
439
|
-
date: monthStart,
|
|
440
|
-
dueDate: monthEnd,
|
|
441
|
-
language: 'ENGLISH',
|
|
442
|
-
currency: contract.currency as Currency,
|
|
443
|
-
vatType: 'EXEMPT',
|
|
444
|
-
rounding: false,
|
|
445
|
-
signed: true,
|
|
446
|
-
client,
|
|
447
|
-
income: [
|
|
448
|
-
{
|
|
449
|
-
description: `GraphQL Hive Enterprise License - ${month} ${year}`,
|
|
450
|
-
quantity: 1,
|
|
451
|
-
price: contract.amount,
|
|
452
|
-
currency: contract.currency as Currency,
|
|
453
|
-
vatType: 'EXEMPT',
|
|
454
|
-
},
|
|
455
|
-
],
|
|
456
|
-
};
|
|
457
|
-
|
|
458
|
-
return draft;
|
|
459
|
-
},
|
|
460
|
-
},
|
|
461
|
-
Mutation: {
|
|
462
|
-
issueGreenInvoiceDocuments: async (
|
|
463
|
-
_,
|
|
464
|
-
{ generateDocumentsInfo },
|
|
465
|
-
{ injector, adminContext: { defaultAdminBusinessId } },
|
|
466
|
-
) => {
|
|
467
|
-
const errors: string[] = [];
|
|
468
|
-
|
|
469
|
-
await Promise.all(
|
|
470
|
-
generateDocumentsInfo.map(document => {
|
|
471
|
-
executeDocumentIssue(
|
|
472
|
-
injector,
|
|
473
|
-
defaultAdminBusinessId,
|
|
474
|
-
document,
|
|
475
|
-
undefined,
|
|
476
|
-
true,
|
|
477
|
-
undefined,
|
|
478
|
-
true,
|
|
479
|
-
).catch(e => {
|
|
480
|
-
console.error(e);
|
|
481
|
-
errors.push(`${document.client?.name ?? document.client?.id}: ${e.message}`);
|
|
482
|
-
});
|
|
483
|
-
}),
|
|
484
|
-
);
|
|
485
|
-
|
|
486
|
-
return {
|
|
487
|
-
success: true,
|
|
488
|
-
errors,
|
|
489
|
-
};
|
|
490
|
-
},
|
|
491
|
-
issueGreenInvoiceDocument: async (
|
|
492
|
-
_,
|
|
493
|
-
{ input: initialInput, emailContent, attachment, chargeId, sendEmail = false },
|
|
494
|
-
{ injector, adminContext: { defaultAdminBusinessId } },
|
|
495
|
-
) => {
|
|
496
|
-
return executeDocumentIssue(
|
|
497
|
-
injector,
|
|
498
|
-
defaultAdminBusinessId,
|
|
499
|
-
initialInput,
|
|
500
|
-
emailContent ?? undefined,
|
|
501
|
-
attachment ?? undefined,
|
|
502
|
-
chargeId ?? undefined,
|
|
503
|
-
sendEmail ?? false,
|
|
504
|
-
);
|
|
505
|
-
},
|
|
506
|
-
previewDocument: async (_, { input: initialInput }, { injector }) => {
|
|
507
|
-
const input = await convertDocumentInputIntoGreenInvoiceInput(initialInput, injector);
|
|
508
|
-
const document = await injector.get(GreenInvoiceClientProvider).previewDocuments({ input });
|
|
509
|
-
|
|
510
|
-
if (!document) {
|
|
511
|
-
throw new GraphQLError('Failed to generate document preview');
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
if ('errorMessage' in document) {
|
|
515
|
-
console.error('Failed to generate document preview', document);
|
|
516
|
-
throw new GraphQLError(
|
|
517
|
-
`Failed to generate document preview, Green Invoice returned: ${document.errorMessage}`,
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if ('file' in document && document.file) {
|
|
522
|
-
return document.file;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
console.error('Document preview does not contain a file', document);
|
|
526
|
-
throw new GraphQLError('Document preview does not contain a file');
|
|
527
|
-
},
|
|
528
|
-
},
|
|
529
|
-
};
|