class OutputsController < ApplicationController
authorize_resource
before_action :authenticate_user!
before_action :set_output, only: [:show, :edit, :update, :destroy]
before_action :set_date
def index
@outputs = Output.where("date = ?", @date)
end
def show
end
def new
@output = Output.new
end
def edit
end
def create
@output = Output.new(output_params)
@output.salary = calc
respond_to do |format|
if @output.save
format.json { head :no_content }
format.js
else
format.json { render json: @output.errors, status: :unprocessable_entity }
end
end
end
def update
op = output_params
op[:salary] = calc
respond_to do |format|
if @output.update(op)
format.json { respond_with_bip(@output) }
format.js
else
format.json { respond_with_bip(@output) }
end
end
end
def calc
salary = 0
@output.output_products.each do |op|
plan = op.product.machine_output.production / 12 * op.work_time
logger.info "PLAN " + plan.to_s
logger.info "OPAM " + op.output_amount.to_s + " PLAN " + plan.to_s
percent = op.output_amount * 100 / plan
logger.info "PERCENTAGE " + percent.to_s
salary += (percent * @output.employee.base_pay / 100 * op.work_time)
end
logger.info "SALARY " + salary.to_s
salary
end
def destroy
@output.destroy
respond_to do |format|
format.js { }
format.json { head :no_content }
end
end
def outpart
@outputs = Output.where("date = ?", @date)
respond_to do |format|
format.html { render :partial => 'outputtable' , :locals => {:outputs => @outputs}} # index.html.erb
# format.json { render json: @outputs, :callback => params[:callback] }
format.xlsx
end
end
def set_date
@date = params[:date] || Time.now.strftime("%Y-%m-%d")
end
private
# Use callbacks to share common setup or constraints between actions.
def set_output
@output = Output.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def output_params
params.require(:output).permit(:salary, :date, :employee_id, :work_time, output_products_attributes: [:id, :product_id, :output_amount, :work_time, :_destroy])
end
end