summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-18 01:55:21 +0300
committereug-vs <eug-vs@keemail.me>2021-03-18 01:55:21 +0300
commit93e5f7eda2f9bf0d274d793f7e823e35502f4763 (patch)
treeaa601736d20f49a9782d97986962e0158acbb51d
parent2ea1cf63bac15552c754375a38909885c96c7eeb (diff)
downloadcommercel-api-93e5f7eda2f9bf0d274d793f7e823e35502f4763.tar.gz
feat: fill products section in spreadsheet
-rw-r--r--src/services/spreadsheets.service.ts25
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;