diff options
author | eug-vs <eug-vs@keemail.me> | 2021-04-24 21:05:11 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-04-24 21:05:11 +0300 |
commit | 96a2ac822ff72fabcd25d2f06a272ecac902c77d (patch) | |
tree | c79f36526da62b18522947a3dae187767db333bf | |
parent | 593159c22c2acad599b67d1b128a52232b79a34b (diff) | |
download | commercel-api-96a2ac822ff72fabcd25d2f06a272ecac902c77d.tar.gz |
feat: parse account from PDF bill
-rw-r--r-- | src/models/transfer/transfer.schema.ts | 5 | ||||
-rw-r--r-- | src/services/transfers.service.ts | 2 | ||||
-rw-r--r-- | src/services/uploads.service.ts | 17 |
3 files changed, 23 insertions, 1 deletions
diff --git a/src/models/transfer/transfer.schema.ts b/src/models/transfer/transfer.schema.ts index ebdb6ab..5a4240a 100644 --- a/src/models/transfer/transfer.schema.ts +++ b/src/models/transfer/transfer.schema.ts @@ -3,6 +3,7 @@ import { Document, Schema, Types } from 'mongoose'; export interface TransferSchema extends Document { date: string; operation: 'in' | 'out'; + accountId: string; contractorId: string; amount: number; } @@ -17,6 +18,10 @@ export const transferSchema = new Schema({ type: String, required: true, }, + accountId: { + type: Types.ObjectId, + required: true, + }, contractorId: { type: Types.ObjectId, required: true, 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<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, }); }); |