Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow producer to edit their products on hubs' orders #13113

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions app/helpers/spree/admin/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ def quantity_field_tag(manifest_item)
end
number_field_tag :quantity, manifest_item.quantity, html_options
end

def prepare_shipment_manifest(shipment)
manifest = shipment.manifest

if distributor_allows_order_editing?(shipment.order)
supplier_ids = spree_current_user.enterprises.ids
manifest.select! { |mi| supplier_ids.include?(mi.variant.supplier_id) }
end

manifest
end

def distributor_allows_order_editing?(order)
order.distributor&.enable_producers_to_edit_orders &&
spree_current_user.can_manage_line_items_in_orders_only?
end

end
end
end
20 changes: 15 additions & 5 deletions app/models/spree/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,22 @@ def add_order_management_abilities(user)
end

def add_manage_line_items_abilities(user)
can [:admin, :read, :index], Spree::Order do |order|
if order.distributor&.enable_producers_to_edit_orders
user_enterprises_ids = user.enterprises.ids
order.variants.any? { |variant| user_enterprises_ids.include?(variant.supplier_id) }
end
can_edit_order_lambda = lambda do |order|
return unless order.distributor&.enable_producers_to_edit_orders

order.variants.any? { |variant| user.enterprises.ids.include?(variant.supplier_id) }
end
Comment on lines +357 to +361
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be it's own method instead of having disguised as lambda here.


can [:admin, :read, :index, :edit, :update], Spree::Order do |order|
can_edit_order_lambda.call(order)
end
can [:admin, :index, :create, :destroy, :update], Spree::LineItem do |item|
can_edit_order_lambda.call(item.order)
end
can [:index, :create, :add, :read, :edit, :update], Spree::Shipment do |shipment|
can_edit_order_lambda.call(shipment.order)
end

end

def add_relationship_management_abilities(user)
Expand Down
7 changes: 7 additions & 0 deletions app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class LineItem < ApplicationRecord
where(spree_adjustments: { id: nil })
}

scope :editable_by_producers, ->(enterprises_ids) {
joins(:variant, order: :distributor).where(
distributor: { enable_producers_to_edit_orders: true },
spree_variants: { supplier_id: enterprises_ids }
)
}

def copy_price
return unless variant

Expand Down
3 changes: 3 additions & 0 deletions app/models/spree/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def can_manage_line_items_in_orders?
end
end

def can_manage_line_items_in_orders_only?
!can_manage_orders? && can_manage_line_items_in_orders?
end
Comment on lines +165 to +167
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec ?


protected

Expand Down
8 changes: 7 additions & 1 deletion app/services/permissions/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ def visible_line_items

# Any line items that I can edit
def editable_line_items
Spree::LineItem.where(order_id: editable_orders.select(:id))
if @user.can_manage_line_items_in_orders_only?
Spree::LineItem.editable_by_producers(
@permissions.managed_enterprises.select("enterprises.id")
)
else
Spree::LineItem.where(order_id: editable_orders.select(:id))
end
Comment on lines +46 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specs ?

end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/orders/_shipment_manifest.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- shipment.manifest.each do |item|
- prepare_shipment_manifest(shipment).each do |item|
- line_item = order.find_line_item_by_variant(item.variant)

- if line_item.present?
Expand Down