Bug 20434: Update UNIMARC framework - script
[koha.git] / labels / label-edit-batch.pl
blob83c74e9c25ca633b0a7ecf5c036d05a6a2ad49d1
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 Modern::Perl;
22 use vars qw($debug);
24 use CGI qw ( -utf8 );
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators;
29 use C4::Labels;
31 use Koha::Items;
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 $errtype = 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 {_item_cn => {label => 'Call number', link_field => 0}},
55 {_barcode => {label => 'Barcode', link_field => 0}},
56 {_delete => {label => 'Actions', link_field => 0}},
57 {select => {label => 'Select', value => '_label_id'}},
59 my $op = $cgi->param('op') || 'edit';
60 my @label_ids;
61 my @item_numbers;
62 my $number_list;
63 my $number_type = $cgi->param('number_type') || "barcode";
64 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || 0;
65 my $description = $cgi->param('description') || '';
66 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
67 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
68 $number_list = $cgi->param('number_list') if $cgi->param('number_list');
70 my $branch_code = C4::Context->userenv->{'branch'};
72 if ($op eq 'remove') {
73 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
74 foreach my $label_id (@label_ids) {
75 $err = $batch->remove_item($label_id);
77 $errtype = 'ITEM_NOT_REMOVED' if $err;
78 # Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
79 # print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
80 # exit;
82 elsif ($op eq 'delete') {
83 $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
84 $errtype = 'BATCH_NOT_DELETED' if $err;
86 elsif ($op eq 'add') {
87 if ($number_list) {
88 my @numbers_list = split /\n/, $number_list; # Entries are effectively passed in as a <cr> separated list
89 foreach my $number (@numbers_list) {
90 $number =~ s/\r$//; # strip any naughty return chars
91 if( $number_type eq "itemnumber" && Koha::Items->find($number) ) {
92 push @item_numbers, $number;
94 elsif ($number_type eq "barcode" ) { # we must test in case an invalid barcode is passed in; we effectively disgard them atm
95 my $item = Koha::Items->find({barcode => $number});
96 push @item_numbers, $item->itemnumber if $item;
100 if ($batch_id != 0) {$batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);}
101 if ($batch_id == 0 || $batch == -2) {$batch = C4::Labels::Batch->new(branch_code => $branch_code);}
102 $template->param( description => $batch->{description} );
103 if ($branch_code){
104 foreach my $item_number (@item_numbers) {
105 $err = $batch->add_item($item_number);
107 $batch_id = $batch->get_attr('batch_id') if $batch_id == 0; #update batch_id if we added to a new batch
108 $errtype = 'ITEM_NOT_ADDED' if $err;
110 else {
111 $err = 1;
112 $errtype = 'BRANCH_NOT_SET';
115 elsif ($op eq 'new') {
116 $batch = C4::Labels::Batch->new(branch_code => $branch_code);
117 $batch_id = $batch->get_attr('batch_id');
119 elsif ($op eq 'de_duplicate') {
120 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
121 $duplicate_count = $batch->remove_duplicates();
122 $duplicate_message = 1 if $duplicate_count != -1;
123 $errtype = 'BATCH_NOT_DEDUP' if $duplicate_count == -1;
125 else { # edit
126 $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
127 $template->param( description => $batch->{description} );
130 my $items = $batch->get_attr('items');
131 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
133 my $table = html_table($display_columns, $db_rows);
135 $template->param(
136 err => $err,
137 errtype => $errtype,
138 ) if ($err ne 0);
140 $template->param(
141 op => $op,
142 batch_id => $batch_id,
143 table_loop => $table,
144 duplicate_message => $duplicate_message,
145 duplicate_count => $duplicate_count,
148 output_html_with_http_headers $cgi, $cookie, $template->output;