Browse Source

导入功能bug修复

laiguoran 1 year atrás
parent
commit
eaa2459de9
3 changed files with 30 additions and 18 deletions
  1. 1 0
      app/public/js/material.js
  2. 8 4
      app/service/material_bills.js
  3. 21 14
      app/service/material_stage_bills.js

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

@@ -1615,6 +1615,7 @@ $(document).ready(() => {
                         if (_.isNumber(t.msg_times)) {
                             t.msg_times = formatDate(t.msg_times, '/');
                         }
+                        t.spec = t.spec === null ? '' : t.spec;
                     }
                     console.log(tree);
                     postData(window.location.pathname + '/save', { type:'export', postData: tree, includeSpec, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {

+ 8 - 4
app/service/material_bills.js

@@ -147,14 +147,16 @@ module.exports = app => {
                 }
                 // 找出需要更新的工料
                 const updateBills = this._.intersectionWith(bills, datas, function(item1, item2) {
-                    return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec.toString() === item2.spec.toString()));
+                    return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit &&
+                        (!includeSpec || (includeSpec && (item1.spec === null ? '' : item1.spec).toString() === (item2.spec === null ? '' : item2.spec).toString()));
                 });
                 if (updateBills.length !== 0) {
                     const updateDatas = [];
                     const updateMonthList = [];
                     for (const u of updateBills) {
                         const oneData = this._.find(datas, function(item) {
-                            return item.code.toString() === u.code.toString() && item.name === u.name && item.unit === u.unit && (!includeSpec || (includeSpec && item.spec.toString() === u.spec.toString()));
+                            return item.code.toString() === u.code.toString() && item.name === u.name && item.unit === u.unit &&
+                                (!includeSpec || (includeSpec && (item.spec === null ? '' : item.spec).toString() === (u.spec === null ? '' : u.spec).toString()));
                         });
                         const upd = { id: u.id };
                         oneData.msg_tp = this.ctx.helper.round(oneData.msg_tp, this.ctx.material.decimal.up);
@@ -177,7 +179,7 @@ module.exports = app => {
                                 upd.msg_times = oneData.msg_times;
                             }
                         }
-                        if (!includeSpec && oneData.spec !== u.spec) {
+                        if (!includeSpec && (oneData.spec === null ? '' : oneData.spec).toString() !== (u.spec === null ? '' : u.spec).toString()) {
                             upd.spec = oneData.spec;
                         }
                         if (!this._.isEqual(upd, { id: u.id })) updateDatas.push(upd);
@@ -199,7 +201,9 @@ module.exports = app => {
                     resultData.updateNum = updateDatas.length;
                     if (updateMonthList.length !== 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
                     if (this.ctx.material.is_stage_self) {
-                        needUpdateTp = await this.ctx.service.materialStageBills.updateByExport(transaction, updateBills, datas, includeSpec, ms_id);
+                        let selfUpdateNum = 0;
+                        [needUpdateTp, selfUpdateNum] = await this.ctx.service.materialStageBills.updateByExport(transaction, updateBills, datas, includeSpec, ms_id);
+                        if (resultData.updateNum === 0 || resultData.updateNum < selfUpdateNum) resultData.updateNum = selfUpdateNum;
                     }
                 }
                 // 更新materialStage 值

+ 21 - 14
app/service/material_stage_bills.js

@@ -142,31 +142,25 @@ module.exports = app => {
             const materialStageBillsList = await this.getAllDataByCondition({ where: { ms_id, mb_id: this._.map(bills, 'id') } });
             const updateDatas = [];
             let needUpdateTp = false;
-            const updateBillsDatas = [];
+            const updateBillIds = [];
             for (const m of materialStageBillsList) {
                 const oneBill = this._.find(bills, { id: m.mb_id });
                 const oneData = this._.find(datas, function(item) {
-                    return item.code.toString() === oneBill.code.toString() && item.name === oneBill.name && item.unit === oneBill.unit && (!includeSpec || (includeSpec && item.spec === oneBill.spec));
+                    return item.code.toString() === oneBill.code.toString() && item.name === oneBill.name && item.unit === oneBill.unit &&
+                        (!includeSpec || (includeSpec && (item.spec === null ? '' : item.spec).toString() === (oneBill.spec === null ? '' : oneBill.spec).toString()));
                 });
                 const upd = { id: m.id };
                 if (oneData.msg_tp !== m.msg_tp) {
                     needUpdateTp = true;
                     upd.msg_tp = oneData.msg_tp;
-                    const [newmsg_spread, newm_spread] = await this.getSpread(m, oneData.msg_tp);
+                    const [newmsg_spread, newm_spread] = await this.getSpread(oneBill, oneData.msg_tp);
                     upd.msg_spread = newmsg_spread;
                     upd.m_spread = newm_spread;
                     const newTp = this.ctx.helper.round(this.ctx.helper.mul(m.quantity, newm_spread), this.ctx.material.decimal.tp);
                     upd.m_tp = newTp;
                     upd.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(oneBill.m_tax, 100))), this.ctx.material.decimal.tp);
-                    // 金额发生变化,返回重新计算本期这条工料的金额
-                    const sql = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price' +
-                        ' FROM ' + this.tableName + ' WHERE `tid` = ? AND `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
-                    const sqlParam = [this.ctx.tender.id, this.ctx.material.id, m.id];
-                    const tp = await transaction.queryOne(sql, sqlParam);
-                    updateBillsDatas.push({
-                        id: m.id,
-                        m_tp: tp.total_price,
-                        m_tax_tp: tp.tax_total_price,
+                    updateBillIds.push({
+                        id: oneBill.id,
                     });
                 }
                 if (oneData.msg_times !== m.msg_times) {
@@ -174,16 +168,29 @@ module.exports = app => {
                 }
                 if (!this._.isEqual(upd, { id: m.id })) updateDatas.push(upd);
             }
+            const updateNum = updateDatas.length;
             if (updateDatas.length !== 0) {
                 await transaction.updateRows(this.tableName, updateDatas);
                 if (needUpdateTp) {
                     // 更新到materialStage金额
                     await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
                     // 更新到materialBills金额
-                    await transaction.updateRows(this.ctx.service.materialBills.tableName, updateBillsDatas);
+                    const updateBillsDatas = [];
+                    for (const bill of updateBillIds) {
+                        const sql = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price' +
+                            ' FROM ' + this.tableName + ' WHERE `tid` = ? AND `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
+                        const sqlParam = [this.ctx.tender.id, this.ctx.material.id, bill.id];
+                        const tp = await transaction.queryOne(sql, sqlParam);
+                        updateBillsDatas.push({
+                            id: bill.id,
+                            m_tp: tp.total_price,
+                            m_tax_tp: tp.tax_total_price,
+                        });
+                    }
+                    if (updateBillsDatas.length > 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateBillsDatas);
                 }
             }
-            return needUpdateTp;
+            return [needUpdateTp, updateNum];
         }
 
         // 单个修改工料时同步修改