Quellcode durchsuchen

1. 本期计量台账,台账,调用变更令
2. 本期计量台账,部位明细,调用变更令

MaiXinRong vor 6 Jahren
Ursprung
Commit
daefc9204c

+ 7 - 3
app/controller/stage_controller.js

@@ -125,6 +125,7 @@ module.exports = app => {
             const times = ctx.stage.status === auditConst.status.checkNo ? ctx.stage.times - 1 : ctx.stage.times;
             ctx.stage.auditors = await ctx.service.stageAudit.getAuditors(ctx.stage.id, times);
             ctx.stage.curAuditor = await ctx.service.stageAudit.getCurAuditor(ctx.stage.id, times);
+            ctx.stage.flowOrder = ctx.stage.curAuditor ? ctx.stage.curAuditor.order : 0;
             ctx.stage.user = await ctx.service.projectAccount.getAccountInfoById(ctx.stage.user_id);
             ctx.stage.auditHistory = [];
             if (ctx.stage.times > 1) {
@@ -600,6 +601,7 @@ module.exports = app => {
          */
         async searchValidChange(ctx) {
             try {
+                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.bills && !data.pos) {
                     throw '数据错误';
@@ -607,7 +609,8 @@ module.exports = app => {
                 const bills = data.bills ? data.bills : await ctx.service.ledger.getDataById(data.pos.lid);
                 const pos = data.pos;
                 const changes = await ctx.service.change.getValidChanges(ctx.tender.id, bills, pos);
-                ctx.body = {err: 0, msg: '', data: changes};
+                const useChanges = await ctx.service.stageChange.getLastestStageData(ctx.tender.id, ctx.stage.id, bills.id, pos ? pos.id : -1);
+                ctx.body = {err: 0, msg: '', data: {changes, useChanges}};
             } catch(err) {
                 this.log(err);
                 ctx.body = {err: 1, msg: err.toString(), data: null};
@@ -621,15 +624,16 @@ module.exports = app => {
          */
         async useChange(ctx) {
             try {
+                await this._getStage(ctx);
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.target || (!data.target.bills && !data.target.pos) || !data.change) {
                     throw '调用变更令数据错误'
                 }
                 let result;
                 if (data.target.bills) {
-                    result = await ctx.service.stageBills.useChange(data.target.bills, data.change);
+                    result = await ctx.service.stageChange.billsChange(data.target.bills, data.change);
                 } else {
-                    result = await ctx.service.stageBills.userChange(data.target.pos, data.change);
+                    result = await ctx.service.stageChange.posChange(data.target.pos, data.change);
                 }
                 ctx.body = {err: 0, msg: '', data: result};
             } catch(err) {

+ 21 - 15
app/public/js/stage.js

@@ -260,24 +260,29 @@ $(document).ready(() => {
                                 return;
                             }
                         }
+                        data.change.push({ cid: c.cid, cbid: c.cbid, qty: c.uamount });
                     }
                 }
-                self.obj.modal('hide');
                 // 提交数据到后端
-                // postData(window.location.pathname + 'use-change', data, function(result) {
-                //     if (result.pos) {
-                //         stagePos.loadCurStageData(result.pos.curStageData);
-                //     }
-                //     const nodes = stageTree.loadPostStageData(result.ledger.curStageData);
-                //     stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
-                //     stagePosSpreadObj.loadCurPosData();
-                //     self.obj.modal('hide');
-                // });
+                postData(window.location.pathname + '/use-change', data, function(result) {
+                    if (result.pos) {
+                        stagePos.loadCurStageData(result.pos);
+                    }
+                    const nodes = stageTree.loadPostStageData(result.bills);
+                    stageTreeSpreadObj.refreshTreeNodes(slSpread.getActiveSheet(), nodes);
+                    stagePosSpreadObj.loadCurPosData();
+                    self.obj.modal('hide');
+                });
             })
         }
-        _calculateValidAmount() {
+        _calculateAmount() {
             for (const c of this.changes) {
-                c.vamount = c.b_amount;
+                c.bamount = _.toNumber(c.b_amount);
+                c.vamount = _.toNumber(c.b_amount);
+                const uc = _.find(this.useChanges, {cid: c.cid, cbid: c.cbid});
+                if (uc) {
+                    c.uamount = uc.qty;
+                }
             }
         }
         _loadChangeDetail(change) {
@@ -345,7 +350,7 @@ $(document).ready(() => {
         }
         _filterEmptyChange(isFilter) {
             for (const c of this.changes) {
-                c.visible = isFilter ? (c.vamount || c.vamount === 0) : true;
+                c.visible = isFilter ? (c.vamount && !checkZero(c.vamount)) : true;
             }
             SpreadJsObj.refreshTreeRowVisible(this.spread.getActiveSheet());
         }
@@ -354,8 +359,9 @@ $(document).ready(() => {
             const self = this;
             $('#b-code-hint').text('当前变更清单:' + code);
             postData(window.location.pathname + '/valid-change', data, function (result) {
-                self.changes = result;
-                self._calculateValidAmount();
+                self.changes = result.changes;
+                self.useChanges = result.useChanges;
+                self._calculateAmount();
                 self._viewChanges();
                 self.obj.modal('show');
             });

+ 1 - 1
app/service/change.js

@@ -684,7 +684,7 @@ module.exports = app => {
             const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) + (pos ? ' And cb.`detail` = ' + this.db.escape(pos.name) : '');
             const sql = 'SELECT c.cid, c.code, c.name, c.w_code, c.p_code, c.peg, c.org_name, c.org_code, c.new_name, c.new_code,' +
                         '    c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
-                        '    cb.code As b_code, cb.name As b_name, cb.unit As b_unit, cb.samount As b_amount, cb.detail As b_detail ' +
+                        '    cb.id As cbid, cb.code As b_code, cb.name As b_name, cb.unit As b_unit, cb.samount As b_amount, cb.detail As b_detail ' +
                         '  FROM ?? As c ' +
                         '  Left Join ?? As cb ' +
                         '  On c.cid = cb.cid ' +

+ 38 - 2
app/service/stage_bills.js

@@ -151,10 +151,46 @@ module.exports = app => {
         }
 
         /**
-         *
+         * 根据
+         * @param transaction
+         * @param ledgerBills
+         * @param stageBills
+         * @param data
+         * @returns {Promise<void>}
+         * @private
+         */
+        async updateStageBillsQty(transaction, ledgerBills, stageBills, data) {
+            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
+            const updateData = {};
+            if (data.contract_qty) {
+                updateData.contract_qty = this.round(data.contract_qty, precision.value);
+                updateData.contract_tp = this.round(updateData.contract_qty * ledgerBills.unit_price);
+            }
+            if (data.qc_qty) {
+                updateData.qc_qty = this.round(data.qc_qty, precision.value);
+                updateData.qc_tp = this.round(updateData.qc_qty * ledgerBills.unit_price);
+            }
+            if (stageBills) {
+                if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
+                    (updateData.qc_qty === undefined || stageBills.qc_qty !== updateData.qc_qty)) {
+                    const curOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
+                    if (stageBills.times === this.ctx.stage.times && stageBills.order === curOrder) {
+                        updateData.id = stageBills.id;
+                        await transaction.update(this.tableName, updateData);
+                    } else {
+                        await this._insertStageBillsData(transaction, updateData, ledgerBills);
+                    }
+                }
+            } else {
+                await this._insertStageBillsData(transaction, updateData, ledgerBills);
+            }
+        };
+        /**
+         * 重算 本期计量 数量 (根据部位明细)
          * @param {Number} tid - 标段id
          * @param {Number} id - 需要计算的节点的id
-         * @param {Object} transaction - 操作所属事务,没有则创建
+         * @param {Number} lid - 台账id
+         * @param {Object} transaction - 操作所属事务
          * @returns {Promise<void>}
          */
         async calc(tid, sid, lid, transaction) {

+ 202 - 0
app/service/stage_change.js

@@ -0,0 +1,202 @@
+'use strict';
+
+/**
+ * 期 - 变更数据
+ *
+ * @author Mai
+ * @date
+ * @version
+ */
+
+const defaultPid = -1; // 非pid
+
+module.exports = app => {
+
+    class StageChange extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'stage_change';
+        }
+
+        /**
+         * 查询 调用的变更令
+         * @param {Number} tid - 标段id
+         * @param {Number} sid - 期id
+         * @param {Number} lid - 台账节点id
+         * @param {Number} pid - 部位明细id
+         * @returns {Promise<*>}
+         */
+        async getLastestStageData(tid, sid, lid, pid) {
+            const sql = 'SELECT c.* FROM ' + this.tableName + ' As c ' +
+                '  INNER JOIN ( ' +
+                '    SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid` From ' + this.tableName +
+                '      WHERE tid = ? And sid = ? And lid = ? And pid = ?' +
+                '  ) As m ' +
+                '  ON c.stimes = m.stimes And c.sorder = m.sorder And c.lid = m.lid And c.pid = m.pid';
+            const sqlParam = [tid, sid, lid, pid ? pid : -1];
+            return await this.db.query(sql, sqlParam);
+        }
+
+        /**
+         * 调用变更令
+         * @param {Object} target - 台账|部位
+         * @param {Array} changes - 调用的变更令
+         * @returns {Promise<void>}
+         */
+        async billsChange(bills, changes) {
+            const self = this;
+            function getNewChange(cid, cbid, times, order, qty) {
+                return {
+                    tid: self.ctx.tender.id,
+                    sid: self.ctx.stage.id,
+                    lid: bills.id,
+                    pid: -1,
+                    cid: cid,
+                    cbid: cbid,
+                    stimes: times,
+                    sorder: order,
+                    qty: qty
+                }
+            }
+            const ledgerBills = await this.ctx.service.ledger.getDataById(bills.id);
+            if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
+                throw '提交数据错误';
+            }
+            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
+            // 获取原变更令
+            const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1);
+            const order = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
+            // 获取更新数据
+            const updateChanges = [], newChanges = [];
+            let billsQty = 0;
+            for (const oc of oldChanges) {
+                const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
+                if (!nc || nc.qty !== oc.qty) {
+                    const qty = nc ? this.round(nc.qty, precision.value) : null;
+                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, order, qty);
+                    billsQty = this.ctx.helper.plus(billsQty, change.qty);
+                    if (oc.stimes === this.ctx.stage.times && oc.sorder === order) {
+                        change.id = oc.id;
+                        updateChanges.push(change);
+                    } else {
+                        newChanges.push(change);
+                    }
+                } else {
+                    billsQty = this.ctx.helper.plus(billsQty, oc.qty);
+                }
+            }
+            for (const c of changes) {
+                const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
+                if (!nc) {
+                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, order, this.round(c.qty, precision.value));
+                    billsQty = this.ctx.helper.plus(billsQty, change.qty);
+                    newChanges.push(change);
+                }
+            }
+            // 更新数据
+            const transaction = await this.db.beginTransaction();
+            try {
+                if (newChanges.length > 0) {
+                    await transaction.insert(this.tableName, newChanges);
+                }
+                for (const c of updateChanges) {
+                    await transaction.update(this.tableName, c);
+                }
+                const stageBills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id);
+                await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, { qc_qty: billsQty });
+                await transaction.commit();
+            } catch (err) {
+                await transaction.rollback();
+                throw err;
+            }
+            const result = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
+            return { bills: result };
+        }
+
+        async posChange(pos, changes) {
+            const self = this;
+            function getNewChange(cid, cbid, times, order, qty) {
+                return {
+                    tid: self.ctx.tender.id,
+                    sid: self.ctx.stage.id,
+                    lid: pos.lid,
+                    pid: pos.id,
+                    cid: cid,
+                    cbid: cbid,
+                    stimes: times,
+                    sorder: order,
+                    qty: qty
+                }
+            }
+            const ledgerBills = await this.ctx.service.ledger.getDataById(pos.lid);
+            if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
+                throw '提交数据错误';
+            }
+            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
+            // 获取原变更令
+            const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id);
+            const updateChanges = [], newChanges = [];
+            let posQty = 0;
+            for (const oc of oldChanges) {
+                const nc = this._.find(changes, {cid: oc.cid, cbid: oc.cbid});
+                if (!nc || nc.qty !== oc.qty) {
+                    const qty = nc ? this.round(nc.qty, precision.value) : null;
+                    const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, qty);
+                    posQty = this.ctx.helper.plus(posQty, change.qty);
+                    if (oc.stimes === this.ctx.stage.times && oc.sorder === this.ctx.stage.flowOrder) {
+                        change.id = oc.id;
+                        updateChanges.push(change);
+                    } else {
+                        newChanges.push(change);
+                    }
+                } else {
+                    posQty = this.ctx.helper.plus(posQty, oc.qty);
+                }
+            }
+            for (const c of changes) {
+                const nc = this._.find(oldChanges, {cid: c.cid, cbid: c.cbid});
+                if (!nc) {
+                    const change = getNewChange(c.cid, c.cbid, this.ctx.stage.times, this.ctx.stage.flowOrder, this.round(c.qty, precision.value));
+                    posQty = this.ctx.helper.plus(posQty, change.qty);
+                    newChanges.push(change);
+                }
+            }
+            // 更新数据
+            const transaction = await this.db.beginTransaction();
+            try {
+                if (newChanges.length > 0) {
+                    await transaction.insert(this.tableName, newChanges);
+                }
+                for (const c of updateChanges) {
+                    await transaction.update(this.tableName, c);
+                }
+                await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty);
+                await transaction.commit();
+            } catch (err) {
+                await transaction.rollback();
+                throw err;
+            }
+
+            // 获取返回数据
+            try {
+                const data = {};
+                data.bills = await this.ctx.service.stageBills.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
+                data.pos = await this.ctx.service.stagePos.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, [pos.id])
+                return data;
+            } catch(err) {
+                throw '获取数据错误,请刷新页面';
+            }
+        }
+
+    }
+
+    return StageChange;
+
+};

+ 20 - 1
app/service/stage_pos.js

@@ -162,7 +162,6 @@ module.exports = app => {
                 if (osp && osp.times === this.ctx.stage.times && osp.order === userOrder) {
                     await transaction.update(this.tableName, d, {where: {id: osp.id}});
                 } else {
-                    console.log(osp);
                     d.tid = this.ctx.tender.id;
                     d.sid = this.ctx.stage.id;
                     d.said = this.ctx.session.sessionUser.accountId;
@@ -269,6 +268,26 @@ module.exports = app => {
             }
         }
 
+        async updateChangeQuantity(transaction, pos, qty) {
+            const orgPos = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.pid);
+            const userOrder = this.ctx.stage.curAuditor ? this.ctx.stage.curAuditor.order : 0;
+            if (orgPos && orgPos.times === this.ctx.stage.times && orgPos.order === userOrder) {
+                await transaction.update(this.tableName, {qc_qty: qty}, {where: {id: osp.id}});
+            } else {
+                await transaction.insert(this.tableName, {
+                    tid: this.ctx.tender.id,
+                    sid: this.ctx.stage.id,
+                    lid: pos.lid,
+                    pid: pos.id,
+                    said: this.ctx.session.sessionUser.accountId,
+                    times: this.ctx.stage.times,
+                    order: userOrder,
+                    qc_qty: qty,
+                });
+            }
+            await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, pos.lid, transaction);
+        }
+
         /**
          * 统计清单下部位明细合计
          * @param {Number} tid - 标段id

+ 4 - 4
config/config.default.js

@@ -124,17 +124,17 @@ module.exports = appInfo => {
     config.customLogger = {
         // 操作失败日志
         fail: {
-            file: path.join(appInfo.root, 'log/fail.log'),
+            file: path.join(appInfo.root, 'logs/fail.log'),
         },
         // 以下为业务日志
         ledger: {
-            file: path.join(appInfo.root, 'log/ledger.log'),
+            file: path.join(appInfo.root, 'logs/ledger.log'),
         },
         stage: {
-            file: path.join(appInfo.root, 'log/stage.log'),
+            file: path.join(appInfo.root, 'logs/stage.log'),
         },
         mixed: {
-            file: path.join(appInfo.root, 'log/mixed.log'),
+            file: path.join(appInfo.root, 'logs/mixed.log'),
         }
     };