Bug 8727 Minor stylistic change to help text
[koha.git] / acqui / basketgroup.pl
index e8273a9..ac0672c 100755 (executable)
@@ -4,6 +4,7 @@
 #written by john.soros@biblibre.com 01/10/2008
 
 # Copyright 2008 - 2009 BibLibre SARL
+# Parts Copyright Catalyst 2010
 #
 # This file is part of Koha.
 #
@@ -16,9 +17,9 @@
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 
 =head1 NAME
@@ -44,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;
@@ -51,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",
@@ -137,8 +139,8 @@ sub BasketTotal {
     my @orders = GetOrders($basketno);
     for my $order (@orders){
         $total = $total + ( $order->{ecost} * $order->{quantity} );
-        if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} || C4::Context->preference("gist") )) {
-            my $gst = $bookseller->{gstrate} || C4::Context->preference("gist");
+        if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
+            my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
             $total = $total * ( $gst / 100 +1);
         }
     }
@@ -183,9 +185,21 @@ sub printbasketgrouppdf{
     my ($basketgroupid) = @_;
     
     my $pdfformat = C4::Context->preference("OrderPdfFormat");
-    eval "use $pdfformat" ;
-    warn @_;
-    eval "use C4::Branch";
+    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'});
@@ -201,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}){
@@ -210,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);
                 }
@@ -221,14 +247,17 @@ sub printbasketgrouppdf{
                     push(@ba_order, $ord->{$key});                                                  #Order lines
                 }
                 push(@ba_order, $bookseller->{discount});
-                push(@ba_order, $bookseller->{gstrate}*100 || C4::Context->preference("gist") || 0);
+                push(@ba_order, $bookseller->{gstrate}*100 // C4::Context->preference("gist") // 0);
                 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);
@@ -237,15 +266,18 @@ sub printbasketgrouppdf{
                 }
             }
         }
-        %orders->{$basket->{basketno}}=\@ba_orders;
+        $orders{$basket->{basketno}}=\@ba_orders;
     }
-    print $input->header( -type => 'application/pdf', -attachment => $basketgroup->{name}.'.pdf' );
-    my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} || C4::Context->preference("gist")) || die "pdf generation failed";
+    print $input->header(
+        -type       => 'application/pdf',
+        -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
+    );
+    my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
     print $pdf;
-    exit;
+
 }
 
-my $op = $input->param('op');
+my $op = $input->param('op') || 'display';
 my $booksellerid = $input->param('booksellerid');
 $template->param(booksellerid => $booksellerid);
 
@@ -270,6 +302,7 @@ if ( $op eq "add" ) {
         my $basketgroupid = $input->param('basketgroupid');
         my $billingplace;
         my $deliveryplace;
+        my $freedeliveryplace;
         if ( $basketgroupid ) {
             # Get the selected baskets in the basketgroup to display them
             my $selecteds = GetBasketsByBasketgroup($basketgroupid);
@@ -284,9 +317,11 @@ if ( $op eq "add" ) {
             $template->param(
                 name            => $basketgroup->{name},
                 deliverycomment => $basketgroup->{deliverycomment},
+                freedeliveryplace => $basketgroup->{freedeliveryplace},
             );
             $billingplace  = $basketgroup->{billingplace};
             $deliveryplace = $basketgroup->{deliveryplace};
+            $freedeliveryplace = $basketgroup->{freedeliveryplace};
         }
 
         # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
@@ -299,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 );
 
@@ -380,15 +411,23 @@ if ( $op eq "add" ) {
     CloseBasketgroup($basketgroupid);
     
     printbasketgrouppdf($basketgroupid);
+    exit;
 }elsif ($op eq 'print'){
     my $basketgroupid = $input->param('basketgroupid');
     
     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');
-    warn $basketgroupid;
     DelBasketgroup($basketgroupid);
-    warn "---------------";
     print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid);
     
 }elsif ( $op eq 'reopen'){
@@ -402,25 +441,27 @@ if ( $op eq "add" ) {
 } elsif ( $op eq 'attachbasket') {
     
     # Getting parameters
-    my $basketgroup = {};
-    my @baskets         = $input->param('basket');
-    my $basketgroupid   = $input->param('basketgroupid');
-    my $basketgroupname = $input->param('basketgroupname');
-    my $booksellerid    = $input->param('booksellerid');
-    my $billingplace    = $input->param('billingplace');
-    my $deliveryplace   = $input->param('deliveryplace');
-    my $deliverycomment = $input->param('deliverycomment');
-    my $close           = $input->param('close') ? 1 : 0;
+    my $basketgroup       = {};
+    my @baskets           = $input->param('basket');
+    my $basketgroupid     = $input->param('basketgroupid');
+    my $basketgroupname   = $input->param('basketgroupname');
+    my $booksellerid      = $input->param('booksellerid');
+    my $billingplace      = $input->param('billingplace');
+    my $deliveryplace     = $input->param('deliveryplace');
+    my $freedeliveryplace = $input->param('freedeliveryplace');
+    my $deliverycomment   = $input->param('deliverycomment');
+    my $close             = $input->param('close') ? 1 : 0;
     # If we got a basketgroupname, we create a basketgroup
     if ($basketgroupid) {
         $basketgroup = {
-              name            => $basketgroupname,
-              id              => $basketgroupid,
-              basketlist      => \@baskets,
-              billingplace    => $billingplace,
-              deliveryplace   => $deliveryplace,
-              deliverycomment => $deliverycomment,
-              closed          => $close,
+              name              => $basketgroupname,
+              id                => $basketgroupid,
+              basketlist        => \@baskets,
+              billingplace      => $billingplace,
+              deliveryplace     => $deliveryplace,
+              freedeliveryplace => $freedeliveryplace,
+              deliverycomment   => $deliverycomment,
+              closed            => $close,
         };
         ModBasketgroup($basketgroup);
         if($close){
@@ -428,12 +469,13 @@ if ( $op eq "add" ) {
         }
     }else{
         $basketgroup = {
-            name            => $basketgroupname,
-            booksellerid    => $booksellerid,
-            basketlist      => \@baskets,
-            deliveryplace   => $deliveryplace,
-            deliverycomment => $deliverycomment,
-            closed          => $close,
+            name              => $basketgroupname,
+            booksellerid      => $booksellerid,
+            basketlist        => \@baskets,
+            deliveryplace     => $deliveryplace,
+            freedeliveryplace => $freedeliveryplace,
+            deliverycomment   => $deliverycomment,
+            closed            => $close,
         };
         $basketgroupid = NewBasketgroup($basketgroup);
     }