From 204e051dffae205c240dd3e99ed63aff4ad7e1ea Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Tue, 1 Sep 2009 12:29:39 -0400 Subject: [PATCH] [14/40] Work on batch management interface. --- C4/Branch.pm | 11 +- C4/Labels/Batch.pm | 103 ++++++++++-------- C4/Labels/Lib.pm | 46 +++++++- .../prog/en/css/staff-global.css | 2 +- .../prog/en/includes/labels-menu.inc | 5 +- .../prog/en/modules/labels/label-manage.tmpl | 15 ++- labels/label-manage.pl | 55 ++++++---- 7 files changed, 163 insertions(+), 74 deletions(-) diff --git a/C4/Branch.pm b/C4/Branch.pm index a6ab59cc80..18177ec651 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -45,7 +45,7 @@ BEGIN { &DelBranch &DelBranchCategory ); - @EXPORT_OK = qw( &onlymine &mybranch ); + @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name ); } =head1 NAME @@ -572,6 +572,15 @@ sub CheckBranchCategorycode { return $total; } +sub get_branch_code_from_name { + my @branch_name = @_; + my $query = "SELECT branchcode FROM branches WHERE branchname=?;"; + my $dbh = C4::Context->dbh(); + my $sth = $dbh->prepare($query); + $sth->execute(@branch_name); + return $sth->fetchrow_array; +} + 1; __END__ diff --git a/C4/Labels/Batch.pm b/C4/Labels/Batch.pm index d10dd18c99..0d72f3290d 100644 --- a/C4/Labels/Batch.pm +++ b/C4/Labels/Batch.pm @@ -24,9 +24,6 @@ use Sys::Syslog qw(syslog); use C4::Context; use C4::Debug; -use C4::Biblio; -use C4::Labels::Layout 1.000000; # use version 1.0.0 or better -use C4::Labels::Template 1.000000; use Data::Dumper; BEGIN { @@ -37,9 +34,10 @@ sub _check_params { my $given_params = {}; my $exit_code = 0; my @valid_template_params = ( - 'layout_id', - 'template_id', - 'profile_id', + 'label_id', + 'batch_id', + 'item_number', + 'branch_code', ); if (scalar(@_) >1) { $given_params = {@_}; @@ -84,10 +82,8 @@ sub new { my $type = ref($invocant) || $invocant; my $self = { batch_id => 0, - layout_id => $params{layout_id}, - template_id => $params{template_id}, - profile_id => $params{profile_id}, items => [], + branch_code => 'NB', batch_stat => 0, # False if any data has changed and the db has not been updated }; bless ($self, $type); @@ -106,8 +102,8 @@ sub new { sub add_item { my $self = shift; my $item_num = shift; - push (@{$self->{items}}, $item_num); - $self->{batch_stat} = 0; + push (@{$self->{'items'}}, $item_num); + $self->{'batch_stat'} = 0; } =head2 $batch->get_attr() @@ -137,9 +133,13 @@ sub delete_item { my $self = shift; my $item_num = shift; my $index = 0; - ++$index until $$self->{items}[$index] == $item_num or $item_num > $#$self->{items}; - delete ($$self->{items}[$index]); - $self->{batch_stat} = 0; + ++$index until $$self->{'items'}[$index] == $item_num or $index > $#$self->{'items'}; + if ($index > $#$self->{'items'}) { + syslog("LOG_ERR", "C4::Labels::Batch->delete_item : Item %s does not exist in batch %s.", $item_num, $self->{'batch_id'}); + return -1; + } + delete ($$self->{'items'}[$index]); + $self->{'batch_stat'} = 0; } =head2 $batch->save() @@ -156,36 +156,36 @@ sub delete_item { sub save { my $self = shift; - if ($self->{batch_id} > 0) { - foreach my $item_number (@$self->{items}) { - my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, template_id=?, profile_id=? WHERE batch_id=?;"; + if ($self->{'batch_id'} > 0) { + foreach my $item_number (@$self->{'items'}) { + my $query = "UPDATE labels_batches SET item_number=?, branch_code=? WHERE batch_id=?;"; warn "DEBUG: Updating: $query\n" if $debug; my $sth->C4::Context->dbh->prepare($query); - $sth->execute($item_number, $self->{layout_id}, $self->{template_id}, $self->{profile_id}, $self->{batch_id}); + $sth->execute($item_number, $self->{'branch_code'}, $self->{'batch_id'}); if ($sth->err) { - syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); + syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted UPDATE: %s", $sth->errstr); return -1; } } } else { - foreach my $item_number (@$self->{items}) { - my $query = "INSERT INTO labels_batches (item_number, layout_id, template_id, profile_id) VALUES (?,?,?,?);"; + my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;"); + $sth1->execute(); + my $batch_id = $sth1->fetchrow_array; + $self->{'batch_id'} = $batch_id++; + foreach my $item_number (@$self->{'items'}) { + my $query = "INSERT INTO labels_batches (batch_id, item_number, branch_code) VALUES (?,?,?);"; warn "DEBUG: Inserting: $query\n" if $debug; my $sth->C4::Context->dbh->prepare($query); - $sth->execute($item_number, $self->{layout_id}, $self->{template_id}, $self->{profile_id}); + $sth->execute($self->{'batch_id'}, $item_number, $self->{'branch_code'}); if ($sth->err) { - syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); + syslog("LOG_ERR", "C4::Labels::Batch->save : Database returned the following error on attempted INSERT: %s", $sth->errstr); return -1; } - my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;"); - $sth1->execute(); - my $batch_id = $sth1->fetchrow_array; - $self->{batch_id} = $batch_id; - return $batch_id; + return $self->{'batch_id'}; } } - $self->{batch_stat} = 1; + $self->{'batch_stat'} = 1; } =head2 C4::Labels::Template->retrieve(template_id) @@ -212,22 +212,20 @@ sub retrieve { my $type = ref($invocant) || $invocant; my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id"; my $sth = C4::Context->dbh->prepare($query); - $sth->execute($opts{batch_id}); + $sth->execute($opts{'batch_id'}); if ($sth->err) { - syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr); + syslog("LOG_ERR", "C4::Labels::Batch->retrieve : Database returned the following error on attempted SELECT: %s", $sth->errstr); return 1; } my $self = { items => [], }; while (my $record = $sth->fetchrow_hashref) { - $self->{batch_id} = $record->{batch_id}; # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way? - $self->{layout_id} = $record->{layout_id}; - $self->{template_id} = $record->{template_id}; - $self->{profile_id} = $record->{profile_id}; - push (@{$self->{items}}, $record->{item_number}); + $self->{'batch_id'} = $record->{'batch_id'}; # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way? + $self->{'branch_code'} = $record->{'branch_code'}; + push (@{$self->{'items'}}, $record->{'item_number'}); } - $self->{batch_stat} = 1; + $self->{'batch_stat'} = 1; bless ($self, $type); return $self; } @@ -244,19 +242,32 @@ sub retrieve { =cut sub delete { - my $self = shift; - my %opts = @_; - if ((ref $self) && !$self->{'batch_id'}) { # If there is no batch batch_id then we cannot delete it from the db - syslog("LOG_ERR", "Cannot delete batch: Batch has not been saved."); - return 1; + my $self = {}; + my %opts = (); + my $call_type = ''; + my $query_param = ''; + if (ref($_[0])) { + $self = shift; # check to see if this is a method call + $call_type = 'C4::Labels::Batch->delete'; + $query_param = $self->{'batch_id'}; } - elsif (!$opts{batch_id}) { - syslog("LOG_ERR", "Cannot delete batch: Missing batch_id."); - return 1; + else { + %opts = @_; + $call_type = 'C4::Labels::Batch::delete'; + $query_param = $opts{'batch_id'}; + } + if ($query_param eq '') { # If there is no template id then we cannot delete it + syslog("LOG_ERR", "%s : Cannot delete batch as the batch id is invalid or non-existant.", $call_type); + return -1; } my $query = "DELETE FROM labels_batches WHERE batch_id = ?"; my $sth = C4::Context->dbh->prepare($query); - $sth->execute($self->{'batch_id'}); +# $sth->{'TraceLevel'} = 3; + $sth->execute($query_param); + if ($sth->err) { + syslog("LOG_ERR", "%s : Database returned the following error on attempted INSERT: %s", $call_type, $sth->errstr); + return -1; + } return 0; } diff --git a/C4/Labels/Lib.pm b/C4/Labels/Lib.pm index cd09e86f7e..8ab8aa95a9 100644 --- a/C4/Labels/Lib.pm +++ b/C4/Labels/Lib.pm @@ -31,6 +31,7 @@ BEGIN { our @EXPORT_OK = qw(get_all_templates get_all_layouts get_all_profiles + get_batch_summary get_barcode_types get_label_types get_font_types @@ -175,13 +176,56 @@ sub get_all_profiles { syslog("LOG_ERR", "C4::Labels::Lib::get_all_profiles : Database returned the following error: %s", $sth->errstr); return -1; } - ADD_LAYOUTS: + ADD_PROFILES: while (my $profile = $sth->fetchrow_hashref) { push(@profiles, $profile); } return \@profiles; } +=head2 C4::Labels::Lib::get_batch_summary() + + This function returns an arrayref whose elements are hashes containing the batch_ids of current batches along with the item count + for each batch upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the syslog. + One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter. + + NOTE: Do not pass in the keyword 'WHERE.' + + examples: + + my $batches = get_batch_summary(); + my $batches = get_batch_summary(filter => filter_string); + +=cut + +sub get_batch_summary { + my %params = @_; + my @batches = (); + my $query = "SELECT DISTINCT batch_id FROM labels_batches"; + $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';'); + my $sth = C4::Context->dbh->prepare($query); +# $sth->{'TraceLevel'} = 3; + $sth->execute(); + if ($sth->err) { + syslog("LOG_ERR", "C4::Labels::Lib::get_batch_summary : Database returned the following error on attempted SELECT: %s", $sth->errstr); + return -1; + } + ADD_BATCHES: + while (my $batch = $sth->fetchrow_hashref) { + my $query = "SELECT count(item_number) FROM labels_batches WHERE batch_id=?;"; + my $sth1 = C4::Context->dbh->prepare($query); + $sth1->execute($batch->{'batch_id'}); + if ($sth1->err) { + syslog("LOG_ERR", "C4::Labels::Lib::get_batch_summary : Database returned the following error on attempted SELECT count: %s", $sth1->errstr); + return -1; + } + my $count = $sth1->fetchrow_arrayref; + $batch->{'_item_count'} = @$count[0]; + push(@batches, $batch); + } + return \@batches; +} + =head2 C4::Labels::Lib::get_barcode_types() This function returns a reference to an array of hashes containing all barcode types along with their name and description. diff --git a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css index 3d28ee97e7..25461dae0b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css +++ b/koha-tmpl/intranet-tmpl/prog/en/css/staff-global.css @@ -474,7 +474,7 @@ fieldset.brief ol, fieldset.brief li { list-style-type : none; } -fieldset.brief div.hint, fieldset.rows div.hint { +fieldset.brief div.hint, fieldset.rows div.hint, div.yui-u div.hint { color : #999999; font-size : 95%; margin-bottom : .4em; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/labels-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-menu.inc index ad2ab988dc..0eb16430c7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/labels-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/labels-menu.inc @@ -2,8 +2,7 @@
  • Labels Home
  • Manage Layouts
  • Manage Templates
  • -
  • Manage Printer Profiles
  • -
  • Manage Label Batches
  • - +
  • Manage Profiles
  • +
  • Manage Batches
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl index ece572e558..c86d274009 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/labels/label-manage.tmpl @@ -28,13 +28,22 @@ return; // no layout selected }; }; + function Print() { + var element_id = selected_layout(); + if (element_id>-1) { + window.location = "/cgi-bin/koha/labels/label-edit-.pl?op=edit&element_id=" + element_id; + } + else { + return; // no layout selected + }; + }; function selected_layout() { for (i=0;i."); return (-1); }; //]]> @@ -56,6 +65,7 @@
    +
    Current Branch:

    Currently Available

    @@ -83,12 +93,13 @@
    +
    - WARNING: An error was encountered and layout was not deleted. Please have your system administrator check the syslog for details. + WARNING: An error was encountered and was not deleted. Please have your system administrator check the syslog for details.
    diff --git a/labels/label-manage.pl b/labels/label-manage.pl index adabe5359b..3d155b02b1 100755 --- a/labels/label-manage.pl +++ b/labels/label-manage.pl @@ -23,6 +23,7 @@ use warnings; use vars qw($debug); use Sys::Syslog qw(syslog); +use Switch qw(Perl6); use CGI; use HTML::Template::Pro; use Data::Dumper; @@ -30,11 +31,13 @@ use Data::Dumper; use C4::Auth; use C4::Output; use C4::Context; +use autouse 'C4::Branch' => qw(get_branch_code_from_name); use C4::Debug; -use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_barcode_types get_label_types get_column_names get_table_names SELECT); +use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_all_profiles get_batch_summary get_barcode_types get_label_types get_column_names get_table_names SELECT); use C4::Labels::Layout 1.000000; use C4::Labels::Template 1.000000; use C4::Labels::Profile 1.000000; +use C4::Labels::Batch 1.000000; my $cgi = new CGI; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -50,9 +53,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( my $error = 0; my $db_rows = {}; -my $column_names = []; -my $table_names = []; - my $display_columns = { layout => [ #db column => display column {layout_id => 'Layout ID'}, {layout_name => 'Layout'}, @@ -72,16 +72,20 @@ my $display_columns = { layout => [ #db column => display column {_template_code => 'Template Name'}, # this display column does not have a corrisponding db column in the profile table, hence the underscore {select => 'Select'}, ], + batch => [ {batch_id => 'Batch ID'}, + {_item_count => 'Item Count'}, + {select => 'Select'}, + ], }; my $label_element = $cgi->param('label_element') || $ARGV[0]; my $op = $cgi->param('op') || $ARGV[1] || ''; my $element_id = $cgi->param('element_id') || $ARGV[2] || ''; +my $branch_code = ($label_element eq 'batch' ? get_branch_code_from_name($template->param('LoginBranchname')) : ''); sub _build_table { my $headers = shift; my $data = shift; - my $element = shift; my $table = []; my $fields = []; my @db_columns = (); @@ -107,15 +111,18 @@ sub _build_table { my $element_id = 0; POPULATE_ROW: foreach my $db_column (@db_columns) { - if ($db_column =~ m/^_((.*)_(.*$))/) { - my $table_name = get_table_names($2); - my $record_set = SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"}); - $db_row->{$db_column} = $$record_set[0]{$1}; - } if (grep {$db_column eq $_} keys %$db_row) { $element_id = $db_row->{$db_column} if $db_column =~ m/id/; $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $db_row->{$db_column}}; $col_index++; + next POPULATE_ROW; + } + elsif ($db_column =~ m/^_((.*)_(.*$))/) { # this a special case + my $table_name = get_table_names($2); + my $record_set = SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"}); + $$fields[$col_index] = {select_field => 0, field_name => ($db_column . "_tbl"), field_value => $$record_set[0]{$1}}; + $col_index++; + next POPULATE_ROW; } } $$fields[$col_index] = {select_field => 1, field_name => 'select', field_value => $element_id}; @@ -128,20 +135,27 @@ sub _build_table { } if ($op eq 'delete') { - $error = C4::Labels::Layout::delete(layout_id => $element_id) if $label_element eq 'layout'; - $error = C4::Labels::Template::delete(template_id => $element_id) if $label_element eq 'template'; - $error = C4::Labels::Profile::delete(profile_id => $element_id) if $label_element eq 'profile'; + given ($label_element) { + when 'layout' {$error = C4::Labels::Layout::delete(layout_id => $element_id); last;} + when 'template' {$error = C4::Labels::Template::delete(template_id => $element_id); last;} + when 'profile' {$error = C4::Labels::Profile::delete(profile_id => $element_id); last;} + when 'batch' {$error = C4::Labels::Batch::delete(batch_id => $element_id); last;} + default {} # FIXME: Some error trapping code + } } -$table_names = get_table_names($label_element); -$column_names = get_column_names(@$table_names[0]); -$db_rows = get_all_layouts() if $label_element eq 'layout'; -$db_rows = get_all_templates() if $label_element eq 'template'; -$db_rows = get_all_profiles() if $label_element eq 'profile'; +given ($label_element) { + when 'layout' {$db_rows = get_all_layouts();} + when 'template' {$db_rows = get_all_templates();} + when 'profile' {$db_rows = get_all_profiles();} + when 'batch' {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\'");} + default {} # FIXME: Some error trapping code +} -my $table = _build_table($display_columns->{$label_element}, $db_rows, $label_element); +my $table = _build_table($display_columns->{$label_element}, $db_rows); $template->param(error => $error) if ($error ne 0); +$template->param(print => 1) if ($label_element eq 'batch'); $template->param( op => $op, element_id => $element_id, @@ -149,7 +163,8 @@ $template->param( label_element => $label_element, label_element_title => ($label_element eq 'layout' ? 'Layouts' : $label_element eq 'template' ? 'Templates' : - $label_element eq 'profile' ? 'Profiles' : + $label_element eq 'profile' ? 'Profiles' : + $label_element eq 'batch' ? 'Batches' : '' ), ); -- 2.39.5