diff --git a/back-office/app/constraints/check_items_constraint.rb b/back-office/app/constraints/check_items_constraint.rb new file mode 100644 index 0000000..023d041 --- /dev/null +++ b/back-office/app/constraints/check_items_constraint.rb @@ -0,0 +1,5 @@ +class CheckItemsConstraint + def self.matches?(request) + request.params[:commit] == 'Check' + end +end diff --git a/back-office/app/constraints/delete_items_constraint.rb b/back-office/app/constraints/delete_items_constraint.rb new file mode 100644 index 0000000..208fcaa --- /dev/null +++ b/back-office/app/constraints/delete_items_constraint.rb @@ -0,0 +1,5 @@ +class DeleteItemsConstraint + def self.matches?(request) + request.params[:commit] == 'Delete Items' + end +end diff --git a/back-office/app/controllers/items_controller.rb b/back-office/app/controllers/items_controller.rb index a3a9b02..751a95e 100644 --- a/back-office/app/controllers/items_controller.rb +++ b/back-office/app/controllers/items_controller.rb @@ -15,4 +15,20 @@ class ItemsController < ApplicationController @missing_from_database = (@codes_to_check_array - items_to_check) || [] @missing_from_codes = (items_to_check - @codes_to_check_array) || [] end + + def delete_items + @suppliers = Supplier.order(:name).all + @selected_supplier = Supplier.find_by_id(params[:supplier_id]) + @items = @selected_supplier.try(:items) || [] + @codes_to_check = params[:codes] || "" + @codes_to_check_array = @codes_to_check.split("\n").reject { |code| code.strip.blank? }.map(&:strip) + items_to_check = @items.map { |i| i.code.strip } + + @items_for_delete = items_to_check & @codes_to_check_array + Item.where(:code => @items_for_delete).destroy_all + + @not_deleted_items = (@codes_to_check_array - @items_for_delete) || [] + + render :template => "items/check_availability" + end end diff --git a/back-office/app/views/items/check_availability.html.erb b/back-office/app/views/items/check_availability.html.erb index 6ca3489..ceb2ab1 100644 --- a/back-office/app/views/items/check_availability.html.erb +++ b/back-office/app/views/items/check_availability.html.erb @@ -2,33 +2,48 @@

<%= select_tag "supplier_id", options_from_collection_for_select(@suppliers, "id", "name", @selected_supplier.try(:id)) %>

-


<%= text_area_tag "codes", @codes_to_check, rows: 20, columns: 50 %>

<%= submit_tag "Check" %>

+

<%= submit_tag "Delete Items" %>

<% end %> -
-

In database: <%= @items.length %>, in file: <%= @codes_to_check_array.length %>

-
+<% if controller.action_name == 'delete_items' %> + +

Delete successful

+ + <% if @not_deleted_items.length > 0 %> +

Not deleted items

+ <% @not_deleted_items.each do |code| %> + <%= code %>
+ <% end %> + <% end %> + +<% else %> + +
+

In database: <%= @items.length %>, in file: <%= @codes_to_check_array.length %>

+
+ +
+

Not in database (<%= @missing_from_database.length %>):

+
+ <% @missing_from_database.each do |code| %> + <%= code %>
+ <% end %> + +
+ +
+

Not in file (<%= @missing_from_codes.length %>):

+
+ <% @missing_from_codes.each do |code| %> + <%= code %>
+ <% end %> + +
-
-

Not in database (<%= @missing_from_database.length %>):

-
-<% @missing_from_database.each do |code| %> - <%= code %>
<% end %> - -
- -
-

Not in file (<%= @missing_from_codes.length %>):

-
-<% @missing_from_codes.each do |code| %> - <%= code %>
-<% end %> - -
- diff --git a/back-office/config/routes.rb b/back-office/config/routes.rb index 9c38196..6c9d4a3 100644 --- a/back-office/config/routes.rb +++ b/back-office/config/routes.rb @@ -15,12 +15,13 @@ Rails.application.routes.draw do resources :multi_media_descriptions do as_routes end resources :sections do as_routes end resources :media_types do as_routes end - resources :items do - collection do + resources :items do + collection do get 'check_availability' - post 'check_availability' + post 'check_availability' => 'items#check_availability', constraints: CheckItemsConstraint + post 'check_availability' => 'items#delete_items', constraints: DeleteItemsConstraint end - as_routes + as_routes end resources :units do as_routes end resources :sub_categories do as_routes end