class JamRuby::AffiliateMonthlyPayment < ActiveRecord::Base

  belongs_to :affiliate_partner, class_name: 'JamRuby::AffiliatePartner', inverse_of: :months


  def self.index(user, options)
    unless user.affiliate_partner
      return [[], nil]
    end

    page = options[:page].to_i
    per_page = options[:per_page].to_i

    if page == 0
      page = 1
    end

    if per_page == 0
      per_page = 50
    end

    start = (page -1 ) * per_page
    limit = per_page

    query = AffiliateMonthlyPayment
                .paginate(page: page, per_page: per_page)
                .where(affiliate_partner_id: user.affiliate_partner.id)
                .order('year ASC, month ASC')

    if query.length == 0
      [query, nil]
    elsif query.length < limit
      [query, nil]
    else
      [query, start + limit]
    end
  end

end
