فهرست منبع

调差平均单价计算

laiguoran 4 سال پیش
والد
کامیت
0af56259a5
3فایلهای تغییر یافته به همراه24 افزوده شده و 5 حذف شده
  1. 11 0
      app/extend/helper.js
  2. 5 1
      app/public/js/material.js
  3. 8 4
      app/service/material_month.js

+ 11 - 0
app/extend/helper.js

@@ -1216,4 +1216,15 @@ module.exports = {
         const reg = /(.png)|(.gif)|(.txt)|(.jpg)|(.jpeg)|(.pdf)/;
         return reg.test(ext);
     },
+
+    /**
+     * 查找数组中某个字符的个数
+     * @param {Array} array 数组
+     * @param {string} val 字符串
+     * @return {Boolean} 匹配结果
+     */
+    arrayCount(array, val) {
+        const counts = (arr, value) => arr.reduce((a, v) => { return v === value ? a + 1 : a + 0; }, 0);
+        return counts(array, val);
+    },
 };

+ 5 - 1
app/public/js/material.js

@@ -573,10 +573,14 @@ $(document).ready(() => {
         getValue: {
             average_msg_tp: function (data) {
                 let msg_tp = 0;
+                let hadnum = 0;
                 for (const m of months) {
                     msg_tp += data[m];
+                    if (data[m] !== null && data[m] !== '') {
+                        hadnum++;
+                    }
                 }
-                const average_tp = ZhCalc.round(ZhCalc.div(msg_tp, months.length), 3);
+                const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), 3) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), 3);
                 return average_tp;
             },
         },

+ 8 - 4
app/service/material_month.js

@@ -59,7 +59,8 @@ module.exports = app => {
                         insertArray.push(one_month);
                         if (monthList.length !== 0) {
                             const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
-                            const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, one_month.msg_tp), material_month.length), 3);
+                            const month_num = material_month.length - this.ctx.helper.arrayCount(this._.concat(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), one_month.msg_tp), null);
+                            const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, one_month.msg_tp), month_num), 3);
                             const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
                             updateArray.push({
                                 id: mb.id,
@@ -107,7 +108,8 @@ module.exports = app => {
                                 return data.indexOf(m.yearmonth) !== -1;
                             });
                             const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
-                            const new_msg_tp = material_month.length !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3) : null;
+                            const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), null);
+                            const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
                             const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
                             updateArray.push({
                                 id: mb.id,
@@ -147,7 +149,8 @@ module.exports = app => {
                 const mbInfo = await transaction.get(this.ctx.service.materialBills.tableName, { id: data.mb_id });
                 if (monthList.length !== 0) {
                     const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
-                    const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3);
+                    const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), null);
+                    const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
                     const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
                     await transaction.update(this.ctx.service.materialBills.tableName, {
                         id: mbInfo.id,
@@ -198,7 +201,8 @@ module.exports = app => {
                     const mbUpdateArray = [];
                     for (const mb of mbList) {
                         const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
-                        const new_msg_tp = material_month.length !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3) : null;
+                        const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), null);
+                        const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
                         const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
                         mbUpdateArray.push({
                             id: mb.id,