An open source, stand-alone, customisable public spending data web app.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require 'lib/models'
  2. require 'fastercsv'
  3. # Before running this script with a CSV file, prepare it so:
  4. # - There is only a single line of column headings on the first line of the file
  5. # - There are no spaces before or after the column headings
  6. # - The column headings correspond with the key names in the columns{} hash below
  7. # - The data starts on line 2
  8. columns =
  9. {
  10. 'Directorate' => nil,
  11. 'Updated' => nil,
  12. 'Service' => nil,
  13. 'Supplier' => nil,
  14. 'Amount' => nil
  15. }
  16. directorate_replacements =
  17. [
  18. [ "Childrens Services", "Children's Services" ],
  19. [ "Policy,Performance and Planning", "Policy, Performance and Planning" ]
  20. ]
  21. service_replacements =
  22. [
  23. [ "Corporate Performance and Developmt", "Corporate Performance and Development" ],
  24. [ "ISB", "ISB - Individual Schools Budget" ],
  25. [ "Library and Information Services", "Libraries and Information Services" ],
  26. [ "On Street Parking", "On-Street Parking" ]
  27. ]
  28. count = 0
  29. if ARGV[0].nil?
  30. puts "Specify the filename of the CSV file to import on the command line"
  31. exit
  32. end
  33. date_format = ARGV[1].upcase
  34. if date_format != 'DMY' && date_format != 'MDY'
  35. puts "Specify the date format as DMY or MDY as the second argument on the command line"
  36. exit
  37. end
  38. FasterCSV.foreach(ARGV[0]) do |row|
  39. count += 1
  40. if (count > 1) # skip first line that doesn't contain data
  41. p row
  42. directorate_name = row[columns['Directorate']].strip.gsub(/&/, "and")
  43. service_name = row[columns['Service']].strip.gsub(/&/, "and")
  44. supplier_name = row[columns['Supplier']].strip.gsub(/&/, "and")
  45. for replacement in directorate_replacements
  46. if directorate_name == replacement[0]
  47. directorate_name = replacement[1]
  48. end
  49. end
  50. for replacement in service_replacements
  51. if service_name == replacement[0]
  52. service_name = replacement[1]
  53. end
  54. end
  55. directorate = Directorate.first_or_create(:name => directorate_name)
  56. directorate.save
  57. service = Service.first_or_create(:name => service_name, :directorate => directorate)
  58. service.save
  59. supplier = Supplier.first_or_create(:name => supplier_name)
  60. supplier.save
  61. dt = row[columns['Updated']].strip.split('/')
  62. # Date.new takes YMD params
  63. if date_format == 'DMY'
  64. d = Date.new(dt[2].to_i, dt[1].to_i, dt[0].to_i)
  65. elsif date_format == 'MDY'
  66. d = Date.new(dt[2].to_i, dt[0].to_i, dt[1].to_i)
  67. elsif date_format == 'YMD'
  68. d = Date.new(dt[0].to_i, dt[1].to_i, dt[2].to_i)
  69. end
  70. payment = Payment.first_or_new(
  71. 'service' => service,
  72. 'supplier' => supplier,
  73. 'amount' => row[columns['Amount']].strip.gsub(/,/, ''),
  74. 'd' => d
  75. )
  76. unless payment.save
  77. puts "ERROR: Failed to save payment"
  78. payment.errors.each do |e|
  79. puts e
  80. end
  81. end
  82. else
  83. # Get the column headings
  84. position = 0
  85. for column in row
  86. columns[column] = position
  87. position += 1
  88. end
  89. puts columns.inspect
  90. end
  91. end