Bug 5549 : Sip scripts should handle dt due dates
[koha.git] / acqui / basketgroup.pl
index 457b616..d2e1856 100755 (executable)
@@ -45,6 +45,7 @@ The bookseller who we want to display the baskets (and basketgroups) of.
 
 use strict;
 use warnings;
+use Carp;
 
 use C4::Input;
 use C4::Auth;
@@ -184,9 +185,21 @@ sub printbasketgrouppdf{
     my ($basketgroupid) = @_;
     
     my $pdfformat = C4::Context->preference("OrderPdfFormat");
-    eval "use $pdfformat";
-    # FIXME consider what would happen if $pdfformat does not
-    # contain the name of a valid Perl module.
+    if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages'){
+       eval {
+        eval "require $pdfformat";
+           import $pdfformat;
+       };
+       if ($@){
+       }
+    }
+    else {
+       print $input->header;  
+       print $input->start_html;  # FIXME Should do a nicer page
+       print "<h1>Invalid PDF Format set</h1>";
+       print "Please go to the systempreferences and set a valid pdfformat";
+       exit;
+    }
     
     my $basketgroup = GetBasketgroup($basketgroupid);
     my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
@@ -202,8 +215,20 @@ sub printbasketgrouppdf{
             #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate
             my @ba_order;
             if ( $ord->{biblionumber} && $ord->{quantity}> 0 ) {
-                eval "use C4::Biblio";
-                eval "use C4::Koha";
+                eval {
+                   require C4::Biblio;
+                   import C4::Biblio;
+               };
+               if ($@){
+                   croak $@;
+               }
+                eval {
+                   require C4::Koha;
+                   import C4::Koha;
+               };
+               if ($@){
+                   croak $@;
+               }
                 my $bib = GetBiblioData($ord->{biblionumber});
                 my $itemtypes = GetItemTypes();
                 if($ord->{isbn}){
@@ -211,8 +236,8 @@ sub printbasketgrouppdf{
                 } else {
                     push(@ba_order, undef);
                 }
-                if ($ord->{itemtype}){
-                    push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description}) if $bib->{itemtype};
+                if ($ord->{itemtype} and $bib->{itemtype}){
+                    push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description});
                 } else {
                     push(@ba_order, undef);
                 }
@@ -226,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);
@@ -246,6 +274,7 @@ sub printbasketgrouppdf{
     );
     my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
     print $pdf;
+
 }
 
 my $op = $input->param('op');
@@ -305,26 +334,22 @@ if ( $op eq "add" ) {
         # Build the combobox to select the billing place
         my @billingplaceloop;
         for (sort keys %$branches) {
-            my $selected = 1 if $_ eq $billingplace;
-            my %row = (
+            push @billingplaceloop, {
                 value      => $_,
-                selected   => $selected,
+                selected   => $_ eq $billingplace,
                 branchname => $branches->{$_}->{branchname},
-            );
-            push @billingplaceloop, \%row;
+            };
         }
         $template->param( billingplaceloop => \@billingplaceloop );
         
         # Build the combobox to select the delivery place
         my @deliveryplaceloop;
         for (sort keys %$branches) {
-            my $selected = 1 if $_ eq $deliveryplace;
-            my %row = (
+            push @deliveryplaceloop, {
                 value      => $_,
-                selected   => $selected,
+                selected   => $_ eq $deliveryplace,
                 branchname => $branches->{$_}->{branchname},
-            );
-            push @deliveryplaceloop, \%row;
+            };
         }
         $template->param( deliveryplaceloop => \@deliveryplaceloop );