ソースを参照

container to show the total invested in stock by pointsale, only for admin

Jose Miguel Ledon Nieblas 8 年 前
コミット
7a1d3a35ac

+ 25 - 0
app/controllers/available_products_controller.rb

@@ -172,6 +172,31 @@ class AvailableProductsController < ApplicationController
     render json: total_prods
   end
 
+  def total_invested_in_pointsale
+    location = params[:location]
+    category = params[:category]
+    sub_category = params[:sub_category]
+    location_id = params[:location][2, params[:location].length] unless location.blank?
+
+    if location.first == 'P'
+      stock = AvailableProduct.activos.where(pointsale_id: location_id).where('stock > 0')
+    elsif location.first == 'W'
+      stock = WarehouseStock.activos.where(warehouse_id: location_id).where('stock > 0')
+    end
+
+    if sub_category.present?
+      stock = stock.joins(:categories).where('categories.id = ?', sub_category)
+    elsif category.present?
+      subs_ids = Category.activos.where(parent_id: category).pluck(:id)
+      stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
+    end
+
+    obj = Hash.new
+    obj['invested_in_pesos'] = location.first == 'P' ? stock.where('products.is_in_dollars = false').sum('available_products.stock * products.price_base') : stock.where('products.is_in_dollars = false').sum('warehouse_stocks.stock * products.price_base')
+    obj['invested_in_dollars'] = location.first == 'P' ? stock.where('products.is_in_dollars = true').sum('available_products.stock * products.price_base_dollars') : stock.where('products.is_in_dollars = true').sum('warehouse_stocks.stock * products.price_base_dollars')
+    render json: obj
+  end
+
   # DELETE //pointsales/5/available_products/1
   def destroy
     @available_product.destroy

+ 46 - 17
app/views/available_products/_form_stock_printable.html.erb

@@ -1,6 +1,6 @@
 <%= form_for(AvailableProduct.new, :html => {:class=>"form-horizontal", :id=> "available_products_form"}) do |f| %>
   <div class="portlet-body form">
-    <div id="error_explanation"></div> 
+    <div id="error_explanation"></div>
       <table class="table table-striped table-bordered table-hover tableadvancedprintableAjax" id="pointsale_stock" data-source="<%= stock_by_pointsale_path(format: "json") %>" >
         <thead>
           <tr>
@@ -18,7 +18,7 @@
             <th>Precio</th>
           </tr>
         </thead>
-        <tbody>      
+        <tbody>
         </tbody>
       </table>
     </div>
@@ -31,26 +31,23 @@
     table = $('#pointsale_stock').DataTable();
     if($('#pointsale').val()) {
       $('#pointsale').trigger("change");
-    }    
+    }
   });
 
   $('body').barcodeListener().on('barcode.valid', function(e, code) {
       $('input[type=search]').val(code);
-      $('#pointsale_stock input').unbind(); 
+      $('#pointsale_stock input').unbind();
       $('#pointsale_stock').dataTable().fnFilter(code);
-  }); 
-
-  $('#pointsale').on('change', function(e) {
-    getTotalProducts();
   });
 
   $('#category, #sub_category, #pointsale').on('change', function(e) {
     table.draw();
     getTotalProducts();
+    getTotalInvested();
   });
 
   function getSub_categories() {
-      $('#sub_category').attr('disabled', true);  
+      $('#sub_category').attr('disabled', true);
       category =  $('#category').val() ? $('#category').val() : 0;
       $.ajax({
         type: "get",
@@ -63,13 +60,13 @@
                 value: data[i].id,
                 text : data[i].category
             }));
-              
-          }     
-          $('#sub_category').select2({ allowClear: true, placeholder: 'Todas'});     
-          $('#sub_category').attr('disabled', false); 
-          $('#sub_category').select2('val', null);        
+
+          }
+          $('#sub_category').select2({ allowClear: true, placeholder: 'Todas'});
+          $('#sub_category').attr('disabled', false);
+          $('#sub_category').select2('val', null);
         }
-      });  
+      });
   }
 
   function getTotalProducts() {
@@ -90,10 +87,42 @@
       url:  '/total_products_by_pointsale?' + uri,
       dataType: 'json',
       success: function(data) {
-        App.unblockUI($("#container_total_prods"));        
+        App.unblockUI($("#container_total_prods"));
         $('#total_prods').html(Math.round(data));
       }
-    });     
+    });
+  }
+
+  function getTotalInvested() {
+    var location = $('#pointsale').val();
+    var category = $('#category').val();
+    var sub_category = $('#sub_category').val();
+    var uri = 'location=' + location;
+    if (category) { uri += '&category=' + category; }
+    if (sub_category) { uri += '&sub_category=' + sub_category; }
+
+    App.blockUI({
+      target: $("#container_total_invested"),
+      animate: true
+    });
+
+    App.blockUI({
+      target: $("#container_total_invested_dlls"),
+      animate: true
+    });
+
+    $.ajax({
+      type: "get",
+      url:  '/total_invested_in_pointsale?' + uri,
+      dataType: 'json',
+      success: function(data) {
+        App.unblockUI($("#container_total_invested"));
+        App.unblockUI($("#container_total_invested_dlls"));
+
+        $('#total_invested').html(accounting.formatMoney(data.invested_in_pesos) + ' MXN');
+        $('#total_invested_dlls').html(accounting.formatMoney(data.invested_in_dollars) + ' USD');
+      }
+    });
   }
 </script>
 

+ 32 - 17
app/views/available_products/stock_by_pointsale.html.erb

@@ -26,22 +26,22 @@
 				<%end%>
 				<!-- BEGIN PAGE BREADCRUMBS -->
 				<ul class="page-breadcrumb breadcrumb">
-					<%= render_breadcrumbs :tag => :li, :separator => ' <i class="fa fa-circle"></i> ' %> 
-				</ul> 
+					<%= render_breadcrumbs :tag => :li, :separator => ' <i class="fa fa-circle"></i> ' %>
+				</ul>
 				<!-- END PAGE BREADCRUMBS -->
 				<!-- BEGIN PAGE CONTENT INNER -->
 				<div class="page-content-inner">
 					<div id="notice"><%= notice %></div>
 					<div class="row">
 						<div class="col-md-12">
-							<div class="portlet light">  
+							<div class="portlet light">
 								<div class="portlet-body form">
-									<div id="error_explanation"></div> 
+									<div id="error_explanation"></div>
 									<!-- lista de productos -->
 									<h4 class="form-section"> Lista de productos</h4>
 									<div class="portlet-body">
 										<div class="form-horizontal">
-											<div class="row"> 
+											<div class="row">
 												<div class="col-md-12">
 													<div class="form-group">
 														<!-- punto de venta -->
@@ -60,25 +60,40 @@
 														<%= label :sub_category, "", {:class=>"col-md-1 control-label"} do %>Sublínea <% end %>
 														<div class="col-md-3 select2-bootstrap-prepend">
 															<%= select_tag "sub_category", options_from_collection_for_select(Category.activos.where('parent_id != 0'), :id, :category), :include_blank => "Todas",  class: "form-control select2 clereable" %>
-														</div>                                              
-													</div>                             
+														</div>
+													</div>
 												</div>
-											</div> 
+											</div>
 											<div class="row">
-												<div class="col-md-2 col-sm-3 col-xs-6 " >
+												<div class="col-md-3 col-sm-4 col-xs-6 " >
 													<div class="text-center well" id="container_total_prods" style="margin-bottom: 0px">
 														<div class="font-grey-mint font-sm">Total de productos </div>
 														<div class="uppercase font-hg font-blue-sharp" id="total_prods"></div>
 													</div>
-												</div> 
-											</div>                                                
+												</div>
+												<% if current_user.usertype == 'A' %>
+													<div class="col-md-3 col-sm-4 col-xs-6 ">
+														<div class="text-center well" id="container_total_invested" style="margin-bottom: 0px">
+															<div class="font-grey-mint font-sm">Total invertido en pesos </div>
+															<div class="uppercase font-hg font-green-sharp" id="total_invested"></div>
+														</div>
+													</div>
+
+													<div class="col-md-3 col-sm-4 col-xs-6 ">
+														<div class="text-center well" id="container_total_invested_dlls" style="margin-bottom: 0px">
+															<div class="font-grey-mint font-sm">Total invertido en dolares</div>
+															<div class="uppercase font-hg font-green-sharp" id="total_invested_dlls"></div>
+														</div>
+													</div>
+												<% end %>
+											</div>
 											<div class='row'>
 												<div class="col-md-12">
 												<h4 class="form-section" > </h4>
 													<%= render 'form_stock_printable' %>
 												</div>
-											</div>   
-										</div>                   
+											</div>
+										</div>
 									</div>
 								</div>
 							</div>
@@ -96,10 +111,10 @@
 <!-- END CONTAINER -->
 <script type="text/javascript">
 	$(document).on('page:change', function() {
-		$(".clereable").select2({ 
+		$(".clereable").select2({
 			allowClear: true,
 			placeholder: 'Todas'
-		});      
+		});
 	});
 
 	function print() {
@@ -114,8 +129,8 @@
 		if (sub_category) { uri += '&sub_category=' + sub_category; }
 		if (search) { uri += '&search=' + encodeURIComponent(search); }
 
-		window.open('/print_stock_by_pointsale.pdf?' + uri, 'New tab', '' );    
-	
+		window.open('/print_stock_by_pointsale.pdf?' + uri, 'New tab', '' );
+
 	}
 </script>
 

+ 1 - 0
config/routes.rb

@@ -219,6 +219,7 @@ Rails.application.routes.draw do
   get 'print_stock_by_pointsale' => 'available_products#print_stock', :as => 'print_stock_by_pointsale'
   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
 
   # find de productos para el typeahead
   get 'find_products/:query' => 'application#find', :format => :json