From f4d4fdce9425c5da464274a7ee0febea96682f66 Mon Sep 17 00:00:00 2001 From: bohung Date: Tue, 6 Oct 2020 12:16:53 +0800 Subject: [PATCH] Fix bugs. --- app/models/venue_management_bill.rb | 6 ++ .../venue_management_bills/_form.html.erb | 62 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/models/venue_management_bill.rb b/app/models/venue_management_bill.rb index 5189270..6f2ffa8 100644 --- a/app/models/venue_management_bill.rb +++ b/app/models/venue_management_bill.rb @@ -12,6 +12,9 @@ class VenueManagementBill 'announced_land_values'=>{'land_values'=>{},'taxable_area'=>{'unit'=>'㎡'},'taxable_tax'=>{}}, 'other'=>{'amount'=>{}}} CACILATYIONFORMULA = {'revenue'=>" + i = find_insert_index(field_values,revenue) + field_values = field_values.slice(0,i) + percents = percents.slice(0,i) field_values.push(revenue) percents.push(0) result = -inner_product(field_values,percents) @@ -22,6 +25,9 @@ class VenueManagementBill if(result < 0) result = 0", "degree"=>" + i = find_insert_index(field_values,consumption) + field_values = field_values.slice(0,i) + dollardegrees = dollardegrees.slice(0,i) field_values.push(consumption) dollardegrees.push(0) result = -inner_product(field_values,dollardegrees) diff --git a/app/views/admin/venue_management_bills/_form.html.erb b/app/views/admin/venue_management_bills/_form.html.erb index 23254a0..5317342 100644 --- a/app/views/admin/venue_management_bills/_form.html.erb +++ b/app/views/admin/venue_management_bills/_form.html.erb @@ -206,7 +206,7 @@ %> var cbt_html = <%=cbt_html.to_s.gsub('=>',':').html_safe %>; //var cbt_init_func = <%#=cbt.map{|k,v| [k,v.map{|kk,vv| (vv["fields"].blank? rescue true) ? ("#{kk}=0") : (vv["fields"].map{|field| "#{field.gsub('/','')}s=[]"}.join(";")) }.join(";")]}.to_h.to_s.gsub('=>',':').html_safe %>; - <%=cbt.map{|k,v| v.map{|kk,vv| (vv["fields"].blank? rescue true) ? ("#{kk}=0") : (vv["fields"].map{|field| "#{field.gsub('/','')}s=[]"}.join(";")) }.join(";")}.join(";").to_s.gsub('=>',':').html_safe %> + <%=cbt.map{|k,v| v.map{|kk,vv| (vv["fields"].blank? rescue true) ? ("#{kk}=0") : (vv["fields"].map{|field| "#{field.gsub('/','')}s={}"}.join(";")) }.join(";")}.join(";").to_s.gsub('=>',':').html_safe %> $("#caculation_basis_type").on('change',function(){ $("#caculation_basis_content").html(cbt_html[this.value]); update_result(); @@ -214,6 +214,8 @@ update_result(); }) }) + <% cbt_fields = cbt.map{|k,v| [k,v.flat_map{|kk,vv| vv['fields'].to_a.map{|f| f.gsub('/','')+"s"}}.select{|f| f != "s"}.uniq] }.to_h %> + var cbt = <%= cbt_fields.to_s.gsub('=>',':').html_safe%>; function inner_product(a,b){ var result = 0; $.each(a,function(i,v){ @@ -224,11 +226,22 @@ }) return result; } + function find_insert_index(arr,val){ + var index = 0; + arr.forEach(function(v){ + if(val >= v){ + index++; + }else{ + return index; + } + }) + console.log(index); + return index; + } function update_result(){ var formula = CACILATYIONFORMULA[calculate_type].replace('f[0]','f'); - console.log(calculate_type); //(function(){eval.call(this,cbt_init_func[calculate_type])})(); - <%=cbt.map{|k,v| v.map{|kk,vv| (vv["fields"].blank? rescue true) ? ("#{kk}=0") : (vv["fields"].map{|field| "#{field.gsub('/','')}s=[]"}.join(";")) }.join(";")}.join(";").to_s.gsub('=>',':').html_safe %> + <%=cbt.map{|k,v| v.map{|kk,vv| (vv["fields"].blank? rescue true) ? ("#{kk}=0") : (vv["fields"].map{|field| "#{field.gsub('/','')}s={}"}.join(";")) }.join(";")}.join(";").to_s.gsub('=>',':').html_safe %> $("#caculation_basis_content input").each(function(i,input){ var name = input.name; name = name.replace('venue_management_bill',''); @@ -236,14 +249,50 @@ name = name.replace('[','').replace(']',''); eval.call(this,''+name+"="+Number(input.value).toString()) }else{ + field_name = name.split("[").slice(1)[0].replace(']',''); name = name.split("[").slice(1).last().replace(']','') + 's'; name = name.replace("/","") if(input.value != "") - eval.call(this,''+name+".push("+Number(input.value).toString()+")") + eval.call(this,name)[field_name] = Number(input.value); + else + eval.call(this,name)[field_name] = null; } }) + var ks = cbt[calculate_type]; + var value_name = cbt[calculate_type][0]; + <%= ('switch (calculate_type) { + '+ + cbt_fields.map{|k,fields| + (fields[0].blank? ? "" : "case '#{k}': + if(#{fields[0].blank? ? "undefined" : fields[0]} != undefined){"+ + fields[1..-1].to_a.map{|f| + "$.each(#{fields[0]},function(k,v){ + if(v == null){ + delete #{f}[k]; + } + }) + #{f} = Object.values(#{f}); + " + }.join(";") + + "var fields_to_delete = []; + $.each(#{fields[0]},function(k,v){ + if(v == null){ + fields_to_delete.push(k); + } + }) + fields_to_delete.forEach(function(f){ + delete #{fields[0]}[f]; + }) + #{fields[0]} = Object.values(#{fields[0]});"+ + "} + break; + ") + }.select{|s| s.present?}.join(" ")+ + 'default:'+ + 'console.log("error"); + break;}').html_safe + %> eval.call(this,formula); - console.log(result); if($("#tax_excluded").prop("checked")){ $("#total_amount").val(Math.round(result*1.05)); $("#actual_amount").val(Math.round(result)); @@ -266,4 +315,7 @@ $("#caculation_basis_content input").off("input").on("input",function(){ update_result(); }) + $(document).ready(function(){ + update_result(); + }) \ No newline at end of file