Merge branch 'bug_7018' into 3.12-master
[koha.git] / acqui / basketgroup.pl
index 5dbae17..6b7e5bf 100755 (executable)
@@ -53,14 +53,14 @@ 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/;
 
-my $input=new CGI;
+our $input=new CGI;
 
-my ($template, $loggedinuser, $cookie)
+our ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "acqui/basketgroup.tmpl",
                             query => $input,
                             type => "intranet",
@@ -187,7 +187,7 @@ sub printbasketgrouppdf{
     my $pdfformat = C4::Context->preference("OrderPdfFormat");
     if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages'){
        eval {
-           require $pdfformat;
+        eval "require $pdfformat";
            import $pdfformat;
        };
        if ($@){
@@ -251,10 +251,13 @@ sub printbasketgrouppdf{
                 push(@ba_orders, \@ba_order);
                 # Editor Number
                 my $en;
-                if (C4::Context->preference("marcflavour") eq 'UNIMARC') {
-                    $en = MARC::Record::new_from_xml($ord->{marcxml},'UTF-8')->subfield('345',"b");
-                } elsif (C4::Context->preference("marcflavour") eq 'MARC21') {
-                    $en = MARC::Record::new_from_xml($ord->{marcxml},'UTF-8')->subfield('037',"a");
+                my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )};
+                if ($marcrecord){
+                    if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
+                        $en = $marcrecord->subfield( '345', "b" );
+                    } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
+                        $en = $marcrecord->subfield( '037', "a" );
+                    }
                 }
                 if($en){
                     push(@ba_order, $en);
@@ -274,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);
 
@@ -325,30 +328,11 @@ if ( $op eq "add" ) {
         my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) );
         $billingplace  = $billingplace  || $borrower->{'branchcode'};
         $deliveryplace = $deliveryplace || $borrower->{'branchcode'};
-        
-        my $branches = GetBranches;
-        
-        # Build the combobox to select the billing place
-        my @billingplaceloop;
-        for (sort keys %$branches) {
-            push @billingplaceloop, {
-                value      => $_,
-                selected   => $_ eq $billingplace,
-                branchname => $branches->{$_}->{branchname},
-            };
-        }
-        $template->param( billingplaceloop => \@billingplaceloop );
-        
-        # Build the combobox to select the delivery place
-        my @deliveryplaceloop;
-        for (sort keys %$branches) {
-            push @deliveryplaceloop, {
-                value      => $_,
-                selected   => $_ eq $deliveryplace,
-                branchname => $branches->{$_}->{branchname},
-            };
-        }
-        $template->param( deliveryplaceloop => \@deliveryplaceloop );
+
+        my $branches = C4::Branch::GetBranchesLoop( $billingplace );
+        $template->param( billingplaceloop => $branches );
+        $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
+        $template->param( deliveryplaceloop => $branches );
 
         $template->param( booksellerid => $booksellerid );
     }
@@ -414,6 +398,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);