Browse Source

材料调差新建期数据 1

laiguoran 5 years ago
parent
commit
0f867eaf62
4 changed files with 99 additions and 7 deletions
  1. 9 7
      app/service/material.js
  2. 35 0
      app/service/material_list.js
  3. 27 0
      app/service/stage_bills.js
  4. 28 0
      app/service/stage_pos.js

+ 9 - 7
app/service/material.js

@@ -127,8 +127,7 @@ module.exports = app => {
             try {
                 if (preMaterial) {
                     newMaterial.rate = preMaterial.rate;
-                    newMaterial.pre_tp = preMaterial.m_tp + preMaterial.pre_tp;
-                    // 计算新一期本期金额
+                    newMaterial.pre_tp = this.ctx.helper.add(preMaterial.m_tp, preMaterial.pre_tp);
                 }
                 // 新增期记录
                 const result = await transaction.insert(this.tableName, newMaterial);
@@ -137,17 +136,20 @@ module.exports = app => {
                 } else {
                     throw '新增期数据失败';
                 }
-                // 存在上一期时,复制上一期审批流程、不参与调差的清单
+                // 存在上一期时,复制上一期审批流程、不参与调差的清单、上期清单并算本期有效价差,本期应耗数量,并算本期总金额
                 if (preMaterial) {
                     const auditResult = await this.ctx.service.materialAudit.copyPreMaterialAuditors(transaction, preMaterial, newMaterial);
                     if (!auditResult) {
                         throw '复制上一期审批流程失败';
                     }
+                    // 复制不参与调差清单
                     const preNotJoinList = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
-                    const materialResult = await this.ctx.service.materialListNotjoin.copyNewStageNotJoinList(transaction, preNotJoinList, newMaterial.id);
-                    if (materialResult.affectedRows === 0) {
-                        throw '新增不参与调差清单数据失败';
-                    }
+                    await this.ctx.service.materialListNotjoin.copyNewStageNotJoinList(transaction, preNotJoinList, newMaterial.id);
+                    // 复制调差清单工料关联表
+                    await this.ctx.service.materialList.copyPreMaterialList(transaction, preMaterial, newMaterial);
+                    // 修改本期应耗数量值和有效价差,需要剔除不参与调差的清单数据
+                    await this.ctx.service.materialBills.updateNewMaterial(transaction, preNotJoinList);
+                    // 计算得出本期总金额
                 }
 
                 await transaction.commit();

+ 35 - 0
app/service/material_list.js

@@ -192,6 +192,41 @@ module.exports = app => {
             const sqlParam = [tid, mid];
             return await this.db.query(sql, sqlParam);
         }
+
+        /**
+         * 复制上一期并生成新一期清单工料关联,计算新一期小计值
+         * @param transaction
+         * @param preMaterial
+         * @param newMid
+         * @returns {Promise<void>}
+         */
+        async copyPreMaterialList(transaction, preMaterial, newMaterial) {
+            const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
+            const copyMLArray = [];
+            for (const ml of materialListData) {
+                // 获取小计值
+                let gather_qty = null;
+                if (ml.mx_id !== null) {
+                    gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
+                } else {
+                    gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
+                }
+                const newMaterialList = {
+                    tid: ml.tid,
+                    order: ml.order,
+                    mid: newMaterial.id,
+                    mb_id: ml.mb_id,
+                    gcl_id: ml.gcl_id,
+                    xmj_id: ml.xmj_id,
+                    mx_id: ml.mx_id,
+                    gather_qty,
+                    quantity: ml.quantity,
+                    in_time: new Date(),
+                };
+                copyMLArray.push(newMaterialList);
+            }
+            return await transaction.insert(this.tableName, copyMLArray);
+        }
     }
     return MaterialList;
 };

+ 27 - 0
app/service/stage_bills.js

@@ -393,6 +393,33 @@ module.exports = app => {
             const result = await this.db.query(sql, sqlParam);
             return result;
         }
+
+        /**
+         * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
+         * @param {Number} tid - 标段id
+         * @param {String} stage_id_list - 期id列表
+         * @param {String} lid - 台账id
+         * @param {String} xid - 项目节id
+         * @returns {Promise<void>}
+         */
+        async getGatherQtyByMaterial(tid, stage_id_list, lid) {
+            stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
+            let gather_qty = 0;
+            for (const sid of stage_id_list) {
+                const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills ' +
+                    '  INNER JOIN ( ' +
+                    '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `progress`, `lid`, `sid` From ' + this.tableName +
+                    '      WHERE tid = ? And sid = ?' +
+                    '      GROUP BY `lid`' +
+                    '  ) As MaxFilter ' +
+                    '  ON (Bills.times * ' + timesLen + ' + `order`) = MaxFilter.progress And Bills.lid = MaxFilter.lid And Bills.`sid` = MaxFilter.`sid`' +
+                    ' WHERE Bills.lid = ?';
+                const sqlParam = [tid, sid, lid];
+                const result = await this.db.queryOne(sql, sqlParam);
+                gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
+            }
+            return gather_qty !== 0 ? gather_qty : null;
+        }
     }
 
     return StageBills;

+ 28 - 0
app/service/stage_pos.js

@@ -481,6 +481,34 @@ module.exports = app => {
             const result = await this.db.query(sql, sqlParam);
             return result;
         }
+
+        /**
+         * 获取多期(合同和数量变更相加)计量-小计(材料调差调用)
+         * @param {Number} tid - 标段id
+         * @param {String} stage_id_list - 期id列表
+         * @param {String} lid - 台账id
+         * @param {String} pid - 部位id
+         * @returns {Promise<void>}
+         */
+        async getGatherQtyByMaterial(tid, stage_id_list, lid, pid) {
+            stage_id_list = stage_id_list !== null ? stage_id_list.split(',') : [];
+            let gather_qty = 0;
+            for (const sid of stage_id_list) {
+                const sql = 'SELECT Pos.contract_qty, Pos.qc_qty FROM ' +
+                    '  (SELECT * FROM ' + this.tableName + ' WHERE tid = ? AND sid = ?) As Pos ' +
+                    '  INNER JOIN ( ' +
+                    '    SELECT MAX(`times` * ' + timesLen + ' + `order`) As `flow`, `tid`, `sid`, `pid` From ' + this.tableName +
+                    '      WHERE `tid` = ? AND sid = ?' +
+                    '      GROUP BY `pid`' +
+                    '  ) As MaxFilter ' +
+                    '  ON (Pos.times * ' + timesLen + ' + Pos.order) = MaxFilter.flow And Pos.pid = MaxFilter.pid And Pos.sid = MaxFilter.sid' +
+                    ' WHERE Pos.lid = ? AND Pos.pid = ?';
+                const sqlParam = [tid, sid, tid, sid, lid, pid];
+                const result = await this.db.queryOne(sql, sqlParam);
+                gather_qty = this.ctx.helper.add(gather_qty, this.ctx.helper.add(result.contract_qty, result.qc_qty));
+            }
+            return gather_qty !== 0 ? gather_qty : null;
+        }
     }
 
     return StagePos;