|
@@ -11,6 +11,7 @@
|
|
|
const auditConst = require('../const/audit').material;
|
|
const auditConst = require('../const/audit').material;
|
|
|
const materialConst = require('../const/material');
|
|
const materialConst = require('../const/material');
|
|
|
const MaterialCalculator = require('../lib/material_calc');
|
|
const MaterialCalculator = require('../lib/material_calc');
|
|
|
|
|
+const Decimal = require('decimal.js');
|
|
|
|
|
|
|
|
module.exports = app => {
|
|
module.exports = app => {
|
|
|
class MaterialExponent extends app.BaseService {
|
|
class MaterialExponent extends app.BaseService {
|
|
@@ -25,6 +26,69 @@ module.exports = app => {
|
|
|
this.tableName = 'material_exponent';
|
|
this.tableName = 'material_exponent';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ _normalizeRisk(value) {
|
|
|
|
|
+ if (value === null || value === undefined || value === '') return 0;
|
|
|
|
|
+ const risk = Number(value);
|
|
|
|
|
+ if (!Number.isInteger(risk) || risk < 0 || risk > 100) {
|
|
|
|
|
+ throw '上涨、下跌幅度必须为0~100的整数';
|
|
|
|
|
+ }
|
|
|
|
|
+ return risk;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getEffectivePrice(data, decimal = (this.ctx.material.exponent_decimal ? this.ctx.material.exponent_decimal.up : materialConst.exponent_decimal.up)) {
|
|
|
|
|
+ if (data.m_price === null || data.m_price === undefined || data.m_price === '') return null;
|
|
|
|
|
+
|
|
|
|
|
+ const currentPrice = new Decimal(data.m_price);
|
|
|
|
|
+ const upRisk = this._normalizeRisk(data.m_up_risk);
|
|
|
|
|
+ const downRisk = this._normalizeRisk(data.m_down_risk);
|
|
|
|
|
+ let effectivePrice = currentPrice;
|
|
|
|
|
+
|
|
|
|
|
+ if ((upRisk !== 0 || downRisk !== 0) && data.basic_price !== null && data.basic_price !== undefined && data.basic_price !== '' && new Decimal(data.basic_price).gt(0)) {
|
|
|
|
|
+ const basicPrice = new Decimal(data.basic_price);
|
|
|
|
|
+ const upperLimit = basicPrice.mul(new Decimal(1).add(new Decimal(upRisk).div(100)));
|
|
|
|
|
+ const lowerLimit = basicPrice.mul(new Decimal(1).sub(new Decimal(downRisk).div(100)));
|
|
|
|
|
+
|
|
|
|
|
+ if (currentPrice.gt(upperLimit)) {
|
|
|
|
|
+ effectivePrice = currentPrice.sub(basicPrice.mul(upRisk).div(100));
|
|
|
|
|
+ } else if (currentPrice.lt(lowerLimit)) {
|
|
|
|
|
+ effectivePrice = currentPrice.add(basicPrice.mul(downRisk).div(100));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ effectivePrice = basicPrice;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Number(effectivePrice.toDecimalPlaces(Number(decimal), Decimal.ROUND_HALF_UP).toString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _normalizeUpdateData(data) {
|
|
|
|
|
+ const editableCols = [
|
|
|
|
|
+ 'id', 'symbol', 'symbol_desc', 'code', 'basic_price', 'basic_times',
|
|
|
|
|
+ 'm_up_risk', 'm_down_risk', 'm_price', 'weight_num', 'remark', 'is_summary',
|
|
|
|
|
+ ];
|
|
|
|
|
+ const updateData = this._.pick(data, editableCols);
|
|
|
|
|
+ if (!updateData.id) throw '指数工料参数有误';
|
|
|
|
|
+ if (updateData.m_up_risk !== undefined) updateData.m_up_risk = this._normalizeRisk(updateData.m_up_risk);
|
|
|
|
|
+ if (updateData.m_down_risk !== undefined) updateData.m_down_risk = this._normalizeRisk(updateData.m_down_risk);
|
|
|
|
|
+ return updateData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _setEffectivePrice(currentData, updateData) {
|
|
|
|
|
+ const priceCols = ['basic_price', 'm_price', 'm_up_risk', 'm_down_risk'];
|
|
|
|
|
+ const needUpdatePrice = priceCols.some(col => updateData[col] !== undefined);
|
|
|
|
|
+ const needUpdateCalc = needUpdatePrice || updateData.weight_num !== undefined;
|
|
|
|
|
+ if (!needUpdateCalc) return updateData;
|
|
|
|
|
+ const calcData = Object.assign({}, currentData, updateData);
|
|
|
|
|
+ if (needUpdatePrice) {
|
|
|
|
|
+ updateData.effective_price = this.getEffectivePrice(calcData);
|
|
|
|
|
+ calcData.effective_price = updateData.effective_price;
|
|
|
|
|
+ }
|
|
|
|
|
+ const effectivePrice = calcData.effective_price !== null && calcData.effective_price !== undefined ? calcData.effective_price : calcData.m_price;
|
|
|
|
|
+ const calcNum = calcData.type === materialConst.ex_type[1].value && calcData.basic_price > 0 ?
|
|
|
|
|
+ this.ctx.helper.mul(calcData.weight_num, this.ctx.helper.div(effectivePrice, calcData.basic_price)) : null;
|
|
|
|
|
+ updateData.calc_num = calcNum ? this.ctx.helper.round(calcNum, this.ctx.material.exponent_decimal.qty) : calcNum;
|
|
|
|
|
+ return updateData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 添加指数清单
|
|
* 添加指数清单
|
|
|
* @return {void}
|
|
* @return {void}
|
|
@@ -156,7 +220,7 @@ module.exports = app => {
|
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
if (!this.ctx.tender || !this.ctx.material) {
|
|
|
throw '数据错误';
|
|
throw '数据错误';
|
|
|
}
|
|
}
|
|
|
- delete data.in_time;
|
|
|
|
|
|
|
+ data = this._normalizeUpdateData(data);
|
|
|
// delete data.m_tp;
|
|
// delete data.m_tp;
|
|
|
// 判断是否可修改
|
|
// 判断是否可修改
|
|
|
// 判断t_type是否为费用
|
|
// 判断t_type是否为费用
|
|
@@ -165,10 +229,15 @@ module.exports = app => {
|
|
|
const result = {};
|
|
const result = {};
|
|
|
const info = {};
|
|
const info = {};
|
|
|
switch (this.ctx.material.exponentStatus) {
|
|
switch (this.ctx.material.exponentStatus) {
|
|
|
- case materialConst.exponent_status.shared_noNode:
|
|
|
|
|
|
|
+ case materialConst.exponent_status.shared_noNode: {
|
|
|
|
|
+ const exponentInfo = await transaction.get(this.tableName, { id: data.id, tid: this.ctx.tender.id });
|
|
|
|
|
+ if (!exponentInfo) throw '指数工料数据不存在';
|
|
|
|
|
+ this._setEffectivePrice(exponentInfo, data);
|
|
|
await transaction.update(this.tableName, data);
|
|
await transaction.update(this.tableName, data);
|
|
|
[result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
|
|
[result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
|
|
|
|
|
+ result.exponentData = await transaction.select(this.tableName, { where: { id: data.id } });
|
|
|
break;
|
|
break;
|
|
|
|
|
+ }
|
|
|
case materialConst.exponent_status.shared_node:
|
|
case materialConst.exponent_status.shared_node:
|
|
|
if (!mn_id) {
|
|
if (!mn_id) {
|
|
|
throw '分项参数有误';
|
|
throw '分项参数有误';
|
|
@@ -215,6 +284,9 @@ module.exports = app => {
|
|
|
break;
|
|
break;
|
|
|
default: break;
|
|
default: break;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (this.ctx.material.order < this.ctx.material.highOrder && this.ctx.session.sessionUser.is_admin) {
|
|
|
|
|
+ await this._syncHistoricalTotals(transaction);
|
|
|
|
|
+ }
|
|
|
await transaction.commit();
|
|
await transaction.commit();
|
|
|
return result;
|
|
return result;
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
@@ -248,6 +320,9 @@ module.exports = app => {
|
|
|
updateData.id = data.id;
|
|
updateData.id = data.id;
|
|
|
await transaction.update(this.tableName, updateData);
|
|
await transaction.update(this.tableName, updateData);
|
|
|
}
|
|
}
|
|
|
|
|
+ if (updateData.basic_price !== undefined) {
|
|
|
|
|
+ await this.ctx.service.materialExponentShard.resetEffectivePrice(transaction, [data.id]);
|
|
|
|
|
+ }
|
|
|
if (needCalc) {
|
|
if (needCalc) {
|
|
|
if (mn_id) {
|
|
if (mn_id) {
|
|
|
await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id, mn_id);
|
|
await this.ctx.service.materialExponentShard.calcMaterialAllExTpByNode(transaction, this.ctx.material.id, mn_id);
|
|
@@ -271,17 +346,25 @@ module.exports = app => {
|
|
|
// 判断t_type是否为费用
|
|
// 判断t_type是否为费用
|
|
|
const transaction = await this.db.beginTransaction();
|
|
const transaction = await this.db.beginTransaction();
|
|
|
try {
|
|
try {
|
|
|
|
|
+ datas = datas.map(data => this._normalizeUpdateData(data));
|
|
|
const result = { ex_tp: this.ctx.material.ex_tp };
|
|
const result = { ex_tp: this.ctx.material.ex_tp };
|
|
|
const info = {};
|
|
const info = {};
|
|
|
if (this.ctx.material.exponentStatus !== materialConst.exponent_status.shared_noNode) {
|
|
if (this.ctx.material.exponentStatus !== materialConst.exponent_status.shared_noNode) {
|
|
|
info.meList = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
|
|
info.meList = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
|
|
|
}
|
|
}
|
|
|
switch (this.ctx.material.exponentStatus) {
|
|
switch (this.ctx.material.exponentStatus) {
|
|
|
- case materialConst.exponent_status.shared_noNode:
|
|
|
|
|
|
|
+ case materialConst.exponent_status.shared_noNode: {
|
|
|
|
|
+ const exponentList = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id'), tid: this.ctx.tender.id } });
|
|
|
|
|
+ for (const data of datas) {
|
|
|
|
|
+ const exponentInfo = this._.find(exponentList, { id: data.id });
|
|
|
|
|
+ if (!exponentInfo) throw '指数工料数据不存在';
|
|
|
|
|
+ this._setEffectivePrice(exponentInfo, data);
|
|
|
|
|
+ }
|
|
|
await transaction.updateRows(this.tableName, datas);
|
|
await transaction.updateRows(this.tableName, datas);
|
|
|
[result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
|
|
[result.ex_tp, result.ex_tax_tp, result.ex_expr] = await this.calcMaterialExTp(transaction);
|
|
|
result.exponentData = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
|
|
result.exponentData = await transaction.select(this.tableName, { where: { id: this._.map(datas, 'id') } });
|
|
|
break;
|
|
break;
|
|
|
|
|
+ }
|
|
|
case materialConst.exponent_status.shared_node:
|
|
case materialConst.exponent_status.shared_node:
|
|
|
if (!mn_id) {
|
|
if (!mn_id) {
|
|
|
throw '参数有误';
|
|
throw '参数有误';
|
|
@@ -316,6 +399,9 @@ module.exports = app => {
|
|
|
break;
|
|
break;
|
|
|
default: break;
|
|
default: break;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (this.ctx.material.order < this.ctx.material.highOrder && this.ctx.session.sessionUser.is_admin) {
|
|
|
|
|
+ await this._syncHistoricalTotals(transaction);
|
|
|
|
|
+ }
|
|
|
await transaction.commit();
|
|
await transaction.commit();
|
|
|
return result;
|
|
return result;
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
@@ -324,6 +410,105 @@ module.exports = app => {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async saveHistoryRisks(datas) {
|
|
|
|
|
+ if (!this.ctx.session.sessionUser.is_admin || this.ctx.material.order >= this.ctx.material.highOrder) {
|
|
|
|
|
+ throw '无权修改往期指数幅度';
|
|
|
|
|
+ }
|
|
|
|
|
+ const updateList = datas instanceof Array ? datas : [datas];
|
|
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
|
|
+ try {
|
|
|
|
|
+ const historyIds = this._.map(updateList, 'id');
|
|
|
|
|
+ const historyList = await transaction.select(this.ctx.service.materialExponentHistory.tableName, {
|
|
|
|
|
+ where: { id: historyIds, mid: this.ctx.material.id, order: this.ctx.material.order },
|
|
|
|
|
+ });
|
|
|
|
|
+ const updates = [];
|
|
|
|
|
+ for (const data of updateList) {
|
|
|
|
|
+ const historyInfo = this._.find(historyList, { id: data.id });
|
|
|
|
|
+ if (!historyInfo || historyInfo.type !== materialConst.ex_type[1].value) throw '往期指数工料数据不存在';
|
|
|
|
|
+ const updateData = { id: historyInfo.id };
|
|
|
|
|
+ if (data.m_up_risk !== undefined) updateData.m_up_risk = this._normalizeRisk(data.m_up_risk);
|
|
|
|
|
+ if (data.m_down_risk !== undefined) updateData.m_down_risk = this._normalizeRisk(data.m_down_risk);
|
|
|
|
|
+ if (Object.keys(updateData).length === 1) continue;
|
|
|
|
|
+ updateData.effective_price = this.getEffectivePrice(Object.assign({}, historyInfo, updateData));
|
|
|
|
|
+ const calcNum = historyInfo.basic_price > 0 ? this.ctx.helper.mul(historyInfo.weight_num, this.ctx.helper.div(updateData.effective_price, historyInfo.basic_price)) : 0;
|
|
|
|
|
+ updateData.calc_num = calcNum ? this.ctx.helper.round(calcNum, this.ctx.material.exponent_decimal.qty) : calcNum;
|
|
|
|
|
+ updates.push(updateData);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (updates.length === 0) throw '上涨、下跌幅度未发生变化';
|
|
|
|
|
+ await transaction.updateRows(this.ctx.service.materialExponentHistory.tableName, updates);
|
|
|
|
|
+ const [ex_tp, ex_tax_tp, ex_expr] = await this._calcHistoryExTp(transaction);
|
|
|
|
|
+ await this._syncHistoricalTotals(transaction);
|
|
|
|
|
+ const historyData = await transaction.select(this.ctx.service.materialExponentHistory.tableName, { where: { id: this._.map(updates, 'id') } });
|
|
|
|
|
+ await transaction.commit();
|
|
|
|
|
+ return { ex_tp, ex_tax_tp, ex_expr, historyData };
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ await transaction.rollback();
|
|
|
|
|
+ throw err;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async _calcHistoryExTp(transaction) {
|
|
|
|
|
+ let basicCalc = 0;
|
|
|
|
|
+ const exCalc = this.ctx.material.ex_calc ? JSON.parse(this.ctx.material.ex_calc) : null;
|
|
|
|
|
+ if (exCalc) {
|
|
|
|
|
+ for (const calc of exCalc) {
|
|
|
|
|
+ if (!calc.select) continue;
|
|
|
|
|
+ basicCalc = this.ctx.helper.add(basicCalc, calc.code === 'zdy' && calc.result ? calc.result : calc.value);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ const exponentList = await transaction.select(this.ctx.service.materialExponentHistory.tableName, {
|
|
|
|
|
+ where: { mid: this.ctx.material.id, order: this.ctx.material.order, is_summary: materialConst.is_summary.yes },
|
|
|
|
|
+ });
|
|
|
|
|
+ let expr = basicCalc + '*[';
|
|
|
|
|
+ let sumByChange = 0;
|
|
|
|
|
+ let constant = 0;
|
|
|
|
|
+ for (const ex of exponentList) {
|
|
|
|
|
+ if (ex.type === materialConst.ex_type[0].value) {
|
|
|
|
|
+ constant = ex.weight_num || 0;
|
|
|
|
|
+ expr += constant.toString();
|
|
|
|
|
+ } else if (ex.type === materialConst.ex_type[1].value) {
|
|
|
|
|
+ const effectivePrice = ex.effective_price !== null && ex.effective_price !== undefined ? ex.effective_price : ex.m_price;
|
|
|
|
|
+ const calcNum = ex.basic_price > 0 ? this.ctx.helper.mul(ex.weight_num, this.ctx.helper.div(effectivePrice, ex.basic_price)) : 0;
|
|
|
|
|
+ const change = calcNum ? this.ctx.helper.round(calcNum, this.ctx.material.exponent_decimal.qty) : 0;
|
|
|
|
|
+ expr += change !== 0 ? '+' + ex.weight_num.toString() + '*(' + effectivePrice.toString() + '/' + ex.basic_price.toString() + ')' : '';
|
|
|
|
|
+ sumByChange = this.ctx.helper.add(sumByChange, change);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ expr += '-1]';
|
|
|
|
|
+ expr = constant !== 0 ? expr : null;
|
|
|
|
|
+ const exTp = constant !== 0 ? this.ctx.helper.round(this.ctx.helper.mul(basicCalc, this.ctx.helper.sub(this.ctx.helper.add(constant, sumByChange), 1)), this.ctx.material.exponent_decimal.tp) : null;
|
|
|
|
|
+ const exTaxTp = exTp !== null ? this.ctx.helper.round(this.ctx.helper.mul(exTp, 1 + this.ctx.material.exponent_rate / 100), this.ctx.material.exponent_decimal.tp) : null;
|
|
|
|
|
+ await transaction.update(this.ctx.service.material.tableName, {
|
|
|
|
|
+ id: this.ctx.material.id,
|
|
|
|
|
+ ex_tp: exTp,
|
|
|
|
|
+ ex_tax_tp: exTaxTp,
|
|
|
|
|
+ ex_expr: expr,
|
|
|
|
|
+ });
|
|
|
|
|
+ return [exTp, exTaxTp, expr];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async _syncHistoricalTotals(transaction) {
|
|
|
|
|
+ const materialList = await transaction.select(this.ctx.service.material.tableName, {
|
|
|
|
|
+ where: { tid: this.ctx.tender.id },
|
|
|
|
|
+ orders: [['order', 'asc']],
|
|
|
|
|
+ });
|
|
|
|
|
+ let exponentPreTp = 0;
|
|
|
|
|
+ let exponentTaxPreTp = 0;
|
|
|
|
|
+ const updates = [];
|
|
|
|
|
+ for (const material of materialList) {
|
|
|
|
|
+ updates.push({
|
|
|
|
|
+ id: material.id,
|
|
|
|
|
+ ex_pre_tp: exponentPreTp,
|
|
|
|
|
+ ex_tax_pre_tp: exponentTaxPreTp,
|
|
|
|
|
+ });
|
|
|
|
|
+ exponentPreTp = this.ctx.helper.add(exponentPreTp, material.ex_tp || 0);
|
|
|
|
|
+ exponentTaxPreTp = this.ctx.helper.add(exponentTaxPreTp, material.ex_tax_tp || 0);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (updates.length > 0) await transaction.updateRows(this.ctx.service.material.tableName, updates);
|
|
|
|
|
+ const tpData = await this.ctx.service.materialAudit.getTpData(transaction, this.ctx.material.id, this.ctx.material.decimal, this.ctx.material.exponent_decimal);
|
|
|
|
|
+ await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, tp_data: JSON.stringify(tpData) });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async _updateMoreExponentData(transaction, datas, result, meList, ms_id = null, mn_id = null) {
|
|
async _updateMoreExponentData(transaction, datas, result, meList, ms_id = null, mn_id = null) {
|
|
|
// 判断data值是否需要更新materialExponent和materialExponentShard
|
|
// 判断data值是否需要更新materialExponent和materialExponentShard
|
|
|
const mesCondition = { me_id: this._.map(datas, 'id') };
|
|
const mesCondition = { me_id: this._.map(datas, 'id') };
|
|
@@ -331,8 +516,8 @@ module.exports = app => {
|
|
|
if (mn_id !== null) mesCondition.mn_id = mn_id;
|
|
if (mn_id !== null) mesCondition.mn_id = mn_id;
|
|
|
const mesList = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: mesCondition });
|
|
const mesList = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: mesCondition });
|
|
|
const materialExponentColArray = ['symbol', 'symbol_desc', 'code', 'basic_price', 'is_summary', 'basic_times'];
|
|
const materialExponentColArray = ['symbol', 'symbol_desc', 'code', 'basic_price', 'is_summary', 'basic_times'];
|
|
|
- const materialExponentShardColArray = ['weight_num', 'm_price', 'remark'];
|
|
|
|
|
- const updateCalcArray = ['weight_num', 'basic_price', 'm_price', 'is_summary'];
|
|
|
|
|
|
|
+ const materialExponentShardColArray = ['weight_num', 'm_up_risk', 'm_down_risk', 'm_price', 'remark'];
|
|
|
|
|
+ const updateCalcArray = ['weight_num', 'basic_price', 'm_up_risk', 'm_down_risk', 'm_price', 'is_summary'];
|
|
|
const updateMeArray = [];
|
|
const updateMeArray = [];
|
|
|
const udpateMseArray = [];
|
|
const udpateMseArray = [];
|
|
|
let needCalc = false;
|
|
let needCalc = false;
|
|
@@ -377,9 +562,15 @@ module.exports = app => {
|
|
|
}
|
|
}
|
|
|
if (udpateMseArray.length > 0) {
|
|
if (udpateMseArray.length > 0) {
|
|
|
await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, udpateMseArray);
|
|
await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, udpateMseArray);
|
|
|
|
|
+ await this.ctx.service.materialExponentShard.resetEffectivePrice(transaction, this._.uniq(this._.map(udpateMseArray, 'id')), true);
|
|
|
// const condition = mn_id ? { mid: this.ctx.material.id, mn_id } : { mid: this.ctx.material.id, ms_id };
|
|
// const condition = mn_id ? { mid: this.ctx.material.id, mn_id } : { mid: this.ctx.material.id, ms_id };
|
|
|
result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { id: this._.map(udpateMseArray, 'id') } });
|
|
result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { id: this._.map(udpateMseArray, 'id') } });
|
|
|
}
|
|
}
|
|
|
|
|
+ const basicPriceMeIds = this._.map(this._.filter(updateMeArray, data => data.basic_price !== undefined), 'id');
|
|
|
|
|
+ if (basicPriceMeIds.length > 0) {
|
|
|
|
|
+ await this.ctx.service.materialExponentShard.resetEffectivePrice(transaction, basicPriceMeIds);
|
|
|
|
|
+ result.exponentShardData = await transaction.select(this.ctx.service.materialExponentShard.tableName, { where: { mid: this.ctx.material.id, me_id: basicPriceMeIds } });
|
|
|
|
|
+ }
|
|
|
return needCalc;
|
|
return needCalc;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -495,10 +686,11 @@ module.exports = app => {
|
|
|
constant = ex.weight_num ? ex.weight_num : 0;
|
|
constant = ex.weight_num ? ex.weight_num : 0;
|
|
|
expr += constant.toString();
|
|
expr += constant.toString();
|
|
|
} else if (ex.type === materialConst.ex_type[1].value) {
|
|
} else if (ex.type === materialConst.ex_type[1].value) {
|
|
|
- const calc_num = ex.basic_price > 0 ? this.ctx.helper.mul(ex.weight_num, this.ctx.helper.div(ex.m_price, ex.basic_price)) : 0;
|
|
|
|
|
|
|
+ const effectivePrice = ex.effective_price !== null && ex.effective_price !== undefined ? ex.effective_price : ex.m_price;
|
|
|
|
|
+ const calc_num = ex.basic_price > 0 ? this.ctx.helper.mul(ex.weight_num, this.ctx.helper.div(effectivePrice, ex.basic_price)) : 0;
|
|
|
const change = calc_num ? this.ctx.helper.round(calc_num, decimal.qty) : 0;
|
|
const change = calc_num ? this.ctx.helper.round(calc_num, decimal.qty) : 0;
|
|
|
// const change = ex.calc_num ? ex.calc_num : 0;
|
|
// const change = ex.calc_num ? ex.calc_num : 0;
|
|
|
- expr = expr + (change !== 0 ? '+' + ex.weight_num.toString() + '*(' + ex.m_price.toString() + '/' + ex.basic_price.toString() + ')' : '');
|
|
|
|
|
|
|
+ expr = expr + (change !== 0 ? '+' + ex.weight_num.toString() + '*(' + effectivePrice.toString() + '/' + ex.basic_price.toString() + ')' : '');
|
|
|
sumByChange = this.ctx.helper.add(sumByChange, change);
|
|
sumByChange = this.ctx.helper.add(sumByChange, change);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -596,45 +788,37 @@ module.exports = app => {
|
|
|
const updateArray = [];
|
|
const updateArray = [];
|
|
|
const updateStageArray = [];
|
|
const updateStageArray = [];
|
|
|
const upColArray = ['basic_price', 'm_price'];
|
|
const upColArray = ['basic_price', 'm_price'];
|
|
|
- // const qtyColArray = ['calc_num'];
|
|
|
|
|
for (const ex of exponentList) {
|
|
for (const ex of exponentList) {
|
|
|
|
|
+ let updateEx = null;
|
|
|
for (const col of upColArray) {
|
|
for (const col of upColArray) {
|
|
|
- if (ex[col] && ex[col] !== this.ctx.helper.round(ex[col], decimal.up)) {
|
|
|
|
|
- const upex = this._.find(updateArray, { id: ex.id });
|
|
|
|
|
- if (upex) {
|
|
|
|
|
- upex[col] = this.ctx.helper.round(ex[col], decimal.up);
|
|
|
|
|
- } else {
|
|
|
|
|
- const insertEx = {
|
|
|
|
|
- id: ex.id,
|
|
|
|
|
- };
|
|
|
|
|
- insertEx[col] = this.ctx.helper.round(ex[col], decimal.up);
|
|
|
|
|
- updateArray.push(insertEx);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (ex[col] !== null && ex[col] !== undefined && ex[col] !== this.ctx.helper.round(ex[col], decimal.up)) {
|
|
|
|
|
+ updateEx = updateEx || { id: ex.id };
|
|
|
|
|
+ updateEx[col] = this.ctx.helper.round(ex[col], decimal.up);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- // for (const col of qtyColArray) {
|
|
|
|
|
- // if (ex[col] && ex[col] !== this.ctx.helper.round(ex[col], decimal.qty)) {
|
|
|
|
|
- // const upex = this._.find(updateArray, { id: ex.id });
|
|
|
|
|
- // if (upex) {
|
|
|
|
|
- // upex[col] = this.ctx.helper.round(ex[col], decimal.qty);
|
|
|
|
|
- // } else {
|
|
|
|
|
- // const insertEx = {
|
|
|
|
|
- // id: ex.id,
|
|
|
|
|
- // };
|
|
|
|
|
- // insertEx[col] = this.ctx.helper.round(ex[col], decimal.qty);
|
|
|
|
|
- // updateArray.push(insertEx);
|
|
|
|
|
- // }
|
|
|
|
|
- // }
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ const effectivePrice = this.getEffectivePrice(Object.assign({}, ex, updateEx || {}), decimal.up);
|
|
|
|
|
+ if (effectivePrice !== ex.effective_price) {
|
|
|
|
|
+ updateEx = updateEx || { id: ex.id };
|
|
|
|
|
+ updateEx.effective_price = effectivePrice;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ex.type === materialConst.ex_type[1].value) {
|
|
|
|
|
+ const calcData = Object.assign({}, ex, updateEx || {});
|
|
|
|
|
+ const calcNum = calcData.basic_price > 0 ? this.ctx.helper.mul(calcData.weight_num, this.ctx.helper.div(effectivePrice, calcData.basic_price)) : 0;
|
|
|
|
|
+ const roundedCalcNum = calcNum ? this.ctx.helper.round(calcNum, decimal.qty) : calcNum;
|
|
|
|
|
+ if (roundedCalcNum !== ex.calc_num) {
|
|
|
|
|
+ updateEx = updateEx || { id: ex.id };
|
|
|
|
|
+ updateEx.calc_num = roundedCalcNum;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (updateEx) updateArray.push(updateEx);
|
|
|
}
|
|
}
|
|
|
for (const ex of exponentShardList) {
|
|
for (const ex of exponentShardList) {
|
|
|
- if (ex.m_price && ex.m_price !== this.ctx.helper.round(ex.m_price, decimal.up)) {
|
|
|
|
|
- const insertEx = {
|
|
|
|
|
- id: ex.id,
|
|
|
|
|
- };
|
|
|
|
|
- insertEx.m_price = this.ctx.helper.round(ex.m_price, decimal.up);
|
|
|
|
|
- updateStageArray.push(insertEx);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const materialExponent = this._.find(exponentList, { id: ex.me_id });
|
|
|
|
|
+ if (!materialExponent) continue;
|
|
|
|
|
+ const updateEx = { id: ex.id };
|
|
|
|
|
+ if (ex.m_price !== null && ex.m_price !== undefined) updateEx.m_price = this.ctx.helper.round(ex.m_price, decimal.up);
|
|
|
|
|
+ updateEx.effective_price = this.getEffectivePrice(Object.assign({}, materialExponent, ex, updateEx), decimal.up);
|
|
|
|
|
+ if (updateEx.m_price !== ex.m_price || updateEx.effective_price !== ex.effective_price) updateStageArray.push(updateEx);
|
|
|
}
|
|
}
|
|
|
if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
|
|
if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
|
|
|
if (updateStageArray.length > 0) await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, updateStageArray);
|
|
if (updateStageArray.length > 0) await transaction.updateRows(this.ctx.service.materialExponentShard.tableName, updateStageArray);
|