- 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'},
- deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ),
- billingplace => C4::Branch::GetBranchName( $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->{$_};
+ 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
+ ) {
+ my $header = $1;
+ my $field = ($2 eq '') ? $1 : $2;
+
+ $header =~ s/^\s+|\s+$//g; # Trim whitespaces
+ push @headers, $header;
+
+ $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
+ $field =~ s/^\s+|\s+$//g; # Trim whitespaces
+ push @fields, $field;
+ }
+ for my $order (@orders) {
+ my @row;
+ my $biblio = Koha::Biblios->find( $order->{biblionumber} );
+ my $biblioitem = $biblio->biblioitem;
+ $order = { %$order, %{ $biblioitem->unblessed } };
+ if ($contract) {
+ $order = {%$order, %$contract};
+ }
+ $order = {%$order, %$basket, %{ $biblio->unblessed }};
+ for my $field (@fields) {
+ push @row, $order->{$field};
+ }
+ push @rows, \@row;
+ }
+ my $content = join( $csv_profile->csv_separator, @headers ) . "\n";
+ for my $row ( @rows ) {
+ $csv->combine(@$row);
+ my $string = $csv->string;
+ $content .= $string . "\n";