diff --git a/app/controllers/sdb/sample_manifests_controller.rb b/app/controllers/sdb/sample_manifests_controller.rb index 2aea301719..b34d6c136b 100644 --- a/app/controllers/sdb/sample_manifests_controller.rb +++ b/app/controllers/sdb/sample_manifests_controller.rb @@ -36,6 +36,8 @@ def index def show @study_id = @sample_manifest.study_id @samples = @sample_manifest.samples.paginate(page: params[:page]) + @barcode_types = Rails.application.config.tube_manifest_barcode_config[:barcode_type_labels].values.sort + @asset_type = @sample_manifest.asset_type end def new @@ -57,12 +59,14 @@ def create # rubocop:todo Metrics/AbcSize end end - def print_labels # rubocop:todo Metrics/MethodLength + def print_labels # rubocop:todo Metrics/MethodLength, Metrics/AbcSize print_job = LabelPrinter::PrintJob.new( params[:printer], LabelPrinter::Label::SampleManifestRedirect, - sample_manifest: @sample_manifest + sample_manifest: @sample_manifest, + label_template_name: label_template_for_2d_barcodes(@sample_manifest.asset_type), + barcode_type: params[:barcode_type] ) if print_job.execute flash[:notice] = print_job.success @@ -112,4 +116,11 @@ def validate_type redirect_to sample_manifests_path end end + + def label_template_for_2d_barcodes(asset_type) + if params[:barcode_type] == Rails.application.config.tube_manifest_barcode_config[:barcode_type_labels]['2d'] && + SampleManifest.tube_asset_types.include?(asset_type) + Rails.application.config.tube_manifest_barcode_config[:two_dimensional_label_template] + end + end end diff --git a/app/views/sdb/sample_manifests/show.html.erb b/app/views/sdb/sample_manifests/show.html.erb index dae20435ce..550404cf81 100644 --- a/app/views/sdb/sample_manifests/show.html.erb +++ b/app/views/sdb/sample_manifests/show.html.erb @@ -46,7 +46,14 @@ <%= label_tag :barcode_printer, 'Barcode printer' %> <%= render partial: "shared/printer_list" %> - + + <% if SampleManifest.tube_asset_types.include?(@asset_type) %> +
+ <%= label_tag :barcode_printer_type_id, 'Barcode type' %> + <%= select_tag :barcode_type, options_for_select(@barcode_types),class: "form-control select2" %> +
+ <% end %> +
<%= f.submit "Reprint all #{@sample_manifest.barcodes&.size || ''} labels", class: 'btn btn-success' %>
diff --git a/app/views/shared/_printer_list.html.erb b/app/views/shared/_printer_list.html.erb index 9008969a43..5d7a1a3d00 100644 --- a/app/views/shared/_printer_list.html.erb +++ b/app/views/shared/_printer_list.html.erb @@ -1,8 +1,8 @@
+ diff --git a/spec/controllers/sample_manifest_controller_spec.rb b/spec/controllers/sample_manifest_controller_spec.rb index f2a429602f..24f78851a7 100644 --- a/spec/controllers/sample_manifest_controller_spec.rb +++ b/spec/controllers/sample_manifest_controller_spec.rb @@ -55,6 +55,73 @@ end end + describe 'POST #print_labels' do + let!(:user) { create(:user, swipecard_code: '123456') } + + before do + user.grant_administrator + session[:user] = user.id + request.env['HTTP_REFERER'] = '/' + end + + context 'when printing labels for a plate manifest' do + let!(:sample_manifest) { create(:sample_manifest, asset_type: 'plate') } + let(:print_job) { instance_double(LabelPrinter::PrintJob) } + + before do + allow(LabelPrinter::PrintJob).to receive(:new).and_return(print_job) + allow(print_job).to receive_messages(execute: true, success: 'Printed') + end + + it 'prints successfully' do + post :print_labels, + params: { + id: sample_manifest.id, + printer: 'printer1' + } + + expect(flash[:notice]).to eq('Printed') + end + end + + context 'when printing labels for a tube manifest with 2D barcodes' do + subject(:make_request) do + post :print_labels, + params: { + id: sample_manifest.id, + printer: 'printer1', + barcode_type: '2D Barcode' + } + end + + let!(:sample_manifest) { create(:sample_manifest, asset_type: '1dtube') } + let(:print_job) { instance_double(LabelPrinter::PrintJob) } + + before do + allow(controller).to receive(:label_template_for_2d_barcodes).and_return('2d_template') + allow(LabelPrinter::PrintJob).to receive(:new).and_return(print_job) + allow(print_job).to receive_messages(execute: true, success: 'Printed') + make_request + end + + it 'passes the 2D label template to the print job' do + expect(LabelPrinter::PrintJob).to have_received(:new).with( + 'printer1', + LabelPrinter::Label::SampleManifestRedirect, + hash_including( + sample_manifest: sample_manifest, + label_template_name: '2d_template', + barcode_type: '2D Barcode' + ) + ) + end + + it 'prints successfully' do + expect(flash[:notice]).to eq('Printed') + end + end + end + def expect_correct_attributes(sample_manifest, study, supplier, purpose) # rubocop:todo Metrics/AbcSize expect(sample_manifest.study_id).to eq(study.id) expect(sample_manifest.supplier_id).to eq(supplier.id)