Bug 7046: Preselect dropdown option and prefill input
[koha.git] / admin / cash_registers.pl
blob56e29a76635d1c0720f8709e5ed9dcd193b5589f
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 authnotrequired => 0,
40 flagsrequired => { parameters => 'manage_cash_registers' },
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 );
53 my $libraries =
54 Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
55 $template->param(
56 branch_list => $libraries,
57 add_form => 1
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(
72 name => $name,
73 description => $description,
74 branch => $branch,
75 starting_float => $float
77 )->store;
78 push @messages, { code => 'success_on_update', type => 'message' };
80 catch {
81 push @messages, { code => 'error_on_update', type => 'alert' };
84 else {
85 try {
86 my $cash_register = Koha::Cash::Register->new(
88 name => $name,
89 description => $description,
90 branch => $branch,
91 starting_float => $float,
93 )->store;
94 push @messages, { code => 'success_on_insert', type => 'message' };
96 catch {
97 push @messages, { code => 'error_on_insert', type => 'alert' };
100 $op = 'list';
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' };
110 catch {
111 push @messages, { code => 'error_on_archive', type => 'alert' };
115 $op = 'list';
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' };
124 catch {
125 push @messages, { code => 'error_on_restore', type => 'alert' };
128 $op = 'list';
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' };
138 catch {
139 push @messages, { code => 'error_on_default', type => 'alert' };
142 $op = 'list';
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' };
151 catch {
152 push @messages, { code => 'error_on_default', type => 'alert' };
155 $op = 'list';
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, );
166 $template->param( op => $op, messages => \@messages );
168 output_html_with_http_headers $cgi, $cookie, $template->output;