diff --git a/app/components/sales_log_summary_component.html.erb b/app/components/sales_log_summary_component.html.erb
index 10b021fb7..5c0668301 100644
--- a/app/components/sales_log_summary_component.html.erb
+++ b/app/components/sales_log_summary_component.html.erb
@@ -32,6 +32,12 @@
<%= log.owning_organisation&.name %>
<% end %>
+ <% if log.managing_organisation %>
+
+
Reported by
+ <%= log.managing_organisation&.name %>
+
+ <% end %>
<% end %>
diff --git a/app/models/form/sales/pages/organisation.rb b/app/models/form/sales/pages/organisation.rb
index 53f10e758..de0b92e8b 100644
--- a/app/models/form/sales/pages/organisation.rb
+++ b/app/models/form/sales/pages/organisation.rb
@@ -10,32 +10,38 @@ class Form::Sales::Pages::Organisation < ::Form::Page
]
end
- def routed_to?(_log, current_user)
+ def routed_to?(log, current_user)
return false unless current_user
return true if current_user.support?
+ return false unless FeatureToggle.sales_managing_organisation_enabled?
- if FeatureToggle.sales_managing_organisation_enabled?
- return true if stock_owners_with_own_stock_count(current_user) > 1
- return true if current_user.organisation.holds_own_stock? && stock_owners_with_own_stock_count(current_user) >= 1
- end
+ return true if stock_owners_with_own_stock_count(current_user) > 1
+ return true if current_user.organisation.holds_own_stock? && stock_owners_with_own_stock_count(current_user) >= 1
- if FeatureToggle.merge_organisations_enabled?
- absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
+ stock_owners = if FeatureToggle.merge_organisations_enabled?
+ current_user.organisation.stock_owners.where(holds_own_stock: true) + current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
+ else
+ current_user.organisation.stock_owners.where(holds_own_stock: true)
+ end
- if current_user.organisation.holds_own_stock?
- return true if absorbed_stock_owners.count >= 1
- else
- return false if absorbed_stock_owners.count.zero?
- return true if absorbed_stock_owners.count > 1
+ if current_user.organisation.holds_own_stock?
+ if FeatureToggle.merge_organisations_enabled? && current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?)
+ return true
end
+ return true if stock_owners.count >= 1
- false
+ log.update!(owning_organisation: current_user.organisation)
else
- !current_user&.support?.nil?
+ return false if stock_owners.count.zero?
+ return true if stock_owners.count > 1
+
+ log.update!(owning_organisation: stock_owners.first)
end
+
+ false
end
-private
+ private
def stock_owners_with_own_stock_count(user)
user.organisation.stock_owners.where(holds_own_stock: true).count
diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb
index 847a94f99..cff497ae4 100644
--- a/app/models/sales_log.rb
+++ b/app/models/sales_log.rb
@@ -316,9 +316,9 @@ class SalesLog < Log
def reset_created_by!
return unless updated_by&.support?
- return if owning_organisation.blank? || created_by.blank?
- return if created_by&.organisation == owning_organisation
- return if created_by&.organisation == owning_organisation.absorbing_organisation
+ return if owning_organisation.blank? || managing_organisation.blank? || created_by.blank?
+ return if created_by&.organisation == owning_organisation || created_by&.organisation == managing_organisation
+ return if created_by&.organisation == owning_organisation.absorbing_organisation || created_by&.organisation == managing_organisation.absorbing_organisation
update!(created_by: nil)
end
diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb
index e930f3b2a..bb6c54aad 100644
--- a/spec/factories/sales_log.rb
+++ b/spec/factories/sales_log.rb
@@ -2,6 +2,7 @@ FactoryBot.define do
factory :sales_log do
created_by { FactoryBot.create(:user) }
owning_organisation { created_by.organisation }
+ managing_organisation { created_by.organisation }
created_at { Time.zone.now }
updated_at { Time.zone.now }
trait :in_progress do
diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb
index 7a1c55579..7ca0c73b4 100644
--- a/spec/requests/sales_logs_controller_spec.rb
+++ b/spec/requests/sales_logs_controller_spec.rb
@@ -66,6 +66,7 @@ RSpec.describe SalesLogsController, type: :request do
let(:params) do
{
"owning_organisation_id": owning_organisation.id,
+ "managing_organisation_id": owning_organisation.id,
"created_by_id": user.id,
"saledate": Time.zone.today,
"purchid": "1",
@@ -153,10 +154,11 @@ RSpec.describe SalesLogsController, type: :request do
post "/sales-logs", headers:
end
- it "sets the stock-owning org as user's org" do
+ it "sets the managing org as user's org" do
created_id = response.location.match(/[0-9]+/)[0]
sales_log = SalesLog.find_by(id: created_id)
- expect(sales_log.owning_organisation.name).to eq("User org")
+ expect(sales_log.owning_organisation).to be_nil
+ expect(sales_log.managing_organisation.name).to eq("User org")
end
end
@@ -199,6 +201,7 @@ RSpec.describe SalesLogsController, type: :request do
:sales_log,
purchid: purchaser_code,
owning_organisation: organisation,
+ managing_organisation: organisation,
)
end
let!(:unauthorized_sales_log) do
@@ -223,6 +226,7 @@ RSpec.describe SalesLogsController, type: :request do
get "/sales-logs", headers: headers, params: {}
expect(page).to have_content("Owned by")
expect(page).not_to have_content("Managed by")
+ expect(page).to have_content("Reported by")
end
it "shows sales logs for all organisations" do