enable EnhancedMessagingPreferences to turn notification on/off
[koha.git] / acqui / basketgroup.pl
index 9baa172..0586323 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
@@ -53,6 +54,8 @@ 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::Bookseller qw/GetBookSellerFromId/;
+use C4::Branch qw/GetBranches/;
+use C4::Members qw/GetMember/;
 
 my $input=new CGI;
 
@@ -180,10 +183,10 @@ sub displaybasketgroups {
 sub printbasketgrouppdf{
     my ($basketgroupid) = @_;
     
-    my $pdfformat = C4::Context->preference("pdfformat");
-    eval "use $pdfformat" ;
-    warn @_;
-    eval "use C4::Branch";
+    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.
     
     my $basketgroup = GetBasketgroup($basketgroupid);
     my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
@@ -235,14 +238,14 @@ sub printbasketgrouppdf{
                 }
             }
         }
-        %orders->{$basket->{basketno}}=\@ba_orders;
+        $orders{$basket->{basketno}}=\@ba_orders;
     }
-    print $input->header( -type => 'application/pdf', -attachment => 'basketgroup.pdf' );
-    my $branch = GetBranchInfo(GetBranch($input, GetBranches()));
-    $branch = @$branch[0];
-    my $pdf = printpdf($basketgroup, $bookseller, $baskets, $branch, \%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');
@@ -268,14 +271,60 @@ if ( $op eq "add" ) {
         }
     } else {
         my $basketgroupid = $input->param('basketgroupid');
-        if($basketgroupid){
+        my $billingplace;
+        my $deliveryplace;
+        if ( $basketgroupid ) {
+            # Get the selected baskets in the basketgroup to display them
             my $selecteds = GetBasketsByBasketgroup($basketgroupid);
             foreach (@{$selecteds}){
                 $_->{total} = BasketTotal($_->{basketno}, $_);
             }
             $template->param(basketgroupid => $basketgroupid,
                              selectedbaskets => $selecteds);
+
+            # Get general informations about the basket group to prefill the form
+            my $basketgroup = GetBasketgroup($basketgroupid);
+            $template->param(
+                name            => $basketgroup->{name},
+                deliverycomment => $basketgroup->{deliverycomment},
+            );
+            $billingplace  = $basketgroup->{billingplace};
+            $deliveryplace = $basketgroup->{deliveryplace};
+        }
+
+        # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data
+        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) {
+            my $selected = 1 if $_ eq $billingplace;
+            my %row = (
+                value      => $_,
+                selected   => $selected,
+                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 = (
+                value      => $_,
+                selected   => $selected,
+                branchname => $branches->{$_}->{branchname},
+            );
+            push @deliveryplaceloop, \%row;
         }
+        $template->param( deliveryplaceloop => \@deliveryplaceloop );
+
         $template->param( booksellerid => $booksellerid );
     }
     $template->param(grouping => 1);
@@ -334,15 +383,15 @@ if ( $op eq "add" ) {
     CloseBasketgroup($basketgroupid);
     
     printbasketgrouppdf($basketgroupid);
+    exit;
 }elsif ($op eq 'print'){
     my $basketgroupid = $input->param('basketgroupid');
     
     printbasketgrouppdf($basketgroupid);
+    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'){
@@ -357,19 +406,24 @@ if ( $op eq "add" ) {
     
     # 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;
     # If we got a basketgroupname, we create a basketgroup
     if ($basketgroupid) {
         $basketgroup = {
-              name => $basketgroupname,
-              id => $basketgroupid,
-              basketlist => \@baskets,
-              closed      => $close,
+              name            => $basketgroupname,
+              id              => $basketgroupid,
+              basketlist      => \@baskets,
+              billingplace    => $billingplace,
+              deliveryplace   => $deliveryplace,
+              deliverycomment => $deliverycomment,
+              closed          => $close,
         };
         ModBasketgroup($basketgroup);
         if($close){
@@ -377,15 +431,19 @@ if ( $op eq "add" ) {
         }
     }else{
         $basketgroup = {
-            name         => $basketgroupname,
-            booksellerid => $booksellerid,
-            basketlist   => \@baskets,
-            closed        => $close,
+            name            => $basketgroupname,
+            booksellerid    => $booksellerid,
+            basketlist      => \@baskets,
+            deliveryplace   => $deliveryplace,
+            deliverycomment => $deliverycomment,
+            closed          => $close,
         };
         $basketgroupid = NewBasketgroup($basketgroup);
     }
-    
-    print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid);
+   
+    my $url = '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid;
+    $url .= "&closed=1" if ($input->param("closed")); 
+    print $input->redirect($url);
     
 }else{
     my $basketgroups = &GetBasketgroups($booksellerid);
@@ -394,5 +452,6 @@ if ( $op eq "add" ) {
 
     displaybasketgroups($basketgroups, $bookseller, $baskets);
 }
+$template->param(closed => $input->param("closed"));
 #prolly won't use all these, maybe just use print, the rest can be done inside validate
 output_html_with_http_headers $input, $cookie, $template->output;