#encoding: utf-8
class TrainingPayinfo < ActiveRecord::Base
  belongs_to :training
  belongs_to :attachment
  attr_accessible :invoice_content, :invoice_no, :invoice_title, :status, :pay_type, :info, :out_trade_no

  # status
  Status_None  = 0       #未支付
  Status_Payed = 1       #已支付
  Status_Wait  = 2       #待确认

  # pay_type
  PayType_Wechat = 1 #微信支付
  PayType_alipay = 2 #支付宝支付
  PayType_bank = 3 #银行卡支付

  def pay_type_str
    pay_type==PayType_Wechat ? '微信支付' : (pay_type==PayType_alipay ? '支付宝支付' : (pay_type==PayType_bank ? '银行卡支付' : ''))
  end

  def pay_status_str
    case status
      when Status_None
        "待支付"
      when Status_Wait
        "待确认"
      when Status_Payed
        "已支付"
    end
  end

  def pay_time
    WechatPay.where(:out_trade_no => self.out_trade_no).first.try(:created_at)
  end

  def wechat_wait_pay?
    pay_type.to_i == PayType_Wechat && status != Status_Payed
  end


  def payed?
    status == Status_Payed
  end

  def attachment_url
    if attachment_id.to_i >0
      "/attachments/download/#{attachment.id}/#{attachment.filename}"
    end
  end

  def self.total_num
    sum('num') + 15
  end


end