12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const _ = require('lodash');
- const StageIm = require('../lib/stage_im');
- const imType = require('../const/tender').imType;
- const audit = require('../const/audit');
- const changeConst = require('../const/change');
- // const path = require('path');
- // const fs = require('fs');
- const stageImTz = 'mem_stage_im_tz';
- const stageImTzBills = 'mem_stage_im_tz_bills';
- const stageImZl = 'mem_stage_im_zl';
- const stageImVersion = '1.3';
- const PermissionCheck = require('../const/account_permission').PermissionCheck;
- const Ledger = require('../lib/ledger');
- const moment = require('moment');
- const auditConst = require('../const/audit');
- const billsFields = (function () {
- const cur = ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'qc_minus_tp', 'gather_qty', 'gather_tp', 'postil'];
- const pre = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_qc_minus_qty', 'pre_qc_minus_tp', 'pre_gather_qty', 'pre_gather_tp'];
- const end = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_qc_minus_qty', 'end_qc_minus_tp', 'end_gather_qty', 'end_gather_tp'];
- const year = ['year_contract_qty', 'year_contract_tp', 'year_qc_qty', 'year_qc_tp', 'year_qc_minus_qty', 'year_qc_minus_tp', 'year_gather_qty', 'year_gather_tp'];
- const final = ['final_tp', 'final_ratio'];
- const final1 = [ 'final_1_qty', 'final_1_tp', 'final_1_ratio'];
- const stageDgn = ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'];
- const stage = cur.concat(pre, end, final);
- const stageEnd = pre.concat(end, final);
- const bgl = ['qc_bgl_code'];
- const leafXmj = ['leaf_xmj_id'];
- return {cur, pre, end, final, final1, stageDgn, stage, stageEnd, bgl, leafXmj};
- })();
- const posFields = (function () {
- const cur = ['contract_qty', 'qc_qty', 'qc_minus_qty', 'gather_qty', 'postil'];
- const pre = ['pre_contract_qty', 'pre_qc_qty', 'pre_qc_minus_qty', 'pre_gather_qty'];
- const end = ['end_contract_qty', 'end_qc_qty', 'end_qc_minus_qty', 'end_gather_qty'];
- const final = ['final_ratio'];
- const final1 = ['final_1_qty', 'final_1_ratio'];
- const stage = cur.concat(pre, end, final);
- const stageEnd = pre.concat(end, final);
- const bgl = ['qc_bgl_code'];
- return {cur, pre, end, final, final1, stage, stageEnd, bgl};
- })();
- module.exports = app => {
- class ReportMemory extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- const self = this;
- this.tableName = 'report_memory';
- // 基础数据类
- // mainData
- this.pos = new Ledger.pos({
- id: 'id', ledgerId: 'lid',
- updateFields: ['contract_qty', 'qc_qty', 'postil'],
- calc: function (p) {
- p.pre_gather_qty = ctx.helper.add(p.pre_contract_qty, p.pre_qc_qty);
- p.gather_qty = ctx.helper.add(p.contract_qty, p.qc_qty);
- p.end_contract_qty = self.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
- p.end_qc_qty = self.ctx.helper.add(p.pre_qc_qty, p.qc_qty);
- p.end_gather_qty = self.ctx.helper.add(p.pre_gather_qty, p.gather_qty);
- p.final_qty = ctx.helper.add(p.end_qc_qty, p.quantity);
- p.final_ratio = ctx.helper.mul(ctx.helper.div(p.end_gather_qty, p.final_qty, 4), 100);
- p.end_qc_minus_qty = ctx.helper.add(p.pre_qc_minus_qty, p.qc_minus_qty);
- p.final_1_qty = ctx.helper.add(p.end_qc_minus_qty, p.quantity);
- p.end_final_1_qty = ctx.helper.add(p.end_qc_qty, p.final_1_qty);
- p.final_1_ratio = ctx.helper.mul(ctx.helper.div(p.end_gather_qty, p.end_final_1_qty, 4), 100);
- }
- });
- // 需要缓存的数据
- this.stageImData = null;
- this.changeData = null;
- this.changeApplyData = null;
- this.changeProjectData = null;
- this.stageValidRole = [];
- }
- _getNewBillsTree(calcFields) {
- return 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: calcFields || ['deal_tp', 'total_price',
- 'contract_tp', 'qc_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'gather_tp',
- 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp', 'final_1_tp',
- 'end_contract_pc_tp', 'end_qc_pc_tp', 'end_pc_tp',
- 'year_contract_tp', 'year_qc_tp', 'year_contract_pc_tp', 'year_qc_pc_tp', 'year_pc_tp', 'year_gather_tp',
- ],
- calc: function (node, helper, decimal) {
- if (node.children && node.children.length === 0) {
- node.pre_gather_qty = 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 = helper.add(node.pre_contract_qty, node.contract_qty);
- node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
- node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
- node.end_qc_minus_qty = helper.add(node.pre_qc_minus_qty, node.qc_minus_qty);
- node.final_1_qty = helper.add(node.quantity, node.end_qc_minus_qty);
- node.end_final_1_qty = helper.add(node.final_1_qty, node.end_qc_qty);
- }
- node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
- node.gather_tp = helper.sum([node.contract_tp, node.qc_tp, node.pc_tp]);
- node.end_contract_tp = helper.sum([node.pre_contract_tp, node.contract_tp, node.contract_pc_tp]);
- node.end_qc_tp = helper.sum([node.pre_qc_tp, node.qc_tp, node.qc_pc_tp]);
- node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
- node.final_tp = helper.add(node.total_price, node.end_qc_tp);
- node.final_ratio = helper.mul(helper.div(node.end_gather_tp, node.final_tp, 4), 100);
- node.final_1_tp = helper.mul(node.unit_price, node.final_1_qty, decimal.tp);
- node.end_final_1_tp = helper.add(node.final_1_tp, node.end_qc_tp);
- node.final_1_ratio = helper.mul(helper.div(node.end_gather_tp, node.end_final_1_tp, 4), 100);
- }
- });
- }
- _checkFieldsExist(source, check) {
- for (const s of source) {
- if (check.indexOf(s) >= 0) {
- return true;
- }
- }
- return false;
- }
- _checkFieldsExistReg(source, regStr) {
- const reg = new RegExp(regStr, 'igm');
- for (const s of source) {
- if (reg.test(s)) {
- return true;
- }
- }
- return false;
- }
- // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
- // 一定程度上算是大Value缓存,数据多了以后:
- // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
- // 2. redis单独服务器
- // 3. redis集群
- async _getReportMemoryCache(name, tid, sid, time, version = '') {
- // redis
- const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
- const data = await this.cache.get(cacheKey);
- if (data) {
- return eval(data);
- } else {
- return null;
- }
- // mysql + IO
- // const rm = await this.getDataByCondition({
- // tid: tid, sid: sid, name: name, time: time
- // });
- // if (rm && rm.file) {
- // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
- // if (fs.existsSync(file)) {
- // const data = await fs.readFileSync(file, 'utf8');
- // return eval(data);
- // } else {
- // return null;
- // }
- // }
- }
- async _setReportMemoryCache(name, tid, sid, time, data, version = '') {
- // redis
- const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
- this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
- // mysql + IO
- // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
- // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
- // const rm = await this.getDataByCondition({
- // tid: tid, sid: sid, name: name, time: time
- // });
- // if (rm) {
- // await this.db.update(this.tableName, {id: rm.id, file: file});
- // } else {
- // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
- // }
- }
- async _generateStageIm(tid, sid, isTz = true) {
- if (isTz && [imType.tz.value, imType.bb.value].indexOf(this.ctx.stage.im_type) === -1) {
- throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
- } else if (!isTz && [imType.zl.value, imType.bw.value].indexOf(this.ctx.stage.im_type) === -1) {
- throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
- }
- const stageIm = new StageIm(this.ctx);
- await stageIm.buildImData();
- this.stageImData.main = stageIm.ImData;
- if (isTz) {
- this.stageImData.bills = stageIm.ImBillsData;
- await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
- await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills, stageImVersion);
- } else {
- await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
- }
- }
- async getStageImTzData(tid, sid, fields, readCache = true) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- if (readCache) {
- const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
- if (cache) return cache;
- }
- if (!this.stageImData) {
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid);
- } catch (err) {
- if (err.stack) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.stack ? '数据错误' : err;
- this.stageImData.bills = this.stageImData.main;
- }
- }
- return this.stageImData.main;
- } catch (err) {
- return [];
- }
- }
- async getStageImTzBillsData(tid, sid, fields, readCache = true) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- if (readCache) {
- const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
- if (cache) return cache;
- }
- if (!this.stageImData) {
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid);
- } catch (err) {
- if (err.stack) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.stack ? '数据错误' : err;
- this.stageImData.bills = this.stageImData.main;
- }
- }
- return this.stageImData.bills;
- } catch (err) {
- return [];
- }
- }
- async getStageImZlData(tid, sid, fields, readCache = true) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- if (readCache) {
- const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
- if (cache) return cache;
- }
- this.stageImData = {};
- try {
- await this._generateStageIm(tid, sid, false);
- } catch (err) {
- if (err.stack) {
- this.ctx.logger.error(err);
- }
- this.stageImData.main = err.statck ? '数据错误' : err;
- }
- return this.stageImData.main;
- } catch (err) {
- return [];
- }
- }
- async getMonthProgress(tid, fields) {
- const helper = this.ctx.helper;
- await this.ctx.service.tender.checkTender(tid);
- const tender = this.ctx.tender;
- const stages = await this.ctx.service.stage.getValidStages(tender.id);
- const lastStage = stages.length > 0 ? stages[0] : null;
- if (lastStage) {
- await this.ctx.service.stage.checkStageGatherData(lastStage, this.ctx.session.sessionUser.is_admin);
- tender.gather_tp = helper.add(lastStage.contract_tp, lastStage.qc_tp);
- tender.end_contract_tp = helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
- tender.end_qc_tp = helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
- tender.end_gather_tp = helper.add(tender.end_contract_tp, tender.end_qc_tp);
- tender.pre_gather_tp = helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
- tender.yf_tp = lastStage.yf_tp;
- tender.qc_ratio = helper.mul(helper.div(tender.end_qc_tp, tender.info.deal_param.contractPrice, 2), 100);
- tender.sum = helper.add(tender.total_price, tender.end_qc_tp);
- tender.pre_ratio = helper.mul(helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
- tender.cur_ratio = helper.mul(helper.div(tender.gather_tp, tender.sum, 2), 100);
- tender.other_tp = helper.sub(helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
- tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
- }
- const monthProgress = [];
- for (const s of stages) {
- if (s.s_time) {
- let progress = monthProgress.find(function (x) {
- return x.month === s.s_time;
- });
- if (!progress) {
- progress = {month: s.s_time};
- monthProgress.push(progress);
- }
- progress.tp = helper.add(helper.add(progress.tp, s.contract_tp), s.qc_tp);
- }
- }
- monthProgress.sort(function (x, y) {
- return Date.parse(x.month) - Date.parse(y.month);
- });
- let sum = 0;
- for (const p of monthProgress) {
- p.ratio = helper.mul(helper.div(p.tp, tender.sum, 4), 100);
- sum = helper.add(sum, p.tp);
- p.end_tp = sum;
- p.end_ratio = helper.mul(helper.div(p.end_tp, tender.sum, 4), 100);
- }
- return monthProgress;
- }
- async _calcBillsBgl(billsTree) {
- if (!this.ctx.stage) return;
- const helper = this.ctx.helper;
- const tender = this.ctx.tender;
- const stage = this.ctx.stage;
- const bglData = this.ctx.stage.readOnly
- ? await this.ctx.service.stageChange.getAuditorAllStageData(tender.id, stage.id, stage.curTimes, stage.curOrder)
- : await this.ctx.service.stageChange.getLastestAllStageData(tender.id, stage.id);
- for (const node of billsTree.nodes) {
- node.qc_bgl_code = '';
- if (node.children && node.children.length > 0) continue;
- const nodeBgl = helper._.filter(bglData, {lid: node.id});
- if (nodeBgl.length === 0) continue;
- helper._.pullAll(bglData, nodeBgl);
- const validBgl = helper._.filter(nodeBgl, function (x) {
- return !helper.checkZero(x.qty);
- });
- node.qc_bgl_code = helper._.uniq(helper._.map(validBgl, 'c_code')).join(';');
- }
- }
- _calcLeafXmjRela(billsTree) {
- for (const node of billsTree.nodes) {
- if (node.b_code) {
- const leafXmj = billsTree.getLeafXmjParent(node);
- node.leaf_xmj_id = leafXmj ? leafXmj.id : -1;
- }
- }
- }
- async _getValidStages(tenderId) {
- const stages = await this.db.select(this.ctx.service.stage.tableName, {
- where: { tid: tenderId },
- orders: [['order', 'desc']],
- });
- if (stages.length !== 0) {
- const lastStage = stages[0];
- if (lastStage.status === auditConst.stage.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
- stages.splice(0, 1);
- }
- }
- return stages;
- }
- async _getTimeZoneStages(tender, zone) {
- const times = zone.split(' - ');
- if (times.length !== 2) throw '选择的汇总周期无效';
- const beginTime = moment(times[0], 'YYYY-MM');
- const endTime = moment(times[1], 'YYYY-MM');
- const stages = await this._getValidStages(tender.id), validStages = [];
- for (const stage of stages) {
- const sTime = moment(stage.s_time, 'YYYY-MM');
- if (sTime.isBetween(beginTime, endTime, null, '[]')) {
- validStages.push(stage);
- }
- }
- return validStages;
- }
- async _gatherStageBills(tender, stages) {
- let billsIndexData = {};
- const helper = this.ctx.helper;
- const sumAssignRelaData = function (index, rela) {
- const loadFields = function (datas, fields, prefix, relaId) {
- for (const d of datas) {
- if (!index[d[relaId]]) index[d[relaId]] = {};
- const m = index[d[relaId]];
- m[relaId] = d[relaId];
- 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);
- }
- };
- for (const stage of stages) {
- await this.ctx.service.stage.doCheckStage(stage);
- const curStage = stage.readOnly
- ? await this.ctx.service.stageBills.getAuditorStageData2(tender.id, stage.id, stage.curTimes, stage.curOrder)
- : await this.ctx.service.stageBills.getLastestStageData2(tender.id, stage.id);
- const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: stage.id } });
- sumAssignRelaData(billsIndexData, [
- { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: '', relaId: 'lid' },
- { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp'], prefix: '', relaId: 'lid' },
- ]);
- }
- const result = [];
- for (const prop in billsIndexData) {
- const yearStageBills = billsIndexData[prop];
- yearStageBills.gather_qty = helper.add(yearStageBills.contract_qty, yearStageBills.qc_qty);
- yearStageBills.gather_tp = helper.sum([yearStageBills.contract_tp, yearStageBills.qc_tp, yearStageBills.pc_tp]);
- result.push(yearStageBills);
- }
- return result;
- };
- async _loadStageBillsZone(tender, zone){
- const stages = await this._getTimeZoneStages(tender, zone);
- return await this._gatherStageBills(tender, stages);
- }
- async _loadStageBillsYear(tender, year) {
- return await this._loadStageBillsZone(tender, `${year}-01 - ${year}-12`);
- }
- async getStageBillsData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- if (sid) {
- await this.ctx.service.stage.checkStage(sid);
- }
- const billsData = this.ctx.stage.ledgerHis
- ? await this.ctx.helper.loadLedgerDataFromOss(this.ctx.stage.ledgerHis.bills_file)
- : await this.ctx.service.ledger.getData(this.ctx.tender.id);
- if (this._checkFieldsExist(fields, billsFields.stageDgn)) {
- const dgnData = await this.ctx.service.stageBillsDgn.getDgnData(this.ctx.tender.id);
- for (const d of dgnData) {
- const l = this.ctx.helper._.find(billsData, {id: d.id});
- this.ctx.helper._.assignIn(l, d);
- }
- }
- let preStage = [], curStage = [];
- if (this._checkFieldsExist(fields, billsFields.stage)) {
- if (this.ctx.stage.readOnly) {
- curStage = await this.ctx.service.stageBills.getAuditorStageData2(this.ctx.tender.id,
- this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
- } else {
- curStage = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
- }
- }
- if (this._checkFieldsExist(fields, billsFields.stageEnd)) {
- preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
- }
- const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
- const endBpcStage = await this.ctx.service.stageBillsPc.getEndStageData(this.ctx.stage);
- const yearStage = this._checkFieldsExistReg(fields, 'year_') && this.ctx.stage.s_time
- ? await this._loadStageBillsYear(this.ctx.tender, this.ctx.stage.s_time.split('-')[0])
- : [];
- this.ctx.helper.assignRelaData(billsData, [
- { data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'postil'], prefix: '', relaId: 'lid' },
- { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid' },
- { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'org_price'], prefix: '', relaId: 'lid' },
- { data: endBpcStage, fields: ['end_contract_pc_tp', 'end_qc_pc_tp', 'end_pc_tp', 'org_price_his'], prefix: '', relaId: 'lid' },
- { data: yearStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'gather_qty', 'gather_tp'], prefix: 'year_', relaId: 'lid' },
- ]);
- billsData.forEach(x => {
- if (x.org_price_his && x.org_price_his.length > 0) {
- for (const prop of x.org_price_his) {
- x[`org_price_${prop}`] = x.org_price_his[prop];
- }
- } else {
- x.org_price_0 = x.unit_price;
- }
- });
- const billsTree = this._getNewBillsTree();
- billsTree.loadDatas(billsData);
- billsTree.calculateAll();
- if (this._checkFieldsExist(fields, billsFields.bgl)) {
- await this._calcBillsBgl(billsTree);
- }
- if (this._checkFieldsExist(fields, billsFields.leafXmj)) {
- this._calcLeafXmjRela(billsTree);
- }
- return billsTree.getDefaultDatas();
- } catch(err) {
- this.ctx.log(err);
- return [];
- }
- }
- async _calcPosBgl() {
- if (!this.ctx.stage) return;
- const helper = this.ctx.helper;
- const tender = this.ctx.tender;
- const stage = this.ctx.stage;
- const bglData = this.ctx.stage.readOnly
- ? await this.ctx.service.stageChange.getAuditorAllStageData(tender.id, stage.id, stage.curTimes, stage.curOrder)
- : await this.ctx.service.stageChange.getLastestAllStageData(tender.id, stage.id);
- for (const p of this.pos.datas) {
- p.qc_bgl_code = '';
- const pBgl = helper._.filter(bglData, {lid: p.lid, pid: p.id});
- if (pBgl.length === 0) continue;
- helper._.pullAll(bglData, pBgl);
- const validBgl = helper._.filter(pBgl, function (x) {
- return !helper.checkZero(x.qty);
- });
- p.qc_bgl_code = helper._.uniq(helper._.map(validBgl, 'c_code')).join(';');
- }
- }
- async getStagePosData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- if (sid) {
- await this.ctx.service.stage.checkStage(sid);
- }
- const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
- let curPosStage = [], prePosStage = [];
- if (this._checkFieldsExist(fields, posFields.stage)) {
- if (this.ctx.stage.readOnly) {
- curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
- this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
- } else {
- curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
- }
- }
- if (this._checkFieldsExist(fields, posFields.stageEnd)) {
- prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
- }
- this.ctx.helper.assignRelaData(posData, [
- {data: curPosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty', 'contract_expr', 'postil'], prefix: '', relaId: 'pid'},
- {data: prePosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'pre_', relaId: 'pid'}
- ]);
- this.pos.loadDatas(posData);
- this.pos.calculateAll();
- if (this._checkFieldsExist(fields, posFields.bgl)) {
- await this._calcPosBgl();
- }
- return this.pos.getDatas();
- } catch (err) {
- return [];
- }
- }
- _getStageValidRole () {
- if (!this.ctx.stage) throw '期数据错误,请重试';
- if (this.stageValidRole && this.stageValidRole.length > 0) return;
- const result = [{dataOrder: 0, flowOrder: 0, uid: this.ctx.stage.user_id}];
- for (const auditor of this.ctx.stage.auditors) {
- if (auditor.status === audit.stage.status.checked ||
- (auditor.status === audit.stage.status.checking && !this.ctx.stage.readOnly)) {
- const role = result.find(function (r) {
- return r.uid === auditor.aid;
- });
- if (role) {
- role.dataOrder = auditor.order;
- } else {
- result.push({
- dataOrder: auditor.order,
- flowOrder: result.length,
- uid: auditor.aid
- })
- }
- }
- }
- this.stageValidRole = result;
- };
- async getStageBillsCompareData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- await this._getStageValidRole();
- const stage = this.ctx.stage, helper = this.ctx.helper;
- const validRole = this.stageValidRole;
- const billsData = this.ctx.stage.ledgerHis
- ? await this.ctx.helper.loadLedgerDataFromOss(this.ctx.stage.ledgerHis.bills_file)
- : await this.ctx.service.ledger.getData(this.ctx.tender.id);
- const allStageBills = await this.ctx.service.stageBills.getAllDataByCondition({where: {sid: sid}});
- const stageBillsIndex = {}, timesLen = 100;
- for (const role of validRole) {
- const stageBills = this.ctx.helper._.filter(allStageBills, function (x) {
- return x.times < stage.curTimes || (x.times === stage.curTimes && x.order <= role.dataOrder);
- });
- this.ctx.helper._.pullAll(allStageBills, stageBills);
- for (const sb of stageBills) {
- const key = 'sb-' + sb.lid;
- const sbi = stageBillsIndex[key];
- if (sbi) {
- if ((sbi.times * timesLen + sbi.order) < (sb.times * timesLen + sb.order)) stageBillsIndex[key] = sb;
- } else {
- stageBillsIndex[key] = sb;
- }
- }
- const filterStageBills = [];
- for (const prop in stageBillsIndex) {
- filterStageBills.push(stageBillsIndex[prop]);
- }
- this.ctx.helper.assignRelaData(billsData, [
- {data: filterStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'r' + role.flowOrder + '_', relaId: 'lid'}
- ]);
- }
- const bpcStage = await this.ctx.service.stageBillsPc.getAllDataByCondition({ where: { sid: this.ctx.stage.id } });
- const endBpcStage = await this.ctx.service.stageBillsPc.getEndStageData(this.ctx.stage);
- const preStage = this._checkFieldsExist(fields, billsFields.stageEnd) && this.ctx.stage.order > 1
- ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1)
- : [];
- this.ctx.helper.assignRelaData(billsData, [
- {data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid'},
- { data: bpcStage, fields: ['contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'org_price'], prefix: '', relaId: 'lid' },
- { data: endBpcStage, fields: ['end_contract_pc_tp', 'end_qc_pc_tp', 'end_pc_tp', 'org_price_his'], prefix: '', relaId: 'lid' },
- ]);
- billsData.forEach(x => {
- if (x.org_price_his && x.org_price_his.length > 0) {
- for (const prop of x.org_price_his) {
- x[`org_price_${prop}`] = x.org_price_his[prop];
- }
- } else {
- x.org_price_0 = x.unit_price;
- }
- });
- const billsTree = this._getNewBillsTree();
- billsTree.loadDatas(billsData);
- for (const role of validRole) {
- const prefix = 'r' + role.flowOrder + '_';
- billsTree.setting.calcFields.push(prefix + 'contract_tp', prefix + 'qc_tp', prefix + 'gather_tp');
- }
- billsTree.calculateAll(function(node) {
- let prefix = '';
- if (node.children && node.children.length === 0) {
- node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
- for (const role of validRole) {
- prefix = 'r' + role.flowOrder + '_';
- node[prefix + 'gather_qty'] = helper.add(node[prefix + 'contract_qty'], node[prefix + 'qc_qty']);
- }
- }
- node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
- for (const role of validRole) {
- prefix = 'r' + role.flowOrder + '_';
- node[prefix + 'gather_tp'] = helper.sum([node[prefix + 'contract_tp'], node[prefix + 'qc_tp'], node.pc_tp]);
- }
- });
- if (this._checkFieldsExist(fields, billsFields.leafXmj)) {
- this._calcLeafXmjRela(billsTree);
- }
- return billsTree.getDefaultDatas();
- } catch (err) {
- return [];
- }
- }
- async getStagePosCompareData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- await this._getStageValidRole();
- const stage = this.ctx.stage, helper = this.ctx.helper;
- const validRole = this.stageValidRole;
- const allStagePos = await this.ctx.service.stagePos.getAllDataByCondition({where: {sid: sid}});
- const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: this.ctx.tender.id }});
- const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
- this.ctx.helper.assignRelaData(posData, [
- {data: prePosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'pre_', relaId: 'pid'}
- ]);
- const stagePosIndex = {}, timesLen = 100;
- for (const role of validRole) {
- const stagePos = this.ctx.helper._.filter(allStagePos, function (x) {
- return x.times < stage.curTimes || (x.times === stage.curTimes && x.order <= role.dataOrder);
- });
- this.ctx.helper._.pullAll(allStagePos, stagePos);
- for (const sp of stagePos) {
- const key = 'sp-' + sp.pid;
- const spi = stagePosIndex[key];
- if (spi) {
- if ((spi.times * timesLen + spi.order) < (sp.times * timesLen + sp.order)) stagePosIndex[key] = sp;
- } else {
- stagePosIndex[key] = sp;
- }
- }
- const filterStagePos = [];
- for (const prop in stagePosIndex) {
- filterStagePos.push(stagePosIndex[prop]);
- }
- this.ctx.helper.assignRelaData(posData, [
- {data: filterStagePos, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: 'r' + role.flowOrder + '_', relaId: 'pid'}
- ]);
- }
- this.pos.loadDatas(posData);
- this.pos.calculateAll(function (p) {
- p.pre_gather_qty = helper.add(p.pre_contract_qty, p.pre_qc_qty);
- for (const role of validRole) {
- const prefix = 'r' + role.flowOrder + '_';
- p[prefix + 'gather_qty'] = helper.add(p[prefix + 'contract_qty'], p[prefix + 'qc_qty']);
- }
- });
- return this.pos.getDatas();
- } catch (err) {
- return [];
- }
- }
- async getStagePayData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const stage = this.ctx.stage;
- const dealPay = await this.ctx.service.stagePay.getStagePays(this.ctx.stage);
- if (!this.ctx.stage.readOnly) {
- // 计算 本期金额
- const PayCalculator = require('../lib/pay_calc');
- const payCalculator = new PayCalculator(this.ctx, this.ctx.stage, this.ctx.tender.info);
- await payCalculator.calculateAll(dealPay);
- }
- if (this._checkFieldsExist(fields, ['start_stage_order']) && this.ctx.stage.status !== audit.stage.status.checked) {
- for (const dp of dealPay) {
- if (!dp.start_stage_order || dp.start_stage_order === this.ctx.stage.order) {
- dp.start_stage_order = this.ctx.helper.checkZero(dp.tp) ? null : this.ctx.stage.order;
- }
- }
- }
- if (this._checkFieldsExistReg(fields, 'r[0-9]+_tp')) {
- this._getStageValidRole();
- const allStagePays = await this.ctx.service.stagePay.getAllDataByCondition({
- where: {sid: stage.id, stimes: stage.curTimes}
- });
- for (const [i, role] of this.stageValidRole.entries()) {
- if (i < this.stageValidRole.length - 1) {
- const stagePays = this.ctx.helper._.filter(allStagePays, function (x) {
- return x.stimes === stage.curTimes && x.sorder === role.dataOrder;
- });
- this.ctx.helper._.pullAll(allStagePays, stagePays);
- for (const sp of stagePays) {
- const dp = dealPay.find(function (x) {return x.pid === sp.pid});
- if (dp) {
- dp['r' + role.flowOrder + '_tp'] = sp.tp;
- }
- }
- } else {
- for (const dp of dealPay) {
- dp['r' + role.flowOrder + '_tp'] = dp.tp;
- }
- }
- }
- }
- return dealPay;
- } catch (err) {
- return [];
- }
- }
- async getStageChangeBillsData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- if (this.ctx.stage.readOnly) {
- return await this.ctx.service.stageChange.getReportAuditorAllStageData(tid, sid,
- this.ctx.stage.curTimes, this.ctx.stage.curOrder);
- } else {
- return await this.ctx.service.stageChange.getReportLastestAllStageData(tid, sid);
- }
- } catch (err) {
- return [];
- }
- }
- _getChangeConstName(define, value) {
- for (const prop in define) {
- if (define[prop].value === value) {
- return define[prop].name;
- }
- }
- return '';
- }
- async _generateChange(tid) {
- if (this.changeData !== null) return;
- const self = this;
- try {
- const decimal = this.ctx.tender.info.decimal, ctx = this.ctx;
- const where = { tid, valid: 1 };
- if (this.ctx.session.sessionProject.page_show.isOnlyChecked) where.status = audit.flow.status.checked;
- const change = await this.ctx.service.change.getAllDataByCondition({ where, orders: [['in_time', 'desc']] });
- for (const c of change) {
- const types = ctx.helper._.map(c.type.split(','), function (t) {
- return self._getChangeConstName(changeConst.type, ctx.helper._.toInteger(t));
- });
- c.type = types.join(';');
- c.class = this._getChangeConstName(changeConst.class, c.class);
- c.quality = this._getChangeConstName(changeConst.quality, c.quality);
- c.charge = this._getChangeConstName(changeConst.charge, c.charge);
- c.attachments = await ctx.service.changeAtt.getChangeAttachment(c.cid);
- const names = ctx.helper._.map(c.attachments, function (x) {
- return x.filename + x.fileext;
- });
- c.attNames = names.join('\n');
- }
- const changeBills = await this.ctx.service.changeAuditList.getChangeAuditBills(tid, this.ctx.session.sessionProject.page_show.isOnlyChecked);
- for (const d of changeBills) {
- d.o_qty = d.oamount;
- d.o_tp = this.ctx.helper.mul(d.o_qty, d.unit_price, decimal.tp);
- d.c_qty = d.camount;
- d.c_tp = this.ctx.helper.mul(d.c_qty, d.unit_price, decimal.tp);
- d.s_qty = d.samount ? parseFloat(d.samount) : 0;
- d.s_tp = this.ctx.helper.mul(d.s_qty, d.unit_price, decimal.tp);
- d.sp_qty = d.spamount;
- d.sp_tp = this.ctx.helper.mul(d.sp_qty, d.unit_price, decimal.tp);
- const auditAmount = d.audit_amount ? d.audit_amount.split(',') : [];
- const relaChange = ctx.helper._.find(change, {cid: d.cid});
- for (const [i, aa] of auditAmount.entries()) {
- const amountField = 'qty_' + (i+1), tpField = 'tp_' + (i+1);
- d[amountField] = aa ? parseFloat(aa) : 0;
- d[tpField] = ctx.helper.mul(d[amountField], d.unit_price, decimal.tp);
- if (relaChange) {
- relaChange[tpField] = ctx.helper.add(relaChange[tpField], d[tpField]);
- }
- }
- }
- change.sort(function (a, b) {
- return a.code.localeCompare(b.code);
- });
- changeBills.sort(function (a, b) {
- const aCIndex = change.findIndex(function (c) {
- return c.cid === a.cid;
- });
- const bCIndex = change.findIndex(function (c) {
- return c.cid === b.cid;
- });
- return aCIndex === bCIndex
- ? ctx.helper.compareCode(a.code, b.code)
- : aCIndex - bCIndex;
- });
- this.changeData = {change: change, bills: changeBills};
- } catch(err) {
- this.ctx.helper.log(err);
- throw err;
- this.changeData = {change: [], bills: []};
- }
- }
- async _generateChangeAudit() {
- if (!!this.changeData.audit) return;
- this.changeData.audit = [];
- for (const c of this.changeData.change) {
- const changeAudit = await this.ctx.service.changeAudit.getListGroupByTimes(c.cid, c.times);
- this.changeData.audit.push(...changeAudit);
- }
- }
- async getChangeData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this._generateChange(tid);
- return this.changeData.change;
- } catch (err) {
- return [];
- }
- }
- async getChangeBillsData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this._generateChange(tid);
- return this.changeData.bills;
- } catch (err) {
- return [];
- }
- }
- async getChangeAuditData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this._generateChange(tid);
- await this._generateChangeAudit();
- return this.changeData.audit;
- } catch (err) {
- return [];
- }
- }
- async _generateImportChange(sid) {
- if (this.importChange !== null) return;
- const self = this;
- try {
- const ctx = this.ctx;
- const change = await this.ctx.service.stageImportChange.getImportChange(sid);
- for (const c of change) {
- const types = ctx.helper._.map(c.type.split(','), function (t) {
- return self._getChangeConstName(changeConst.type, ctx.helper._.toInteger(t));
- });
- c.type = types.join(';');
- c.class = this._getChangeConstName(changeConst.class, c.class);
- c.quality = this._getChangeConstName(changeConst.quality, c.quality);
- c.charge = this._getChangeConstName(changeConst.charge, c.charge);
- }
- change.sort(function (a, b) {
- return a.code.localeCompare(b.code);
- });
- const changeBills = [];
- for (const c of change) {
- const bills = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: c.cid } });
- for (const b of bills) {
- b.o_qty = b.oamount;
- b.c_qty = b.camount;
- b.s_qty = b.samount ? parseFloat(b.samount) : 0;
- b.sp_qty = b.spamount;
- }
- bills.sort(function (a, b) {
- return ctx.helper.compareCode(a.code, b.code);
- });
- changeBills.push(...bills);
- }
- this.importChange = {change: change, bills: changeBills};
- } catch(err) {
- this.ctx.helper.log(err);
- throw err;
- this.importChange = {change: [], bills: []};
- }
- }
- async getImportChangeData(tid, sid, fields) {
- try {
- await this._generateImportChange(sid);
- return this.importChange.change;
- } catch (err) {
- return [];
- }
- }
- async getImportChangeBillsData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this._generateImportChange(tid);
- return this.importChange.bills;
- } catch (err) {
- return [];
- }
- }
- async _generateChangeApply(tid) {
- if (!!this.changeApplyData) return;
- const helper = this.ctx.helper;
- const decimal = this.ctx.tender.info.decimal;
- const where = { tid };
- if (this.ctx.session.sessionProject.page_show.isOnlyChecked) where.status = audit.changeApply.status.checked;
- const apply = await this.ctx.service.changeApply.getAllDataByCondition({ where });
- const bills = await this.ctx.service.changeApplyList.getChangeBills(tid, this.ctx.session.sessionProject.page_show.isOnlyChecked);
- bills.forEach(x => {
- const a = apply.find(y => { return y.id === x.caid });
- const a_decimal = a.decimal ? JSON.stringify(a.decimal) : { tp: decimal.tp, up: decimal.up };
- x.o_tp = helper.mul(x.oamount, x.unit_price, a_decimal.tp);
- x.c_tp = helper.mul(x.camount, x.unit_price, a_decimal.tp);
- });
- bills.sort(function(a, b ) {
- const aCIndex = apply.findIndex(function (c) {
- return c.id === a.caid;
- });
- const bCIndex = apply.findIndex(function (c) {
- return c.id === b.caid;
- });
- return aCIndex === bCIndex
- ? helper.compareCode(a.code, b.code)
- : aCIndex - bCIndex;
- });
- this.changeApplyData = { data: apply, bills };
- }
- async _generateChangeApplyAudit() {
- if (!!this.changeApplyData.audit) return;
- this.changeApplyData.audit = [];
- for (const c of this.changeApplyData.data) {
- const changeAudit = await this.ctx.service.changeApplyAudit.getAllDataByCondition({ where: { caid: c.id, times: c.times }});
- const changeAuditFilter = this.ctx.helper.filterLastestData(changeAudit, ['aid']);
- this.changeApplyData.audit.push(...changeAuditFilter);
- }
- }
- async getChangeApplyData(tid, sid, fields) {
- try {
- await this._generateChangeApply(tid);
- return this.changeApplyData.data;
- } catch (err) {
- return [];
- }
- }
- async getChangeApplyBillsData(tid, sid, fields) {
- try {
- await this._generateChangeApply(tid);
- return this.changeApplyData.bills;
- } catch (err) {
- return [];
- }
- }
- async getChangeApplyAuditData(tid, sid, fields) {
- try {
- await this._generateChangeApply(tid);
- await this._generateChangeApplyAudit();
- return this.changeApplyData.audit;
- } catch (err) {
- this.ctx.log(err);
- return [];
- }
- }
- async _generateChangePlan(tid) {
- if (!!this.changePlanData) return;
- const helper = this.ctx.helper;
- const decimal = this.ctx.tender.info.decimal;
- this.changePlanData = {};
- const where = { tid };
- if (this.ctx.session.sessionProject.page_show.isOnlyChecked) where.status = audit.changeApply.status.checked;
- const data = await this.ctx.service.changePlan.getAllDataByCondition({ where });
- const bills = await this.ctx.service.changePlanList.getChangeBills(tid, this.ctx.session.sessionProject.page_show.isOnlyChecked);
- bills.forEach(x => {
- const plan = data.find(y => { return y.id === x.cpid });
- const p_decimal = plan.decimal ? JSON.stringify(plan.decimal) : { tp: decimal.tp, up: decimal.up };
- x.o_tp = helper.mul(x.oamount, x.unit_price, p_decimal.tp);
- x.c_tp = helper.mul(x.camount, x.unit_price, p_decimal.tp);
- x.s_tp = helper.mul(x.samount, x.unit_price, p_decimal.tp);
- x.sp_tp = helper.mul(x.spamount, x.unit_price, p_decimal.tp);
- const auditAmount = x.audit_amount ? x.audit_amount.split(',') : [];
- for (const [i, aa] of auditAmount.entries()) {
- const amountField = 'qty_' + (i+1), tpField = 'tp_' + (i+1);
- x[amountField] = aa ? parseFloat(aa) : 0;
- x[tpField] = helper.mul(x[amountField], x.unit_price, p_decimal.tp);
- if (plan) {
- plan[tpField] = helper.add(plan[tpField], x[tpField]);
- }
- }
- });
- bills.sort(function(a, b ) {
- const aCIndex = data.findIndex(function (c) {
- return c.id === a.cpid;
- });
- const bCIndex = data.findIndex(function (c) {
- return c.id === b.cpid;
- });
- return aCIndex === bCIndex
- ? helper.compareCode(a.code, b.code)
- : aCIndex - bCIndex;
- });
- this.changePlanData = { data, bills };
- }
- async _generateChangePlanAudit() {
- if (!!this.changePlanData.audit) return;
- this.changePlanData.audit = [];
- for (const c of this.changePlanData.data) {
- const changeAudit = await this.ctx.service.changePlanAudit.getAllDataByCondition({ where: { cpid: c.id, times: c.times }});
- const changeAuditFilter = this.ctx.helper.filterLastestData(changeAudit, ['aid']);
- this.changePlanData.audit.push(...changeAuditFilter);
- }
- }
- async getChangePlanData(tid, sid, fields) {
- try {
- await this._generateChangePlan(tid);
- return this.changePlanData.data;
- } catch (err) {
- return [];
- }
- }
- async getChangePlanBillsData(tid, sid, fields) {
- try {
- await this._generateChangePlan(tid);
- return this.changePlanData.bills;
- } catch (err) {
- return [];
- }
- }
- async getChangePlanAuditData(tid, sid, fields) {
- try {
- await this._generateChangePlan(tid);
- await this._generateChangePlanAudit();
- return this.changePlanData.audit;
- } catch (err) {
- this.ctx.log(err);
- return [];
- }
- }
- async _generateChangeProject(tid) {
- if (!!this.changeProjectData) return;
- const where = { tid };
- if (this.ctx.session.sessionProject.page_show.isOnlyChecked) where.status = audit.changeProject.status.checked;
- this.changeProjectData = { data: await this.ctx.service.changeProject.getAllDataByCondition({ where }) };
- }
- async _generateChangeProjectAudit() {
- if (!!this.changeProjectData.audit) return;
- this.changeProjectData.audit = [];
- for (const c of this.changeProjectData.data) {
- const changeAudit = await this.ctx.service.changeProjectAudit.getAllDataByCondition({ where: { cpid: c.id, times: c.times }});
- const changeAuditFilter = this.ctx.helper.filterLastestData(changeAudit, ['aid']);
- this.changeProjectData.audit.push(...changeAuditFilter);
- }
- }
- async getChangeProjectData(tid, sid, fields) {
- try {
- await this._generateChangeProject(tid);
- return this.changeProjectData.data;
- } catch (err) {
- this.ctx.log(err);
- return [];
- }
- }
- async getChangeProjectAuditData(tid, sid, fields) {
- try {
- await this._generateChangeProject(tid);
- await this._generateChangeProjectAudit();
- return this.changeProjectData.audit;
- } catch (err) {
- return [];
- }
- }
- async getStageJgcl(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const data = await this.ctx.service.stageJgcl.getStageData(this.ctx.stage);
- const preData = await this.ctx.service.stageJgcl.getPreStageData(this.ctx.stage.order);
- for (const d of data) {
- const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
- if (pd) {
- d.pre_arrive_qty = pd.arrive_qty;
- d.pre_arrive_tp = pd.arrive_tp;
- d.pre_deduct_qty = pd.deduct_qty;
- d.pre_deduct_tp = pd.deduct_tp;
- }
- d.end_arrive_qty = this.ctx.helper.add(d.pre_arrive_qty, d.arrive_qty);
- d.end_arrive_tp = this.ctx.helper.add(d.pre_arrive_tp, d.arrive_tp);
- d.end_deduct_qty = this.ctx.helper.add(d.pre_deduct_qty, d.deduct_qty);
- d.end_deduct_tp = this.ctx.helper.add(d.pre_deduct_tp, d.deduct_tp);
- }
- return data;
- } catch (err) {
- return [];
- }
- }
- async getStageBonus(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const data = await this.ctx.service.stageBonus.getEndStageData(this.ctx.stage.order);
- for (const d of data) {
- const names = this.ctx.helper._.map(d.proof_file, function (x) {
- return x.filename + x.fileext;
- });
- d.proof = names.join('\n');
- }
- return data;
- } catch (err) {
- return [];
- }
- }
- async getStageOther(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const data = await this.ctx.service.stageOther.getStageData(this.ctx.stage);
- const preData = await this.ctx.service.stageOther.getPreStageData(this.ctx.stage.order);
- for (const d of data) {
- const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
- if (pd) {
- d.pre_tp = pd.tp;
- }
- d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
- }
- return data;
- } catch (err) {
- return [];
- }
- }
- async getStageSafeProd(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const data = await this.ctx.service.stageSafeProd.getStageData(this.ctx.stage);
- const preData = await this.ctx.service.stageSafeProd.getPreStageData(this.ctx.stage.order);
- for (const d of data) {
- const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
- if (pd) {
- d.pre_qty = pd.qty;
- d.pre_tp = pd.tp;
- }
- d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
- d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
- }
- return data;
- } catch (err) {
- return [];
- }
- }
- async getStageTempLand(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const data = await this.ctx.service.stageTempLand.getStageData(this.ctx.stage);
- const preData = await this.ctx.service.stageTempLand.getPreStageData(this.ctx.stage.order);
- for (const d of data) {
- const pd = this.ctx.helper._.find(preData, {uuid: d.uuid});
- if (pd) {
- d.pre_qty = pd.qty;
- d.pre_tp = pd.tp;
- }
- d.end_qty = this.ctx.helper.add(d.pre_qty, d.qty);
- d.end_tp = this.ctx.helper.add(d.pre_tp, d.tp);
- }
- return data;
- } catch (err) {
- return [];
- }
- }
- async getSumStageBillsData(tid, sid, fields) {
- try {
- await this.ctx.service.tender.checkTender(tid);
- const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
- const checkStageField = function (stageOrder) {
- for (const f of fields) {
- if (f.indexOf('s' + stageOrder + '_') >= 0) {
- return true;
- }
- }
- return false;
- };
- const calcFields = ['deal_tp', 'total_price'], calcPrefix = [];
- const stages = this.ctx.service.stage.getValidStages(this.ctx.tender.id);
- for (const stage of stages) {
- if (!checkStageField(stage.order)) return;
- await this.ctx.service.stage.doCheckStage(stage);
- calcFields.push('s' + stage.order + '_contract_tp');
- calcFields.push('s' + stage.order + '_qc_tp');
- calcFields.push('s' + stage.order + '_gather_tp');
- calcFields.push('s' + stage.order + '_');
- const curStage = stage.readOnly
- ? await this.ctx.service.stageBills.getAuditorStageData2(this.ctx.tender.id, stage.id, stage.curTimes, stage.curOrder)
- : await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, stage.id);
- this.ctx.helper.assignRelaData(billsData, [
- {data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp'], prefix: 's' + stage.order + '_', relaId: 'lid'}
- ]);
- }
- 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: calcFields,
- calc: function (node) {
- for (const prefix of calcPrefix) {
- if (node.children && node.children.length === 0) {
- node[prefix + 'gather_qty'] = self.ctx.helper.add(node[prefix + 'contract_qty'], node[prefix + 'qc_qty']);
- }
- node[prefix + 'gather_tp'] = self.ctx.helper.add(node[prefix + 'contract_tp'], node[prefix + 'qc_tp']);
- }
- }
- });
- billsTree.loadDatas(billsData);
- billsTree.calculateAll();
- return billsTree.getDefaultDatas();
- } catch (err) {
- return [];
- }
- }
- async getStageAuditors(tid, sid) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const auditors = await this.ctx.service.stageAudit.getFinalAuditGroup(this.ctx.stage.id, this.ctx.stage.curTimes);
- const user = await this.ctx.service.projectAccount.getDataById(this.ctx.stage.user_id);
- const result = [{
- aid: user.id,
- name: user.name,
- company: user.company,
- role: user.role,
- mobile: user.mobile,
- telephone: user.telephone,
- sign_path: user.sign_path,
- opinion: user.opinion,
- end_time: auditors && auditors.length > 0 ? auditors[0].begin_time : null,
- sort: 0,
- }, ...auditors];
- return result;
- }
- async getSignSelect(tid, sid, customSelect) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- const signSelect = customSelect.sign_select, result = {};
- for (const [i, ss] of signSelect.entries()) {
- const user = await this.ctx.service.projectAccount.getDataById(ss.id);
- const sign = {
- id: ss.id, name: user.name, company: user.company, role: user.role,
- mobile: user.mobile, telephone: user.telephone,
- };
- if (ss.id !== this.ctx.stage.user_id) {
- const audit = this.ctx.stage.auditors.find(x => {return x.aid === ss.id});
- user.end_time = audit ? audit.end_time : ss.audit_time;
- } else {
- user.end_time = this.ctx.stage.auditors[0].end_time;
- }
- if (user.end_time) sign.sign_path = user.sign_path;
- result['sign' + (i+1)] = sign;
- }
- return result;
- }
- async getChangeInfo(tid, sid) {
- await this.ctx.service.tender.checkTender(tid);
- await this.ctx.service.stage.checkStage(sid);
- this.changeInfo = await this.ctx.service.stageChange.getChangeWithUsedInfo(this.ctx.stage);
- const usedChangesId = await this.ctx.service.stageChange.getStageUsedChangeId(sid);
- for (const c of this.changeInfo) {
- c.cur_used = usedChangesId.indexOf(c.cid) >= 0;
- c.used_ratio = c.used_pt;
- c.is_import = 0;
- }
- return this.changeInfo;
- }
- async getChangeInfoBills(tid, sid) {
- if (!this.changeInfo) await this.getChangeInfo(tid, sid);
- this.changeInfoBills = [];
- for (const c of this.changeInfo) {
- const cb = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: c.cid } });
- const cbu = await this.ctx.service.stageChange.getUsedData(tid, c.cid);
- const curUsedBills = await this.ctx.service.stageChange.getStageUsedData(sid, c.cid);
- for (const b of cb) {
- b.qty = this.ctx.helper._.toNumber(b.samount);
- b.tp = this.ctx.helper.round(this.ctx.helper.mul(b.qty, b.unit_price), this.ctx.tender.info.decimal.tp);
- const u = cbu.filter(x => { return x.cbid === b.id; });
- b.used_qty = u.length > 0 ? this.ctx.helper.sum(u.map(x => { return x.qty})) : 0;
- b.valid_qty = this.ctx.helper.sub(b.qty, b.used_qty);
- const cur = this.ctx.helper._.filter(curUsedBills, { cbid: b.id });
- b.cur_qty = 0;
- for (const cc of cur) {
- b.cur_qty = this.ctx.helper.add(b.cur_qty, cc.qty);
- }
- b.cur_used = !!b.cur_qty;
- this.changeInfoBills.push(b);
- }
- }
- return this.changeInfoBills;
- }
- async getPmDeal() {
- let result = [];
- if (!PermissionCheck.viewPmData(this.ctx.session.sessionUser.permission)) return result;
- const selects = await this.ctx.service.project.getPmDealCache(this.ctx.session.sessionProject.id);
- const pm = require('../lib/pm');
- this.pmDeal = await pm.dealData(this.ctx, this.ctx.session.sessionProject.code, selects);
- result = this.pmDeal.contracts ? this.pmDeal.contracts.filter(x => { return x.ContractsType === 2 }) : [];
- const fields = ['Code', 'Name', 'ContractName', 'ContractCode', 'ContractPrice', 'ContractReturned', 'ContractsPaid', 'ContractStatus', 'ContractLocking', 'CreateTime'], self = this;
- result.forEach(x => {
- const treeNode = self.pmDeal.tree_contracts.find(y => { return x.TreeId === y.Id });
- if (!treeNode) return;
- for (const f of fields) {
- x['Tree_' + f] = treeNode[f];
- }
- });
- // 排序:标段-合同类别-创建时间
- result.sort((x, y) => {
- const bidSort = selects.indexOf(x.BidsectionId + '') - selects.indexOf(y.BidsectionId + '');
- if (bidSort) return bidSort;
- const typeSort = x.ContractsType - y.ContractsType;
- if (typeSort) return typeSort;
- return x.create_time - y.create_time;
- });
- return result;
- }
- }
- return ReportMemory;
- };
|