I'm working on generating a report with multiple columns. Once the report is rendered, I need to allow users to input a value that will be subtracted from an existing column. With several rows of data, I've attempted the following approach in my views page:
<script>
function sum() {
var txtFirstNumberValue = document.getElementById('txt1').value;
var txtSecondNumberValue = document.getElementById('txt2').value;
var result = parseFloat(txtFirstNumberValue) - parseFloat(txtSecondNumberValue);
if(!isNaN(result)) {
document.getElementById('txt3').value = result; } }
</script>
<% @arr.each do |j| %>
<% @q =[] %>
<% t= 0 %>
<%= form_for ProductionReport.new ,:url=>{:controller=>"users",:action=>"rate_per_unit_report" } do |i| %>
<tr>
<td><%= j[0] %></td>
<td><%= j[1] %></td>
<td><%= j[2] %></td>
<% @q << j[3] %>
<td><%= i.text_field j[3], :value=>@q[t], :id=>"txt1", :onkeyup=>"sum()", :class=>"txt" %></td>
<td><%= i.text_field :selling_price, :id=>"txt2", :onkeyup=>"sum()", :class=>"txt" %></td>
<td><%= i.text_field :profit_loss, :id=>"txt3", :class=>"txt", :readonly =>true %></td>
<% end %>
<% t = t+1 %>
<% end %>
<td>total</td>
<td><%= @total %></td>
<td><%= @total1 %></td>
<td><%= @total2 %></td>
In my controller:
def rate_per_unit_report
@user=User.new
@user = User.find(session[:user_id]).name
@rpus = params[:production_report][:intial_date]
@rpue = params[:production_report][:final_date]
@production_report = ProductionReport.where(:date => @rpus..@rpue)
@production = @production_report.select(:finished_goods_name).uniq
@arr=[]
g = 0
@production.each do|i|
@p = @production_report.pluck(:issue_id)
@ll = @production_report.where(:finished_goods_name=>i.finished_goods_name).select(:total_no_of_items_produced).sum :total_no_of_items_produced
@k = Issue.where(:id=>@p).pluck(:consolidated_cost)
@rate = @k[g].to_f / @ll.to_f
@r = @rate.round(2)
@arr<<[i.finished_goods_name]+[@ll]+ [@k[g]] + [@r]
g = g+1
end
@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10647f64717c2d507162623e797e7a757364">[email protected]</a>(0){|sum,x| sum + x[1].to_i }
@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52263d26333e636f123320207c3b3c38373126">[email protected]</a>(0){|sum,y| sum + y[2].to_i }
@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01756e75606d333c416073732f686f6b646275">[email protected]</a>(0){|sum,z| sum + z[3].to_i }
end
The sample output page displays: []
To subtract the fifth column from the fourth column, I am looking for guidance on how to achieve this.
If further information is needed, please don't hesitate to let me know. Thank you.