# encoding:utf-8
class CashBalance < ActiveRecord::Base
  has_paper_trail
  self.table_name = "cash_balances"
  belongs_to :wx_user, :foreign_key => :wx_uid
  validates_presence_of :wx_uid, :count, :relate_id, :source
  attr_accessor :remain_sum,:get_happen_total

  #after_find :get_happen_total
  SOURCE_ENUM = [["提货券充值佣金", "balance_benefit"],
                 ["商品佣金", "product_benefit"],
                 ["店长申请", "shop_benefit"],
                 ["好友购物佣金", "fx_product_benefit"],
                 ["提现", "take_cash"],
                 ["员工订餐", "order_food"],
                 ["取消订餐", "cancel_food"],
                 ["拒绝提现", "extract_reject"],
                 ["销售奖金","sale_reward_monthly"],
                 ["商品代销金", "product_sale"],
                 ["助农项目", "project_invest"],
                 ["充值","recharge"],
                 ["预定活动","reserve_act_return"],
                 ["项目回款","receive_buyback"],
                 ["扣款","deduct"],
                 ["借款","loan"],
                 ["项目退款","project_refund"],
                 ["商品退款","product_refund"],
                 ["商品部分退款","product_part_refund"],
                 ["冲正","correct"],
                 ["项目放款","project_loan"],
                 ["货款","good_payment"],
                 ["活动退款","dollar_win_return"],
                 ["活动报名","dollar_win"],
                 ["佣金转提货券","cash_to_balance"],
                 ["佣金自动分账","cash_to_profit"],
  ]

  def remain_sum
    b = CashBalance.where("wx_uid = ? and id < ?", self.wx_uid, self.id).last
    if !b.blank?
      return self.count + b.remain_sum
    else
      return self.count
    end
  end
  # puts Rails.request.request_uri
  @total=0
  LIST =  []
  def get_happen_total
    unless LIST.include?(self.id)
      LIST.append(self.id)
    end
    # list.append(self.id)
    total =  CashBalance.where(id:LIST).sum("count")
    return total
  end

  def profit_state
    if self.trade_no.length>0
      if self.profit_flag
        return "分账已完成"
      else
        return "分账处理中"
      end
    else
      return "--"
    end
  end

  rails_admin do
    navigation_label '资金管理'
    weight -920

    list do


      items_per_page 10
      filters [:id, :source, :wx_user, :relate_id, :created_at]
      # include_all_fields
      field :id
      field :wx_user
      field :wx_uid
      field :count do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :remain_sum do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :relate_id
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :remark
      field :profit_state
      field :created_at
      # field :updated_at

=begin
      field :get_happen_total do
        formatted_value do
          value.to_f / 100.0
        end
      end
=end
    end

    show do
      field :wx_uid
      field :count do
        formatted_value do # used in form views
          value.to_f / 100.0
        end
      end
      field :remain_sum do
        formatted_value do
          value.to_f / 100.0
        end
      end
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :relate_id
      field :remark
      field :profit_state
      field :created_at
    end

    edit do
      field :wx_uid
      field :count do
        label "变动金额，单位(分)"
      end
      field :relate_id
      field :source, :enum do
        enum do
          SOURCE_ENUM
        end
      end
      field :remark
    end
  end
end
