Browse Source

调差bug和清空本期单价和时间

laiguoran 5 years ago
parent
commit
c8feec5c3a
3 changed files with 30 additions and 6 deletions
  1. 1 0
      app/service/material_audit.js
  2. 27 5
      app/service/material_bills.js
  3. 2 1
      app/service/material_list.js

+ 1 - 0
app/service/material_audit.js

@@ -213,6 +213,7 @@ module.exports = app => {
                         m_up_risk: mb.m_up_risk,
                         m_down_risk: mb.m_down_risk,
                         m_spread: mb.m_spread,
+                        m_tp: mb.m_tp,
                         pre_tp: mb.pre_tp,
                     };
                     mbhList.push(newMbh);

+ 27 - 5
app/service/material_bills.js

@@ -155,32 +155,54 @@ module.exports = app => {
          * @returns {Promise<*>}
          */
         async calcQuantityByMB(transaction, mid, mb, materialCalculator) {
+            const [newmsg_spread, newm_spread] = await this.getSpread(mb);
             if (mb.t_type === materialConst.t_type[0].value) {
                 const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
                 const sqlParam = [mid, mb.id];
                 const mb_quantity = await transaction.queryOne(sql, sqlParam);
                 console.log(mb_quantity);
-                // 取历史期记录获取截止上期调差金额
+                // 取历史期记录获取截止上期调差金额,并清空本期单价和时间,重新计算价差和有效价差
                 const updateData = {
                     id: mb.id,
                     quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
-                    pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
+                    msg_tp: null,
+                    msg_times: null,
+                    msg_spread: newmsg_spread,
+                    m_spread: newm_spread,
+                    pre_tp: mb.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, mb.m_tp), 2) : mb.pre_tp,
                 };
                 await transaction.update(this.tableName, updateData);
-                return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, mb.m_spread), 2);
+                return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, newm_spread), 2);
             } else if (mb.t_type === materialConst.t_type[1].value) {
                 const quantity = await materialCalculator.calculateExpr(mb.expr);
                 const updateData = {
                     id: mb.id,
                     quantity: quantity !== 0 && quantity !== null ? this.ctx.helper.round(quantity, 3) : null,
-                    pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
+                    msg_tp: null,
+                    msg_times: null,
+                    msg_spread: newmsg_spread,
+                    m_spread: newm_spread,
+                    pre_tp: mb.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, mb.m_tp), 2) : mb.pre_tp,
                 };
                 await transaction.update(this.tableName, updateData);
-                return await this.ctx.helper.round(this.ctx.helper.mul(quantity, mb.m_spread), 2);
+                return await this.ctx.helper.round(this.ctx.helper.mul(quantity, newm_spread), 2);
             }
         }
 
         /**
+         * 清空本期信息价后更新价差和有效价差
+         * @param data
+         * @returns {Promise<void>}
+         */
+        async getSpread(data) {
+            data.msg_tp = null;
+            const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, data.basic_price), 3);
+            const cor = msg_spread >= 0 ? this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_up_risk, 100)) : this.ctx.helper.mul(data.basic_price, this.ctx.helper.div(data.m_down_risk, 100));
+            const m_spread = Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? this.ctx.helper.round(this.ctx.helper.sub(msg_spread, cor), 3) : this.ctx.helper.round(this.ctx.helper.add(msg_spread, cor), 3)) : 0;
+            return [msg_spread, m_spread];
+        }
+
+        /**
          * 修改 expr和quantity值,返回本期金额和单条数据
          * @param data
          * @returns {Promise<void>}

+ 2 - 1
app/service/material_list.js

@@ -189,10 +189,11 @@ module.exports = app => {
             const updateData = {
                 id: mb_id,
                 quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
+                m_tp: this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(mb_quantity.quantity, 3), mbInfo.m_spread), 2),
             };
             await transaction.update(this.ctx.service.materialBills.tableName, updateData);
             // 计算本期总金额
-            const sql2 = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
+            const sql2 = 'SELECT SUM(`m_tp`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
             const sqlParam2 = [this.ctx.tender.id];
             const tp = await transaction.queryOne(sql2, sqlParam2);
             console.log(tp);