ror How can i order by has many value - ruby-on-rails

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)

Related

Search form don't update table in RoR

I'm trying to write a filter for the table. Here is the code:
.../models/message.rb
Class Message < ActiveRecord::Base
include Filterable
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :user_id, presence: true
validates :content, presence: { message: "Выберите файл послания для загрузки!" }, file_size: { less_than_or_equal_to: 20.megabytes }
validates :fromdate, presence: true
validates :tilldate, presence: true
mount_uploader :content, ContentUploader
scope :id, -> (id) {where id: id }
scope :tariff, -> (tariff) { where tariff: tariff }
scope :status, -> (status) { where("status like ?", "#{status}%")}
end
.../controllers/concerns/filterable.rb
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end
results
end
end
end
.../controllers/messages_controller.rb
...
def index
#messages = Message.filter(params.slice(:id, :tariff, :status)).paginate(page: params[:page], :per_page => 10)
end
...
.../views/messages/index.html.erb
...
<table class="table table-bordered">
<thead>
<tr>
<th>Клиент</th>
<th>№</th>
<th>Дата</th>
<th>Послание</th>
<th>Тариф</th>
<th>Показ с</th>
<th>до</th>
<th>Стоимость, р.</th>
<th>Статус</th>
<% if current_user.admin? %>
<th>Модератор</th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<%= form_tag messages_path, method: "get" do %>
<td><%= text_field_tag "client" %></td>
<td><%= text_field_tag "id", nil, size: 1 %></td>
<td></td>
<td><%= submit_tag "Выбрать" %><br><br><%= button_tag "Очистить" %></td>
<td><%= select_tag "tariff", options_for_select(["", "1. Вечер", "2. Весь день", "1001. ВЕЧЕР БЕСПЛАТНО", "1002. ДЕНЬ БЕСПЛАТНО"]), {} %></td>
<td></td>
<td></td>
<td></td>
<td><%= select_tag "status", options_for_select(["", "В обработке", "Исполняется", "Отклонена", "Выполнена"]), {} %></td>
<td></td>
<% end %>
</tr>
<%= render #messages %>
</tbody>
</table>
</div>
...
.../views/messages/_message.html.erb
...
<% case message.status
when "В обработке"
#messagecolor="warning"
when "Исполняется"
#messagecolor="success"
when "Выполнена"
#messagecolor="info"
when /Отклонена.*/
#messagecolor="danger"
end
unless message.moderator.nil?
#moderator = User.find(message.moderator).name
end
%>
<tr class=<%= #messagecolor %> data-message_id="<%= message.id %>">
<% if current_user.admin? || current_user.role == "Модератор" %>
<td><%= message.user.name %></td>
<% end %>
<td><%= message.id %></td>
<td><%= message.created_at.strftime("%d/%m/%Y %T %z") %></td>
<td><img src="<%= message.content_url(:thumb) %>" data-toggle="modal" data-target="#myModal" data-content="<%= message.content_url%>"></td>
<td><%= message.tariff %></td>
<td><%= message.fromdate.strftime("%d/%m/%Y") %></td>
<td><%= message.tilldate.strftime("%d/%m/%Y") %></td>
<td><%= message.cost %></td>
<td class="status">
<%= message.status %>
<% if (current_user.admin? || current_user.role == "Модератор") && message.status == "В обработке" %>
<br>
<div class="btn-group">
<button type="button" class="btn btn-success" data-status="Исполняется" data-message_id="<%= message.id %>">Принять</button>
<button type="button" class="btn btn-danger" data-status="Отклонена" data-message_id="<%= message.id %>">Отклонить</button>
</div>
<% end %>
<% if (current_user.admin? || current_user.role == "Модератор") && message.status == "Исполняется" %>
<br>
<%= link_to "Изменить", edit_message_path(message.id), :class => "btn btn-info" %>
<% end %>
</td>
<% if current_user.admin? %>
<td class="moderator">
<%= #moderator %><br>
<%= message.updated_at.strftime("%d/%m/%Y %T %z")%>
</td>
<% end %>
</tr>
...
The filter only works after the page is refreshed. It is worth to get back to the page through the menu - the form button does not work, update by F5 - it works. Why?
I will also be grateful if you tell me how to save the selected filter values ​​in the fields after it is applied.
I found the answer to my main question: it need to include the entire table into the form in .../views/messages/index.html.erb
...
<%= form_tag messages_path, method: "get" do %>
<table class="table table-bordered">
<thead>
<tr>
<th>Клиент</th>
<th>№</th>
<th>Дата</th>
<th>Послание</th>
<th>Тариф</th>
<th>Показ с</th>
<th>до</th>
<th>Стоимость, р.</th>
<th>Статус</th>
<% if current_user.admin? %>
<th>Модератор</th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<td><%= text_field_tag "client" %></td>
<td><%= text_field_tag "id", nil, size: 1 %></td>
<td></td>
<td><%= submit_tag "Выбрать" %><br><br><%= button_tag "Очистить" %></td>
<td><%= select_tag "tariff", options_for_select(["", "1. Вечер", "2. Весь день", "1001. ВЕЧЕР БЕСПЛАТНО", "1002. ДЕНЬ БЕСПЛАТНО"]), {} %></td>
<td></td>
<td></td>
<td></td>
<td><%= select_tag "status", options_for_select(["", "В обработке", "Исполняется", "Отклонена", "Выполнена"]), {} %></td>
<td></td>
</tr>
<%= render #messages %>
</tbody>
</table>
<% end %>
</div>
...
And now it works correctly.

How to check if association exists and display associated record instead of actual record.

I am trying to have the system check to see if there is a customer category, if there is, display the customer category and if not display the internal category. I tried doing something like
<% if c.customer_labor_category != 'nil' %>
I also tried the following right above where it displays "l"
<% if c.customer_labor_category.any? %>
I basically need it to check if there is a customer category if there is, use that field instead of the internal one.
<% c = lh.pluck('categories.category').uniq %>
<% if c.length == 0 %>
<tr>
<td class="tg-multirow" rowspan="1">Category</td>
<td class="tg-datacell"></td>
<td class="tg-celltitle" rowspan="1">Effort</td>
<td class="tg-datacell"></td>
</tr>
<% else %>
<% c.each do |l| %>
<tr>
<% if l == c.first %>
<td class="tg-celltitle" rowspan="<%= lc.length %>">Labor Category</td>
<% end %>
<td class="tg-datacell"> <%= l %> </td>
<% if l == c.first %>
<td class="tg-celltitle" rowspan="<%= c.length %>">effort</td>
<% end %>
<% hours = lh.joins(:category).where(:categories => { :category => l }).each.sum(&:hours) %>
<td class="tg-datacell"> <%= hours %></td>
</tr>
<% end %>
<% end %>
c.customer_labor_category.nil? # true if nil
c.customer_labor_category.present? # true if not nil

Rails loop with heading

I have this loop in Rails and I want to list each associated region_id under each region.name. So far under each region.name I get a list of all items.
Region has_many :trials
Trials belongs_to :region, :primary_key => 'region_id'
View
<% #regions.each do |region, list| %>
<h3><%= region.region.name %></h3>
<table class="table">
<% list.each do |list| %>
<tr>
<td>
<%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) unless list.site.site_name.blank? %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
<td>
<%= link_to list.grower.name, trial_trials_path(trial_id: list.trial_id) unless list.grower.name.blank? %>
</td>
</tr>
<% end %>
</table>
<% end %>
</div>
Controller
def index
list = Trial.where('year = ?', Time.now.year).order(:region_id)
#regions = list.group_by { |t| t.region_id }
end
In your controller, you have to group your items by region, using group_by added to your where close
In your view, you can then iterate through your items with #list[region].each

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 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