| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- class ProductWastesController < ApplicationController
- ##--- Abilities
- load_and_authorize_resource
- before_action :set_product_waste, only: [:show, :edit, :update, :destroy]
- ##--- Breadcrum_rails
- add_breadcrumb I18n.t("breadcrumbs." + controller_name), :product_wastes_path
- add_breadcrumb "Nueva merma de productos", :new_product_waste_path, only: :new
- before_action :set_pos_config, only: [:show, :edit, :update, :destroy]
- # GET /product_wastes
- # GET /product_wastes.json
- def index
- @product_wastes = ProductWaste.includes(:product, :user, :pointsale, :warehouse).activos
- @product_wastes =
- if current_user.usertype == 'G' || current_user.usertype == 'C'
- @product_wastes.where(pointsale_id: current_user.pointsale_id)
- elsif current_user.usertype == 'S'
- @product_wastes.where(warehouse_id: current_user.warehouse_id)
- else
- @product_wastes
- end
- end
- # GET /product_wastes/new
- def new; end
- # POST /product_wastes
- # POST /product_wastes.json
- def create
- @product_waste = ProductWaste.new(product_waste_params)
- @product_waste.user_id = current_user.id
- @product_waste.status = :active
- if current_user.usertype == 'S'
- @product_waste.warehouse_id = current_user.warehouse_id
- @stock = WarehouseStock.find_by(warehouse_id: @product_waste.warehouse_id, product_id: @product_waste.product_id)
- else
- @product_waste.pointsale_id = current_user.pointsale_id
- @stock = AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
- end
- respond_to do |format|
- if @stock.stock >= @product_waste.quantity
- @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} creada"
- if @product_waste.save
- @stock.stock -= @product_waste.quantity
- @stock.save
- format.js
- else
- format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
- end
- else
- @product_waste.errors.add(:base, "Stock insuficiente, sólo se cuenta con #{@stock.stock} unidades del producto #{@product_waste.product.name}")
- format.json { render json: @product_waste.errors.values, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /product_wastes/1
- # DELETE /product_wastes/1.json
- def destroy
- respond_to do |format|
- @product_waste.audit_comment = "Merma del producto #{@product_waste.product.name} eliminada."
- if @product_waste.update_attributes(status: :inactive)
- @stock = current_user.usertype == "S" ? WarehouseStock.find_by(warehouse_id: @product_waste.warehouse_id, product_id: @product_waste.product_id) : AvailableProduct.find_by(pointsale_id: @product_waste.pointsale_id, product_id: @product_waste.product_id)
- @stock.stock = @stock.stock + @product_waste.quantity
- @stock.save
- format.html { redirect_to product_wastes_url, warning: 'Merma de producto eliminada.' }
- format.json { head :ok }
- else
- format.json { render json: @product_waste.errors, status: :unprocessable_entity }
- end
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_product_waste
- @product_waste = ProductWaste.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def product_waste_params
- params.require(:product_waste).permit(:warehouse_id, :pointsale_id, :product_id, :quantity, :reason, :status)
- end
- end
|