Created
January 19, 2012 16:26
-
-
Save derencius/1640992 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
def create | |
erros = [] | |
variants = [] | |
ProductionLabel.transaction do | |
begin | |
label = ProductionLabel.create!(params[:production_labels]) | |
unless (label.valid?) | |
raise label.errors.full_messages | |
end | |
listOrders = ProductionOrderImporter.import_orders(label.production_order_from, label.production_order_to) | |
## valida se retornou alguma ordem de produção do logix | |
if (listOrders.empty?) | |
raise "Nenhum ordem de produção encontrada para o range [#{label.production_order_from}, #{label.production_order_to}] foi encontrado." | |
end | |
## itera as ordens retornadas para verificar se o produto esta cadastrado no CRM | |
listOrders.each do |order| | |
item = Variant.find_all_by_code(order[:code]).first | |
!item.nil? ? variants << item : erros << order[:code] | |
end | |
if (erros.empty?) | |
variants.each { | variant| label.production_orders.create(:variant => variant, :product => variant.product} | |
flash[:success] = "Lote de etiquetas gerado com sucesso." | |
redirect_to :action => "show", :id => label | |
else | |
raise "A(s) etiqueta(s) não foram geradas pois os produtos a seguir, não estão cadastrados no CRM: #{erros.join(', ')}" | |
end | |
rescue => exc | |
flash.now[:error] = exc.to_s | |
render :action => "new" | |
raise ActiveRecord::Rollback | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment