Created
October 20, 2019 23:35
-
-
Save victorgiraldes/bdd5782922aa34b99e3f0ed26ac2b9a6 to your computer and use it in GitHub Desktop.
This file contains 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 | |
biding.pry | |
if @company.present? | |
@entry = @company.entries.build({ | |
provider: @provider, | |
date: options[:date], | |
invoce: options[:invoce], | |
value: options[:value], | |
discount: options[:discount], | |
entry_inputs_attributes.each do |index, value| | |
"#{index}:" "#{value}" | |
end | |
}) | |
@entry.user = @user | |
@file = options[:file] | |
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