diff options
Diffstat (limited to 'src/services/uploads.service.ts')
-rw-r--r-- | src/services/uploads.service.ts | 17 |
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, }); }); |