Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha.git] / admin / cash_registers.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2019 PTFS Europe
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 use strict;
21 use warnings;
22
23 use CGI;
24 use Try::Tiny;
25
26 use C4::Auth;
27 use Koha::Libraries;
28 use C4::Koha;
29 use C4::Context;
30 use C4::Output;
31 use Koha::Cash::Registers;
32
33 my $cgi = CGI->new();
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {
36         template_name   => 'admin/cash_registers.tt',
37         query           => $cgi,
38         type            => 'intranet',
39         authnotrequired => 0,
40         flagsrequired   => { parameters => 'manage_cash_registers' },
41     }
42 );
43
44 my $op         = $cgi->param('op') || 'list';
45 my $registerid = $cgi->param('id');             # update/archive
46 my $dbh        = C4::Context->dbh;
47 my @messages;
48 if ( $op eq 'add_form' ) {
49     if ($registerid) {
50         my $cash_register = Koha::Cash::Registers->find($registerid);
51         $template->param( cash_register => $cash_register );
52     }
53     my $libraries =
54       Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
55     $template->param(
56         branch_list => $libraries,
57         add_form    => 1
58     );
59 }
60 elsif ( $op eq 'add_validate' ) {
61     my $name = $cgi->param('name');
62     $name ||= q{};
63     my $description = $cgi->param('description');
64     $description ||= q{};
65     my $branch = $cgi->param('branch');
66     my $float  = $cgi->param('starting_float') // 0;
67     if ($registerid) {
68         try {
69             my $cash_register = Koha::Cash::Registers->find($registerid);
70             $cash_register->set(
71                 {
72                     name           => $name,
73                     description    => $description,
74                     branch         => $branch,
75                     starting_float => $float
76                 }
77             )->store;
78             push @messages, { code => 'success_on_update', type => 'message' };
79         }
80         catch {
81             push @messages, { code => 'error_on_update', type => 'alert' };
82         }
83     }
84     else {
85         try {
86             my $cash_register = Koha::Cash::Register->new(
87                 {
88                     name           => $name,
89                     description    => $description,
90                     branch         => $branch,
91                     starting_float => $float,
92                 }
93             )->store;
94             push @messages, { code => 'success_on_insert', type => 'message' };
95         }
96         catch {
97             push @messages, { code => 'error_on_insert', type => 'alert' };
98         }
99     }
100     $op = 'list';
101 }
102
103 elsif ( $op eq 'archive' ) {
104     if ($registerid) {
105         try {
106             my $cash_register = Koha::Cash::Registers->find($registerid);
107             $cash_register->archived(1)->store();
108             push @messages, { code => 'success_on_archive', type => 'message' };
109         }
110         catch {
111             push @messages, { code => 'error_on_archive', type => 'alert' };
112
113         }
114     }
115     $op = 'list';
116 }
117 elsif ( $op eq 'unarchive' ) {
118     if ($registerid) {
119         try {
120             my $cash_register = Koha::Cash::Registers->find($registerid);
121             $cash_register->archived(0)->store();
122             push @messages, { code => 'success_on_restore', type => 'message' };
123         }
124         catch {
125             push @messages, { code => 'error_on_restore', type => 'alert' };
126         }
127     }
128     $op = 'list';
129 }
130
131 elsif ( $op eq 'make_default' ) {
132     if ($registerid) {
133         try {
134             my $cash_register = Koha::Cash::Registers->find($registerid);
135             $cash_register->make_default;
136             push @messages, { code => 'success_on_default', type => 'message' };
137         }
138         catch {
139             push @messages, { code => 'error_on_default', type => 'alert' };
140         }
141     }
142     $op = 'list';
143 }
144 elsif ( $op eq 'drop_default' ) {
145     if ($registerid) {
146         try {
147             my $cash_register = Koha::Cash::Registers->find($registerid);
148             $cash_register->drop_default;
149             push @messages, { code => 'success_on_default', type => 'message' };
150         }
151         catch {
152             push @messages, { code => 'error_on_default', type => 'alert' };
153         }
154     }
155     $op = 'list';
156 }
157
158
159 if ( $op eq 'list' ) {
160     my $cash_registers =
161       Koha::Cash::Registers->search( {},
162         { prefetch => 'branch', order_by => { -asc => [qw/branch name/] } } );
163     $template->param( cash_registers => $cash_registers, );
164 }
165
166 $template->param( op => $op, messages => \@messages );
167
168 output_html_with_http_headers $cgi, $cookie, $template->output;