Bug 32401: Remove x-koha-query support
[koha.git] / members / update-child.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
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 updatechild.pl
21
22     script to update a child member to (usually) an adult member category
23
24     - if called with op=multi, will return all available non child categories, for selection.
25     - if called with op=update, script will update member record via Koha::Patron->store.
26
27 =cut
28
29 use Modern::Perl;
30 use CGI qw ( -utf8 );
31 use C4::Context;
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_html_with_http_headers output_and_exit_if_error output_and_exit );
34 use Koha::Patrons;
35 use Koha::Patron::Categories;
36 use Koha::Patrons;
37
38 my $dbh   = C4::Context->dbh;
39 my $input = CGI->new;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "members/update-child.tt",
44         query           => $input,
45         type            => "intranet",
46         flagsrequired   => { borrowers => 'edit_borrowers' },
47     }
48 );
49
50 my $borrowernumber = $input->param('borrowernumber');
51 my $catcode        = $input->param('catcode');
52 my $cattype        = $input->param('cattype');
53 my $op             = $input->param('op');
54
55 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
56
57 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({ category_type => 'A' }, {order_by => ['categorycode']});
58 if ( $op eq 'multi' ) {
59     # FIXME - what are the possible upgrade paths?  C -> A , C -> S ...
60     #   currently just allowing C -> A
61     $template->param(
62         MULTI             => 1,
63         borrowernumber    => $borrowernumber,
64         patron_categories => $patron_categories,
65     );
66     output_html_with_http_headers $input, $cookie, $template->output;
67 }
68 elsif ( $op eq 'update' ) {
69     my $patron         = Koha::Patrons->find( $borrowernumber );
70     output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
71
72     my $adult_category;
73     if ( $patron_categories->count == 1 ) {
74         $adult_category = $patron_categories->next;
75     } else {
76         $adult_category = $patron_categories->search({'me.categorycode' => $catcode })->next;
77     }
78
79     # Just in case someone is trying something bad
80     # But we should not hit that with a normal use of the interface
81     die "You are doing something wrong updating this child" unless $adult_category;
82
83     $_->delete() for $patron->guarantor_relationships->as_list;
84
85     $patron->categorycode($adult_category->categorycode);
86     $patron->store;
87
88     # FIXME We should not need that
89     # We could redirect with a friendly message
90     if ( $patron_categories->count > 1 ) {
91         $template->param(
92             SUCCESS        => 1,
93             borrowernumber => $borrowernumber,
94         );
95         output_html_with_http_headers $input, $cookie, $template->output;
96     }
97     else {
98         print $input->redirect(
99             "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber"
100         );
101     }
102 }
103