Pārlūkot izejas kodu

台账分解,计量单元,批量提交数据,计算问题

MaiXinRong 5 gadi atpakaļ
vecāks
revīzija
db3d7a8c30
1 mainītis faili ar 24 papildinājumiem un 13 dzēšanām
  1. 24 13
      app/service/pos.js

+ 24 - 13
app/service/pos.js

@@ -234,6 +234,7 @@ module.exports = app => {
             const bills = await this.ctx.service.ledger.getDataById(op.lid);
             const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
             const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
+            let needUpdateBills;
             for (const d of data) {
                 if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
                     if (d.sgfh_qty !== undefined) {
@@ -252,29 +253,39 @@ module.exports = app => {
                         d.qtcl_qty = op.qtcl_qty;
                     }
                     d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
+                    needUpdateBills = true;
                 }
             }
             const updateBills = {id: bills.id};
-            for (const bp of billsPos) {
-                const newPos = data.find(function (x) { return x.id === bp.id });
-                const calcData = newPos ? newPos : bp;
-                updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
-                updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
-                updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
-                updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
+            if (needUpdateBills) {
+                for (const bp of billsPos) {
+                    const newPos = data.find(function (x) { return x.id === bp.id });
+                    updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
+                        ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
+                        : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
+                    updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
+                        ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
+                        : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
+                    updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
+                        ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
+                        : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
+                    updateBills.quantity = newPos && newPos.quantity !== undefined
+                        ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
+                        : this.ctx.helper.add(updateBills.quantity, bp.quantity);
+                }
+                const info = this.ctx.tender.info;
+                updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
+                updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
+                updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
+                updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
             }
-            const info = this.ctx.tender.info;
-            updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
-            updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
-            updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
-            updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
 
             const transaction = await this.db.beginTransaction();
             try {
                 for (const d of data) {
                     transaction.update(this.tableName, d);
                 }
-                await transaction.update(this.ctx.service.ledger.tableName, updateBills);
+                if (needUpdateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
                 await transaction.commit();
                 updateBills.ledger_id = bills.ledger_id;
                 return {