Browse Source

cashier now are able to create transfers

Jose Miguel Ledon Nieblas 8 years ago
parent
commit
d57c1dbe21
3 changed files with 57 additions and 65 deletions
  1. 56 63
      app/controllers/transfers_controller.rb
  2. 0 2
      app/views/transfers/index.html.erb
  3. 1 0
      config/navigation.rb

+ 56 - 63
app/controllers/transfers_controller.rb

@@ -5,9 +5,9 @@ class TransfersController < ApplicationController
   ##--- Breadcrum_rails
   add_breadcrumb I18n.t("breadcrumbs." + controller_name), :transfers_path
   add_breadcrumb "Nuevo traspaso", :new_transfer_path, only: :new
-  add_breadcrumb "Detalle del traspaso" , :transfer_path, only: :show
-  add_breadcrumb "Editar traspaso" , :edit_transfer_path, only: :edit
-  add_breadcrumb "Verificar traspaso" , :verify_transfer_path, only: :verify_transfer
+  add_breadcrumb "Detalle del traspaso", :transfer_path, only: :show
+  add_breadcrumb "Editar traspaso", :edit_transfer_path, only: :edit
+  add_breadcrumb "Verificar traspaso", :verify_transfer_path, only: :verify_transfer
 
   before_action :set_transfer, only: [:show, :edit, :update, :destroy]
   before_action :get_filters, only: [:index, :show, :edit, :new]
@@ -18,35 +18,35 @@ class TransfersController < ApplicationController
     if current_user.usertype == 'A'
       @transfers = Transfer.all.includes(:user, :received_by).order('id DESC, transfer_date DESC')
     elsif current_user.usertype == 'G' || current_user.usertype == 'C'
-      @received = Transfer.includes(:user, :received_by).where(:destiny_id => current_user.pointsale_id).order('id DESC, transfer_date DESC')
-      @sent = Transfer.includes(:user, :received_by).where(:origin_id => current_user.pointsale_id).order('id DESC, transfer_date DESC')
+      @received = Transfer.includes(:user, :received_by).where(destiny_id: current_user.pointsale_id).order('id DESC, transfer_date DESC')
+      @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.pointsale_id).order('id DESC, transfer_date DESC')
     elsif current_user.usertype == 'S'
-      @received = Transfer.includes(:user, :received_by).where(:destiny_id => current_user.warehouse_id).order('id DESC, transfer_date DESC')
-      @sent = Transfer.includes(:user, :received_by).where(:origin_id => current_user.warehouse_id).order('id DESC, transfer_date DESC')
+      @received = Transfer.includes(:user, :received_by).where(destiny_id: current_user.warehouse_id).order('id DESC, transfer_date DESC')
+      @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.warehouse_id).order('id DESC, transfer_date DESC')
     end
   end
 
   # GET /transfers/1
   # GET /transfers/1.json
-  def show
-  end
+  def show; end
 
   # GET /transfers/new
+  # rubocop:disable Style/ZeroLengthPredicate
   def new
     @transfer = Transfer.new
     @destiny_pointsales = Pointsale.activos.ignore_current(current_user.pointsale_id)
-    @pre_transfers = PreTransfer.where(:user_id => current_user.id)
+    @pre_transfers = PreTransfer.where(user_id: current_user.id)
     @disable_origin = false
     @disable_destiny = false
     @disabled_button = true
     if @pre_transfers.size > 0
-      @origin_id =  @pre_transfers[0].origin_is_pointsale == 1 ? "P-#{@pre_transfers[0].origin_id}" : "W-#{@pre_transfers[0].origin_id}"
-      @destiny_id =  @pre_transfers[0].destiny_is_pointsale == 1 ? "P-#{@pre_transfers[0].destiny_id}" : "W-#{@pre_transfers[0].destiny_id}"
+      @origin_id = @pre_transfers[0].origin_is_pointsale == 1 ? "P-#{@pre_transfers[0].origin_id}" : "W-#{@pre_transfers[0].origin_id}"
+      @destiny_id = @pre_transfers[0].destiny_is_pointsale == 1 ? "P-#{@pre_transfers[0].destiny_id}" : "W-#{@pre_transfers[0].destiny_id}"
       @disable_origin = true
       @disable_destiny = true
       @disabled_button = false
     end
-    if current_user.usertype == "G"
+    if current_user.usertype == "G" || current_user.usertype == "C"
       @origin_id = "P-#{current_user.pointsale_id}"
       @disable_origin = true
     elsif current_user.usertype == "S"
@@ -54,16 +54,16 @@ class TransfersController < ApplicationController
       @disable_origin = true
     end
   end
+  # rubocop:enble Style/ZeroLengthPredicate
 
   # GET /transfers/1/edit
-  def edit
-  end
+  def edit; end
 
   # POST /transfers
   # POST /transfers.json
   def create
     @transfer = Transfer.new(transfer_params)
-    pre_transfers = PreTransfer.where(:user_id => current_user.id)
+    pre_transfers = PreTransfer.where(user_id: current_user.id)
     @transfer.user_id = current_user.id
     @transfer.status = :pending
     @transfer.origin_is_pointsale = params[:transfer][:origin_id].first == 'P' ? 1 : 0
@@ -129,6 +129,7 @@ class TransfersController < ApplicationController
     respond_to do |format|
       @transfer = Transfer.find(params[:transfer_id])
       @transfer.transfer_details.each do |detail|
+        # rubocop:disable Style/Next
         if detail.id == params[:detail_id].to_i
           detail.adjustment = params[:transfer_details][:adjustment].to_i
           if detail.adjustment > detail.quantity
@@ -143,10 +144,12 @@ class TransfersController < ApplicationController
             format.json { head :ok }
           end
         end
+        # rubocop:enable Style/Next
       end
     end
   end
 
+  # rubocop:disable Metrics/BlockLength
   def accept_transfer
     respond_to do |format|
       @transfer = Transfer.find(params[:transfer_id])
@@ -158,33 +161,30 @@ class TransfersController < ApplicationController
       @transfer.save
       @transfer.transfer_details.each do |detail|
         if @transfer.destiny_is_pointsale == 1
-          stockProduct = AvailableProduct.find_by(:product_id => detail.product_id, :pointsale_id => @transfer.destiny_id)
-          if stockProduct.present?
-            stockProduct.stock = 0 if stockProduct.stock.blank?
-            stockProduct.stock += detail.adjustment
-            stockProduct.save
+          stock_product = AvailableProduct.find_by(product_id: detail.product_id, pointsale_id: @transfer.destiny_id)
+          if stock_product.present?
+            stock_product.stock = 0 if stock_product.stock.blank?
+            stock_product.stock += detail.adjustment
           else
-            stockProduct = AvailableProduct.new
-            stockProduct.product_id = detail.product_id
-            stockProduct.pointsale_id = @transfer.destiny_id
-            stockProduct.stock = detail.adjustment
-            stockProduct.save
+            stock_product = AvailableProduct.new
+            stock_product.product_id = detail.product_id
+            stock_product.pointsale_id = @transfer.destiny_id
+            stock_product.stock = detail.adjustment
           end
         else
           # actualizar stock del producto cuando es para almacen
-          stockProduct = WarehouseStock.find_by(:product_id => detail.product_id, :warehouse_id => @transfer.destiny_id)
-          if stockProduct.present?
-            stockProduct.stock = 0 if stockProduct.stock.blank?
-            stockProduct.stock += detail.adjustment
-            stockProduct.save
+          stock_product = WarehouseStock.find_by(product_id: detail.product_id, warehouse_id: @transfer.destiny_id)
+          if stock_product.present?
+            stock_product.stock = 0 if stock_product.stock.blank?
+            stock_product.stock += detail.adjustment
           else
-            stockProduct = WarehouseStock.new
-            stockProduct.product_id = detail.product_id
-            stockProduct.warehouse_id = @transfer.destiny_id
-            stockProduct.stock = detail.adjustment
-            stockProduct.save
+            stock_product = WarehouseStock.new
+            stock_product.product_id = detail.product_id
+            stock_product.warehouse_id = @transfer.destiny_id
+            stock_product.stock = detail.adjustment
           end
         end
+        stock_product.save
 
         # guardar en bitacora de inventario
         move = InventoriesMove.new
@@ -198,17 +198,19 @@ class TransfersController < ApplicationController
       format.js { flash[:success] = "Traspaso realizado correctamente." }
     end
   end
+  # rubocop:enable Metrics/BlockLength
 
+  # rubocop:disable Style/ConditionalAssignment
   def print_receipt
     respond_to do |format|
       @transfer = Transfer.find(params[:transfer_id])
       origin = @transfer.origin_is_pointsale == 1 ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name
       destiny = @transfer.destiny_is_pointsale == 1 ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name
-      with_looses = @transfer.transfer_details.where(:has_looses => 1).count
-      with_surplus =  @transfer.transfer_details.where(:has_surplus => 1).count
-      if with_looses > 0 && with_surplus == 0
+      with_looses = @transfer.transfer_details.where(has_looses: 1).count
+      with_surplus = @transfer.transfer_details.where(has_surplus: 1).count
+      if with_looses > 0 && with_surplus.zero?
         reception_status = 'CON PERDIDAS'
-      elsif with_surplus > 0 && with_looses == 0
+      elsif with_surplus > 0 && with_looses.zero?
         reception_status = 'CON EXCEDENTE'
       elsif with_surplus > 0 && with_looses > 0
         reception_status = 'CON INCONSISTENCIAS'
@@ -217,35 +219,26 @@ class TransfersController < ApplicationController
       end
 
       format.pdf do
-        render pdf: "traspaso_#{@transfer.id}",
-          template: "transfers/receipt.pdf.erb",
-          layout: 'receipt.html.erb',
-          locals: { :transfer => @transfer, :reception_status => reception_status, :origin => origin, :destiny => destiny },
-          show_as_html: params.key?('debug'),
-          page_width: '80mm',
-          page_height: '300mm'
+        render pdf: "traspaso_#{@transfer.id}", template: "transfers/receipt.pdf.erb", layout: 'receipt.html.erb', locals: { transfer: @transfer, reception_status: reception_status, origin: origin, destiny: destiny }, show_as_html: params.key?('debug'), page_width: '80mm', page_height: '300mm'
       end
     end
   end
+  # rubocop:enable Style/ConditionalAssignment
 
   private
-    # Use callbacks to share common setup or constraints between actions.
-    def set_transfer
-      @transfer = Transfer.find(params[:id])
-    end
 
-    def get_filters
-      if params[:current_page].blank?
-        @current_page = 1
-      else
-        @current_page = params[:current_page]
-      end
-      @filter = params[:filter]
-    end
+  # Use callbacks to share common setup or constraints between actions.
+  def set_transfer
+    @transfer = Transfer.find(params[:id])
+  end
 
-    # Never trust parameters from the scary internet, only allow the white list through.
-    def transfer_params
-      params.require(:transfer).permit(:origin_id, :destiny_id, :transfer_date, :user_id, :status, :received_by_id, :observations, :transfer_details_attributes => [:transfer_id, :product_id, :quantity, :status, :adjustment, :reception_date])
+  def get_filters
+    @current_page = params[:current_page].blank? ? 1 : params[:current_page]
+    @filter = params[:filter]
+  end
 
-    end
+  # Never trust parameters from the scary internet, only allow the white list through.
+  def transfer_params
+    params.require(:transfer).permit(:origin_id, :destiny_id, :transfer_date, :user_id, :status, :received_by_id, :observations, transfer_details_attributes: [:transfer_id, :product_id, :quantity, :status, :adjustment, :reception_date])
+  end
 end

+ 0 - 2
app/views/transfers/index.html.erb

@@ -43,10 +43,8 @@
                     <span class="caption-subject bold uppercase">Lista de traspasos</span>
                   </div>
                   <div class="actions">
-                    <% if current_user.usertype != 'C' %>
                     <%= link_to new_transfer_path, {:class=>"btn bold green pull-right filtros"} do %> Nuevo traspaso <i class="fa fa-plus"></i>
                     <% end %>
-                    <% end %>
                   </div>
                 </div>
                 <div class="portlet-body">

+ 1 - 0
config/navigation.rb

@@ -238,6 +238,7 @@ SimpleNavigation::Configuration.run do |navigation|
       # conceptos de egresos
       primary.item :transfers, { icon: "fa fa-fw fa-dollar", text: "Traspasos" }, transfers_path, class: 'menu-dropdown classic-menu-dropdown' do |sub_nav|
         sub_nav.dom_attributes = { class: 'dropdown-menu pull-left' }
+        sub_nav.item :new_transfer, 'Nuevo traspaso', new_transfer_path
         sub_nav.item :list_expenses, 'Lista de traspasos', transfers_path
       end
     end