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

(-)a/Koha/Patron/Discharge.pm (-7 / +19 lines)
Lines 30-36 sub count { Link Here
30
        $values->{validated} = { '!=', undef };
30
        $values->{validated} = { '!=', undef };
31
    }
31
    }
32
32
33
    return $rs->search( $values )->count;
33
    return search_limited( $values )->count;
34
}
34
}
35
35
36
sub can_be_discharged {
36
sub can_be_discharged {
Lines 52-60 sub is_discharged { Link Here
52
    my $borrowernumber = $params->{borrowernumber};
52
    my $borrowernumber = $params->{borrowernumber};
53
53
54
    my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
54
    my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
55
    my $validated = get_validated({borrowernumber => $borrowernumber});
55
    my @validated = get_validated({borrowernumber => $borrowernumber});
56
56
57
    if ($restricted && $validated) {
57
    if ($restricted && @validated) {
58
        return 1;
58
        return 1;
59
    } else {
59
    } else {
60
        return 0;
60
        return 0;
Lines 161-168 sub get_pendings { Link Here
161
        ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
161
        ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
162
    };
162
    };
163
163
164
    my @rs = $rs->search( $cond, { join => 'borrower' } );
164
    return search_limited( $cond );
165
    return \@rs;
166
}
165
}
167
166
168
sub get_validated {
167
sub get_validated {
Lines 176-184 sub get_validated { Link Here
176
        ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
175
        ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
177
    };
176
    };
178
177
179
    my @rs = $rs->search( $cond, { join => 'borrower' } );
178
    return search_limited( $cond );
180
    return \@rs;
181
}
179
}
182
180
181
# TODO This module should be based on Koha::Object[s]
182
sub search_limited {
183
    my ( $params, $attributes ) = @_;
184
    my $userenv = C4::Context->userenv;
185
    my @restricted_branchcodes;
186
    if ( $userenv ) {
187
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
188
        @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
189
    }
190
    $params->{'borrower.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
191
    $attributes->{join} = 'borrower';
192
193
    return $rs->search( $params, { join => 'borrower' } );
194
}
183
195
184
1;
196
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharges.tt (-1 / +1 lines)
Lines 23-29 Link Here
23
            </tr>
23
            </tr>
24
          </thead>
24
          </thead>
25
          <tbody>
25
          <tbody>
26
            [% FOR d IN pending_discharges %]
26
            [% FOREACH d IN pending_discharges %]
27
              <tr>
27
              <tr>
28
                <td><a href="/cgi-bin/koha/members/discharge.pl?borrowernumber=[% d.borrower.borrowernumber %]">[% d.borrower.surname %], [% d.borrower.firstname %]</a></td>
28
                <td><a href="/cgi-bin/koha/members/discharge.pl?borrowernumber=[% d.borrower.borrowernumber %]">[% d.borrower.surname %], [% d.borrower.firstname %]</a></td>
29
                <td><a href="/cgi-bin/koha/members/discharges.pl?op=allow&borrowernumber=[% d.borrower.borrowernumber %]">Allow</a></td>
29
                <td><a href="/cgi-bin/koha/members/discharges.pl?op=allow&borrowernumber=[% d.borrower.borrowernumber %]">Allow</a></td>
(-)a/members/discharge.pl (-2 / +2 lines)
Lines 102-108 if ( $input->param('discharge') and $can_be_discharged ) { Link Here
102
}
102
}
103
103
104
# Already generated discharges
104
# Already generated discharges
105
my $validated_discharges = Koha::Patron::Discharge::get_validated({
105
my @validated_discharges = Koha::Patron::Discharge::get_validated({
106
    borrowernumber => $borrowernumber,
106
    borrowernumber => $borrowernumber,
107
});
107
});
108
108
Lines 132-138 $template->param( Link Here
132
    branchcode        => $patron->branchcode,
132
    branchcode        => $patron->branchcode,
133
    has_reserves      => $has_reserves,
133
    has_reserves      => $has_reserves,
134
    can_be_discharged => $can_be_discharged,
134
    can_be_discharged => $can_be_discharged,
135
    validated_discharges => $validated_discharges,
135
    validated_discharges => \@validated_discharges,
136
);
136
);
137
137
138
$template->param( dischargeview => 1, );
138
$template->param( dischargeview => 1, );
(-)a/members/discharges.pl (-3 / +2 lines)
Lines 52-61 if( $op eq 'allow' ) { Link Here
52
    }) if $borrowernumber;
52
    }) if $borrowernumber;
53
}
53
}
54
54
55
my $pending_discharges = Koha::Patron::Discharge::get_pendings({
55
my @pending_discharges = Koha::Patron::Discharge::get_pendings({
56
    branchcode => $branchcode
56
    branchcode => $branchcode
57
});
57
});
58
58
$template->param( pending_discharges => \@pending_discharges );
59
$template->param( pending_discharges => $pending_discharges );
60
59
61
output_html_with_http_headers $input, $cookie, $template->output;
60
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/t/db_dependent/Patron/Borrower_Discharge.t (-10 / +47 lines)
Lines 15-21 Link Here
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
use Test::More tests => 18;
18
use Test::More tests => 19;
19
use Test::Warn;
19
use Test::Warn;
20
use MARC::Record;
20
use MARC::Record;
21
21
Lines 43-55 my $another_library = $builder->build({ source => 'Branch' }); Link Here
43
my $itemtype        = $builder->build({ source => 'Itemtype' })->{itemtype};
43
my $itemtype        = $builder->build({ source => 'Itemtype' })->{itemtype};
44
44
45
C4::Context->_new_userenv('xxx');
45
C4::Context->_new_userenv('xxx');
46
C4::Context->set_userenv(0, 0, 0, 'firstname', 'surname', $library->{branchcode}, $library->{branchcode}, '', '', '', '');
47
my $patron = $builder->build({
46
my $patron = $builder->build({
48
    source => 'Borrower',
47
    source => 'Borrower',
49
    value => {
48
    value => {
50
        branchcode => $library->{branchcode},
49
        branchcode => $library->{branchcode},
50
        flags      => 1, # superlibrarian
51
    }
51
    }
52
});
52
});
53
my $p = Koha::Patrons->find( $patron->{borrowernumber} );
54
set_logged_in_user( $p );
55
53
my $patron2 = $builder->build({
56
my $patron2 = $builder->build({
54
    source => 'Borrower',
57
    source => 'Borrower',
55
    value => {
58
    value => {
Lines 60-67 my $patron3 = $builder->build({ Link Here
60
    source => 'Borrower',
63
    source => 'Borrower',
61
    value => {
64
    value => {
62
        branchcode => $another_library->{branchcode},
65
        branchcode => $another_library->{branchcode},
66
        flags => undef,
63
    }
67
    }
64
});
68
});
69
my $p3 = Koha::Patrons->find( $patron3->{borrowernumber} );
65
70
66
# Discharge not possible with issues
71
# Discharge not possible with issues
67
my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
72
my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
Lines 80-87 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{bor Link Here
80
85
81
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }), undef, 'No request done if patron has issues' );
86
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }), undef, 'No request done if patron has issues' );
82
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
87
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
83
is_deeply( Koha::Patron::Discharge::get_pendings(), [], 'There is no pending discharge request' );
88
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
84
is_deeply( Koha::Patron::Discharge::get_validated(), [], 'There is no validated discharge' );
89
is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
85
90
86
AddReturn( $barcode );
91
AddReturn( $barcode );
87
92
Lines 96-113 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->{borrowernumbe Link Here
96
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
101
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
97
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
102
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
98
is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
103
is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
99
is( scalar( @{ Koha::Patron::Discharge::get_validated() } ),             3,            'There are 3 validated discharges' );
104
is( scalar( Koha::Patron::Discharge::get_validated ),             3,            'There are 3 validated discharges' );
100
is( scalar( @{ Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) } ), 1, 'There is 1 validated discharge for a given patron' );
105
is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) ), 1, 'There is 1 validated discharge for a given patron' );
101
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
106
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
102
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
107
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
103
ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
108
ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
104
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
109
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
105
110
106
# Verify that the discharge works multiple times
111
# Verify that the discharge works multiple times
107
Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
112
Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
108
is(scalar( @{ Koha::Patron::Discharge::get_pendings() }), 1, 'There is a pending discharge request (second time)');
113
is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
109
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
114
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
110
is_deeply( Koha::Patron::Discharge::get_pendings(), [], 'There is no pending discharge request (second time)');
115
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
111
116
112
# Check if PDF::FromHTML is installed.
117
# Check if PDF::FromHTML is installed.
113
my $check = eval { require PDF::FromHTML; };
118
my $check = eval { require PDF::FromHTML; };
Lines 126-131 else { Link Here
126
# FIXME Should be a Koha::Object object
131
# FIXME Should be a Koha::Object object
127
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
132
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
128
133
134
subtest 'search_limited' => sub {
135
    plan tests => 4;
136
    $dbh->do(q|DELETE FROM discharges|);
137
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
138
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
139
    # $patron and $patron2 are from the same library, $patron3 from another one
140
    # Logged in user is $patron, superlibrarian
141
    set_logged_in_user( $p );
142
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->{branchcode} })->store();
143
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron3->{branchcode} })->store();
144
    Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
145
    Koha::Patron::Discharge::request({ borrowernumber => $patron2->{borrowernumber} });
146
    Koha::Patron::Discharge::request({ borrowernumber => $patron3->{borrowernumber} });
147
    is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
148
    is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
149
150
    # With patron 3 logged in, only discharges from his group are visible
151
    set_logged_in_user( $p3 );
152
    is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
153
    is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
154
};
155
129
$schema->storage->txn_rollback;
156
$schema->storage->txn_rollback;
130
157
158
sub set_logged_in_user {
159
    my ($patron) = @_;
160
    C4::Context->set_userenv(
161
        $patron->borrowernumber, $patron->userid,
162
        $patron->cardnumber,     'firstname',
163
        'surname',               $patron->library->branchcode,
164
        'Midway Public Library', $patron->flags,
165
        '',                      ''
166
    );
167
}
168
131
1;
169
1;
132
- 

Return to bug 18403