module EducodeSales class Business < ApplicationRecord validates_uniqueness_of :number, allow_nil: true belongs_to :clazz, class_name: 'Common' belongs_to :staff belongs_to :department belongs_to :last_follow_up, class_name: 'FollowUp', optional: true # 允许last_follow_up_id字段中的数为nil belongs_to :p_staff, class_name: 'Staff', optional: true belongs_to :p_sale_staff, class_name: 'Staff', optional: true belongs_to :p_deleter, class_name: 'Staff', optional: true has_many :sale_plans has_many :follow_ups has_many :business_clazz_changes has_many :business_levels, dependent: :destroy has_many :business_watches, dependent: :destroy has_many :business_histories, dependent: :destroy has_many :money_plans has_one :invoice_apply has_many :invoice_details has_many :sales_details belongs_to :school belongs_to :sourcable, :polymorphic => true, optional: true has_many :assign_staffs, as: :sourcable, dependent: :destroy # 售后人员 enum source_way: ['会议活动', '公司资源', '400电话', '商务邮箱', '渠道代理', '合作伙伴', '招标信息', '客户', '朋友'] # 关联关注 has_many :users, :class_name => 'EducodeSales::BusinessRelationShip', foreign_key: 'business_id', :dependent => :destroy # 每次查询时 默认的查询条件 default_scope -> { where(deleted_at: nil) } enum p_stage: %w[合同阶段 验收阶段 回款阶段] enum p_difficulty: %w[困难 适中 容易] enum p_special: %w[是 否] enum p_status: %w[未完成 已完成] def soft_destroy(user_id) self.update(deleted_at: Time.now, state_id: 2) self.sale_plans.each do |d| d.soft_destroy(user_id) end self.follow_ups.update_all(deleted_at: Time.now) EducodeSales::Recycle.create(source: self, deleter_id: user_id) end def p_soft_destroy(staff_id) self.update(p_deleted_at: Time.now, p_deleter_id: staff_id) end def p_recycle self.update(p_deleted_at: nil, p_deleter_id: nil) end def self.include_types(type) { '1' => %w[a_class b_class c_class d_class e_class], 'a' => ['a_class'], 'b' => ['b_class'], }[type] end def self.include_steps(type) { '已中标' => ['已中标','已签单','已验收','回款中', '服务中','已结束'], '已签单' => ['已签单','已验收','回款中', '服务中','已结束'], '应收款' => ['已中标','已签单','已验收','回款中', '服务中','已结束'] }[type] end end end