From cbcbc02e9b0bb0fce3022451b4812f36eabb9658 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 16 Feb 2012 14:32:03 +0100 Subject: [PATCH] [SIGNED-OFF] 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. Signed-off-by: Katrin Fischer Tested together with bug 5356. --- C4/Acquisition.pm | 146 +++++++++++++++----- C4/Output.pm | 4 +- acqui/basket.pl | 9 +- acqui/basketgroup.pl | 12 +- .../prog/en/modules/acqui/basketgroup.tt | 3 + .../prog/en/modules/acqui/csv/basket.tt | 3 + .../prog/en/modules/acqui/csv/basketgroup.tt | 3 + 7 files changed, 141 insertions(+), 39 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 52f5545..663d87c 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); @@ -514,8 +589,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 21002c0..8cfa351 100644 --- a/C4/Output.pm +++ b/C4/Output.pm @@ -42,13 +42,13 @@ BEGIN { @ISA = qw(Exporter); @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead - %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar + %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar &gettemplate &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)], ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)], html =>[qw(&output_with_http_headers &output_html_with_http_headers)] ); push @EXPORT, qw( - &themelanguage &gettemplate setlanguagecookie getlanguagecookie pagination_bar + &themelanguage &gettemplate setlanguagecookie getlanguagecookie pagination_bar &gettemplate ); push @EXPORT, qw( &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers FormatData FormatNumber 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 9ab47f3..df33b1b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt @@ -305,6 +305,9 @@ function yuiToolbar() {
+ +
+ [% 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.9.5