module EducodeSales class MoneyPlan < ApplicationRecord belongs_to :staff belongs_to :business belongs_to :follow_up, counter_cache: true has_many :money_plan_claims # todo 1 :实际回款 0:计划回款 enum clazz: ['计划回款', '实际回款'] enum category: ['预收款', '交付款', '验收款', '质保金'] after_save :update_return_money after_destroy :update_return_money def return_period if self.date_at && self.business&.last_follow_up&.signed_date signed_date = self.business&.last_follow_up&.signed_date month = (self.date_at.year - signed_date.year) * 12 + self.date_at.month - signed_date.month - (self.date_at.day >= signed_date.day ? 0 : 1) if month <= 3 '3个月内' elsif month <= 6 '4-6个月内' elsif month <= 9 '7-9个月' elsif month <= 12 '10-12个月' elsif month <= 24 '1-2年' elsif month <= 36 '2-3年' else '3年以上' end else '' end end 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