访问表中循环外部的变量到总列数

时间:2018-05-16 20:21:42

标签: ruby ruby-on-rails-4

我有一个表格,我通过affiliate_sales循环,它给了我大部分我需要的数据,除了计算总收入"总收入"柱。

<thead>
        <tr>
            <td><strong>Model Name</strong></td>
            <td><strong>Subscriber's Email</strong></td>
            <td><strong>Subscription Amount</strong></td>
            <td><strong>Your Share</strong></td>
            <td><strong>Lifetime *</strong></td>
            <td><strong>Date Started</strong></td>
            <td><strong>Date Canceled</strong></td>
            <td><strong>Payments</strong></td>
            <td><strong>Total Earned</strong></td>
        </tr>
    </thead>
    <tbody>
          <% @affiliate_sales.each do |sales| %>
          <!-- figure out number of payments made -->
            <% if sales.active? && sales.lifetime.blank? %>
                <% active_payments = ((Date.today.year * 12 + Date.today.month) - (sales.created_at.year * 12 + sales.created_at.month) + 1) %>
            <% end %>
            <% if sales.active === false  && sales.lifetime.blank? %>
                <% canceled_payments = ((sales.end_date.year * 12 + sales.end_date.month) - (sales.created_at.year * 12 + sales.created_at.month)) %>
            <% end %>
            <!-- end payments calc -->
            <% if active_payments.present? %>
                    <% total_active_payments = (sales.cost * 0.25) * (active_payments) %>
            <% end %>
            <% if canceled_payments.present? %>
                <% total_canceled_payments = (sales.cost * 0.25) * (canceled_payments) %>
            <% end %>
            <tr>
                <td><%= sales.model.stage_name %></td>
                <td><%= sales.user.email %></td>
                <td><%= number_to_currency(sales.cost) %></td>
                <td><%= number_to_currency(sales.cost * 0.25) %></td>
                <td>
                    <% if sales.lifetime? %>
                        yes
                    <% else %>
                        no
                    <% end %></td>
                <td><%= Time.at(sales.created_at).to_datetime.strftime("%B %d, %Y") %></td>
                <% if sales.end_date.present? %>
                    <td><%= Time.at(sales.end_date).to_datetime.strftime("%B %d, %Y") %></td>
                <% else %>
                    <td>active</td>
                <% end %>
                <!-- start of column payments  -->
                <% if active_payments.present? %>
                    <td><%= active_payments %></td>
                <% end %>
                <% if canceled_payments.present? %>
                    <td><%= canceled_payments %></td>
                <% end %>
                <!-- end of number of payments made -->
                <!-- start total earned -->
                <% if total_active_payments.present? %>
                    <td><%= number_to_currency(total_active_payments) %></td>
                <% end %>
                <% if total_canceled_payments.present? %>
                    <td><%= number_to_currency(total_canceled_payments) %></td>
                <% end %>
                <!-- end total of payments -->
            </tr>
        <% end %>
            <tr class="totals">
        <td>Totals</td>
        <td></td>
          <td></td>
          <td><%= number_to_currency @affiliate_sales_total %></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>

我的一个计算是弄清楚订阅者已经支付了多少月付款,我正在使用我的循环中的变量active_payments和canceled_pa​​yments成功完成。

从那里开始,我将按订阅费用的次数乘以附属公司的百分比,这样就可以获得联盟会员每次订阅的总收入(total_active_payments和total_canceled_pa​​yments)。

在我的&#34;总数&#34;行(在循环之外)我使用我的affiliate_sites_controller中的方法来计算订阅的总成本:

def affiliate_sales
  if current_user.affiliate_sites.present?
    Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token)
  end
end

def affiliate_canceled_subs
  if current_user.affiliate_sites.present?
    Subscription.where(active: false, teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token)
  end
end

def affiliate_sales_total
  if current_user.affiliate_sites.present?
    affiliate_sales.sum(:cost) * 0.25
  end
end

我遇到问题的地方是试图加上&#34;总收入&#34;列为总计。由于我无法在循环之外访问这些变量,是否有其他方法可以解决这个问题?

这是我的控制人员:

before_action :authenticate_user!

def new
    @affiliate_site = current_user.affiliate_sites.new
end

def show
  @affiliate_site = current_user.affiliate_sites.first
  @affiliate_site_token = affiliate_site_token
  @affiliate_sales = affiliate_sales
  @affiliate_sales_total = affiliate_sales_total
  @progress_bar = percentage_to_payout
  @affiliate_canceled_subs = affiliate_canceled_subs
end

def create
  @affiliate_site = current_user.affiliate_sites.new(affiliate_site_params)
  if @affiliate_site.save
  # send_model_application_email
    redirect_to profile_path(current_user), notice: "Your Affiliate information has been successfully submitted!"
  else
    render :new
  end
end

def edit
  @affiliate_site = current_user.affiliate_sites.first
end

def update
  @affiliate_site = current_user.affiliate_sites.first
  if @affiliate_site.update(affiliate_site_params)
    redirect_to affiliate_site_path(current_user.affiliate_sites.first), notice: "Your information has been successfully updated!"
  else
    render :edit
  end
end

def affiliate_site_token
  if current_user.affiliate_sites.present?
    current_user.affiliate_sites.first.affiliate_site_token
  end
end

def affiliate_sales
  if current_user.affiliate_sites.present?
    Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token)
  end
end

def affiliate_canceled_subs
  if current_user.affiliate_sites.present?
    Subscription.where(active: false, teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token)
  end
end

def affiliate_sales_total
  if current_user.affiliate_sites.present?
    affiliate_sales.sum(:cost) * 0.25
  end
end

def affiliate_rebills
  active_subscriptions = Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token, active: true, lifetime: [false, nil])
  # figure out number of months it has rebilled
  (Date.today.year * 12 + Date.today.month) - (active_subscriptions.created_at.year * 12 + active_subscriptions.created_at.month)
end

def rebills_before_canceled
  canceled_subscriptions = Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token, active: false, lifetime: [false, nil]) && Subscription.end_date?
    (canceled_subscriptions.end_date.year * 12 + canceled_subscriptions.end_date.month) - (canceled_subscriptions.created_at.year * 12 + canceled_subscriptions.created_at.month)
end

def percentage_to_payout
  if current_user.affiliate_sites.present?
    result = (affiliate_sales_total / 50.0) * 100
    return result.to_s.at(0..4) + '%'
  end
end

private

  def affiliate_site_params
    params.require(:affiliate_site).permit(:full_name, :company_name, :tax_id, :affiliate_site_token, :website, :email, :user_id, :affiliate_terms)
  end
end
编辑:所以,我能够解决这个问题。在重构了我的逻辑沉重视图后,我在subscription.rb模型中创建了两个方法来计算订阅者的付款数量:

def total_active_payments
    ((Date.today.year * 12 + Date.today.month) - (created_at.year * 12 + created_at.month) + 1)
end

def canceled_subscription_payments
    ((end_date.year * 12 + end_date.month) - (created_at.year * 12 + created_at.month))
end

然后我在我的控制器中添加了两个新方法:

def grand_total_active_payments
  grand_total_active_payments = 0
  Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token, active: true).each do |sub|
    grand_total_active_payments += ((sub.cost * 0.25) * sub.total_active_payments)
  end
  grand_total_active_payments
end

def grand_total_canceled_payments
  grand_total_canceled_payments = 0
  Subscription.where(teddys_affiliate_site_token: current_user.affiliate_sites.first.affiliate_site_token, active: false).each do |sub|
    grand_total_canceled_payments += ((sub.cost * 0.25) * sub.canceled_subscription_payments)
  end
  grand_total_canceled_payments
end

然后我在视图中简单地将这两个实例一起添加:

<tr class="totals">
  <td>Totals</td>
    <td></td>
    <td></td>
    <td><%= number_to_currency @affiliate_sales_total %></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><%= number_to_currency(@grand_total_active_payments + @grand_total_canceled_payments) %></td>
  </tr>

0 个答案:

没有答案