class PrePurchasesController < ApplicationController before_action :set_pre_purchase, only: [:show, :edit, :update, :destroy] # GET /pre_purchases/new def new end # POST /pre_purchases # POST /pre_purchases.json def create respond_to do |format| @pre_purchase = PrePurchase.new(pre_purchase_params) @pre_purchase.user_id = current_user.id @pre_purchase.get_totals if @pre_purchase.save format.json { head :no_content } format.js else format.json { render json: @pre_purchase.errors.values, status: :unprocessable_entity } end end end # PATCH/PUT /pre_purchases/1 # PATCH/PUT /pre_purchases/1.json def update @pre_purchase.quantity = params[:pre_purchase][:quantity].to_i @pre_purchase.get_totals respond_to do |format| if @pre_purchase.update(pre_purchase_params) format.json { head :ok } end end end # DELETE /pre_purchases/1 # DELETE /pre_purchases/1.json def destroy @pre_purchase.destroy respond_to do |format| if @pre_purchase.destroy format.json { head :ok } end end end def add_pre_purchase_by_barcode respond_to do |format| @product = Product.find_by(:barcode => params[:barcode]) if @product.blank? format.js { render :action => "create" } else @pre_purchase = PrePurchase.new @pre_purchase.supplier_id = params[:supplier_id] @pre_purchase.user_id = current_user.id @pre_purchase.pointsale_id = params[:pointsale_id] @pre_purchase.warehouse_id = params[:warehouse_id] @pre_purchase.product_id = @product.id @pre_purchase.quantity = 1 @pre_purchase.get_totals @pre_purchase.save format.js { render :action => "create" } end end end private # Use callbacks to share common setup or constraints between actions. def set_pre_purchase @pre_purchase = PrePurchase.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def pre_purchase_params params.require(:pre_purchase).permit(:supplier_id, :pointsale_id, :warehouse_id, :product_id, :quantity, :amount, :tax, :total, :barcode, :price_base, :exchange) end end