products_controller.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. class ProductsController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :products_path , only: [:index, :new, :show, :edit]
  6. add_breadcrumb "Nuevo " + I18n.t("breadcrumbs." + controller_name).singularize, :new_product_path, only: :new
  7. add_breadcrumb "Detalle del " + I18n.t("breadcrumbs." + controller_name).singularize , :product_path, only: :show
  8. add_breadcrumb "Editar " + I18n.t("breadcrumbs." + controller_name).singularize , :edit_product_path, only: :edit
  9. add_breadcrumb "Seguimiento de productos", :product_track_path , only: :product_track
  10. before_action :set_product, only: [:show, :edit, :update, :destroy]
  11. before_action :set_product_id, only: [:list_prices, :list_prices_variants, :edit_variants, :update_variants, :edit_from_purchase, :update_from_purchase, :update_status]
  12. before_action :get_categories, only: [:create, :update]
  13. before_action :get_attrs, only: [:show, :edit, :update]
  14. before_action :get_filters, only: [:index, :show, :edit, :new]
  15. skip_before_action :get_categories, :only => [:edit_from_purchase]
  16. # GET /products
  17. # GET /products.json
  18. def index
  19. respond_to do |format|
  20. format.html
  21. format.json { render json: ProductsDatatable.new(view_context, current_user) }
  22. end
  23. end
  24. def list_prices
  25. if @product.presentation
  26. # @available_products = @product.get_available_children(false)
  27. @pointsales = @product.get_available_children(true)
  28. end
  29. end
  30. def list_prices_variants
  31. children = Product.where("parent_id = ? and status != 0 ", params[:product_id]).pluck(:id)
  32. @variants = AvailableProduct.where("pointsale_id = ? and product_id IN (?)", params[:pointsale_id], children).joins(:product).select(:name, :id, :product_id, :attributes_json, :sku)
  33. @variants_json = Array.new
  34. @variants.each do |v|
  35. @variants_json.push({:name => v.sku + " " + v.name + " <br><small>" + Product.find(v.product_id).display_attributes + "</small>", :price => Product.find(v.product_id).get_price_sale(params[:pointsale_id]), :available_product_id => v.id })
  36. end
  37. render :json => @variants_json.to_json
  38. end
  39. def edit_variants
  40. end
  41. def update_variants
  42. if @product.is_parent
  43. @product.update_attributes_to_variants(params[:new_size_list], params[:new_color_list], params[:new_style_list])
  44. @product.save_new_attributes(params[:mynewsizes], params[:mynewcolors], params[:mynewstyles])
  45. end
  46. respond_to do |format|
  47. if @product.save(:validate => false)
  48. format.html { redirect_to products_url, success: 'Se modificaron las variantes del producto ' + @product.sku + ' - ' + @product.name + '.' }
  49. format.json { render :show, status: :ok, location: @product }
  50. else
  51. format.html { render :edit_variants }
  52. format.json { render json: @product.errors, status: :unprocessable_entity }
  53. end
  54. end
  55. end
  56. # GET /products/1
  57. # GET /products/1.json
  58. def show
  59. end
  60. # GET /products/new
  61. def new
  62. @product = Product.new
  63. if params[:remoto].present?
  64. @remoto = params[:remoto]
  65. @pointsale = params[:pointsale]
  66. @warehouse = params[:warehouse]
  67. @supplier = params[:supplier]
  68. @exchange = params[:exchange]
  69. end
  70. @product.price_base = nil
  71. @product.price_sale = nil
  72. end
  73. # GET /products/1/edit
  74. def edit
  75. @with_presentation = true
  76. end
  77. # POST /products
  78. # POST /products.json
  79. def create
  80. @with_presentation = true
  81. @product = Product.new(product_params)
  82. if @product.presentation
  83. @product.is_parent = true
  84. end
  85. respond_to do |format|
  86. message = 'El producto ' + @product.sku + ' fue creado.'
  87. @product.audit_comment = message
  88. if @product.save
  89. @product.save_variants
  90. ##--- Para cuando se agrega un producto desde purchase
  91. if params[:remoto] == "true"
  92. ##--- Guardar pre_purchase
  93. @pre_purchases = Array.new
  94. if @product.presentation && @product.is_parent
  95. @product.children.each do |variant|
  96. save_pre_purchase(variant)
  97. set_available
  98. @pre_purchases << @pre_purchase
  99. end
  100. else
  101. save_pre_purchase(@product)
  102. set_available
  103. @pre_purchases << @pre_purchase
  104. end
  105. format.json { head :no_content }
  106. format.js
  107. else
  108. format.html { redirect_to products_url, success: message }
  109. format.json { render :show, status: :created, location: @product }
  110. end
  111. else
  112. format.html { render :new }
  113. format.json { render json: @product.errors, status: :unprocessable_entity }
  114. end
  115. end
  116. end
  117. # PATCH/PUT /products/1
  118. # PATCH/PUT /products/1.json
  119. def update
  120. respond_to do |format|
  121. @product.skip_sku_validation = true
  122. message = 'El producto ' + @product.sku + ' fue modificado.'
  123. @product.audit_comment = message
  124. if @product.update(product_params)
  125. if @product.is_parent
  126. @product.children.each_with_index do |variant, index|
  127. variant.skip_sku_validation = true
  128. params_to_edit = product_params
  129. params_to_edit[:sku] = @product.sku + "#{index + 1}" + "A"
  130. variant.update_attributes(params_to_edit)
  131. end
  132. end
  133. format.html { redirect_to products_url, success: message }
  134. format.json { render :show, status: :ok, location: @product }
  135. else
  136. format.html { render :edit }
  137. format.json { render json: @product.errors, status: :unprocessable_entity }
  138. end
  139. end
  140. end
  141. def edit_from_purchase
  142. respond_to do |format|
  143. purchase_in_dollars = params[:is_in_dollars]
  144. exchange = params[:exchange].to_f
  145. if purchase_in_dollars == "true"
  146. @product.price_base_dollars = params[:product][:price_base].to_f
  147. price_in_peso = @product.price_base_dollars * exchange
  148. @product.price_base = @product.price_base.present? ? price_in_peso : nil
  149. @suggested_price_sale = (price_in_peso * (@pos_config.gain_margin / 100)) + price_in_peso
  150. else
  151. @product.price_base = params[:product][:price_base].to_f
  152. price_in_dollars = @product.price_base / exchange if exchange != 0
  153. @product.price_base_dollars = @product.price_base_dollars.present? ? price_in_dollars : nil
  154. @suggested_price_sale = (@product.price_base * (@pos_config.gain_margin / 100)) + @product.price_base
  155. end
  156. @product.audit_comment = 'El producto ' + @product.sku + ' fue modificado.'
  157. @product.save
  158. format.json { head :no_content }
  159. format.js
  160. end
  161. end
  162. def update_from_purchase
  163. ##--- Si el producto es hijo, sacar el registro del padre para cambiar todos los valores
  164. if !@product.parent_id.nil?
  165. @product = Product.find(@product.parent_id)
  166. end
  167. message = "Se ha modificado el precio de venta base del producto " + @product.name
  168. respond_to do |format|
  169. if @product.update(product_params)
  170. @product.children.each_with_index do |variant, index|
  171. variant.update_attributes(product_params)
  172. variant.save
  173. end
  174. @product.audit_comment = message
  175. format.json { head :no_content }
  176. format.js { flash[:success] = message }
  177. else
  178. format.js { render :edit_from_purchase }
  179. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  180. end
  181. end
  182. end
  183. def update_status
  184. product = @product
  185. if product.active?
  186. product.status = 2
  187. elsif product.inactive?
  188. product.status = 1
  189. end
  190. if product.presentation
  191. product.children.each do |pv|
  192. pv.status = product.status
  193. pv.save(:validate => false)
  194. end
  195. end
  196. respond_to do |format|
  197. message = "El producto " + product.name + " fue "+ (product.active? ? "activado" : "desactivado")+"."
  198. product.audit_comment = message
  199. if product.save(:validate => false)
  200. format.html { redirect_to products_url, warning: message }
  201. # format.json { render :show, status: :ok, location: @pointsale }
  202. format.json { head :no_content }
  203. else
  204. format.html { redirect_to products_url }
  205. format.json { render json: @product.errors, status: :unprocessable_entity }
  206. end
  207. end
  208. end
  209. # DELETE /products/1
  210. # DELETE /products/1.json
  211. def destroy
  212. respond_to do |format|
  213. message = "El producto " + @product.name + " fue eliminado."
  214. @product.audit_comment = message
  215. if @product.update_attributes(:status => 0)
  216. if @product.is_parent
  217. @product.children.each_with_index do |variant|
  218. variant.update_attributes(:status => 0)
  219. variant.save
  220. end
  221. end
  222. format.html { redirect_to products_url, warning: message }
  223. format.json { head :no_content }
  224. else
  225. format.html { redirect_to products_url }
  226. format.json { render json: @product.errors, status: :unprocessable_entity }
  227. end
  228. end
  229. end
  230. def validate_unique_barcode
  231. respond_to do |format|
  232. @product = Product.find_by(:barcode => params[:barcode])
  233. unless @product.blank?
  234. format.js
  235. else
  236. format.js {head :ok}
  237. end
  238. end
  239. end
  240. def product_track #--Seguimiento de productos
  241. # @sales = SalesDetail.new
  242. product_id = params[:product_id]
  243. pointsale_id = params[:pointsale_id]
  244. if product_id.present?
  245. respond_to do |format|
  246. if !pointsale_id.present?
  247. @sales = SalesDetail.includes(:sale).where( :sales =>{status:1 }, :sales_details =>{product_id: product_id}).order("sales_details.id DESC")
  248. else
  249. @sales = Pointsale.find(pointsale_id).sales_details.includes(:sale).where(:sales =>{status:1 }, :sales_details =>{product_id: product_id}).order("sales_details.id DESC")
  250. end
  251. format.js
  252. end
  253. end
  254. end
  255. private
  256. # Use callbacks to share common setup or constraints between actions.
  257. def set_product
  258. @product = Product.find(params[:id])
  259. end
  260. def set_product_id
  261. @product = Product.find(params[:product_id])
  262. end
  263. def get_filters
  264. if params[:current_page].blank?
  265. @current_page = 1
  266. else
  267. @current_page = params[:current_page]
  268. end
  269. @filter = params[:filter]
  270. end
  271. # Never trust parameters from the scary internet, only allow the white list through.
  272. def product_params
  273. params.require(:product).permit(:sku, :name, :description, :price_base, :price_sale, :img_product, :img_product_cache, :presentation, :inventory, :unit_id, :content, :status, :categorias, :category_ids, :include_purchase_tax, :include_sale_tax, :barcode, :is_in_dollars, :price_base_dollars, :size_list => [], :color_list => [], :style_list => [], :available_products_attributes => [:id, :price_sale])
  274. end
  275. def get_categories
  276. @categories = Array.new
  277. unless params[:product][:category_ids].blank?
  278. @categories << Category.find(params[:product][:category_ids])
  279. @product.categories = @categories
  280. end
  281. end
  282. def save_pre_purchase(product)
  283. @pre_purchase = PrePurchase.new
  284. @pre_purchase.supplier_id = params[:supplier]
  285. @pre_purchase.pointsale_id = params[:pointsale] if params[:pointsale].present?
  286. @pre_purchase.warehouse_id = params[:warehouse] if params[:warehouse].present?
  287. @pre_purchase.exchange = params[:exchange] if params[:exchange].present?
  288. @pre_purchase.user_id = current_user.id
  289. @pre_purchase.product_id = product.id
  290. @pre_purchase.quantity = 1
  291. @pre_purchase.get_totals
  292. @pre_purchase.save
  293. end
  294. def set_available
  295. ##--- agregar a productos disponibles ya sea punto de venta o almacen
  296. if params[:pointsale].present?
  297. availableProduct = AvailableProduct.new
  298. availableProduct.pointsale_id = @pre_purchase.pointsale_id
  299. else
  300. availableProduct = WarehouseStock.new
  301. availableProduct.warehouse_id = @pre_purchase.warehouse_id
  302. end
  303. availableProduct.product_id = @pre_purchase.product_id
  304. availableProduct.stock_min = params[:stock_min]
  305. availableProduct.stock_max = params[:stock_max]
  306. availableProduct.save
  307. end
  308. def get_attrs
  309. @attrs = Array.new
  310. @product.variants_attributes.each do |attri|
  311. @attrs << attri.context
  312. end
  313. end
  314. end