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 (-69 / +102 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-78 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
C4::Context->_new_userenv('xxx');
50
C4::Context->_new_userenv('xxx');
51
my $patron = $builder->build_object({
51
my $patron = $builder->build_object(
52
    class => 'Koha::Patrons',
52
    {
53
    value => {
53
        class => 'Koha::Patrons',
54
        branchcode => $library->{branchcode},
54
        value => {
55
        flags      => 1, # superlibrarian
55
            branchcode => $library->{branchcode},
56
            flags      => 1,                        # superlibrarian
57
        }
56
    }
58
    }
57
});
59
);
58
t::lib::Mocks::mock_userenv({ patron => $patron });
60
t::lib::Mocks::mock_userenv( { patron => $patron } );
59
61
60
my $patron2 = $builder->build_object({
62
my $patron2 = $builder->build_object(
61
    class => 'Koha::Patrons',
63
    {
62
    value => {
64
        class => 'Koha::Patrons',
63
        branchcode => $library->{branchcode},
65
        value => {
66
            branchcode => $library->{branchcode},
67
        }
64
    }
68
    }
65
});
69
);
66
my $patron3 = $builder->build_object({
70
my $patron3 = $builder->build_object(
67
    class => 'Koha::Patrons',
71
    {
68
    value => {
72
        class => 'Koha::Patrons',
69
        branchcode => $another_library->{branchcode},
73
        value => {
70
        flags => undef,
74
            branchcode => $another_library->{branchcode},
75
            flags      => undef,
76
        }
71
    }
77
    }
72
});
78
);
73
79
74
# Discharge not possible with issues
80
# Discharge not possible with issues
75
my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
81
my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
76
my $barcode = 'BARCODE42';
82
my $barcode = 'BARCODE42';
77
$builder->build_sample_item(
83
$builder->build_sample_item(
78
    {
84
    {
Lines 84-98 $builder->build_sample_item( Link Here
84
);
90
);
85
91
86
AddIssue( $patron, $barcode );
92
AddIssue( $patron, $barcode );
87
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
93
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
88
is( $can, 0, 'A patron with issues cannot be discharged' );
94
is( $can,                   0,     'A patron with issues cannot be discharged' );
89
is( $problems->{checkouts}, 1, "Patron has checkouts" );
95
is( $problems->{checkouts}, 1,     "Patron has checkouts" );
90
is( $problems->{debt}, undef, "Patron has no debt" );
96
is( $problems->{debt},      undef, "Patron has no debt" );
91
97
92
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }), undef, 'No request done if patron has issues' );
98
is(
93
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->borrowernumber }), undef, 'No discharge done if patron has issues' );
99
    Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ), undef,
94
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
100
    'No request done if patron has issues'
95
is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
101
);
102
is(
103
    Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ), undef,
104
    'No discharge done if patron has issues'
105
);
106
is_deeply( [Koha::Patron::Discharge::get_pendings],  [], 'There is no pending discharge request' );
107
is_deeply( [Koha::Patron::Discharge::get_validated], [], 'There is no validated discharge' );
96
108
97
# Discharge not possible with issues and fines
109
# Discharge not possible with issues and fines
98
$patron->account->add_debit(
110
$patron->account->add_debit(
Lines 103-126 $patron->account->add_debit( Link Here
103
    }
115
    }
104
);
116
);
105
117
106
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
118
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
107
is(
119
is(
108
    $can, 0,
120
    $can, 0,
109
    'A patron with fines and checkouts cannot be discharged'
121
    'A patron with fines and checkouts cannot be discharged'
110
);
122
);
111
is( $problems->{checkouts}, 1, "Patron has checkouts" );
123
is( $problems->{checkouts}, 1,   "Patron has checkouts" );
112
is( $problems->{debt}, 0.1, "Patron has debt" );
124
is( $problems->{debt},      0.1, "Patron has debt" );
113
125
114
AddReturn( $barcode );
126
AddReturn($barcode);
115
127
116
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
128
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
117
is(
129
is(
118
    $can, 0,
130
    $can, 0,
119
    'A patron with fines cannot be discharged'
131
    'A patron with fines cannot be discharged'
120
);
132
);
121
is( $problems->{checkouts}, undef, "Patron has checkouts" );
133
is( $problems->{checkouts}, undef, "Patron has checkouts" );
122
is( $problems->{debt}, 0.1, "Patron has debt" );
134
is( $problems->{debt},      0.1,   "Patron has debt" );
123
124
135
125
$patron->account->pay(
136
$patron->account->pay(
126
    {
137
    {
Lines 129-161 $patron->account->pay( Link Here
129
);
140
);
130
141
131
# Discharge possible without issue or fine
142
# Discharge possible without issue or fine
132
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber });
143
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } );
133
is(
144
is(
134
    $can, 1,
145
    $can, 1,
135
    'A patron without issues or fines can be discharged'
146
    'A patron without issues or fines can be discharged'
136
);
147
);
137
is( scalar keys %$problems, 0, "No dishcharge problems found" );
148
is( scalar keys %$problems, 0, "No dishcharge problems found" );
138
149
139
is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
150
is( Koha::Patron::Discharge::generate_as_pdf, undef, "Confirm failure when lacking borrower number" );
140
151
141
# Verify that the user is not discharged anymore if the restriction has been lifted
152
# Verify that the user is not discharged anymore if the restriction has been lifted
142
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
153
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
143
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
154
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
144
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
155
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
145
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, 'The patron has been discharged' );
156
is(
146
is( Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
157
    Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1,
147
is( scalar( Koha::Patron::Discharge::get_validated ),             3,            'There are 3 validated discharges' );
158
    'The patron has been discharged'
148
is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, 'There is 1 validated discharge for a given patron' );
159
);
149
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
160
is(
161
    Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31',
162
    'The patron has been debarred after discharge'
163
);
164
is( scalar(Koha::Patron::Discharge::get_validated), 3, 'There are 3 validated discharges' );
165
is(
166
    scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1,
167
    'There is 1 validated discharge for a given patron'
168
);
169
is(
170
    scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2,
171
    'There is 2 validated discharges for a given branchcode'
172
);    # This is not used in the code yet
150
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
173
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
151
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
174
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
152
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 'The patron is not discharged after the restriction has been lifted' );
175
ok(
176
    !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ),
177
    'The patron is not discharged after the restriction has been lifted'
178
);
153
179
154
# Verify that the discharge works multiple times
180
# Verify that the discharge works multiple times
155
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
181
Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } );
156
is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
182
is( scalar(Koha::Patron::Discharge::get_pendings), 1, 'There is a pending discharge request (second time)' );
157
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
183
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
158
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
184
is_deeply( [Koha::Patron::Discharge::get_pendings], [], 'There is no pending discharge request (second time)' );
159
185
160
SKIP: {
186
SKIP: {
161
    skip "Skipping because weasyprint is not installed",
187
    skip "Skipping because weasyprint is not installed",
Lines 172-217 SKIP: { Link Here
172
    $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
198
    $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
173
199
174
    my $result;
200
    my $result;
175
    warning_is
201
    warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
176
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
202
    'Some error',
177
        'Some error',
178
        'Failed call to run() prints the generated error';
203
        'Failed call to run() prints the generated error';
179
204
180
    is( $result, undef, 'undef returned if failed run' );
205
    is( $result, undef, 'undef returned if failed run' );
181
206
182
    $mocked_ipc->mock( 'can_run', undef );
207
    $mocked_ipc->mock( 'can_run', undef );
183
208
184
    warning_is
209
    warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
185
        { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
210
    'weasyprint not found!',
186
        'weasyprint not found!',
187
        'Expected failure because of missing weasyprint';
211
        'Expected failure because of missing weasyprint';
188
212
189
    is( $result, undef, 'undef returned if missing weasyprint' );
213
    is( $result, undef, 'undef returned if missing weasyprint' );
190
}
214
}
191
215
192
# FIXME Should be a Koha::Object object
216
# FIXME Should be a Koha::Object object
193
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
217
is(
218
    ref( Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ) ),
219
    'Koha::Schema::Result::Discharge', 'Discharge request sent'
220
);
194
221
195
subtest 'search_limited' => sub {
222
subtest 'search_limited' => sub {
196
    plan tests => 4;
223
    plan tests => 4;
197
    $dbh->do(q|DELETE FROM discharges|);
224
    $dbh->do(q|DELETE FROM discharges|);
198
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
225
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
199
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
226
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
227
200
    # $patron and $patron2 are from the same library, $patron3 from another one
228
    # $patron and $patron2 are from the same library, $patron3 from another one
201
    # Logged in user is $patron, superlibrarian
229
    # Logged in user is $patron, superlibrarian
202
    t::lib::Mocks::mock_userenv({ patron => $patron });
230
    t::lib::Mocks::mock_userenv( { patron => $patron } );
203
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
231
    Koha::Library::Group->new( { parent_id => $group_1->id, branchcode => $patron->branchcode } )->store();
204
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron3->branchcode })->store();
232
    Koha::Library::Group->new( { parent_id => $group_2->id, branchcode => $patron3->branchcode } )->store();
205
    Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
233
    Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } );
206
    Koha::Patron::Discharge::request({ borrowernumber => $patron2->borrowernumber });
234
    Koha::Patron::Discharge::request( { borrowernumber => $patron2->borrowernumber } );
207
    Koha::Patron::Discharge::request({ borrowernumber => $patron3->borrowernumber });
235
    Koha::Patron::Discharge::request( { borrowernumber => $patron3->borrowernumber } );
208
    is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
236
    is( scalar(Koha::Patron::Discharge::get_pendings),      3, 'With permission, all discharges are visible' );
209
    is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
237
    is( Koha::Patron::Discharge::count( { pending => 1 } ), 3, 'With permission, all discharges are visible' );
210
238
211
    # With patron 3 logged in, only discharges from their group are visible
239
    # With patron 3 logged in, only discharges from their group are visible
212
    t::lib::Mocks::mock_userenv({ patron => $patron3 });
240
    t::lib::Mocks::mock_userenv( { patron => $patron3 } );
213
    is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
241
    is(
214
    is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
242
        scalar(Koha::Patron::Discharge::get_pendings), 1,
243
        'Without permission, only discharge from our group are visible'
244
    );
245
    is(
246
        Koha::Patron::Discharge::count( { pending => 1 } ), 1,
247
        'Without permission, only discharge from our group are visible'
248
    );
215
};
249
};
216
250
217
$schema->storage->txn_rollback;
251
$schema->storage->txn_rollback;
218
- 

Return to bug 14250