From 7683d9aece91ab27634ec2537616458e15938fc8 Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Tue, 5 Nov 2024 15:35:09 -0300 Subject: [PATCH] update qrcode --- app/controllers/products_controller.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index c7bf55c3..3bdceec5 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -234,14 +234,29 @@ def print_tags def print_tags_by_skus skus = params[:sku_list].to_s.split(/\r?\n/).map(&:strip).reject(&:blank?) - # Using ILIKE for case-insensitive matching + + # Using ILIKE for case-insensitive matching, with exact matches @products = Product.where(account_id: current_tenant) - .where("sku ILIKE ANY (ARRAY[?])", skus.map { |sku| sku }) + .where("sku ILIKE ANY (ARRAY[?])", skus) + + # Sort products based on the order of input SKUs + @products = @products.sort_by do |product| + # Find the index of the matching SKU in the original input list + skus.index { |sku| sku.casecmp(product.sku) == 0 } || Float::INFINITY + end + @copies = params[:copies].to_i + # Find SKUs that weren't matched (preserving original case for display) + found_skus = @products.map(&:sku) + not_found_skus = skus.reject { |sku| found_skus.any? { |found| found.casecmp(sku) == 0 } } + if @products.empty? - redirect_to product_print_tags_products_path, alert: 'Nenhum produto encontrado com os SKUs fornecidos.' + redirect_to product_print_tags_products_path, + alert: "Nenhum produto encontrado com os SKUs fornecidos.\nSKUs não encontrados: #{not_found_skus.join(', ')}" return + elsif not_found_skus.any? + flash[:warning] = "Alguns SKUs não foram encontrados: #{not_found_skus.join(', ')}" end respond_to do |format|