From 1792d9fd5bb11a0c6a0d9d71041d9ab6922d399d Mon Sep 17 00:00:00 2001
From: genevieve <genevieve.plantin@inlibro.com>
Date: Mon, 14 Sep 2015 14:02:38 -0400
Subject: [PATCH] Bug 8612 - Clean basket.pl getting rid of subroutines with
sql query and use GetBasketAsCSV instead
GetBasketAsCSV now has a new argument the profile_csv_id, when this param is defined, it loads the profile_content, searches the value of the predefined fields and assembles the data to create the output of the csv file.
---
C4/Acquisition.pm | 113 +++++++++++++++++++++++++++++++++++++----------------
acqui/basket.pl | 72 +---------------------------------
2 files changed, 80 insertions(+), 105 deletions(-)
diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 0118b7c..0a0a71b 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -301,7 +301,7 @@ $cgi parameter is needed for column name translation
=cut
sub GetBasketAsCSV {
- my ($basketno, $cgi) = @_;
+ my ($basketno, $cgi, $csv_profile_id) = @_;
my $basket = GetBasket($basketno);
my @orders = GetOrders($basketno);
my $contract = GetContract({
@@ -309,45 +309,90 @@ sub GetBasketAsCSV {
});
my $template = C4::Templates::gettemplate("acqui/csv/basket.tt", "intranet", $cgi);
-
my @rows;
- foreach my $order (@orders) {
- my $bd = GetBiblioData( $order->{'biblionumber'} );
- my $row = {
- contractname => $contract->{'contractname'},
- ordernumber => $order->{'ordernumber'},
- entrydate => $order->{'entrydate'},
- isbn => $order->{'isbn'},
- author => $bd->{'author'},
- title => $bd->{'title'},
- publicationyear => $bd->{'publicationyear'},
- publishercode => $bd->{'publishercode'},
- collectiontitle => $bd->{'collectiontitle'},
- notes => $order->{'order_vendornote'},
- quantity => $order->{'quantity'},
- rrp => $order->{'rrp'},
- deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ),
- billingplace => C4::Branch::GetBranchName( $basket->{'billingplace'} ),
- };
- foreach(qw(
- contractname author title publishercode collectiontitle notes
- deliveryplace billingplace
- ) ) {
- # Double the quotes to not be interpreted as a field end
- $row->{$_} =~ s/"/""/g if $row->{$_};
+ if ($csv_profile_id) {
+ my $csv_profile = C4::Csv::GetCsvProfile( $csv_profile_id );
+ die "There is no valid csv profile given" unless $csv_profile;
+
+ my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->{csv_separator},'binary'=>1});
+ my $csv_profile_content = $csv_profile->{content};
+ my ( @headers, @fields );
+ while ( $csv_profile_content =~ /
+ ([^=]+) # header
+ =
+ ([^\|]+) # fieldname (table.row or row)
+ \|? /gxms
+ ) {
+ push @headers, $1;
+ my $field = $2;
+ $field =~ s/[^\.]*\.?//; # Remove the table name if exists.
+ push @fields, $field;
+ }
+ for my $order (@orders) {
+ my @row;
+ my $bd = GetBiblioData( $order->{'biblionumber'} );
+ my @biblioitems = GetBiblioItemByBiblioNumber( $order->{'biblionumber'});
+ for my $biblioitem (@biblioitems) {
+ if ($biblioitem->{isbn} eq $order->{isbn}) {
+ $order = {%$order, %$biblioitem};
+ }
+ }
+ if ($contract) {
+ $order = {%$order, %$contract};
+ }
+ $order = {%$order, %$basket, %$bd};
+ for my $field (@fields) {
+ push @row, $order->{$field};
+ }
+ push @rows, \@row;
}
- push @rows, $row;
+ my $content = join( $csv_profile->{csv_separator}, @headers ) . "\n";
+ for my $row ( @rows ) {
+ $csv->combine(@$row);
+ my $string = $csv->string;
+ $content .= $string . "\n";
+ }
+ return $content;
}
+ else {
+ foreach my $order (@orders) {
+ my $bd = GetBiblioData( $order->{'biblionumber'} );
+ my $row = {
+ contractname => $contract->{'contractname'},
+ ordernumber => $order->{'ordernumber'},
+ entrydate => $order->{'entrydate'},
+ isbn => $order->{'isbn'},
+ author => $bd->{'author'},
+ title => $bd->{'title'},
+ publicationyear => $bd->{'publicationyear'},
+ publishercode => $bd->{'publishercode'},
+ collectiontitle => $bd->{'collectiontitle'},
+ notes => $order->{'order_vendornote'},
+ quantity => $order->{'quantity'},
+ rrp => $order->{'rrp'},
+ deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ),
+ billingplace => C4::Branch::GetBranchName( $basket->{'billingplace'} ),
+ };
+ foreach(qw(
+ contractname author title publishercode collectiontitle notes
+ deliveryplace billingplace
+ ) ) {
+ # Double the quotes to not be interpreted as a field end
+ $row->{$_} =~ s/"/""/g if $row->{$_};
+ }
+ push @rows, $row;
+ }
- @rows = sort {
- if(defined $a->{publishercode} and defined $b->{publishercode}) {
- $a->{publishercode} cmp $b->{publishercode};
- }
- } @rows;
+ @rows = sort {
+ if(defined $a->{publishercode} and defined $b->{publishercode}) {
+ $a->{publishercode} cmp $b->{publishercode};
+ }
+ } @rows;
- $template->param(rows => \@rows);
+ $template->param(rows => \@rows);
- return $template->output;
+ return $template->output;
+ }
}
diff --git a/acqui/basket.pl b/acqui/basket.pl
index 5605cbf..48a709b 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -161,21 +161,7 @@ if ( $op eq 'delete_confirm' ) {
print GetBasketAsCSV($query->param('basketno'), $query);
} else {
my $csv_profile_id = $query->param('csv_profile');
- my $csv_profile = C4::Csv::GetCsvProfile( $csv_profile_id );
- die "There is no valid csv profile given" unless $csv_profile;
-
- my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->{csv_separator},'binary'=>1});
- my $csv_profile_content = $csv_profile->{content};
-
- my $basket_info = get_basket_csv_profile_info($csv_profile_content, $query->param('basketno'));
-
- print join( $csv_profile->{csv_separator}, @{$basket_info->{headers}} ) . "\n";
-
- for my $row ( @{$basket_info->{rows}} ) {
- $csv->combine(@$row);
- my $string = $csv->string;
- print $string, "\n";
- }
+ print GetBasketAsCSV($query->param('basketno'), $query, $csv_profile_id);
}
exit;
} elsif ($op eq 'close') {
@@ -492,60 +478,4 @@ sub get_order_infos {
return \%line;
}
-sub get_basket_DB_info{
- my ( $basketno ) = @_;
- return () unless $basketno;
- my $dbh = C4::Context->dbh;
- my $query ="
- SELECT biblio.*,biblioitems.*, aqbasket.*,aqcontract.*,
- aqorders.*,
- aqbudgets.*
- FROM aqorders
- LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
- LEFT JOIN aqcontract ON aqbasket.contractnumber = aqcontract.contractnumber
- LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
- LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
- LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
- WHERE aqorders.basketno=?
- AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
- ORDER BY biblioitems.publishercode,biblio.title
- ";
- my $result_set =
- $dbh->selectall_arrayref( $query, { Slice => {} }, $basketno );
- return @{$result_set};
-}
-
-sub get_basket_csv_profile_info{
- my ($csv_profile_content,$basketno) = @_;
-
- my ( @headers, @fields );
- while ( $csv_profile_content =~ /
- ([^=]+) # header
- =
- ([^\|]+) # fieldname (table.row or row)
- \|? /gxms
- ) {
- push @headers, $1;
- my $field = $2;
- $field =~ s/[^\.]*\.?//; # Remove the table name if exists.
- push @fields, $field;
- }
-
- my @rows;
- my @basket_info = get_basket_DB_info($basketno);
-
- for my $basket_info ( @basket_info ) {
- my @row;
- for my $field ( @fields ) {
- push @row, $basket_info->{$field};
- }
- push @rows, \@row;
- }
- my $hash_ref;
- $hash_ref->{ 'headers' } = \@headers;
- $hash_ref->{ 'rows' } = \@rows;
-
- return $hash_ref;
-}
-
output_html_with_http_headers $query, $cookie, $template->output;
--
1.7.9.5