transfers_controller.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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
  28. end
  29. # GET /transfers/new
  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"
  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. # GET /transfers/1/edit
  53. def edit
  54. 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. if detail.id == params[:detail_id].to_i
  120. detail.adjustment = params[:transfer_details][:adjustment].to_i
  121. if detail.adjustment > detail.quantity
  122. detail.has_looses = 0
  123. detail.has_surplus = 1
  124. elsif detail.adjustment < detail.quantity
  125. detail.has_looses = 1
  126. detail.has_surplus = 0
  127. end
  128. if detail.save
  129. format.json { head :ok }
  130. end
  131. end
  132. end
  133. end
  134. end
  135. def accept_transfer
  136. respond_to do |format|
  137. @transfer = Transfer.find(params[:transfer_id])
  138. @transfer.observations = params[:transfer][:observations]
  139. @transfer.received_by_id = current_user.id
  140. @transfer.status = :received
  141. @transfer.reception_date = Date.today
  142. @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}"
  143. @transfer.save
  144. @transfer.transfer_details.each do |detail|
  145. if @transfer.destiny_is_pointsale == 1
  146. stockProduct = AvailableProduct.find_by(:product_id => detail.product_id, :pointsale_id => @transfer.destiny_id)
  147. if stockProduct.present?
  148. stockProduct.stock = 0 if stockProduct.stock.blank?
  149. stockProduct.stock += detail.adjustment
  150. stockProduct.save
  151. else
  152. stockProduct = AvailableProduct.new
  153. stockProduct.product_id = detail.product_id
  154. stockProduct.pointsale_id = @transfer.destiny_id
  155. stockProduct.stock = detail.adjustment
  156. stockProduct.save
  157. end
  158. else
  159. # actualizar stock del producto cuando es para almacen
  160. stockProduct = WarehouseStock.find_by(:product_id => detail.product_id, :warehouse_id => @transfer.destiny_id)
  161. if stockProduct.present?
  162. stockProduct.stock = 0 if stockProduct.stock.blank?
  163. stockProduct.stock += detail.adjustment
  164. stockProduct.save
  165. else
  166. stockProduct = WarehouseStock.new
  167. stockProduct.product_id = detail.product_id
  168. stockProduct.warehouse_id = @transfer.destiny_id
  169. stockProduct.stock = detail.adjustment
  170. stockProduct.save
  171. end
  172. end
  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. def print_receipt
  186. respond_to do |format|
  187. @transfer = Transfer.find(params[:transfer_id])
  188. origin = @transfer.origin_is_pointsale == 1 ? Pointsale.find(@transfer.origin_id).name : Warehouse.find(@transfer.origin_id).name
  189. destiny = @transfer.destiny_is_pointsale == 1 ? Pointsale.find(@transfer.destiny_id).name : Warehouse.find(@transfer.destiny_id).name
  190. with_looses = @transfer.transfer_details.where(:has_looses => 1).count
  191. with_surplus = @transfer.transfer_details.where(:has_surplus => 1).count
  192. if with_looses > 0 && with_surplus == 0
  193. reception_status = 'CON PERDIDAS'
  194. elsif with_surplus > 0 && with_looses == 0
  195. reception_status = 'CON EXCEDENTE'
  196. elsif with_surplus > 0 && with_looses > 0
  197. reception_status = 'CON INCONSISTENCIAS'
  198. else
  199. reception_status = 'TRASPASO COMPLETO'
  200. end
  201. format.pdf do
  202. render pdf: "traspaso_#{@transfer.id}",
  203. template: "transfers/receipt.pdf.erb",
  204. layout: 'receipt.html.erb',
  205. locals: { :transfer => @transfer, :reception_status => reception_status, :origin => origin, :destiny => destiny },
  206. show_as_html: params.key?('debug'),
  207. page_width: '80mm',
  208. page_height: '300mm'
  209. end
  210. end
  211. end
  212. private
  213. # Use callbacks to share common setup or constraints between actions.
  214. def set_transfer
  215. @transfer = Transfer.find(params[:id])
  216. end
  217. def get_filters
  218. if params[:current_page].blank?
  219. @current_page = 1
  220. else
  221. @current_page = params[:current_page]
  222. end
  223. @filter = params[:filter]
  224. end
  225. # Never trust parameters from the scary internet, only allow the white list through.
  226. def transfer_params
  227. 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])
  228. end
  229. end