Bug 24603: Allow to cancel charges in patron accounting
[koha.git] / serials / subscription-numberpatterns.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011-2013 Biblibre SARL
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 NAME
21
22 subscription-numberpatterns.pl
23
24 =head1 DESCRIPTION
25
26 Manage numbering patterns
27
28 =cut
29
30 use Modern::Perl;
31 use CGI qw ( -utf8 );
32
33 use C4::Auth;
34 use C4::Output;
35 use C4::Serials::Numberpattern;
36 use C4::Serials::Frequency;
37
38 my $input = CGI->new;
39 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
40     template_name   => 'serials/subscription-numberpatterns.tt',
41     query           => $input,
42     type            => 'intranet',
43     flagsrequired   => { 'serials' => 1 }
44 } );
45
46 my $op = $input->param('op');
47
48 if($op && $op eq 'savenew') {
49     my $label = $input->param('label');
50     my $numberpattern;
51     foreach(qw/ label description numberingmethod displayorder
52       label1 label2 label3 add1 add2 add3 every1 every2 every3
53       setto1 setto2 setto3 whenmorethan1 whenmorethan2 whenmorethan3
54       numbering1 numbering2 numbering3 /) {
55         $numberpattern->{$_} = $input->param($_);
56         if($numberpattern->{$_} and $numberpattern->{$_} eq '') {
57             $numberpattern->{$_} = undef;
58         }
59     }
60     my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
61
62     if(!defined $numberpattern2) {
63         AddSubscriptionNumberpattern($numberpattern);
64     } else {
65         $op = 'new';
66         $template->param(error_existing_numberpattern => 1);
67         $template->param(%$numberpattern);
68     }
69 } elsif ($op && $op eq 'savemod') {
70     my $id = $input->param('id');
71     my $label = $input->param('label');
72     my $numberpattern = GetSubscriptionNumberpattern($id);
73     my $mod_ok = 1;
74     if($numberpattern->{'label'} ne $label) {
75         my $numberpattern2 = GetSubscriptionNumberpatternByName($label);
76         if(defined $numberpattern2 && $id != $numberpattern2->{'id'}) {
77             $mod_ok = 0;
78         }
79     }
80     if($mod_ok) {
81         foreach(qw/ id label description numberingmethod displayorder
82           label1 label2 label3 add1 add2 add3 every1 every2 every3
83           setto1 setto2 setto3 whenmorethan1 whenmorethan2 whenmorethan3
84           numbering1 numbering2 numbering3 /) {
85             $numberpattern->{$_} = $input->param($_) || undef;
86         }
87         ModSubscriptionNumberpattern($numberpattern);
88     } else {
89         $op = 'modify';
90         $template->param(error_existing_numberpattern => 1);
91     }
92 }
93
94 if($op && ($op eq 'new' || $op eq 'modify')) {
95     if($op eq 'modify') {
96         my $id = $input->param('id');
97         if(defined $id) {
98             my $numberpattern = GetSubscriptionNumberpattern($id);
99             $template->param(%$numberpattern);
100         } else {
101             $op = 'new';
102         }
103     }
104     my @frequencies = GetSubscriptionFrequencies();
105     my @subtypes;
106     push @subtypes, { value => $_ } for (qw/ issues weeks months /);
107
108     my $languages = [ map {
109         {
110             language => $_->{iso639_2_code},
111             description => $_->{language_description} || $_->{language}
112         }
113     } @{ C4::Languages::getAllLanguages() } ];
114
115     $template->param(
116         $op => 1,
117         frequencies_loop => \@frequencies,
118         subtypes_loop => \@subtypes,
119         locales => $languages,
120     );
121     output_html_with_http_headers $input, $cookie, $template->output;
122     exit;
123 }
124
125 if($op && $op eq 'del') {
126     my $id = $input->param('id');
127     if ($id) {
128         my $confirm = $input->param('confirm');
129         if ($confirm) {
130             DelSubscriptionNumberpattern($id);
131         } else {
132             my @subs = GetSubscriptionsWithNumberpattern($id);
133             if (@subs) {
134                 $template->param(
135                     id => $id,
136                     still_used => 1,
137                     subscriptions => \@subs
138                 );
139             } else {
140                 DelSubscriptionNumberpattern($id);
141             }
142         }
143     }
144 }
145
146 my @numberpatterns_loop = GetSubscriptionNumberpatterns();
147
148 $template->param(
149     numberpatterns_loop => \@numberpatterns_loop,
150 );
151
152 output_html_with_http_headers $input, $cookie, $template->output;