From f6a4a6bd33ac424260f8366b6fc1e97bf3cd20c3 Mon Sep 17 00:00:00 2001 From: Blou Date: Thu, 26 Mar 2015 16:07:44 -0400 Subject: [PATCH] Bug 8612 - System preference to have custom fields in export csv basket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch allows the user use a CSV export profile to create the fields to export the basket as CSV in a basket page. To test: 1) Apply the patch; 2) Create a CSV export profile - type sql - Home › Tools › CSV export profiles ex: 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 3) Create a new basket and add an order to the basket 4) Validate: the text "Select CSV profile:" and a dropdown in the basket view 5) In the new dropdown, select the CSV export profile created 6) Validate the CSV file. 7) Do the same validation with a closed basket. a) close the basket b) View the basket c) validate that there is a dropdown d) test it with an export Signed-off-by: Josef Moravec Works as expected for me Signed-off-by: Josef Moravec --- acqui/basket.pl | 79 +++++++++++++++++++++- .../intranet-tmpl/prog/en/modules/acqui/basket.tt | 44 +++++++++++- 2 files changed, 120 insertions(+), 3 deletions(-) diff --git a/acqui/basket.pl b/acqui/basket.pl index ca2d2d2..eec3058 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -33,6 +33,7 @@ use C4::Biblio; use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket use C4::Items; use C4::Suggestions; +use C4::Csv; use Koha::Libraries; use C4::Letters qw/SendAlerts/; use Date::Calc qw/Add_Delta_Days/; @@ -162,7 +163,26 @@ 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'); + my $csv_profile = C4::Csv::GetCsvProfile( $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 $basket_info = get_basket_csv_profile_info($csv_profile_content, $query->param('basketno')); + + print join( $csv_profile->{csv_separator}, @{$basket_info->{headers}} ) . "\n"; + + for my $row ( @{$basket_info->{rows}} ) { + $csv->combine(@$row); + my $string = $csv->string; + print $string, "\n"; + } + } exit; } elsif ($op eq 'email') { my $err = eval { @@ -418,6 +438,7 @@ if ( $op eq 'list' ) { unclosable => @orders ? $basket->{is_standing} : 1, has_budgets => $has_budgets, duplinbatch => $duplinbatch, + csv_profiles => C4::Csv::GetCsvProfiles( "sql" ), ); } @@ -504,6 +525,62 @@ sub get_order_infos { return \%line; } +sub get_basket_DB_info{ + my ( $basketno ) = @_; + return () unless $basketno; + my $dbh = C4::Context->dbh; + my $query =" + SELECT biblio.*,biblioitems.*, aqbasket.*,aqcontract.*, + aqorders.*, + aqbudgets.* + FROM aqorders + LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno + LEFT JOIN aqcontract ON aqbasket.contractnumber = aqcontract.contractnumber + LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id + LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber + LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber + WHERE aqorders.basketno=? + AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00') + ORDER BY biblioitems.publishercode,biblio.title + "; + my $result_set = + $dbh->selectall_arrayref( $query, { Slice => {} }, $basketno ); + return @{$result_set}; +} + +sub get_basket_csv_profile_info{ + my ($csv_profile_content,$basketno) = @_; + + 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; + } + + my @rows; + my @basket_info = get_basket_DB_info($basketno); + + for my $basket_info ( @basket_info ) { + my @row; + for my $field ( @fields ) { + push @row, $basket_info->{$field}; + } + push @rows, \@row; + } + my $hash_ref; + $hash_ref->{ 'headers' } = \@headers; + $hash_ref->{ 'rows' } = \@rows; + + return $hash_ref; +} + sub edi_close_and_order { my $confirm = $query->param('confirm') || $confirm_pref eq '2'; if ($confirm) { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt index 77ec72e..9b97424 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -121,6 +121,16 @@ e.preventDefault(); confirm_reopen(); }); + // Generates a dynamic link for exporting the selections data as CSV + $("#exportbutton").click(function() { + // Building the url from currently checked boxes + var url = '/cgi-bin/koha/acqui/basket.pl'; + url += $('#exportbutton').attr('href'); + url += '&csv_profile=' + $("#csv_profile_for_export option:selected").val(); + // And redirecting to the CSV page + location.href = url; + return false; + }); }); function UserSearchPopup(f) { @@ -197,10 +207,25 @@ Close this basket [% END %] - +
+
+ + +
+
+ + [% IF ediaccount %] [% END %] + [% IF ( active && books_loop ) %]
@@ -211,6 +236,7 @@
[% END %] +