rails: organizing data into a table using for - ruby-on-rails

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

Related

how to set multiple default form values on submission in rails?

attendance table that takes has attribute status, date,recuritment_id, and project_site_id .
project_site has one_to_many association with attendance
recuritmenthas one_to_many association with attendance.
i am taking attribute from recruitment table when attribute status is joined in attendance#form view.
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
here #project_site.attendance_month contains the attendance month value. based on month i calculate number of days column along with name from recuritmnet table.
here is view-
holydays master contains corresponding month holyday's date and in view it auto match date and prints "H",
All input default selected as P. there is final submission_button that chnages boolean attribute. now on final submission i want to push all default selected P into attendance table.
attendance_controller.rb
def new
if #project_site.submission_status == false
#attendance = Attendance.new
#date = HolydayCalendar.all
#recruitment = Recruitment.all.where(current_status: "2")
else
redirect_to project_site_attendances_path
end
end
from.html.erb (attendance controller view)
<table>
<thead>
<tr>
<th class="attendance-emp-name">Emp. Name</th>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<th class="text-center"><%= date %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #recruitment.where(location: #project_site.site_id).each do |recruitment| %>
<tr>
<td class="attendance-emp-name"><%= recruitment.name %></td>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<%= form_with(model: attendance, :html => {:id => 'attendance-form-validation'}, url:[#project_site, #attendance], local: true) do |f| %>
<% if HolydayCalendar.find_by(date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s), total_site_id: #project_site.site_id)%>
<td class="holyday text-center"><%= "H" %></td>
<% elsif recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) == nil %>
<td>
<%= f.select :status, [['P', 1], ['A', 2], ['L', 4], ['WE', 5], ['CO', 6]], {}, { onchange: 'this.form.submit()', class: 'attendance-select-input' } %>
</td>
<% else %>
<% attendance_value = recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) %>
<%if attendance_value.status == 1 %>
<td class="presant text-center"><%="P" %></td>
<% elsif attendance_value.status == 2 %>
<td class="absent text-center"><%="A" %></td>
<%elsif attendance_value.status == 3 %>
<td class="holyday text-center"><%="H" %></td>
<%elsif attendance_value.status == 4 %>
<td class="leave text-center"><%= "L" %></td>
<%elsif attendance_value.status == 5 %>
<td class="weekend text-center"><%= "WE" %></td>
<%elsif attendance_value.status == 6 %>
<td class="compoff text-center"><%= "CO" %></td>
<% end %>
<% end %>
<%= f.hidden_field :attendance_date, value: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)%>
<%=f.hidden_field :recruitment_id, value: recruitment.id%>
<%=f.hidden_field :project_site_id, value: #project_site.id%>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% if #project_site.submission_status == true %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary disabled" %>
</div>
<% else %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary" %>
</div>
<% end %>
project_sites_controller.rb
def set_submission_status
#project_site = ProjectSite.find(params[:id])
#project_site.update(submission_status: true)
end
It's a little unclear what your question is, but I gather it has to do with your form sending back an array op options.
If you want your html form to return an array of information, then you need to indicate it in your html using the name = "attendance[]" syntax, notice the square brackets.
Check out this article on the subject https://mattstauffer.com/blog/a-little-trick-for-grouping-fields-in-an-html-form/

Rails - Subtotaling based on if statements

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

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 summary with sub-totals by date

I'm writing my first Rails view that summarizes data by date. I want one row for each date with the columns summarized for that date.
I have been able to make it work. But, it's awkward coding. This is what I have:
<h3>Carwings Daily Summary</h3>
<table class="display dataTable table table-striped table-bordered" id="dataTable2">
<thead>
<tr>
<th>Date</th>
<th># Trips</th>
<th>E Consumption (kWh)</th>
<th>E Regeneration (kWh)</th>
<th>E Total (kWh)</th>
<th>Distance (Miles)</th>
<th>Energy Economy (Miles/kWh)</th>
<th>CO2 Emission Reduction (lbs)</th>
</tr>
</thead>
<tbody>
<% trips = 0 %>
<% consumption = 0 %>
<% regen = 0 %>
<% total = 0 %>
<% distance = 0 %>
<% economy = 0 %>
<% emissions = 0 %>
<% sumdate = nil %>
<% #carwings.each.with_index do |carwing, index| %>
<% sumdate = carwing.date if index == 0 %>
<% if carwing.date == sumdate %>
<% trips = trips + 1 %>
<% consumption = consumption + carwing.e_consumption %>
<% regen = regen + carwing.e_regen %>
<% total = total + carwing.e_total %>
<% distance = distance + carwing.distance %>
<% economy = economy + carwing.economy %>
<% emissions = emissions + carwing.emission_reduction %>
<% else %>
<tr>
<td class="nowrap"><%= sumdate %></td>
<td><%= trips %></td>
<td><%= consumption %></td>
<td><%= regen %></td>
<td><%= total %></td>
<td><%= distance %></td>
<td><%= economy %></td>
<td><%= emissions %></td>
</tr>
<% trips = 1 %>
<% consumption = carwing.e_consumption %>
<% regen = carwing.e_regen %>
<% total = carwing.e_total %>
<% distance = carwing.distance %>
<% economy = carwing.economy %>
<% emissions = carwing.emission_reduction %>
<% sumdate = carwing.date %>
<% end %>
<% end %>
<tr>
<td class="nowrap"><%= sumdate %></td>
<td><%= trips %></td>
<td><%= consumption %></td>
<td><%= regen %></td>
<td><%= total %></td>
<td><%= distance %></td>
<td><%= economy %></td>
<td><%= emissions %></td>
</tr>
</tbody>
</table>
There's got to be a better way.
Suggestions?
Thanks for the Help!!
Some minor stuff:
trips = trips + 1
# is better written as:
trips += 1
erb tags can be multiline eg:
<% if blah
do_something
something else
end %>
If you are setting multiple variables to the same value, you don't need to repeat them each line eg:
trips = consumption = regen = 0
yes - this is minor stuff - but clean up the minor stuff and it'll give you a better shape of what you're trying to do.
perhaps if you gave us your logic in descriptive pseudocode (so we aren't just guessing what you're trying to do) then we can give you better structure to your code too. :)
Personally: I'd recommend setting up all this data in your controller (or even your carwing model) before it ever hits the view. I'd use a hash - whee the carwnig.date is the key, and all the rest is another hash eg:
data = Hash.new({:trips => 0, :consumption => 0}) # google initialising hashes
#carwings.each do |carwing|
data[carwing.date][:trips] += 1
data[carwing.date][:consumption] += carwing.e_consumption
# etc
end

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