Bug 10963: Simplified creation - AR framework
[koha.git] / labels / label-edit-batch.pl
blob76bfa796830199a5ec92a7a7ca8df458dc599dd1
1 #!/usr/bin/perl
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
23 use vars qw($debug);
25 use CGI qw ( -utf8 );
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Items qw(GetItemnumberFromBarcode);
30 use C4::Creators;
31 use C4::Labels;
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 template_name => "labels/label-edit-batch.tt",
37 query => $cgi,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => { catalogue => 1 },
41 debug => 1,
45 my $err = 0;
46 my $errstr = undef;
47 my $duplicate_count = undef;
48 my $duplicate_message = undef;
49 my $db_rows = {};
50 my $batch = undef;
51 my $display_columns = [ {_label_number => {label => 'Label Number', link_field => 0}},
52 {_summary => {label => 'Summary', link_field => 0}},
53 {_item_type => {label => 'Item Type', link_field => 0}},
54 {_barcode => {label => 'Barcode', link_field => 0}},
55 {select => {label => 'Select', value => '_label_id'}},
57 my $op = $cgi->param('op') || 'edit';
58 my @label_ids;
59 my @item_numbers;
60 my $barcode;
61 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
62 @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
63 @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
64 $barcode = $cgi->param('barcode') if $cgi->param('barcode');
66 my $branch_code = C4::Context->userenv->{'branch'};
68 if ($op eq 'remove') {
69 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
70 foreach my $label_id (@label_ids) {
71 $err = $batch->remove_item($label_id);
73 $errstr = "item(s) not removed from batch $batch_id." if $err;
74 # Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
75 # print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
76 # exit;
78 elsif ($op eq 'delete') {
79 $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
80 $errstr = "batch $batch_id was not deleted." if $err;
82 elsif ($op eq 'add') {
83 if ($barcode) {
84 my @barcodes = split /\n/, $barcode; # $barcode is effectively passed in as a <cr> separated list
85 foreach my $number (@barcodes) {
86 $number =~ s/\r$//; # strip any naughty return chars
87 if (my $item_number = GetItemnumberFromBarcode($number)) { # we must test in case an invalid barcode is passed in; we effectively disgard them atm
88 push @item_numbers, $item_number;
92 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
93 $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
94 if ($branch_code){
95 foreach my $item_number (@item_numbers) {
96 $err = $batch->add_item($item_number);
98 $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
99 $errstr = "item(s) not added to batch $batch_id." if $err;
101 else {
102 $err = 1;
103 $errstr = "items(s) not added, the error was: Branch is not set, you please set your branch before adding items to a batch";
106 elsif ($op eq 'new') {
107 $batch = C4::Labels::Batch->new(branch_code => $branch_code);
108 $batch_id = $batch->get_attr('batch_id');
110 elsif ($op eq 'de_duplicate') {
111 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
112 $duplicate_count = $batch->remove_duplicates();
113 $duplicate_message = 1 if $duplicate_count != -1;
114 $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
116 else { # edit
117 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
120 my $items = $batch->get_attr('items');
121 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
123 my $table = html_table($display_columns, $db_rows);
125 $template->param(
126 err => $err,
127 errstr => $errstr,
128 ) if ($err ne 0);
130 $template->param(
131 op => $op,
132 batch_id => $batch_id,
133 table_loop => $table,
134 duplicate_message => $duplicate_message,
135 duplicate_count => $duplicate_count,
138 output_html_with_http_headers $cgi, $cookie, $template->output;