Added cost and cost_qm in job model
Changes to the migration file makes it necessary to reimport the database: db:drop db:create db:migrate db:seed
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user