summaryrefslogtreecommitdiff
path: root/src/services/uploads.service.ts
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-04-24 21:05:11 +0300
committereug-vs <eug-vs@keemail.me>2021-04-24 21:05:11 +0300
commit96a2ac822ff72fabcd25d2f06a272ecac902c77d (patch)
treec79f36526da62b18522947a3dae187767db333bf /src/services/uploads.service.ts
parent593159c22c2acad599b67d1b128a52232b79a34b (diff)
downloadcommercel-api-96a2ac822ff72fabcd25d2f06a272ecac902c77d.tar.gz
feat: parse account from PDF bill
Diffstat (limited to 'src/services/uploads.service.ts')
-rw-r--r--src/services/uploads.service.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/services/uploads.service.ts b/src/services/uploads.service.ts
index 2a887e4..3235f4c 100644
--- a/src/services/uploads.service.ts
+++ b/src/services/uploads.service.ts
@@ -25,6 +25,7 @@ const parsePdfItems = async (fileName: string): Promise<any[]> => {
});
};
+
const parseTransfersBill = async (context: HookContext): Promise<HookContext> => {
const { id } = context.result;
const fileName = `${dir}/${id}`;
@@ -33,6 +34,10 @@ const parseTransfersBill = async (context: HookContext): Promise<HookContext> =>
const hash = _.groupBy(items, 'y')
const rows = _.map(hash, (elements: any[]) => _.map(elements, 'text'));
+ const accountName = rows.find(row => row[0]?.startsWith('Наименование'))?.[0].slice(13);
+ const accountCodeRow = rows.find(row => row[0]?.startsWith('Счет клиента'));
+ const accountCode = new RegExp(/Счет клиента (\w+)/g).exec(accountCodeRow?.[0])?.[1];
+
const transfers = rows.reduce((acc: any, cols: string[]) => {
if (cols[0]?.startsWith('УНП')) {
acc[acc.length - 1].vatId = cols[2];
@@ -54,6 +59,17 @@ const parseTransfersBill = async (context: HookContext): Promise<HookContext> =>
// At this point we can remove the file
context.service.remove(id);
+ const accountId = await context.app
+ .service('accounts')
+ .find({ code: accountCode })
+ .then((results: any[]) => {
+ if (results.length) return results[0];
+ return context.app
+ .service('accounts')
+ .create({ code: accountCode, name: accountName });
+ })
+ .then((account: any) => account._id);
+
context.result = await Bluebird.mapSeries(transfers, async (transfer: any) => {
const { date, vatId, name } = transfer;
const debet = parseFloat(transfer.debet.replace(/ /g, ''));
@@ -78,6 +94,7 @@ const parseTransfersBill = async (context: HookContext): Promise<HookContext> =>
operation,
amount,
contractorId,
+ accountId,
});
});