-1

我已经打破了近两天的头,我找不到解决方案。

我有一张表格,列出发票编号、总额、已付金额和余额;它运作良好。现在我必须将所有行项目添加到表中。

invoices 表有以下列:of_lax、air_rate_customer、pss,所以这部分很简单,我只需要检查那里是否有值。

棘手的部分fcl_variable_cost_1_amount属性,我得到了其中的 8 个(fcl_variable_cost_1_amount、fcl_variable_cost_2_amount 等)。

有一个对应的fcl_variable_cost_1_amountfcl_variable_cost_1_charge_id 链接到一个 Charge 表。

因此,在一张发票中,fcl_variable_cost_1_id 可能为 34,金额为 100 美元(id 34 对应于“ISF”)。

在另一张发票中,fcl_variable_cost_1_id 可能是 12,金额为 50 美元(id 12 对应于“考试费”)。

那么如何使变量名称行项目出现在它们应该出现的列中?

这是我的代码。还有一个截图。在此处输入图像描述

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

1 回答 1

0

以备将来参考,以防有人遇到类似任务

<% 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>
于 2012-06-05T20:56:24.170 回答