Bugzilla – Attachment 8944 Details for
Bug 7302
CSV export of a basketgroup
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 7302 Export basketgroup as CSV
0001-Bug-7302-Export-basketgroup-as-CSV.patch (text/plain), 14.80 KB, created by
Jonathan Druart
on 2012-04-06 10:36:51 UTC
(
hide
)
Description:
Bug 7302 Export basketgroup as CSV
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2012-04-06 10:36:51 UTC
Size:
14.80 KB
patch
obsolete
>From f31987fb7572f305ce0e3d7480a485324f798f5c Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@biblibre.com> >Date: Thu, 16 Feb 2012 14:32:03 +0100 >Subject: [PATCH 1/1] Bug 7302: Export basketgroup as CSV > >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 | 146 +++++++++++++++----- > C4/Output.pm | 4 +- > acqui/basket.pl | 9 +- > acqui/basketgroup.pl | 12 ++- > .../prog/en/modules/acqui/basketgroup.tt | 5 +- > .../prog/en/modules/acqui/csv/basket.tt | 3 + > .../prog/en/modules/acqui/csv/basketgroup.tt | 3 + > 7 files changed, 142 insertions(+), 40 deletions(-) > create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt > create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basketgroup.tt > >diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm >index bebc3a8..3e20927 100644 >--- a/C4/Acquisition.pm >+++ b/C4/Acquisition.pm >@@ -29,6 +29,8 @@ use C4::Suggestions; > use C4::Biblio; > use C4::Debug; > use C4::SQLHelper qw(InsertInTable); >+use C4::Bookseller qw(GetBookSellerFromId); >+use C4::Templates qw(gettemplate); > > use Time::localtime; > use HTML::Entities; >@@ -42,7 +44,7 @@ BEGIN { > @ISA = qw(Exporter); > @EXPORT = qw( > &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket >- &GetBasketAsCSV >+ &GetBasketAsCSV &GetBasketGroupAsCSV > &GetBasketsByBookseller &GetBasketsByBasketgroup > &GetBasketsInfosByBookseller > >@@ -227,54 +229,127 @@ sub CloseBasket { > > Export a basket as CSV > >+$cgi parameter is needed for column name translation >+ > =cut > > sub GetBasketAsCSV { >- my ($basketno) = @_; >+ my ($basketno, $cgi) = @_; > my $basket = GetBasket($basketno); > my @orders = GetOrders($basketno); > my $contract = GetContract($basket->{'contractnumber'}); >- my $csv = Text::CSV->new(); >- my $output; > >- # TODO: Translate headers >- my @headers = qw(contractname ordernumber entrydate isbn author title publishercode collectiontitle notes quantity rrp); >- >- $csv->combine(@headers); >- $output = $csv->string() . "\n"; >+ my $template = C4::Templates::gettemplate("acqui/csv/basket.tmpl", "intranet", $cgi); > > my @rows; > foreach my $order (@orders) { >- my @cols; >- # newlines are not valid characters for Text::CSV combine() >- $order->{'notes'} =~ s/[\r\n]+//g; >- push(@cols, >- $contract->{'contractname'}, >- $order->{'ordernumber'}, >- $order->{'entrydate'}, >- $order->{'isbn'}, >- $order->{'author'}, >- $order->{'title'}, >- $order->{'publishercode'}, >- $order->{'collectiontitle'}, >- $order->{'notes'}, >- $order->{'quantity'}, >- $order->{'rrp'}, >- ); >- push (@rows, \@cols); >+ 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->{'notes'}, >+ quantity => $order->{'quantity'}, >+ rrp => $order->{'rrp'}, >+ deliveryplace => $basket->{'deliveryplace'}, >+ billingplace => $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; > } > >- foreach my $row (@rows) { >- $csv->combine(@$row); >- $output .= $csv->string() . "\n"; >+ @rows = sort { >+ if(defined $a->{publishercode} and defined $b->{publishercode}) { >+ $a->{publishercode} cmp $b->{publishercode}; >+ } >+ } @rows; > >- } >- >- return $output; >+ $template->param(rows => \@rows); > >+ return $template->output; > } > > >+=head3 GetBasketGroupAsCSV >+ >+=over 4 >+ >+&GetBasketGroupAsCSV($basketgroupid); >+ >+Export a basket group as CSV >+ >+$cgi parameter is needed for column name translation >+ >+=back >+ >+=cut >+ >+sub GetBasketGroupAsCSV { >+ my ($basketgroupid, $cgi) = @_; >+ my $baskets = GetBasketsByBasketgroup($basketgroupid); >+ >+ my $template = C4::Templates::gettemplate('acqui/csv/basketgroup.tmpl', 'intranet', $cgi); >+ >+ my @rows; >+ for my $basket (@$baskets) { >+ my @orders = GetOrders( $$basket{basketno} ); >+ my $contract = GetContract( $$basket{contractnumber} ); >+ my $bookseller = GetBookSellerFromId( $$basket{booksellerid} ); >+ >+ foreach my $order (@orders) { >+ my $bd = GetBiblioData( $order->{'biblionumber'} ); >+ my $row = { >+ clientnumber => $bookseller->{accountnumber}, >+ basketname => $basket->{basketname}, >+ ordernumber => $order->{ordernumber}, >+ author => $bd->{author}, >+ title => $bd->{title}, >+ publishercode => $bd->{publishercode}, >+ publicationyear => $bd->{publicationyear}, >+ collectiontitle => $bd->{collectiontitle}, >+ isbn => $order->{isbn}, >+ quantity => $order->{quantity}, >+ rrp => $order->{rrp}, >+ discount => $bookseller->{discount}, >+ ecost => $order->{ecost}, >+ notes => $order->{notes}, >+ entrydate => $order->{entrydate}, >+ booksellername => $bookseller->{name}, >+ bookselleraddress => $bookseller->{address1}, >+ booksellerpostal => $bookseller->{postal}, >+ contractnumber => $contract->{contractnumber}, >+ contractname => $contract->{contractname}, >+ }; >+ foreach(qw( >+ basketname author title publishercode collectiontitle notes >+ booksellername bookselleraddress booksellerpostal contractname >+ basketgroupdeliveryplace basketgroupbillingplace >+ basketdeliveryplace basketbillingplace >+ ) ) { >+ # Double the quotes to not be interpreted as a field end >+ $row->{$_} =~ s/"/""/g if $row->{$_}; >+ } >+ push @rows, $row; >+ } >+ } >+ $template->param(rows => \@rows); >+ >+ return $template->output; >+ >+} >+ > =head3 CloseBasketgroup > > &CloseBasketgroup($basketgroupno); >@@ -510,8 +585,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/C4/Output.pm b/C4/Output.pm >index 9087075..54d622d 100644 >--- a/C4/Output.pm >+++ b/C4/Output.pm >@@ -41,13 +41,13 @@ BEGIN { > require Exporter; > @ISA = qw(Exporter); > @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead >- %EXPORT_TAGS = ( all =>[qw(&pagination_bar >+ %EXPORT_TAGS = ( all =>[qw(&pagination_bar &gettemplate > &output_with_http_headers &output_html_with_http_headers)], > ajax =>[qw(&output_with_http_headers is_ajax)], > html =>[qw(&output_with_http_headers &output_html_with_http_headers)] > ); > push @EXPORT, qw( >- &output_html_with_http_headers &output_with_http_headers FormatData FormatNumber pagination_bar >+ &output_html_with_http_headers &output_with_http_headers FormatData FormatNumber pagination_bar &gettemplate > ); > } > >diff --git a/acqui/basket.pl b/acqui/basket.pl >index de66891..4e676d6 100755 >--- a/acqui/basket.pl >+++ b/acqui/basket.pl >@@ -146,7 +146,7 @@ if ( $op eq 'delete_confirm' ) { > -type => 'text/csv', > -attachment => 'basket' . $basket->{'basketno'} . '.csv', > ); >- print GetBasketAsCSV($query->param('basketno')); >+ print GetBasketAsCSV($query->param('basketno'), $query); > exit; > } elsif ($op eq 'close') { > my $confirm = $query->param('confirm') || $confirm_pref eq '2'; >@@ -156,8 +156,15 @@ if ( $op eq 'delete_confirm' ) { > $basketno =~ /^\d+$/ and CloseBasket($basketno); > # if requested, create basket group, close it and attach the basket > if ($query->param('createbasketgroup')) { >+ my $branchcode; >+ if(C4::Context->userenv and C4::Context->userenv->{'branch'} >+ and C4::Context->userenv->{'branch'} ne "NO_LIBRARY_SET") { >+ $branchcode = C4::Context->userenv->{'branch'}; >+ } > my $basketgroupid = NewBasketgroup( { name => $basket->{basketname}, > booksellerid => $booksellerid, >+ deliveryplace => $branchcode, >+ billingplace => $branchcode, > closed => 1, > }); > ModBasket( { basketno => $basketno, >diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl >index 3300cea..f873580 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, $input ); >+ 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 572d353..c2d60aa 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt >@@ -293,7 +293,7 @@ function yuiToolbar() { > [% IF ( basketgroup.closed ) %] > <tr> > <td> >- <a href="/cgi-bin/koha/acqui/basketgroup.pl?op=reopen&booksellerid=[% basketgroup.booksellerid %]&basketgroupid[% basketgroup.id %]">[% IF ( basketgroup.name ) %] >+ <a href="/cgi-bin/koha/acqui/basketgroup.pl?op=reopen&booksellerid=[% basketgroup.booksellerid %]&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 %] >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt >new file mode 100644 >index 0000000..2d5c2df >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt >@@ -0,0 +1,3 @@ >+Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher code,Collection title,Notes,Quantity,RRP,Delivery place,Billing place >+[% FOREACH r IN rows %]"[% r.contractname %]",[% r.ordernumber %],[% r.entrydate %],[% r.isbn %],"[% r.author %]","[% r.title %]",[% r.publicationyear %],"[% r.publishercode %]","[% r.collectiontitle %]","[% r.notes %]",[% r.quantity %],[% r.rrp %],"[% r.deliveryplace %]","[% r.billingplace %]" >+[% END %] >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basketgroup.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basketgroup.tt >new file mode 100644 >index 0000000..0d8208e >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basketgroup.tt >@@ -0,0 +1,3 @@ >+Client number,Basket name,Order number,Author,Title,Publisher code,Publication year,Collection title,ISBN,Quantity,RRP,Discount,Estimated cost,Notes,Entry date,Bookseller name,Bookseller physical address,Bookseller postal address,Contract number,Contract name,Basket group delivery place,Basket group billing place,Basket delivery place,Basket billing place >+[% FOREACH r IN rows %][% r.clientnumber %],"[% r.basketname %]",[% r.ordernumber %],"[% r.author %]","[% r.title %]","[% r.publishercode %]",[% r.publicationyear %],"[% r.collectiontitle %]",[% r.isbn %],[% r.quantity %],[% r.rrp %],[% r.discount %],[% r.ecost %],"[% r.notes %]",[% r.entrydate %],"[% r.booksellername %]","[% r.bookselleraddress %]","[% r.booksellerpostal %]",[% r.contractnumber %],"[% r.contractname %]","[% r.basketgroupdeliveryplace %]","[% r.basketgroupbillingplace %]","[% r.basketdeliveryplace %]","[% r.basketbillingplace %]" >+[% END %] >-- >1.7.7.3 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 7302
:
7687
|
8381
|
8400
|
8530
|
8944
|
9227
|
9974
|
10150
|
10336
|
10337