class OutputsController ApplicationControlle authorize_resource before

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
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