[21/40] Adding de-duplicating method and associated label edit batch 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 Sys::Syslog qw(syslog);
26 use Switch qw(Perl6);
27 use CGI;
28 use HTML::Template::Pro;
29 use Data::Dumper;
30 use JSON;
31
32 use C4::Auth;
33 use C4::Output;
34 use C4::Context;
35 use C4::Branch qw(get_branch_code_from_name);
36 use C4::Debug;
37 use C4::Labels::Lib 1.000000 qw(get_label_summary html_table);
38 use C4::Labels::Batch 1.000000;
39
40 my $cgi = new CGI;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "labels/label-edit-batch.tmpl",
44         query           => $cgi,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { catalogue => 1 },
48         debug           => 1,
49     }
50 );
51 my $err = 0;
52 my $errstr = undef;
53 my $duplicate_count = undef;
54 my $duplicate_message = undef;
55 my $db_rows = {};
56 my $batch = undef;
57 my $display_columns = [ {_label_number  => {label => 'Label Number', link_field => 0}},
58                         {_summary       => {label => 'Summary', link_field => 0}},
59                         {_item_type     => {label => 'Item Type', link_field => 0}},
60                         {_barcode       => {label => 'Barcode', link_field => 0}},
61                         {select         => {label => 'Select', value => '_label_id'}},
62                       ];
63 my $op = $cgi->param('op') || undef;
64 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
65 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
66 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
67 my $branch_code = get_branch_code_from_name($template->param('LoginBranchname'));
68
69 if ($op eq 'remove') {
70     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
71     foreach my $label_id (@label_ids) {
72     $err = $batch->remove_item($label_id);
73     }
74     $errstr = "item(s) not removed from batch $batch_id." if $err;
75 #    Something like this would be nice to avoid problems with the browser's 'refresh' button, but it needs an error handling mechanism...
76 #    print $cgi->redirect("label-edit-batch.pl?op=edit&batch_id=$batch_id");
77 #    exit;
78 }
79 elsif ($op eq 'delete') {
80     $err = C4::Labels::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
81     $errstr = "batch $batch_id was not deleted." if $err;
82 }
83 elsif ($op eq 'add') {
84     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
85     $batch = C4::Labels::Batch->new(branch_code => $branch_code) if $batch == -2;
86     foreach my $item_number (@item_numbers) {
87         $err = $batch->add_item($item_number);
88     }
89     $errstr = "item(s) not added to batch $batch_id." if $err;
90 }
91 elsif ($op eq 'new') {
92     $batch = C4::Labels::Batch->new(branch_code => $branch_code);
93     $batch_id = $batch->get_attr('batch_id');
94 }
95 elsif ($op eq 'de_duplicate') {
96     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
97     $duplicate_count = $batch->remove_duplicates();
98     $duplicate_message = 1 if $duplicate_count != -1;
99     $errstr = "batch $batch_id not fully de-duplicated." if $duplicate_count == -1;
100 }
101 else { # edit
102     $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
103 }
104
105 my $items = $batch->get_attr('items');
106 $db_rows = get_label_summary(items => $items, batch_id => $batch_id);
107
108 my $table = html_table($display_columns, $db_rows);
109
110 $template->param(
111                 duplicate_message       => $duplicate_message,
112                 duplicate_count         => $duplicate_count,
113                 );
114
115 $template->param(   
116                 err         => $err,
117                 errstr      => $errstr,
118                 ) if ($err ne 0);
119
120 $template->param(
121                 op              => $op,
122                 batch_id        => $batch_id,
123                 table_loop      => $table,
124                 );
125
126 output_html_with_http_headers $cgi, $cookie, $template->output;