mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-14 23:47:48 +00:00
Add sales sessions and orders data
This commit is contained in:
committed by
David Cook
parent
65685a871e
commit
ed5ff023fd
@@ -11,8 +11,11 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def affiliate_sales_data
|
||||
sales_data = AffiliateSalesDataBuilder.build(user)
|
||||
render json: DfcIo.export(sales_data)
|
||||
render json: DfcIo.export(
|
||||
AffiliateSalesDataBuilder.build_person(user),
|
||||
*AffiliateSalesDataBuilder.build_orders,
|
||||
*AffiliateSalesDataBuilder.build_sales_session,
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,30 +1,69 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AffiliateSalesDataBuilder < DfcBuilder
|
||||
def self.build(user)
|
||||
def self.build_person(user)
|
||||
DataFoodConsortium::Connector::Person.new(
|
||||
urls.affiliate_sales_data_person_url(user.id),
|
||||
logo: nil,
|
||||
firstName: nil,
|
||||
lastName: nil,
|
||||
affiliatedOrganizations: user_enterprise(user.enterprises.first)
|
||||
affiliatedOrganizations: user_enterprises(user.enterprises)
|
||||
)
|
||||
end
|
||||
|
||||
def self.user_enterprise(enterprise)
|
||||
DataFoodConsortium::Connector::Enterprise.new(
|
||||
urls.enterprise_url(enterprise.id),
|
||||
name: enterprise.name
|
||||
)
|
||||
def self.user_enterprises(enterprises)
|
||||
enterprises.map do |enterprise|
|
||||
DataFoodConsortium::Connector::Enterprise.new(urls.enterprise_url(enterprise.id))
|
||||
end
|
||||
end
|
||||
|
||||
# def self.build_producers
|
||||
# sales_data.uniq.map do |sale|
|
||||
# DataFoodConsortium::Connector::Enterprise.new(
|
||||
# urls.enterprise_url(sale.producer_id),
|
||||
# name: nil,
|
||||
# logo: nil,
|
||||
# description: nil,
|
||||
# vatNumber: nil
|
||||
# )
|
||||
# end
|
||||
# end
|
||||
|
||||
def self.build_orders
|
||||
sales_data.map do |sale|
|
||||
DataFoodConsortium::Connector::Order.new(
|
||||
urls.enterprise_order_url(sale.producer_id, sale.order_id),
|
||||
number: nil,
|
||||
date: sale.order_date.strftime("%Y-%m-%d"),
|
||||
saleSession: DataFoodConsortium::Connector::SaleSession.new(
|
||||
urls.enterprise_sale_session_url(sale.producer_id, sale.order_id),
|
||||
beginDate: nil,
|
||||
endDate: nil,
|
||||
quantity: nil
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def self.build_sales_session
|
||||
sales_data.map do |sale|
|
||||
DataFoodConsortium::Connector::SaleSession.new(
|
||||
urls.enterprise_sale_session_url(sale.producer_id, sale.order_id),
|
||||
beginDate: nil,
|
||||
endDate: nil,
|
||||
quantity: nil
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def self.sales_data
|
||||
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))
|
||||
@sales_data ||=
|
||||
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 self.joins_conditions
|
||||
@@ -40,13 +79,17 @@ class AffiliateSalesDataBuilder < DfcBuilder
|
||||
end
|
||||
|
||||
def self.select_fields
|
||||
"spree_products.name AS product_name,
|
||||
"spree_orders.id AS order_id,
|
||||
spree_orders.created_at AS order_date,
|
||||
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,
|
||||
producers.id AS producer_id,
|
||||
distributors.id AS distributor_id,
|
||||
distributors.zipcode AS distributor_postcode,
|
||||
producers.zipcode AS producer_postcode"
|
||||
end
|
||||
@@ -56,12 +99,15 @@ class AffiliateSalesDataBuilder < DfcBuilder
|
||||
end
|
||||
|
||||
def self.group_fields
|
||||
'spree_products.name,
|
||||
'spree_orders.id,
|
||||
spree_products.name,
|
||||
spree_variants.display_name,
|
||||
spree_variants.unit_value,
|
||||
spree_variants.unit_presentation,
|
||||
spree_products.variant_unit,
|
||||
spree_line_items.price,
|
||||
producers.id,
|
||||
distributors.id,
|
||||
distributors.zipcode,
|
||||
producers.zipcode'
|
||||
end
|
||||
@@ -69,6 +115,4 @@ class AffiliateSalesDataBuilder < DfcBuilder
|
||||
def self.order_fields
|
||||
'spree_products.name'
|
||||
end
|
||||
|
||||
private_class_method :sales_data
|
||||
end
|
||||
|
||||
@@ -7,6 +7,8 @@ DfcProvider::Engine.routes.draw do
|
||||
resources :offers, only: [:show, :update]
|
||||
resources :supplied_products, only: [:create, :show, :update]
|
||||
resources :social_medias, only: [:show]
|
||||
resources :sale_sessions, only: [:show]
|
||||
resources :orders, only: [:show]
|
||||
end
|
||||
resources :enterprise_groups, only: [:index, :show] do
|
||||
resources :affiliated_by, only: [:create, :destroy], module: 'enterprise_groups'
|
||||
|
||||
Reference in New Issue
Block a user