module EducodeSales class MoneyPlan < ApplicationRecord belongs_to :staff belongs_to :follow_up, counter_cache: true # todo 1 :实际回款 0:计划回款 enum clazz: ['计划回款', '实际回款'] after_save :update_return_money after_destroy :update_return_money private def update_return_money if follow_up.business.last_follow_up_id == self.follow_up_id follow_up.business.update(return_money: MoneyPlan.where(clazz: '实际回款', follow_up_id: self.follow_up_id).sum(:amount)) else follow_up.update(return_money: follow_up.money_plans.where(clazz: '实际回款').sum(:amount)) end end end end