]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons.rb
Release the code under AGPLv3 and add missing copyright information
[gestion-adh.git] / lib / nos_oignons.rb
1 #-*- coding: utf-8 -*-
2 #
3 # Système de gestion des adhésions de Nos oignons
4 # Copyright © 2013-2014 Nos oignons <contact@nos-oignons.net>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 require 'nos_oignons/git'
20 require 'nos_oignons/mailman'
21 require 'nos_oignons/member'
22 require 'nos_oignons/reminder'
23 require 'nos_oignons/reminder_db'
24
25 module NosOignons
26   BOARD_EMAIL = 'ca@nos-oignons.net'
27   ADVISORS_EMAIL = 'deontologie@nos-oignons.net'
28   MEMBER_MAILING_LIST = 'ag'
29   CONTACT_INFO = <<-EOT.gsub(/^    /, '')
30     https://nos-oignons.net/
31     contact@nos-oignons.net
32     Téléphone : +33 9 72 42 96 04
33     Fax : +33 9 72 42 96 06
34   EOT
35   POSTAL_ADDRESS = <<-EOT.gsub(/^    /, '')
36     Nos oignons
37     Centre UBIDOCA, 7585
38     105 route des Pommiers
39     74370 Saint Martin Bellevue
40     France
41   EOT
42
43   # The following class methods are all meant to be called as command-line scripts
44   class << self
45     def create_membership_fee_receipt!(member_id, amount)
46       member = NosOignons::Member.new(member_id)
47       member.create_receipt!(amount)
48     end
49
50     def list_emails!
51       NosOignons::Member.all.each do |member|
52         if member.up_to_date?
53           puts member.email
54         end
55       end
56     end
57
58     def update_ag_subscribers!
59       list = NosOignons::MEMBER_MAILING_LIST
60
61       current_emails = NosOignons::Mailman.list_members(list)
62       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
63
64       emails_to_add = uptodate_emails - current_emails
65       NosOignons::Mailman.add_members(list, emails_to_add) unless emails_to_add.empty?
66       emails_to_remove = current_emails - uptodate_emails
67       NosOignons::Mailman.remove_members(list, emails_to_remove) unless emails_to_remove.empty?
68     end
69
70     def send_membership_reminders!
71       today = Time.now.to_date
72       NosOignons::Member.all.select(&:up_to_date?).each do |member|
73         reminders = NosOignons::Reminder.all.sort_by(&:days)
74         anniversary = Time.new(today.year, member.joined_on.month,
75                                member.joined_on.day).to_date
76         next if member.membership_fee_paid_on >= anniversary - reminders.last.days
77         reminders.each do |reminder|
78           next if (anniversary - today).to_i > reminder.days
79           next if member.reminded_on && (anniversary - member.reminded_on).to_i <= reminder.days
80
81           member.remind(reminder)
82           break
83         end
84       end
85     end
86
87     def send_member_emails_to_advisors!
88       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
89       subject = 'Adresses email des membres à jour de cotisation'
90       body = <<-END_OF_BODY.gsub(/^        /, '')
91         Cher comité de déontologie de Nos oignons,
92
93         Comme le prévoient les statuts l'article 12 des statuts de
94         l'association, vous devez être en mesure de pouvoir convoquer une
95         assemblée générale extraordinaire. Pour ce faire, voici donc la liste
96         des emails à jour de cotisation à la date d'aujourd'hui :
97
98         #{uptodate_emails.join("\n")}
99
100         Et merci encore de votre engagement auprès de Nos oignons !
101
102         -- 
103         Le robot du conseil d'administration
104
105                               GNU AGPLv3 © Nos oignons <contact@nos-oignons.net>
106                                git clone https://nos-oignons.net/gestion-adh.git
107       END_OF_BODY
108       mail = Mail.new :charset => 'utf-8',
109                       :from => NosOignons::BOARD_EMAIL,
110                       :to => NosOignons::ADVISORS_EMAIL,
111                       :subject => subject,
112                       :body => body
113       mail.deliver
114     end
115
116     def pre_commit_hook!
117       if system('git rev-parse --quiet --verify HEAD >/dev/null')
118         against = 'HEAD'
119       else
120         # Initial commit: diff against an empty tree object
121         against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
122       end
123
124       IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
125         NosOignons::Git.handle_modified_files(io) do |file|
126           next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
127           begin
128             # Use empty ref to get the index
129             NosOignons::Member.read_from_git('', file)
130           rescue ArgumentError, Psych::SyntaxError
131             $stderr.puts "Désolé : #{file} n'a pas le bon format !"
132             exit 1
133           end
134         end
135       end
136     end
137
138     def pre_receive_hook!(stdin)
139       stdin.readlines.each do |ref_line|
140         old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
141         IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
142           NosOignons::Git.handle_modified_files(io) do |file|
143             next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
144             begin
145               NosOignons::Member.read_from_git(new_value, file)
146             rescue ArgumentError, Psych::SyntaxError
147               $stderr.puts "Désolé : #{file} n'a pas le bon format !"
148               exit 1
149             end
150           end
151         end
152       end
153     end
154   end
155 end