diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/spreadsheets.service.ts | 25 | 
1 files changed, 21 insertions, 4 deletions
| diff --git a/src/services/spreadsheets.service.ts b/src/services/spreadsheets.service.ts index b52d47e..a05a6f5 100644 --- a/src/services/spreadsheets.service.ts +++ b/src/services/spreadsheets.service.ts @@ -1,8 +1,9 @@  import { Application } from '@feathersjs/express';  import { ServiceMethods, Id } from '@feathersjs/feathers';  import ExcelJS from 'exceljs'; -import _ from 'lodash'; +import Bluebird from 'bluebird';  import fs from 'fs'; +import _ from 'lodash';  import { WaybillSchema } from '../models/waybill/waybill.schema';  const duplicateRowWithMergedCells = (sheet: any, row: number, count: number) => { @@ -46,18 +47,34 @@ class Spreadsheets implements Partial<ServiceMethods<any>> {        sheet.getCell('AS5').value = waybill.contractor.vatId;      } -    duplicateRowWithMergedCells(sheet, 30, waybill.records.length - 1); -    waybill.records.forEach((record: any, index: number) => { +    // Fill the products section +    duplicateRowWithMergedCells(sheet, 30, waybill.records.length - 1); +    await Bluebird.map(waybill.records, async (record: any, index: number) => {        const row = 30 + index;        const getCol = (col: string) => sheet.getCell(`${col}${row}`); -      getCol('A').value = record.productId; +      const product = await this.app.service('products').get(record.productId); + +      getCol('A').value = product.name;        getCol('W').value = 'шт';        getCol('AG').value = record.quantity;        getCol('AP').value = record.price; +      getCol('AZ').value = record.price * record.quantity; +      getCol('BK').value = 0.2; +      getCol('BT').value = record.price * record.quantity * 0.2; +      getCol('CC').value = record.price * record.quantity * 1.2; +    }); + +    // Total +    const totalRow = 30 + waybill.records.length; +    ['AG', 'AP', 'AZ', 'BT', 'CC'].forEach((col: string) => { +      sheet.getCell(`${col}${totalRow}`).value = { +        formula: `SUM(${col}30:${col}${totalRow - 1})` +      } as any;      }); +      const fileName = `./documents/${waybill._id}.xlsx`;      await workbook.xlsx.writeFile(fileName);      return fileName; | 
