View | Details | Raw Unified | Return to bug 29110
Collapse All | Expand All

(-)a/Koha/ArticleRequest.pm (-91 / +96 lines)
Lines 27-32 use Koha::Items; Link Here
27
use Koha::Libraries;
27
use Koha::Libraries;
28
use Koha::DateUtils qw( dt_from_string );
28
use Koha::DateUtils qw( dt_from_string );
29
use Koha::ArticleRequest::Status;
29
use Koha::ArticleRequest::Status;
30
use Koha::Exceptions;
30
use Koha::Exceptions::ArticleRequest;
31
use Koha::Exceptions::ArticleRequest;
31
32
32
use base qw(Koha::Object);
33
use base qw(Koha::Object);
Lines 39-46 Koha::ArticleRequest - Koha Article Request Object class Link Here
39
40
40
=head2 Class methods
41
=head2 Class methods
41
42
43
=head3 store
44
45
Overridden I<store> method that performs some checks and triggers.
46
42
=cut
47
=cut
43
48
49
sub store {
50
    my ($self) = @_;
51
52
    if ( !$self->in_storage ) {
53
        # new request
54
        Koha::Exceptions::MissingParameter->throw(
55
            "Missing mandatory parameter: borrowernumber")
56
          unless $self->borrowernumber;
57
58
        my $patron = Koha::Patrons->find( $self->borrowernumber );
59
60
        Koha::Exceptions::Object::FKConstraint->throw(
61
            broken_fk => 'borrowernumber',
62
            value     => $self->borrowernumber
63
        ) unless $patron;
64
65
        Koha::Exceptions::ArticleRequest::LimitReached->throw(
66
            error => 'Patron cannot request more articles for today' )
67
          unless $patron->can_request_article;
68
69
        $self->created_on( dt_from_string() );
70
        # this is to avoid calling ->discard_changes to retrieve
71
        # the default value from the DB
72
        $self->status( Koha::ArticleRequest::Status::Requested )
73
            unless $self->status;
74
    }
75
76
    $self->SUPER::store;
77
    $self->notify;
78
79
    return $self;
80
}
81
44
=head3 request
82
=head3 request
45
83
46
=cut
84
=cut
Lines 48-61 Koha::ArticleRequest - Koha Article Request Object class Link Here
48
sub request {
86
sub request {
49
    my ($self) = @_;
87
    my ($self) = @_;
50
88
51
    Koha::Exceptions::ArticleRequest::LimitReached->throw(
52
        error => 'Patron cannot request more articles for today'
53
    ) unless $self->borrower->can_request_article;
54
55
    $self->status(Koha::ArticleRequest::Status::Requested);
89
    $self->status(Koha::ArticleRequest::Status::Requested);
56
    $self->SUPER::store();
90
    return $self->store;
57
    $self->notify();
58
    return $self;
59
}
91
}
60
92
61
=head3 set_pending
93
=head3 set_pending
Lines 66-74 sub set_pending { Link Here
66
    my ($self) = @_;
98
    my ($self) = @_;
67
99
68
    $self->status(Koha::ArticleRequest::Status::Pending);
100
    $self->status(Koha::ArticleRequest::Status::Pending);
69
    $self->SUPER::store();
101
    return $self->store;
70
    $self->notify();
71
    return $self;
72
}
102
}
73
103
74
=head3 process
104
=head3 process
Lines 79-87 sub process { Link Here
79
    my ($self) = @_;
109
    my ($self) = @_;
80
110
81
    $self->status(Koha::ArticleRequest::Status::Processing);
111
    $self->status(Koha::ArticleRequest::Status::Processing);
82
    $self->store();
112
    return $self->store;
83
    $self->notify();
84
    return $self;
85
}
113
}
86
114
87
=head3 complete
115
=head3 complete
Lines 92-100 sub complete { Link Here
92
    my ($self) = @_;
120
    my ($self) = @_;
93
121
94
    $self->status(Koha::ArticleRequest::Status::Completed);
122
    $self->status(Koha::ArticleRequest::Status::Completed);
95
    $self->store();
123
    return $self->store;
96
    $self->notify();
97
    return $self;
98
}
124
}
99
125
100
=head3 cancel
126
=head3 cancel
Lines 107-165 sub cancel { Link Here
107
    $self->status(Koha::ArticleRequest::Status::Canceled);
133
    $self->status(Koha::ArticleRequest::Status::Canceled);
108
    $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
134
    $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
109
    $self->notes($notes) if $notes;
135
    $self->notes($notes) if $notes;
110
    $self->store();
136
    return $self->store;
111
    $self->notify();
112
    return $self;
113
}
114
115
=head3 notify
116
117
=cut
118
119
sub notify {
120
    my ($self) = @_;
121
122
    my $status = $self->status;
123
    my $reason = $self->notes;
124
    if ( !defined $reason && $self->cancellation_reason ) {
125
        my $av = Koha::AuthorisedValues->search(
126
            {
127
                category            => 'AR_CANCELLATION',
128
                authorised_value    => $self->cancellation_reason
129
            }
130
        )->next;
131
        $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
132
    }
133
134
    require C4::Letters;
135
    if (
136
        my $letter = C4::Letters::GetPreparedLetter(
137
            module                 => 'circulation',
138
            letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
139
            message_transport_type => 'email',
140
            lang                   => $self->borrower->lang,
141
            tables                 => {
142
                article_requests => $self->id,
143
                borrowers        => $self->borrowernumber,
144
                biblio           => $self->biblionumber,
145
                biblioitems      => $self->biblionumber,
146
                items            => $self->itemnumber,
147
                branches         => $self->branchcode,
148
            },
149
            substitute => {
150
                reason => $reason,
151
            },
152
        )
153
      )
154
    {
155
        C4::Letters::EnqueueLetter(
156
            {
157
                letter                 => $letter,
158
                borrowernumber         => $self->borrowernumber,
159
                message_transport_type => 'email',
160
            }
161
        ) or warn "can't enqueue letter " . $letter->{code};
162
    }
163
}
137
}
164
138
165
=head3 biblio
139
=head3 biblio
Lines 171-179 Returns the Koha::Biblio object for this article request Link Here
171
sub biblio {
145
sub biblio {
172
    my ($self) = @_;
146
    my ($self) = @_;
173
147
174
    $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
148
    my $biblio = $self->_result->biblio;
175
149
    return Koha::Biblio->_new_from_dbic( $biblio );
176
    return $self->{_biblio};
177
}
150
}
178
151
179
=head3 item
152
=head3 item
Lines 185-193 Returns the Koha::Item object for this article request Link Here
185
sub item {
158
sub item {
186
    my ($self) = @_;
159
    my ($self) = @_;
187
160
188
    $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
161
    my $item = $self->_result->item;
189
162
    return Koha::Item->_new_from_dbic( $item );
190
    return $self->{_item};
191
}
163
}
192
164
193
=head3 borrower
165
=head3 borrower
Lines 199-207 Returns the Koha::Patron object for this article request Link Here
199
sub borrower {
171
sub borrower {
200
    my ($self) = @_;
172
    my ($self) = @_;
201
173
202
    $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
174
    my $patron = $self->_result->patron;
203
175
    return Koha::Patron->_new_from_dbic( $patron );
204
    return $self->{_borrower};
205
}
176
}
206
177
207
=head3 branch
178
=head3 branch
Lines 213-241 Returns the Koha::Library object for this article request Link Here
213
sub branch {
184
sub branch {
214
    my ($self) = @_;
185
    my ($self) = @_;
215
186
216
    $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
187
    my $library = $self->_result->library;
217
188
    return Koha::Library->_new_from_dbic( $library );
218
    return $self->{_branch};
219
}
189
}
220
190
221
=head3 store
191
=head2 Internal methods
222
192
223
Override the default store behavior so that new opac requests
193
=head3 notify
224
will have notifications sent.
225
194
226
=cut
195
=cut
227
196
228
sub store {
197
sub notify {
229
    my ($self) = @_;
198
    my ($self) = @_;
230
    if ( $self->in_storage ) {
199
231
        return $self->SUPER::store;
200
    my $status = $self->status;
232
    } else {
201
    my $reason = $self->notes;
233
        $self->created_on( dt_from_string() );
202
    if ( !defined $reason && $self->cancellation_reason ) {
234
        return $self->request;
203
        my $av = Koha::AuthorisedValues->search(
204
            {
205
                category            => 'AR_CANCELLATION',
206
                authorised_value    => $self->cancellation_reason
207
            }
208
        )->next;
209
        $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
210
    }
211
212
    require C4::Letters;
213
    if (
214
        my $letter = C4::Letters::GetPreparedLetter(
215
            module                 => 'circulation',
216
            letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
217
            message_transport_type => 'email',
218
            lang                   => $self->borrower->lang,
219
            tables                 => {
220
                article_requests => $self->id,
221
                borrowers        => $self->borrowernumber,
222
                biblio           => $self->biblionumber,
223
                biblioitems      => $self->biblionumber,
224
                items            => $self->itemnumber,
225
                branches         => $self->branchcode,
226
            },
227
            substitute => {
228
                reason => $reason,
229
            },
230
        )
231
      )
232
    {
233
        C4::Letters::EnqueueLetter(
234
            {
235
                letter                 => $letter,
236
                borrowernumber         => $self->borrowernumber,
237
                message_transport_type => 'email',
238
            }
239
        ) or warn "can't enqueue letter " . $letter->{code};
235
    }
240
    }
236
}
241
}
237
242
238
=head2 Internal methods
243
239
244
240
=head3 _type
245
=head3 _type
241
246
(-)a/Koha/Schema/Result/ArticleRequest.pm (-2 / +37 lines)
Lines 293-298 __PACKAGE__->belongs_to( Link Here
293
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-08-18 10:41:01
293
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-08-18 10:41:01
294
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1q7Vlki3fRVBdww1zHLCRA
294
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1q7Vlki3fRVBdww1zHLCRA
295
295
296
__PACKAGE__->belongs_to(
297
  "biblio",
298
  "Koha::Schema::Result::Biblio",
299
  { biblionumber => "biblionumber" },
300
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
301
);
302
303
__PACKAGE__->belongs_to(
304
  "item",
305
  "Koha::Schema::Result::Item",
306
  { itemnumber => "itemnumber" },
307
  {
308
    is_deferrable => 1,
309
    join_type     => "LEFT",
310
    on_delete     => "SET NULL",
311
    on_update     => "CASCADE",
312
  },
313
);
314
315
__PACKAGE__->belongs_to(
316
  "library",
317
  "Koha::Schema::Result::Branch",
318
  { branchcode => "branchcode" },
319
  {
320
    is_deferrable => 1,
321
    join_type     => "LEFT",
322
    on_delete     => "SET NULL",
323
    on_update     => "CASCADE",
324
  },
325
);
326
327
__PACKAGE__->belongs_to(
328
  "patron",
329
  "Koha::Schema::Result::Borrower",
330
  { borrowernumber => "borrowernumber" },
331
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
332
);
296
333
297
# You can replace this text with custom code or comments, and it will be preserved on regeneration
298
1;
334
1;
299
- 

Return to bug 29110