Bug 34883: Stop patron expiry date being set to NULL during import
[koha.git] / Koha / Acquisition / Bookseller.pm
1 package Koha::Acquisition::Bookseller;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::Acquisition::Bookseller::Aliases;
21 use Koha::Acquisition::Bookseller::Contacts;
22 use Koha::Acquisition::Bookseller::Interfaces;
23 use Koha::Acquisition::Bookseller::Issues;
24 use Koha::Subscriptions;
25
26 use base qw( Koha::Object );
27
28 =head1 NAME
29
30 Koha::Acquisition::Bookseller Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 baskets
37
38     my $vendor  = Koha::Acquisition::Booksellers->find( $id );
39     my @baskets = $vendor->baskets();
40
41 Returns the list of baskets for the vendor
42
43 =cut
44
45 sub baskets {
46     my ( $self ) = @_;
47     my $baskets_rs = $self->_result->aqbaskets;
48     return Koha::Acquisition::Baskets->_new_from_dbic( $baskets_rs );
49 }
50
51 =head3 contacts
52
53     my $vendor   = Koha::Acquisition::Booksellers->find( $id );
54     my @contacts = $vendor->contacts();
55
56 Returns the list of contacts for the vendor
57
58 =cut
59
60 sub contacts {
61     my ($self) = @_;
62     my $contacts_rs = $self->_result->aqcontacts;
63     return Koha::Acquisition::Bookseller::Contacts->_new_from_dbic( $contacts_rs );
64 }
65
66 =head3 subscriptions
67
68     my $vendor        = Koha::Acquisition::Booksellers->find( $id );
69     my $subscriptions = $vendor->subscriptions();
70
71 Returns the list of subscriptions for the vendor
72
73 =cut
74
75 sub subscriptions {
76     my ($self) = @_;
77
78     # FIXME FK missing at DB level
79     return Koha::Subscriptions->search( { aqbooksellerid => $self->id } );
80 }
81
82 =head3 aliases
83
84     my $aliases = $vendor->aliases
85
86     $vendor->aliases([{ alias => 'one alias'}]);
87
88 =cut
89
90 sub aliases {
91     my ($self, $aliases) = @_;
92
93     if ($aliases) {
94         my $schema = $self->_result->result_source->schema;
95         $schema->txn_do(
96             sub {
97                 $self->aliases->delete;
98                 for my $alias (@$aliases) {
99                     $self->_result->add_to_aqbookseller_aliases($alias);
100                 }
101             }
102         );
103     }
104
105     my $rs = $self->_result->aqbookseller_aliases;
106     return Koha::Acquisition::Bookseller::Aliases->_new_from_dbic( $rs );
107 }
108
109 =head3 interfaces
110
111     my $interfaces = $vendor->interfaces
112
113     $vendor->interfaces(\@interfaces);
114
115 =cut
116
117 sub interfaces {
118     my ($self, $interfaces) = @_;
119
120     if ($interfaces) {
121         my $schema = $self->_result->result_source->schema;
122         $schema->txn_do(
123             sub {
124                 $self->interfaces->delete;
125                 for my $interface (@$interfaces) {
126                     Koha::Acquisition::Bookseller::Interface->new(
127                         {
128                             %$interface,
129                             vendor_id => $self->id,
130                         }
131                     )->store;
132                 }
133             }
134         );
135     }
136
137     my $rs = $self->_result->aqbookseller_interfaces;
138     return Koha::Acquisition::Bookseller::Interfaces->_new_from_dbic( $rs );
139 }
140
141 =head3 issues
142
143     my $issues = $vendor->issues
144
145 =cut
146
147 sub issues {
148     my ($self) = @_;
149     my $rs = $self->_result->aqbookseller_issues;
150     return Koha::Acquisition::Bookseller::Issues->_new_from_dbic($rs);
151 }
152
153 =head3 to_api_mapping
154
155 This method returns the mapping for representing a Koha::Acquisition::Bookseller object
156 on the API.
157
158 =cut
159
160 sub to_api_mapping {
161     return {
162         listprice       => 'list_currency',
163         invoiceprice    => 'invoice_currency',
164         gstreg          => 'gst',
165         listincgst      => 'list_includes_gst',
166         invoiceincgst   => 'invoice_includes_gst'
167     };
168 }
169
170 =head2 Internal methods
171
172 =head3 _type
173
174 =cut
175
176 sub _type {
177     return 'Aqbookseller';
178 }
179
180 1;