@bisondesk/documents-sdk 1.0.441 → 1.0.443
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/apis/document-drafts.d.ts +7 -1
- package/lib/apis/document-drafts.d.ts.map +1 -1
- package/lib/apis/document-drafts.js +38 -0
- package/lib/apis/document-drafts.js.map +1 -1
- package/lib/types/document-categories.d.ts +1 -0
- package/lib/types/document-categories.d.ts.map +1 -1
- package/lib/types/document-categories.js.map +1 -1
- package/lib/types/legacy.d.ts +1 -0
- package/lib/types/legacy.d.ts.map +1 -1
- package/lib/types/legacy.js.map +1 -1
- package/lib/utils/finance.d.ts +3 -2
- package/lib/utils/finance.d.ts.map +1 -1
- package/lib/utils/finance.js +31 -3
- package/lib/utils/finance.js.map +1 -1
- package/package.json +1 -1
- package/src/apis/document-drafts.ts +66 -0
- package/src/types/document-categories.ts +1 -0
- package/src/types/legacy.ts +1 -1
- package/src/utils/finance.ts +60 -7
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TransactionType } from '@bisondesk/commons-sdk/types';
|
|
2
|
+
import { CostCenter, DocumentCategory, DocumentCategoryType, FinanceDocumentLineCategory } from '../types/document-categories.js';
|
|
2
3
|
import { DocumentValidationRulesResults } from '../types/documents.js';
|
|
3
4
|
export declare const getDocumentCategory: <T extends DocumentCategory>(tenantId: string, branchId: string, docCategoryId: string) => Promise<T | undefined>;
|
|
5
|
+
export declare const listDocumentCategories: <T extends DocumentCategory>(tenantId: string, branchId: string, filter: {
|
|
6
|
+
type: DocumentCategoryType;
|
|
7
|
+
transactionType?: TransactionType;
|
|
8
|
+
}) => Promise<T[]>;
|
|
4
9
|
export declare const listFinanceLineCategories: (tenantId: string, branchId: string, docCategoryId: string) => Promise<FinanceDocumentLineCategory[]>;
|
|
10
|
+
export declare const listVehicleFinanceLineCategories: (tenantId: string, branchId: string) => Promise<FinanceDocumentLineCategory[]>;
|
|
5
11
|
export declare const listCostCenters: (tenantId: string, branchId: string) => Promise<CostCenter[]>;
|
|
6
12
|
export declare const getLastValidationRulesResults: (tenantId: string, documentId: string) => Promise<DocumentValidationRulesResults>;
|
|
7
13
|
//# sourceMappingURL=document-drafts.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document-drafts.d.ts","sourceRoot":"/","sources":["apis/document-drafts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"document-drafts.d.ts","sourceRoot":"/","sources":["apis/document-drafts.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,oBAAoB,EACpB,2BAA2B,EAC5B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,8BAA8B,EAAE,MAAM,uBAAuB,CAAC;AAEvE,eAAO,MAAM,mBAAmB,GAAU,CAAC,SAAS,gBAAgB,YACxD,MAAM,YACN,MAAM,iBACD,MAAM,KACpB,OAAO,CAAC,CAAC,GAAG,SAAS,CAwBvB,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAU,CAAC,SAAS,gBAAgB,YAC3D,MAAM,YACN,MAAM,UACR;IACN,IAAI,EAAE,oBAAoB,CAAC;IAC3B,eAAe,CAAC,EAAE,eAAe,CAAC;CACnC,KACA,OAAO,CAAC,CAAC,EAAE,CA2Bb,CAAC;AAEF,eAAO,MAAM,yBAAyB,aAC1B,MAAM,YACN,MAAM,iBACD,MAAM,KACpB,OAAO,CAAC,2BAA2B,EAAE,CAwBvC,CAAC;AAEF,eAAO,MAAM,gCAAgC,aACjC,MAAM,YACN,MAAM,KACf,OAAO,CAAC,2BAA2B,EAAE,CAuBvC,CAAC;AAEF,eAAO,MAAM,eAAe,aAChB,MAAM,YACN,MAAM,KACf,OAAO,CAAC,UAAU,EAAE,CAuBtB,CAAC;AAEF,eAAO,MAAM,6BAA6B,aAC9B,MAAM,cACJ,MAAM,KACjB,OAAO,CAAC,8BAA8B,CAuBxC,CAAC"}
|
|
@@ -22,6 +22,25 @@ export const getDocumentCategory = async (tenantId, branchId, docCategoryId) =>
|
|
|
22
22
|
status: response.status,
|
|
23
23
|
});
|
|
24
24
|
};
|
|
25
|
+
export const listDocumentCategories = async (tenantId, branchId, filter) => {
|
|
26
|
+
const auth = await getAdminAuth();
|
|
27
|
+
const response = await fetch(`${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/document-categories?type=${filter.type}${filter.transactionType ? '&transactionType=' + filter.transactionType : ''}`, {
|
|
28
|
+
headers: {
|
|
29
|
+
Authorization: auth,
|
|
30
|
+
[TENANT_ID_ADMIN_HEADER]: tenantId,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
if (response.status === 200) {
|
|
34
|
+
return response.json();
|
|
35
|
+
}
|
|
36
|
+
const body = await response.text();
|
|
37
|
+
throw new XError('documents-sdk.drafts.list-doc-categories-fail', {
|
|
38
|
+
tenantId,
|
|
39
|
+
branchId,
|
|
40
|
+
response: body,
|
|
41
|
+
status: response.status,
|
|
42
|
+
});
|
|
43
|
+
};
|
|
25
44
|
export const listFinanceLineCategories = async (tenantId, branchId, docCategoryId) => {
|
|
26
45
|
const auth = await getAdminAuth();
|
|
27
46
|
const response = await fetch(`${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/document-categories/${docCategoryId}/line-categories`, {
|
|
@@ -42,6 +61,25 @@ export const listFinanceLineCategories = async (tenantId, branchId, docCategoryI
|
|
|
42
61
|
status: response.status,
|
|
43
62
|
});
|
|
44
63
|
};
|
|
64
|
+
export const listVehicleFinanceLineCategories = async (tenantId, branchId) => {
|
|
65
|
+
const auth = await getAdminAuth();
|
|
66
|
+
const response = await fetch(`${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/vehicle-line-categories`, {
|
|
67
|
+
headers: {
|
|
68
|
+
Authorization: auth,
|
|
69
|
+
[TENANT_ID_ADMIN_HEADER]: tenantId,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
if (response.status === 200) {
|
|
73
|
+
return response.json();
|
|
74
|
+
}
|
|
75
|
+
const body = await response.text();
|
|
76
|
+
throw new XError('documents-sdk.drafts.get-vehicle-lines-categories-fail', {
|
|
77
|
+
tenantId,
|
|
78
|
+
branchId,
|
|
79
|
+
response: body,
|
|
80
|
+
status: response.status,
|
|
81
|
+
});
|
|
82
|
+
};
|
|
45
83
|
export const listCostCenters = async (tenantId, branchId) => {
|
|
46
84
|
const auth = await getAdminAuth();
|
|
47
85
|
const response = await fetch(`${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/cost-centers`, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document-drafts.js","sourceRoot":"/","sources":["apis/document-drafts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"document-drafts.js","sourceRoot":"/","sources":["apis/document-drafts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D,OAAO,KAAmB,MAAM,YAAY,CAAC;AAS7C,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACtC,QAAgB,EAChB,QAAgB,EAChB,aAAqB,EACG,EAAE;IAC1B,MAAM,IAAI,GAAG,MAAM,YAAY,EAAE,CAAC;IAClC,MAAM,QAAQ,GAAa,MAAM,KAAK,CACpC,GAAG,OAAO,CAAC,GAAG,CAAC,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AC,EAAE,QAAQ;SACnC;KACF,CACF,CAAC;IAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QAC5B,OAAO,QAAQ,CAAC,IAAI,EAAS,CAAC;IAChC,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACnC,MAAM,IAAI,MAAM,CAAC,kDAAkD,EAAE;QACnE,QAAQ;QACR,UAAU;QACV,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,QAAQ,CAAC,MAAM;KACxB,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { TENANT_ID_ADMIN_HEADER } from '@bisondesk/commons-sdk/constants';\nimport { XError } from '@bisondesk/commons-sdk/errors';\nimport { getAdminAuth } from '@bisondesk/commons-sdk/fetch';\nimport { TransactionType } from '@bisondesk/commons-sdk/types';\nimport fetch, { Response } from 'node-fetch';\nimport {\n CostCenter,\n DocumentCategory,\n DocumentCategoryType,\n FinanceDocumentLineCategory,\n} from '../types/document-categories.js';\nimport { DocumentValidationRulesResults } from '../types/documents.js';\n\nexport const getDocumentCategory = async <T extends DocumentCategory>(\n tenantId: string,\n branchId: string,\n docCategoryId: string\n): Promise<T | undefined> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/document-categories/${docCategoryId}`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.get-doc-category-fail', {\n tenantId,\n branchId,\n docCategoryId,\n response: body,\n status: response.status,\n });\n};\n\nexport const listDocumentCategories = async <T extends DocumentCategory>(\n tenantId: string,\n branchId: string,\n filter: {\n type: DocumentCategoryType;\n transactionType?: TransactionType;\n }\n): Promise<T[]> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${\n process.env.DOCUMENT_DRAFTS_API_ORIGIN\n }/api/documents/settings/${branchId}/document-categories?type=${filter.type}${\n filter.transactionType ? '&transactionType=' + filter.transactionType : ''\n }`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.list-doc-categories-fail', {\n tenantId,\n branchId,\n response: body,\n status: response.status,\n });\n};\n\nexport const listFinanceLineCategories = async (\n tenantId: string,\n branchId: string,\n docCategoryId: string\n): Promise<FinanceDocumentLineCategory[]> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/document-categories/${docCategoryId}/line-categories`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.get-doc-lines-categories-fail', {\n tenantId,\n branchId,\n docCategoryId,\n response: body,\n status: response.status,\n });\n};\n\nexport const listVehicleFinanceLineCategories = async (\n tenantId: string,\n branchId: string\n): Promise<FinanceDocumentLineCategory[]> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/vehicle-line-categories`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.get-vehicle-lines-categories-fail', {\n tenantId,\n branchId,\n response: body,\n status: response.status,\n });\n};\n\nexport const listCostCenters = async (\n tenantId: string,\n branchId: string\n): Promise<CostCenter[]> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/cost-centers`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.get-cost-centers-fail', {\n tenantId,\n branchId,\n response: body,\n status: response.status,\n });\n};\n\nexport const getLastValidationRulesResults = async (\n tenantId: string,\n documentId: string\n): Promise<DocumentValidationRulesResults> => {\n const auth = await getAdminAuth();\n const response: Response = await fetch(\n `${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/document-drafts/status-log/last-validation-results/${documentId}`,\n {\n headers: {\n Authorization: auth,\n [TENANT_ID_ADMIN_HEADER]: tenantId,\n },\n }\n );\n\n if (response.status === 200) {\n return response.json() as any;\n }\n\n const body = await response.text();\n throw new XError('documents-sdk.drafts.get-validation-results-fail', {\n tenantId,\n documentId,\n response: body,\n status: response.status,\n });\n};\n"]}
|
|
@@ -43,6 +43,7 @@ export type FinanceDocumentLineCategory = {
|
|
|
43
43
|
description: string;
|
|
44
44
|
associationMandatory: boolean;
|
|
45
45
|
glAccount: string;
|
|
46
|
+
isVehicleCategory?: boolean;
|
|
46
47
|
validationRulesConfigs?: ValidationRulesConfigs;
|
|
47
48
|
};
|
|
48
49
|
export type NewFinanceDocumentLineCategory = Omit<FinanceDocumentLineCategory, 'id' | 'createdAt'>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document-categories.d.ts","sourceRoot":"/","sources":["types/document-categories.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,oBAAY,0BAA0B;IACpC,QAAQ,aAAa;IACrB,OAAO,YAAY;CACpB;AAED,oBAAY,oBAAoB;IAC9B,cAAc,mBAAmB;IACjC,OAAO,YAAY;CACpB;AAED,KAAK,oBAAoB,GAAG;IAC1B,SAAS,EAAE,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M,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,cAAc,GAAG;IACxC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,cAAc,GAAG;IAC3C,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,EAAE,CAAC,EAAE,SAAS,CAAC;CAChB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document-categories.js","sourceRoot":"/","sources":["types/document-categories.ts"],"names":[],"mappings":"AAGA,MAAM,CAAN,IAAY,0BAGX;AAHD,WAAY,0BAA0B;IACpC,mDAAqB,CAAA;IACrB,iDAAmB,CAAA;AACrB,CAAC,EAHW,0BAA0B,KAA1B,0BAA0B,QAGrC;AAED,MAAM,CAAN,IAAY,oBAGX;AAHD,WAAY,oBAAoB;IAC9B,yDAAiC,CAAA;IACjC,2CAAmB,CAAA;AACrB,CAAC,EAHW,oBAAoB,KAApB,oBAAoB,QAG/B","sourcesContent":["import { TransactionType } from '@bisondesk/commons-sdk/types';\nimport { ValidationRulesConfigs } from './rules.js';\n\nexport enum DocumentCategoryVisibility {\n Standard = 'standard',\n Private = 'private',\n}\n\nexport enum DocumentCategoryType {\n Administrative = 'administrative',\n Finance = 'finance',\n}\n\ntype BaseDocumentCategory = {\n createdAt: string;\n branchId: string;\n id: string;\n type: DocumentCategoryType;\n title: string;\n};\n\nexport type AdministrativeDocumentCategory = BaseDocumentCategory & {\n type: DocumentCategoryType.Administrative;\n subtype: 'generic' | 'deal';\n validators?: undefined;\n};\n\nexport type FinanceDocumentCategory = BaseDocumentCategory & {\n type: DocumentCategoryType.Finance;\n journal: string;\n visibility: DocumentCategoryVisibility;\n transactionType: TransactionType;\n validators?: string[];\n bankAccounts?: string[];\n};\n\nexport type DocumentCategory = AdministrativeDocumentCategory | FinanceDocumentCategory;\n\nexport type NewDocumentCategory = (\n | Omit<FinanceDocumentCategory, 'id' | 'createdAt'>\n | Omit<AdministrativeDocumentCategory, 'id' | 'createdAt'>\n) & {\n createdAt?: undefined;\n id?: undefined;\n};\n\nexport type FinanceDocumentLineCategory = {\n createdAt: string;\n branchId: string;\n categoryId: string;\n costCenterId?: string;\n id: string;\n title: string;\n description: string;\n associationMandatory: boolean;\n glAccount: string;\n\n validationRulesConfigs?: ValidationRulesConfigs;\n};\n\nexport type NewFinanceDocumentLineCategory = Omit<FinanceDocumentLineCategory, 'id' | 'createdAt'>;\n\ntype BaseCostCenter = {\n branchId: string;\n name: string;\n accountingCode?: string;\n};\n\nexport type CostCenter = BaseCostCenter & {\n createdAt: string;\n createdBy: string;\n id: string;\n};\n\nexport type NewCostCenter = BaseCostCenter & {\n createdAt?: undefined;\n createdBy?: string;\n id?: undefined;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"document-categories.js","sourceRoot":"/","sources":["types/document-categories.ts"],"names":[],"mappings":"AAGA,MAAM,CAAN,IAAY,0BAGX;AAHD,WAAY,0BAA0B;IACpC,mDAAqB,CAAA;IACrB,iDAAmB,CAAA;AACrB,CAAC,EAHW,0BAA0B,KAA1B,0BAA0B,QAGrC;AAED,MAAM,CAAN,IAAY,oBAGX;AAHD,WAAY,oBAAoB;IAC9B,yDAAiC,CAAA;IACjC,2CAAmB,CAAA;AACrB,CAAC,EAHW,oBAAoB,KAApB,oBAAoB,QAG/B","sourcesContent":["import { TransactionType } from '@bisondesk/commons-sdk/types';\nimport { ValidationRulesConfigs } from './rules.js';\n\nexport enum DocumentCategoryVisibility {\n Standard = 'standard',\n Private = 'private',\n}\n\nexport enum DocumentCategoryType {\n Administrative = 'administrative',\n Finance = 'finance',\n}\n\ntype BaseDocumentCategory = {\n createdAt: string;\n branchId: string;\n id: string;\n type: DocumentCategoryType;\n title: string;\n};\n\nexport type AdministrativeDocumentCategory = BaseDocumentCategory & {\n type: DocumentCategoryType.Administrative;\n subtype: 'generic' | 'deal';\n validators?: undefined;\n};\n\nexport type FinanceDocumentCategory = BaseDocumentCategory & {\n type: DocumentCategoryType.Finance;\n journal: string;\n visibility: DocumentCategoryVisibility;\n transactionType: TransactionType;\n validators?: string[];\n bankAccounts?: string[];\n};\n\nexport type DocumentCategory = AdministrativeDocumentCategory | FinanceDocumentCategory;\n\nexport type NewDocumentCategory = (\n | Omit<FinanceDocumentCategory, 'id' | 'createdAt'>\n | Omit<AdministrativeDocumentCategory, 'id' | 'createdAt'>\n) & {\n createdAt?: undefined;\n id?: undefined;\n};\n\nexport type FinanceDocumentLineCategory = {\n createdAt: string;\n branchId: string;\n categoryId: string;\n costCenterId?: string;\n id: string;\n title: string;\n description: string;\n associationMandatory: boolean;\n glAccount: string;\n\n isVehicleCategory?: boolean;\n validationRulesConfigs?: ValidationRulesConfigs;\n};\n\nexport type NewFinanceDocumentLineCategory = Omit<FinanceDocumentLineCategory, 'id' | 'createdAt'>;\n\ntype BaseCostCenter = {\n branchId: string;\n name: string;\n accountingCode?: string;\n};\n\nexport type CostCenter = BaseCostCenter & {\n createdAt: string;\n createdBy: string;\n id: string;\n};\n\nexport type NewCostCenter = BaseCostCenter & {\n createdAt?: undefined;\n createdBy?: string;\n id?: undefined;\n};\n"]}
|
package/lib/types/legacy.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legacy.d.ts","sourceRoot":"/","sources":["types/legacy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,MAAM,MAAM,aAAa,GAAG;IAC1B,YAAY,EAAE;QACZ,MAAM,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;QAC/C,KAAK,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,SAAS,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SA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AAM,CAAC;IAEf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,WAAW,CAAC,EAAE;QACZ,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IAEF,KAAK,EAAE;QACL,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IAEF,mBAAmB,CAAC,EAAE;QACpB,EAAE,EAAE,MAAM,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,CAAC,EAAE,MAAM,CAAC;QAEnB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,IAAI,CAAC,EAAE,MAAM,CAAC;QAEd,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;CACH,CAAC"}
|
package/lib/types/legacy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legacy.js","sourceRoot":"/","sources":["types/legacy.ts"],"names":[],"mappings":"","sourcesContent":["import { AttachmentValue } from '@bisondesk/commons-sdk/types';\n\nexport type FinanceLineV1 = {\n associations: {\n others: { vehicleId: string; label: string }[];\n sales: { id: string; vehicleId: string; label: string }[];\n purchases: { id: string; vehicleId: string; label: string }[];\n };\n description: string;\n id: string;\n lineCode: string;\n quantity: string;\n unitPriceExclusive: string;\n vatCode: string;\n vatCoefficient: string;\n};\n\nexport type FinanceDocumentV1 = {\n id: string;\n branchId: string;\n version?: 1.0;\n\n type: 'Sale Finance' | 'Purchase Finance';\n subtype: 'Invoice' | 'Credit Note' | 'Proforma' | 'Purchase Order';\n reference: string;\n\n attachment: AttachmentValue;\n lines: FinanceLineV1[];\n accountingOnlyLines?: FinanceLineV1[];\n notes?: string;\n\n createdAt: string;\n createdBy: string;\n issueDate: string;\n paidAt?: string;\n dueDate?: string;\n\n salesperson?: {\n name: string;\n username: string;\n };\n\n total: {\n amountExcl: string;\n amount: string;\n vatAmount: string;\n };\n\n organizationSummary?: {\n id: string;\n code: string;\n name: string;\n country: string;\n vatNumber?: string;\n\n addressLine1?: string;\n addressLine2?: string;\n addressLine3?: string;\n postcode?: string;\n city?: string;\n\n email?: string;\n phone?: string;\n fax?: string;\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"legacy.js","sourceRoot":"/","sources":["types/legacy.ts"],"names":[],"mappings":"","sourcesContent":["import { AttachmentValue } from '@bisondesk/commons-sdk/types';\n\nexport type FinanceLineV1 = {\n associations: {\n others: { vehicleId: string; label: string }[];\n sales: { id: string; vehicleId: string; opportunityId?: string; label: string }[];\n purchases: { id: string; vehicleId: string; label: string }[];\n };\n description: string;\n id: string;\n lineCode: string;\n quantity: string;\n unitPriceExclusive: string;\n vatCode: string;\n vatCoefficient: string;\n};\n\nexport type FinanceDocumentV1 = {\n id: string;\n branchId: string;\n version?: 1.0;\n\n type: 'Sale Finance' | 'Purchase Finance';\n subtype: 'Invoice' | 'Credit Note' | 'Proforma' | 'Purchase Order';\n reference: string;\n\n attachment: AttachmentValue;\n lines: FinanceLineV1[];\n accountingOnlyLines?: FinanceLineV1[];\n notes?: string;\n\n createdAt: string;\n createdBy: string;\n issueDate: string;\n paidAt?: string;\n dueDate?: string;\n\n salesperson?: {\n name: string;\n username: string;\n };\n\n total: {\n amountExcl: string;\n amount: string;\n vatAmount: string;\n };\n\n organizationSummary?: {\n id: string;\n code: string;\n name: string;\n country: string;\n vatNumber?: string;\n\n addressLine1?: string;\n addressLine2?: string;\n addressLine3?: string;\n postcode?: string;\n city?: string;\n\n email?: string;\n phone?: string;\n fax?: string;\n };\n};\n"]}
|
package/lib/utils/finance.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { Decimal } from 'decimal.js';
|
|
|
2
2
|
import { DocumentDraft, FinanceDocument, FinanceDocumentGenerateRequest, FinanceDocumentV2, NewDocumentDraft } from '../types/documents.js';
|
|
3
3
|
export declare const documentGoesToAccounting: (doc: FinanceDocumentV2 | DocumentDraft<FinanceDocumentV2> | FinanceDocumentGenerateRequest) => boolean;
|
|
4
4
|
export declare const getAccountingDescription: (doc: FinanceDocumentV2) => string;
|
|
5
|
-
export declare const getTotalExcl: <T extends FinanceDocument["lines"]>(lines: T, unitPrice: (line: T[0]) => any) => Decimal;
|
|
6
|
-
export declare const getTotalVat: <T extends FinanceDocument["lines"], L = T[0]>(lines: T, unitPrice: (doc: L) => any, vatCoefficient: (doc: L) => any) => Decimal;
|
|
5
|
+
export declare const getTotalExcl: <T extends FinanceDocument["lines"]>(lines: T, unitPrice: (line: T[0], index: number) => any) => Decimal;
|
|
6
|
+
export declare const getTotalVat: <T extends FinanceDocument["lines"], L = T[0]>(lines: T, unitPrice: (doc: L, index: number) => any, vatCoefficient: (doc: L, index: number) => any) => Decimal;
|
|
7
7
|
export declare const getDocumentTotals: (doc: FinanceDocument | DocumentDraft<FinanceDocumentV2> | NewDocumentDraft<FinanceDocumentV2> | FinanceDocumentGenerateRequest) => {
|
|
8
8
|
totalExcl: Decimal;
|
|
9
9
|
totalVat: Decimal;
|
|
@@ -32,4 +32,5 @@ export declare const getLineAmounts: (unitPriceExcl: string, quantity: string, v
|
|
|
32
32
|
vatAmount: string;
|
|
33
33
|
};
|
|
34
34
|
export declare const isGeneratingAttachment: (doc: FinanceDocument) => boolean;
|
|
35
|
+
export declare const filterDocumentLinesForRecord: (doc: FinanceDocument, recordId: string) => FinanceDocument;
|
|
35
36
|
//# sourceMappingURL=finance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"finance.d.ts","sourceRoot":"/","sources":["utils/finance.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGrC,OAAO,EAEL,aAAa,EACb,eAAe,EACf,8BAA8B,EAG9B,iBAAiB,
|
|
1
|
+
{"version":3,"file":"finance.d.ts","sourceRoot":"/","sources":["utils/finance.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAGrC,OAAO,EAEL,aAAa,EACb,eAAe,EACf,8BAA8B,EAG9B,iBAAiB,EAGjB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAG/B,eAAO,MAAM,wBAAwB,QAC9B,iBAAiB,GAAG,aAAa,CAAC,iBAAiB,CAAC,GAAG,8BAA8B,KACzF,OAKF,CAAC;AAEF,eAAO,MAAM,wBAAwB,QAAS,iBAAiB,KAAG,MAoCjE,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,eAAe,CAAC,OAAO,CAAC,SACtD,CAAC,aACG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,GAAG,YAK9C,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC,gBACxD,CAAC,aACG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,GAAG,kBACzB,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,GAAG,YAS/C,CAAC;AAEF,eAAO,MAAM,iBAAiB,QAExB,eAAe,GACf,aAAa,CAAC,iBAAiB,CAAC,GAChC,gBAAgB,CAAC,iBAAiB,CAAC,GACnC,8BAA8B;;;;CA0BnC,CAAC;AAEF,eAAO,MAAM,uBAAuB,QAE9B,eAAe,GACf,aAAa,CAAC,iBAAiB,CAAC,GAChC,gBAAgB,CAAC,iBAAiB,CAAC,GACnC,8BAA8B,iBACnB,MAAM,oBACH;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;CACtB;;;;;;;;;;;;CAuBF,CAAC;AAKF,eAAO,MAAM,cAAc,kBAAmB,MAAM,YAAY,MAAM,WAAW,MAAM;;;;;CAYtF,CAAC;AAEF,eAAO,MAAM,sBAAsB,QAAS,eAAe,YACC,CAAC;AAK7D,eAAO,MAAM,4BAA4B,QAClC,eAAe,YACV,MAAM,KACf,eAgCF,CAAC"}
|
package/lib/utils/finance.js
CHANGED
|
@@ -3,7 +3,7 @@ import { roundMoney } from '@bisondesk/commons-sdk/money';
|
|
|
3
3
|
import { Decimal } from 'decimal.js';
|
|
4
4
|
import { uniqBy } from 'lodash-es';
|
|
5
5
|
import { GENERATING_PLACEHOLDER_ATTACHMENT } from '../constants/index.js';
|
|
6
|
-
import { FinanceDocumentSubtype, isFinanceDocumentV1, } from '../types/documents.js';
|
|
6
|
+
import { FinanceDocumentSubtype, isFinanceDocumentV1, isFinanceDocumentV2, } from '../types/documents.js';
|
|
7
7
|
export const documentGoesToAccounting = (doc) => {
|
|
8
8
|
return (!!doc.subtype &&
|
|
9
9
|
[FinanceDocumentSubtype.Invoice, FinanceDocumentSubtype.CreditNote].includes(doc.subtype));
|
|
@@ -36,12 +36,12 @@ export const getAccountingDescription = (doc) => {
|
|
|
36
36
|
export const getTotalExcl = (lines, unitPrice) => {
|
|
37
37
|
return lines.length === 0
|
|
38
38
|
? new Decimal(0)
|
|
39
|
-
: Decimal.sum(...lines.map((line) => Decimal.mul(line.quantity, unitPrice(line))));
|
|
39
|
+
: Decimal.sum(...lines.map((line, i) => Decimal.mul(line.quantity, unitPrice(line, i))));
|
|
40
40
|
};
|
|
41
41
|
export const getTotalVat = (lines, unitPrice, vatCoefficient) => {
|
|
42
42
|
return lines.length === 0
|
|
43
43
|
? new Decimal(0)
|
|
44
|
-
: Decimal.sum(...lines.map((line) => Decimal.mul(line.quantity, unitPrice(line)).mul(vatCoefficient(line))));
|
|
44
|
+
: Decimal.sum(...lines.map((line, i) => Decimal.mul(line.quantity, unitPrice(line, i)).mul(vatCoefficient(line, i))));
|
|
45
45
|
};
|
|
46
46
|
export const getDocumentTotals = (doc) => {
|
|
47
47
|
let totalVat;
|
|
@@ -100,4 +100,32 @@ export const getLineAmounts = (unitPriceExcl, quantity, vatRate) => {
|
|
|
100
100
|
};
|
|
101
101
|
};
|
|
102
102
|
export const isGeneratingAttachment = (doc) => doc.attachment.id === GENERATING_PLACEHOLDER_ATTACHMENT.id;
|
|
103
|
+
export const filterDocumentLinesForRecord = (doc, recordId) => {
|
|
104
|
+
const filteredLines = isFinanceDocumentV2(doc)
|
|
105
|
+
? doc.lines
|
|
106
|
+
.filter((line) => line.associations?.some((assoc) => assoc.recordId === recordId))
|
|
107
|
+
.map((line) => ({
|
|
108
|
+
...line,
|
|
109
|
+
unitPriceExcl: calculateDistributedPrice(line.unitPriceExcl, line.associations?.length || 1),
|
|
110
|
+
}))
|
|
111
|
+
: doc.lines
|
|
112
|
+
.filter((line) => getAllV1Associations(line.associations).some((assoc) => assoc.vehicleId === recordId ||
|
|
113
|
+
assoc.opportunityId === recordId ||
|
|
114
|
+
assoc.id === recordId))
|
|
115
|
+
.map((line) => ({
|
|
116
|
+
...line,
|
|
117
|
+
unitPriceExclusive: calculateDistributedPrice(line.unitPriceExclusive, getAllV1Associations(line.associations).length || 1),
|
|
118
|
+
}));
|
|
119
|
+
return {
|
|
120
|
+
...doc,
|
|
121
|
+
lines: filteredLines,
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
const calculateDistributedPrice = (price, count) => {
|
|
125
|
+
return new Decimal(price).div(Math.max(1, count)).toString();
|
|
126
|
+
};
|
|
127
|
+
const getAllV1Associations = (ass) => {
|
|
128
|
+
const allAss = uniqBy([...(ass?.sales ?? []), ...(ass?.purchases ?? []), ...(ass.others ?? [])], (ass) => ass.vehicleId);
|
|
129
|
+
return allAss;
|
|
130
|
+
};
|
|
103
131
|
//# sourceMappingURL=finance.js.map
|
package/lib/utils/finance.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"finance.js","sourceRoot":"/","sources":["utils/finance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,iCAAiC,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAML,sBAAsB,EAGtB,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAG/B,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,GAA0F,EACjF,EAAE;IACX,OAAO,CACL,CAAC,CAAC,GAAG,CAAC,OAAO;QACb,CAAC,sBAAsB,CAAC,OAAO,EAAE,sBAAsB,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAC1F,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,GAAsB,EAAU,EAAE;IACzE,IAAI,CAAC,CAAC,GAAG,CAAC,qBAAqB,EAAE,CAAC;QAChC,OAAO,GAAG,CAAC,qBAAqB,CAAC;IACnC,CAAC;IAED,MAAM,YAAY,GAAG,GAAG,CAAC,KAAK;QAC5B,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC;SAC3C,MAAM,CAAC,OAAO,CAAkB,CAAC;IAGpC,MAAM,mBAAmB,GAAG;QAC1B,CAAC,iBAAiB,CAAC,mBAAmB,CAAC,EAAE,iBAAiB,CAAC,QAAQ;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ACH,CAAC,CAAC,SAAS;KACd,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,aAAqB,EAAE,QAAgB,EAAE,EAAE,CAChE,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAE1D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,aAAqB,EAAE,QAAgB,EAAE,OAAe,EAAE,EAAE;IACzF,MAAM,UAAU,GAAG,aAAa,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;IAC1D,MAAM,SAAS,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC;SACpC,KAAK,CAAC,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;SACjC,KAAK,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAExC,OAAO;QACL,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE;QAC5C,UAAU,EAAE,UAAU,CAAC,QAAQ,EAAE;QACjC,aAAa;QACb,SAAS,EAAE,SAAS,CAAC,QAAQ,EAAE;KAChC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAoB,EAAE,EAAE,CAC7D,GAAG,CAAC,UAAU,CAAC,EAAE,KAAK,iCAAiC,CAAC,EAAE,CAAC","sourcesContent":["import { BusinessEntityIds } from '@bisondesk/commons-sdk/constants';\nimport { roundMoney } from '@bisondesk/commons-sdk/money';\nimport { Decimal } from 'decimal.js';\nimport { uniqBy } from 'lodash-es';\nimport { GENERATING_PLACEHOLDER_ATTACHMENT } from '../constants/index.js';\nimport {\n Association,\n DocumentDraft,\n FinanceDocument,\n FinanceDocumentGenerateRequest,\n FinanceDocumentLine,\n FinanceDocumentSubtype,\n FinanceDocumentV2,\n NewDocumentDraft,\n isFinanceDocumentV1,\n} from '../types/documents.js';\nimport { FinanceDocumentV1, FinanceLineV1 } from '../types/legacy.js';\n\nexport const documentGoesToAccounting = (\n doc: FinanceDocumentV2 | DocumentDraft<FinanceDocumentV2> | FinanceDocumentGenerateRequest\n): boolean => {\n return (\n !!doc.subtype &&\n [FinanceDocumentSubtype.Invoice, FinanceDocumentSubtype.CreditNote].includes(doc.subtype)\n );\n};\n\nexport const getAccountingDescription = (doc: FinanceDocumentV2): string => {\n if (!!doc.accountingDescription) {\n return doc.accountingDescription;\n }\n\n const associations = doc.lines\n ?.flatMap((line) => line.associations ?? [])\n .filter(Boolean) as Association[];\n\n // In the UI the user can only select one vehicle associations\n const equivalentEntityIds = {\n [BusinessEntityIds.ExternalVehicleSale]: BusinessEntityIds.Vehicles,\n };\n\n const uniqueAssociations = uniqBy(\n associations,\n (a) => `${equivalentEntityIds[a.businessEntityId] ?? a.businessEntityId}_${a.recordId}`\n );\n\n if (uniqueAssociations.length !== 1) {\n return doc.reference;\n }\n\n const firstAssociation = associations[0];\n if (\n [\n BusinessEntityIds.Vehicles,\n BusinessEntityIds.Opportunities,\n BusinessEntityIds.ExternalVehicleSale,\n ].includes(firstAssociation.businessEntityId) &&\n firstAssociation.tags != null\n ) {\n return `${firstAssociation.tags?.slice(0, 3).join(' ')} ${doc.reference}`.trim();\n }\n\n return doc.reference;\n};\n\nexport const getTotalExcl = <T extends FinanceDocument['lines']>(\n lines: T,\n unitPrice: (line: T[0]) => any\n) => {\n return lines.length === 0\n ? new Decimal(0)\n : Decimal.sum(...lines.map((line) => Decimal.mul(line.quantity, unitPrice(line))));\n};\n\nexport const getTotalVat = <T extends FinanceDocument['lines'], L = T[0]>(\n lines: T,\n unitPrice: (doc: L) => any,\n vatCoefficient: (doc: L) => any\n) => {\n return lines.length === 0\n ? new Decimal(0)\n : Decimal.sum(\n ...lines.map((line) =>\n Decimal.mul(line.quantity, unitPrice(line)).mul(vatCoefficient(line))\n )\n );\n};\n\nexport const getDocumentTotals = (\n doc:\n | FinanceDocument\n | DocumentDraft<FinanceDocumentV2>\n | NewDocumentDraft<FinanceDocumentV2>\n | FinanceDocumentGenerateRequest\n) => {\n let totalVat: Decimal;\n let totalExcl: Decimal;\n\n if (isFinanceDocumentV1(doc as FinanceDocumentV1)) {\n const lines = (doc as FinanceDocumentV1).lines;\n const getUnitPriceExclusive = (line: FinanceLineV1) => line.unitPriceExclusive;\n\n totalVat = getTotalVat(lines, getUnitPriceExclusive, (line) => line.vatCoefficient);\n totalExcl = getTotalExcl(lines, getUnitPriceExclusive);\n } else {\n const lines = (doc as FinanceDocumentV2).lines ?? [];\n const getUnitPriceExcl = (line: FinanceDocumentLine) => line.unitPriceExcl;\n\n totalVat = getTotalVat(lines, getUnitPriceExcl, (line) =>\n new Decimal(line.vatPercentage).div(100)\n );\n totalExcl = getTotalExcl(lines, getUnitPriceExcl);\n }\n\n return {\n totalExcl,\n totalVat,\n subTotal: totalExcl.add(totalVat),\n };\n};\n\nexport const getRoundedDocumentTotal = (\n doc:\n | FinanceDocument\n | DocumentDraft<FinanceDocumentV2>\n | NewDocumentDraft<FinanceDocumentV2>\n | FinanceDocumentGenerateRequest,\n currencyCode?: string,\n foreignCurrency?: {\n currencyCode: string;\n exchangeRate: string;\n }\n) => {\n const { subTotal, totalExcl, totalVat } = getDocumentTotals(doc);\n const rate = foreignCurrency?.exchangeRate ?? '1';\n const subTotalForeign = subTotal.div(rate).toString();\n const totalExclForeign = totalExcl.div(rate).toString();\n const totalVatForeign = totalVat.div(rate).toString();\n\n return {\n amount: roundMoney(subTotal).toString(),\n amountExcl: roundMoney(totalExcl).toString(),\n vatAmount: roundMoney(totalVat).toString(),\n currencyCode: currencyCode ?? 'EUR',\n foreign: foreignCurrency\n ? {\n amount: roundMoney(subTotalForeign).toString(),\n amountExcl: roundMoney(totalExclForeign).toString(),\n vatAmount: roundMoney(totalVatForeign).toString(),\n currencyCode: foreignCurrency.currencyCode,\n exchangeRate: foreignCurrency.exchangeRate,\n }\n : undefined,\n };\n};\n\nconst getAmountExcl = (unitPriceExcl: string, quantity: string) =>\n new Decimal(unitPriceExcl).times(new Decimal(quantity));\n\nexport const getLineAmounts = (unitPriceExcl: string, quantity: string, vatRate: string) => {\n const amountExcl = getAmountExcl(unitPriceExcl, quantity);\n const vatAmount = new Decimal(quantity)\n .times(new Decimal(unitPriceExcl))\n .times(new Decimal(vatRate).div(100));\n\n return {\n amount: amountExcl.add(vatAmount).toString(),\n amountExcl: amountExcl.toString(),\n unitPriceExcl,\n vatAmount: vatAmount.toString(),\n };\n};\n\nexport const isGeneratingAttachment = (doc: FinanceDocument) =>\n doc.attachment.id === GENERATING_PLACEHOLDER_ATTACHMENT.id;\n"]}
|
|
1
|
+
{"version":3,"file":"finance.js","sourceRoot":"/","sources":["utils/finance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,iCAAiC,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAML,sBAAsB,EAEtB,mBAAmB,EACnB,mBAAmB,GAEpB,MAAM,uBAAuB,CAAC;AAG/B,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,GAA0F,EACjF,EAAE;IACX,OAAO,CACL,CAAC,CAAC,GAAG,CAAC,OAAO;QACb,CAAC,sBAAsB,CAAC,OAAO,EAAE,sBAAsB,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAC1F,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,GAAsB,EAAU,EAAE;IACzE,IAAI,CAAC,CAAC,GAAG,CAAC,qBAAqB,EAAE,CAAC;QAChC,OAAO,GAAG,CAAC,qBAAq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KAAK,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;AAC/D,CAAC,CAAC;AAEF,MAAM,oBAAoB,GAAG,CAAC,GAAkC,EAAE,EAAE;IAClE,MAAM,MAAM,GAAG,MAAM,CACnB,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,SAAS,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,EACzE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,SAAS,CACvB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["import { BusinessEntityIds } from '@bisondesk/commons-sdk/constants';\nimport { roundMoney } from '@bisondesk/commons-sdk/money';\nimport { Decimal } from 'decimal.js';\nimport { uniqBy } from 'lodash-es';\nimport { GENERATING_PLACEHOLDER_ATTACHMENT } from '../constants/index.js';\nimport {\n Association,\n DocumentDraft,\n FinanceDocument,\n FinanceDocumentGenerateRequest,\n FinanceDocumentLine,\n FinanceDocumentSubtype,\n FinanceDocumentV2,\n isFinanceDocumentV1,\n isFinanceDocumentV2,\n NewDocumentDraft,\n} from '../types/documents.js';\nimport { FinanceDocumentV1, FinanceLineV1 } from '../types/legacy.js';\n\nexport const documentGoesToAccounting = (\n doc: FinanceDocumentV2 | DocumentDraft<FinanceDocumentV2> | FinanceDocumentGenerateRequest\n): boolean => {\n return (\n !!doc.subtype &&\n [FinanceDocumentSubtype.Invoice, FinanceDocumentSubtype.CreditNote].includes(doc.subtype)\n );\n};\n\nexport const getAccountingDescription = (doc: FinanceDocumentV2): string => {\n if (!!doc.accountingDescription) {\n return doc.accountingDescription;\n }\n\n const associations = doc.lines\n ?.flatMap((line) => line.associations ?? [])\n .filter(Boolean) as Association[];\n\n // In the UI the user can only select one vehicle associations\n const equivalentEntityIds = {\n [BusinessEntityIds.ExternalVehicleSale]: BusinessEntityIds.Vehicles,\n };\n\n const uniqueAssociations = uniqBy(\n associations,\n (a) => `${equivalentEntityIds[a.businessEntityId] ?? a.businessEntityId}_${a.recordId}`\n );\n\n if (uniqueAssociations.length !== 1) {\n return doc.reference;\n }\n\n const firstAssociation = associations[0];\n if (\n [\n BusinessEntityIds.Vehicles,\n BusinessEntityIds.Opportunities,\n BusinessEntityIds.ExternalVehicleSale,\n ].includes(firstAssociation.businessEntityId) &&\n firstAssociation.tags != null\n ) {\n return `${firstAssociation.tags?.slice(0, 3).join(' ')} ${doc.reference}`.trim();\n }\n\n return doc.reference;\n};\n\nexport const getTotalExcl = <T extends FinanceDocument['lines']>(\n lines: T,\n unitPrice: (line: T[0], index: number) => any\n) => {\n return lines.length === 0\n ? new Decimal(0)\n : Decimal.sum(...lines.map((line, i) => Decimal.mul(line.quantity, unitPrice(line, i))));\n};\n\nexport const getTotalVat = <T extends FinanceDocument['lines'], L = T[0]>(\n lines: T,\n unitPrice: (doc: L, index: number) => any,\n vatCoefficient: (doc: L, index: number) => any\n) => {\n return lines.length === 0\n ? new Decimal(0)\n : Decimal.sum(\n ...lines.map((line, i) =>\n Decimal.mul(line.quantity, unitPrice(line, i)).mul(vatCoefficient(line, i))\n )\n );\n};\n\nexport const getDocumentTotals = (\n doc:\n | FinanceDocument\n | DocumentDraft<FinanceDocumentV2>\n | NewDocumentDraft<FinanceDocumentV2>\n | FinanceDocumentGenerateRequest\n) => {\n let totalVat: Decimal;\n let totalExcl: Decimal;\n\n if (isFinanceDocumentV1(doc as FinanceDocumentV1)) {\n const lines = (doc as FinanceDocumentV1).lines;\n const getUnitPriceExclusive = (line: FinanceLineV1) => line.unitPriceExclusive;\n\n totalVat = getTotalVat(lines, getUnitPriceExclusive, (line) => line.vatCoefficient);\n totalExcl = getTotalExcl(lines, getUnitPriceExclusive);\n } else {\n const lines = (doc as FinanceDocumentV2).lines ?? [];\n const getUnitPriceExcl = (line: FinanceDocumentLine) => line.unitPriceExcl;\n\n totalVat = getTotalVat(lines, getUnitPriceExcl, (line) =>\n new Decimal(line.vatPercentage).div(100)\n );\n totalExcl = getTotalExcl(lines, getUnitPriceExcl);\n }\n\n return {\n totalExcl,\n totalVat,\n subTotal: totalExcl.add(totalVat),\n };\n};\n\nexport const getRoundedDocumentTotal = (\n doc:\n | FinanceDocument\n | DocumentDraft<FinanceDocumentV2>\n | NewDocumentDraft<FinanceDocumentV2>\n | FinanceDocumentGenerateRequest,\n currencyCode?: string,\n foreignCurrency?: {\n currencyCode: string;\n exchangeRate: string;\n }\n) => {\n const { subTotal, totalExcl, totalVat } = getDocumentTotals(doc);\n const rate = foreignCurrency?.exchangeRate ?? '1';\n const subTotalForeign = subTotal.div(rate).toString();\n const totalExclForeign = totalExcl.div(rate).toString();\n const totalVatForeign = totalVat.div(rate).toString();\n\n return {\n amount: roundMoney(subTotal).toString(),\n amountExcl: roundMoney(totalExcl).toString(),\n vatAmount: roundMoney(totalVat).toString(),\n currencyCode: currencyCode ?? 'EUR',\n foreign: foreignCurrency\n ? {\n amount: roundMoney(subTotalForeign).toString(),\n amountExcl: roundMoney(totalExclForeign).toString(),\n vatAmount: roundMoney(totalVatForeign).toString(),\n currencyCode: foreignCurrency.currencyCode,\n exchangeRate: foreignCurrency.exchangeRate,\n }\n : undefined,\n };\n};\n\nconst getAmountExcl = (unitPriceExcl: string, quantity: string) =>\n new Decimal(unitPriceExcl).times(new Decimal(quantity));\n\nexport const getLineAmounts = (unitPriceExcl: string, quantity: string, vatRate: string) => {\n const amountExcl = getAmountExcl(unitPriceExcl, quantity);\n const vatAmount = new Decimal(quantity)\n .times(new Decimal(unitPriceExcl))\n .times(new Decimal(vatRate).div(100));\n\n return {\n amount: amountExcl.add(vatAmount).toString(),\n amountExcl: amountExcl.toString(),\n unitPriceExcl,\n vatAmount: vatAmount.toString(),\n };\n};\n\nexport const isGeneratingAttachment = (doc: FinanceDocument) =>\n doc.attachment.id === GENERATING_PLACEHOLDER_ATTACHMENT.id;\n\n// returns a new finance document where\n// - all lines are filtered by the recordId\n// - the unit price is divided by the number of associations\nexport const filterDocumentLinesForRecord = (\n doc: FinanceDocument,\n recordId: string // vehicleId, opportunityId, or externalVehicleSaleId\n): FinanceDocument => {\n const filteredLines = isFinanceDocumentV2(doc)\n ? doc.lines\n .filter((line) => line.associations?.some((assoc) => assoc.recordId === recordId))\n .map((line) => ({\n ...line,\n unitPriceExcl: calculateDistributedPrice(\n line.unitPriceExcl,\n line.associations?.length || 1\n ),\n }))\n : doc.lines\n .filter((line) =>\n getAllV1Associations(line.associations).some(\n (assoc) =>\n assoc.vehicleId === recordId ||\n (assoc as FinanceLineV1['associations']['sales'][0]).opportunityId === recordId ||\n (assoc as FinanceLineV1['associations']['sales'][0]).id === recordId\n )\n )\n .map((line) => ({\n ...line,\n unitPriceExclusive: calculateDistributedPrice(\n line.unitPriceExclusive,\n getAllV1Associations(line.associations).length || 1\n ),\n }));\n\n return {\n ...doc,\n lines: filteredLines,\n } as FinanceDocument;\n};\n\nconst calculateDistributedPrice = (price: string, count: number): string => {\n return new Decimal(price).div(Math.max(1, count)).toString();\n};\n\nconst getAllV1Associations = (ass: FinanceLineV1['associations']) => {\n const allAss = uniqBy(\n [...(ass?.sales ?? []), ...(ass?.purchases ?? []), ...(ass.others ?? [])],\n (ass) => ass.vehicleId\n );\n return allAss;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { TENANT_ID_ADMIN_HEADER } from '@bisondesk/commons-sdk/constants';
|
|
2
2
|
import { XError } from '@bisondesk/commons-sdk/errors';
|
|
3
3
|
import { getAdminAuth } from '@bisondesk/commons-sdk/fetch';
|
|
4
|
+
import { TransactionType } from '@bisondesk/commons-sdk/types';
|
|
4
5
|
import fetch, { Response } from 'node-fetch';
|
|
5
6
|
import {
|
|
6
7
|
CostCenter,
|
|
7
8
|
DocumentCategory,
|
|
9
|
+
DocumentCategoryType,
|
|
8
10
|
FinanceDocumentLineCategory,
|
|
9
11
|
} from '../types/document-categories.js';
|
|
10
12
|
import { DocumentValidationRulesResults } from '../types/documents.js';
|
|
@@ -39,6 +41,42 @@ export const getDocumentCategory = async <T extends DocumentCategory>(
|
|
|
39
41
|
});
|
|
40
42
|
};
|
|
41
43
|
|
|
44
|
+
export const listDocumentCategories = async <T extends DocumentCategory>(
|
|
45
|
+
tenantId: string,
|
|
46
|
+
branchId: string,
|
|
47
|
+
filter: {
|
|
48
|
+
type: DocumentCategoryType;
|
|
49
|
+
transactionType?: TransactionType;
|
|
50
|
+
}
|
|
51
|
+
): Promise<T[]> => {
|
|
52
|
+
const auth = await getAdminAuth();
|
|
53
|
+
const response: Response = await fetch(
|
|
54
|
+
`${
|
|
55
|
+
process.env.DOCUMENT_DRAFTS_API_ORIGIN
|
|
56
|
+
}/api/documents/settings/${branchId}/document-categories?type=${filter.type}${
|
|
57
|
+
filter.transactionType ? '&transactionType=' + filter.transactionType : ''
|
|
58
|
+
}`,
|
|
59
|
+
{
|
|
60
|
+
headers: {
|
|
61
|
+
Authorization: auth,
|
|
62
|
+
[TENANT_ID_ADMIN_HEADER]: tenantId,
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
if (response.status === 200) {
|
|
68
|
+
return response.json() as any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const body = await response.text();
|
|
72
|
+
throw new XError('documents-sdk.drafts.list-doc-categories-fail', {
|
|
73
|
+
tenantId,
|
|
74
|
+
branchId,
|
|
75
|
+
response: body,
|
|
76
|
+
status: response.status,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
42
80
|
export const listFinanceLineCategories = async (
|
|
43
81
|
tenantId: string,
|
|
44
82
|
branchId: string,
|
|
@@ -69,6 +107,34 @@ export const listFinanceLineCategories = async (
|
|
|
69
107
|
});
|
|
70
108
|
};
|
|
71
109
|
|
|
110
|
+
export const listVehicleFinanceLineCategories = async (
|
|
111
|
+
tenantId: string,
|
|
112
|
+
branchId: string
|
|
113
|
+
): Promise<FinanceDocumentLineCategory[]> => {
|
|
114
|
+
const auth = await getAdminAuth();
|
|
115
|
+
const response: Response = await fetch(
|
|
116
|
+
`${process.env.DOCUMENT_DRAFTS_API_ORIGIN}/api/documents/settings/${branchId}/vehicle-line-categories`,
|
|
117
|
+
{
|
|
118
|
+
headers: {
|
|
119
|
+
Authorization: auth,
|
|
120
|
+
[TENANT_ID_ADMIN_HEADER]: tenantId,
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (response.status === 200) {
|
|
126
|
+
return response.json() as any;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const body = await response.text();
|
|
130
|
+
throw new XError('documents-sdk.drafts.get-vehicle-lines-categories-fail', {
|
|
131
|
+
tenantId,
|
|
132
|
+
branchId,
|
|
133
|
+
response: body,
|
|
134
|
+
status: response.status,
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
|
|
72
138
|
export const listCostCenters = async (
|
|
73
139
|
tenantId: string,
|
|
74
140
|
branchId: string
|
package/src/types/legacy.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { AttachmentValue } from '@bisondesk/commons-sdk/types';
|
|
|
3
3
|
export type FinanceLineV1 = {
|
|
4
4
|
associations: {
|
|
5
5
|
others: { vehicleId: string; label: string }[];
|
|
6
|
-
sales: { id: string; vehicleId: string; label: string }[];
|
|
6
|
+
sales: { id: string; vehicleId: string; opportunityId?: string; label: string }[];
|
|
7
7
|
purchases: { id: string; vehicleId: string; label: string }[];
|
|
8
8
|
};
|
|
9
9
|
description: string;
|
package/src/utils/finance.ts
CHANGED
|
@@ -11,8 +11,9 @@ import {
|
|
|
11
11
|
FinanceDocumentLine,
|
|
12
12
|
FinanceDocumentSubtype,
|
|
13
13
|
FinanceDocumentV2,
|
|
14
|
-
NewDocumentDraft,
|
|
15
14
|
isFinanceDocumentV1,
|
|
15
|
+
isFinanceDocumentV2,
|
|
16
|
+
NewDocumentDraft,
|
|
16
17
|
} from '../types/documents.js';
|
|
17
18
|
import { FinanceDocumentV1, FinanceLineV1 } from '../types/legacy.js';
|
|
18
19
|
|
|
@@ -65,23 +66,23 @@ export const getAccountingDescription = (doc: FinanceDocumentV2): string => {
|
|
|
65
66
|
|
|
66
67
|
export const getTotalExcl = <T extends FinanceDocument['lines']>(
|
|
67
68
|
lines: T,
|
|
68
|
-
unitPrice: (line: T[0]) => any
|
|
69
|
+
unitPrice: (line: T[0], index: number) => any
|
|
69
70
|
) => {
|
|
70
71
|
return lines.length === 0
|
|
71
72
|
? new Decimal(0)
|
|
72
|
-
: Decimal.sum(...lines.map((line) => Decimal.mul(line.quantity, unitPrice(line))));
|
|
73
|
+
: Decimal.sum(...lines.map((line, i) => Decimal.mul(line.quantity, unitPrice(line, i))));
|
|
73
74
|
};
|
|
74
75
|
|
|
75
76
|
export const getTotalVat = <T extends FinanceDocument['lines'], L = T[0]>(
|
|
76
77
|
lines: T,
|
|
77
|
-
unitPrice: (doc: L) => any,
|
|
78
|
-
vatCoefficient: (doc: L) => any
|
|
78
|
+
unitPrice: (doc: L, index: number) => any,
|
|
79
|
+
vatCoefficient: (doc: L, index: number) => any
|
|
79
80
|
) => {
|
|
80
81
|
return lines.length === 0
|
|
81
82
|
? new Decimal(0)
|
|
82
83
|
: Decimal.sum(
|
|
83
|
-
...lines.map((line) =>
|
|
84
|
-
Decimal.mul(line.quantity, unitPrice(line)).mul(vatCoefficient(line))
|
|
84
|
+
...lines.map((line, i) =>
|
|
85
|
+
Decimal.mul(line.quantity, unitPrice(line, i)).mul(vatCoefficient(line, i))
|
|
85
86
|
)
|
|
86
87
|
);
|
|
87
88
|
};
|
|
@@ -173,3 +174,55 @@ export const getLineAmounts = (unitPriceExcl: string, quantity: string, vatRate:
|
|
|
173
174
|
|
|
174
175
|
export const isGeneratingAttachment = (doc: FinanceDocument) =>
|
|
175
176
|
doc.attachment.id === GENERATING_PLACEHOLDER_ATTACHMENT.id;
|
|
177
|
+
|
|
178
|
+
// returns a new finance document where
|
|
179
|
+
// - all lines are filtered by the recordId
|
|
180
|
+
// - the unit price is divided by the number of associations
|
|
181
|
+
export const filterDocumentLinesForRecord = (
|
|
182
|
+
doc: FinanceDocument,
|
|
183
|
+
recordId: string // vehicleId, opportunityId, or externalVehicleSaleId
|
|
184
|
+
): FinanceDocument => {
|
|
185
|
+
const filteredLines = isFinanceDocumentV2(doc)
|
|
186
|
+
? doc.lines
|
|
187
|
+
.filter((line) => line.associations?.some((assoc) => assoc.recordId === recordId))
|
|
188
|
+
.map((line) => ({
|
|
189
|
+
...line,
|
|
190
|
+
unitPriceExcl: calculateDistributedPrice(
|
|
191
|
+
line.unitPriceExcl,
|
|
192
|
+
line.associations?.length || 1
|
|
193
|
+
),
|
|
194
|
+
}))
|
|
195
|
+
: doc.lines
|
|
196
|
+
.filter((line) =>
|
|
197
|
+
getAllV1Associations(line.associations).some(
|
|
198
|
+
(assoc) =>
|
|
199
|
+
assoc.vehicleId === recordId ||
|
|
200
|
+
(assoc as FinanceLineV1['associations']['sales'][0]).opportunityId === recordId ||
|
|
201
|
+
(assoc as FinanceLineV1['associations']['sales'][0]).id === recordId
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
.map((line) => ({
|
|
205
|
+
...line,
|
|
206
|
+
unitPriceExclusive: calculateDistributedPrice(
|
|
207
|
+
line.unitPriceExclusive,
|
|
208
|
+
getAllV1Associations(line.associations).length || 1
|
|
209
|
+
),
|
|
210
|
+
}));
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
...doc,
|
|
214
|
+
lines: filteredLines,
|
|
215
|
+
} as FinanceDocument;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const calculateDistributedPrice = (price: string, count: number): string => {
|
|
219
|
+
return new Decimal(price).div(Math.max(1, count)).toString();
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const getAllV1Associations = (ass: FinanceLineV1['associations']) => {
|
|
223
|
+
const allAss = uniqBy(
|
|
224
|
+
[...(ass?.sales ?? []), ...(ass?.purchases ?? []), ...(ass.others ?? [])],
|
|
225
|
+
(ass) => ass.vehicleId
|
|
226
|
+
);
|
|
227
|
+
return allAss;
|
|
228
|
+
};
|