|
|
@@ -15,14 +15,18 @@ class TransfersController < ApplicationController
|
|
|
# GET /transfers
|
|
|
# GET /transfers.json
|
|
|
def index
|
|
|
- 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, destiny_is_pointsale: 1).order('id DESC, transfer_date DESC')
|
|
|
- @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.pointsale_id, origin_is_pointsale: 1).order('id DESC, transfer_date DESC')
|
|
|
- elsif current_user.usertype == 'S'
|
|
|
- @received = Transfer.includes(:user, :received_by).where(destiny_id: current_user.warehouse_id, destiny_is_pointsale: 0).order('id DESC, transfer_date DESC')
|
|
|
- @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.warehouse_id, origin_is_pointsale: 0).order('id DESC, transfer_date DESC')
|
|
|
+ @transfers = Transfer.includes(:user, :received_by, :transfer_details).all.order('id DESC, transfer_date DESC')
|
|
|
+ unless current_user.usertype == "A"
|
|
|
+ is_destiny =
|
|
|
+ if current_user.usertype == "S"
|
|
|
+ id_filter = current_user.warehouse_id
|
|
|
+ 0
|
|
|
+ else
|
|
|
+ id_filter = current_user.pointsale_id
|
|
|
+ 1
|
|
|
+ end
|
|
|
+ @received = @transfers.where(destiny_id: id_filter, destiny_is_pointsale: is_destiny).order('id DESC, transfer_date DESC')
|
|
|
+ @sent = @transfers.where(origin_id: id_filter, origin_is_pointsale: is_destiny).order('id DESC, transfer_date DESC')
|
|
|
end
|
|
|
end
|
|
|
|
|
|
@@ -31,30 +35,10 @@ class TransfersController < ApplicationController
|
|
|
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)
|
|
|
- @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}"
|
|
|
- @disable_origin = true
|
|
|
- @disable_destiny = true
|
|
|
- @disabled_button = false
|
|
|
- end
|
|
|
- if current_user.usertype == "G" || current_user.usertype == "C"
|
|
|
- @origin_id = "P-#{current_user.pointsale_id}"
|
|
|
- @disable_origin = true
|
|
|
- elsif current_user.usertype == "S"
|
|
|
- @origin_id = "W-#{current_user.warehouse_id}"
|
|
|
- @disable_origin = true
|
|
|
- end
|
|
|
+ set_transfer_data(nil)
|
|
|
end
|
|
|
- # rubocop:enble Style/ZeroLengthPredicate
|
|
|
|
|
|
# GET /transfers/1/edit
|
|
|
def edit; end
|
|
|
@@ -71,44 +55,27 @@ class TransfersController < ApplicationController
|
|
|
@transfer.origin_id = params[:transfer][:origin_id][2, params[:transfer][:origin_id].length]
|
|
|
@transfer.destiny_id = params[:transfer][:destiny_id][2, params[:transfer][:destiny_id].length]
|
|
|
|
|
|
+ pre_transfers.each do |pre|
|
|
|
+ detail = TransferDetail.new(transfer_id: @transfer.id, product_id: pre.product_id, quantity: pre.quantity, adjustment: pre.quantity, status: "pending")
|
|
|
+ @transfer.transfer_details << detail
|
|
|
+ end
|
|
|
respond_to do |format|
|
|
|
- @transfer.audit_comment = "Traspaso de #{@transfer.origin_is_pointsale? ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name} a #{@transfer.destiny_is_pointsale? ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name} creado."
|
|
|
+ origin_pointsale_name = @transfer.origin_is_pointsale? ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name
|
|
|
+ destiny_pointsale_name = @transfer.destiny_is_pointsale? ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name
|
|
|
+ @transfer.audit_comment = "Traspaso de #{origin_pointsale_name} a #{destiny_pointsale_name} creado."
|
|
|
if @transfer.save
|
|
|
- pre_transfers.each do |pre|
|
|
|
- detail = TransferDetail.new
|
|
|
- detail.transfer_id = @transfer.id
|
|
|
- detail.product_id = pre.product_id
|
|
|
- detail.quantity = pre.quantity
|
|
|
- detail.adjustment = pre.quantity
|
|
|
- detail.status = "pending"
|
|
|
- @transfer.transfer_details << detail
|
|
|
- pre.destroy
|
|
|
- end
|
|
|
-
|
|
|
- message = "Traspaso creado al " + (@transfer.destiny_is_pointsale == 1 ? "punto de venta #{Pointsale.find(@transfer.destiny_id).name}" : "almacén #{Warehouse.find(@transfer.destiny_id).name}")
|
|
|
+ pre_transfers.destroy_all
|
|
|
+ message = "Traspaso creado al " + (@transfer.destiny_is_pointsale? ? "punto de venta #{destiny_pointsale_name}" : "almacén #{destiny_pointsale_name}")
|
|
|
format.html { redirect_to transfers_path, success: message }
|
|
|
format.json { render :show, status: :created, location: @transfer }
|
|
|
else
|
|
|
+ set_transfer_data(params)
|
|
|
format.html { render :new }
|
|
|
format.json { render json: @transfer.errors, status: :unprocessable_entity }
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
|
|
|
- # PATCH/PUT /transfers/1
|
|
|
- # PATCH/PUT /transfers/1.json
|
|
|
- def update
|
|
|
- respond_to do |format|
|
|
|
- if @transfer.update(transfer_params)
|
|
|
- format.html { redirect_to @transfer, notice: 'Transfer was successfully updated.' }
|
|
|
- format.json { render :show, status: :ok, location: @transfer }
|
|
|
- else
|
|
|
- format.html { render :edit }
|
|
|
- format.json { render json: @transfer.errors, status: :unprocessable_entity }
|
|
|
- end
|
|
|
- end
|
|
|
- end
|
|
|
-
|
|
|
# DELETE /transfers/1
|
|
|
# DELETE /transfers/1.json
|
|
|
def destroy
|
|
|
@@ -149,81 +116,96 @@ class TransfersController < ApplicationController
|
|
|
end
|
|
|
end
|
|
|
|
|
|
- # rubocop:disable Metrics/BlockLength
|
|
|
def accept_transfer
|
|
|
- respond_to do |format|
|
|
|
- @transfer = Transfer.find(params[:transfer_id])
|
|
|
- @transfer.observations = params[:transfer][:observations]
|
|
|
- @transfer.received_by_id = current_user.id
|
|
|
- @transfer.status = :received
|
|
|
- @transfer.reception_date = Date.today
|
|
|
- @transfer.audit_comment = "Se dio entrada a traspaso de #{@transfer.origin_is_pointsale? ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name} a #{@transfer.destiny_is_pointsale? ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name}"
|
|
|
- @transfer.save
|
|
|
- @transfer.transfer_details.each do |detail|
|
|
|
- if @transfer.destiny_is_pointsale == 1
|
|
|
- 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
|
|
|
- stock_product = AvailableProduct.new
|
|
|
- stock_product.product_id = detail.product_id
|
|
|
- stock_product.pointsale_id = @transfer.destiny_id
|
|
|
- stock_product.stock = detail.adjustment
|
|
|
- end
|
|
|
+ @transfer = Transfer.find(params[:transfer_id])
|
|
|
+ @transfer.observations = params[:transfer][:observations]
|
|
|
+ @transfer.received_by_id = current_user.id
|
|
|
+ @transfer.status = :received
|
|
|
+ @transfer.reception_date = Date.today
|
|
|
+ @transfer.audit_comment = "Se dio entrada a traspaso de #{@transfer.origin_is_pointsale? ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name} a #{@transfer.destiny_is_pointsale? ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name}"
|
|
|
+ @transfer.save
|
|
|
+ @transfer.transfer_details.each do |detail|
|
|
|
+ if @transfer.destiny_is_pointsale == 1
|
|
|
+ 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
|
|
|
- # actualizar stock del producto cuando es para almacen
|
|
|
- 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
|
|
|
- stock_product = WarehouseStock.new
|
|
|
- stock_product.product_id = detail.product_id
|
|
|
- stock_product.warehouse_id = @transfer.destiny_id
|
|
|
- stock_product.stock = detail.adjustment
|
|
|
- end
|
|
|
+ stock_product = AvailableProduct.new(product_id: detail.product_id, pointsale_id: @transfer.destiny_id, stock: detail.adjustment)
|
|
|
+ end
|
|
|
+ else
|
|
|
+ # actualizar stock del producto cuando es para almacen
|
|
|
+ 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
|
|
|
+ stock_product = WarehouseStock.new(product_id: detail.product_id, warehouse_id: @transfer.destiny_id, stock: detail.adjustment)
|
|
|
end
|
|
|
- stock_product.save
|
|
|
-
|
|
|
- # guardar en bitacora de inventario
|
|
|
- move = InventoriesMove.new
|
|
|
- move.product_id = detail.product_id
|
|
|
- move.quantity = detail.adjustment
|
|
|
- move.transfer_id = @transfer.id
|
|
|
- move.move_type = "incoming"
|
|
|
- move.reason = "transfer"
|
|
|
- move.save
|
|
|
end
|
|
|
+ stock_product.save
|
|
|
+
|
|
|
+ # guardar en bitacora de inventario
|
|
|
+ move = InventoriesMove.new(product_id: detail.product_id, quantity: detail.adjustment, transfer_id: @transfer.id, move_type: "incoming", reason: "transfer")
|
|
|
+ move.save
|
|
|
+ end
|
|
|
+ respond_to do |format|
|
|
|
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
|
|
|
+ @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
|
|
|
+ reception_status =
|
|
|
if with_looses > 0 && with_surplus.zero?
|
|
|
- reception_status = 'CON PERDIDAS'
|
|
|
+ 'CON PERDIDAS'
|
|
|
elsif with_surplus > 0 && with_looses.zero?
|
|
|
- reception_status = 'CON EXCEDENTE'
|
|
|
+ 'CON EXCEDENTE'
|
|
|
elsif with_surplus > 0 && with_looses > 0
|
|
|
- reception_status = 'CON INCONSISTENCIAS'
|
|
|
+ 'CON INCONSISTENCIAS'
|
|
|
else
|
|
|
- reception_status = 'TRASPASO COMPLETO'
|
|
|
+ 'TRASPASO COMPLETO'
|
|
|
end
|
|
|
-
|
|
|
+ respond_to do |format|
|
|
|
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'
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
- # rubocop:enable Style/ConditionalAssignment
|
|
|
+
|
|
|
+ def set_transfer_data(params)
|
|
|
+ @destiny_pointsales = Pointsale.activos.ignore_current(current_user.pointsale_id)
|
|
|
+ @pre_transfers = PreTransfer.where(user_id: current_user.id)
|
|
|
+ @disable_origin = false
|
|
|
+ @disable_destiny = false
|
|
|
+ # reestablecer origen y destino
|
|
|
+ if @pre_transfers.present?
|
|
|
+ @origin_id = @pre_transfers[0].origin_is_pointsale? ? "P-#{@pre_transfers[0].origin_id}" : "W-#{@pre_transfers[0].origin_id}"
|
|
|
+ @destiny_id = @pre_transfers[0].destiny_is_pointsale? ? "P-#{@pre_transfers[0].destiny_id}" : "W-#{@pre_transfers[0].destiny_id}"
|
|
|
+ @disable_origin = true
|
|
|
+ @disable_destiny = true
|
|
|
+ elsif params.present?
|
|
|
+ @origin_id = params[:transfer][:origin_id]
|
|
|
+ @destiny_id = params[:transfer][:destiny_id]
|
|
|
+ end
|
|
|
+
|
|
|
+ if current_user.usertype == "G" || current_user.usertype == "C"
|
|
|
+ @origin_id = "P-#{current_user.pointsale_id}"
|
|
|
+ @disable_origin = true
|
|
|
+ elsif current_user.usertype == "S"
|
|
|
+ @origin_id = "W-#{current_user.warehouse_id}"
|
|
|
+ @disable_origin = true
|
|
|
+ end
|
|
|
+
|
|
|
+ if @origin_id == @destiny_id
|
|
|
+ @disable_origin = false
|
|
|
+ @disable_destiny = false
|
|
|
+ end
|
|
|
+ end
|
|
|
|
|
|
private
|
|
|
|