diff --git a/app/models/job.rb b/app/models/job.rb index 8a520f0..eb2f999 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -10,6 +10,7 @@ class Job < ApplicationRecord before_save :update_printed_at, if: :will_save_change_to_status? before_save :update_paid_at, if: :will_save_change_to_status? before_save :update_status_changed_at, if: :will_save_change_to_status? + before_save :set_cost_qm # TODO: works only when job is created. Should move analyzer to activestorage :https://discuss.rubyonrails.org/t/active-storage-in-production-lessons-learned-and-in-depth-look-at-how-it-works/83289 after_create_commit :analyze_pdf @@ -86,18 +87,29 @@ class Job < ApplicationRecord self.status_changed_at = Time.now end + def calc_cost + self.cost = (number_of_plans_a0 * cost_qm) + (number_of_plans_a1 * cost_qm / 2) + + (number_of_plans_a2 * cost_qm / 4) + (number_of_plans_a3 * cost_qm / 8) + + (costum_qm_plan * cost_qm) + end + + def set_cost_qm + # TODO: get cost from global settings + self.cost_qm = 7 + end + def analyze_pdf # return unless pdf.attached? && pdf.new_record? + self.number_of_plans_a0 = 0 + self.number_of_plans_a1 = 0 + self.number_of_plans_a2 = 0 + self.number_of_plans_a3 = 0 + self.costum_qm_plan = 0 # TODO: add any check if attachment has changed # pdfs.each do |pdf| pdf.blob.open do |file| - self.number_of_plans_a0 = 0 - self.number_of_plans_a1 = 0 - self.number_of_plans_a2 = 0 - self.number_of_plans_a3 = 0 - self.costum_qm_plan = 0 # file = ActiveStorage::Blob.service.path_for(pdf.key).to_s pdf_analyzer = Services::PdfAnalyzer.new(file) pdf_analyzer.analyze @@ -106,8 +118,9 @@ class Job < ApplicationRecord self.number_of_plans_a2 += pdf_analyzer.pages_a2 self.number_of_plans_a3 += pdf_analyzer.pages_a3 self.costum_qm_plan += pdf_analyzer.costum_qm - save end # end + calc_cost + save end end diff --git a/app/views/jobs/_job_tr.html.erb b/app/views/jobs/_job_tr.html.erb index bef53dc..e14e24d 100644 --- a/app/views/jobs/_job_tr.html.erb +++ b/app/views/jobs/_job_tr.html.erb @@ -14,7 +14,8 @@ <%= job.number_of_plans_a1 %> <%= job.number_of_plans_a2 %> <%= job.number_of_plans_a3 %> - <%= job.costum_qm_plan.round(2) %> + <%= job.costum_qm_plan.round(2) %> m² + <%= job.cost.round(2) %> € <%= job.status %> diff --git a/app/views/jobs/index.html.erb b/app/views/jobs/index.html.erb index 0dfc387..25bed62 100644 --- a/app/views/jobs/index.html.erb +++ b/app/views/jobs/index.html.erb @@ -17,7 +17,8 @@ A1 A2 A3 - qm + noDIN + Kosten Status Actions diff --git a/db/migrate/20240727101347_create_jobs.rb b/db/migrate/20240727101347_create_jobs.rb index fee7ba4..d37e475 100644 --- a/db/migrate/20240727101347_create_jobs.rb +++ b/db/migrate/20240727101347_create_jobs.rb @@ -20,9 +20,11 @@ class CreateJobs < ActiveRecord::Migration[7.1] t.integer :number_of_plans_a2, default: 0 t.integer :number_of_plans_a3, default: 0 t.float :costum_qm_plan, default: 0 + t.float :cost, default: 0 + t.float :cost_qm, default: 0 t.boolean :privacy_policy_accepted, default: false t.timestamps end end -end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 918bd83..dbba104 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -59,6 +59,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_01_153403) do t.integer "number_of_plans_a2", default: 0 t.integer "number_of_plans_a3", default: 0 t.float "costum_qm_plan", default: 0.0 + t.float "cost", default: 0.0 + t.float "cost_qm", default: 0.0 t.boolean "privacy_policy_accepted", default: false t.datetime "created_at", null: false t.datetime "updated_at", null: false