Commit c532c3e2 authored by Sara Kuqi's avatar Sara Kuqi
Browse files

Result and Diference calculations complete, pdf report with prawn

parent 11358561
......@@ -9,7 +9,9 @@ gem 'bcrypt','~>3.1.11'
gem 'will_paginate', '~> 3.1.0'
gem 'bootstrap-will_paginate', '~> 0.0.10'
gem 'font-awesome-rails'
gem 'sablon'
gem 'prawn'
gem 'prawn-table'
gem 'unicorn'# gem 'therubyracer', platforms: :ruby
gem "cocoon"
gem "json"
......
......@@ -94,8 +94,13 @@ GEM
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
pdf-core (0.6.1)
pg (0.18.4)
pkg-config (1.1.7)
prawn (2.1.0)
pdf-core (~> 0.6.1)
ttfunk (~> 1.4.0)
prawn-table (0.1.2)
puma (3.4.0)
rack (2.0.1)
rack-test (0.6.3)
......@@ -128,6 +133,10 @@ GEM
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rubyzip (1.2.0)
sablon (0.0.20)
nokogiri (>= 1.6.0)
rubyzip (>= 1.1)
sass (3.4.22)
sass-rails (5.0.5)
railties (>= 4.0.0, < 6)
......@@ -149,6 +158,7 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
ttfunk (1.4.0)
turbolinks (5.0.0)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
......@@ -185,8 +195,11 @@ DEPENDENCIES
json
listen (~> 3.0.5)
pg
prawn
prawn-table
puma (~> 3.0)
rails (~> 5.0.0)
sablon
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
......
......@@ -245,5 +245,4 @@ div.absolutem {
right: 0;
width: 115px;
}
......@@ -69,6 +69,16 @@ end
else
@sample = current_user.samples.find(params[:id])
end
respond_to do |format|
format.html
format.js
format.pdf do
pdf = SamplePdf.new(@sample)
send_data pdf.render, filename: "sample_#{@sample.id}.pdf",
type:"application/pdf",
disposition: "inline"
end
end
end
def destroy
......@@ -99,7 +109,7 @@ end
def sample_params
keys=Sample::ELEMENTS.keys
params.require(:sample).permit(:name, :formula, results_attributes: [:id, :_destroy, :element_C,:element_H,:element_N,:element_O])
params.require(:sample).permit(:name, :formula, results_attributes: [:id, :_destroy, :element_C,:element_H,:element_N,:element_S])
end
def correct_user
......
......@@ -28,41 +28,40 @@ class Sample < ApplicationRecord
result
end
def parse_formula
left_over = formula
@formul={}
while !left_over.blank?
left_over.match(REG)
return false unless $1
left_over = $'
if $2 == ""
then value = 1
else
value = $2.to_i
end
@formul[$1] = value
def parse_formula
left_over = formula
@formul={}
while !left_over.blank?
left_over.match(REG)
return false unless $1
left_over = $'
if $2 == ""
then value = 1
else
value = $2.to_i
end
@formul[$1] = value
end
@formul
end
@formul
end
def calculate_mw
@mw=0
@formul.each_pair do |key, value|
puts "#{key} is #{value}"
@mw += get_atomic_weight(key)*value
puts "#{key} is #{value}"
@mw += get_atomic_weight(key)*value
end
@mw
end
@mw
end
def calculate_percentage
@expected_result={}
unless parse_formula
errors.add('Warning','Formula is not valid')
throw(:abort)
end
calculate_mw
@formul.each_pair{|k,v| @expected_result[k]=((v*get_atomic_weight(k)/@mw)*100).to_f}
assign_attributes(calculation: @expected_result)
def calculate_percentage
@expected_result={}
unless parse_formula
errors.add('Warning','Formula is not valid')
throw(:abort)
end
calculate_mw
@formul.each_pair{|k,v| @expected_result[k]=((v*get_atomic_weight(k)/@mw)*100).to_f}
assign_attributes(calculation: @expected_result)
end
end
require "prawn/table"
class SamplePdf < Prawn::Document
def initialize(sample)
super(top_margin: 50)
@sample = sample
text "id: \ #{@sample.id}
name: \ #{@sample.name}
formula:\ #{@sample.formula}
status: \ #{@sample.status}", size: 15, style: :bold
move_down 20
table(table_data,width: bounds.width)
move_down 100
text I18n.l(DateTime.now.to_date, format: "%d %B %Y").to_s
move_down 30
text "Signature_____________________"
end
def table_header
["Elements:","C","N","H","S","Other","Total"]
end
def table_rows
output = [expected]
@sample.results.each_with_index do |result,i|
#update = [[res],difference]]
r = ["Result #{i+1}:"]
r += res(result)
output << r
output += [difference(result)]
end
output
end
def table_data
[table_header] + table_rows
end
def expected
total = 0
i = 0
output = ["Expected:"]
@sample.formatted_calculation.each_pair do |k,v|
if i<4
total=total.to_f+v.to_f
i=i+1
output += ["%.2f" %v]
end
end if @sample.calculation
other = 100 - total
other.abs.round(2)
total += other
output += ["%.2f" %other, total]
end
def res(result)
update = []
values = ['C','N','H','S'].map do |a|
result.send('element_'+a).to_f
end
sum = values.inject(0){|sum,x| sum + x }
update += values.map{|x| "%.2f" % x}
update +=[""]
( result.measurement.keys - ["C","H","N","S"]).each do |k|
if result.measurement[k] then "%.2f" % result.measurement[k].to_f else '' end
update += ["%.2f" %sum=sum+result.measurement[k].to_f]
end if result.measurement
update +=["%.2f" %sum]
end
def difference(result)
update = ["Difference:"]
diff={}
Sample::PREDEF_ELEMENTS.each do |k|
v = @sample.calculation[k]
diff[k] = percentage_diff(v,result.send('element_'+k))
end if @sample.calculation
update +=["%.2f" %diff['C']]
update +=["%.2f" %diff['N']]
update +=["%.2f" %diff['H']]
update +=["%.2f" %diff['S']]
update +=[""]
update +=[""]
@sample.formatted_calculation.each_pair do |k,v|
( result.measurement.keys - ["C","H","N","S"]).each do |k|
( (v.to_f - result.measurement[k].to_f) / ((v.to_f + result.measurement[k].to_f)/2) ) * 100
end
end
update
end
def percentage_diff(expected,measured)
e = expected.to_f
m = measured.to_f
if m == e
0.0
else
(m - e).abs
end
end
end
......@@ -3,6 +3,6 @@
<td><%= f.text_field :element_C %></td>
<td><%= f.text_field :element_N %></td>
<td><%= f.text_field :element_H %></td>
<td><%= f.text_field :element_O %></td>
<td><%= f.text_field :element_S %></td>
</tr>
......@@ -125,14 +125,14 @@ $(document).ready(function() {
<% @sample.results.each do |result| %> <tr>
<td>Result <%= i %> : </td>
<td><%= result.element_C %> </td>
<td><%= result.element_C %> </td>
<%sum2=sum2.to_f+result.element_C.to_f %>
<td><%= result.element_N %> </td>
<% sum2=sum2.to_f+result.element_H.to_f %>
<td><%= result.element_H %></td>
<% sum2=sum2.to_f+result.element_N.to_f %>
<td><%= result.element_O %></td>
<% sum2=sum2.to_f+result.element_O.to_f %>
<td><%= result.element_S %></td>
<% sum2=sum2.to_f+result.element_S.to_f %>
<%( result.measurement.keys - ["C","H","N","O"]).each do |k| %>
<td> <%= if result.measurement[k] then "%.2f" % result.measurement[k].to_f else '' end%></td>
<% sum2=sum2.to_f+result.measurement[k].to_f %>
......@@ -142,16 +142,15 @@ $(document).ready(function() {
<td><%= sum2%></td>
<%sum2=0%>
<%# end if result
+result.element_C.to_i+result.element_H.to_i+result.element_N.to_i +result.element_O.to_i
+result.element_C.to_i+result.element_H.to_i+result.element_N.to_i +result.element_S.to_i
%>
</tr>
<tr>
<td> Difference (%) </td>
<%diff={}%>
<%@sample.formatted_calculation.each_pair do |k,v| %>
<% kk = (k == 'S' && 'O') || k%>
<% diff[k] = percentage_diff(v,result.send('element_'+kk)) %>
<%Sample::PREDEF_ELEMENTS.each do |k| %>
<% v = @sample.calculation[k]%>
<% diff[k] = percentage_diff(v,result.send('element_'+k)) %>
<%end if @sample.calculation %>
<td class="y_n"><%=diff['C'].abs.round(2)%></td>
......@@ -159,7 +158,7 @@ $(document).ready(function() {
<td class="y_n"><%=diff['H'].abs.round(2)%></td>
<td class="y_n"><%=diff['S'].abs.round(2)%></td>
<%@sample.formatted_calculation.each_pair do |k,v| %>
<%( result.measurement.keys - ["C","H","N","O"]).each do |k| %>
<%( result.measurement.keys - ["C","H","N","S"]).each do |k| %>
<%=( (v.to_f - result.measurement[k].to_f) / ((v.to_f + result.measurement[k].to_f)/2) ) * 100 %>
<%end %>
<%end %>
......@@ -171,3 +170,4 @@ $(document).ready(function() {
</table>
</tbody>
</table>
<p><%=link_to "Printable Receipt (PDF)", sample_path(@sample, format: "pdf") %><p>
# Be sure to restart your server when you modify this file.
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register "application/pdf", :pdf
......@@ -2,10 +2,10 @@ class CreateResults < ActiveRecord::Migration[5.0]
def change
create_table :results do |t|
t.integer :sample_id
t.float :element_C
t.float :element_N
t.float :element_O
t.float :element_H
t.float :element_C, default: 0
t.float :element_N, default: 0
t.float :element_S, default: 0
t.float :element_H, default: 0
t.string :name
t.timestamps
......
......@@ -18,14 +18,13 @@ ActiveRecord::Schema.define(version: 20160803065947) do
create_table "results", force: :cascade do |t|
t.integer "sample_id"
t.float "element_C"
t.float "element_N"
t.float "element_O"
t.float "element_H"
t.float "element_C", default: 0.0
t.float "element_N", default: 0.0
t.float "element_S", default: 0.0
t.float "element_H", default: 0.0
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.hstore "measurement"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "samples", force: :cascade do |t|
......
......@@ -4,7 +4,7 @@ one:
sample_id: 1
element_C: 1.5
element_N: 1.5
element_O: 1.5
element_S: 1.5
element_H: 1.5
name: MyString
......@@ -12,6 +12,6 @@ two:
sample_id: 1
element_C: 1.5
element_N: 1.5
element_O: 1.5
element_S: 1.5
element_H: 1.5
name: MyString
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment