]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons.rb
Pas d'envoi de rappels si la cotisation a déjà été payée dans l'année
[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     Identifiant SIREN 842 479 313
31     https://nos-oignons.net/
32     contact@nos-oignons.net
33     Téléphone : +33 9 72 42 96 04
34     Fax : +33 9 72 42 96 06
35   EOT
36   POSTAL_ADDRESS = <<-EOT.gsub(/^    /, '')
37     Nos oignons
38     Centre UBIDOCA, 7585
39     105 route des Pommiers
40     74370 Saint Martin Bellevue
41     France
42   EOT
43
44   # The following class methods are all meant to be called as command-line scripts
45   class << self
46     def create_membership_fee_receipt!(member_id, amount)
47       member = NosOignons::Member.new(member_id)
48       member.create_receipt!(amount)
49     end
50
51     def list_emails!
52       NosOignons::Member.all.each do |member|
53         if member.up_to_date?
54           puts member.email
55         end
56       end
57     end
58
59     def update_ag_subscribers!
60       list = NosOignons::MEMBER_MAILING_LIST
61
62       current_emails = NosOignons::Mailman.list_members(list)
63       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
64
65       emails_to_add = uptodate_emails - current_emails
66       NosOignons::Mailman.add_members(list, emails_to_add) unless emails_to_add.empty?
67       emails_to_remove = current_emails - uptodate_emails
68       NosOignons::Mailman.remove_members(list, emails_to_remove) unless emails_to_remove.empty?
69     end
70
71     def send_membership_reminders!
72       today = Time.now.to_date
73       NosOignons::Member.all.select(&:up_to_date?).each do |member|
74         next if member.membership_fee_paid_on.year == today.year
75         reminders = NosOignons::Reminder.all.sort_by(&:days)
76         anniversary = Time.new(today.year, member.joined_on.month,
77                                member.joined_on.day).to_date
78         next if member.membership_fee_paid_on >= anniversary - reminders.last.days
79         reminders.each do |reminder|
80           next if (anniversary - today).to_i > reminder.days
81           next if member.reminded_on && (anniversary - member.reminded_on).to_i <= reminder.days
82
83           member.remind(reminder)
84           break
85         end
86       end
87     end
88
89     def send_member_emails_to_advisors!
90       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
91       subject = 'Adresses email des membres à jour de cotisation'
92       body = <<-END_OF_BODY.gsub(/^        /, '')
93         Cher comité de déontologie de Nos oignons,
94
95         Comme le prévoient les statuts l'article 12 des statuts de
96         l'association, vous devez être en mesure de pouvoir convoquer une
97         assemblée générale extraordinaire. Pour ce faire, voici donc la liste
98         des emails à jour de cotisation à la date d'aujourd'hui :
99
100         #{uptodate_emails.join("\n")}
101
102         Et merci encore de votre engagement auprès de Nos oignons !
103
104         -- 
105         Le robot du conseil d'administration
106
107                               GNU AGPLv3 © Nos oignons <contact@nos-oignons.net>
108                                git clone https://nos-oignons.net/gestion-adh.git
109       END_OF_BODY
110       mail = Mail.new :charset => 'utf-8',
111                       :from => NosOignons::BOARD_EMAIL,
112                       :to => NosOignons::ADVISORS_EMAIL,
113                       :subject => subject,
114                       :body => body
115       mail.deliver
116     end
117
118     def pre_commit_hook!
119       if system('git rev-parse --quiet --verify HEAD >/dev/null')
120         against = 'HEAD'
121       else
122         # Initial commit: diff against an empty tree object
123         against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
124       end
125
126       IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
127         NosOignons::Git.handle_modified_files(io) do |file|
128           next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
129           begin
130             # Use empty ref to get the index
131             NosOignons::Member.read_from_git('', file)
132           rescue ArgumentError, Psych::SyntaxError
133             $stderr.puts "Désolé : #{file} n'a pas le bon format !"
134             exit 1
135           end
136         end
137       end
138     end
139
140     def pre_receive_hook!(stdin)
141       stdin.readlines.each do |ref_line|
142         old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
143         IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
144           NosOignons::Git.handle_modified_files(io) do |file|
145             next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
146             begin
147               NosOignons::Member.read_from_git(new_value, file)
148             rescue ArgumentError, Psych::SyntaxError
149               $stderr.puts "Désolé : #{file} n'a pas le bon format !"
150               exit 1
151             end
152           end
153         end
154       end
155     end
156   end
157 end