From 083bfba7fa11b550c3a82e50638afe27dc6dc45b 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.

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
---
 C4/Acquisition.pm | 115 +++++++++++++++++++++++++++++++++++++-----------------
 acqui/basket.pl   |  72 +---------------------------------
 2 files changed, 81 insertions(+), 106 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 6ad5ff7..aec8425 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -276,7 +276,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({
@@ -284,48 +284,93 @@ 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'},
-        };
-        for my $place ( qw( deliveryplace billingplace ) ) {
-            if ( my $library = Koha::Libraries->find( $row->{deliveryplace} ) ) {
-                $row->{$place} = $library->branchname
+    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;
         }
-        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->{$_};
+        my $content = join( $csv_profile->{csv_separator}, @headers ) . "\n";
+        for my $row ( @rows ) {
+            $csv->combine(@$row);
+            my $string = $csv->string;
+            $content .= $string . "\n";
         }
-        push @rows, $row;
+        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'},
+            };
+            for my $place ( qw( deliveryplace billingplace ) ) {
+                if ( my $library = Koha::Libraries->find( $row->{deliveryplace} ) ) {
+                    $row->{$place} = $library->branchname
+                }
+            }
+            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 eec3058..f5739e2 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -167,21 +167,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 'email') {
@@ -525,62 +511,6 @@ 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;
-}
-
 sub edi_close_and_order {
     my $confirm = $query->param('confirm') || $confirm_pref eq '2';
     if ($confirm) {
-- 
2.1.4