|  | @@ -86,13 +86,11 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  |          async getTenderInfoEx(tenderId) {
 | 
	
		
			
				|  |  |              const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
 | 
	
		
			
				|  |  | -            // console.log('getTenderInfoEx: ' + tenderId);
 | 
	
		
			
				|  |  |              const sqlParam = [tenderId];
 | 
	
		
			
				|  |  |              const list = await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  |              const info = list[0];
 | 
	
		
			
				|  |  |              const len = info.deal_info.length;
 | 
	
		
			
				|  |  |              info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
 | 
	
		
			
				|  |  | -            // console.log(info);
 | 
	
		
			
				|  |  |              for (const pi of parseInfo) {
 | 
	
		
			
				|  |  |                  info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
 | 
	
		
			
				|  |  |                  this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
 | 
	
	
		
			
				|  | @@ -200,12 +198,9 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        async saveDecimal(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | -            const changeBills = [],
 | 
	
		
			
				|  |  | -                changeAdvanceBills = [],
 | 
	
		
			
				|  |  | -                calcUp = newDecimal.up < oldDecimal.up,
 | 
	
		
			
				|  |  | -                calcTp = newDecimal.tp !== oldDecimal.tp,
 | 
	
		
			
				|  |  | -                caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
 | 
	
		
			
				|  |  | +        async _reCalcLedger(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | +            const changeBills = [];
 | 
	
		
			
				|  |  | +            const calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
 | 
	
		
			
				|  |  |              if (calcUp || calcTp) {
 | 
	
		
			
				|  |  |                  const bills = await this.ctx.service.ledger.getAllDataByCondition({
 | 
	
		
			
				|  |  |                      columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
 | 
	
	
		
			
				|  | @@ -222,6 +217,54 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                      changeBills.push(cb);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +            return changeBills;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        async _reCalcStageExtra(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | +            let changeSj = [], changeSb = [], changeSo = [];
 | 
	
		
			
				|  |  | +            const stage = await this.ctx.service.stage.getLastestStage(tenderId, true);
 | 
	
		
			
				|  |  | +            if (!stage) return [changeSj, changeSb, changeSo];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            const upDecimal = newDecimal.up, tpDecimal = newDecimal.extra ? newDecimal.extraTp : newDecimal.tp;
 | 
	
		
			
				|  |  | +            const calcUp = upDecimal < oldDecimal.up,
 | 
	
		
			
				|  |  | +                calcTp = tpDecimal < (oldDecimal.extra ? oldDecimal.extraTp : oldDecimal.tp);
 | 
	
		
			
				|  |  | +            console.log(calcUp, calcTp);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (calcUp || calcTp) {
 | 
	
		
			
				|  |  | +                const stageJgcl = await this.ctx.service.stageJgcl.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                    columns: ['id', 'unit_price', 'arrive_qty', 'deduct_qty'],
 | 
	
		
			
				|  |  | +                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                for (const sj of stageJgcl) {
 | 
	
		
			
				|  |  | +                    const cj = { id: sj.id };
 | 
	
		
			
				|  |  | +                    cj.unit_price = calcUp ? this.ctx.helper.round(sj.unit_price, upDecimal) : sj.unit_price;
 | 
	
		
			
				|  |  | +                    cj.arrive_tp = this.ctx.helper.mul(sj.arrive_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | +                    cj.deduct_tp = this.ctx.helper.mul(sj.deduct_qty, sj.unit_price, tpDecimal);
 | 
	
		
			
				|  |  | +                    changeSj.push(cj);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (calcTp) {
 | 
	
		
			
				|  |  | +                changeSb = await this.ctx.service.stageBonus.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                    columns: ['id', 'tp'],
 | 
	
		
			
				|  |  | +                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                for (const cb of changeSb) {
 | 
	
		
			
				|  |  | +                    cb.tp = this.ctx.helper.round(cb.tp, tpDecimal);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                changeSo = await this.ctx.service.stageOther.getAllDataByCondition({
 | 
	
		
			
				|  |  | +                    columns: ['id', 'total_price', 'tp'],
 | 
	
		
			
				|  |  | +                    where: { sid: stage.id }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                for (const co of changeSo) {
 | 
	
		
			
				|  |  | +                    if (stage.order === 1) co.total_price = this.ctx.helper.round(co.total_price, tpDecimal);
 | 
	
		
			
				|  |  | +                    co.tp = this.ctx.helper.round(co.tp, tpDecimal);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return [changeSj, changeSb, changeSo];
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        async saveDecimal(tenderId, newDecimal, oldDecimal) {
 | 
	
		
			
				|  |  | +            const changeAdvanceBills = [];
 | 
	
		
			
				|  |  | +            const caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
 | 
	
		
			
				|  |  |              if (caclPayTp) {
 | 
	
		
			
				|  |  |                  // 获取预付款需要修改的相关记录
 | 
	
		
			
				|  |  |                  const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
 | 
	
	
		
			
				|  | @@ -238,14 +281,21 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                      changeAdvanceBills.push(cb);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            if (changeBills.length > 0) {
 | 
	
		
			
				|  |  | +            const changeBills = await this._reCalcLedger(tenderId, newDecimal, oldDecimal);
 | 
	
		
			
				|  |  | +            const [changeSj, changeSb, changeSo] = await this._reCalcStageExtra(tenderId, newDecimal, oldDecimal);
 | 
	
		
			
				|  |  | +            if (changeBills.length > 0 ||
 | 
	
		
			
				|  |  | +                changeAdvanceBills.length > 0 ||
 | 
	
		
			
				|  |  | +                changeSj.length > 0 || changeSb.length > 0 || changeSo.length > 0) {
 | 
	
		
			
				|  |  |                  const transaction = await this.db.beginTransaction();
 | 
	
		
			
				|  |  |                  try {
 | 
	
		
			
				|  |  |                      await transaction.update(this.tableName,
 | 
	
		
			
				|  |  |                          { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
 | 
	
		
			
				|  |  | -                    await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
 | 
	
		
			
				|  |  | +                    if (changeBills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
 | 
	
		
			
				|  |  | +                    if (changeSj.length > 0) await transaction.updateRows(this.ctx.service.stageJgcl.tableName, changeSj);
 | 
	
		
			
				|  |  | +                    if (changeSb.length > 0) await transaction.updateRows(this.ctx.service.stageBonus.tableName, changeSb);
 | 
	
		
			
				|  |  | +                    if (changeSo.length > 0) await transaction.updateRows(this.ctx.service.stageOther.tableName, changeSo);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                    // await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
 | 
	
		
			
				|  |  | +                    if (changeAdvanceBills.length) await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
 | 
	
		
			
				|  |  |                      await transaction.commit();
 | 
	
		
			
				|  |  |                  } catch (error) {
 | 
	
		
			
				|  |  |                      await transaction.rollback();
 | 
	
	
		
			
				|  | @@ -255,10 +305,6 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  await this.db.update(this.tableName,
 | 
	
		
			
				|  |  |                      { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            // 更新预付款记录
 | 
	
		
			
				|  |  | -            if (changeAdvanceBills.length) {
 | 
	
		
			
				|  |  | -                await this.db.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |