pre_sales_controller.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. class PreSalesController < ApplicationController
  2. before_action :set_pre_sale, only: [:show, :edit, :update, :destroy]
  3. # POST /pre_sales
  4. # POST /pre_sales.json
  5. def create
  6. @pre_sale = PreSale.find_by(product_id: params[:pre_sale][:product_id], user_id: current_user.id)
  7. if @pre_sale.present? # si ya existe un pre sale con ese producto
  8. @pre_sale.quantity += 1
  9. else
  10. @is_new = true
  11. @pre_sale = PreSale.new(pre_sale_params)
  12. @pre_sale.user_id = current_user.id
  13. end
  14. total_in_pre_sales = PreSale.where(user_id: @pre_sale.user_id).sum(:total)
  15. #precio unitario
  16. @pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
  17. @pre_sale.get_totals
  18. total_in_pre_sales += @pre_sale.total
  19. respond_to do |format|
  20. if has_enough_stock?(@pre_sale)
  21. #cuando la venta es a credito checar que el credito permita esta nueva venta
  22. case @pre_sale.sale_type
  23. when "credit" then
  24. debiting = @pre_sale.customer.get_debiting
  25. if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales) && @pre_sale.save
  26. format.js
  27. else
  28. @pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
  29. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  30. format.js
  31. end
  32. when "cash", "reserved" then
  33. if @pre_sale.save
  34. format.js
  35. else
  36. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  37. end
  38. end
  39. else
  40. @pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
  41. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  42. format.js
  43. end
  44. end
  45. end
  46. # PATCH/PUT /pre_sales/1
  47. # PATCH/PUT /pre_sales/1.json
  48. def update
  49. dup_presale = @pre_sale.dup
  50. @pre_sale.quantity = params[:pre_sale][:quantity].to_i
  51. availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: @pre_sale.product_id)
  52. total_in_pre_sales = PreSale.where(user_id: current_user.id, product_id: @pre_sale.product_id).where.not(id: @pre_sale.id).sum(:total)
  53. respond_to do |format|
  54. if availableProduct.stock >= @pre_sale.quantity
  55. @pre_sale.get_totals
  56. total_in_pre_sales += @pre_sale.total
  57. case @pre_sale.sale_type
  58. when "credit" then
  59. if @pre_sale.customer.sale_approved?(@pre_sale.customer.get_debiting + total_in_pre_sales)
  60. @pre_sale.update(pre_sale_params)
  61. format.json { render json: @pre_sale }
  62. else
  63. @pre_sale.errors.add(:base, "El cliente ya no tiene crédito disponible")
  64. format.json { render json: { errors: @pre_sale.errors.values, presale: dup_presale, quantity: dup_presale.quantity }, status: :unprocessable_entity }
  65. end
  66. when "cash", "reserved" then
  67. @pre_sale.update(pre_sale_params)
  68. format.json { render json: @pre_sale }
  69. end
  70. else
  71. @pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
  72. format.json { render json: { errors: @pre_sale.errors.values, presale: dup_presale, quantity: dup_presale.quantity }, status: :unprocessable_entity }
  73. end
  74. end
  75. end
  76. # DELETE /pre_sales/1
  77. # DELETE /pre_sales/1.json
  78. def destroy
  79. respond_to do |format|
  80. if @pre_sale.destroy
  81. format.json { head :ok }
  82. end
  83. end
  84. end
  85. def add_pre_sale_by_barcode
  86. total_in_pre_sales = 0
  87. pointsale = OpenCashRegister.get_pointsale(params[:open_cash_register_id], "open_cash_register")
  88. @product = pointsale.products.find_by("barcode = ? and stock > 0", params[:barcode])
  89. respond_to do |format|
  90. if @product.blank?
  91. @pre_sale = PreSale.new
  92. @pre_sale.errors.add(:product_id, "No se encontro el producto")
  93. format.js { render action: "create" }
  94. else
  95. total_in_pre_sales = PreSale.where(user_id: current_user.id).sum(:total)
  96. @pre_sale = PreSale.new(customer_id: params[:customer_id], user_id: current_user.id, open_cash_register_id: params[:open_cash_register_id], product_id: @product.id, sale_type: params[:sale_type], quantity: 1)
  97. #precio unitario
  98. @pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
  99. @pre_sale.get_totals
  100. total_in_pre_sales += @pre_sale.total
  101. #cuando la venta es a credito checar que el credito permita esta nueva venta
  102. if has_enough_stock?(@pre_sale)
  103. @is_new = true if @pre_sale.quantity == 1
  104. #cuando la venta es a credito checar que el credito permita esta nueva venta
  105. case @pre_sale.sale_type
  106. when "credit" then
  107. debiting = @pre_sale.customer.get_debiting
  108. if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales) && @pre_sale.save
  109. format.js { render action: "create" }
  110. else
  111. @pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
  112. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  113. format.js
  114. end
  115. when "cash", "reserved" then
  116. if @pre_sale.save
  117. format.js { render action: "create" }
  118. else
  119. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  120. end
  121. end
  122. else
  123. @pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
  124. format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
  125. format.js { render action: "create" }
  126. end
  127. end
  128. end
  129. end
  130. def update_presale_customer
  131. @pre_sales = PreSale.where(user_id: current_user.id)
  132. @pre_sales.update_all(customer_id: params[:customer_id], sale_type: 1) if @pre_sales.present?
  133. respond_to do |format|
  134. format.json { head :ok }
  135. end
  136. end
  137. def update_saletype
  138. pre_sales = PreSale.where(user_id: current_user.id)
  139. type =
  140. case params[:type]
  141. when "credit" then
  142. 0
  143. when "cash" then
  144. 1
  145. when "reserved" then
  146. 3
  147. end
  148. respond_to do |format|
  149. if pre_sales.blank?
  150. format.json { head :ok }
  151. else
  152. if params[:type] != "credit" || pre_sales[0].customer.sale_approved?(pre_sales[0].customer.get_debiting + pre_sales.sum(:total))
  153. pre_sales.update_all(sale_type: type)
  154. format.json { head :ok }
  155. else
  156. pre_sales[0].errors.add(:base, "El cliente ya no tiene crédito disponible")
  157. format.json { render json: pre_sales[0].errors.values, status: :unprocessable_entity }
  158. end
  159. end
  160. end
  161. end
  162. private
  163. # Use callbacks to share common setup or constraints between actions.
  164. def set_pre_sale
  165. @pre_sale = PreSale.find(params[:id])
  166. end
  167. # Never trust parameters from the scary internet, only allow the white list through.
  168. def pre_sale_params
  169. params.require(:pre_sale).permit(:customer_id, :user_id, :open_cash_register_id, :sale_type, :product_id, :quantity, :amount, :tax, :discount, :total, :barcode, :haggle)
  170. end
  171. def has_enough_stock?(pre_sale)
  172. availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: pre_sale.product_id)
  173. if availableProduct.stock >= pre_sale.quantity
  174. return true
  175. else
  176. return false
  177. end
  178. end
  179. end