Rails - Subtotaling based on if statements - ruby-on-rails

<h1> What it looks like currently</h1><br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Month Total</th>
<th>Written</th>
<th>Verbal</th>
<th>Probable 75%</th>
<th>Probable 25%</th>
<th>Speculative</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr><td>Jan 2017</td> <td>50</td><td>0</td><td>50</td><td>0</td><td>0</td><td>0</td></tr>
<tr> <td>Feb 2017</td> <td>100</td><td>0</td><td>100</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>Mar 2017</td> <td>700</td><td>0</td><td>700</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>Jan 2017</td> <td>700</td><td>700</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>Feb 2017</td> <td>5000</td><td>5000</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>Jan 2017</td> <td>500</td><td>0</td><td>0</td><td>500</td><td>0</td><td>0</td></tr>
<tr><td>Jan 2017</td> <td>5000</td><td>0</td><td>0</td><td>0</td><td>0</td><td>5000</td></tr>
</tbody>
</table>
<h1> What I need it look like </h1><br>
<table>
<thead>
<tr>
<th>Month</th>
<th>Month Total</th>
<th>Written</th>
<th>Verbal</th>
<th>Probable 75%</th>
<th>Probable 25%</th>
<th>Speculative</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr><td>Jan 2017</td> <td>1250</td><td>700</td><td>50</td><td>500</td><td>0</td><td>5000</td></tr>
<tr> <td>Feb 2017</td> <td>5100</td><td>5000</td><td>100</td><td>0</td><td>0</td><td>0</td></tr>
<tr><td>Mar 2017</td> <td>700</td><td>0</td><td>700</td><td>0</td><td>0</td><td>0</td></tr>
</tbody>
</table>
I'm a bit of a coding noob so I could be going about this all the wrong way, but I've muddled my way through so far...until now.
What I need is to subtotal the months so the values all appear on the relevant line: Here
The code I used for that is:`
<% #months.each do |t| %>
<tr>
<td><%= t.monthYear %> </td> <td><%= t.monthValue %></td>
<% if(t.destination.status == "Written") %>
<td><%= t.monthValue %></td>
<% else %>
<td>0</td>
<% end %>
<% if(t.destination.status == "Verbal") %>
<td><%= t.monthValue %></td>
<% else %>
<td>0</td>
<% end %>
<% if(t.destination.status == "Probable 75%") %>
<td><%= t.monthValue %></td>
<% else %>
<td>0</td>
<% end %>
<% if(t.destination.status == "Probable 25%") %>
<td><%= t.monthValue %></td>
<% else %>
<td>0</td>
<% end %>
<% if(t.destination.status == "Speculative") %>
<td><%= t.monthValue %></td>
<% else %>
<td>0</td>
<% end %>
</tr>
<% end %>`
The models:
Destination:
class Destination < ActiveRecord::Base
belongs_to :tag
has_many :months
end
Month:
class Month < ActiveRecord::Base
belongs_to :destination
belongs_to :tag
end
The months have 3 fields: monthYear, monthValue & Destination_id.
Destination has Status and others which I don't think are relevant.
I've been searching and used:
% #months.select(:monthYear, :monthValue, :destination_id).group(:monthYear).each do |t| %> and different variations including trying to sum by :monthValue but end up with this: Subtotaled months but not values.
Thanks in advance!

So I figured it out, but the answer definitely isn't pretty! I appreciate this could be done a lot clearer, but I've muddled my way through! Here's the code I used and the result:
View
<tbody>
<% #months.group(:monthYear).each do |z| %>
<% this_thing = 0 %>
<% this_thing2 = 0 %>
<% this_thing3 = 0 %>
<% this_thing4 = 0 %>
<% this_thing5 = 0 %>
<% #testing.each do |b| %>
<% if (z.monthYear == b.monthYear) && (b.destination.status == "Written") %>
<% this_thing = this_thing + b.monthValue %>
<% end %>
<% if (z.monthYear == b.monthYear) && (b.destination.status == "Verbal") %>
<% this_thing2 = this_thing2 + b.monthValue %>
<% end %>
<% if (z.monthYear == b.monthYear) && (b.destination.status == "Probable 75%") %>
<% this_thing3 = this_thing3 + b.monthValue %>
<% end %>
<% if (z.monthYear == b.monthYear) && (b.destination.status == "Probable 25%") %>
<% this_thing4 = this_thing4 + b.monthValue %>
<% end %>
<% if (z.monthYear == b.monthYear) && (b.destination.status == "Speculative") %>
<% this_thing5 = this_thing5 + b.monthValue %>
<% end %>
<% end %>
<td><%=z.monthYear %></td>
<td><%= this_thing + this_thing2 + this_thing3 + this_thing4 + this_thing5 %> </td>
<td><%= this_thing %></td>
<td><%= this_thing2 %></td>
<td><%= this_thing3 %></td>
<td><%= this_thing4 %></td>
<td><%= this_thing5 %></td>
<td><%= (this_thing * 1)+(this_thing2 * 1)+(this_thing3 *0.75)+(this_thing4 * 0.25)+(this_thing5 * 0)%></td>
</tr>
<% end %>
</tbody>
Tags Controller
def index
#months = Month.all
#testing = Month.joins(:destination).includes(:destination).select("months.monthYear, months.monthValue, destination_id, destinations.status")
end
The end result!

Related

ror How can i order by has many value

How can I order by has-many value
I want to do something like #cases = #user.cases.order_by(step.name="shipped" desc)
I have cases table and I want if the case.step.name = "Shipped" to be at the end of the table
I have case model
has_many :steptations, dependent: :destroy
has_many :steps, through: :steptations
accepts_nested_attributes_for :steps
and step model
has_many :steptations,dependent: :destroy
has_many :cases, through: :steptations
Here is my cases controller
def index
#cases = #user.cases.all
end
Here is my case.html
<% #cases.each do |item| %><tr>
<td ><%= link_to case_path(item) do %><%= item.number %><% end %></td>
<% if item.steptations.present? %>
<% item.steptations.where(:status=>true).each do |t| %>
<% if t.step&.id == 13 %>
<td class= 'fa fa-circle red' ></td>
<% elsif item.finished == true && t.step&.name == "Shipped"%>
<td class= 'fa fa-circle red' ></td>
<% elsif item.finished == true %>
<td class= 'fa fa-circle dark'></td>
<% else %>
<td class= 'fa fa-circle green'></td>
<% end %>
<% end %>
<% else %>
<td>0</td>
<% end %>
<td> <%= link_to case_path(item) do %><%= item.pt_first_name.capitalize %> <%= item.pt_last_name.capitalize %><% end %></td>
<td><%=link_to item.user.full_name,doctor_path(item.user)%></td>
<td> <%= item.date_received.strftime("%b-%-d-%-y") %></td>
<td> <%= item.due_date.strftime("%b-%-d-%-y") %></td>
<td><%= item.shade %></td>
<td><%= item.mould %></td>
<td><%= item.upper_lower %></td>
<% if item.steptations.present? %>
<% item.steptations.where(:status=>true).each do |t| %>
<% if t.step&.name == "Conversion" %>
<td class="red"><%= t.step&.name%></td>
<% else %>
<td><%= t.step&.name%> </td>
<% end %>
<% end %>
<% else %>
<td>0</td>
<% end %>
</td>
</tr>
<% end %></tbody>
</table><br>
What you're looking for is the SQL ORDER BY FIELD function. In Ruby it should be something like this:
Case.where(user: #user).order("FIELD(name, #{some name param variable}) desc")
Try this:
Case.joins(steptations: :step)
.group("cases.id")
.select("cases.*,
COUNT(steps.name) filter (where steps.name = 'shipped') as shipped")
.order(:shipped)

will_paginate in the same page for the same instance variable but for three different displays

This is the controller I have writter for Orders.
def index
if current_user.has_role? :admin
#orders = Order.all.paginate(:page => params[:page], :per_page => 5)
elsif current_user.has_role? :customer
#orders = Order.where(:email => current_user.email).paginate(:page => params[:page], :per_page => 5)
elsif current_user.has_role? :white_label
#orders = Order.where(:performer_id => (Performer.where(:white_label_id => current_user.white_label.id))).paginate(:page => params[:page], :per_page => 5)
else
#orders = current_user.performer.orders.paginate(:page => params[:page], :per_page => 5)
end
#custom_video = CustomVideo.new
end
As you see I have used will_paginate gem to do pagination. This is the view page where I am doing pagination.
<div class="container">
<div class="span 8">
<% if current_user.has_role? :admin %>
<%role =1%>
<%elsif current_user.has_role? :performer %>
<%role =2%>
<% else %>
<%role =3%>
<% end %>
<h3>Awaiting Upload</h3>
<table id="order_table" class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th>Description</th>
<th>Total</th>
<% if role==2 %>
<th>Select a video to upload for the order</th>
<%end %>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if !CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date + var1.days%>
<tr>
<td> <%= order.id%></td>
<td><% if order.location %>
<%= order.location.name %>
<% end %></td>
<td><% if order.performer %>
<%= order.performer.first_name %>
<% end %></td>
<td><% if order.duration %>
<%= order.duration.time %>
<% end %></td>
<td><% if order.quality %>
<%= order.quality.name %>
<% end %></td>
<td><% if order.delivery_time %>
<%= order.delivery_time.duration %>
<% end %></td>
<td><% if order.clip_category %>
<%= order.clip_category.name %>
<% end %></td>
<td><% if order.description %>
<%= order.description %>
<% end %></td>
<td><%= order.total %></td>
<% if role==2 %>
<td>
<%= simple_form_for(#custom_video, :html => { :multipart => true, :id => "fileupload" }) do |f| %>
<%= f.error_notification %>
<%= f.file_field :path%><br/><br>
<%= f.hidden_field :order_id,:value => order.id %>
<%=f.button :submit, :value=>"Save", :class=>"btn btn-success" %>
<% end %>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
<%end%>
</td>
<td>
<% if role==1 %>
<%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
<%= hidden_field_tag(:id, order.id) %>
<%= submit_tag ("Refund"),:class => "btn btn-success download" %>
<% end %>
<% end %>
</td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<%else %>
<% next %>
<%end %>
<% end -%>
<% end %>
<%= will_paginate #orders, :param_name => 'awaiting_orders' %>
</tbody>
</table><br/>
<h3>Completed Orders</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th>id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date+var1.days%>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<td>
<% if role==1 %>
<%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
<%= hidden_field_tag(:id, order.id) %>
<%= submit_tag ("Refund"),:class => "btn btn-success download" %>
<% end %>
<% end %>
</td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table><br/>
<h3>Expired Orders</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th> id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if Time.now > order.created_at.to_date+var1.days%>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table>
<br>
<h3>Orders Refunded</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th> id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.refunded %>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table>
<br>
</div>
</div>
<%# link_to 'New Order', new_order_path %>
The problem in this is that. If I click the second page for the first table, it is going to the second for the next table too. I have tried giving the :param_name => 'questions_page' suggested here. But the trouble here is that, I am not using different instance variable for the tables but the same one. How do I solve this?>
You've almost answered your own question. You need to be using separate instance variables for each table if you want them to behave separately.

rails: organizing data into a table using for

I have the following view:
<table class="fixed">
<tr>
<th>Student Name</th>
<!-- create as many <th> as there are evaluations -->
<% #eval_count.times do |i| %>
<th>Evaluation <%= i+1 %></th>
<% end %>
<th>Student Average <br />(for this goal)</th>
</tr>
<% for eval in #evals %>
<tr class="<%= cycle("odd", "even", name: "evals")%>">
<!-- eval returns { s_id [eval],[eval]} -->
<td><%= eval[1].first.student.name%></td>
<!-- in each student's row, print the score for each consecutive evaluation -->
<% #eval_count.times do |i| %>
<td><%= eval[1][i].score %><% #ss_scores << eval[1][i].score %></td>
<% end %>
<td><%= #ss_scores %></td>
</tr>
<% reset_cycle("evals") %>
<% end %>
</table>
<% #ss_scores.in_groups(#student_count, false) do |group|%>
<%= (group.sum.to_f/group.size).round(2) %>
<% end %>
which renders the following:
I want to put the average for each student in the last column, but #ss_scores is a variable and so calling anything on it doesn't work. But when the for loop has finished, #ss_scores can be worked with nicely as in the bottom of the screenshot. Any idea how to do this better?
Try emptying the array everytime, using [] and calculate the average inline, like below
<td><%= #ss_scores.inject(0.0) { |sum, el| sum + el } / #ss_scores.size %></td>
<% #ss_scores = [] %>
-
<% for eval in #evals %>
<tr class="<%= cycle("odd", "even", name: "evals")%>">
<!-- eval returns { s_id [eval],[eval]} -->
<td><%= eval[1].first.student.name%></td>
<!-- in each student's row, print the score for each consecutive evaluation -->
<% #eval_count.times do |i| %>
<td><%= eval[1][i].score %>
<% #ss_scores << eval[1][i].score %>
</td>
<% end %>
<td><%= #ss_scores.inject(0.0) { |sum, el| sum + el } / #ss_scores.size %></td>
<% #ss_scores = [] %>
</tr>
<% reset_cycle("evals") %>
<% end %>

Using a if statement in a looped table rails

I have two models, one with cards and another with their associated rewards programs. I'm showing all of them in a table with a conditional if statement in some of the columns but I can't figure out why an if else statement screws up my columns. I posted two examples one that works and one that doesn't. I need the second one to work to add some additional functionality
This example works
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% #cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<% if category.name.downcase == "gas" %>
<td><%= category.threshold_check(#gas) %></td>
<% end %>
<% if category.name.downcase == "movies" %>
<td><%= category.threshold_check(#movies) %></td>
<% end %>
<% if category.name.downcase == "museums" %>
<td><%= category.threshold_check(#museums) %></td>
<% end %>
<% if category.name.downcase == "theme parks" %>
<td><%= category.threshold_check(#theme_parks) %></td>
<% end %>
<% if category.name.downcase == "restaurants" %>
<td><%= category.threshold_check(#restaurants) %></td>
<% end %>
<% if category.name.downcase == "department stores" %>
<td><%= category.threshold_check(#department_stores) %></td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
This adds extra columns to the end
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% #cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<td><%= category.name.downcase == "gas" ? category.threshold_check(#gas) : 0 %></td>
<td><%= category.name.downcase == "movies" ? category.threshold_check(#movies) : 0 %></td>
<td><%= category.name.downcase == "museums" ? category.threshold_check(#museums) : 0 %></td>
<td><%= category.name.downcase == "theme parks" ? category.threshold_check(#theme_parks) : 0 %></td>
<td><%= category.name.downcase == "restaurants" ? category.threshold_check(#restaurants) : 0 %></td>
<td><%= category.name.downcase == "department stores" ? category.threshold_check(#department_stores) : 0 %></td>
<% end %>
</tr>
<% end %>
</table>
If you will notice the main difference between your 2 versions is that the first one if your conditionals dont comply, you will not issue a <td></td>
On the other hand your second version does have <td></td> no matter if the conditionals are true or not.
Maybe that could be the source for your extra columns.
P.S. In your first example I think you have a typo but <%= category.threshold_check(#movies) %> doesn't have an opening <td> and it's closing </td> is outside the if statement.

Rails 3. How to build a table from invoice's line items and the line item names vary?

I have been breaking my head for almost two days now and I can't find a solution.
I have a table that lists invoices with invoice number, total, amount paid and balance; and it works well. Now I have to add all the line items to the table.
The invoices table has the columns: of_lax, air_rate_customer, pss, so that part it's easy, I just have to check if there is a value there.
The tricky part is for the fcl_variable_cost_1_amount attributes, I got 8 of those (fcl_variable_cost_1_amount, fcl_variable_cost_2_amount, etc).
The fcl_variable_cost_1_amount has a corresponding fcl_variable_cost_1_charge_id that links to a Charge tables.
So in one invoice fcl_variable_cost_1_id might be 34 and amount $100 (id 34 corresponds to "ISF").
In another invoice fcl_variable_cost_1_id might be 12 and amount $50 (id 12 corresponds to "Examination fee").
so how can I make the variable name line items appear in the column they should?
This is my code. There is also a screenshot.
<% headings = Hash.new %>
<% #shipments.each do |shipment| %>
<% unless shipment.invoice.nil? %>
<% unless shipment.invoice.of_customer_amount_for_customer_inv.nil? %>
<% headings['of_customer_amount_for_customer_inv'] = "Ocean Freight (FCL)" %>
<% end %>
<% unless shipment.invoice.of_lax.nil? %>
<% headings['of_lax'] = "Ocean Freight (LCL)" %>
<% end %>
<% unless shipment.invoice.air_rate_customer.nil? %>
<% headings['air_rate_customer'] = "Air Freight" %>
<% end %>
<% unless shipment.invoice.pss.nil? %>
<% headings['pss'] = "PSS" %>
<% end %>
<% unless shipment.invoice.hc_lax.nil? %>
<% headings['hc_lax'] = "HC LAX" %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_1_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_1_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_1_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_2_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_2_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_2_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_3_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_3_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_3_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_4_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_4_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_4_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_5_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_5_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_5_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_6_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_6_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_6_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_7_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_7_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_7_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_8_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_8_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_8_charge_id).name %>
<% end %>
<% end %>
<% end %>
<table>
<tr>
<th colspan="9">Customer AR Statement for <%= #customer.company_name %></th>
</tr>
<tr>
<th style="text-align:center;">MTY</th>
<th>Shipper</th>
<th>HBL</th>
<th>Container</th>
<th>Status</th>
<th>Age</th>
<th>Delivered Customer</th>
<th>Invoice Date</th>
<% headings.each_pair do |k,v|%>
<th><%= v %></th>
<% end %>
<th>Invoice Total</th>
<th>Amount Paid</th>
<th>Balance</th>
</tr>
<% #shipments.each do |shipment| %>
<tr>
<td style="text-align:center;"><%= shipment.file_number %></td>
<td><%= shipment.shipper.company_name %></td>
<td><%= shipment.hbl %></td>
<td><%= shipment.container %></td>
<td><%= shipment.status %></td>
<td><%= shipment.age %></td>
<td><%= shipment.invoice.delivered_customer ? "Yes" : "No" %></td>
<td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td>
<% if shipment.invoice.nil? %>
<td colspan="<%= headings.count %>"></td>
<% else %>
<% headings.each_pair do |k,v| %>
<% if k == "of_lax" and !shipment.invoice.of_lax.nil? %>
<td><%= number_to_currency shipment.invoice.lcl_of_customer_total %>
<% elsif k == "of_customer_amount_for_customer_inv" and !shipment.invoice.of_customer_amount_for_customer_inv.nil? %>
<td><%= number_to_currency shipment.invoice.of_customer_amount_for_customer_inv %></td>
<% elsif k == "air_rate_customer" and !shipment.invoice.air_rate_customer.nil? %>
<td><%= number_to_currency (shipment.volweight * shipment.invoice.air_rate_customer.to_s.to_d) %></td>
<% elsif k == "pss" and !shipment.invoice.pss.nil? %>
<td><%= number_to_currency (shipment.invoice.pss.to_s.to_d * shipment.volweight) %></td>
<% elsif k == "hc_lax" and !shipment.invoice.hc_lax.nil? %>
<td><%= number_to_currency (shipment.invoice.hc_lax.to_s.to_d * shipment.volweight) %></td>
<% else %>
<td></td>
<% end %>
<% end %>
<% end %>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td>
</tr>
<% end %>
<tr>
<td colspan="<%= 7 + (headings.count) %>"></td>
<th>Totals</th>
<td style="text-align:right;"><%= number_to_currency #totals[:overall] %></td>
<td style="text-align:right;"><%= number_to_currency #totals[:paid] %></td>
<td style="text-align:right;"><%= number_to_currency #totals[:balance] %></td>
</tr>
</table>
For future reference in case anyone runs into a similar task
<% headings = Hash.new %>
<% #shipments.each do |shipment| %>
<% unless shipment.invoice.nil? %>
<% unless shipment.invoice.of_customer_amount_for_customer_inv.nil? %>
<% headings['of_customer_amount_for_customer_inv'] = "Ocean Freight (FCL)" %>
<% end %>
<% unless shipment.invoice.of_lax.nil? %>
<% headings['of_lax'] = "Ocean Freight (LCL)" %>
<% end %>
<% unless shipment.invoice.air_rate_customer.nil? %>
<% headings['air_rate_customer'] = "Air Freight" %>
<% end %>
<% unless shipment.invoice.pss.nil? %>
<% headings['pss'] = "PSS" %>
<% end %>
<% unless shipment.invoice.hc_lax.nil? %>
<% headings['hc_lax'] = "HC LAX" %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_1_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_1_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_1_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_2_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_2_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_2_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_3_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_3_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_3_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_4_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_4_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_4_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_5_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_5_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_5_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_6_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_6_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_6_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_7_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_7_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_7_charge_id).name %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_8_amount.nil? %>
<% headings[(Charge.find(shipment.invoice.fcl_variable_cost_8_charge_id)).name] = Charge.find(shipment.invoice.fcl_variable_cost_8_charge_id).name %>
<% end %>
<% end %>
<% end %>
<table>
<tr>
<th colspan="9">Customer AR Statement for <%= #customer.company_name %></th>
</tr>
<tr>
<th style="text-align:center;">MTY</th>
<th>Shipper</th>
<th>HBL</th>
<th>Container</th>
<th>Status</th>
<th>Age</th>
<th>Delivered Customer</th>
<th>Invoice Date</th>
<% headings.each_pair do |k,v|%>
<th><%= v %></th>
<% end %>
<th>Invoice Total</th>
<th>Amount Paid</th>
<th>Balance</th>
</tr>
<% #shipments.each do |shipment| %>
<tr>
<td style="text-align:center;"><%= shipment.file_number %></td>
<td><%= shipment.shipper.company_name %></td>
<td><%= shipment.hbl %></td>
<td><%= shipment.container %></td>
<td><%= shipment.status %></td>
<td><%= shipment.age %></td>
<td><%= shipment.invoice.delivered_customer ? "Yes" : "No" %></td>
<td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td>
<% if shipment.invoice.nil? %>
<td colspan="<%= headings.count %>"></td>
<% else %>
<% headings.each_pair do |k,v| %>
<% if k == "of_lax" and !shipment.invoice.of_lax.nil? %>
<td><%= number_to_currency shipment.invoice.lcl_of_customer_total %>
<% elsif k == "of_customer_amount_for_customer_inv" and !shipment.invoice.of_customer_amount_for_customer_inv.nil? %>
<td><%= number_to_currency shipment.invoice.of_customer_amount_for_customer_inv %></td>
<% elsif k == "air_rate_customer" and !shipment.invoice.air_rate_customer.nil? %>
<td><%= number_to_currency (shipment.volweight * shipment.invoice.air_rate_customer.to_s.to_d) %></td>
<% elsif k == "pss" and !shipment.invoice.pss.nil? %>
<td><%= number_to_currency (shipment.invoice.pss.to_s.to_d * shipment.volweight) %></td>
<% elsif k == "hc_lax" and !shipment.invoice.hc_lax.nil? %>
<td><%= number_to_currency (shipment.invoice.hc_lax.to_s.to_d * shipment.volweight) %></td>
<% else %>
<td>
<% unless shipment.invoice.fcl_variable_cost_1_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_1_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_1_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_2_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_2_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_2_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_3_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_3_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_3_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_4_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_4_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_4_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_5_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_5_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_5_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_6_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_6_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_6_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_7_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_7_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_7_amount %>
<% end %>
<% end %>
<% unless shipment.invoice.fcl_variable_cost_8_amount.nil? %>
<% if Charge.find(shipment.invoice.fcl_variable_cost_8_charge_id).name == v %>
<%= number_to_currency shipment.invoice.fcl_variable_cost_8_amount %>
<% end %>
<% end %>
</td>
<% end %>
<% end %>
<% end %>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td>
<td style="text-align:right;"><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td>
</tr>
<% end %>
<tr>
<td colspan="<%= 7 + (headings.count) %>"></td>
<th>Totals</th>
<td style="text-align:right;"><%= number_to_currency #totals[:overall] %></td>
<td style="text-align:right;"><%= number_to_currency #totals[:paid] %></td>
<td style="text-align:right;"><%= number_to_currency #totals[:balance] %></td>
</tr>
</table>

Resources