2016-11-24 29 views
1

私は、このアプリの初めから合計を集計する計算をしています。私は現在の月の合計を計算するのに役立つ必要があり、それを行う方法がわかりません。前もって感謝します。例えばルビー計算を使用した月合計計算

where(created_at: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month) 

statistics.html.erb

class Item < ActiveRecord::Base 
    def profit_calc 
    sold_for - bought_for - fees - shipping rescue 0 
    end 

    def self.purchase_total 
    sum(:bought_for) 
    end 

    def self.fee_total 
    sum(:fees) 
    end 

    def self.shipping_total 
    sum(:shipping) 
    end 

    def self.sales_total 
    sum(:sold_for) 
    end 

    def self.profit_total 
    sold.sum(:sold_for) - sold.sum(:bought_for) - sold.sum(:fees) - sold.sum(:shipping) 
    end 

    def profit_margin_item 
    profit_calc/sold_for * 100 rescue 0 
    end 

    def self.profit_margin_total 
    profit_total/ sum(:sold_for) * 100 
    end 

    scope :visible, -> { where(sold: false) } 
    scope :sold, -> { where(sold: true) } 

    def self.search(search) 
    where("description LIKE ?", "%#{search}%") 
    end 
end 

item.rb

<h1 id="title">Statistics</h1> 
<br> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Bought</th> 
     <th>Bought This Month</th> 
     <th>Total Sold</th> 
     <th>Sold This Month</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= @items.count %></td> 
     <td><%= @items_month.count %></td> 
     <td><%= @items_sold.count %></td> 
     <td><%= @items_sold_month.count %></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Purchases</th> 
     <th>Total Month Purchases</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_currency(@items.purchase_total) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Sales</th> 
     <th>Total Month Sales</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_currency(@items.sales_total) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Fees</th> 
     <th>Total Month Fees</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_currency(@items.fee_total) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Shipping</th> 
     <th>Total Month Shipping</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_currency(@items.shipping_total) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Profit</th> 
     <th>Total Month Profit</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_currency(@items.profit_total) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

<table align="center" style="width: 95%" class="table table-striped table-bordered"> 
    <thead> 
    <tr> 
     <th>Total Margin</th> 
     <th>Total Month Margin</th> 
    </tr> 
    </thead> 

    <tbody> 
    <tr> 
     <td><%= number_to_percentage(@items.profit_margin_total, precision: 0) %></td> 
     <td></td> 
    </tr> 
    </tbody> 
</table> 

答えて

2

今月の計算を取得するには、次の条件を追加します。

def self.purchase_total 
    where(created_at: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month).sum(:bought_for) 
end 

あなたはscopeとして追加し、広範囲にそれを使用するつもりなら:

scope :current_month, -> { where(created_at: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month) } 

def self.purchase_total 
    current_month.sum(:bought_for) 
end 
+1

良い答えが、あなたはあなたのスコープ内用 – Fallenhero

+1

@FallenheroのTHXを.SUMまま気づいて!固定:) –

+0

それは素晴らしい作品です。あなたの助けとハッピー感謝祭に感謝します! –