transfers_controller.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. class TransfersController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :transfers_path
  6. add_breadcrumb "Nuevo traspaso", :new_transfer_path, only: :new
  7. add_breadcrumb "Detalle del traspaso", :transfer_path, only: :show
  8. add_breadcrumb "Editar traspaso", :edit_transfer_path, only: :edit
  9. add_breadcrumb "Verificar traspaso", :verify_transfer_path, only: :verify_transfer
  10. before_action :set_transfer, only: [:show, :edit, :update, :destroy]
  11. before_action :get_filters, only: [:index, :show, :edit, :new]
  12. # GET /transfers
  13. # GET /transfers.json
  14. def index
  15. if current_user.usertype == 'A'
  16. @transfers = Transfer.all.includes(:user, :received_by).order('id DESC, transfer_date DESC')
  17. elsif current_user.usertype == 'G' || current_user.usertype == 'C'
  18. @received = Transfer.includes(:user, :received_by).where(destiny_id: current_user.pointsale_id).order('id DESC, transfer_date DESC')
  19. @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.pointsale_id).order('id DESC, transfer_date DESC')
  20. elsif current_user.usertype == 'S'
  21. @received = Transfer.includes(:user, :received_by).where(destiny_id: current_user.warehouse_id).order('id DESC, transfer_date DESC')
  22. @sent = Transfer.includes(:user, :received_by).where(origin_id: current_user.warehouse_id).order('id DESC, transfer_date DESC')
  23. end
  24. end
  25. # GET /transfers/1
  26. # GET /transfers/1.json
  27. def show; end
  28. # GET /transfers/new
  29. # rubocop:disable Style/ZeroLengthPredicate
  30. def new
  31. @transfer = Transfer.new
  32. @destiny_pointsales = Pointsale.activos.ignore_current(current_user.pointsale_id)
  33. @pre_transfers = PreTransfer.where(user_id: current_user.id)
  34. @disable_origin = false
  35. @disable_destiny = false
  36. @disabled_button = true
  37. if @pre_transfers.size > 0
  38. @origin_id = @pre_transfers[0].origin_is_pointsale == 1 ? "P-#{@pre_transfers[0].origin_id}" : "W-#{@pre_transfers[0].origin_id}"
  39. @destiny_id = @pre_transfers[0].destiny_is_pointsale == 1 ? "P-#{@pre_transfers[0].destiny_id}" : "W-#{@pre_transfers[0].destiny_id}"
  40. @disable_origin = true
  41. @disable_destiny = true
  42. @disabled_button = false
  43. end
  44. if current_user.usertype == "G" || current_user.usertype == "C"
  45. @origin_id = "P-#{current_user.pointsale_id}"
  46. @disable_origin = true
  47. elsif current_user.usertype == "S"
  48. @origin_id = "W-#{current_user.warehouse_id}"
  49. @disable_origin = true
  50. end
  51. end
  52. # rubocop:enble Style/ZeroLengthPredicate
  53. # GET /transfers/1/edit
  54. def edit; end
  55. # POST /transfers
  56. # POST /transfers.json
  57. def create
  58. @transfer = Transfer.new(transfer_params)
  59. pre_transfers = PreTransfer.where(user_id: current_user.id)
  60. @transfer.user_id = current_user.id
  61. @transfer.status = :pending
  62. @transfer.origin_is_pointsale = params[:transfer][:origin_id].first == 'P' ? 1 : 0
  63. @transfer.destiny_is_pointsale = params[:transfer][:destiny_id].first == 'P' ? 1 : 0
  64. @transfer.origin_id = params[:transfer][:origin_id][2, params[:transfer][:origin_id].length]
  65. @transfer.destiny_id = params[:transfer][:destiny_id][2, params[:transfer][:destiny_id].length]
  66. respond_to do |format|
  67. @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."
  68. if @transfer.save
  69. pre_transfers.each do |pre|
  70. detail = TransferDetail.new
  71. detail.transfer_id = @transfer.id
  72. detail.product_id = pre.product_id
  73. detail.quantity = pre.quantity
  74. detail.adjustment = pre.quantity
  75. detail.status = "pending"
  76. @transfer.transfer_details << detail
  77. pre.destroy
  78. end
  79. 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}")
  80. format.html { redirect_to transfers_path, success: message }
  81. format.json { render :show, status: :created, location: @transfer }
  82. else
  83. format.html { render :new }
  84. format.json { render json: @transfer.errors, status: :unprocessable_entity }
  85. end
  86. end
  87. end
  88. # PATCH/PUT /transfers/1
  89. # PATCH/PUT /transfers/1.json
  90. def update
  91. respond_to do |format|
  92. if @transfer.update(transfer_params)
  93. format.html { redirect_to @transfer, notice: 'Transfer was successfully updated.' }
  94. format.json { render :show, status: :ok, location: @transfer }
  95. else
  96. format.html { render :edit }
  97. format.json { render json: @transfer.errors, status: :unprocessable_entity }
  98. end
  99. end
  100. end
  101. # DELETE /transfers/1
  102. # DELETE /transfers/1.json
  103. def destroy
  104. @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} eliminado."
  105. @transfer.destroy
  106. respond_to do |format|
  107. format.html { redirect_to transfers_url, notice: 'Transfer was successfully destroyed.' }
  108. format.json { head :no_content }
  109. end
  110. end
  111. def verify_transfer
  112. @transfer = Transfer.find(params[:transfer_id])
  113. end
  114. # para guardar la cantidad recibida por producto en el traspaso
  115. def detail_adjustment
  116. respond_to do |format|
  117. @transfer = Transfer.find(params[:transfer_id])
  118. @transfer.transfer_details.each do |detail|
  119. # rubocop:disable Style/Next
  120. if detail.id == params[:detail_id].to_i
  121. detail.adjustment = params[:transfer_details][:adjustment].to_i
  122. if detail.adjustment > detail.quantity
  123. detail.has_looses = 0
  124. detail.has_surplus = 1
  125. elsif detail.adjustment < detail.quantity
  126. detail.has_looses = 1
  127. detail.has_surplus = 0
  128. end
  129. if detail.save
  130. format.json { head :ok }
  131. end
  132. end
  133. # rubocop:enable Style/Next
  134. end
  135. end
  136. end
  137. # rubocop:disable Metrics/BlockLength
  138. def accept_transfer
  139. respond_to do |format|
  140. @transfer = Transfer.find(params[:transfer_id])
  141. @transfer.observations = params[:transfer][:observations]
  142. @transfer.received_by_id = current_user.id
  143. @transfer.status = :received
  144. @transfer.reception_date = Date.today
  145. @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}"
  146. @transfer.save
  147. @transfer.transfer_details.each do |detail|
  148. if @transfer.destiny_is_pointsale == 1
  149. stock_product = AvailableProduct.find_by(product_id: detail.product_id, pointsale_id: @transfer.destiny_id)
  150. if stock_product.present?
  151. stock_product.stock = 0 if stock_product.stock.blank?
  152. stock_product.stock += detail.adjustment
  153. else
  154. stock_product = AvailableProduct.new
  155. stock_product.product_id = detail.product_id
  156. stock_product.pointsale_id = @transfer.destiny_id
  157. stock_product.stock = detail.adjustment
  158. end
  159. else
  160. # actualizar stock del producto cuando es para almacen
  161. stock_product = WarehouseStock.find_by(product_id: detail.product_id, warehouse_id: @transfer.destiny_id)
  162. if stock_product.present?
  163. stock_product.stock = 0 if stock_product.stock.blank?
  164. stock_product.stock += detail.adjustment
  165. else
  166. stock_product = WarehouseStock.new
  167. stock_product.product_id = detail.product_id
  168. stock_product.warehouse_id = @transfer.destiny_id
  169. stock_product.stock = detail.adjustment
  170. end
  171. end
  172. stock_product.save
  173. # guardar en bitacora de inventario
  174. move = InventoriesMove.new
  175. move.product_id = detail.product_id
  176. move.quantity = detail.adjustment
  177. move.transfer_id = @transfer.id
  178. move.move_type = "incoming"
  179. move.reason = "transfer"
  180. move.save
  181. end
  182. format.js { flash[:success] = "Traspaso realizado correctamente." }
  183. end
  184. end
  185. # rubocop:enable Metrics/BlockLength
  186. # rubocop:disable Style/ConditionalAssignment
  187. def print_receipt
  188. respond_to do |format|
  189. @transfer = Transfer.find(params[:transfer_id])
  190. origin = @transfer.origin_is_pointsale == 1 ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name
  191. destiny = @transfer.destiny_is_pointsale == 1 ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name
  192. with_looses = @transfer.transfer_details.where(has_looses: 1).count
  193. with_surplus = @transfer.transfer_details.where(has_surplus: 1).count
  194. if with_looses > 0 && with_surplus.zero?
  195. reception_status = 'CON PERDIDAS'
  196. elsif with_surplus > 0 && with_looses.zero?
  197. reception_status = 'CON EXCEDENTE'
  198. elsif with_surplus > 0 && with_looses > 0
  199. reception_status = 'CON INCONSISTENCIAS'
  200. else
  201. reception_status = 'TRASPASO COMPLETO'
  202. end
  203. format.pdf do
  204. 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'
  205. end
  206. end
  207. end
  208. # rubocop:enable Style/ConditionalAssignment
  209. private
  210. # Use callbacks to share common setup or constraints between actions.
  211. def set_transfer
  212. @transfer = Transfer.find(params[:id])
  213. end
  214. def get_filters
  215. @current_page = params[:current_page].blank? ? 1 : params[:current_page]
  216. @filter = params[:filter]
  217. end
  218. # Never trust parameters from the scary internet, only allow the white list through.
  219. def transfer_params
  220. 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])
  221. end
  222. end