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

(-)a/opac/opac-discharge.pl (-33 / +41 lines)
Lines 21-27 use Modern::Perl; Link Here
21
use Carp qw( carp );
21
use Carp qw( carp );
22
22
23
use C4::Auth qw( get_template_and_user );
23
use C4::Auth qw( get_template_and_user );
24
use CGI qw( -utf8 );
24
use CGI      qw( -utf8 );
25
use C4::Context;
25
use C4::Context;
26
use C4::Output qw( output_html_with_http_headers );
26
use C4::Output qw( output_html_with_http_headers );
27
use Koha::Patrons;
27
use Koha::Patrons;
Lines 37-47 unless ( C4::Context->preference('useDischarge') ) { Link Here
37
my $op = $input->param("op") // '';
37
my $op = $input->param("op") // '';
38
38
39
# Getting the template and auth
39
# Getting the template and auth
40
my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
40
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41
    template_name => "opac-discharge.tt",
41
    {
42
    query         => $input,
42
        template_name => "opac-discharge.tt",
43
    type          => "opac",
43
        query         => $input,
44
});
44
        type          => "opac",
45
    }
46
);
45
47
46
my ( $can_be_discharged, $discharge_problems ) =
48
my ( $can_be_discharged, $discharge_problems ) =
47
    Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $loggedinuser } );
49
    Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $loggedinuser } );
Lines 50-87 my $has_pending_checkouts = $patron->checkouts->count; Link Here
50
my $has_debt              = ( $patron->account->outstanding_debits->total_outstanding > 0 );
52
my $has_debt              = ( $patron->account->outstanding_debits->total_outstanding > 0 );
51
53
52
$template->param(
54
$template->param(
53
    can_be_discharged   => $can_be_discharged,
55
    can_be_discharged  => $can_be_discharged,
54
    discharge_problems => $discharge_problems,
56
    discharge_problems => $discharge_problems,
55
    has_checkouts       => $has_pending_checkouts,
57
    has_checkouts      => $has_pending_checkouts,
56
    has_debt            => $has_debt,
58
    has_debt           => $has_debt,
57
);
59
);
58
60
59
my $pending = Koha::Patron::Discharge::count({
61
my $pending = Koha::Patron::Discharge::count(
60
    borrowernumber => $loggedinuser,
62
    {
61
    pending        => 1,
63
        borrowernumber => $loggedinuser,
62
});
64
        pending        => 1,
63
my $available = Koha::Patron::Discharge::is_discharged({borrowernumber => $loggedinuser});
65
    }
66
);
67
my $available = Koha::Patron::Discharge::is_discharged( { borrowernumber => $loggedinuser } );
64
68
65
if ( $op eq 'cud-request' ) {
69
if ( $op eq 'cud-request' ) {
66
    if ($pending || $available) {
70
    if ( $pending || $available ) {
71
67
        # Request already done
72
        # Request already done
68
        print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
73
        print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
69
        exit;
74
        exit;
70
    }
75
    }
71
    my $success = Koha::Patron::Discharge::request({
76
    my $success = Koha::Patron::Discharge::request(
72
        borrowernumber => $loggedinuser,
77
        {
73
    });
78
            borrowernumber => $loggedinuser,
79
        }
80
    );
74
    if ($success) {
81
    if ($success) {
75
        $template->param( success => 1 );
82
        $template->param( success => 1 );
76
    }
83
    } else {
77
    else {
84
        $template->param( failure    => 1 );
78
        $template->param( failure => 1 );
79
        $template->param( has_issues => $has_pending_checkouts );
85
        $template->param( has_issues => $has_pending_checkouts );
80
        $template->param( has_debt => $has_debt );
86
        $template->param( has_debt   => $has_debt );
81
    }
87
    }
82
}
88
} elsif ( $op eq 'get' ) {
83
elsif ( $op eq 'get' ) {
84
    unless ($available) {
89
    unless ($available) {
90
85
        # No valid discharge to get
91
        # No valid discharge to get
86
        print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
92
        print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
87
        exit;
93
        exit;
Lines 89-99 elsif ( $op eq 'get' ) { Link Here
89
    eval {
95
    eval {
90
96
91
        # Getting member data
97
        # Getting member data
92
        my $patron = Koha::Patrons->find( $loggedinuser );
98
        my $patron   = Koha::Patrons->find($loggedinuser);
93
        my $pdf_path = Koha::Patron::Discharge::generate_as_pdf({
99
        my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
94
            borrowernumber => $loggedinuser,
100
            {
95
            branchcode => $patron->branchcode,
101
                borrowernumber => $loggedinuser,
96
        });
102
                branchcode     => $patron->branchcode,
103
            }
104
        );
97
105
98
        binmode(STDOUT);
106
        binmode(STDOUT);
99
        print $input->header(
107
        print $input->header(
Lines 106-120 elsif ( $op eq 'get' ) { Link Here
106
        close $fh;
114
        close $fh;
107
        print @lines;
115
        print @lines;
108
    };
116
    };
109
    if ( $@ ) {
117
    if ($@) {
110
        carp $@;
118
        carp $@;
111
        $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
119
        $template->param( messages => [ { type => 'error', code => 'unable_to_generate_pdf' } ] );
112
    } else {
120
    } else {
121
113
        # no error, pdf is sent, so stop sending data to browser
122
        # no error, pdf is sent, so stop sending data to browser
114
        exit;
123
        exit;
115
    }
124
    }
116
}
125
} else {
117
else {
118
    $template->param(
126
    $template->param(
119
        available => $available,
127
        available => $available,
120
        pending   => $pending,
128
        pending   => $pending,
(-)a/t/db_dependent/Patron/Borrower_Discharge.t (-63 / +94 lines)
Lines 25-31 use IPC::Cmd qw(can_run); Link Here
25
use MARC::Record;
25
use MARC::Record;
26
26
27
use C4::Circulation qw( AddIssue AddReturn );
27
use C4::Circulation qw( AddIssue AddReturn );
28
use C4::Biblio qw( AddBiblio );
28
use C4::Biblio      qw( AddBiblio );
29
use C4::Context;
29
use C4::Context;
30
30
31
use Koha::Patrons;
31
use Koha::Patrons;
Lines 35-41 use Koha::Database; Link Here
35
use t::lib::TestBuilder;
35
use t::lib::TestBuilder;
36
use t::lib::Mocks;
36
use t::lib::Mocks;
37
37
38
my $schema  = Koha::Database->new->schema;
38
my $schema = Koha::Database->new->schema;
39
$schema->storage->txn_begin;
39
$schema->storage->txn_begin;
40
40
41
my $builder = t::lib::TestBuilder->new;
41
my $builder = t::lib::TestBuilder->new;
Lines 43-51 my $builder = t::lib::TestBuilder->new; Link Here
43
my $dbh = C4::Context->dbh;
43
my $dbh = C4::Context->dbh;
44
$dbh->do(q|DELETE FROM discharges|);
44
$dbh->do(q|DELETE FROM discharges|);
45
45
46
my $library         = $builder->build({ source => 'Branch' });
46
my $library         = $builder->build( { source => 'Branch' } );
47
my $another_library = $builder->build({ source => 'Branch' });
47
my $another_library = $builder->build( { source => 'Branch' } );
48
my $itemtype        = $builder->build({ source => 'Itemtype' })->{itemtype};
48
my $itemtype        = $builder->build( { source => 'Itemtype' } )->{itemtype};
49
49
50
my $patron = $builder->build_object({
50
my $patron = $builder->build_object({
51
    class => 'Koha::Patrons',
51
    class => 'Koha::Patrons',
Lines 54-77 my $patron = $builder->build_object({ Link Here
54
        flags      => 1, # superlibrarian
54
        flags      => 1, # superlibrarian
55
    }
55
    }
56
});
56
});
57
t::lib::Mocks::mock_userenv({ patron => $patron });
57
t::lib::Mocks::mock_userenv( { patron => $patron } );
58
58
59
my $patron2 = $builder->build_object({
59
my $patron2 = $builder->build_object(
60
    class => 'Koha::Patrons',
60
    {
61
    value => {
61
        class => 'Koha::Patrons',
62
        branchcode => $library->{branchcode},
62
        value => {
63
            branchcode => $library->{branchcode},
64
        }
63
    }
65
    }
64
});
66
);
65
my $patron3 = $builder->build_object({
67
my $patron3 = $builder->build_object(
66
    class => 'Koha::Patrons',
68
    {
67
    value => {
69
        class => 'Koha::Patrons',
68
        branchcode => $another_library->{branchcode},
70
        value => {
69
        flags => undef,
71
            branchcode => $another_library->{branchcode},
72
            flags      => undef,
73
        }
70
    }
74
    }
71
});
75
);
72
76
73
# Discharge not possible with issues
77
# Discharge not possible with issues
74
my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
78
my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
75
my $barcode = 'BARCODE42';
79
my $barcode = 'BARCODE42';
76
$builder->build_sample_item(
80
$builder->build_sample_item(
77
    {
81
    {
Lines 83-97 $builder->build_sample_item( Link Here
83
);
87
);
84
88
85
AddIssue( $patron, $barcode );
89
AddIssue( $patron, $barcode );
86
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
90
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
87
is( $can, 0, 'A patron with issues cannot be discharged' );
91
is( $can,                   0,     'A patron with issues cannot be discharged' );
88
is( $problems->{checkouts}, 1, "Patron has checkouts" );
92
is( $problems->{checkouts}, 1,     "Patron has checkouts" );
89
is( $problems->{debt}, undef, "Patron has no debt" );
93
is( $problems->{debt},      undef, "Patron has no debt" );
90
94
91
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }), undef, 'No request done if patron has issues' );
95
is(
92
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->borrowernumber }), undef, 'No discharge done if patron has issues' );
96
    Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ), undef,
93
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
97
    'No request done if patron has issues'
94
is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
98
);
99
is(
100
    Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ), undef,
101
    'No discharge done if patron has issues'
102
);
103
is_deeply( [Koha::Patron::Discharge::get_pendings],  [], 'There is no pending discharge request' );
104
is_deeply( [Koha::Patron::Discharge::get_validated], [], 'There is no validated discharge' );
95
105
96
# Discharge not possible with issues and fines
106
# Discharge not possible with issues and fines
97
$patron->account->add_debit(
107
$patron->account->add_debit(
Lines 102-125 $patron->account->add_debit( Link Here
102
    }
112
    }
103
);
113
);
104
114
105
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
115
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
106
is(
116
is(
107
    $can, 0,
117
    $can, 0,
108
    'A patron with fines and checkouts cannot be discharged'
118
    'A patron with fines and checkouts cannot be discharged'
109
);
119
);
110
is( $problems->{checkouts}, 1, "Patron has checkouts" );
120
is( $problems->{checkouts}, 1,   "Patron has checkouts" );
111
is( $problems->{debt}, 0.1, "Patron has debt" );
121
is( $problems->{debt},      0.1, "Patron has debt" );
112
122
113
AddReturn( $barcode );
123
AddReturn($barcode);
114
124
115
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
125
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
116
is(
126
is(
117
    $can, 0,
127
    $can, 0,
118
    'A patron with fines cannot be discharged'
128
    'A patron with fines cannot be discharged'
119
);
129
);
120
is( $problems->{checkouts}, undef, "Patron has checkouts" );
130
is( $problems->{checkouts}, undef, "Patron has checkouts" );
121
is( $problems->{debt}, 0.1, "Patron has debt" );
131
is( $problems->{debt},      0.1,   "Patron has debt" );
122
123
132
124
$patron->account->pay(
133
$patron->account->pay(
125
    {
134
    {
Lines 128-160 $patron->account->pay( Link Here
128
);
137
);
129
138
130
# Discharge possible without issue or fine
139
# Discharge possible without issue or fine
131
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
140
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
132
is(
141
is(
133
    $can, 1,
142
    $can, 1,
134
    'A patron without issues or fines can be discharged'
143
    'A patron without issues or fines can be discharged'
135
);
144
);
136
is( scalar keys %$problems, 0, "No dishcharge problems found" );
145
is( scalar keys %$problems, 0, "No dishcharge problems found" );
137
146
138
is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
147
is( Koha::Patron::Discharge::generate_as_pdf, undef, "Confirm failure when lacking borrower number" );
139
148
140
# Verify that the user is not discharged anymore if the restriction has been lifted
149
# Verify that the user is not discharged anymore if the restriction has been lifted
141
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
150
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
142
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
151
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
143
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
152
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
144
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, 'The patron has been discharged' );
153
is(
145
is( Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
154
    Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1,
146
is( scalar( Koha::Patron::Discharge::get_validated ),             3,            'There are 3 validated discharges' );
155
    'The patron has been discharged'
147
is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, 'There is 1 validated discharge for a given patron' );
156
);
148
is( scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2, 'There is 2 validated discharges for a given branchcode' );    # This is not used in the code yet
157
is(
158
    Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31',
159
    'The patron has been debarred after discharge'
160
);
161
is( scalar(Koha::Patron::Discharge::get_validated), 3, 'There are 3 validated discharges' );
162
is(
163
    scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1,
164
    'There is 1 validated discharge for a given patron'
165
);
166
is(
167
    scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2,
168
    'There is 2 validated discharges for a given branchcode'
169
);    # This is not used in the code yet
149
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
170
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
150
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
171
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
151
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 'The patron is not discharged after the restriction has been lifted' );
172
ok(
173
    !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ),
174
    'The patron is not discharged after the restriction has been lifted'
175
);
152
176
153
# Verify that the discharge works multiple times
177
# Verify that the discharge works multiple times
154
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
178
Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } );
155
is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
179
is( scalar(Koha::Patron::Discharge::get_pendings), 1, 'There is a pending discharge request (second time)' );
156
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
180
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
157
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
181
is_deeply( [Koha::Patron::Discharge::get_pendings], [], 'There is no pending discharge request (second time)' );
158
182
159
SKIP: {
183
SKIP: {
160
    skip "Skipping because weasyprint is not installed",
184
    skip "Skipping because weasyprint is not installed",
Lines 171-216 SKIP: { Link Here
171
    $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
195
    $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
172
196
173
    my $result;
197
    my $result;
174
    warning_is
198
    warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
175
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
199
    'Some error',
176
        'Some error',
177
        'Failed call to run() prints the generated error';
200
        'Failed call to run() prints the generated error';
178
201
179
    is( $result, undef, 'undef returned if failed run' );
202
    is( $result, undef, 'undef returned if failed run' );
180
203
181
    $mocked_ipc->mock( 'can_run', undef );
204
    $mocked_ipc->mock( 'can_run', undef );
182
205
183
    warning_is
206
    warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
184
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
207
    'weasyprint not found!',
185
        'weasyprint not found!',
186
        'Expected failure because of missing weasyprint';
208
        'Expected failure because of missing weasyprint';
187
209
188
    is( $result, undef, 'undef returned if missing weasyprint' );
210
    is( $result, undef, 'undef returned if missing weasyprint' );
189
}
211
}
190
212
191
# FIXME Should be a Koha::Object object
213
# FIXME Should be a Koha::Object object
192
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
214
is(
215
    ref( Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ) ),
216
    'Koha::Schema::Result::Discharge', 'Discharge request sent'
217
);
193
218
194
subtest 'search_limited' => sub {
219
subtest 'search_limited' => sub {
195
    plan tests => 4;
220
    plan tests => 4;
196
    $dbh->do(q|DELETE FROM discharges|);
221
    $dbh->do(q|DELETE FROM discharges|);
197
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
222
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
198
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
223
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
224
199
    # $patron and $patron2 are from the same library, $patron3 from another one
225
    # $patron and $patron2 are from the same library, $patron3 from another one
200
    # Logged in user is $patron, superlibrarian
226
    # Logged in user is $patron, superlibrarian
201
    t::lib::Mocks::mock_userenv({ patron => $patron });
227
    t::lib::Mocks::mock_userenv( { patron => $patron } );
202
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
228
    Koha::Library::Group->new( { parent_id => $group_1->id, branchcode => $patron->branchcode } )->store();
203
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron3->branchcode })->store();
229
    Koha::Library::Group->new( { parent_id => $group_2->id, branchcode => $patron3->branchcode } )->store();
204
    Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
230
    Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } );
205
    Koha::Patron::Discharge::request({ borrowernumber => $patron2->borrowernumber });
231
    Koha::Patron::Discharge::request( { borrowernumber => $patron2->borrowernumber } );
206
    Koha::Patron::Discharge::request({ borrowernumber => $patron3->borrowernumber });
232
    Koha::Patron::Discharge::request( { borrowernumber => $patron3->borrowernumber } );
207
    is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
233
    is( scalar(Koha::Patron::Discharge::get_pendings),      3, 'With permission, all discharges are visible' );
208
    is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
234
    is( Koha::Patron::Discharge::count( { pending => 1 } ), 3, 'With permission, all discharges are visible' );
209
235
210
    # With patron 3 logged in, only discharges from their group are visible
236
    # With patron 3 logged in, only discharges from their group are visible
211
    t::lib::Mocks::mock_userenv({ patron => $patron3 });
237
    t::lib::Mocks::mock_userenv( { patron => $patron3 } );
212
    is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
238
    is(
213
    is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
239
        scalar(Koha::Patron::Discharge::get_pendings), 1,
240
        'Without permission, only discharge from our group are visible'
241
    );
242
    is(
243
        Koha::Patron::Discharge::count( { pending => 1 } ), 1,
244
        'Without permission, only discharge from our group are visible'
245
    );
214
};
246
};
215
247
216
$schema->storage->txn_rollback;
248
$schema->storage->txn_rollback;
217
- 

Return to bug 14250