diff --git a/app/reflexes/admin/orders_reflex.rb b/app/reflexes/admin/orders_reflex.rb index b3145214a1..fe84ebf7ea 100644 --- a/app/reflexes/admin/orders_reflex.rb +++ b/app/reflexes/admin/orders_reflex.rb @@ -32,13 +32,20 @@ module Admin end def bulk_invoice(params) + visible_orders = editable_orders.where(id: params[:bulk_ids]).filter(&:invoiceable?) + if Spree::Config.enterprise_number_required_on_invoices? && + !all_distributors_can_invoice?(visible_orders) + render_business_number_required_error(visible_orders) + return + end + cable_ready.append( selector: "#orders-index", html: render(partial: "spree/admin/orders/bulk/invoice_modal") ).broadcast BulkInvoiceJob.perform_later( - params[:bulk_ids], + visible_orders.pluck(:id), "tmp/invoices/#{Time.zone.now.to_i}-#{SecureRandom.hex(2)}.pdf", channel: SessionChannel.for_request(request), current_user_id: current_user.id @@ -106,5 +113,19 @@ module Admin def set_param_for_controller params[:id] = @order.number end + + def all_distributors_can_invoice?(orders) + distributor_ids = orders.map(&:distributor_id) + Enterprise.where(id: distributor_ids, abn: nil).empty? + end + + def render_business_number_required_error(orders) + distributor_ids = orders.map(&:distributor_id) + distributor_names = Enterprise.where(id: distributor_ids, abn: nil).pluck(:name) + + flash[:error] = I18n.t(:must_have_valid_business_number, + enterprise_name: distributor_names.join(", ")) + morph_admin_flashes + end end end diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 30cd03ac3c..7a08a82974 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -584,10 +584,13 @@ describe ' reader.pages.map(&:text) end + let(:order4_selector){ "#order_#{order4.id} input[name='bulk_ids[]']" } + let(:order5_selector){ "#order_#{order5.id} input[name='bulk_ids[]']" } + shared_examples "can bulk print invoices from 2 orders" do it "bulk prints invoices in pdf format" do - page.find("#listing_orders tbody tr:nth-child(1) input[name='bulk_ids[]']").click - page.find("#listing_orders tbody tr:nth-child(2) input[name='bulk_ids[]']").click + page.find(order4_selector).click + page.find(order5_selector).click page.find("span.icon-reorder", text: "ACTIONS").click within ".ofn-drop-down .menu" do @@ -622,10 +625,126 @@ describe ' end end - it_behaves_like "can bulk print invoices from 2 orders" + shared_examples "should ignore the non invoiceable order" do + it "bulk prints invoices in pdf format" do + page.find(order4_selector).click + page.find(order5_selector).click - context "with legal invoices feature", feature: :invoices do + page.find("span.icon-reorder", text: "ACTIONS").click + within ".ofn-drop-down .menu" do + expect { + page.find("span", text: "Print Invoices").click # Prints invoices in bulk + }.to enqueue_job(BulkInvoiceJob).exactly(:once) + end + + expect(page).to have_content "Compiling Invoices" + expect(page).to have_content "Please wait until the PDF is ready " \ + "before closing this modal." + + perform_enqueued_jobs(only: BulkInvoiceJob) + + expect(page).to have_content "Bulk Invoice created" + + within ".modal-content" do + expect(page).to have_link(class: "button", text: "VIEW FILE", + href: /invoices/) + + invoice_content = extract_pdf_content + + expect(invoice_content).to have_content("TAX INVOICE", count: 1) + expect(invoice_content).to_not have_content(order4.number.to_s) + expect(invoice_content).to have_content(order5.number.to_s) + expect(invoice_content).to_not have_content(distributor4.name.to_s) + expect(invoice_content).to have_content(distributor5.name.to_s) + expect(invoice_content).to_not have_content(order_cycle4.name.to_s) + expect(invoice_content).to have_content(order_cycle5.name.to_s) + end + end + end + + context "ABN is not required" do + before do + allow(Spree::Config).to receive(:enterprise_number_required_on_invoices?) + .and_return false + end it_behaves_like "can bulk print invoices from 2 orders" + + context "with legal invoices feature", feature: :invoices do + it_behaves_like "can bulk print invoices from 2 orders" + end + + context "one of the two orders is not invoiceable" do + before do + order4.cancel! + end + + it_behaves_like "should ignore the non invoiceable order" + context "with legal invoices feature", feature: :invoices do + it_behaves_like "should ignore the non invoiceable order" + end + end + end + + context "ABN is required" do + before do + allow(Spree::Config).to receive(:enterprise_number_required_on_invoices?) + .and_return true + end + context "All the distributors setup the ABN" do + before do + order4.distributor.update(abn: "123456789") + order5.distributor.update(abn: "987654321") + end + context "all the orders are invoiceable (completed/resumed)" do + it_behaves_like "can bulk print invoices from 2 orders" + context "with legal invoices feature", feature: :invoices do + it_behaves_like "can bulk print invoices from 2 orders" + end + end + + context "one of the two orders is not invoiceable" do + before do + order4.cancel! + end + + it_behaves_like "should ignore the non invoiceable order" + context "with legal invoices feature", feature: :invoices do + it_behaves_like "should ignore the non invoiceable order" + end + end + end + context "the distributor of one of the order didn't set the ABN" do + before do + order4.distributor.update(abn: "123456789") + order5.distributor.update(abn: nil) + end + + shared_examples "should not print the invoice" do + it "should render a warning message" do + page.find(order4_selector).click + page.find(order5_selector).click + + page.find("span.icon-reorder", text: "ACTIONS").click + within ".ofn-drop-down .menu" do + expect { + page.find("span", text: "Print Invoices").click # Prints invoices in bulk + }.to_not enqueue_job(BulkInvoiceJob) + end + + expect(page).to_not have_content "Compiling Invoices" + expect(page).to_not have_content "Please wait until the PDF is ready " \ + "before closing this modal." + + expect(page).to have_content "#{ + order5.distributor.name + } must have a valid ABN before invoices can be used." + end + end + it_behaves_like "should not print the invoice" + context "with legal invoices feature", feature: :invoices do + it_behaves_like "should not print the invoice" + end + end end end