Guest User

Untitled

a guest
Mar 3rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. class ImportJob
  2. def self.run
  3. commesse = Commessa.find :all
  4. if commesse.empty?
  5. puts "Non ci sono commesse da importare."
  6. else
  7. puts "Sto per importare #{commesse.size} commessa/e."
  8. commesse.each {|c| commessa_to_job(c)}
  9. end
  10. end
  11.  
  12. # Importare una commessa prevede la creazione
  13. # nel caso non esistano, di un account sul sito,
  14. # un veicolo, una ownership.
  15. def self.commessa_to_job(commessa)
  16. puts "elaboro commessa n. #{commessa.COMMESSA}"
  17.  
  18. job = (Job.find_by_identification commessa.COMMESSA) || Job.new
  19. puts "\tgia' censita... #{!job.new_record?}"
  20.  
  21. if job.new_record?
  22. # TODO: job.partner_id
  23. job.identification = commessa.COMMESSA
  24. job.job_date = commessa.GMA_ENTR
  25. job.mileage = commessa.KM
  26. job.invoice_number = commessa.NUM_IVA
  27. job.invoice_date = commessa.GMA_IVA
  28. # TODO: job.invoice_id
  29.  
  30. puts "\tcreo nuovo Job... done"
  31. end
  32.  
  33. # FIXME: Probabile incasinamento se i clienti cominciano a cambiare targa sulle proprie ownership.
  34. # Suggerisco di non permettere il cambiamento della targa
  35. ownership = (Ownership.find_by_license_plate commessa.TARGA) || Ownership.new
  36. puts "\townership esistente... #{!ownership.new_record?}"
  37.  
  38. if ownership.new_record?
  39. ownership.license_plate = commessa.TARGA
  40. ownership.description = commessa.TIPO_VEICOLO
  41.  
  42. vehicle = (Vehicle.find_by_frame_number commessa.TELAIO) || Vehicle.new
  43. puts "\tveicolo gia' esistente... #{!vehicle.new_record?}"
  44.  
  45. if vehicle.new_record?
  46. vehicle.frame_number = commessa.TELAIO
  47. vehicle.brand = commessa.MARCA
  48. end
  49.  
  50. ownership.vehicle = vehicle
  51.  
  52. sede_legale = (SedeLegale.find_by_KS_ID_Azienda commessa.CLIENTE) || SedeLegale.new
  53. puts "\tutente gia' censito... #{!sede_legale.new_record?}"
  54.  
  55. if sede_legale.new_record?
  56. sede_legale.KS_ID_Sede_Legale = 1 #TODO: FIXME: Inserire l'algoritmo che determina questo campo
  57. sede_legale.XS_Ragione_Sociale = "Sede Legale Generata #{sede_legale.id}"
  58. sede_legale.XS_Partita_IVA = commessa.PAR_IVA
  59. sede_legale.KS_ID_Azienda = commessa.CLIENTE
  60. end
  61.  
  62. user = (Utente.find_by_ID_Azienda sede_legale.KS_ID_Azienda) || Utente.new
  63. puts "\tutente presente in tabella utenti... #{!user.new_record?}"
  64.  
  65. if user.new_record?
  66. user.UserID = sede_legale.XS_Partita_IVA
  67. user.Password = "pippo" # In che modo è hashata la password???
  68. user.Data_Ultimo_Rinnovo_Psw = Time.now
  69. user.ID_Azienda = sede_legale.KS_ID_Azienda
  70. end
  71.  
  72. customer = (Customer.find_by_utente_id user.id) || Customer.new
  73. puts "\tutente presente nel sistema gestione mezzi... #{!customer.new_record?}"
  74.  
  75. if customer.new_record?
  76. customer.name = sede_legale.XS_Ragione_Sociale
  77. customer.vat_number = sede_legale.XS_Partita_IVA
  78. customer.utente = user
  79. end
  80.  
  81. ownership.customer = customer
  82. end
  83.  
  84. job.ownership = ownership
  85.  
  86. customer.save
  87. user.save
  88. sede_legale.save
  89. ownership.save
  90. vehicle.save
  91. job.save
  92.  
  93. end
  94. end
Add Comment
Please, Sign In to add comment