Bug 25261: (follow-up) item.material may contain non-numerics
[koha.git] / admin / cash_registers.pl
blobae39c11986474bb26b42fc667879ffafd99e5cad
1 #!/usr/bin/perl
3 # Copyright 2019 PTFS Europe
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 use CGI;
24 use Try::Tiny;
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;
33 my $cgi = CGI->new();
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 template_name => 'admin/cash_registers.tt',
37 query => $cgi,
38 type => 'intranet',
39 flagsrequired => { parameters => 'manage_cash_registers' },
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 );
52 my $libraries =
53 Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
54 $template->param(
55 branch_list => $libraries,
56 add_form => 1
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(
71 name => $name,
72 description => $description,
73 branch => $branch,
74 starting_float => $float
76 )->store;
77 push @messages, { code => 'success_on_update', type => 'message' };
79 catch {
80 push @messages, { code => 'error_on_update', type => 'alert' };
83 else {
84 try {
85 my $cash_register = Koha::Cash::Register->new(
87 name => $name,
88 description => $description,
89 branch => $branch,
90 starting_float => $float,
92 )->store;
93 push @messages, { code => 'success_on_insert', type => 'message' };
95 catch {
96 push @messages, { code => 'error_on_insert', type => 'alert' };
99 $op = 'list';
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' };
109 catch {
110 push @messages, { code => 'error_on_archive', type => 'alert' };
114 $op = 'list';
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' };
123 catch {
124 push @messages, { code => 'error_on_restore', type => 'alert' };
127 $op = 'list';
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' };
137 catch {
138 push @messages, { code => 'error_on_default', type => 'alert' };
141 $op = 'list';
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' };
150 catch {
151 push @messages, { code => 'error_on_default', type => 'alert' };
154 $op = 'list';
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, );
165 $template->param( op => $op, messages => \@messages );
167 output_html_with_http_headers $cgi, $cookie, $template->output;