Browse Source

Display reported by, correctly update owning org

pull/2077/head
Kat 3 years ago
parent
commit
addecd4b0c
  1. 6
      app/components/sales_log_summary_component.html.erb
  2. 28
      app/models/form/sales/pages/organisation.rb
  3. 6
      app/models/sales_log.rb
  4. 1
      spec/factories/sales_log.rb
  5. 8
      spec/requests/sales_logs_controller_spec.rb

6
app/components/sales_log_summary_component.html.erb

@ -32,6 +32,12 @@
<dd class="app-metadata__definition"><%= log.owning_organisation&.name %></dd>
</div>
<% end %>
<% if log.managing_organisation %>
<div class="app-metadata__item">
<dt class="app-metadata__term">Reported by</dt>
<dd class="app-metadata__definition"><%= log.managing_organisation&.name %></dd>
</div>
<% end %>
</dl>
<% end %>
</div>

28
app/models/form/sales/pages/organisation.rb

@ -10,29 +10,35 @@ 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
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 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

6
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

1
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

8
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

Loading…
Cancel
Save