enable EnhancedMessagingPreferences to turn notification on/off
[koha.git] / acqui / basketgroup.pl
index e8027a8..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
@@ -51,8 +52,10 @@ use C4::Output;
 use CGI;
 
 use C4::Bookseller qw/GetBookSellerFromId/;
-use C4::Acquisition qw/GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup/;
+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;
 
@@ -169,17 +172,87 @@ sub displaybasketgroups {
         if( ! @$baskets[$i]->{'closedate'} ) {
             splice(@$baskets, $i, 1);
             --$i;
+        }else{
+            @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller);
         }
     }
     $template->param(baskets => $baskets);
     $template->param( booksellername => $bookseller ->{'name'});
 }
 
+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.
+    
+    my $basketgroup = GetBasketgroup($basketgroupid);
+    my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
+    my $baskets = GetBasketsByBasketgroup($basketgroupid);
+    
+    my %orders;
+    for my $basket (@$baskets) {
+        my @ba_orders;
+        my @ords = &GetOrders($basket->{basketno});
+        for my $ord (@ords) {
+            # ba_order is filled with : 
+            # 0      1        2        3         4            5         6       7      8        9
+            #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";
+                my $bib = GetBiblioData($ord->{biblionumber});
+                my $itemtypes = GetItemTypes();
+                if($ord->{isbn}){
+                    push(@ba_order, $ord->{isbn});
+                } else {
+                    push(@ba_order, undef);
+                }
+                if ($ord->{itemtype}){
+                    push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description}) if $bib->{itemtype};
+                } else {
+                    push(@ba_order, undef);
+                }
+#             } else {
+#                 push(@ba_order, undef, undef);
+                for my $key (qw/author title publishercode quantity listprice ecost/) {
+                    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_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");
+                }
+                if($en){
+                    push(@ba_order, $en);
+                } else {
+                    push(@ba_order, undef);
+                }
+            }
+        }
+        $orders{$basket->{basketno}}=\@ba_orders;
+    }
+    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;
+}
 
 my $op = $input->param('op');
 my $booksellerid = $input->param('booksellerid');
+$template->param(booksellerid => $booksellerid);
 
-if (! $op ) {
+if ( $op eq "add" ) {
     if(! $booksellerid){
         $template->param( ungroupedlist => 1);
         my @booksellers = GetBookSeller('');
@@ -197,8 +270,64 @@ if (! $op ) {
             }
         }
     } else {
+        my $basketgroupid = $input->param('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);
     my $basketgroups = &GetBasketgroups($booksellerid);
     my $bookseller = &GetBookSellerFromId($booksellerid);
     my $baskets = &GetBasketsByBookseller($booksellerid);
@@ -248,56 +377,81 @@ if (! $op ) {
     $baskets = &GetBasketsByBookseller($booksellerid);
 
     displaybasketgroups($basketgroups, $bookseller, $baskets);
-} elsif ( $op eq 'printbgroup') {
-    my $pdfformat = C4::Context->preference("pdfformat");
-    eval "use $pdfformat" ;
-    eval "use C4::Branch";
-    my $basketgroupid = $input->param('bgroupid');
-    my $basketgroup = GetBasketgroup($basketgroupid);
-    my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
-    my $baskets = GetBasketsByBasketgroup($basketgroupid);
-    my %orders;
-    for my $basket (@$baskets) {
-        my @ba_orders;
-        my @ords = &GetOrders($basket->{basketno});
-        for my $ord (@ords) {
-            # ba_order is filled with : 
-            # 0      1        2        3         4            5         6       7      8        9
-            #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";
-                my $bib = GetBiblioData($ord->{biblionumber});
-                my $itemtypes = GetItemTypes();
-                if($ord->{isbn}){
-                    push(@ba_order, $ord->{isbn});
-                } else {
-                    push(@ba_order, undef);
-                }
-                if ($ord->{itemtype}){
-                    push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description}) if $bib->{itemtype};
-                } else {
-                    push(@ba_order, undef);
-                }
-#             } else {
-#                 push(@ba_order, undef, undef);
-                for my $key (qw/author title publishercode quantity listprice ecost/) {
-                    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_orders, \@ba_order);
-            }
+} elsif ( $op eq 'closeandprint') {
+    my $basketgroupid = $input->param('basketgroupid');
+    
+    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');
+    DelBasketgroup($basketgroupid);
+    print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid);
+    
+}elsif ( $op eq 'reopen'){
+    my $basketgroupid   = $input->param('basketgroupid');
+    my $booksellerid    = $input->param('booksellerid');
+    
+    ReOpenBasketgroup($basketgroupid);
+        
+    print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid . '#closed');
+    
+} 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;
+    # 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,
+        };
+        ModBasketgroup($basketgroup);
+        if($close){
+            
         }
-        %orders->{$basket->{basketno}}=\@ba_orders;
+    }else{
+        $basketgroup = {
+            name            => $basketgroupname,
+            booksellerid    => $booksellerid,
+            basketlist      => \@baskets,
+            deliveryplace   => $deliveryplace,
+            deliverycomment => $deliverycomment,
+            closed          => $close,
+        };
+        $basketgroupid = NewBasketgroup($basketgroup);
     }
-    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 $pdf;
-    exit;
+   
+    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);
+    my $bookseller = &GetBookSellerFromId($booksellerid);
+    my $baskets = &GetBasketsByBookseller($booksellerid);
+
+    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;