Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
2.7 KiB

  1. package Koha::Template::Plugin::Registers;
  2. # Copyright PTFS Europe 2020
  3. # This file is part of Koha.
  4. #
  5. # Koha is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Koha is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  17. =head1 NAME
  18. Koha::Template::Plugin::Registers
  19. =head1 DESCRIPTION
  20. The Registers plugin is a helper that returns register related session information for templates
  21. =head1 SYNOPSIS
  22. [% USE Registers %]
  23. [% SET registers = Registers.all() %]
  24. [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
  25. =cut
  26. use Modern::Perl;
  27. use Template::Plugin;
  28. use base qw( Template::Plugin );
  29. use C4::Koha;
  30. use C4::Context;
  31. use Koha::Cash::Registers;
  32. =head1 FUNCTIONS
  33. =head2 session_register_id
  34. Return the register_id for the register attached to the current session.
  35. =cut
  36. sub session_register_id {
  37. my ($self) = @_;
  38. return C4::Context->userenv ?
  39. C4::Context->userenv->{'register_id'} :
  40. '';
  41. }
  42. =head2 session_register_name
  43. Return the register_name for the register attached to the current session.
  44. =cut
  45. sub session_register_name {
  46. my ($self) = @_;
  47. return C4::Context->userenv
  48. ? C4::Context->userenv->{'register_name'}
  49. : '';
  50. }
  51. =head2 all
  52. [% SET registers = Registers.all() %]
  53. [% SET registers = Registers.all( { filters => { current_branch => 1 } } );
  54. Returns a list of all cash registers available that adhere to the passed filters.
  55. =cut
  56. sub all {
  57. my ( $self, $params ) = @_;
  58. return unless C4::Context->preference('UseCashRegisters');
  59. my $filters = $params->{filters} // {};
  60. my $where = { archived => 0 };
  61. $where->{branch} = C4::Context->userenv->{'branch'}
  62. if ( $filters->{current_branch} && C4::Context->userenv );
  63. my $registers = Koha::Cash::Registers->search($where)->unblessed();
  64. my $selected = $params->{selected};
  65. for my $register ( @{$registers} ) {
  66. if ( defined($selected) ) {
  67. $register->{selected} = ( $register->{id} == $selected ) ? 1 : 0;
  68. }
  69. else {
  70. $register->{selected} = ( defined( $self->session_register_id )
  71. && $register->{id} eq $self->session_register_id ) ? 1 : 0;
  72. }
  73. }
  74. return $registers;
  75. }
  76. 1;