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

(-)a/Koha/DataObject/Discharge.pm (+86 lines)
Line 0 Link Here
1
package Koha::DataObject::Discharge;
2
3
use Modern::Perl;
4
use List::MoreUtils qw( uniq );
5
6
use Koha::Database;
7
8
use C4::Members qw( GetMember );
9
10
use base qw( Class::Accessor );
11
__PACKAGE__->mk_accessors(qw( borrowernumber needed validated ));
12
13
my $rs = Koha::Database->new->schema->resultset('Discharge');
14
15
sub new {
16
    my ( $class, $discharge ) = @_;
17
18
    my $self = $class->SUPER::new(
19
        {
20
            borrowernumber => $discharge->{borrowernumber},
21
            needed         => $discharge->{needed},
22
            validated      => $discharge->{validated},
23
        }
24
    );
25
26
    bless $self, $class;
27
    return $self;
28
}
29
30
sub insert {
31
    my ($self) = @_;
32
    return $rs->create(
33
        {
34
            borrower  => $self->borrowernumber,
35
            needed    => $self->needed,
36
            validated => $self->validated,
37
        }
38
    );
39
}
40
41
sub update {
42
    my ($self) = @_;
43
    return $rs->update(
44
        {
45
            borrower  => $self->borrowernumber,
46
            needed    => $self->needed,
47
            validated => $self->validated,
48
        }
49
    );
50
}
51
52
sub fetch {
53
    my ($self) = @_;
54
    my $data = $rs->single( { borrower => $self->borrowernumber } );
55
    $self->needed( $data->needed );
56
    $self->validated( $data->validated );
57
    return $self;
58
}
59
60
sub search {
61
    my ($params) = @_;
62
63
    my $values = $params;
64
    my @joins;
65
    for my $v ( keys %$values ) {
66
        if ( $v eq 'branchcode' ) {
67
            push @joins, 'borrower';
68
        }
69
    }
70
71
    return $rs->search( $params, { join => [ uniq @joins ], } );
72
}
73
74
sub borrower {
75
    my ($self) = @_;
76
77
    return $self->{borrower}
78
      if exists $self->{borrower};
79
80
    $self->{borrower} =
81
      C4::Members::GetMember( borrowernumber => $self->borrowernumber );
82
83
    return $self->{borrower};
84
}
85
86
1;
(-)a/Koha/Service/Borrower/Discharge.pm (+169 lines)
Line 0 Link Here
1
package Koha::Service::Borrower::Discharge;
2
3
use Modern::Perl;
4
use CGI;
5
use File::Temp qw( :POSIX );
6
7
use C4::Members qw( GetPendingIssues );
8
use C4::Reserves qw( GetReservesFromBorrowernumber CancelReserve );
9
10
use Koha::Database;
11
use Koha::DataObject::Discharge;
12
use Koha::DateUtils qw( dt_from_string output_pref );
13
14
sub count {
15
    my ($params) = @_;
16
    my $values = {};
17
18
    if ( $params->{borrowernumber} ) {
19
        $values->{borrower} = $params->{borrowernumber};
20
    }
21
    if ( $params->{pending} ) {
22
        $values->{needed} = { '!=', undef };
23
        $values->{validated} = undef;
24
    }
25
    elsif ( $params->{validated} ) {
26
        $values->{validated} = { '!=', undef };
27
    }
28
29
    return Koha::DataObject::Discharge::search( { %$values } )->count;
30
}
31
32
sub can_be_discharged {
33
    my ($params) = @_;
34
35
    return unless $params->{borrowernumber};
36
37
    my $issues = GetPendingIssues( $params->{borrowernumber} );
38
39
    return 'has_issues', 0
40
      if @$issues;
41
42
    return 1;
43
}
44
45
sub request {
46
    my ($params) = @_;
47
48
    return
49
      unless $params->{borrowernumber}
50
          and
51
          can_be_discharged( { borrowernumber => $params->{borrowernumber} } );
52
53
    my $count = count($params);
54
55
    die
56
"This patron ($params->{borrowernumber}) has already asked for a discharge"
57
      if $count;
58
59
    my $discharge = Koha::DataObject::Discharge->new(
60
        {
61
            borrowernumber => $params->{borrowernumber},
62
            needed         => dt_from_string,
63
        }
64
    );
65
66
    return $discharge->insert;
67
}
68
69
sub discharge {
70
    my ($params) = @_;
71
72
    my $borrowernumber = $params->{borrowernumber};
73
    return
74
      unless $borrowernumber
75
          and can_be_discharged( { borrowernumber => $borrowernumber } );
76
77
    # Calcel reserves
78
    my @reserves = GetReservesFromBorrowernumber($borrowernumber);
79
    for my $reserve (@reserves) {
80
        CancelReserve( { reserve_id => $reserve->{reserve_id} } );
81
    }
82
83
    # Debar the member
84
    Koha::Borrower::Debarments::AddDebarment(
85
        {
86
            borrowernumber => $borrowernumber,
87
            type           => 'DISCHARGE'
88
        }
89
    );
90
91
    # Generate the discharge
92
    my $discharge = Koha::DataObject::Discharge->new(
93
        { borrowernumber => $borrowernumber, } );
94
95
    unless ( $discharge->fetch ) {
96
        $discharge->insert;
97
    }
98
99
    $discharge->validated(dt_from_string);
100
    return $discharge->update;
101
}
102
103
sub generate_as_pdf {
104
    my ($params) = @_;
105
106
    return unless $params->{borrowernumber};
107
108
    my $letter = C4::Letters::GetPreparedLetter(
109
        module      => 'members',
110
        letter_code => 'DISCHARGE',
111
        tables      => { borrowers => $params->{borrowernumber}, },
112
    );
113
114
    my $today = output_pref( dt_from_string() );
115
    $letter->{'title'}   =~ s/<<today>>/$today/g;
116
    $letter->{'content'} =~ s/<<today>>/$today/g;
117
118
    my $template =
119
      C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet', new CGI,
120
      );
121
122
    $template->param(
123
        stylesheet => C4::Context->preference("NoticeCSS"),
124
        today      => $today,
125
        messages   => [$letter],
126
    );
127
128
    my $html_path = tmpnam();
129
    open my $fh, '>', $html_path;
130
    print $fh $template->output;
131
    close $fh;
132
    my $pdf_path = tmpnam();
133
    qx{xhtml2pdf $html_path $pdf_path};
134
    return $pdf_path;
135
}
136
137
sub get_pendings {
138
    my ($params)       = @_;
139
    my $branchcode     = $params->{branchcode};
140
    my $borrowernumber = $params->{borrowernumber};
141
    my $rs             = Koha::DataObject::Discharge::search(
142
        {
143
            needed    => { '!=', undef },
144
            validated => undef,
145
            ( defined $branchcode ? ( branchcode => $branchcode ) : () ),
146
            (
147
                defined $borrowernumber
148
                ? ( borrowernumber => $borrowernumber )
149
                : ()
150
            ),
151
        }
152
    );
153
154
    my @discharges;
155
    while ( my $discharge = $rs->next ) {
156
        push @discharges,
157
          Koha::DataObject::Discharge->new(
158
            {
159
                borrowernumber => $discharge->borrower->borrowernumber,
160
                needed         => $discharge->needed,
161
                validated      => $discharge->validated,
162
            }
163
          );
164
    }
165
166
    return \@discharges;
167
}
168
169
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt (+41 lines)
Line 0 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>Koha &rsaquo; Patrons &rsaquo; [% IF (unknowuser) %]Patron does not exist[% ELSE %]Discharge for [% firstname %] [% surname %] ([% cardnumber %])[% END %]</title>
3
[% INCLUDE 'doc-head-close.inc' %]
4
</head>
5
<body>
6
[% INCLUDE 'header.inc' %]
7
[% INCLUDE 'patron-search.inc' %]
8
9
<div id="breadcrumbs">
10
         <a href="/cgi-bin/koha/mainpage.pl">Home</a>
11
&rsaquo; <a href="/cgi-bin/koha/members/members-home.pl">Patrons</a>
12
&rsaquo; [% IF (unknowuser) %]Patron does not exist[% ELSE %]Discharge for [% firstname %] [% surname %] ([% cardnumber %])[% END %]
13
</div>
14
15
<div id="doc3" class="yui-t1">
16
   <div id="bd">
17
    <div id="yui-main">
18
    <div class="yui-b">
19
<div class="yui-g">
20
<h3>Discharge</h3>
21
[% UNLESS can_be_discharged %]
22
    <p>Cannot edit discharge: borrower has issues.</p>
23
[% ELSE %]
24
    [% IF hasreserves %]
25
        <p>Borrower has reserves: they will be canceled if the discharge is generated.</p>
26
    [% END %]
27
    <form method="post">
28
        <input type="submit" value="Generate discharge" name="discharge" />
29
        <input type="hidden" value="[% borrowernumber %]" name="borrowernumber" />
30
    </form>
31
[% END %]
32
</div>
33
34
35
</div>
36
</div>
37
<div class="yui-b">
38
[% INCLUDE 'circ-menu.inc' %]
39
</div>
40
</div>
41
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharges.tt (+41 lines)
Line 0 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>Koha &rsaquo; Patrons &rsaquo; Pending discharge requests</title>
3
[% INCLUDE 'doc-head-close.inc' %]
4
</head>
5
<body id="pat_discharges" class="pat">
6
[% INCLUDE 'header.inc' %]
7
[% INCLUDE 'patron-search.inc' %]
8
9
<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/members/members-home.pl">Patrons</a> &rsaquo; Pending discharge requests</div>
10
11
<div id="doc2" class="yui-t7">
12
<div id="bd">
13
  <div id="yui-main">
14
    [% IF pending_discharges %]
15
      <h2>Pending discharge requests</h2>
16
      <div id="pending_updates">
17
        <table>
18
          <thead>
19
            <tr>
20
              <th>Patron</th>
21
              <th>Allow</th>
22
            </tr>
23
          </thead>
24
          <tbody>
25
            [% FOR d IN pending_discharges %]
26
              <tr>
27
                <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/discharges.pl?op=allow&borrowernumber=[% d.borrower.borrowernumber %]">Allow</a></td>
29
              </tr>
30
            [% END %]
31
          </tbody>
32
        </table>
33
      </div>
34
    [% ELSE %]
35
      <div class="dialog message">
36
        <p>There are no pending discharge requests.</p>
37
      </div>
38
    [% END %]
39
  </div>
40
</div>
41
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/opac-tmpl/prog/en/modules/opac-discharge.tt (+51 lines)
Line 0 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
2
[% IF (LibraryNameTitle) %][% LibraryNameTitle %][% ELSE %]Koha Online[% END %] Catalog
3
[% INCLUDE 'doc-head-close.inc' %]
4
</head>
5
<body id="opac-search-history">
6
[% IF OpacNav or loggedinusername %]
7
  <div id="doc3" class="yui-t1">
8
[% ELSE %]
9
  <div id="doc3" class="yui-t7">
10
[% END %]
11
<div id="bd">
12
[% INCLUDE 'masthead.inc' %]
13
14
<div id="yui-main">
15
  <div class="yui-b">
16
    <div class="yui-g">
17
      <div id="discharge" class="container">
18
        <h1>Discharge</h1>
19
        [% IF generated %]
20
          [% IF success %]
21
            <p>Your discharge request has been sent. Your discharge will be available on this page within a few days.</p>
22
          [% ELSE %]
23
            <p>Your discharge request could not be sent. Please retry later or contact an administrator.</p>
24
          [% END %]
25
        [% ELSIF available %]
26
          <a href="/cgi-bin/koha/opac-discharge.pl?op=get">Get your discharge</a></li>
27
        [% ELSIF pending %]
28
            <p>Your discharge will be available on this page within a few days.</p>
29
        [% ELSIF has_issues %]
30
            <p>You cannot be discharged, you have issues. Please return items before asking for a discharge.</p>
31
        [% ELSE %]
32
          <h2>What is a discharge?</h2>
33
          <p>This document certifies that you have returned all borrowed items. It is sometimes asked during a file transfer from a school to another. The discharge is sent by us to your school. You will also find it available on your reader account.</p>
34
          <p><strong>Warning</strong>: This request is only valid if you are in good standing with the library. Once the application is made, you can not borrow library materials.</p>
35
          <a href="/cgi-bin/koha/opac-discharge.pl?op=request">Ask for a discharge</a>
36
        [% END %]
37
      </div>
38
    </div>
39
  </div>
40
</div>
41
42
[% IF ( OpacNav || loggedinusername ) %]
43
  <div class="yui-b">
44
    <div class="container">
45
      [% INCLUDE 'navigation.inc' %]
46
      [% INCLUDE 'usermenu.inc' %]
47
    </div>
48
  </div>
49
[% END %]
50
</div>
51
[% INCLUDE 'opac-bottom.inc' %]
(-)a/members/discharge.pl (+123 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright 2013 BibLibre
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
=head1 NAME
21
22
discharges.pl
23
24
=head1 DESCRIPTION
25
26
Allows librarian to edit and/or manage borrowers' discharges
27
28
=cut
29
30
use Modern::Perl;
31
32
use CGI;
33
use C4::Auth;
34
use C4::Output;
35
use C4::Members;
36
use C4::Reserves;
37
use C4::Letters;
38
use Koha::Service::Borrower::Discharge;
39
40
use Koha::DateUtils;
41
42
my $input = new CGI;
43
my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
44
    {
45
        template_name   => 'members/discharge.tt',
46
        query           => $input,
47
        type            => 'intranet',
48
        authnotrequired => 0,
49
        flagsrequired   => { 'borrowers' => '*' },
50
    }
51
);
52
53
my $borrowernumber;
54
my $data;
55
if ( $input->param('borrowernumber') ) {
56
    $borrowernumber = $input->param('borrowernumber');
57
58
    # Getting member data
59
    $data = GetMember( borrowernumber => $borrowernumber );
60
61
    my $can_be_discharged =
62
      Koha::Service::Borrower::Discharge::can_be_discharged(
63
        { borrowernumber => $borrowernumber, } );
64
65
    # Getting reserves
66
    my @reserves    = GetReservesFromBorrowernumber($borrowernumber);
67
    my $hasreserves = scalar(@reserves);
68
69
    # Generating discharge if needed
70
    if ( $input->param('discharge') and $can_be_discharged ) {
71
        my $is_discharged = Koha::Service::Borrower::Discharge::count(
72
            {
73
                borrowernumber => $borrowernumber,
74
                validated      => 1,
75
            }
76
        );
77
        unless ($is_discharged) {
78
            Koha::Service::Borrower::Discharge::discharge(
79
                { borrowernumber => $borrowernumber, } );
80
        }
81
        my $pdf_path = Koha::Service::Borrower::Discharge::generate_as_pdf(
82
            { borrowernumber => $borrowernumber, } );
83
84
        print $input->header(
85
            -type       => 'application/pdf',
86
            -charset    => 'utf-8',
87
            -attachment => "discharge_$borrowernumber.pdf",
88
        );
89
        open my $fh, '<', $pdf_path;
90
        my @lines = <$fh>;
91
        close $fh;
92
        print @lines;
93
        exit;
94
    }
95
96
    $template->param(
97
        borrowernumber    => $borrowernumber,
98
        biblionumber      => $data->{'biblionumber'},
99
        title             => $data->{'title'},
100
        initials          => $data->{'initials'},
101
        surname           => $data->{'surname'},
102
        borrowernumber    => $borrowernumber,
103
        firstname         => $data->{'firstname'},
104
        cardnumber        => $data->{'cardnumber'},
105
        categorycode      => $data->{'categorycode'},
106
        category_type     => $data->{'category_type'},
107
        categoryname      => $data->{'description'},
108
        address           => $data->{'address'},
109
        address2          => $data->{'address2'},
110
        city              => $data->{'city'},
111
        zipcode           => $data->{'zipcode'},
112
        country           => $data->{'country'},
113
        phone             => $data->{'phone'},
114
        email             => $data->{'email'},
115
        branchcode        => $data->{'branchcode'},
116
        hasreserves       => $hasreserves,
117
        can_be_discharged => $can_be_discharged,
118
    );
119
}
120
121
$template->param( dischargeview => 1, );
122
123
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/members/discharges.pl (+59 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright 2013 BibLibre
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use CGI;
23
use C4::Auth;
24
use C4::Output;
25
use C4::Context;
26
use Koha::Service::Borrower::Discharge;
27
28
my $input = new CGI;
29
my $op = $input->param('op') // 'list';
30
31
my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
32
    {
33
        template_name   => "members/discharges.tt",
34
        query           => $input,
35
        type            => "intranet",
36
        authnotrequired => 0,
37
        flagsrequired   => { borrowers => 1 },
38
    }
39
);
40
41
my $branchcode =
42
  ( C4::Context->preference("IndependentBranches")
43
      and not C4::Context->IsSuperLibrarian() )
44
  ? C4::Context->userenv()->{'branch'}
45
  : undef;
46
47
if ( $op eq 'allow' ) {
48
    my $borrowernumber = $input->param('borrowernumber');
49
    Koha::Service::Borrower::Discharge::discharge(
50
        { borrowernumber => $borrowernumber } )
51
      if $borrowernumber;
52
}
53
54
my $pending_discharges = Koha::Service::Borrower::Discharge::get_pendings(
55
    { branchcode => $branchcode } );
56
57
$template->param( pending_discharges => $pending_discharges, );
58
59
output_html_with_http_headers $input, $cookie, $template->output;
(-)a/opac/opac-discharge.pl (-1 / +107 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright 2013 BibLibre
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use C4::Auth qw(:DEFAULT get_session);
23
use CGI;
24
use C4::Context;
25
use C4::Output;
26
use C4::Log;
27
use C4::Debug;
28
use C4::Branch;
29
use C4::Members;
30
use C4::Discharges;
31
use Koha::Service::Borrower::Discharge;
32
use Koha::DateUtils;
33
use Mail::Sendmail;
34
35
my $input = new CGI;
36
37
my $op = $input->param("op") // 'show';
38
39
# Getting the template and auth
40
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41
    {
42
        template_name => "opac-discharge.tt",
43
        query         => $input,
44
        type          => "opac",
45
        debug         => 1,
46
    }
47
);
48
49
# Sending an email to the librarian if user requested a discharge
50
if ( $op eq 'request' ) {
51
    my $can_be_discharged =
52
      Koha::Service::Borrower::Discharge::can_be_discharged(
53
        { borrowernumber => $loggedinuser } );
54
55
    if ($can_be_discharged) {
56
        eval {
57
            Koha::Service::Borrower::Discharge::request(
58
                { borrowernumber => $loggedinuser, } );
59
        };
60
        warn $@ if $@;
61
        my $success = $@ ? 0 : 1;
62
        $template->param(
63
            success   => $success,
64
            generated => 1,
65
        );
66
    }
67
    else {
68
        $template->param( has_issues => 1 );
69
    }
70
}
71
elsif ( $op eq 'get' ) {
72
    my $pdf_path = Koha::Service::Borrower::Discharge::generate_as_pdf(
73
        { borrowernumber => $loggedinuser, } );
74
75
    print $input->header(
76
        -type       => 'application/pdf',
77
        -charset    => 'utf-8',
78
        -attachment => "discharge_$loggedinuser.pdf",
79
    );
80
    open my $fh, '<', $pdf_path;
81
    my @lines = <$fh>;
82
    close $fh;
83
    print @lines;
84
    exit;
85
}
86
else {
87
    my $pending = Koha::Service::Borrower::Discharge::count(
88
        {
89
            borrowernumber => $loggedinuser,
90
            pending        => 1,
91
        }
92
    );
93
    my $available = Koha::Service::Borrower::Discharge::count(
94
        {
95
            borrowernumber => $loggedinuser,
96
            validated      => 1,
97
        }
98
    );
99
    $template->param(
100
        available => $available,
101
        pending   => $pending,
102
    );
103
}
104
105
$template->param( dischargeview => 1 );
106
107
output_html_with_http_headers $input, $cookie, $template->output;

Return to bug 8007