Selaa lähdekoodia

计量台账,合价计算相关

MaiXinRong 1 kuukausi sitten
vanhempi
commit
128b8d6311
4 muutettua tiedostoa jossa 85 lisäystä ja 30 poistoa
  1. 8 6
      app/controller/tender_controller.js
  2. 22 3
      app/lib/sum_load.js
  3. 47 13
      app/service/stage_bills.js
  4. 8 8
      app/service/stage_pos.js

+ 8 - 6
app/controller/tender_controller.js

@@ -710,12 +710,14 @@ module.exports = app => {
                 if (!data || (!data.ledger_check && !data.fun_rela && !data.s_type && !data.calc_type && !data.over_range_check)) throw '提交数据错误';
                 if (!ctx.session.sessionUser.is_admin) throw '您无权修改该内容';
 
-                const updateData = {};
-                if (data.ledger_check) updateData.ledger_check = data.ledger_check;
-                if (data.fun_rela) updateData.fun_rela = data.fun_rela;
-                if (data.over_range_check) updateData.over_range_check = data.over_range_check;
-                if (data.s_type) updateData.s_type = data.s_type;
-                await ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, updateData);
+                if (data.ledger_check || data.fun_rela || data.s_type || data.over_range_check) {
+                    const updateData = {};
+                    if (data.ledger_check) updateData.ledger_check = data.ledger_check;
+                    if (data.fun_rela) updateData.fun_rela = data.fun_rela;
+                    if (data.over_range_check) updateData.over_range_check = data.over_range_check;
+                    if (data.s_type) updateData.s_type = data.s_type;
+                    await ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, updateData);
+                }
                 if (data.calc_type !== undefined) await ctx.service.tenderInfo.saveCalcType(ctx.tender.id, data.calc_type);
 
                 ctx.body = { err: 0, msg: '', data: JSON.parse(ctx.request.body.data) };

+ 22 - 3
app/lib/sum_load.js

@@ -384,6 +384,9 @@ class gatherStageGclTree extends loadGclBaseTree {
                 qc_tp: 0,
                 is_tp: d.is_tp,
                 hasPos: relaPos.length > 0,
+                pre_contract_qty: d.pre_contract_qty || 0,
+                pre_contract_tp: d.pre_contract_tp || 0,
+                pre_qc_minus_qty: d.pre_qc_minus_qty || 0,
             };
             parent.children.push(baseNode);
             Index[baseNode.ledger_id] = baseNode;
@@ -399,6 +402,18 @@ class gatherStageGclTree extends loadGclBaseTree {
             node.change_detail.push(cd);
         }
     }
+    calcContractTp(info, node) {
+        if (info.calc_type === 'tp') {
+            let activeQty = this.ctx.helper.add(node.quantity, node.qc_minus_qty);
+            let end_contract_qty = node.contract_qty;
+            activeQty = this.ctx.helper.add(activeQty, node.pre_qc_minus_qty);
+            end_contract_qty = this.ctx.helper.add(end_contract_qty, node.pre_contract_qty);
+            const end_contract_tp = this.ctx.helper.mul(this.ctx.helper.div(end_contract_qty, activeQty), node.total_price, decimal.tp);
+            return this.ctx.helper.sub(end_contract_tp, node.pre_contract_tp);
+        } else if (info.calc_type === 'up') {
+            return this.ctx.helper.mul(node.unit_price, node.contract_qty, info.decimal.tp);
+        }
+    }
     gather(source, parent) {
         parent = parent ? parent : this.parent;
         const checkFun = function (node, source) {
@@ -410,9 +425,6 @@ class gatherStageGclTree extends loadGclBaseTree {
         if (node.is_tp) {
             node.contract_tp = this.ctx.helper.add(node.contract_tp, source.contract_tp);
         } else {
-            node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
-            node.contract_tp = this.ctx.helper.mul(node.unit_price, node.contract_qty, this.ctx.tender.info.decimal.tp);
-
             if (this.loadChange) {
                 node.qc_qty = this.ctx.helper.add(node.qc_qty, source.qc_qty);
                 node.qc_tp = this.ctx.helper.mul(node.unit_price, node.qc_qty, this.ctx.tender.info.decimal.tp);
@@ -421,7 +433,12 @@ class gatherStageGclTree extends loadGclBaseTree {
                 node.positive_qc_tp = this.ctx.helper.mul(node.unit_price, node.positive_qc_qty, this.ctx.tender.info.decimal.tp);
                 node.negative_qc_qty = this.ctx.helper.add(node.negative_qc_qty, source.negative_qc_qty);
                 node.negative_qc_tp = this.ctx.helper.mul(node.unit_price, node.negative_qc_qty, this.ctx.tender.info.decimal.tp);
+            } else {
+                node.qc_minus_qty = node.org_qc_minus_qty;
             }
+            node.contract_qty = this.ctx.helper.add(node.contract_qty, source.contract_qty);
+            node.contract_tp = this.calcContractTp(this.ctx.tender.info, node);
+
         }
         if (this.loadChange) this._gatherChange(node, source);
         return node;
@@ -595,11 +612,13 @@ class sumLoad {
         const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
         const importLid = await this.ctx.service.stageImportChange.getLeafXmjImportLid(this.ctx.stage.id, select.id);
         const settleStatusBills = this.ctx.stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: this.ctx.stage.readySettle.id }}) : [];
+        const preStageBills = await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender.data, this.ctx.stage.preCheckedStage.order);
         this.ctx.helper.assignRelaData(posterity, [
             { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
             { data: importLid, fields: ['is_import'], prefix: '', relaId: 'lid' },
             { data: stageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'positive_qc_qty', 'positive_qc_tp', 'negative_qc_qty', 'negative_qc_tp'], prefix: '', relaId: 'lid' },
             { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
+            { data: preStageBills, fields: ['contract_qty', 'contract_tp', 'qc_minus_qty'], prefix: 'pre_', relaId: 'lid'},
         ]);
         const pos = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
         const settleStatusPos = this.ctx.stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: this.ctx.stage.readySettle.id }}) : [];

+ 47 - 13
app/service/stage_bills.js

@@ -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);

+ 8 - 8
app/service/stage_pos.js

@@ -190,7 +190,7 @@ module.exports = app => {
                 if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
 
                 for (const lid of calcStageBills) {
-                    await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
+                    await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, lid, transaction);
                 }
                 await transaction.commit();
                 return result;
@@ -332,7 +332,7 @@ module.exports = app => {
                 if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
                 if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
                 if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
-                if (bills) await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, bills.id, transaction);
+                if (bills) await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, bills.id, transaction);
                 await transaction.commit();
                 return result;
             } catch (err) {
@@ -414,13 +414,14 @@ module.exports = app => {
                 }
                 if (stageBills && stageBills.times === this.ctx.stage.curTimes && stageBills.order === this.ctx.stage.curOrder) {
                     if (contract_qty === stageBills.contract_qty) continue;
+                    const contract_tp = await this.ctx.service.stageBills.calcContractTp(this.ctx.tender.info, this.ctx.stage, b, { contract_qty, qc_minus_qty: stageBills.qc_minus_qty });
                     updateBillsStage.push({
                         id: stageBills.id,
                         said: this.ctx.session.sessionUser.accountId,
-                        contract_qty: contract_qty,
-                        contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
+                        contract_qty: contract_qty, contract_tp,
                     });
                 } else {
+                    const contract_tp = await this.ctx.service.stageBills.calcContractTp(this.ctx.tender.info, this.ctx.stage, b, { contract_qty, qc_minus_qty: 0 });
                     insertBillsStage.push({
                         tid: this.ctx.tender.id,
                         lid: b.id,
@@ -428,8 +429,7 @@ module.exports = app => {
                         times: this.ctx.stage.curTimes,
                         order: this.ctx.stage.curOrder,
                         said: this.ctx.session.sessionUser.accountId,
-                        contract_qty: contract_qty,
-                        contract_tp: this.ctx.helper.mul(contract_qty, b.unit_price, info.decimal.tp),
+                        contract_qty: contract_qty, contract_tp
                     })
                 }
             }
@@ -478,7 +478,7 @@ module.exports = app => {
                 await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id, id: data});
                 // 删除部位明细计量数据
                 await transaction.delete(this.tableName, {tid: this.ctx.tender.id, lid: data});
-                await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos[0].lid, transaction);
+                await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, pos[0].lid, transaction);
                 await transaction.commit();
                 // 获取需要更新的数据
                 result.ledger = [pos[0].lid];
@@ -569,7 +569,7 @@ module.exports = app => {
                     ex_stage_qty1: orgPos ? orgPos.ex_stage_qty1 || 0 : 0,
                 });
             }
-            await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
+            await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage, pos.lid, transaction);
         }
 
         /**