mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-14 23:47:48 +00:00
Add person data
This commit is contained in:
committed by
David Cook
parent
4d8f24f6d6
commit
65685a871e
@@ -1,66 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DfcProvider
|
||||
class AffiliateSalesDataController < DfcProvider::ApplicationController
|
||||
def show
|
||||
orders = anonymous_orders.map do |order|
|
||||
OrderBuilder.build_anonymous(order)
|
||||
end
|
||||
render json: DfcIo.export(*orders)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def anonymous_orders
|
||||
Spree::LineItem
|
||||
.joins(Arel.sql(joins_conditions))
|
||||
.select(Arel.sql(select_fields))
|
||||
.where(where_conditions)
|
||||
.group(Arel.sql(group_fields))
|
||||
.order(Arel.sql(order_fields))
|
||||
end
|
||||
|
||||
def joins_conditions
|
||||
[
|
||||
"JOIN spree_orders ON spree_orders.id = spree_line_items.order_id",
|
||||
"JOIN spree_variants ON spree_variants.id = spree_line_items.variant_id",
|
||||
"JOIN spree_products ON spree_products.id = spree_variants.product_id",
|
||||
"JOIN enterprises AS enterprise1 ON spree_orders.distributor_id = enterprise1.id",
|
||||
"JOIN enterprises AS enterprise2 ON spree_products.supplier_id = enterprise2.id",
|
||||
"JOIN spree_addresses AS distributors ON enterprise1.address_id = distributors.id",
|
||||
"JOIN spree_addresses AS producers ON enterprise2.address_id = producers.id"
|
||||
].join(' ')
|
||||
end
|
||||
|
||||
def select_fields
|
||||
"spree_products.name AS product_name,
|
||||
spree_variants.display_name AS unit_name,
|
||||
spree_products.variant_unit AS unit_type,
|
||||
spree_variants.unit_value AS units,
|
||||
spree_variants.unit_presentation,
|
||||
SUM(spree_line_items.quantity) AS quantity_sold,
|
||||
spree_line_items.price,
|
||||
distributors.zipcode AS distributor_postcode,
|
||||
producers.zipcode AS producer_postcode"
|
||||
end
|
||||
|
||||
def where_conditions
|
||||
{ spree_orders: { state: 'complete' } }
|
||||
end
|
||||
|
||||
def group_fields
|
||||
'spree_products.name,
|
||||
spree_variants.display_name,
|
||||
spree_variants.unit_value,
|
||||
spree_variants.unit_presentation,
|
||||
spree_products.variant_unit,
|
||||
spree_line_items.price,
|
||||
distributors.zipcode,
|
||||
producers.zipcode'
|
||||
end
|
||||
|
||||
def order_fields
|
||||
'spree_products.name'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,12 +2,20 @@
|
||||
|
||||
class AffiliateSalesDataBuilder < DfcBuilder
|
||||
def self.build(user)
|
||||
DataFoodConsortium::Connector::Person.new(urls.affiliate_sales_data_person_url(user.id))
|
||||
DataFoodConsortium::Connector::Person.new(
|
||||
urls.affiliate_sales_data_person_url(user.id),
|
||||
logo: nil,
|
||||
firstName: nil,
|
||||
lastName: nil,
|
||||
affiliatedOrganizations: user_enterprise(user.enterprises.first)
|
||||
)
|
||||
end
|
||||
|
||||
def self.user_enterprise
|
||||
# DataFoodConsortium::Connector::Enterprise.new(
|
||||
# )
|
||||
def self.user_enterprise(enterprise)
|
||||
DataFoodConsortium::Connector::Enterprise.new(
|
||||
urls.enterprise_url(enterprise.id),
|
||||
name: enterprise.name
|
||||
)
|
||||
end
|
||||
|
||||
def self.sales_data
|
||||
|
||||
Reference in New Issue
Block a user