Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ImportJob
- def self.run
- commesse = Commessa.find :all
- if commesse.empty?
- puts "Non ci sono commesse da importare."
- else
- puts "Sto per importare #{commesse.size} commessa/e."
- commesse.each {|c| commessa_to_job(c)}
- end
- end
- # Importare una commessa prevede la creazione
- # nel caso non esistano, di un account sul sito,
- # un veicolo, una ownership.
- def self.commessa_to_job(commessa)
- puts "elaboro commessa n. #{commessa.COMMESSA}"
- job = (Job.find_by_identification commessa.COMMESSA) || Job.new
- puts "\tgia' censita... #{!job.new_record?}"
- if job.new_record?
- # TODO: job.partner_id
- job.identification = commessa.COMMESSA
- job.job_date = commessa.GMA_ENTR
- job.mileage = commessa.KM
- job.invoice_number = commessa.NUM_IVA
- job.invoice_date = commessa.GMA_IVA
- # TODO: job.invoice_id
- puts "\tcreo nuovo Job... done"
- end
- # FIXME: Probabile incasinamento se i clienti cominciano a cambiare targa sulle proprie ownership.
- # Suggerisco di non permettere il cambiamento della targa
- ownership = (Ownership.find_by_license_plate commessa.TARGA) || Ownership.new
- puts "\townership esistente... #{!ownership.new_record?}"
- if ownership.new_record?
- ownership.license_plate = commessa.TARGA
- ownership.description = commessa.TIPO_VEICOLO
- vehicle = (Vehicle.find_by_frame_number commessa.TELAIO) || Vehicle.new
- puts "\tveicolo gia' esistente... #{!vehicle.new_record?}"
- if vehicle.new_record?
- vehicle.frame_number = commessa.TELAIO
- vehicle.brand = commessa.MARCA
- end
- ownership.vehicle = vehicle
- sede_legale = (SedeLegale.find_by_KS_ID_Azienda commessa.CLIENTE) || SedeLegale.new
- puts "\tutente gia' censito... #{!sede_legale.new_record?}"
- if sede_legale.new_record?
- sede_legale.KS_ID_Sede_Legale = 1 #TODO: FIXME: Inserire l'algoritmo che determina questo campo
- sede_legale.XS_Ragione_Sociale = "Sede Legale Generata #{sede_legale.id}"
- sede_legale.XS_Partita_IVA = commessa.PAR_IVA
- sede_legale.KS_ID_Azienda = commessa.CLIENTE
- end
- user = (Utente.find_by_ID_Azienda sede_legale.KS_ID_Azienda) || Utente.new
- puts "\tutente presente in tabella utenti... #{!user.new_record?}"
- if user.new_record?
- user.UserID = sede_legale.XS_Partita_IVA
- user.Password = "pippo" # In che modo è hashata la password???
- user.Data_Ultimo_Rinnovo_Psw = Time.now
- user.ID_Azienda = sede_legale.KS_ID_Azienda
- end
- customer = (Customer.find_by_utente_id user.id) || Customer.new
- puts "\tutente presente nel sistema gestione mezzi... #{!customer.new_record?}"
- if customer.new_record?
- customer.name = sede_legale.XS_Ragione_Sociale
- customer.vat_number = sede_legale.XS_Partita_IVA
- customer.utente = user
- end
- ownership.customer = customer
- end
- job.ownership = ownership
- customer.save
- user.save
- sede_legale.save
- ownership.save
- vehicle.save
- job.save
- end
- end
Add Comment
Please, Sign In to add comment