ruby - Rails 3. How to build a table from invoice's line items and the line item names vary? -
i have been breaking head 2 days , can't find solution.
i have table lists invoices invoice number, total, amount paid , balance; , works well. have add line items table.
the invoices table has columns: of_lax, air_rate_customer, pss, part it's easy, have check if there value there.
the tricky part fcl_variable_cost_1_amount attributes, got 8 of (fcl_variable_cost_1_amount, fcl_variable_cost_2_amount, etc).
the fcl_variable_cost_1_amount has corresponding fcl_variable_cost_1_charge_id links charge tables.
so in 1 invoice fcl_variable_cost_1_id might 34 , amount $100 (id 34 corresponds "isf").
in invoice fcl_variable_cost_1_id might 12 , amount $50 (id 12 corresponds "examination fee").
so how can make variable name line items appear in column should?
this code. there screenshot. 
<% headings = hash.new %> <% @shipments.each |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 <%= @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 |k,v|%> <th><%= v %></th> <% end %> <th>invoice total</th> <th>amount paid</th> <th>balance</th> </tr> <% @shipments.each |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 |k,v| %> <% if k == "of_lax" , !shipment.invoice.of_lax.nil? %> <td><%= number_to_currency shipment.invoice.lcl_of_customer_total %> <% elsif k == "of_customer_amount_for_customer_inv" , !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" , !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" , !shipment.invoice.pss.nil? %> <td><%= number_to_currency (shipment.invoice.pss.to_s.to_d * shipment.volweight) %></td> <% elsif k == "hc_lax" , !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 runs similar task
<% headings = hash.new %> <% @shipments.each |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 <%= @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 |k,v|%> <th><%= v %></th> <% end %> <th>invoice total</th> <th>amount paid</th> <th>balance</th> </tr> <% @shipments.each |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 |k,v| %> <% if k == "of_lax" , !shipment.invoice.of_lax.nil? %> <td><%= number_to_currency shipment.invoice.lcl_of_customer_total %> <% elsif k == "of_customer_amount_for_customer_inv" , !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" , !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" , !shipment.invoice.pss.nil? %> <td><%= number_to_currency (shipment.invoice.pss.to_s.to_d * shipment.volweight) %></td> <% elsif k == "hc_lax" , !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>
Comments
Post a Comment