瀏覽代碼

pos优化更新

MaiXinRong 5 年之前
父節點
當前提交
8efe1f928b
共有 1 個文件被更改,包括 65 次插入2 次删除
  1. 65 2
      app/service/pos.js

+ 65 - 2
app/service/pos.js

@@ -163,11 +163,70 @@ module.exports = app => {
                     throw err;
                 }
             } else {
-                await this.db.update(this.tableName, data, {tid: tid, id: data.id});
+                await this.db.update(this.tableName, data);
                 return {pos: data};
             }
         }
 
+        async _updatePosDatas(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 _deletePosData(tid, data) {
             if (!data || data.length === 0) {
                 throw '提交数据错误';
@@ -214,7 +273,11 @@ module.exports = app => {
                 case 'add':
                     return await this._addPosData(tid, data.updateData);
                 case 'update':
-                    return await this._updatePosData(tid, data.updateData);
+                    if (data.updateData instanceof Array) {
+                        return await this._updatePosDatas(tid, data.updateData);
+                    } else {
+                        return await this._updatePosData(tid, data.updateData);
+                    }
                 case 'delete':
                     return await this._deletePosData(tid, data.updateData);
             }