123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/10/30
- * @version
- */
- const infoConst = require('../const/tender_info');
- const parseInfo = infoConst.parseInfo;
- const arrayInfo = infoConst.arrayInfo;
- const defaultInfo = infoConst.defaultInfo;
- const advanceConst = require('../const/audit').advance;
- const auditConst = require('../const/audit');
- module.exports = app => {
- class TenderInfo extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'tender_info';
- }
- /**
- * 新增 标段相关信息
- *
- * @param tenderId - 标段Id
- * @param projectId - 项目Id
- * @param transaction - 事务
- * @return {Promise<void>}
- */
- async addTenderInfo(tenderId, projectId, transaction) {
- const info = JSON.parse(JSON.stringify(defaultInfo));
- info.tid = tenderId;
- info.pid = projectId;
- for (const pi of parseInfo) {
- info[pi] = JSON.stringify(info[pi]);
- }
- for (const pi of arrayInfo) {
- info[pi] = JSON.stringify(info[pi]);
- }
- if (transaction) {
- await transaction.insert(this.tableName, info);
- } else {
- await this.db.insert(this.tableName, info);
- }
- return info;
- }
- /**
- * 获取标段相关信息
- * @param tenderId
- * @return {Promise<void>}
- */
- async getTenderInfo(tenderId) {
- let info = await this.getDataByCondition({ tid: tenderId });
- // 兼容不存在info的情况
- if (!info) {
- info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
- }
- for (const pi of parseInfo) {
- info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
- this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
- }
- for (const ai of arrayInfo) {
- info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
- }
- if (info.decimal) {
- info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
- }
- return info;
- }
- /**
- * 获取标段相关信息(报表用)
- * @param tenderId
- * @return {Promise<void>}
- */
- async getTenderInfoEx(tenderId) {
- const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
- const sqlParam = [tenderId];
- const list = await this.db.query(sql, sqlParam);
- const info = list[0];
- const len = info.deal_info.length;
- info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
- for (const pi of parseInfo) {
- info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
- this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
- }
- for (const ai of arrayInfo) {
- info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
- }
- if (info.decimal) {
- info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
- }
- return info;
- }
- /**
- * 保存标段相关信息
- *
- * @param data
- * @return {Promise<void>}
- */
- async saveTenderInfo(tenderId, data) {
- for (const di in data) {
- if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
- data[di] = JSON.stringify(data[di]);
- }
- }
- await this.db.update(this.tableName, data, { where: { tid: tenderId } });
- }
- async _getLedgerService() {
- try {
- if (this.ctx.tender.data.ledger_status === auditConst.ledger.status.checked) {
- const stageCount = await this.ctx.service.stage.count({tid: this.ctx.tender.id});
- if (stageCount === 0) {
- const revise = await this.ctx.service.ledgerRevise.getLastestRevise(this.ctx.tender.id);
- if (revise.status === auditConst.revise.status.uncheck || revise.status === auditConst.revise.status.checkNo) {
- return [this.ctx.service.reviseBills, this.ctx.service.revisePos];
- }
- }
- } else {
- return [this.ctx.service.ledger, this.ctx.service.pos];
- }
- } catch(err) {
- }
- return [];
- }
- async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
- const changePrecision = [];
- const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
- const defUnits = this._.map(newPrecision, 'units');
- const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
- let changeUnits = [];
- for (const prop in newPrecision) {
- if (oldPrecision[prop]) {
- if (newPrecision[prop].value < oldPrecision[prop].value) {
- changePrecision.push(newPrecision[prop]);
- }
- } else {
- changePrecision.push(newPrecision[prop]);
- }
- }
- for (const cp of changePrecision) {
- if (cp.unit) {
- changeUnits.push(cp.unit);
- } else {
- changeUnits.push(otherUnits);
- }
- }
- changeUnits = this._.flatten(changeUnits);
- const [billsService, posService] = await this._getLedgerService();
- if (changeUnits.length > 0 && billsService && posService) {
- const bills = await billsService.getAllDataByCondition({
- columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
- where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
- });
- const pos = changeUnits.length > 0 ? await posService.getPosDataByUnits(tenderId, changeUnits) : [];
- for (const b of bills) {
- const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
- const bPos = this._.filter(pos, { lid: b.id });
- if (bPos.length > 0) {
- let sgfh_qty = 0,
- sjcl_qty = 0,
- qtcl_qty = 0,
- quantity = 0;
- for (const p of bPos) {
- this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
- p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
- sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
- sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
- qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
- quantity = this.ctx.helper.add(quantity, p.quantity);
- }
- b.sgfh_qty = sgfh_qty;
- b.sjcl_qty = sjcl_qty;
- b.qtcl_qty = qtcl_qty;
- b.quantity = quantity;
- } else {
- this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
- }
- b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
- b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
- b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
- b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
- b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
- b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName,
- { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
- if (bills.length > 0) await transaction.updateRows(billsService.tableName, bills);
- if (pos.length > 0) await transaction.updateRows(posService.tableName, pos);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- } else {
- await this.db.update(this.tableName,
- { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
- }
- }
- async _reCalcLedger(tenderId, billsService, newDecimal, oldDecimal) {
- if (!billsService) return [];
- const changeBills = [];
- const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
- if (calcUp || calcTp) {
- const bills = await billsService.getAllDataByCondition({
- columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
- where: { tender_id: tenderId, is_leaf: true },
- });
- for (const b of bills) {
- const cb = { id: b.id };
- cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
- cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
- cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
- cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
- cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
- cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
- changeBills.push(cb);
- }
- }
- return changeBills;
- }
- async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
- let changeSj = [], changeSb = [], changeSo = [];
- const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
- if (!stage) return [changeSj, changeSb, changeSo];
- const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
- const calcUp = upDecimal < oldDecimal.up,
- calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
- if (calcUp || calcTp) {
- const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
- columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
- where: { sid: stage.id }
- });
- for (const sj of stageJgcl) {
- const cj = { id: sj.id };
- cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
- cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
- cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
- changeSj.push(cj);
- }
- }
- if (calcTp) {
- changeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
- columns: ['id', 'tp'],
- where: { sid: stage.id }
- });
- for (const cb of changeSb) {
- cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
- }
- changeSo = await this.ctx.service.stageOther.getAllDataByCondition({
- columns: ['id', 'total_price', 'tp'],
- where: { sid: stage.id }
- });
- for (const co of changeSo) {
- if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
- co.tp = this.ctx.helper.round(co.tp, tpDecimal);
- }
- }
- return [changeSj, changeSb, changeSo];
- }
- async saveDecimal(tenderId, newDecimal, oldDecimal) {
- const changeAdvanceBills = [];
- const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
- if (caclPayTp) {
- // 获取预付款需要修改的相关记录
- const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
- columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
- where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
- });
- const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
- // 根据精度重新计算相关金额
- for (const ad of ad_bills) {
- const cb = { id: ad.id };
- const cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
- cb.cur_amount = cur_amount;
- cb.prev_total_amount = this.ctx.helper.add(cur_amount, ad.prev_amount);
- changeAdvanceBills.push(cb);
- }
- }
- const [billsService] = await this._getLedgerService();
- console.log(billsService);
- const changeBills = await this._reCalcLedger(tenderId, billsService, newDecimal, oldDecimal);
- const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
- if (changeBills.length > 0 ||
- changeAdvanceBills.length > 0 ||
- changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName,
- { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
- if (changeBills.length > 0) await transaction.updateRows(billsService.tableName, changeBills);
- if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
- if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
- if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
- if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
- await transaction.commit();
- } catch (error) {
- await transaction.rollback();
- throw error;
- }
- } else {
- await this.db.update(this.tableName,
- { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
- }
- }
- /**
- * 获取标段审批相关信息 (审批设置用)
- * @param tenderId
- * @return {Promise<void>}
- */
- async getTenderShenpiInfo(tenderId) {
- const info = await this.getDataByCondition({ tid: tenderId });
- // 还没选择模式的标段不应该可以选择审批流程设置
- if (!info) {
- return false;
- }
- const defaultShenpiInfo = JSON.parse(JSON.stringify(defaultInfo.shenpi));
- const shenpiInfo = !info.shenpi || info.shenpi === null || info.shenpi === '' ? defaultShenpiInfo : JSON.parse(info.shenpi);
- return shenpiInfo;
- }
- /**
- * 拷贝标段数据至当前标段
- * @param {number} id - 当前标段id
- * @param {number} copy_id - 被拷贝的标段id
- * @param {Array} typeArr - 需要拷贝的类型数组
- */
- async copyTenderHandler(id, copy_id, typeArr) {
- const columns = [];
- typeArr.forEach(item => {
- if (item === 'tender') {
- columns.push('deal_info', 'construction_unit', 'tech_param');
- } else if (item === 'chapter') {
- columns.push('chapter');
- } else if (item === 'pay_account') {
- columns.push('pay_account');
- }
- });
- if (!columns.length) throw '未选择需要拷贝的设置';
- const [data] = await this.getAllDataByCondition({
- where: {
- tid: copy_id,
- },
- columns,
- });
- const isUpdate = await this.update(data, { tid: id });
- if (isUpdate) {
- await this.ctx.service.tender.update({ copy_id }, { id });
- }
- }
- }
- return TenderInfo;
- };
|