[5/30] A rework of Label Creator code
[koha.git] / labels / label-edit-batch.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use warnings;
23 use vars qw($debug);
24
25 use CGI;
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Branch qw(get_branch_code_from_name);
30 use C4::Creators::Lib 1.000000 qw(get_label_summary html_table);
31 use C4::Labels::Batch 1.000000;
32
33 my $cgi = new CGI;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "labels/label-edit-batch.tmpl",
37         query           => $cgi,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => { catalogue => 1 },
41         debug           => 1,
42     }
43 );
44
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'}},
56                       ];
57 my $op = $cgi->param('op') || 'edit';
58 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
59 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
60 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
61
62 my $branch_code = get_branch_code_from_name($template->param('LoginBranchname'));
63
64 if ($op eq 'remove') {
65     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
66     foreach my $label_id (@label_ids) {
67     $err = $batch->remove_item($label_id);
68     }
69     $errstr = "item(s) not removed from batch $batch_id." if $err;
70 #    Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
71 #    print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
72 #    exit;
73 }
74 elsif ($op eq 'delete') {
75     $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
76     $errstr = "batch $batch_id was not deleted." if $err;
77 }
78 elsif ($op eq 'add') {
79     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
80     $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
81     if ($branch_code){
82         foreach my $item_number (@item_numbers) {
83             $err = $batch->add_item($item_number);
84         }
85         $errstr = "item(s) not added to batch $batch_id." if $err;
86     }
87     else {
88         $err = 1;
89         $errstr = "items(s) not added, the error was: Branch is not set, you please set your branch before adding items to a batch";
90     }
91 }
92 elsif ($op eq 'new') {
93     $batch = C4::Labels::Batch->new(branch_code => $branch_code);
94     $batch_id = $batch->get_attr('batch_id');
95 }
96 elsif ($op eq 'de_duplicate') {
97     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
98     $duplicate_count = $batch->remove_duplicates();
99     $duplicate_message = 1 if $duplicate_count != -1;
100     $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
101 }
102 else { # edit
103     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
104 }
105
106 my $items = $batch->get_attr('items');
107 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
108
109 my $table = html_table($display_columns, $db_rows);
110
111 $template->param(
112                 err         => $err,
113                 errstr      => $errstr,
114                 ) if ($err ne 0);
115
116 $template->param(
117                 op                      => $op,
118                 batch_id                => $batch_id,
119                 table_loop              => $table,
120                 duplicate_message       => $duplicate_message,
121                 duplicate_count         => $duplicate_count,
122                 );
123
124 output_html_with_http_headers $cgi, $cookie, $template->output;