From 3a1fde40c537aa9e9b2e8d11a44c384098f8b106 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 23 Jun 2022 11:20:20 +0200 Subject: [PATCH 1/3] Add a Spree config option: enterprise_number_required_on_invoices --- app/controllers/admin/invoice_settings_controller.rb | 1 + app/models/spree/app_configuration.rb | 1 + app/views/admin/invoice_settings/edit.html.haml | 5 +++++ config/locales/en.yml | 1 + 4 files changed, 8 insertions(+) diff --git a/app/controllers/admin/invoice_settings_controller.rb b/app/controllers/admin/invoice_settings_controller.rb index 36548e7df3..c0005fd9bf 100644 --- a/app/controllers/admin/invoice_settings_controller.rb +++ b/app/controllers/admin/invoice_settings_controller.rb @@ -19,6 +19,7 @@ module Admin :enable_invoices?, :invoice_style2?, :enable_receipt_printing?, + :enterprise_number_required_on_invoices?, ) end end diff --git a/app/models/spree/app_configuration.rb b/app/models/spree/app_configuration.rb index dccce80ce3..66d26c2553 100644 --- a/app/models/spree/app_configuration.rb +++ b/app/models/spree/app_configuration.rb @@ -129,6 +129,7 @@ module Spree preference :enable_invoices?, :boolean, default: true preference :invoice_style2?, :boolean, default: false preference :enable_receipt_printing?, :boolean, default: false + preference :enterprise_number_required_on_invoices?, :boolean, default: true # Stripe payments preference :stripe_connect_enabled, :boolean, default: false diff --git a/app/views/admin/invoice_settings/edit.html.haml b/app/views/admin/invoice_settings/edit.html.haml index 85b53999df..3852b53f66 100644 --- a/app/views/admin/invoice_settings/edit.html.haml +++ b/app/views/admin/invoice_settings/edit.html.haml @@ -20,5 +20,10 @@ = check_box_tag 'preferences[enable_receipt_printing?]', '1', Spree::Config[:enable_receipt_printing?] = label_tag nil, t('.enable_receipt_printing?') + .field.align-center + = hidden_field_tag 'preferences[enterprise_number_required_on_invoices?]', '0' + = check_box_tag 'preferences[enterprise_number_required_on_invoices?]', '1', Spree::Config[:enterprise_number_required_on_invoices?] + = label_tag nil, t('.enterprise_number_required_on_invoices?') + .form-buttons{"data-hook" => "buttons"} = button t(:update), 'icon-refresh' diff --git a/config/locales/en.yml b/config/locales/en.yml index f86de538be..30a55105aa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -552,6 +552,7 @@ en: enable_invoices?: "Enable Invoices?" invoice_style2?: "Use the alternative invoice model that includes total tax breakdown per rate and tax rate info per item (not yet suitable for countries displaying prices excluding tax)" enable_receipt_printing?: "Show options for printing receipts using thermal printers in order dropdown?" + enterprise_number_required_on_invoices?: "Require an ABN to generate an invoice?" stripe_connect_settings: edit: From bb6298d06d5867a331de06f820826c7bba86b7b6 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 23 Jun 2022 11:21:43 +0200 Subject: [PATCH 2/3] Check for enterprise_number_required_on_invoices boolean if true, then can edit invoice ; if false, then print a confirm alert popup to specify that a enterprise number must be set for this enterprise --- app/helpers/spree/admin/orders_helper.rb | 17 ++++++++++++++++- app/models/enterprise.rb | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/helpers/spree/admin/orders_helper.rb b/app/helpers/spree/admin/orders_helper.rb index d70560abad..37a4fb45cb 100644 --- a/app/helpers/spree/admin/orders_helper.rb +++ b/app/helpers/spree/admin/orders_helper.rb @@ -44,6 +44,14 @@ module Spree end end + def print_invoice_link + if @order.distributor.can_invoice? + print_invoice_link_with_url + else + notify_about_required_enterprise_number + end + end + def ticket_links return [] unless Spree::Config[:enable_receipt_printing?] @@ -78,13 +86,20 @@ module Spree confirm: t(:must_have_valid_business_number, enterprise_name: @order.distributor.name) } end - def print_invoice_link + def print_invoice_link_with_url { name: t(:print_invoice), url: spree.print_admin_order_path(@order), icon: 'icon-print', target: "_blank" } end + def notify_about_required_enterprise_number + { name: t(:print_invoice), + url: "#", + icon: 'icon-print', + confirm: t(:must_have_valid_business_number, enterprise_name: @order.distributor.name) } + end + def print_ticket_link { name: t(:print_ticket), url: print_ticket_admin_order_path(@order), diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 461f08f919..aa7edfc960 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -408,6 +408,8 @@ class Enterprise < ApplicationRecord end def can_invoice? + return true unless Spree::Config.enterprise_number_required_on_invoices? + abn.present? end From 5ad09a6fab310d32a69693a80415f06af78e7799 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 23 Jun 2022 16:37:16 +0200 Subject: [PATCH 3/3] Check if user can/can't print invoice in case of company number mandatory or present --- spec/system/admin/order_spec.rb | 63 +++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 98d187c37e..12c83e26d6 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -474,12 +474,71 @@ describe ' within "#links-dropdown" do expect(page).to have_link "Resend Confirmation", href: spree.resend_admin_order_path(order) - expect(page).to have_link "Send Invoice", href: spree.invoice_admin_order_path(order) - expect(page).to have_link "Print Invoice", href: spree.print_admin_order_path(order) expect(page).to have_link "Cancel Order", href: spree.fire_admin_order_path(order, e: 'cancel') end end + + context "Check send/print invoice links" do + context "when abn number is not mandatory to send/print invoices" do + before do + Spree::Config[:enterprise_number_required_on_invoices?] = false + end + + it "should display normal links" do + visit spree.edit_admin_order_path(order) + + find("#links-dropdown .ofn-drop-down").click + expect(page).to have_link "Send Invoice", href: spree.invoice_admin_order_path(order) + expect(page).to have_link "Print Invoice", href: spree.print_admin_order_path(order) + end + end + + context "when abn number is mandatory to send/print invoices" do + before do + Spree::Config[:enterprise_number_required_on_invoices?] = true + end + + context "and a abn numer is set on the distributor" do + before do + distributor1.update_attribute(:abn, '12345678') + end + + it "should display normal links" do + visit spree.edit_admin_order_path(order) + + find("#links-dropdown .ofn-drop-down").click + expect(page).to have_link "Send Invoice", href: spree.invoice_admin_order_path(order) + expect(page).to have_link "Print Invoice", href: spree.print_admin_order_path(order) + end + end + + context "and a abn number is not set on the distributor" do + before do + distributor1.update_attribute(:abn, "") + end + + it "should not display links but a js alert" do + visit spree.edit_admin_order_path(order) + + find("#links-dropdown .ofn-drop-down").click + expect(page).to have_link "Send Invoice", href: "#" + expect(page).to have_link "Print Invoice", href: "#" + + message = accept_prompt do + click_link "Print Invoice" + end + expect(message).to eq "#{distributor1.name} must have a valid ABN before invoices can be sent." + + find("#links-dropdown .ofn-drop-down").click + message = accept_prompt do + click_link "Send Invoice" + end + expect(message).to eq "#{distributor1.name} must have a valid ABN before invoices can be sent." + end + end + end + end it "cannot split the order in different stock locations" do # There's only 1 stock location in OFN,