class EconomicStatic < ActiveRecord::Base
    has_paper_trail
    self.table_name = "economic_statics"
    validates :begin_time, presence: true
    before_save :before_save

    def before_save
      balanceCount = Balance.where("created_at <= ?", self.begin_time).sum(:count)
      cashBalanceCount = CashBalance.where("created_at <= ?", self.begin_time).sum(:count)
      centCount = CentBalance.where("created_at <= ?", self.begin_time).sum(:count)

      self.balance = balanceCount
      self.cash_balance = cashBalanceCount
      self.cent = centCount

    end

    rails_admin do
      navigation_label '资金管理'
      weight -500
        list do
          filters [:id,:begin_time,:balance,:cash_balance,:cent]
          field :id
          field :begin_time
          field :balance do
            label "提货券（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cash_balance do
            label "佣金（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cent do
            label "积分（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :remark
          field :created_at
        end

        show do
          field :id
          field :begin_time
          field :balance do
            label "提货券（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cash_balance do
            label "佣金（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :cent do
            label "积分（元）"
            formatted_value do # used in form views
              value.to_f / 100
            end
          end
          field :remark
          field :created_at
          field :updated_at
        end

        edit do
          field :begin_time
          field :remark
        end
    end
end