products_controller.rb 12 KB

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