Browse Source

台账修订,部位明细数据更新调整

MaiXinRong 5 years ago
parent
commit
b4bf4e8bdd
2 changed files with 64 additions and 1 deletions
  1. 5 1
      app/controller/revise_controller.js
  2. 59 0
      app/service/revise_pos.js

+ 5 - 1
app/controller/revise_controller.js

@@ -470,7 +470,11 @@ module.exports = app => {
                 case 'add':
                     return await this.ctx.service.revisePos.addPos(revise.tid, revise.id, data.postData);
                 case 'update':
-                    return await this.ctx.service.revisePos.updatePos(revise.tid, data.postData);
+                    if (data.postData instanceof Array) {
+                        return await this.ctx.service.revisePos.updatePosArr(revise.tid, data.postData);
+                    } else {
+                        return await this.ctx.service.revisePos.updatePos(revise.tid, data.postData);
+                    }
                 case 'delete':
                     return await this.ctx.service.revisePos.deletePos(revise.tid, data.postData);
                 case 'paste':

+ 59 - 0
app/service/revise_pos.js

@@ -177,6 +177,65 @@ module.exports = app => {
             }
         }
 
+        async updatePosArr(tid, data) {
+            if (data.length === 0) return;
+            const op = await this.getDataById(data[0].id);
+            const bills = await this.ctx.service.ledger.getDataById(op.lid);
+            const billsPos = await this.getAllDataByCondition({where: {lid: op.lid} });
+            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
+            for (const d of data) {
+                if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
+                    if (d.sgfh_qty !== undefined) {
+                        d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
+                    } else if (op) {
+                        d.sgfh_qty = op.sgfh_qty;
+                    }
+                    if (d.sjcl_qty !== undefined) {
+                        d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
+                    } else if (op) {
+                        d.sjcl_qty = op.sjcl_qty;
+                    }
+                    if (d.qtcl_qty !== undefined) {
+                        d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
+                    } else if (op) {
+                        d.qtcl_qty = op.qtcl_qty;
+                    }
+                    d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
+                }
+            }
+            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);
+            }
+            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);
+                }
+                transaction.update(this.ctx.service.ledger.tableName, updateBills);
+                await transaction.commit();
+                updateBills.ledger_id = bills.ledger_id;
+                return {
+                    ledger: { update: [updateBills] },
+                    pos: data,
+                }
+            } catch(err) {
+                await transaction.rollback();
+                throw err;
+            }
+        }
+
         async deletePos(tid, data) {
             if (!data || data.length === 0) {
                 throw '提交数据错误';