Created
October 20, 2019 22:55
-
-
Save victorgiraldes/8d750064aa3b5bdf18dc87ff0eef1752 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module StockService | |
class CreateImportEntry | |
include ActiveModel::Model | |
attr_accessor :entry, :company, :user, :file | |
def initialize(options = {}, company, user, stock_group, file) | |
@options = options | |
@company = company | |
@user = user | |
@stock_group = stock_group | |
@provider = Provider.find_by(id: options[:provider_id]) | |
@file = file | |
end | |
validates :company, :entry, presence: { message: I18n.t('services.stock_service.create_import_entry.errors.blank') } | |
def build | |
entry_input = @options[:entry_inputs_attributes].map { |index, value| [value[:input_id], | |
value[:amount], value[:amount_entry], value[:kind], value[:unit], value[:lot], | |
value[:validity], value[:value]] } | |
binding.pry | |
input_id = entry_input[0][0].to_i | |
amount = entry_input[0][1].to_d | |
amount_entry = entry_input[0][2].to_d | |
kind = entry_input[0][3] | |
unit = entry_input[0][4] | |
lot = entry_input[0][5] | |
validity = entry_input[0][6] | |
value = entry_input[0][7].to_i | |
if @company.present? | |
@entry = Builder::EntryBuilder.build do |builder| | |
binding.pry | |
builder.set_entry( | |
@company.id, | |
@provider.id, | |
@options[:date], | |
@options[:invoce], | |
@options[:value], | |
@options[:discount] | |
) | |
builder.set_entry_inputs( | |
input_id, | |
amount, | |
amount_entry, | |
kind, | |
unit, | |
lot, | |
validity, | |
value | |
) | |
end | |
end | |
@file | |
@entry.user = @user | |
end | |
def call | |
build | |
@entry.save | |
ActiveRecord::Base.transaction do | |
up_score | |
update_stock_group | |
end | |
true | |
end | |
private | |
def up_score | |
return unless @user.Potencial? | |
@user.up_score!(1.8) | |
end | |
def update_stock_group | |
return unless @stock_group.blank? | |
@entry.stocks.each{|s| s.update_attribute(:stock_group_id, @stock_group.to_i)} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment