|
@@ -171,12 +171,38 @@ module.exports = app => {
|
|
|
return await this.db.queryOne(sql, sqlParam);
|
|
|
}
|
|
|
|
|
|
- _calcStageBillsData(data, orgData, ledgerData) {
|
|
|
+ _calcContractTpByTp(bills, preStageBills, stageBills, decimal) {
|
|
|
+ let activeQty = this.ctx.helper.add(bills.quantity, stageBills.qc_minus_qty);
|
|
|
+ let end_contract_qty = stageBills.contract_qty;
|
|
|
+ if (preStageBills) {
|
|
|
+ activeQty = this.ctx.helper.add(activeQty, preStageBills.qc_minus_qty);
|
|
|
+ end_contract_qty = this.ctx.helper.add(end_contract_qty, preStageBills.contract_qty);
|
|
|
+ }
|
|
|
+ const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), bills.total_price, decimal.tp);
|
|
|
+ return preStageBills ? this.ctx.helper.sub(end_contract_tp, preStageBills.contract_tp) : end_contract_tp;
|
|
|
+ }
|
|
|
+ async calcContractTp(info, stage, bills, stageBills) {
|
|
|
+ if (info.calc_type === 'tp') {
|
|
|
+ const preStageBills = await this.ctx.service.stageBillsFinal.getDataByCondition({ tid: stage.tid, sorder: stage.order - 1, lid: bills.id });
|
|
|
+ return this._calcContractTpByTp(bills, preStageBills, stageBills, info.decimal);
|
|
|
+ } else if (info.calc_type === 'up') {
|
|
|
+ return this.ctx.helper.mul(stageBills.contract_qty, bills.unit_price, info.decimal.tp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async _calcStageBillsData(data, orgData, ledgerData) {
|
|
|
const info = this.ctx.tender.info;
|
|
|
const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
|
|
|
+ if (data.qc_minus_qty !== undefined) {
|
|
|
+ data.qc_minus_qty = data.qc_minus_qty ? this.round(data.qc_minus_qty, precision.value) : 0;
|
|
|
+ } else if (data.contract_qty !== undefined) {
|
|
|
+ data.qc_minus_qty = orgData.qc_minus_qty;
|
|
|
+ }
|
|
|
if (data.contract_qty !== undefined) {
|
|
|
data.contract_qty = this.round(data.contract_qty, precision.value);
|
|
|
- data.contract_tp = this.ctx.helper.mul(data.contract_qty, ledgerData.unit_price, info.decimal.tp);
|
|
|
+ data.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, data);
|
|
|
+ } else if (data.qc_minus_qty !== undefined) {
|
|
|
+ data.contract_qty = orgData.contract_qty;
|
|
|
+ data.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, data);
|
|
|
}
|
|
|
if (data.qc_qty !== undefined) {
|
|
|
data.qc_qty = this.round(data.qc_qty, precision.value);
|
|
@@ -224,9 +250,12 @@ module.exports = app => {
|
|
|
}
|
|
|
const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
|
|
|
|
|
|
+ if (insertData.qc_minus_qty !== undefined) {
|
|
|
+ d.qc_minus_qty = insertData.qc_minus_qty ? this.round(insertData.qc_minus_qty, precision.value) : 0;
|
|
|
+ }
|
|
|
if (insertData.contract_qty !== undefined) {
|
|
|
d.contract_qty = this.round(insertData.contract_qty, precision.value);
|
|
|
- d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
|
|
|
+ d.contract_tp = await this.calcContractTp(info, this.ctx.stage, ledgerData, d);
|
|
|
}
|
|
|
if (insertData.contract_expr !== undefined) d.contract_expr = insertData.contract_expr;
|
|
|
if (insertData.qc_qty !== undefined) {
|
|
@@ -241,9 +270,6 @@ module.exports = app => {
|
|
|
d.negative_qc_qty = this.round(insertData.negative_qc_qty, precision.value);
|
|
|
d.negative_qc_tp = this.ctx.helper.mul(d.negative_qc_qty, ledgerData.unit_price, info.decimal.tp);
|
|
|
}
|
|
|
- if (insertData.qc_minus_qty !== undefined) {
|
|
|
- d.qc_minus_qty = insertData.qc_minus_qty ? this.round(insertData.qc_minus_qty, precision.value) : 0;
|
|
|
- }
|
|
|
if (insertData.ex_stage_qty1 !== undefined) {
|
|
|
d.ex_stage_qty1 = this.round(insertData.ex_stage_qty1, precision.value);
|
|
|
d.ex_stage_tp1 = this.ctx.helper.mul(d.ex_stage_qty1, ledgerData.unit_price, info.decimal.tp);
|
|
@@ -275,7 +301,7 @@ module.exports = app => {
|
|
|
} else {
|
|
|
d.id = stageBills.id;
|
|
|
d.said = this.ctx.session.sessionUser.accountId;
|
|
|
- this._calcStageBillsData(d, stageBills, ledgerBills);
|
|
|
+ await this._calcStageBillsData(d, stageBills, ledgerBills);
|
|
|
await transaction.update(this.tableName, d);
|
|
|
}
|
|
|
}
|
|
@@ -300,12 +326,13 @@ module.exports = app => {
|
|
|
if (stageBills) {
|
|
|
if ((data.contract_qty === undefined || stageBills.contract_qty !== data.contract_qty) ||
|
|
|
(data.qc_qty === undefined || stageBills.qc_qty !== data.qc_qty) ||
|
|
|
+ (data.qc_minus_qty === undefined || stageBills.qc_minus_qty !== data.qc_minus_qty) ||
|
|
|
(data.ex_stage_qty1 === undefined || stageBills.ex_stage_qty1 !== data.ex_stage_qty1)
|
|
|
) {
|
|
|
if (stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
|
|
|
data.id = stageBills.id;
|
|
|
data.said = this.ctx.session.sessionUser.accountId;
|
|
|
- this._calcStageBillsData(data, stageBills, ledgerBills);
|
|
|
+ await this._calcStageBillsData(data, stageBills, ledgerBills);
|
|
|
await transaction.update(this.tableName, data);
|
|
|
} else {
|
|
|
await this._insertStageBillsData(transaction, data, stageBills, ledgerBills);
|
|
@@ -323,22 +350,29 @@ module.exports = app => {
|
|
|
* @param {Object} transaction - 操作所属事务
|
|
|
* @return {Promise<void>}
|
|
|
*/
|
|
|
- async calc(tid, sid, lid, transaction) {
|
|
|
+ async calc(tid, stage, lid, transaction) {
|
|
|
const info = this.ctx.tender.info;
|
|
|
- const stageBills = await this.getLastestStageData2(tid, sid, lid);
|
|
|
+ const stageBills = await this.getLastestStageData2(tid, stage.id, lid);
|
|
|
const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(lid);
|
|
|
if (!ledgerBills) {
|
|
|
throw '提交数据错误';
|
|
|
}
|
|
|
- const posGather = await this.ctx.service.stagePos.getPosGather(tid, sid, lid, transaction);
|
|
|
+ const posGather = await this.ctx.service.stagePos.getPosGather(tid, stage.id, lid, transaction);
|
|
|
if (!posGather) { return; }
|
|
|
- posGather.qc_minus_qty = posGather.qc_minus_qty || 0;
|
|
|
+ if (posGather.qc_minus_qty !== undefined) {
|
|
|
+ posGather.qc_minus_qty = posGather.qc_minus_qty || 0;
|
|
|
+ } else if (posGather.contract_qty !== undefined) {
|
|
|
+ posGather.qc_minus_qty = stageBills.qc_minus_qty;
|
|
|
+ }
|
|
|
|
|
|
const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
|
|
|
// 计算
|
|
|
if (posGather.contract_qty !== undefined) {
|
|
|
posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
|
|
|
- posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
|
|
|
+ posGather.contract_tp = await this.calcContractTp(info, stage, ledgerBills, posGather);
|
|
|
+ } else if (posGather.qc_minus_qty) {
|
|
|
+ posGather.contract_qty = stageBills.contract_qty;
|
|
|
+ posGather.contract_tp = await this.calcContractTp(info, stage, ledgerBills, posGather);
|
|
|
}
|
|
|
if (posGather.qc_qty !== undefined) {
|
|
|
posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
|