From 6ef4b82889850c02ffab0898d541fb516c6d0870 Mon Sep 17 00:00:00 2001 From: Jonathan Druart 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 | 7 +- .../prog/en/modules/acqui/csv/basket.tt | 3 + .../prog/en/modules/acqui/csv/basketgroup.tt | 3 + 7 files changed, 143 insertions(+), 41 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 4fd5cc2..d8f5ef0 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 4386125..e133799 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -145,7 +145,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'; @@ -155,8 +155,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 d2e1856..41b1f80 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 c651e00..5d4e0c3 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() { - + @@ -293,7 +293,7 @@ function yuiToolbar() { [% IF ( basketgroup.closed ) %] + [% 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
Basket GroupActionBasket GroupAction
- [% IF ( basketgroup.name ) %] + [% IF ( basketgroup.name ) %] [% basketgroup.name %] [% ELSE %] Basket group no. [% basketgroup.id %] @@ -305,6 +305,9 @@ function yuiToolbar() {
+
+