PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/main_controller.rb

https://github.com/kaazoo/Perequation
Ruby | 492 lines | 319 code | 125 blank | 48 comment | 27 complexity | adba7bce0d7cca24a46af02adf314764 MD5 | raw file
Possible License(s): GPL-3.0
  1. class MainController < ApplicationController
  2. # user login:
  3. include AuthenticatedSystem
  4. #before_filter :login_required
  5. # pdf-generierung
  6. #require "htmldoc"
  7. require 'fpdf'
  8. # Klasse für Geld und Kommadarstellung:
  9. require 'more_money'
  10. MoreMoney::Money.add_currency({:code => 'EUR', :description => 'euro'})
  11. def menue
  12. # ist nutzer eingeloggt?
  13. if logged_in?
  14. @nutzer = current_user
  15. @logout_link = "<a href=\"/main/logout\">ausloggen</a>"
  16. end
  17. end
  18. def abrechnungen
  19. erste_einnahme = Gain.find(:first, :order => "datum ASC", :conditions => ['geloescht = ?', false]).datum
  20. erste_ausgabe = Expense.find(:first, :order => "datum ASC", :conditions => ['geloescht = ?', false]).datum
  21. if erste_einnahme < erste_ausgabe
  22. min = erste_einnahme
  23. else
  24. min = erste_ausgabe
  25. end
  26. @erstes_jahr = min.year
  27. @erster_monat = min.month
  28. end
  29. def abrechnen
  30. # paramater extrahieren
  31. dats_input = CGI::unescape(params[:id])
  32. dats = dats_input.split(/,\s*/)
  33. dat = Date.strptime(dats[0])
  34. dat_end = Date.strptime(dats[1])
  35. @month_name = Date.strptime(dat.to_s).strftime("%B")
  36. @year = Date.strptime(dat.to_s).strftime("%Y")
  37. # user-ids aus db holen
  38. first_user_id = User.find_by_login(APP_CONFIG['pq_first_user']).id
  39. second_user_id = User.find_by_login(APP_CONFIG['pq_second_user']).id
  40. @summe_einnahmen_first_user = 0
  41. @summe_einnahmen_second_user = 0
  42. # alle einnahmen im zeitraum abrechnen und summen bilden
  43. abr_einnahmen = Gain.find(:all, :conditions => ['geloescht=? and bezahlt=?', false, true])
  44. abr_einnahmen.each do |x|
  45. if (x.datum >= dat) && (x.datum < dat_end)
  46. x.abgerechnet = true
  47. x.save
  48. if x.user_id == first_user_id
  49. @summe_einnahmen_first_user += x.netto
  50. end
  51. if x.user_id == second_user_id
  52. @summe_einnahmen_second_user += x.netto
  53. end
  54. end
  55. end
  56. @summe_ausgaben_first_user = 0
  57. @summe_ausgaben_second_user = 0
  58. # alle ausgaben im zeitraum abrechnen und summen bilden
  59. abr_ausgaben = Expense.find(:all, :conditions => ['geloescht=? and bezahlt=?', false, true])
  60. abr_ausgaben.each do |x|
  61. if (x.datum >= dat) && (x.datum < dat_end)
  62. x.abgerechnet = true
  63. x.save
  64. if x.user_id == first_user_id
  65. @summe_ausgaben_first_user += x.netto
  66. end
  67. if x.user_id == second_user_id
  68. @summe_ausgaben_second_user += x.netto
  69. end
  70. end
  71. end
  72. # gewinne
  73. @summe_gewinn_first_user = @summe_einnahmen_first_user - @summe_ausgaben_first_user
  74. @summe_gewinn_second_user = @summe_einnahmen_second_user - @summe_ausgaben_second_user
  75. @expenses_first_user = Expense.find(:all, :order => "datum DESC", :conditions => ["geloescht = ? AND bezahlt = ? AND datum >= '"+dat.to_s+"' AND datum < '"+dat_end.to_s+"' AND user_id = "+first_user_id.to_s, false, true])
  76. @expenses_second_user = Expense.find(:all, :order => "datum DESC", :conditions => ["geloescht = ? AND bezahlt = ? AND datum >= '"+dat.to_s+"' AND datum < '"+dat_end.to_s+"' AND user_id = "+second_user_id.to_s, false, true])
  77. @gains_first_user = Gain.find(:all, :order => "datum DESC", :conditions => ["geloescht = ? AND bezahlt = ? AND datum >= '"+dat.to_s+"' AND datum < '"+dat_end.to_s+"' AND user_id = "+first_user_id.to_s, false, true])
  78. @gains_second_user = Gain.find(:all, :order => "datum DESC", :conditions => ["geloescht = ? AND bezahlt = ? AND datum >= '"+dat.to_s+"' AND datum < '"+dat_end.to_s+"' AND user_id = "+second_user_id.to_s, false, true])
  79. # neue abrechnung erstellen
  80. # ERSTMA NICH
  81. st = Statement.new
  82. st.erstellungsdatum = Date.today.to_s
  83. st.name = dats_input
  84. st.save
  85. # pdf generierung
  86. pdf=FPDF.new
  87. pdf.AddPage
  88. pdf.SetFont('Arial','B',16)
  89. pdf.Cell(40,10,'Abrechnung '+@month_name+' '+@year)
  90. pdf.Ln(20)
  91. pdf.SetFont('Arial','B',11)
  92. pdf.Cell(40,10,'Alle Werte sind in Netto.')
  93. pdf.Ln(20)
  94. # uebersicht
  95. pdf.SetFont('Arial','B',14)
  96. pdf.Cell(40,10,APP_CONFIG['pq_first_user'])
  97. pdf.Ln(5)
  98. pdf.SetFont('Arial','B',13)
  99. pdf.Cell(40,10,'Einnahmen: '+MoreMoney::Money.new(@summe_einnahmen_first_user *100, 'EUR').format(:with_thousands)+' EUR')
  100. pdf.Ln(5)
  101. pdf.Cell(40,10,'Ausgaben: '+MoreMoney::Money.new(@summe_ausgaben_first_user *100, 'EUR').format(:with_thousands)+' EUR')
  102. pdf.Ln(5)
  103. pdf.Cell(40,10,'Gewinn: '+MoreMoney::Money.new(@summe_gewinn_first_user *100, 'EUR').format(:with_thousands)+' EUR')
  104. pdf.Ln(20)
  105. pdf.SetFont('Arial','B',14)
  106. pdf.Cell(40,10,APP_CONFIG['pq_second_user'])
  107. pdf.Ln(5)
  108. pdf.SetFont('Arial','B',13)
  109. pdf.Cell(40,10,'Einnahmen: '+MoreMoney::Money.new(@summe_einnahmen_second_user *100, 'EUR').format(:with_thousands)+' EUR')
  110. pdf.Ln(5)
  111. pdf.Cell(40,10,'Ausgaben: '+MoreMoney::Money.new(@summe_ausgaben_second_user *100, 'EUR').format(:with_thousands)+' EUR')
  112. pdf.Ln(5)
  113. pdf.Cell(40,10,'Gewinn: '+MoreMoney::Money.new(@summe_gewinn_second_user *100, 'EUR').format(:with_thousands)+' EUR')
  114. pdf.Ln(20)
  115. pdf.Cell(40,10,'Differenz: '+MoreMoney::Money.new( ((@summe_gewinn_first_user - @summe_gewinn_second_user).abs / 2) *100, 'EUR').format(:with_thousands)+' EUR')
  116. pdf.Ln(20)
  117. # einnahmen first_user
  118. pdf.SetFont('Arial','B',14)
  119. pdf.Cell(40,10,'alle Einnahmen '+APP_CONFIG['pq_first_user'])
  120. pdf.Ln(10)
  121. pdf.SetFont('Arial','B',11)
  122. pdf.Cell(45, 7, 'Name', 1)
  123. pdf.Cell(22, 7, 'Datum', 1)
  124. pdf.Cell(35, 7, 'Netto', 1)
  125. #pdf.Cell(30, 7, 'Brutto', 1)
  126. pdf.Cell(15, 7, 'MwSt', 1)
  127. pdf.Cell(30, 7, 'Eintragsdatum', 1)
  128. pdf.Cell(20, 7, 'Person', 1)
  129. pdf.Ln()
  130. @gains_first_user.each do |gain|
  131. pdf.Cell(45, 6, gain.name, 1)
  132. old_date = gain.datum.to_s
  133. pdf.Cell(22, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  134. pdf.Cell(35, 6, ((gain.netto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  135. #pdf.Cell(30, 6, ((gain.brutto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  136. pdf.Cell(15, 6, (gain.mwst * 100).to_i.to_s + " %", 1)
  137. old_date = gain.eintragsdatum.to_s
  138. pdf.Cell(30, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  139. pdf.Cell(20, 6, User.find(gain.user_id).name, 1)
  140. pdf.Ln()
  141. end
  142. pdf.Ln(20)
  143. # einnahmen second_user
  144. pdf.SetFont('Arial','B',14)
  145. pdf.Cell(40,10,'alle Einnahmen '+APP_CONFIG['pq_second_user'])
  146. pdf.Ln(10)
  147. pdf.SetFont('Arial','B',11)
  148. pdf.Cell(45, 7, 'Name', 1)
  149. pdf.Cell(22, 7, 'Datum', 1)
  150. pdf.Cell(35, 7, 'Netto', 1)
  151. #pdf.Cell(30, 7, 'Brutto', 1)
  152. pdf.Cell(15, 7, 'MwSt', 1)
  153. pdf.Cell(30, 7, 'Eintragsdatum', 1)
  154. pdf.Cell(20, 7, 'Person', 1)
  155. pdf.Ln()
  156. @gains_second_user.each do |gain|
  157. pdf.Cell(45, 6, gain.name, 1)
  158. old_date = gain.datum.to_s
  159. pdf.Cell(22, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  160. pdf.Cell(35, 6, ((gain.netto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  161. #pdf.Cell(30, 6, ((gain.brutto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  162. pdf.Cell(15, 6, (gain.mwst * 100).to_i.to_s + " %", 1)
  163. old_date = gain.eintragsdatum.to_s
  164. pdf.Cell(30, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  165. pdf.Cell(20, 6, User.find(gain.user_id).name, 1)
  166. pdf.Ln()
  167. end
  168. pdf.Ln(20)
  169. # ausgaben first_user
  170. pdf.SetFont('Arial','B',14)
  171. pdf.Cell(40,10,'alle Ausgaben '+APP_CONFIG['pq_first_user'])
  172. pdf.Ln(10)
  173. pdf.SetFont('Arial','B',11)
  174. pdf.Cell(45, 7, 'Name', 1)
  175. pdf.Cell(22, 7, 'Datum', 1)
  176. pdf.Cell(35, 7, 'Netto', 1)
  177. #pdf.Cell(30, 7, 'Brutto', 1)
  178. pdf.Cell(15, 7, 'MwSt', 1)
  179. pdf.Cell(10, 7, 'Art', 1)
  180. pdf.Cell(30, 7, 'Eintragsdatum', 1)
  181. pdf.Cell(20, 7, 'Person', 1)
  182. pdf.Ln()
  183. @expenses_first_user.each do |expense|
  184. pdf.Cell(45, 6, expense.name, 1)
  185. old_date = expense.datum.to_s
  186. pdf.Cell(22, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  187. pdf.Cell(35, 6, ((expense.netto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  188. #pdf.Cell(30, 6, ((expense.brutto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  189. pdf.Cell(15, 6, (expense.mwst * 100).to_i.to_s + " %", 1)
  190. pdf.Cell(10, 6, expense.art[0,1], 1)
  191. old_date = expense.eintragsdatum.to_s
  192. pdf.Cell(30, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  193. pdf.Cell(20, 6, User.find(expense.user_id).name, 1)
  194. pdf.Ln()
  195. end
  196. pdf.Ln(20)
  197. # ausgaben second_user
  198. pdf.SetFont('Arial','B',14)
  199. pdf.Cell(40,10,'alle Ausgaben '+APP_CONFIG['pq_second_user'])
  200. pdf.Ln(10)
  201. pdf.SetFont('Arial','B',11)
  202. pdf.Cell(45, 7, 'Name', 1)
  203. pdf.Cell(22, 7, 'Datum', 1)
  204. pdf.Cell(35, 7, 'Netto', 1)
  205. #pdf.Cell(30, 7, 'Brutto', 1)
  206. pdf.Cell(15, 7, 'MwSt', 1)
  207. pdf.Cell(10, 7, 'Art', 1)
  208. pdf.Cell(30, 7, 'Eintragsdatum', 1)
  209. pdf.Cell(20, 7, 'Person', 1)
  210. pdf.Ln()
  211. @expenses_second_user.each do |expense|
  212. pdf.Cell(45, 6, expense.name, 1)
  213. old_date = expense.datum.to_s
  214. pdf.Cell(22, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  215. pdf.Cell(35, 6, ((expense.netto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  216. #pdf.Cell(30, 6, ((expense.brutto * 100).round/100.0).to_s.gsub(/\./,',')+' EUR', 1)
  217. pdf.Cell(15, 6, (expense.mwst * 100).to_i.to_s + " %", 1)
  218. pdf.Cell(10, 6, expense.art[0,1], 1)
  219. old_date = expense.eintragsdatum.to_s
  220. pdf.Cell(30, 6, "#{old_date[8,2]}.#{old_date[5,2]}.#{old_date[0,4]}", 1)
  221. pdf.Cell(20, 6, User.find(expense.user_id).name, 1)
  222. pdf.Ln()
  223. end
  224. # pdf abspeichern
  225. pdf.Output("public/"+APP_CONFIG['pq_pdf_prefix']+"_"+dats_input.gsub(',', '_')+".pdf")
  226. pdf.Ln(10)
  227. render '/main/abrechnung'
  228. end
  229. def abrechnung_pdf
  230. dats_input = CGI::unescape(params[:id])
  231. # anpassung an browser
  232. if request.env['HTTP_USER_AGENT'] =~ /msie/i
  233. headers['Pragma'] = ''
  234. headers['Cache-Control'] = ''
  235. else
  236. headers['Pragma'] = 'no-cache'
  237. headers['Cache-Control'] = 'no-cache, must-revalidate'
  238. end
  239. # datei an nutzer schicken
  240. send_file "public/"+APP_CONFIG['pq_pdf_prefix']+"_"+dats_input.gsub(',', '_')+".pdf", :filename => APP_CONFIG['pq_pdf_prefix']+"_"+dats_input.gsub(',', '_')+".pdf", :type => "application/pdf"
  241. end
  242. def login
  243. return unless request.post?
  244. self.current_user = User.authenticate(params[:login], params[:password])
  245. if logged_in?
  246. if params[:remember_me] == "1"
  247. self.current_user.remember_me
  248. cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
  249. end
  250. redirect_back_or_default(:controller => '/main', :action => 'menue')
  251. flash[:notice] = "Logged in successfully"
  252. end
  253. end
  254. def signup
  255. @user = User.new(params[:user])
  256. return unless request.post?
  257. @user.save!
  258. self.current_user = @user
  259. redirect_back_or_default(:controller => '/main', :action => 'menue')
  260. flash[:notice] = "Thanks for signing up!"
  261. rescue ActiveRecord::RecordInvalid
  262. render :action => 'signup'
  263. end
  264. def logout
  265. self.current_user.forget_me if logged_in?
  266. cookies.delete :auth_token
  267. reset_session
  268. flash[:notice] = "You have been logged out."
  269. redirect_back_or_default(:controller => '/main', :action => 'menue')
  270. #redirect_to '/'
  271. end
  272. def statistic_month
  273. if params[:date] == nil
  274. # start with last month
  275. mydate = Date.today.last_month
  276. @startmonth = mydate.month
  277. @startyear = mydate.year
  278. else
  279. mydate = Date.parse(params[:date])
  280. @startmonth = mydate.month
  281. @startyear = mydate.year
  282. end
  283. # one month earlier
  284. @month_before = mydate.last_month.month
  285. @year_before = mydate.last_month.year
  286. # one month after
  287. @month_next = mydate.next_month.month
  288. @year_next = mydate.next_month.year
  289. end
  290. def statistic_year
  291. if params[:year] == nil
  292. mydate = Date.today
  293. @startyear = mydate.year
  294. else
  295. @startyear = params[:year].to_i
  296. end
  297. # one year earlier
  298. @year_before = @startyear - 1
  299. # one year after
  300. @year_next = @startyear + 1
  301. end
  302. def render_graph
  303. month = params[:m].to_i
  304. year = params[:y].to_i
  305. if (month > 0) && (year > 0)
  306. # get number of days in month
  307. numdays = Time::days_in_month(month, year)
  308. # generate arrays for month
  309. gains_arr = Array.new(numdays)
  310. gains_arr.fill(0)
  311. expenses_arr = Array.new(numdays)
  312. expenses_arr.fill(0)
  313. # first and last day of month
  314. first_day = year.to_s+"-"+month.to_s+"-01"
  315. last_day = year.to_s+"-"+month.to_s+"-"+numdays.to_s
  316. # add gains to array
  317. gains = Gain.find(:all, :conditions => { :datum => first_day..last_day })
  318. gains.each do |gain|
  319. datum = Date.parse(gain[:datum].to_s)
  320. gains_arr[datum.day] += gain[:netto]
  321. end
  322. # add expenses to array
  323. expenses = Expense.find(:all, :conditions => { :datum => first_day..last_day })
  324. expenses.each do |expense|
  325. datum = Date.parse(expense[:datum].to_s)
  326. expenses_arr[datum.day] += expense[:netto]
  327. end
  328. # render graph
  329. graph = Scruffy::Graph.new
  330. graph.title = Date::MONTHNAMES[month].to_s+" "+year.to_s
  331. graph.add(:line, t(:gains), gains_arr)
  332. graph.add(:line, t(:expenses), expenses_arr)
  333. elsif (month = 0) && (year > 0)
  334. # number of weeks in year
  335. numdays = 53
  336. # generate arrays for year
  337. gains_arr = Array.new(numdays)
  338. gains_arr.fill(0)
  339. expenses_arr = Array.new(numdays)
  340. expenses_arr.fill(0)
  341. # first and last day of year
  342. first_day = year.to_s+"-01-01"
  343. last_day = year.to_s+"-12-31"
  344. # add gains to array
  345. gains = Gain.find(:all, :conditions => { :datum => first_day..last_day })
  346. gains.each do |gain|
  347. datum = Date.parse(gain[:datum].to_s)
  348. week_of_year = datum.strftime("%W").to_i
  349. gains_arr[week_of_year] += gain[:netto]
  350. end
  351. # add expenses to array
  352. expenses = Expense.find(:all, :conditions => { :datum => first_day..last_day })
  353. expenses.each do |expense|
  354. datum = Date.parse(expense[:datum].to_s)
  355. week_of_year = datum.strftime("%W").to_i
  356. expenses_arr[week_of_year] += expense[:netto]
  357. end
  358. # render graph
  359. graph = Scruffy::Graph.new
  360. graph.title = year.to_s
  361. graph.add(:line, t(:gains), gains_arr)
  362. graph.add(:line, t(:expenses), expenses_arr)
  363. end
  364. graph_blob = graph.render :size => [900,500], :as => 'PNG'
  365. send_data graph_blob, :filename => 'graph.png', :type => 'image/png', :disposition => 'inline'
  366. end
  367. end