Browse Source

suggest print quantity based on pointsale stock

chemi ledon 7 years ago
parent
commit
6d276500bf

+ 8 - 2
app/controllers/available_products_controller.rb

@@ -133,9 +133,9 @@ class AvailableProductsController < ApplicationController
       location_id = params[:location][2, params[:location].length] unless location.blank?
 
       if (current_user.usertype == 'S' && location.blank?) || (!location.blank? && location.first == 'W')
-        stock = WarehouseStock.activos.includes(:warehouse, :categories).where(warehouse_id: (location_id.blank? ? current_user.warehouse_id : location_id)).where('stock > 0')
+        stock = WarehouseStock.activos.(:warehouse, :categories).where(warehouse_id: (location_id.blank? ? current_user.warehouse_id : location_id)).where('stock > 0')
       elsif (current_user.usertype != 'S' && location.blank?) || (!location.blank? && location.first == 'P')
-        stock = AvailableProduct.activos.includes(:pointsale, :categories).where(pointsale_id: (location_id.blank? ? current_user.pointsale_id : location_id)).where('stock > 0')
+        stock = AvailableProduct.activos.(:pointsale, :categories).where(pointsale_id: (location_id.blank? ? current_user.pointsale_id : location_id)).where('stock > 0')
       end
 
       if sub_category.present?
@@ -223,6 +223,12 @@ class AvailableProductsController < ApplicationController
     # end
   end
 
+  def get_availables_by_product
+    product = Product.find(params[:product_id])
+    availables = product.presentation? ? AvailableProduct.includes(:product).where(product_id: product.children.pluck(:id), pointsale_id: params[:pointsale_id]) : AvailableProduct.includes(:product).where(product_id: product.id, pointsale_id: params[:pointsale_id])
+    render json: availables
+  end
+
   private
 
   # Use callbacks to share common setup or constraints between actions.

+ 40 - 4
app/views/products/_labels_list.html.erb

@@ -1,12 +1,13 @@
 <div class="row form-horizontal">
     <div class="col-md-12">
         <h4><%= @product.name %></h4>
+        <%= hidden_field_tag 'product_id', @product.id %>
 
         <% if @product.presentation? && @pointsales.present? %>
             <div class="form-group">
                 <label class="col-md-3 control-label">Punto de venta</label>
                 <div class="col-md-9">
-                    <%= select_tag :pointsale_id, options_from_collection_for_select(@pointsales, :pointsale_id, :name), :include_blank => "Seleccione punto de venta",  class: "form-control select2 first_input" %>
+                    <%= select_tag :pointsale_id, options_from_collection_for_select(@pointsales, :pointsale_id, :name), :include_blank => "Seleccione punto de venta",  class: "form-control select2 first_input", onchange: 'getAvailables()' %>
                     <span class="help-block">Las etiquetas se imprimirán con el precio de venta del punto de venta seleccionado</span>
                 </div>
                 <br>
@@ -15,7 +16,7 @@
             <div class="form-group">
                 <label class="col-md-3 control-label">Punto de venta</label>
                 <div class="col-md-9">
-                    <%= select_tag :pointsale_id, options_from_collection_for_select(@product.pointsales, :id, :name), :include_blank => "Seleccione punto de venta",  class: "form-control select2 first_input" %>
+                    <%= select_tag :pointsale_id, options_from_collection_for_select(@product.pointsales, :id, :name), :include_blank => "Seleccione punto de venta",  class: "form-control select2 first_input", onchange: 'getAvailables()' %>
                     <span class="help-block">Las etiquetas se imprimirán con el precio de venta del punto de venta seleccionado</span>
                 </div>
                 <br>
@@ -50,9 +51,8 @@
         <% else %>
             <div class="form-group" style="margin-bottom:20px">
                 <label class="col-md-3 control-label">Cantidad a imprimir</label>
-                <div class="col-md-3" style="padding-top:10px">
+                <div class="col-md-3" style="padding-top:10px" id="individual_prod_container">
                     <input type="number" id="quantity_to_print" value="0" class="form-control first_input">
-                    <%= hidden_field_tag 'product_id', @product.id %>
                 </div>
             </div>
         <% end %>
@@ -126,6 +126,42 @@
         }
 	}
 
+    function getAvailables() {
+        var pointsale_id = $('#pointsale_id').val();
+        var product_id = $('#product_id').val();
+        var blockUI_target = "";
+        var is_presentation_prod = $("#variant_table_for_labels").length ? true : false;
+        if(pointsale_id) {
+            blockUI_target = is_presentation_prod ? '#variant_table_for_labels' : '#individual_prod_container';
+
+            App.blockUI({
+                target: blockUI_target,
+                animate: true
+            });
+
+            $.ajax({
+                type: "get",
+                url:  '/get_availables_by_product',
+                dataType: 'json',
+                data: {
+                    pointsale_id: pointsale_id,
+                    product_id: product_id
+                },
+                success: function(data) {
+                    var availables = data;
+                    if(is_presentation_prod) {
+                        availables.forEach(function(available) {
+                            $('#variant_' + available.product_id.toString()).val(parseFloat(available.stock));
+                        });
+                    } else {
+                        $('#quantity_to_print').val(availables[0].stock);
+                    }
+                    App.unblockUI(blockUI_target);
+                }
+            });
+        }
+    }
+
     function cerrarDialog() {
         $('#dialog').modal('toggle');
     }

+ 1 - 0
config/routes.rb

@@ -234,6 +234,7 @@ Rails.application.routes.draw do
   put 'set_initial_stock' => 'available_products#set_initial_stock', defaults: { format: 'json' }
   get 'total_products_by_pointsale' => 'available_products#total_products_by_pointsale', :format => :json
   get 'total_invested_in_pointsale' => 'available_products#total_invested_in_pointsale', :format => :json
+  get 'get_availables_by_product' => 'available_products#get_availables_by_product', :format => :json
 
   # find de productos para el typeahead
   get 'find_products' => 'application#find', :format => :json