Last active
October 18, 2019 22:03
-
-
Save victorgiraldes/0ef1ed67c90a4070e95f226eb1080099 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, :file, :company, :user, :file, :provider | |
def initialize(options = {}) | |
@company = Company.find_by(options[:token]) | |
@user = User.find_by(options[:token]) | |
@stock_group = options[:stock_group] | |
@provider = Provider.find_by(options[:token]) | |
@year = YearCompany.find_by(options[:token]) | |
@input = Input.find_by(options[:token]) | |
end | |
validates :company, :entry, presence: { message: I18n.t('services.stock_service.create_import_entry.errors.blank') } | |
def call(options) | |
if @company.present? | |
@entry = @company.entries.build({ | |
provider: @provider, | |
date: options[:date], | |
invoce: options[:invoce], | |
value: options[:value], | |
discount: options[:discount], | |
entry_inputs_attributes: [{ | |
input: @input, | |
amount: options[:amount], | |
amount_entry: options[:amount_entry], | |
kind: options[:kind], | |
unit: options[:unit], | |
lot: options[:lot], | |
validity: options[:validity], | |
value: options[:value], | |
id: options[:id] | |
}] | |
}) | |
@entry.user = @user | |
@file = options[:file] | |
end | |
@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