Created
October 21, 2019 00:25
-
-
Save victorgiraldes/b57df6f1e22fda1017798403d9a78b30 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 Base | |
include ActiveModel::Model | |
attr_accessor :permitted_params | |
def initialize(permitted_params) | |
@permitted_params = permitted_params | |
end | |
end | |
end | |
module StockService | |
class CreateImportEntry < Base | |
include ActiveModel::Model | |
attr_accessor :entry, :company, :user, :file | |
def initialize(permitted_params, company, user, stock_group, file) | |
super(permitted_params) | |
@options = @permitted_params | |
@entry_inputs = @options[:entry_inputs_attributes] | |
@company = company | |
@user = user | |
@stock_group = stock_group | |
@provider = @company.group.providers.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 | |
if @company.present? | |
@entry = @company.entries.build({ | |
provider: @provider, | |
date: @options[:date], | |
invoce: @options[:invoce], | |
value: @options[:value], | |
discount: @options[:discount], | |
@entry_inputs.each do |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