+=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;
+
+}
+