mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-24 01:13:21 +00:00
81 lines
2.7 KiB
Ruby
81 lines
2.7 KiB
Ruby
module Permissions
|
|
class Order
|
|
def initialize(user)
|
|
@user = user
|
|
@permissions = OpenFoodNetwork::Permissions.new(@user)
|
|
end
|
|
|
|
# Find orders that the user can see
|
|
def visible_orders
|
|
Spree::Order.where(id:
|
|
managed_orders.select(:id) |
|
|
coordinated_orders.select(:id) |
|
|
produced_orders.select("spree_orders.id"))
|
|
end
|
|
|
|
# Any orders that the user can edit
|
|
def editable_orders
|
|
Spree::Order.where(id:
|
|
managed_orders.select(:id) |
|
|
coordinated_orders.select(:id) )
|
|
end
|
|
|
|
def visible_line_items
|
|
Spree::LineItem.where(id:
|
|
editable_line_items.select(:id) |
|
|
produced_line_items.select("spree_line_items.id"))
|
|
end
|
|
|
|
# Any line items that I can edit
|
|
def editable_line_items
|
|
Spree::LineItem.where(order_id: editable_orders)
|
|
end
|
|
|
|
private
|
|
|
|
# Any orders placed through any hub that I manage
|
|
def managed_orders
|
|
Spree::Order.where(distributor_id: @permissions.managed_enterprises.select("enterprises.id"))
|
|
end
|
|
|
|
# Any order that is placed through an order cycle one of my managed enterprises coordinates
|
|
def coordinated_orders
|
|
Spree::Order.where(order_cycle_id: @permissions.coordinated_order_cycles.select(:id))
|
|
end
|
|
|
|
def produced_orders
|
|
Spree::Order.with_line_items_variants_and_products_outer.
|
|
where(
|
|
distributor_id: granted_distributor_ids,
|
|
spree_products: { supplier_id: enterprises_with_associated_orders }
|
|
)
|
|
end
|
|
|
|
def enterprises_with_associated_orders
|
|
# Any orders placed through hubs that my producers have granted P-OC,
|
|
# and which contain their products. This is pretty complicated but it's looking for order
|
|
# where at least one of my producers has granted P-OC to the distributor
|
|
# AND the order contains products of at least one of THE SAME producers
|
|
@permissions.related_enterprises_granting(:add_to_order_cycle, to: granted_distributor_ids).
|
|
merge(@permissions.managed_enterprises.is_primary_producer)
|
|
end
|
|
|
|
def granted_distributor_ids
|
|
@granted_distributor_ids ||= @permissions.related_enterprises_granted(
|
|
:add_to_order_cycle,
|
|
by: @permissions.managed_enterprises.is_primary_producer.select("enterprises.id")
|
|
).select("enterprises.id")
|
|
end
|
|
|
|
# Any from visible orders, where the product is produced by one of my managed producers
|
|
def produced_line_items
|
|
Spree::LineItem.where(order_id: visible_orders.select(:id)).
|
|
joins(:product).
|
|
where(spree_products:
|
|
{
|
|
supplier_id: @permissions.managed_enterprises.is_primary_producer.select("enterprises.id")
|
|
})
|
|
end
|
|
end
|
|
end
|