123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 'use strict';
- /**
- * 期计量 数据模型
- *
- * @author Mai
- * @date 2018/8/13
- * @version
- */
- const auditConst = require('../const/audit').material;
- module.exports = app => {
- class MaterialBills extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'material_bills';
- }
- /**
- * 添加工料
- * @return {void}
- */
- async add() {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- const newBills = {
- tid: this.ctx.tender.id,
- mid: this.ctx.material.id,
- in_time: new Date(),
- };
- // 新增工料
- const result = await this.db.insert(this.tableName, newBills);
- if (result.affectedRows !== 1) {
- throw '新增工料数据失败';
- }
- return await this.getDataById(result.insertId);
- }
- /**
- * 删除工料
- * @param {int} id 工料id
- * @return {void}
- */
- async del(id) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- // 判断是否可删
- return await this.deleteById(id);
- }
- /**
- * 修改工料信息
- * @param {Object} data 工料内容
- * @return {void}
- */
- async save(data) {
- if (!this.ctx.tender || !this.ctx.material) {
- throw '数据错误';
- }
- delete data.in_time;
- delete data.m_tp;
- // 判断是否可修改
- return await this.db.update(this.tableName, data);
- }
- async updateNewMaterial(transaction, tid, mid) {
- const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
- let m_tp = 0;
- for (const mb of materialBillsData) {
- const one_tp = await this.calcQuantityByMB(transaction, mid, mb);
- m_tp = this.ctx.helper.add(m_tp, one_tp);
- }
- return m_tp;
- }
- /**
- * 修改quantity,m_spread值和返回单条调差金额(新增一期)
- * @param transaction
- * @param mid
- * @param mb
- * @returns {Promise<*>}
- */
- async calcQuantityByMB(transaction, mid, mb) {
- if (mb.t_type === 1) {
- 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,
- };
- await transaction.update(this.tableName, updateData);
- return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, mb.m_spread), 2);
- }
- const updateData = {
- id: mb.id,
- 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,
- };
- await transaction.update(this.tableName, updateData);
- return await this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), 2);
- }
- }
- return MaterialBills;
- };
|