sacarmonar15
9/22/2017 - 5:05 PM

Clone Stands

#Esta tarea permite copiar stands de un evento a otro evento, INCLUYENDO LA RESERVA.
#Normalmente la piden para copiar los stands de un antiguo evento de camacol a otro mas reciente.
#Despues de correr la tarea si hubo algun fallo este se guardara en track_errors(Array)
#OJO: Esta tarea no es trasaccional. 
from_event = Event.find(2189)
to_event = Event.find(3660) #Target
track_errors = []
skipped = []
contact_params = EventParam.get_by_event(from_event.id).where(param_name: 5)
contact_params_match = {}
contact_params.each do |param|
    new_param = EventParam.get_by_event(to_event.id).where(param_name: 5, param_value: param.param_value).first
    contact_params_match[param.id] = new_param.id
end
from_event.zones.each do |from_zone|
    to_zone = Zone.where(event_id: to_event.id, archived: false, draft: false, name: from_zone.name).first
    unless to_zone.blank?
        puts "-----------------------------"
        puts "processing zone #{to_zone.name}"
        Stand.transaction do
            errors = []
            zone_stands = to_zone.stands.present? ? to_zone.stands.map{|s| s.current_reservation.company.name.strip.downcase if s.current_reservation.present? } : []
            zone_stands = zone_stands.map{|company| company.strip.downcase if company.present? } if zone_stands.present?
            from_zone.stands.each_with_index do |stand, index|
                puts "---------------------"
                puts "---------------------"
                puts zone_stands
                if stand.current_reservation.present? and zone_stands.include? stand.current_reservation.company.name.strip.downcase
                    skipped << stand.identifier
                    next
                end
                new_stand = Stand.new({event_id: to_event.id, zone_id: to_zone.id, status: 1, archived: false})
                new_stand.attributes = stand.attributes.select{|key, value| ["price", "identifier", "description", "width", "length"].include?(key.to_s)}
                new_stand.identifier = "clone #{new_stand.identifier}"
                creation_succed = new_stand.save
                errors << "Stand not saved #{stand.identifier} #{new_stand.errors.full_messages.join(',')}" if !creation_succed
                if creation_succed
                    reservation = stand.current_reservation
                    new_reservation = StandReservation.new
                    new_reservation.attributes = reservation.attributes.except("id")
                    new_reservation.stand_id = new_stand.id
                    new_reservation.draft = true
                    if !new_reservation.save(validate: false)
                        errors << "reservation not saved #{stand.identifier} #{new_reservation.errors.full_messages.join(',')}"
                    else
                        reservation.stand_reservation_contacts.each do |contact|
                            new_contact = StandReservationContact.new
                            new_contact.attributes = contact.attributes.except("id")
                            new_contact.param_contact_id = contact_params_match[contact.param_contact_id]
                            new_contact.stand_reservation_id = new_reservation.id
                            contacts_succed = new_contact.save
                            errors << "reservation not saved #{stand.identifier} #{new_contact.errors.full_messages.join(',')}" if !contacts_succed
                            errors << "reservation not saved #{stand.identifier} #{new_reservation.errors.full_messages.join(',')}" if contacts_succed and !new_reservation.valid?
                            if contacts_succed and new_reservation.valid?
                                new_reservation.draft = false
                                new_reservation.save
                                # new_reservation.update_column(:draft, false)
                                # new_stand.update_column(:status, 2)
                            end
                        end
                    end
                end
            end
            puts "Errors found: #{errors.count}"
            if errors.count > 0
                track_errors << "#{errors.join('\n')}"
                puts "#{errors.join('\n')}"
                raise ActiveRecord::Rollback
            end
        end
        puts "*******************************"
    end
end