From 96a2ac822ff72fabcd25d2f06a272ecac902c77d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 24 Apr 2021 21:05:11 +0300 Subject: feat: parse account from PDF bill --- src/services/transfers.service.ts | 2 +- src/services/uploads.service.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/services') diff --git a/src/services/transfers.service.ts b/src/services/transfers.service.ts index 77fef8a..d835844 100644 --- a/src/services/transfers.service.ts +++ b/src/services/transfers.service.ts @@ -21,7 +21,7 @@ const populateSchema = { const addFields = (item: any) => { const { operation, contractor, amount } = item; const op = operation === 'in' ? 'приход' : 'расход'; - const name = `Перевод: ${op} ${amount}р от ${contractor.name}`; + const name = `Платёж: ${op} ${amount}р от ${contractor.name}`; return { ...item, name }; }; 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 => { }); }; + const parseTransfersBill = async (context: HookContext): Promise => { const { id } = context.result; const fileName = `${dir}/${id}`; @@ -33,6 +34,10 @@ const parseTransfersBill = async (context: HookContext): Promise => 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 => // 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 => operation, amount, contractorId, + accountId, }); }); -- cgit v1.2.3