Added has_many assoziations for creator and cashier to jobs, fixed all views according to that changes. Implemented allocation of roles infos when status changes in job model.

This commit is contained in:
2024-10-01 17:45:52 +02:00
parent de3aa07259
commit 5b60eb64ad
13 changed files with 212 additions and 69 deletions

View File

@@ -1,6 +1,10 @@
class Job < ApplicationRecord
belongs_to :operator, class_name: "User", optional: true, counter_cache: :jobs_as_operator_count
belongs_to :costumer, class_name: "User", optional: true, counter_cache: :jobs_as_costumer_count
attr_accessor :current_user
belongs_to :costumer, class_name: "User", optional: true, counter_cache: :jobs_as_costumer_count, inverse_of: :jobs_as_costumer
belongs_to :operator, class_name: "User", optional: true, counter_cache: :jobs_as_operator_count, inverse_of: :jobs_as_operator
belongs_to :creator, class_name: "User", optional: true, counter_cache: :created_jobs_count, inverse_of: :created_jobs
belongs_to :cashier, class_name: "User", optional: true, counter_cache: :cashed_jobs_count, inverse_of: :cashed_jobs
has_one_attached :pdf, dependent: :purge
@@ -11,11 +15,12 @@ class Job < ApplicationRecord
validate :acceptable_pdf
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 :update_user_status_infos, if: :will_save_change_to_status?
before_save :set_cost_qm
before_save :calc_cost, if: :printed_pages_changes?
before_validation :set_costumer_infos, unless: :created_by_operator?, on: :create
# TODO: works only when job is created. Should move analyzer to activestorage :
@@ -36,6 +41,10 @@ class Job < ApplicationRecord
AVAILABLE_PAGE_FORMATS = [ :a0, :a1, :a2, :a3 ]
# scope :created_as_operator, -> { where created_as_operator: true }
# scope :created_as_costumer, -> { where created_as_operator: false }
scope :not_canceled, -> { !canceled }
# NOTE: only named status are returned because of WHERE/IN clause for the enum values
scope :in_status_order, -> { in_order_of(:status, %w[open printing pickup paid canceled]) }
@@ -51,6 +60,8 @@ class Job < ApplicationRecord
scope :status_changed_on_day, lambda { |date|
where("status_changed_at >= ? AND status_changed_at <= ?", date.beginning_of_day, date.end_of_day)
}
scope :created_by_costumer, -> { not(:created_by_operator) }
# Returns all jobs with status: open print pickup and jobs from today with status: paid canceled
# paid: only updated_at today
# canceled: only updated_at today
@@ -111,19 +122,18 @@ class Job < ApplicationRecord
end
def self.report_to_csv(jobs)
columns = [ "id", "costumer_firstname", "costumer_lastname", "paid_at", "cost" ]
columns_readable = [ "ID", "Name", "Nachname", "bezahlt am", "Betrag" ]
columns_readable = [ "ID", "Kunde Vorname", "Kunde Nachname", "Kassierer Vorname", "Kassierer Nachname", "bezahlt am", "Betrag" ]
CSV.generate(col_sep: ";") do |csv|
csv << columns_readable
jobs.each do |job|
# csv << job.attributes.values_at(*columns)
csv << [ job.id, job.costumer_firstname, job.costumer_lastname, job.paid_at.localtime.strftime("%Y-%m-%d"), job.cost.to_s + "" ]
csv << [ job.id, job.costumer_firstname, job.costumer_lastname, job.cashier_firstname, job.cashier_lastname, job.paid_at.localtime.strftime("%Y-%m-%d"), job.cost.to_s + "" ]
end
end
end
def self.ransackable_attributes(auth_object = nil)
[ "created_at", "id", "costumer_firstname", "costumer_lastname", "pdf.", "number_of_plans_a0", "number_of_plans_a1", "number_of_plans_a2", "number_of_plans_a3", "costum_qm_plan", "cost", "status" ]
[ "created_at", "id", "costumer_firstname", "costumer_lastname", "pdf.", "created_by_operator", "number_of_plans_a0", "number_of_plans_a1", "number_of_plans_a2", "number_of_plans_a3", "costum_qm_plan", "cost", "status" ]
end
def self.ransackable_associations(auth_object = nil)
@@ -136,14 +146,6 @@ class Job < ApplicationRecord
costum_qm_plan_changed? || number_of_plans_a0_changed? || number_of_plans_a1_changed? || number_of_plans_a2_changed? || number_of_plans_a3_changed?
end
def update_printed_at
self.printed_at = Time.now if pickup? || (paid? && printed_at.nil?)
end
def update_paid_at
self.paid_at = Time.now if paid?
end
def update_status_changed_at
self.status_changed_at = Time.now
end
@@ -186,7 +188,67 @@ class Job < ApplicationRecord
end
def set_costumer_infos
self.costumer = current_user unless self.costumer
self.costumer_firstname = costumer.firstname
self.costumer_lastname = costumer.lastname
end
def set_operator_infos
self.operator = current_user unless self.operator
self.operator_firstname = operator.firstname
self.operator_lastname = operator.lastname
end
def clear_operator_infos
self.operator = nil
self.operator_firstname = nil
self.operator_lastname = nil
end
def set_cashier_infos
self.paid_at = Time.now
self.cashier = current_user unless self.cashier
self.cashier_firstname = cashier.firstname
self.cashier_lastname = cashier.lastname
end
def clear_cashier_infos
self.paid_at = nil
self.cashier = nil
self.cashier_firstname = nil
self.cashier_lastname = nil
end
def reset_operator_and_cashier_infos
clear_operator_infos
clear_cashier_infos
end
def update_user_status_infos
if status_changed?
case status.to_sym
when :open
reset_operator_and_cashier_infos
self.printed_at = nil
self.paid_at = nil
when :printing
clear_cashier_infos
set_operator_infos
self.printed_at = nil
when :pickup
clear_cashier_infos
self.printed_at = Time.now unless self.printed_at
when :paid
set_operator_infos unless self.operator
self.printed_at = Time.now unless self.printed_at
set_cashier_infos unless self.cashier
self.paid_at = Time.now unless self.paid_at
end
end
end
def reset_to_status_open?
status_changed? && open?
end
end