Bug 33406: (QA follow-up) Adjust tests and tidy
[koha.git] / Koha / Template / Plugin / Registers.pm
1 package Koha::Template::Plugin::Registers;
2
3 # Copyright PTFS Europe 2020
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 Koha::Template::Plugin::Registers
23
24 =head1 DESCRIPTION
25
26 The Registers plugin is a helper that returns register related session information for templates
27
28 =head1 SYNOPSIS
29
30     [% USE Registers %]
31     [% SET registers = Registers.all() %]
32     [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
33
34 =cut
35
36 use Modern::Perl;
37
38 use Template::Plugin;
39 use base qw( Template::Plugin );
40
41 use C4::Koha;
42 use C4::Context;
43 use Koha::Cash::Registers;
44
45 =head1 FUNCTIONS
46
47 =head2 session_register_id
48
49 Return the register_id for the register attached to the current session.
50
51 =cut
52
53 sub session_register_id {
54     my ($self) = @_;
55
56     return C4::Context->userenv ?
57         C4::Context->userenv->{'register_id'} :
58         '';
59 }
60
61 =head2 session_register_name
62
63 Return the register_name for the register attached to the current session.
64
65 =cut
66
67 sub session_register_name {
68     my ($self) = @_;
69
70     return C4::Context->userenv
71       ? C4::Context->userenv->{'register_name'}
72       : '';
73 }
74
75 =head2 all
76
77     [% SET registers = Registers.all() %]
78     [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
79
80 Returns a list of all cash registers available that adhere to the passed filters.
81
82 =cut
83
84 sub all {
85     my ( $self, $params ) = @_;
86
87     return unless C4::Context->preference('UseCashRegisters');
88
89     my $filters = $params->{filters} // {};
90     my $where = { archived => 0 };
91     $where->{branch} = C4::Context->userenv->{'branch'}
92       if ( $filters->{current_branch} && C4::Context->userenv );
93     my $registers = Koha::Cash::Registers->search($where)->unblessed();
94
95     my $selected = $params->{selected};
96     for my $register ( @{$registers} ) {
97         if ( defined($selected) ) {
98             $register->{selected} = ( $register->{id} == $selected ) ? 1 : 0;
99         }
100         else {
101             $register->{selected} = ( defined( $self->session_register_id )
102               && $register->{id} eq $self->session_register_id ) ? 1 : 0;
103         }
104     }
105
106     return $registers;
107 }
108
109 1;