pre_purchases_controller.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. class PrePurchasesController < ApplicationController
  2. before_action :set_pre_purchase, only: [:show, :edit, :update, :destroy]
  3. # GET /pre_purchases/new
  4. def new
  5. end
  6. # POST /pre_purchases
  7. # POST /pre_purchases.json
  8. def create
  9. respond_to do |format|
  10. @pre_purchase = PrePurchase.new(pre_purchase_params)
  11. @pre_purchase.user_id = current_user.id
  12. @pre_purchase.get_totals
  13. if @pre_purchase.save
  14. format.json { head :no_content }
  15. format.js
  16. else
  17. format.json { render json: @pre_purchase.errors.values, status: :unprocessable_entity }
  18. end
  19. end
  20. end
  21. # PATCH/PUT /pre_purchases/1
  22. # PATCH/PUT /pre_purchases/1.json
  23. def update
  24. @pre_purchase.quantity = params[:pre_purchase][:quantity].to_i
  25. @pre_purchase.get_totals
  26. respond_to do |format|
  27. if @pre_purchase.update(pre_purchase_params)
  28. format.json { head :ok }
  29. end
  30. end
  31. end
  32. # DELETE /pre_purchases/1
  33. # DELETE /pre_purchases/1.json
  34. def destroy
  35. @pre_purchase.destroy
  36. respond_to do |format|
  37. if @pre_purchase.destroy
  38. format.json { head :ok }
  39. end
  40. end
  41. end
  42. def add_pre_purchase_by_barcode
  43. respond_to do |format|
  44. @product = Product.find_by(:barcode => params[:barcode])
  45. if @product.blank?
  46. format.js { render :action => "create" }
  47. else
  48. @pre_purchase = PrePurchase.new
  49. @pre_purchase.supplier_id = params[:supplier_id]
  50. @pre_purchase.user_id = current_user.id
  51. @pre_purchase.pointsale_id = params[:pointsale_id]
  52. @pre_purchase.warehouse_id = params[:warehouse_id]
  53. @pre_purchase.product_id = @product.id
  54. @pre_purchase.quantity = 1
  55. @pre_purchase.get_totals
  56. @pre_purchase.save
  57. format.js { render :action => "create" }
  58. end
  59. end
  60. end
  61. private
  62. # Use callbacks to share common setup or constraints between actions.
  63. def set_pre_purchase
  64. @pre_purchase = PrePurchase.find(params[:id])
  65. end
  66. # Never trust parameters from the scary internet, only allow the white list through.
  67. def pre_purchase_params
  68. params.require(:pre_purchase).permit(:supplier_id, :pointsale_id, :warehouse_id, :product_id, :quantity, :amount, :tax, :total, :barcode, :price_base, :exchange)
  69. end
  70. end