Koha/patroncards/edit-batch.pl
Nick Clemens a8a9c7b7fd Bug 14739: Add several patrons to a new batch will create a new batch per patron added
This patch changes the functionality of adding patrons to patroncard batches.  Instead of using only the patron search, there is now a textarea which can either be manually populated with borrowernumbers, or can be populated using the patron-search

To test:
1 - Create a new patron batch
2 - Click the add items button, if there is no data in the borrowrnumber form you should get a popup
3 - Add items to batch using the popup, borrowernumbers should populate to the form
4 - Click add items again and items should be added to a new batch
5 - Create a new batch
6 - Manually enter invalid borrowernumbers to the form
7 - Click add items, batch should return empty
8 - Manually enter valid borrowernumber
9 - Click add items, batch should be updated
10 - Edit an existing batch and confirm add items works as above
11 - Create a new label batch
12 - Click add items with no data, popup item search should appear
13 - Adding items by checking boxes or using 'Add' button - they should populate the text box on the edit batch page
14 - When adding using form the 'Enter by itemnumber' button should be marked automatically
15 - Submit items and confirm they are added to batch
16 - Create a new batch, enter by barcode manually and check 'Enter by itemnumber"
17 - Click add items, no items should be added
18 - Repopulate form with barcodes, check 'Enter by barcode'
19 - Click add items, items should be added correctly
20 - Test various combinations of radio buttons and numbers and using form or not

Signed-off-by: Barton Chittenden <barton@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2015-10-06 10:25:22 -03:00

150 lines
5.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
# Copyright 2006 Katipo Communications.
# Parts Copyright 2009 Foundations Bible College.
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
use vars qw($debug);
use CGI qw ( -utf8 );
use autouse 'Data::Dumper' => qw(Dumper);
use C4::Auth qw(get_template_and_user);
use C4::Output qw(output_html_with_http_headers);
use C4::Creators;
use C4::Patroncards;
use C4::Members qw(GetMember);
my $cgi = new CGI;
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "patroncards/edit-batch.tt",
query => $cgi,
type => "intranet",
authnotrequired => 0,
flagsrequired => { catalogue => 1 },
debug => 1,
}
);
my $err = 0;
my $duplicate_count = undef;
my $duplicate_message = undef;
my $db_rows = {};
my $batch = undef;
my $display_columns = [ {_card_number => {label => 'Card Number', link_field => 0}},
{_summary => {label => 'Summary', link_field => 0}},
{borrowernumber => {label => 'Borrower Number', link_field => 0}},
{select => {label => 'Select', value => '_label_id'}},
];
my $op = $cgi->param('op') || 'new';
my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
my $errstr = $cgi->param('error') || '';
my $bor_num_list = $cgi->param('bor_num_list') || undef;
my $branch_code = C4::Context->userenv->{'branch'};
if ($op eq 'remove') {
$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
foreach my $label_id (@label_ids) {
$err = $batch->remove_item($label_id);
}
if ($err) {
print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=403"); # this allows us to avoid problems with the user hitting their refresh button
exit;
}
}
elsif ($op eq 'delete') {
$err = C4::Creators::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
if ($err) {
print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=404");
exit;
}
}
elsif ($op eq 'add') {
if ($bor_num_list) {
my @bor_nums_unchecked = split /\n/, $bor_num_list; # $bor_num_list is effectively passed in as a <cr> separated list
foreach my $number (@bor_nums_unchecked) {
$number =~ s/\r$//; # strip any naughty return chars
if ( GetMember(borrowernumber => $number)) { # we must test in case an invalid borrowernumber is passed in; we effectively disgard them atm
my $borrower_number = $number;
push @borrower_numbers, $borrower_number;
}
}
}
if ($batch_id != 0) {$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);}
if ($batch_id == 0 || $batch == -2) {$batch = C4::Patroncards::Batch->new(branch_code => $branch_code);}
if ($branch_code){
foreach my $borrower_number (@borrower_numbers) {
$err = $batch->add_item($borrower_number);
}
$batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
if ($err) {
print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=401");
exit;
}
}
else {
print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=402");
exit;
}
}
elsif ($op eq 'de_duplicate') {
$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
$duplicate_count = $batch->remove_duplicates();
$duplicate_message = 1 if $duplicate_count != -1;
if ($duplicate_count == -1) {
print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=405");
exit;
}
}
elsif ($op eq 'edit') {
$batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
}
elsif ($op eq 'new') {
if ($branch_code eq '') {
warn sprintf('Batch edit interface called with an invalid/non-existent branch code: %s',$branch_code ? $branch_code : 'NULL');
print $cgi->redirect("manage.pl?card_element=batch&error=203");
exit;
}
$batch = C4::Patroncards::Batch->new(branch_code => $branch_code);
$batch_id = $batch->get_attr('batch_id');
}
else {
warn sprintf('Batch edit interface called an unsupported operation: %s',$op);
print $cgi->redirect("manage.pl?card_element=batch&error=202");
exit;
}
my $items = $batch->get_attr('items');
$db_rows = get_card_summary(items => $items, batch_id => $batch_id);
my $table = html_table($display_columns, $db_rows);
$template->param(
op => $op,
batch_id => $batch_id,
table_loop => $table,
duplicate_message => $duplicate_message,
duplicate_count => $duplicate_count,
error => $errstr,
);
output_html_with_http_headers $cgi, $cookie, $template->output;