]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons/member.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 / member.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 'date'
20 require 'safe_yaml'
21 SafeYAML::OPTIONS[:default_mode] = :safe
22
23 require 'nos_oignons/reminder_db'
24
25 module NosOignons
26   MEMBER_FIELDS = [:name, :address, :email, :joined_on, :membership_fee_paid_on]
27   MEMBER_MANDATORY_FIELDS = [:name, :email]
28   # Directory in the board wiki which holds the member pages
29   MEMBERS_DB_DIR = 'Membres'
30
31   class Member < Struct.new(*MEMBER_FIELDS)
32     class << self
33       def db_path
34         if ENV['NOS_OIGNONS_BOARD_WIKI_PATH']
35           @db_path = File.join(ENV['NOS_OIGNONS_BOARD_WIKI_PATH'], MEMBERS_DB_DIR)
36         else
37           return @db_path if @db_path
38
39           git_path = `git rev-parse --show-toplevel`.strip
40           if File.exists?(File.join(git_path, MEMBERS_DB_DIR))
41             @db_path = File.join(git_path, MEMBERS_DB_DIR)
42           else
43             @db_path = File.join(File.expand_path('../wiki-ca', git_path), MEMBERS_DB_DIR)
44           end
45         end
46         @db_path
47       end
48
49       def all
50         Dir.glob("#{db_path}/*.mdwn").sort.collect do |file|
51           member_id = File.basename(file).gsub(/\.mdwn$/, '')
52           Member.new(member_id)
53         end
54       end
55
56       def filename_for_id(member_id)
57         case member_id
58         when String
59           "#{NosOignons::Member.db_path}/#{member_id}.mdwn"
60         when Integer
61           "#{NosOignons::Member.db_path}/%06d.mdwn" % member_id
62         end
63       end
64
65       def read_from_git(ref, file)
66         IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
67           member_id = File.basename(file).gsub(/\.mdwn$/, '')
68           Member.new(member_id, f.read)
69         end
70       end
71     end
72
73     attr_reader :member_id
74
75     def initialize(member_id, page_content=nil)
76       unless member_id =~ /\A\d{6}\z/
77         raise ArgumentError.new('bad member id format')
78       end
79       @member_id = member_id
80       unless page_content
81         begin
82           page_content = File.open(Member.filename_for_id(member_id)).read
83         rescue Errno::ENOENT
84           raise ArgumentError.new('unknown member')
85         end
86       end
87       unless page_content.start_with?("---\n")
88         raise ArgumentError.new('content is not a proper YAML document')
89       end
90       yaml_content = /\A---\n(.*)\n---\n/m.match(page_content)[1]
91       data = YAML.load(yaml_content)
92       MEMBER_FIELDS.each do |field|
93         self[field] = data[field.to_s]
94       end
95       # Immutability for the win
96       MEMBER_FIELDS.each do |field|
97         instance_eval{ undef :"#{field}=" }
98       end
99       MEMBER_MANDATORY_FIELDS.each do |sym|
100         raise ArgumentError.new('missing mandatory fields') unless self[sym]
101       end
102       [:joined_on, :membership_fee_paid_on].each do |sym|
103         if self[sym] && !self[sym].is_a?(Date)
104           raise ArgumentError.new("#{sym.to_s} is not a date")
105         end
106       end
107     end
108
109     def up_to_date?
110       return false if !joined_on || !membership_fee_paid_on
111
112       today = Time.now.to_date
113       expire_on = Time.new(membership_fee_paid_on.year + 1, joined_on.month, joined_on.day).to_date
114       today <= expire_on
115     end
116
117     def remind(reminder)
118       reminder.send(self)
119       ReminderDb.instance.record(self)
120     end
121
122     def reminded_on
123       ReminderDb.instance.last_reminder(self)
124     end
125
126     def create_receipt!(amount)
127       require 'nos_oignons/receipt'
128
129       receipt = Receipt.new(self, amount)
130       receipt.create!
131     end
132   end
133 end