@@ -, +, @@ example: biblionumber=biblio.biblionumber|auteur=biblio.author|titre=biblio.title|date=biblioitems.copyrightdate|editeur=biblioitems.publishercode|isbn=biblioitems.isbn|quantite=aqorders.quantity|prix=aqorders.rrp|panier=aqorders.basketno a) close the basket b) View the basket c) validate that there is an export button d) test it with an export --- C4/Acquisition.pm | 116 ++++++++++++++------- acqui/basket.pl | 9 +- installer/data/mysql/atomicupdate/bug_8612.sql | 1 + .../intranet-tmpl/prog/en/modules/acqui/basket.tt | 39 ++++++- .../prog/en/modules/tools/csv-profiles.tt | 36 ++++--- serials/claims.pl | 2 +- t/db_dependent/Acquisition/GetBasketAsCSV.t | 76 ++++++++++++++ t/db_dependent/Koha/CsvProfiles.t | 2 +- 8 files changed, 225 insertions(+), 56 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_8612.sql create mode 100644 t/db_dependent/Acquisition/GetBasketAsCSV.t --- a/C4/Acquisition.pm +++ a/C4/Acquisition.pm @@ -32,6 +32,7 @@ use Koha::Acquisition::Order; use Koha::Acquisition::Booksellers; use Koha::Number::Price; use Koha::Libraries; +use Koha::CsvProfiles; use C4::Koha; @@ -276,7 +277,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 +285,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 = Koha::CsvProfiles->find( $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; + } } --- a/acqui/basket.pl +++ a/acqui/basket.pl @@ -40,6 +40,7 @@ use C4::Letters qw/SendAlerts/; use Date::Calc qw/Add_Delta_Days/; use Koha::Database; use Koha::EDI qw( create_edi_order get_edifact_ean ); +use Koha::CsvProfiles; =head1 NAME @@ -168,7 +169,12 @@ if ( $op eq 'delete_confirm' ) { -type => 'text/csv', -attachment => 'basket' . $basket->{'basketno'} . '.csv', ); - print GetBasketAsCSV($query->param('basketno'), $query); + if ( $query->param('csv_profile') eq 'default'){ + print GetBasketAsCSV($query->param('basketno'), $query); + } else { + my $csv_profile_id = $query->param('csv_profile'); + print GetBasketAsCSV($query->param('basketno'), $query, $csv_profile_id); + } exit; } elsif ($op eq 'email') { my $err = eval { @@ -424,6 +430,7 @@ if ( $op eq 'list' ) { unclosable => @orders ? $basket->{is_standing} : 1, has_budgets => $has_budgets, duplinbatch => $duplinbatch, + csv_profiles => [ Koha::CsvProfiles->search({ type => 'export_basket' }) ], ); } --- a/installer/data/mysql/atomicupdate/bug_8612.sql +++ a/installer/data/mysql/atomicupdate/bug_8612.sql @@ -0,0 +1, @@ +UPDATE export_format SET type = 'late_issues' WHERE type ='sql'; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -1,3 +1,17 @@ +[% BLOCK csv_export %] +
+ Export as CSV + + +
+[% END %] [% USE KohaDates %] [% USE Branches %] [% USE Price %] @@ -121,6 +135,18 @@ e.preventDefault(); confirm_reopen(); }); + // Generates a dynamic link for exporting the selections data as CSV + $("#exportbutton, #export-csv-menu a").click(function() { + // Building the url from currently checked boxes + var url = '/cgi-bin/koha/acqui/basket.pl'; + url += $('#exportbutton').attr('href'); + if($(this).attr("data-value")) { + url += '&csv_profile=' + $(this).attr("data-value"); + } + // And redirecting to the CSV page + location.href = url; + return false; + }); }); function UserSearchPopup(f) { @@ -203,10 +229,13 @@ Close this basket [% END %] -
Export this basket as CSV
+ + [% PROCESS csv_export %] + [% IF ediaccount %]
Create EDIFACT order
[% END %] + [% IF ( active && books_loop ) %]
@@ -218,7 +247,8 @@ [% END %]
[% END %] - + +