|
@@ -0,0 +1,271 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Mai
|
|
|
+ * @date
|
|
|
+ * @version
|
|
|
+ */
|
|
|
+
|
|
|
+const Ledger = require('../lib/ledger');
|
|
|
+const moment = require('moment');
|
|
|
+const indexPre = 'id_';
|
|
|
+
|
|
|
+module.exports = app => {
|
|
|
+ class RptGatherMemory extends app.BaseService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ *
|
|
|
+ * @param {Object} ctx - egg全局context
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.resultTree = new Ledger.gatherTree(ctx, {
|
|
|
+ id: 'id',
|
|
|
+ pid: 'pid',
|
|
|
+ order: 'order',
|
|
|
+ level: 'level',
|
|
|
+ rootId: -1
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ _checkSpecialTender(tender, special) {
|
|
|
+ for (const spec of special) {
|
|
|
+ if (tender[spec.key] === true) return spec.key;
|
|
|
+ }
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherMonthData(sTender, index, month) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+ const billsTree = new Ledger.billsTree(this.ctx, {
|
|
|
+ id: 'ledger_id',
|
|
|
+ pid: 'ledger_pid',
|
|
|
+ order: 'order',
|
|
|
+ level: 'level',
|
|
|
+ rootId: -1,
|
|
|
+ keys: ['id', 'tender_id', 'ledger_id'],
|
|
|
+ stageId: 'id',
|
|
|
+ calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
|
|
|
+ calc: function (node) {
|
|
|
+ if (node.children && node.children.length === 0) {
|
|
|
+ // node.pre_gather_qty = self.ctx.helper.add(node.pre_contract_qty, node.pre_qc_qty);
|
|
|
+ node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
|
|
|
+ // node.end_contract_qty = self.ctx.helper.add(node.pre_contract_qty, node.contract_qty);
|
|
|
+ // node.end_qc_qty = self.ctx.helper.add(node.pre_qc_qty, node.qc_qty);
|
|
|
+ // node.end_gather_qty = self.ctx.helper.add(node.pre_gather_qty, node.gather_qty);
|
|
|
+ }
|
|
|
+ // node.pre_gather_tp = self.ctx.helper.add(node.pre_contract_tp, node.pre_qc_tp);
|
|
|
+ node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
|
|
|
+ // node.end_contract_tp = self.ctx.helper.add(node.pre_contract_tp, node.contract_tp);
|
|
|
+ // node.end_qc_tp = self.ctx.helper.add(node.pre_qc_tp, node.qc_tp);
|
|
|
+ // node.end_gather_tp = self.ctx.helper.add(node.pre_gather_tp, node.gather_tp);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const billsData = await this.ctx.service.ledger.getData(tender.id);
|
|
|
+ const stage = await this.ctx.service.stage.getDataByCondition({tid: tender.id, s_time: month});
|
|
|
+ if (stage) {
|
|
|
+ await this.ctx.service.stage.doCheckStage(stage);
|
|
|
+ if (stage.readOnly) {
|
|
|
+ const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
|
|
|
+ stage.id, stage.curTimes, stage.curOrder);
|
|
|
+ this.ctx.helper.assignRelaData(billsData, [
|
|
|
+ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
|
|
|
+ this.ctx.helper.assignRelaData(billsData, [
|
|
|
+ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ billsTree.loadDatas(billsData);
|
|
|
+ billsTree.calculateAll();
|
|
|
+ this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
|
|
|
+ gatherNode['t_' + index + '_id'] = tender.id;
|
|
|
+ gatherNode['t_' + index + '_name'] = tender.name;
|
|
|
+
|
|
|
+ gatherNode['t_' + index + "_qty"] = helper.add(gatherNode['t_' + index + "_qty"], sourceNode.quantity);
|
|
|
+ gatherNode['t_' + index + "_tp"] = helper.add(gatherNode['t_' + index + "_tp"], sourceNode.total_price);
|
|
|
+ gatherNode['t_' + index + "_contract_qty"] = helper.add(gatherNode['t_' + index + "_contract_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_contract_tp"] = helper.add(gatherNode['t_' + index + "_contract_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['t_' + index + "_qc_qty"] = helper.add(gatherNode['t_' + index + "_qc_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_qc_tp"] = helper.add(gatherNode['t_' + index + "_qc_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['t_' + index + "_gather_qty"] = helper.add(gatherNode['t_' + index + "_gather_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_gather_tp"] = helper.add(gatherNode['t_' + index + "_gather_tp"], sourceNode.contract_tp);
|
|
|
+
|
|
|
+ gatherNode['s_' + "qty"] = helper.add(gatherNode['t_' + "qty"], sourceNode.quantity);
|
|
|
+ gatherNode['s_' + "tp"] = helper.add(gatherNode['t_' + "tp"], sourceNode.total_price);
|
|
|
+ gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['t_' + "contract_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['t_' + "contract_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['t_' + "qc_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['t_' + "qc_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['t_' + "gather_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['t_' + "gather_tp"], sourceNode.contract_tp);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherZoneData(sTender, index, zone) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+ /**
|
|
|
+ * 汇总并合并 相关数据
|
|
|
+ * @param {Array} index - 主数据
|
|
|
+ * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
|
|
|
+ */
|
|
|
+ const sumAssignRelaData = function (index, rela) {
|
|
|
+ const loadFields = function (datas, fields, prefix, relaId) {
|
|
|
+ for (const d of datas) {
|
|
|
+ const key = indexPre + d[relaId];
|
|
|
+ const m = index[key];
|
|
|
+ if (m) {
|
|
|
+ for (const f of fields) {
|
|
|
+ if (d[f] !== undefined) {
|
|
|
+ m[prefix + f] = helper.add(m[prefix + f], d[f]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ for (const r of rela) {
|
|
|
+ loadFields(r.data, r.fields, r.prefix, r.relaId);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const billsTree = new Ledger.billsTree(this.ctx, {
|
|
|
+ id: 'ledger_id',
|
|
|
+ pid: 'ledger_pid',
|
|
|
+ order: 'order',
|
|
|
+ level: 'level',
|
|
|
+ rootId: -1,
|
|
|
+ keys: ['id', 'tender_id', 'ledger_id'],
|
|
|
+ stageId: 'id',
|
|
|
+ calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
|
|
|
+ calc: function (node) {
|
|
|
+ if (node.children && node.children.length === 0) {
|
|
|
+ node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
|
|
|
+ }
|
|
|
+ node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const billsData = await this.ctx.service.ledger.getData(tender.id);
|
|
|
+
|
|
|
+ let billsIndexData = {};
|
|
|
+ for (const bd of billsData) {
|
|
|
+ billsIndexData[indexPre + bd.id] = bd;
|
|
|
+ }
|
|
|
+
|
|
|
+ const times = zone.split(' - ');
|
|
|
+ if (times.length !== 2) throw '选择的汇总周期无效';
|
|
|
+ const beginTime = moment(times[0], 'YYYY-MM').date();
|
|
|
+ const endTime = moment(times[1], 'YYYY-MM').date();
|
|
|
+
|
|
|
+
|
|
|
+ const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tender.id } });
|
|
|
+ for (const stage of stages) {
|
|
|
+ const sTime = moment(stage.s_time, 'YYYY-MM').date();
|
|
|
+ if (sTime >= beginTime && sTime <= endTime) {
|
|
|
+ await this.ctx.service.stage.doCheckStage(stage);
|
|
|
+ if (stage.readOnly) {
|
|
|
+ const curStage = await this.ctx.service.stageBills.getAuditorStageData(tender.id,
|
|
|
+ stage.id, stage.curTimes, stage.curOrder);
|
|
|
+ sumAssignRelaData(billsIndexData, [
|
|
|
+ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ const curStage = await this.ctx.service.stageBills.getLastestStageData(tender.id, stage.id);
|
|
|
+ sumAssignRelaData(billsIndexData, [
|
|
|
+ {data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid'}
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ billsTree.loadDatas(billsData);
|
|
|
+ billsTree.calculateAll();
|
|
|
+ this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
|
|
|
+ gatherNode['t_' + index + '_id'] = tender.id;
|
|
|
+ gatherNode['t_' + index + '_name'] = tender.name;
|
|
|
+
|
|
|
+ gatherNode['t_' + index + "_qty"] = helper.add(gatherNode['t_' + index + "_qty"], sourceNode.quantity);
|
|
|
+ gatherNode['t_' + index + "_tp"] = helper.add(gatherNode['t_' + index + "_tp"], sourceNode.total_price);
|
|
|
+ gatherNode['t_' + index + "_contract_qty"] = helper.add(gatherNode['t_' + index + "_contract_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_contract_tp"] = helper.add(gatherNode['t_' + index + "_contract_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['t_' + index + "_qc_qty"] = helper.add(gatherNode['t_' + index + "_qc_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_qc_tp"] = helper.add(gatherNode['t_' + index + "_qc_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['t_' + index + "_gather_qty"] = helper.add(gatherNode['t_' + index + "_gather_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['t_' + index + "_gather_tp"] = helper.add(gatherNode['t_' + index + "_gather_tp"], sourceNode.contract_tp);
|
|
|
+
|
|
|
+ gatherNode['s_' + "qty"] = helper.add(gatherNode['t_' + "qty"], sourceNode.quantity);
|
|
|
+ gatherNode['s_' + "tp"] = helper.add(gatherNode['t_' + "tp"], sourceNode.total_price);
|
|
|
+ gatherNode['s_' + "contract_qty"] = helper.add(gatherNode['t_' + "contract_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "contract_tp"] = helper.add(gatherNode['t_' + "contract_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['s_' + "qc_qty"] = helper.add(gatherNode['t_' + "qc_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "qc_tp"] = helper.add(gatherNode['t_' + "qc_tp"], sourceNode.contract_tp);
|
|
|
+ gatherNode['s_' + "gather_qty"] = helper.add(gatherNode['t_' + "gather_qty"], sourceNode.contract_qty);
|
|
|
+ gatherNode['s_' + "gather_tp"] = helper.add(gatherNode['t_' + "gather_tp"], sourceNode.contract_tp);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async _gatherSpecialData(sTender, sKey) {
|
|
|
+ const helper = this.ctx.helper;
|
|
|
+ const billsTree = new Ledger.billsTree(this.ctx, {
|
|
|
+ id: 'ledger_id',
|
|
|
+ pid: 'ledger_pid',
|
|
|
+ order: 'order',
|
|
|
+ level: 'level',
|
|
|
+ rootId: -1,
|
|
|
+ keys: ['id', 'tender_id', 'ledger_id'],
|
|
|
+ stageId: 'id',
|
|
|
+ calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
|
|
|
+ });
|
|
|
+ const tender = await this.ctx.service.tender.getCheckTender(sTender.tid);
|
|
|
+ const billsData = await this.ctx.service.ledger.getData(tender.id);
|
|
|
+ billsTree.loadDatas(billsData);
|
|
|
+ billsTree.calculateAll();
|
|
|
+ this.resultTree.loadGatherTree(billsTree, function (gatherNode, sourceNode) {
|
|
|
+ gatherNode['ts_' + sKey + "_qty"] = helper.add(gatherNode['ts_' + sKey + "_qty"], sourceNode.quantity);
|
|
|
+ gatherNode['ts_' + sKey + "_tp"] = helper.add(gatherNode['ts_' + sKey + "_tp"], sourceNode.total_price);
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ _calculateSum(tree) {
|
|
|
+ for (const node of tree.nodes) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async getGatherStageBills(memFieldKeys, gsDefine, gsCustom) {
|
|
|
+ if (!gsDefine || !gsDefine.enable) return [];
|
|
|
+ if (!gsCustom || !gsCustom.tenders || gsCustom.tenders.length === 0) return [];
|
|
|
+
|
|
|
+ let commonIndex = 0;
|
|
|
+ for (const tender of gsCustom.tenders) {
|
|
|
+ const specialKey = this._checkSpecialTender(tender, gsDefine.setting.special);
|
|
|
+ if (specialKey === '') {
|
|
|
+ switch (gsDefine.setting.type) {
|
|
|
+ case 'month':
|
|
|
+ await this._gatherMonthData(tender, commonIndex, gsCustom.month);
|
|
|
+ break;
|
|
|
+ case 'zone':
|
|
|
+ await this._gatherZoneData(tender, commonIndex, gsCustom.month);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ commonIndex++;
|
|
|
+ } else {
|
|
|
+ await this._gatherSpecialData(tender, specialKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ this.resultTree.generateSortNodes();
|
|
|
+ this._calculateSum(this.resultTree);
|
|
|
+ return this.resultTree.getDefaultDatas();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return RptGatherMemory;
|
|
|
+};
|