Bug 28036: Improve breadcrumbs of serial claims page
[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         flagsrequired   => { parameters => 'manage_cash_registers' },
40     }
41 );
42
43 my $op         = $cgi->param('op') || 'list';
44 my $registerid = $cgi->param('id');             # update/archive
45 my $dbh        = C4::Context->dbh;
46 my @messages;
47 if ( $op eq 'add_form' ) {
48     if ($registerid) {
49         my $cash_register = Koha::Cash::Registers->find($registerid);
50         $template->param( cash_register => $cash_register );
51     }
52     my $libraries =
53       Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
54     $template->param(
55         branch_list => $libraries,
56         add_form    => 1
57     );
58 }
59 elsif ( $op eq 'add_validate' ) {
60     my $name = $cgi->param('name');
61     $name ||= q{};
62     my $description = $cgi->param('description');
63     $description ||= q{};
64     my $branch = $cgi->param('branch');
65     my $float  = $cgi->param('starting_float') // 0;
66     if ($registerid) {
67         try {
68             my $cash_register = Koha::Cash::Registers->find($registerid);
69             $cash_register->set(
70                 {
71                     name           => $name,
72                     description    => $description,
73                     branch         => $branch,
74                     starting_float => $float
75                 }
76             )->store;
77             push @messages, { code => 'success_on_update', type => 'message' };
78         }
79         catch {
80             push @messages, { code => 'error_on_update', type => 'alert' };
81         }
82     }
83     else {
84         try {
85             my $cash_register = Koha::Cash::Register->new(
86                 {
87                     name           => $name,
88                     description    => $description,
89                     branch         => $branch,
90                     starting_float => $float,
91                 }
92             )->store;
93             push @messages, { code => 'success_on_insert', type => 'message' };
94         }
95         catch {
96             push @messages, { code => 'error_on_insert', type => 'alert' };
97         }
98     }
99     $op = 'list';
100 }
101
102 elsif ( $op eq 'archive' ) {
103     if ($registerid) {
104         try {
105             my $cash_register = Koha::Cash::Registers->find($registerid);
106             $cash_register->archived(1)->store();
107             push @messages, { code => 'success_on_archive', type => 'message' };
108         }
109         catch {
110             push @messages, { code => 'error_on_archive', type => 'alert' };
111
112         }
113     }
114     $op = 'list';
115 }
116 elsif ( $op eq 'unarchive' ) {
117     if ($registerid) {
118         try {
119             my $cash_register = Koha::Cash::Registers->find($registerid);
120             $cash_register->archived(0)->store();
121             push @messages, { code => 'success_on_restore', type => 'message' };
122         }
123         catch {
124             push @messages, { code => 'error_on_restore', type => 'alert' };
125         }
126     }
127     $op = 'list';
128 }
129
130 elsif ( $op eq 'make_default' ) {
131     if ($registerid) {
132         try {
133             my $cash_register = Koha::Cash::Registers->find($registerid);
134             $cash_register->make_default;
135             push @messages, { code => 'success_on_default', type => 'message' };
136         }
137         catch {
138             push @messages, { code => 'error_on_default', type => 'alert' };
139         }
140     }
141     $op = 'list';
142 }
143 elsif ( $op eq 'drop_default' ) {
144     if ($registerid) {
145         try {
146             my $cash_register = Koha::Cash::Registers->find($registerid);
147             $cash_register->drop_default;
148             push @messages, { code => 'success_on_default', type => 'message' };
149         }
150         catch {
151             push @messages, { code => 'error_on_default', type => 'alert' };
152         }
153     }
154     $op = 'list';
155 }
156
157
158 if ( $op eq 'list' ) {
159     my $cash_registers =
160       Koha::Cash::Registers->search( {},
161         { prefetch => 'branch', order_by => { -asc => [qw/branch name/] } } );
162     $template->param( cash_registers => $cash_registers, );
163 }
164
165 $template->param( op => $op, messages => \@messages );
166
167 output_html_with_http_headers $cgi, $cookie, $template->output;