[Koha-patches] [PATCH 1/1] Bug 7302: Export basketgroup as CSV

Jonathan Druart jonathan.druart at biblibre.com
Thu Feb 16 14:41:04 CET 2012


Adds new action export for basketgroup.
This action is available only if your basketgroup is closed.
This export generates a csv file with order informations.
---
 C4/Acquisition.pm                                  |   68 ++++++++++++++++++-
 acqui/basketgroup.pl                               |   12 +++-
 .../prog/en/modules/acqui/basketgroup.tt           |    7 ++-
 3 files changed, 79 insertions(+), 8 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 0a96a02..8821c04 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -29,9 +29,12 @@ use C4::Suggestions;
 use C4::Biblio;
 use C4::Debug;
 use C4::SQLHelper qw(InsertInTable);
+use C4::Bookseller qw(GetBookSellerFromId);
 
 use Time::localtime;
 use HTML::Entities;
+use Text::CSV::Encoded;
+use utf8;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -42,7 +45,7 @@ BEGIN {
     @ISA    = qw(Exporter);
     @EXPORT = qw(
         &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket
-	&GetBasketAsCSV
+        &GetBasketAsCSV &GetBasketGroupAsCSV
         &GetBasketsByBookseller &GetBasketsByBasketgroup
 
         &ModBasketHeader
@@ -233,7 +236,7 @@ sub GetBasketAsCSV {
     my $basket = GetBasket($basketno);
     my @orders = GetOrders($basketno);
     my $contract = GetContract($basket->{'contractnumber'});
-    my $csv = Text::CSV->new();
+    my $csv = Text::CSV::Encoded->new ({ encoding  => "utf8" });
     my $output; 
 
     # TODO: Translate headers
@@ -274,6 +277,60 @@ sub GetBasketAsCSV {
 }
 
 
+=head3 GetBasketGroupAsCSV
+
+=over 4
+
+&GetBasketGroupAsCSV($basketgroupid);
+
+Export a basket group as CSV
+
+=back
+
+=cut
+
+sub GetBasketGroupAsCSV {
+    my ($basketgroupid) = @_;
+    my $baskets = GetBasketsByBasketgroup($basketgroupid);
+
+    # TODO: Translate headers
+    my @headers = qw(booksellername bookselleraddress booksellerpostal accountnumber contractnumber contractname ordernumber entrydate isbn author title publishercode collectiontitle notes quantity rrp);
+
+    my $csv = Text::CSV::Encoded->new ({ encoding  => "utf8" });
+
+    my $output;
+    $csv->combine(@headers);
+    $output = $csv->string() . "\n";
+
+    for my $basket (@$baskets) {
+        my @orders     = GetOrders( $$basket{basketno} );
+        my $contract   = GetContract( $$basket{contractnumber} );
+        my $bookseller = GetBookSellerFromId( $$basket{booksellerid} );
+
+        my @rows;
+        foreach my $order (@orders) {
+            my @cols;
+            my $bd = GetBiblioData( $order->{'biblionumber'} );
+            push( @cols,
+                $bookseller->{name}, $bookseller->{address1}, $bookseller->{postal}, $bookseller->{accountnumber},
+                $contract->{contractnumber}, $contract->{contractname},
+                $order->{ordernumber},  $order->{entrydate}, $order->{isbn},
+                $bd->{author}, $bd->{title}, $bd->{publishercode}, $bd->{collectiontitle},
+                $order->{notes}, $order->{quantity}, $order->{rrp}, );
+            push( @rows, \@cols );
+        }
+
+        foreach my $row (@rows) {
+            $csv->combine(@$row);
+            $output .= $csv->string() . "\n";
+
+        }
+    }
+
+    return $output;
+
+}
+
 =head3 CloseBasketgroup
 
   &CloseBasketgroup($basketgroupno);
@@ -476,8 +533,11 @@ Returns a reference to all baskets that belong to basketgroup $basketgroupid.
 
 sub GetBasketsByBasketgroup {
     my $basketgroupid = shift;
-    my $query = "SELECT * FROM aqbasket
-                LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?";
+    my $query = qq{
+        SELECT *, aqbasket.booksellerid as booksellerid
+        FROM aqbasket
+        LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?
+    };
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare($query);
     $sth->execute($basketgroupid);
diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl
index d2e1856..babfd49 100755
--- a/acqui/basketgroup.pl
+++ b/acqui/basketgroup.pl
@@ -53,7 +53,7 @@ use C4::Output;
 use CGI;
 
 use C4::Bookseller qw/GetBookSellerFromId/;
-use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket/;
+use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket GetBasketGroupAsCSV/;
 use C4::Bookseller qw/GetBookSellerFromId/;
 use C4::Branch qw/GetBranches/;
 use C4::Members qw/GetMember/;
@@ -277,7 +277,7 @@ sub printbasketgrouppdf{
 
 }
 
-my $op = $input->param('op');
+my $op = $input->param('op') || 'display';
 my $booksellerid = $input->param('booksellerid');
 $template->param(booksellerid => $booksellerid);
 
@@ -417,6 +417,14 @@ if ( $op eq "add" ) {
     
     printbasketgrouppdf($basketgroupid);
     exit;
+}elsif ( $op eq "export" ) {
+    my $basketgroupid = $input->param('basketgroupid');
+    print $input->header(
+        -type       => 'text/csv',
+        -attachment => 'basketgroup' . $basketgroupid . '.csv',
+    );
+    print GetBasketGroupAsCSV( $basketgroupid );
+    exit;
 }elsif( $op eq "delete"){
     my $basketgroupid = $input->param('basketgroupid');
     DelBasketgroup($basketgroupid);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
index 2c29865..84ffc3b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
@@ -285,7 +285,7 @@ function yuiToolbar() {
 		<table>
 			<thead>
 				<tr>
-					<th>Basket Group</th><th colspan="3">Action</th>
+					<th>Basket Group</th><th colspan="4">Action</th>
 				</tr>
 			</thead>
 			<tbody>
@@ -293,7 +293,7 @@ function yuiToolbar() {
 				[% IF ( basketgroup.closed ) %]
 				<tr>
 				<td>
-					<a href="/cgi-bin/koha/acqui/basketgroup.pl?op=reopen&amp;booksellerid=[% basketgroup.booksellerid %]&amp;basketgroupid[% basketgroup.id %]">[% IF ( basketgroup.name ) %]
+					<a href="/cgi-bin/koha/acqui/basketgroup.pl?op=reopen&amp;booksellerid=[% basketgroup.booksellerid %]&amp;basketgroupid=[% basketgroup.id %]">[% IF ( basketgroup.name ) %]
 											[% basketgroup.name %]
 										[% ELSE %]
 											Basket group no. [% basketgroup.id %]
@@ -305,6 +305,9 @@ function yuiToolbar() {
 						<td>
 							<form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="print" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Print" /></form>
 						</td>
+                    <td>
+                        <form action="/cgi-bin/koha/acqui/basketgroup.pl" method="get"><input type="hidden" name="op" value="export" /><input type="hidden" name="basketgroupid" value="[% basketgroup.id %]" /><input type="submit" value="Export as CSV" /></form>
+                    </td>
 				</tr>
 				[% END %]
 				[% END %]
-- 
1.7.7.3



More information about the Koha-patches mailing list