module JamRuby class AffiliatePayment < ActiveRecord::Base belongs_to :affiliate_monthly_payment belongs_to :affiliate_quarterly_payment def self.index(user, options) unless user.affiliate_partner return [[], nil] end affiliate_partner_id = user.affiliate_partner.id 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 = AffiliatePayment # .includes(affiliate_quarterly_payment: [], affiliate_monthly_payment:[]) # .where(affiliate_partner_id: affiliate_partner_id) # .where("(payment_type='quarterly' AND closed = true) OR payment_type='monthly'") # .where('(paid = TRUE or due_amount_in_cents < 10000 or paid is NULL)') # .paginate(:page => page, :per_page => limit) # .order('year ASC, time_sort ASC, payment_type ASC') query = AffiliatePayment .includes(:affiliate_monthly_payment => { :affiliate_partner => :affiliate_distributions }) .where(affiliate_partner_id: affiliate_partner_id) .where("payment_type='monthly'") .where('(paid = TRUE or due_amount_in_cents < 10000 or paid is NULL)') .paginate(:page => page, :per_page => limit) .order('year DESC, month DESC') if query.length == 0 [query, nil] elsif query.length < limit [query, nil] else [query, start + limit] end end end end