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 @@