]> git.koha-community.org Git - koha.git/blob - C4/Bookseller/Contact.pm
Bug 5260 - Add option to send an order by e-mail to the acquisition module
[koha.git] / C4 / Bookseller / Contact.pm
1 package C4::Bookseller::Contact;
2
3 # Copyright 2013 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 C4::Bookseller::Contact - object class for contacts associated with vendors
23
24 =head1 SYNPOSIS
25
26 This class provides an object-oriented interface for handling vendor contacts.
27 It uses Class::Accessor to provide access to the following fields:
28
29 =head1 FIELDS
30
31 =over 8
32
33 =item id
34
35 ID of the contact. This is not used initially, since contacts are actually
36 stored in the aqbooksellers table.
37
38 =item name
39
40 Contact name.
41
42 =item position
43
44 Contact's position.
45
46 =item phone
47
48 Contact's primary phone number.
49
50 =item altphone
51
52 Contact's alternate phone number.
53
54 =item fax
55
56 Contact's fax number.
57
58 =item email
59
60 Contact's e-mail address.
61
62 =item notes
63
64 Notes about contact.
65
66 =item orderacquisition
67
68 Whether the contact should receive acquisitions orders.
69
70 =item claimacquisition
71
72 Whether the contact should receive acquisitions claims.
73
74 =item claimissues
75
76 Whether the contact should receive serials claims.
77
78 =item acqprimary
79
80 Whether the contact is the primary contact for acquisitions.
81
82 =item serialsprimary
83
84 Whether the contact is the primary contact for serials.
85
86 =item bookseller
87
88 ID of the bookseller the contact is associated with.
89
90 =back
91
92 =cut
93
94 use Modern::Perl;
95 use C4::Context;
96
97 use base qw(Class::Accessor);
98
99 __PACKAGE__->mk_accessors(qw(id name position phone altphone fax email notes orderacquisition claimacquisition claimissues acqprimary serialsprimary bookseller));
100
101 =head1 METHODS
102
103 =head2 get_from_bookseller
104
105     my @contacts = @{C4::Bookseller::Contact->get_from_bookseller($booksellerid)};
106
107 Returns a reference to an array of C4::Bookseller::Contact objects for the
108 specified bookseller. This will always return at least one item, though that one
109 item may be an empty contact.
110
111 =cut
112
113 sub get_from_bookseller {
114     my ($class, $bookseller) = @_;
115
116     return unless $bookseller;
117
118     my @contacts;
119     my $query = "SELECT * FROM aqcontacts WHERE booksellerid = ?";
120     my $dbh = C4::Context->dbh;
121     my $sth = $dbh->prepare($query);
122     $sth->execute($bookseller);
123     while (my $rec = $sth->fetchrow_hashref()) {
124         push @contacts, $class->new($rec);
125     }
126
127     push @contacts, $class->new() unless @contacts;
128
129     return \@contacts;
130 }
131
132
133 =head2 fetch
134
135     my $contact = C4::Bookseller::Contact->fetch($contactid);
136
137 Retrieves the specified contact from the database. Currently commented out
138 because there is no separate table from which contacts can be fetched.
139
140 =cut
141
142 sub fetch {
143     my ($class, $id) = @_;
144
145     my $rec = { };
146     if ($id) {
147         my $query = "SELECT * FROM aqcontacts WHERE id = ?";
148         my $dbh = C4::Context->dbh;
149         my $sth = $dbh->prepare($query);
150         $sth->execute($id);
151         $rec = $sth->fetchrow_hashref();
152     }
153     my $self = $class->new($rec);
154     bless $self, $class;
155     return $self;
156 }
157
158 =head2 save
159
160     $contact->save();
161
162 Save a contact to the database.
163
164 =cut
165
166 sub save {
167     my ($self) = @_;
168
169     my $query;
170     my @params = (
171         $self->name,  $self->position,
172         $self->phone, $self->altphone,
173         $self->fax,   $self->email,
174         $self->notes, $self->acqprimary ? 1 : 0,
175         $self->serialsprimary ? 1 : 0,
176         $self->orderacquisition ? 1 : 0, $self->claimacquisition ? 1 : 0,
177         $self->claimissues ? 1 : 0, $self->bookseller
178     );
179     if ($self->id) {
180         $query = 'UPDATE aqcontacts SET name = ?, position = ?, phone = ?, altphone = ?, fax = ?, email = ?, notes = ?, acqprimary = ?, serialsprimary = ?, orderacquisition = ?, claimacquisition = ?, claimissues = ?, booksellerid = ? WHERE id = ?;';
181         push @params, $self->id;
182     } else {
183         $query = 'INSERT INTO aqcontacts (name, position, phone, altphone, fax, email, notes, acqprimary, serialsprimary, orderacquisition, claimacquisition, claimissues, booksellerid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
184     }
185     my $dbh = C4::Context->dbh;
186     my $sth = $dbh->prepare($query);
187     $sth->execute(@params);
188     $self->id($dbh->{'mysql_insertid'}) unless $self->id;
189     return $self->id;
190 }
191
192 sub delete {
193     my ($self) = @_;
194
195     return unless $self->id;
196
197     my $dbh = C4::Context->dbh;
198     my $sth = $dbh->prepare("DELETE FROM aqcontacts WHERE id = ?;");
199     $sth->execute($self->id);
200     return;
201 }
202
203 =head1 AUTHOR
204
205 Jared Camins-Esakov <jcamins@cpbibliography.com>
206
207 =cut
208
209 1;