diff --git a/app/controllers/operator/jobs_controller.rb b/app/controllers/operator/jobs_controller.rb index 7e976c1..d058f32 100644 --- a/app/controllers/operator/jobs_controller.rb +++ b/app/controllers/operator/jobs_controller.rb @@ -1,5 +1,5 @@ class Operator::JobsController < ApplicationController - before_action :set_job, only: %i[show edit update destroy cancel] + before_action :set_job, only: %i[show edit update destroy cancel increment_page decrement_page] # GET /jobs or /jobs.json def index @@ -29,9 +29,9 @@ class Operator::JobsController < ApplicationController respond_to do |format| if @job.save flash[:notice] = "Job was successfully created." - Turbo::StreamsChannel.broadcast_prepend_later_to "jobs", target: :jobs, partial: "jobs/job_tr", + Turbo::StreamsChannel.broadcast_prepend_later_to "jobs", target: :jobs, partial: "operator/jobs/job_tr", locals: { job: @job } - format.html { redirect_to jobs_url } + format.html { redirect_to operator_jobs_url } else format.html { render :new, status: :unprocessable_entity } end @@ -43,7 +43,7 @@ class Operator::JobsController < ApplicationController respond_to do |format| if @job.update(job_params) broadcast_update_job - format.html { redirect_to jobs_url, notice: "Job was successfully updated." } + format.html { redirect_to operator_jobs_url, notice: "Job was successfully updated." } else format.html { render :edit, status: :unprocessable_entity } end @@ -60,7 +60,7 @@ class Operator::JobsController < ApplicationController respond_to do |format| broadcast_update_job format.turbo_stream - format.html { redirect_to jobs_url } + format.html { redirect_to operator_jobs_url } end end @@ -70,7 +70,23 @@ class Operator::JobsController < ApplicationController respond_to do |format| Turbo::StreamsChannel.broadcast_remove_to "jobs", target: @job - format.html { redirect_to jobs_url, notice: "Job was successfully destroyed." } + format.html { redirect_to operator_jobs_url, notice: "Job was successfully destroyed." } + end + end + + def increment_page + @job.increment_page(params[:din]) + + respond_to do |format| + format.html { redirect_to operator_jobs_url, notice: "Job was successfully updated." } + end + end + + def decrement_page + @job.decrement_page(params[:din]) + + respond_to do |format| + format.html { redirect_to operator_jobs_url, notice: "Job was successfully updated." } end end @@ -87,7 +103,7 @@ class Operator::JobsController < ApplicationController # Only allow a list of trusted parameters through. def job_params - params.require(:job).permit(:operator_id, :costumer_id, :opertator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname) + params.require(:job).permit(:operator_id, :costumer_id, :operator_firstname, :operator_lastname, :costumer_firstname, :costumer_lastname) end end diff --git a/app/models/job.rb b/app/models/job.rb index 112b482..9050b5c 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -5,18 +5,23 @@ class Job < ApplicationRecord has_one_attached :pdf, dependent: :purge validates_presence_of :costumer_firstname, :costumer_lastname, :privacy_policy_accepted, :pdf + # validates_numericality_of {:number_of_plans_a0,:number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3}, greater_than_or_equal_to: 0 + validates :number_of_plans_a0, :number_of_plans_a1, :number_of_plans_a2, :number_of_plans_a3, numericality: { greater_than_or_equal_to: 0 } + + 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 :set_cost_qm + before_save :calc_cost, if: :printed_pages_changes? # 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 # NOTE: Multiple status if paing before brinting? - enum status: { + enum :status, { open: 0, printing: 1, pickup: 2, @@ -24,6 +29,8 @@ class Job < ApplicationRecord canceled: 4 } + AVAILABLE_PAGE_FORMATS = [ :a0, :a1, :a2, :a3 ] + # 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]) } @@ -86,8 +93,24 @@ class Job < ApplicationRecord end end + def increment_page(din) + return false unless AVAILABLE_PAGE_FORMATS.include?(din.to_sym) + public_send("number_of_plans_#{din}=", public_send("number_of_plans_#{din}") + 1) if respond_to? "number_of_plans_#{din}=" + save + end + + def decrement_page(din) + return false unless AVAILABLE_PAGE_FORMATS.include?(din.to_sym) + public_send("number_of_plans_#{din}=", public_send("number_of_plans_#{din}") - 1) if respond_to? "number_of_plans_#{din}=" + save + end + private + def printed_pages_changes? + costum_qm_plan_previously_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 diff --git a/app/views/operator/jobs/_job_tr.html.erb b/app/views/operator/jobs/_job_tr.html.erb index 6f6049a..1108e77 100644 --- a/app/views/operator/jobs/_job_tr.html.erb +++ b/app/views/operator/jobs/_job_tr.html.erb @@ -6,7 +6,7 @@ <% if job.pdf.attached? %> - <%= link_to_if job.printing?, image_tag(url_for(job.pdf.preview(resize_to_limit: [100, 100]))), job.pdf, download:true, class: "shadow-lg" %> + <%= link_to_if job.printing?, image_tag(url_for(job.pdf.preview(resize_to_limit: [100, 100]))), job.pdf, target: "_blank", class: "shadow-lg" %> <% end %> @@ -16,42 +16,31 @@ <% if job.pdf.attached? %> <%#= link_to job.pdf.filename, rails_blob_path(job.pdf, disposition: "attachment") %> - <%= link_to job.pdf.filename, job.pdf, download:true %> + <%= link_to_if job.printing?, job.pdf.filename, job.pdf, download:true %> <%=number_to_human_size job.pdf.blob.byte_size%> <% end %> - - <% if job.printing? %> - - <%= button_to icon("chevron-up", class: "size-5 inline", title: "erhöhen"), operator_job_path(job), method: :patch, form_class: "inline" %> - - <% end %> - - <%= job.number_of_plans_a0 %> - - <% if job.printing? %> - - <%= button_to icon("chevron-down", class: "size-5 inline", title: "verringern"), operator_job_path(job), method: :patch, form_class: "inline" %> - - <% end %> - - - - <%= job.number_of_plans_a1 %> - - - - - <%= job.number_of_plans_a2 %> - - - - - <%= job.number_of_plans_a3 %> - - + <% Job::AVAILABLE_PAGE_FORMATS.each do |din| %> + + <% if job.printing? %> +
+ + <%= button_to icon("chevron-up", class: "size-5 inline", title: "erhöhen"), increment_page_operator_job_path(job, din:), method: :patch, form_class: "inline" %> + + <% end %> + + <%= job.public_send("number_of_plans_#{din}") if job.respond_to? "number_of_plans_#{din}" %> + + <% if job.printing? %> + + <%= button_to icon("chevron-down", class: "size-5 inline", title: "verringern"), decrement_page_operator_job_path(job, din:), method: :patch, form_class: "inline" %> + +
+ <% end %> + + <% end %> <%= job.costum_qm_plan.round(2) %> m² diff --git a/config/routes.rb b/config/routes.rb index 6991fff..7dcf8d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,8 @@ Rails.application.routes.draw do patch "pickup" patch "printing" patch "paid" + patch "increment_page" + patch "decrement_page" end end end