Forráskód Böngészése

运输单签收错误

lrf402788946 4 éve
szülő
commit
ff493402aa
1 módosított fájl, 95 hozzáadás és 12 törlés
  1. 95 12
      app/service/order/transport.js

+ 95 - 12
app/service/order/transport.js

@@ -25,7 +25,7 @@ class TransportService extends CrudService {
    */
   async sign(data) {
     const { id, sign_time } = data;
-    const transport = await this.model.find({ _id: ObjectId(id) });
+    const transport = await this.model.findOne({ _id: ObjectId(id) });
     if (!transport) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到运输单信息');
     }
@@ -169,23 +169,106 @@ class TransportService extends CrudService {
     // 获取车牌号
     const car = await this.ctx.model.Car.findById(car_no);
     if (car) car_no = car.car_no;
-    const arr = [];
-    const alignStyle = { vertical: 'middle', horizontal: 'center' };
+    let arr = [];
     // 根据内容,计算标题单元格结束位置的字母
     const keys = Object.keys(data);
-    let ecell = '';
-    if (keys.includes('daily')) ecell = 'L1';
-    else ecell = '';
+    let eletter = '';
+    if (keys.includes('daily')) eletter = 'L';
+    else eletter = 'H';
+    const headObj = this.getHead(eletter); // 占 1-4行
+    arr.push(headObj);
+    // 先拼count部分
+    const countArr = this.getCount(data, eletter); // 占 5-6行
+    arr.push(...countArr);
+    // 各部分
+    // 收入部分 前4个(A-D), 运输支出部分中间4个(E-H),日常维修部分最后4个(I-L)
+    const excelData = this.getExcelData(data);
+    // 文字位置样式,都加上
+    const text = { vertAlign: 'superscript', size: 16 };
+    const alignment = { vertical: 'middle', horizontal: 'center' };
+    arr = arr.map(i => {
+      i.alignment = alignment;
+      i.font = { ...text, ..._.get(i, 'font', {}) };
+      return i;
+    });
+    await this.ctx.service.util.excel.toExcel({ data: arr });
+  }
+
+  /**
+   * 整理收入/支出数据
+   * @param {Object} data 核算结果集
+   */
+  getExcelData(data) {
+    const inBill = _.get(data, 'inBill', []);
+    const outBill = _.get(data, 'outBill', []);
+    const daily = _.get(data, 'daily', []);
+    const max = _.max([ inBill.length, outBill.length, daily.length ]);
+
+  }
+
+  /**
+   * 拼接count部分
+   * @param {Object} data 要导出的数据
+   * @param {String} letter 字母
+   */
+  getCount(data, letter) {
+    const arr = [];
+    const count = _.get(data, 'count');
+    if (!count) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到合计数据'); }
+    // 总计行
+    if (count.total || count.total === 0) {
+      const countTotal = {
+        content: `总计:${count.total || 0}`,
+        scell: 'A5',
+        ecell: `${letter}5`,
+      };
+      arr.push(countTotal);
+    }
+
+    // 各部分总计行, 每部分之占4列
+    // 收入
+    if (count.im || count.im === 0) {
+      const countIn = {
+        content: `收入${count.im || 0}`,
+        scell: 'A6',
+        ecell: 'D6',
+      };
+      arr.push(countIn);
+    }
+    // 运输支出
+    if (count.om || count.om === 0) {
+      const countOut = {
+        content: `运输支出${count.om || 0}`,
+        scell: 'E6',
+        ecell: 'H6',
+      };
+      arr.push(countOut);
+    }
+    // 日常支出
+    if (count.dm || count.dm === 0) {
+      const countDaily = {
+        content: `日常维修支出${count.om || 0}`,
+        scell: 'I6',
+        ecell: 'L6',
+      };
+      arr.push(countDaily);
+    }
+    return arr;
+  }
+
+
+  /**
+   * 获取核算标题
+   * @param {String} letter 字母
+   */
+  getHead(letter) {
     const headObj = {
       content: '核算单',
       scell: 'A1',
-      ecell,
-      alignment: alignStyle,
+      ecell: `${letter}4`,
+      font: { size: 20, bold: true },
     };
-    arr.push(headObj);
-    // 先拼count
-
-    await this.ctx.service.util.excel.toExcel({ data: arr });
+    return headObj;
   }
 }
 module.exports = TransportService;