Bug 24665: Add support for filtering by cash register
[koha.git] / Koha / Template / Plugin / Registers.pm
blobcb5fab8b61b66f8ad9ddd49b2f2a22eaa9eedd65
1 package Koha::Template::Plugin::Registers;
3 # Copyright PTFS Europe 2020
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 =head1 NAME
22 Koha::Template::Plugin::Registers
24 =head1 DESCRIPTION
26 The Registers plugin is a helper that returns register related session information for templates
28 =head1 SYNOPSIS
30 [% USE Registers %]
31 [% SET registers = Registers.all() %]
32 [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
34 =cut
36 use Modern::Perl;
38 use Template::Plugin;
39 use base qw( Template::Plugin );
41 use C4::Koha;
42 use C4::Context;
43 use Koha::Cash::Registers;
45 =head1 FUNCTIONS
47 =head2 session_register_id
49 Return the register_id for the register attached to the current session.
51 =cut
53 sub session_register_id {
54 my ($self) = @_;
56 return C4::Context->userenv ?
57 C4::Context->userenv->{'register_id'} :
58 '';
61 =head2 session_register_name
63 Return the register_name for the register attached to the current session.
65 =cut
67 sub session_register_name {
68 my ($self) = @_;
70 return C4::Context->userenv
71 ? C4::Context->userenv->{'register_name'}
72 : '';
75 =head2 all
77 [% SET registers = Registers.all() %]
78 [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
80 Returns a list of all cash registers available that adhere to the passed filters.
82 =cut
84 sub all {
85 my ( $self, $params ) = @_;
87 return unless C4::Context->preference('UseCashRegisters');
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();
95 my $selected = $params->{selected};
96 for my $register ( @{$registers} ) {
97 if ( defined($selected) ) {
98 $register->{selected} = ( $register->{id} == $selected ) ? 1 : 0;
100 else {
101 $register->{selected} = ( defined( $self->session_register_id )
102 && $register->{id} eq $self->session_register_id ) ? 1 : 0;
106 return $registers;