mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Controller plumbing for Product & Inventory report
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
require 'csv'
|
||||
require 'open_food_network/order_and_distributor_report'
|
||||
require 'open_food_network/products_and_inventory_report'
|
||||
require 'open_food_network/group_buy_report'
|
||||
require 'open_food_network/order_grouper'
|
||||
require 'open_food_network/model_class_from_controller_name'
|
||||
@@ -535,7 +536,16 @@ Spree::Admin::ReportsController.class_eval do
|
||||
end
|
||||
|
||||
def products_and_inventory
|
||||
my_distributors = Enterprise.is_distributor.managed_by(spree_current_user)
|
||||
my_suppliers = Enterprise.is_primary_producer.managed_by(spree_current_user)
|
||||
distributors_of_my_products = Enterprise.with_distributed_products_outer.merge(Spree::Product.in_any_supplier(my_suppliers))
|
||||
@distributors = my_distributors | distributors_of_my_products
|
||||
suppliers_of_products_I_distribute = my_distributors.map { |d| Spree::Product.in_distributor(d) }.flatten.map(&:supplier).uniq
|
||||
@suppliers = my_suppliers | suppliers_of_products_I_distribute
|
||||
@order_cycles = OrderCycle.active_or_complete.accessible_by(spree_current_user).order('orders_close_at DESC')
|
||||
@report_types = REPORT_TYPES[:products_and_inventory]
|
||||
|
||||
@report = OpenFoodNetwork::ProductsAndInventoryReport.new params
|
||||
end
|
||||
|
||||
def render_report (header, table, create_csv, csv_file_name)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
%ul{style: "margin-left: 12pt"}
|
||||
- report_types.each do |report_type|
|
||||
%li
|
||||
= link_to report_type[0], "#{products_and_inventory_admin_reports_url}?report_type=#{report_type[1]}"
|
||||
@@ -0,0 +1,45 @@
|
||||
= form_for @search, :url => spree.products_and_inventory_admin_reports_url do |f|
|
||||
%br
|
||||
= label_tag nil, "Distributor: "
|
||||
= select_tag(:distributor_id,
|
||||
options_from_collection_for_select(@distributors, :id, :name, params[:distributor_id]),
|
||||
:include_blank => true)
|
||||
|
||||
%br
|
||||
= label_tag nil, "Supplier: "
|
||||
= select_tag(:supplier_id,
|
||||
options_from_collection_for_select(@suppliers, :id, :name, params[:supplier_id]),
|
||||
:include_blank => true)
|
||||
|
||||
%br
|
||||
= label_tag nil, "Order Cycle: "
|
||||
- order_cycles_select = @order_cycles.collect {|oc| [ "#{oc.name} (#{oc.orders_open_at.to_s(:short)} - #{oc.orders_close_at.to_s(:short)})".html_safe, oc.id ] }
|
||||
= select_tag(:order_cycle_id,
|
||||
options_for_select(order_cycles_select, params[:order_cycle_id]),
|
||||
include_blank: true)
|
||||
|
||||
%br
|
||||
= label_tag nil, "Report Type: "
|
||||
= select_tag(:report_type, options_for_select(@report_types, @report_type))
|
||||
|
||||
%br
|
||||
%br
|
||||
= check_box_tag :csv
|
||||
= label_tag :csv, "Download as csv"
|
||||
%br
|
||||
= button t(:search)
|
||||
%br
|
||||
%br
|
||||
%table#listing_products.index
|
||||
%thead
|
||||
%tr{'data-hook' => "products_header"}
|
||||
- @header.each do |heading|
|
||||
%th=heading
|
||||
%tbody
|
||||
- @table.each do |row|
|
||||
%tr
|
||||
- row.each do |column|
|
||||
%td= column
|
||||
- if @table.empty?
|
||||
%tr
|
||||
%td{:colspan => "2"}= t(:none)
|
||||
@@ -165,4 +165,43 @@ describe Spree::Admin::ReportsController do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "Products & Inventory" do
|
||||
let(:user) do
|
||||
user = create(:user)
|
||||
user.spree_roles << Spree::Role.find_or_create_by_name!('admin')
|
||||
user
|
||||
end
|
||||
before do
|
||||
controller.stub spree_current_user: user
|
||||
end
|
||||
|
||||
it "should build distributors for the current user" do
|
||||
spree_get :products_and_inventory
|
||||
assigns(:distributors).should == [d1, d2, d3]
|
||||
end
|
||||
|
||||
it "builds suppliers for the current user" do
|
||||
spree_get :products_and_inventory
|
||||
assigns(:suppliers).should == [s1, s2, s3]
|
||||
end
|
||||
|
||||
it "builds order cycles for the current user" do
|
||||
spree_get :products_and_inventory
|
||||
assigns(:order_cycles).should == [ocB, ocA]
|
||||
end
|
||||
|
||||
it "assigns report types" do
|
||||
spree_get :products_and_inventory
|
||||
assigns(:report_types).should == Spree::Admin::ReportsController::REPORT_TYPES[:products_and_inventory]
|
||||
end
|
||||
|
||||
it "creates a ProductAndInventoryReport" do
|
||||
OpenFoodNetwork::ProductsAndInventoryReport.should_receive(:new)
|
||||
.with({"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"products_and_inventory"})
|
||||
.and_return({})
|
||||
spree_get :products_and_inventory, :test => "foo"
|
||||
assigns(:report).should == {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user