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

@@ -22,8 +22,7 @@ class Operator::JobsController < ApplicationController
def create
@job = Job.new(job_params)
@job.created_by_operator = true
# TODO: rename costumer to creater? When created by operator the operator is referenced instead of costumer.
@job.costumer = current_user
@job.creator = current_user
respond_to do |format|
if @job.save
@@ -40,6 +39,8 @@ class Operator::JobsController < ApplicationController
def update
@job.assign_attributes(job_params)
@status_changed = @job.status_changed?
@job.current_user = current_user if @status_changed
respond_to do |format|
if @job.save
broadcast_update_job

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

View File

@@ -1,7 +1,10 @@
class User < ApplicationRecord
has_secure_password
# has_many :jobs
has_many :jobs_as_costumer, foreign_key: :costumer_id, class_name: "Job"
has_many :jobs_as_operator, foreign_key: :operator_id, class_name: "Job"
has_many :created_jobs, foreign_key: :creator_id, class_name: "Job"
has_many :cashed_jobs, foreign_key: :cashier_id, class_name: "Job"
generates_token_for :email_verification, expires_in: 2.days do
email

View File

@@ -44,11 +44,20 @@
<%= l job.created_at.localtime.to_date %>
</td>
<td class="p-2 py-3 text-center">
<%= l job.paid_at.localtime.to_date if job.paid_at %>
</td>
<td class="p-2 py-3">
<span class="badge badge-status text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light">
<%= job.status %>
</span>
</td>
<% if job.created_by_operator %>
<%= link_to admin_user_path(job.creator) do %>
<span class="badge badge-status">Operator</span>
<% end %>
<% else %>
<%= link_to admin_user_path(job.creator) do %>
<span class="badge badge-status badge-hover">Kunden</span>
<% end %>
<% end %>
</span>
</td>
<td class="p-2 py-3">
<span class="badge badge-status text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light">
<%= job.status %>
</span>
</td>
</tr>

View File

@@ -15,6 +15,8 @@
<%= f.search_field :costumer_firstname_or_costumer_lastname_or_pdf_blob_filename_cont, placeholder: "Suchen", oninput: 'this.form.requestSubmit();' %>
<%= f.label :status_eq, "Status:" %>
<%= f.select :status_eq, Job.statuses.keys, {include_blank: "alle"}, onchange: 'this.form.requestSubmit();' %>
<%= f.label :created_by_operator_eq, "Erstellt vom:" %>
<%= f.select :created_by_operator_eq, [["Operator", true],["Kunden", false]], {include_blank: "alle"}, onchange: 'this.form.requestSubmit();' %>
<%= f.submit "Filter anwenden", class: "py-2 px-3 bg-hsrm-red hover:bg-hsrm-red-light shadow-lg text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
@@ -40,7 +42,7 @@
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :costum_qm_plan, "no DIN") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :cost, "Kosten") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Erstellt am") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Bezahlt am") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_by_operator, "Erstellt von") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :status, "Status") %></th>
</tr>
</thead>

View File

@@ -6,6 +6,10 @@
</p>
<p><%= user.email %></p>
<p>E-Mail Verifiziert:
<%= icon bool_icon(user.verified), class: "icon #{user.verified ? "text-green-600" : "text-red-600"}" %>
<p><%= user.created_at %></p>
</div>
<%= icon bool_icon(user.verified), class: "icon #{user.verified ? "text-green-600" : "text-red-600"}" %></p>
<p><%= user.created_at %></p>
<p>Druckaufträge als Kunde: <%= @user.jobs_as_costumer.size %></p>
<p>davon abgebrochen: <%= @user.jobs_as_cosutmer.canceled.size %></p>
<p>Druckaufträge als Operator: <%= @user.jobs_as_operator.size %></p>
<p>Druckaufträge kassiert: <%= @user.cashed_jobs.size %></p>
</div>

View File

@@ -18,7 +18,7 @@
<%= highlight user.email, [params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :email_start).to_s] %>
</td>
<td class="p-2 py-3 text-right">
<%= user.jobs_as_costumer.where.not(status: :canceled).size %>
<%= user.created_jobs.not_canceled.size %>
</td>
<td class="p-2 py-3 text-center">
<%= l user.created_at.localtime.to_date %>

View File

@@ -1,14 +1,8 @@
<div>
<h1 class="text-4xl font-bold">Benutzer Details</h1>
<h1 class="text-4xl font-bold text-hsrm-gray py-4">Benutzer Details</h1>
<%= render partial: 'user', locals: { user: @user } %>
</div>
<p>Some Stats:
<ul>
<li>Druckaufträge insgesammt: <%= @user.jobs_as_costumer.size %></li>
<li>davon abgebrochen: <%= @user.jobs_as_costumer.canceled.size %></li>
<li>letzten 5 Druckaufträge</li>
</ul>
</p>
<h1 class="py-4 text-4xl font-bold text-hsrm-gray">Die letzten 10 Druckaufträge</h1>
<div class="min-w-full overflow-auto shadow-lg">
<table class="w-full py-8 table-auto">
<thead class="font-semibold tracking-wide bg-gray-200 border-b-2 border-gray-300 text text-hsrm-gray">
@@ -27,7 +21,7 @@
</tr>
</thead>
<tbody id='jobs' class="divide-y divivde-gray-300">
<%= render partial: "jobs/job_tr", collection: @user.jobs_as_costumer.limit(5), as: :job, locals: { no_actions: true } %>
<%= render partial: "jobs/job_tr", collection: @user.created_jobs.order(created_at: :desc).limit(10), as: :job, locals: { no_actions: true } %>
</tbody>
</table>
</div>

View File

@@ -4,36 +4,58 @@
</div>
<div>
<p class="my-3">
<strong class="mb-1 font-medium">Operator ID:</strong>
<% if job.operator %>
<%= link_to_if allowed_to?(:show, job.operator, namespace: :Admin), "#{job.operatorr_id} - #{job.operator.name} (#{job.operator.email})", admin_user_path(job.operator) %>
<strong class="mb-1 font-medium">Costumer ID:</strong>
<% if job.costumer %>
<%= link_to_if allowed_to?(:show?, job.costumer, namespace: :Admin), "#{job.costumer_id} - #{job.costumer.name} (#{job.costumer.email})", admin_user_path(job.costumer) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Costumer ID:</strong>
<% if job.costumer %>
<%= link_to_if allowed_to?(:show, job.costumer, namespace: :Admin), "#{job.costumer_id} - #{job.costumer.name} (#{job.costumer.email})", admin_user_path(job.costumer) %>
<strong class="mb-1 font-medium">Creator ID:</strong>
<% if job.creator %>
<%= link_to_if allowed_to?(:show?, job.creator, namespace: :Admin, ), "#{job.creator_id} - #{job.creator.name} (#{job.creator.email})", admin_user_path(job.creator) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Operator ID:</strong>
<% if job.operator %>
<%= link_to_if allowed_to?(:show?, job.operator, namespace: :Admin), "#{job.operator_id} - #{job.operator.name} (#{job.operator.email})", admin_user_path(job.operator) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Cashier ID:</strong>
<% if job.cashier %>
<%= link_to_if allowed_to?(:show?, job.cashier, namespace: :Admin), "#{job.cashier_id} - #{job.cashier.name} (#{job.cashier.email})", admin_user_path(job.cashier) %>
<% else %>
-
<% end %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Kunde:</strong>
<%= link_to_if job.costumer && allowed_to?(:show? , job.costumer, namespace: :Admin), "#{job.costumer_firstname} #{job.costumer_lastname}", admin_user_path(job.costumer) %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Opertator:</strong>
<%= link_to_if job.operator && allowed_to?(:show, job.operator, namespace: :Admin), "#{job.operator_firstname} #{job.operator_lastname}", admin_users_path(job.operator) %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Kassierer:</strong>
<%= link_to_if job.cashier && allowed_to?(:show, job.cashier, namespace: :Admin), "#{job.cashier_firstname} #{job.cashier_lastname}", admin_users_path(job.cashier) %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Erstellt durch Opertator:</strong>
<%= icon bool_icon(job.created_by_operator), class: "icon #{job.created_by_operator ? "text-green-600" : "text-red-600"}" %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Auftraggeber:</strong>
<%= link_to_if job.costumer && !job.created_by_operator && allowed_to?(:show? , job.costumer, namespace: :Admin), "#{job.costumer_firstname} #{job.costumer_lastname}", admin_user_path(job.costumer) %>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Aktueller Status:</strong>
<%= %>
<span class="badge badge-status inline text-status-<%= job.status.to_sym %> bg-status-<%= job.status %>-light">
<%= job.status %>
</span>
</p>
<p class="my-3">
<strong class="mb-1 font-medium">Paid at:</strong>