'use strict'; /** * 期计量 数据模型 * * @author Mai * @date 2018/8/13 * @version */ const auditConst = require('../const/audit').material; const materialConst = require('../const/material'); const MaterialCalculator = require('../lib/material_calc'); module.exports = app => { class MaterialMonth extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'material_month'; } /** * 返回月信息价列表 * @return {void} */ async getListByMid(mid, transaction = null) { return transaction ? await transaction.select(this.tableName, { where: { mid } }) : await this.getAllDataByCondition({ where: { mid } }); } /** * 添加月信息价 并更新 工料平均单价、本期单价,调差金额等 * @return {void} */ async add(data, monthList, mbList) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : []; material_month.push(data.yearmonth); material_month.sort(); if (mbList.length !== 0) { const insertArray = []; const updateArray = []; for (const mb of mbList) { const one_month = { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: mb.id, msg_tp: monthList.length !== 0 ? null : mb.msg_tp, yearmonth: data.yearmonth, }; 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 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, '', 0]); 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), this.ctx.material.decimal.up); const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp); const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp); updateArray.push({ id: mb.id, msg_tp: new_msg_tp, msg_spread: newmsg_spread, m_spread: newm_spread, m_tp: newTp, m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp), }); } } if (insertArray.length !== 0) await transaction.insert(this.tableName, insertArray); if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray); } await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') }); const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction); await transaction.commit(); this.ctx.material.months = material_month.join(','); return m_tp; } catch (err) { await transaction.rollback(); throw err; } } /** * 删除月信息价 并更新 工料平均单价、本期单价,调差金额等 * @return {void} */ async del(data, monthList, mbList) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : []; this._.remove(material_month, function(n) { return data.indexOf(n) !== -1; }); await transaction.delete(this.tableName, { mid: this.ctx.material.id, yearmonth: data }); if (mbList.length !== 0) { const updateArray = []; for (const mb of mbList) { this._.remove(monthList, function(m) { return data.indexOf(m.yearmonth) !== -1; }); const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'); const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]); const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null; const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp); const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp); updateArray.push({ id: mb.id, msg_tp: new_msg_tp, msg_spread: newmsg_spread, m_spread: newm_spread, m_tp: newTp, m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp), }); } if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray); } await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') }); const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction); await transaction.commit(); this.ctx.material.months = material_month.join(','); return m_tp; } catch (err) { await transaction.rollback(); throw err; } } /** * 修改月信息价值 并更新 本期单价,调差金额等 * @return {void} */ async save(data) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : []; await transaction.update(this.tableName, { msg_tp: data.value }, { where: { mb_id: data.mb_id, yearmonth: data.yearmonth, mid: this.ctx.material.id } }); const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id, mid: this.ctx.material.id } }); 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 month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]); const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null; const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp); const newTp = this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), this.ctx.material.decimal.tp); await transaction.update(this.ctx.service.materialBills.tableName, { id: mbInfo.id, msg_tp: new_msg_tp, msg_spread: newmsg_spread, m_spread: newm_spread, m_tp: newTp, m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp), }); } const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction); await transaction.commit(); return m_tp; } catch (err) { await transaction.rollback(); throw err; } } /** * 修改多个月信息价值 并更新 本期单价,调差金额等 * @return {void} */ async saveDatas(datas, mbList) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : []; const updateArray = []; for (const data of datas) { for (const m of material_month) { const one_update = { row: { msg_tp: data[m], }, where: { mb_id: data.mb_id, yearmonth: m, }, }; updateArray.push(one_update); } } await transaction.updateRows(this.tableName, updateArray); const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } }); if (mbList.length !== 0) { const mbUpdateArray = []; for (const mb of mbList) { const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'); const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]); const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), this.ctx.material.decimal.up) : null; const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp); const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), this.ctx.material.decimal.tp); mbUpdateArray.push({ id: mb.id, msg_tp: new_msg_tp, msg_spread: newmsg_spread, m_spread: newm_spread, m_tp: newTp, m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), this.ctx.material.decimal.tp), }); } if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray); } const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction); await transaction.commit(); return m_tp; } catch (err) { await transaction.rollback(); throw err; } } async getMonthList(materialBillsData, transaction = null) { // 取月信息价表 const monthsList = []; if (this.ctx.material.months) { const material_month = this.ctx.material.months.split(','); material_month.sort(); const materialMonthList = await this.getListByMid(this.ctx.material.id, transaction); for (const mbd of materialBillsData) { const one_mb = { code: mbd.code, name: mbd.name, unit: mbd.unit, origin: mbd.origin, mb_id: mbd.id, order: mbd.order, }; for (const m of material_month) { const mb_id = this.ctx.material.highOrder !== this.ctx.material.order ? mbd.mb_id : mbd.id; const one_mm = this._.find(materialMonthList, { mb_id, yearmonth: m }); one_mb[m] = one_mm.msg_tp; } monthsList.push(one_mb); } } return monthsList; } } return MaterialMonth; };