Bug 6810: [QA Follow-up] Exit cronjob if pref not set
[srvgit] / acqui / basketgroup.pl
index 36c4462..b2ed7db 100755 (executable)
@@ -8,18 +8,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY 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.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 
 =head1 NAME
@@ -47,22 +47,21 @@ use strict;
 use warnings;
 use Carp;
 
-use C4::Input;
 use C4::Auth;
 use C4::Output;
-use CGI;
+use CGI qw ( -utf8 );
 
-use C4::Bookseller qw/GetBookSellerFromId/;
 use C4::Budgets qw/ConvertCurrency/;
 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/;
 
+use Koha::Acquisition::Bookseller;
+
 our $input=new CGI;
 
 our ($template, $loggedinuser, $cookie)
-    = get_template_and_user({template_name => "acqui/basketgroup.tmpl",
+    = get_template_and_user({template_name => "acqui/basketgroup.tt",
                             query => $input,
                             type => "intranet",
                             authnotrequired => 0,
@@ -70,69 +69,6 @@ our ($template, $loggedinuser, $cookie)
                             debug => 1,
                 });
 
-sub parseinputbaskets {
-    my $booksellerid = shift;
-    my $baskets = &GetBasketsByBookseller($booksellerid);
-    for(my $i=0; $i < scalar @$baskets; ++$i) {
-        if( @$baskets[$i] && ! @$baskets[$i]->{'closedate'} ) {
-            splice(@$baskets, $i, 1);
-            --$i;
-        }
-    }
-    foreach my $basket (@$baskets){
-#perl DBI uses value "undef" for the mysql "NULL" value, so i need to check everywhere where $basket->{'basketgroupid'} is used for undef ☹
-        $basket->{'basketgroupid'} = $input->param($basket->{'basketno'}.'-group') || undef;
-    }
-    return $baskets;
-}
-
-
-
-sub parseinputbasketgroups {
-    my $booksellerid = shift;
-    my $baskets = shift;
-    my $basketgroups = &GetBasketgroups($booksellerid);
-    my $newbasketgroups;
-    foreach my $basket (@$baskets){
-        my $basketgroup;
-        my $i = 0;
-        my $exists;
-        if(! $basket->{'basketgroupid'} || $basket->{'basketgroupid'} == 0){
-            $exists = "true";
-        } else {
-            foreach my $basketgroup (@$basketgroups){
-                if($basket->{'basketgroupid'} == $basketgroup->{'id'}){
-                    $exists = "true";
-                    push(@{$basketgroup->{'basketlist'}}, $basket->{'basketno'});
-                    last;
-                }
-            }
-        }
-        if (! $exists){
-#if the basketgroup doesn't exist yet
-            $basketgroup = $newbasketgroups->{$basket->{'basketgroupid'}} || undef;
-            $basketgroup->{'booksellerid'} = $booksellerid;
-        } else {
-            while($i < scalar @$basketgroups && @$basketgroups[$i]->{'id'} != $basket->{'basketgroupid'}){
-                ++$i;
-            }
-            $basketgroup = @$basketgroups[$i];
-        }
-        $basketgroup->{'id'}=$basket->{'basketgroupid'};
-        $basketgroup->{'name'}=$input->param('basketgroup-'.$basketgroup->{'id'}.'-name') || "";
-        $basketgroup->{'closed'}= $input->param('basketgroup-'.$basketgroup->{'id'}.'-closed');
-        push(@{$basketgroup->{'basketlist'}}, $basket->{'basketno'});
-        if (! $exists){
-            $newbasketgroups->{$basket->{'basketgroupid'}} = $basketgroup;
-        } else {
-            if($basketgroup->{'id'}){
-                @$basketgroups[$i] = $basketgroup;
-            }
-        }
-    }
-    return($basketgroups, $newbasketgroups);
-}
-
 sub BasketTotal {
     my $basketno = shift;
     my $bookseller = shift;
@@ -145,7 +81,7 @@ sub BasketTotal {
             $total = $total * ( $gst / 100 +1);
         }
     }
-    $total .= $bookseller->{invoiceprice} // 0;
+    $total .= " " . ($bookseller->{invoiceprice} // 0);
     return $total;
 }
 
@@ -189,7 +125,8 @@ sub printbasketgrouppdf{
     my ($basketgroupid) = @_;
     
     my $pdfformat = C4::Context->preference("OrderPdfFormat");
-    if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'){
+    if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages' || $pdfformat eq 'pdfformat::layout3pagesfr'
+        || $pdfformat eq 'pdfformat::layout2pagesde'){
        eval {
         eval "require $pdfformat";
            import $pdfformat;
@@ -206,7 +143,7 @@ sub printbasketgrouppdf{
     }
     
     my $basketgroup = GetBasketgroup($basketgroupid);
-    my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'});
+    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basketgroup->{booksellerid} });
     my $baskets = GetBasketsByBasketgroup($basketgroupid);
     
     my %orders;
@@ -231,28 +168,7 @@ sub printbasketgrouppdf{
                 croak $@;
             }
 
-            $ord->{rrp} = ConvertCurrency( $ord->{'currency'}, $ord->{rrp} );
-            if ( $bookseller->{'listincgst'} ) {
-                $ord->{rrpgsti} = sprintf( "%.2f", $ord->{rrp} );
-                $ord->{gstgsti} = sprintf( "%.2f", $ord->{gstrate} * 100 );
-                $ord->{rrpgste} = sprintf( "%.2f", $ord->{rrp} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
-                $ord->{gstgste} = sprintf( "%.2f", $ord->{gstgsti} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
-                $ord->{ecostgsti} = sprintf( "%.2f", $ord->{ecost} );
-                $ord->{ecostgste} = sprintf( "%.2f", $ord->{ecost} / ( 1 + ( $ord->{gstgsti} / 100 ) ) );
-                $ord->{gstvalue} = sprintf( "%.2f", ( $ord->{ecostgsti} - $ord->{ecostgste} ) * $ord->{quantity});
-                $ord->{totalgste} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgste} );
-                $ord->{totalgsti} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgsti} );
-            } else {
-                $ord->{rrpgsti} = sprintf( "%.2f", $ord->{rrp} * ( 1 + ( $ord->{gstrate} ) ) );
-                $ord->{rrpgste} = sprintf( "%.2f", $ord->{rrp} );
-                $ord->{gstgsti} = sprintf( "%.2f", $ord->{gstrate} * 100 );
-                $ord->{gstgste} = sprintf( "%.2f", $ord->{gstrate} * 100 );
-                $ord->{ecostgsti} = sprintf( "%.2f", $ord->{ecost} * ( 1 + ( $ord->{gstrate} ) ) );
-                $ord->{ecostgste} = sprintf( "%.2f", $ord->{ecost} );
-                $ord->{gstvalue} = sprintf( "%.2f", ( $ord->{ecostgsti} - $ord->{ecostgste} ) * $ord->{quantity});
-                $ord->{totalgste} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgste} );
-                $ord->{totalgsti} = sprintf( "%.2f", $ord->{quantity} * $ord->{ecostgsti} );
-            }
+            $ord = C4::Acquisition::populate_order_with_prices({ order => $ord, booksellerid => $bookseller->{id}, ordering => 1 });
             my $bib = GetBiblioData($ord->{biblionumber});
             my $itemtypes = GetItemTypes();
 
@@ -262,25 +178,23 @@ sub printbasketgrouppdf{
 
             # Editor Number
             my $en;
+            my $edition;
             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" );
+                    $edition = $marcrecord->subfield( '205', 'a' );
                 } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) {
                     $en = $marcrecord->subfield( '037', "a" );
+                    $edition = $marcrecord->subfield( '250', 'a' );
                 }
             }
 
-            my $ba_order = {
-                isbn => ($ord->{isbn} ? $ord->{isbn} : undef),
-                itemtype => ( $ord->{itemtype} and $bib->{itemtype} ? $itemtypes->{$bib->{itemtype}}->{description} : undef ),
-                en => ( $en ? $en : undef ),
-            };
-            for my $key ( qw/ gstrate author title itemtype publishercode discount quantity rrpgsti rrpgste gstgsti gstgste ecostgsti ecostgste gstvalue totalgste totalgsti / ) {
-                $ba_order->{$key} = $ord->{$key};
-            }
+            $ord->{itemtype} = ( $ord->{itemtype} and $bib->{itemtype} ) ? $itemtypes->{$bib->{itemtype}}->{description} : undef;
+            $ord->{en} = $en ? $en : undef;
+            $ord->{edition} = $edition ? $edition : undef;
 
-            push(@ba_orders, $ba_order);
+            push(@ba_orders, $ord);
         }
         $orders{$basket->{basketno}} = \@ba_orders;
     }
@@ -297,7 +211,6 @@ my $op = $input->param('op') || 'display';
 # possible values of $op :
 # - add : adds a new basketgroup, or edit an open basketgroup, or display a closed basketgroup
 # - mod_basket : modify an individual basket of the basketgroup
-# - validate :  FIXME dead code
 # - closeandprint : close and print an closed basketgroup in pdf. called by clicking on "Close and print" button in closed basketgroups list
 # - print : print a closed basketgroup. called by clicking on "Print" button in closed basketgroups list
 # - export : export in CSV a closed basketgroup. called by clicking on "Export" button in closed basketgroups list
@@ -314,68 +227,48 @@ if ( $op eq "add" ) {
 # else, edit (if it is open) or display (if it is close) the basketgroup basketgroupid
 # the template will know if basketgroup must be displayed or edited, depending on the value of closed key
 #
-    if(! $booksellerid){
-# Unknown bookseller
-# FIXME : ungroupedlist does not seem to be used in this file nor in template
-        $template->param( ungroupedlist => 1);
-        my @booksellers = GetBookSeller('');
-       for (my $i=0; $i < scalar @booksellers; $i++) {
-            my $baskets = &GetBasketsByBookseller($booksellers[$i]->{id});
-            for (my $j=0; $j < scalar @$baskets; $j++) {
-                if(! @$baskets[$i]->{closedate} || @$baskets[$i]->{basketgroupid}) {
-                    splice(@$baskets, $j, 1);
-                    $j--;
-                }
-            }
-            if (scalar @$baskets == 0){
-                splice(@booksellers, $i, 1);
-                $i--;
-            }
+    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+    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);
+        foreach my $basket(@{$selecteds}){
+            $basket->{total} = BasketTotal($basket->{basketno}, $bookseller);
         }
+        $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},
+            freedeliveryplace => $basketgroup->{freedeliveryplace},
+        );
+        $billingplace  = $basketgroup->{billingplace};
+        $deliveryplace = $basketgroup->{deliveryplace};
+        $freedeliveryplace = $basketgroup->{freedeliveryplace};
+        $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
     } else {
-# Known bookseller
-        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);
-            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},
-                freedeliveryplace => $basketgroup->{freedeliveryplace},
-            );
-            $billingplace  = $basketgroup->{billingplace};
-            $deliveryplace = $basketgroup->{deliveryplace};
-            $freedeliveryplace = $basketgroup->{freedeliveryplace};
-            $template->param( closedbg => ($basketgroup ->{'closed'}) ? 1 : 0);
-        } else {
-            $template->param( closedbg => 0);
-        }
-        # 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 = C4::Branch::GetBranchesLoop( $billingplace );
-        $template->param( billingplaceloop => $branches );
-        $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
-        $template->param( deliveryplaceloop => $branches );
-        $template->param( booksellerid => $booksellerid );
+        $template->param( closedbg => 0);
     }
+    # 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 = C4::Branch::GetBranchesLoop( $billingplace );
+    $template->param( billingplaceloop => $branches );
+    $branches = C4::Branch::GetBranchesLoop( $deliveryplace );
+    $template->param( deliveryplaceloop => $branches );
+    $template->param( booksellerid => $booksellerid );
+
     # the template will display a unique basketgroup
     $template->param(grouping => 1);
     my $basketgroups = &GetBasketgroups($booksellerid);
-    my $bookseller = &GetBookSellerFromId($booksellerid);
     my $baskets = &GetBasketsByBookseller($booksellerid);
     displaybasketgroups($basketgroups, $bookseller, $baskets);
 } elsif ($op eq 'mod_basket') {
@@ -387,47 +280,6 @@ if ( $op eq "add" ) {
   ModBasket( { basketno => $basketno,
                          basketgroupid => $basketgroupid } );
   print $input->redirect("basket.pl?basketno=" . $basketno);
-} elsif ($op eq 'validate') {
-#
-#  FIXME dead code
-#
-    if(! $booksellerid){
-        $template->param( booksellererror => 1);
-    } else {
-        $template->param( booksellerid => $booksellerid );
-    }
-    my $baskets = parseinputbaskets($booksellerid);
-    my ($basketgroups, $newbasketgroups) = parseinputbasketgroups($booksellerid, $baskets);
-    foreach my $nbgid (keys %$newbasketgroups){
-#javascript just picks an ID that's higher than anything else, the ID might not be correct..change it and change all the basket's basketgroupid as well
-        my $bgid = NewBasketgroup($newbasketgroups->{$nbgid});
-        ${$newbasketgroups->{$nbgid}}->{'id'} = $bgid;
-        ${$newbasketgroups->{$nbgid}}->{'oldid'} = $nbgid;
-    }
-    foreach my $basket (@$baskets){
-#if the basket was added to a new basketgroup, first change the groupid to the groupid of the basket in mysql, because it contains the id from javascript otherwise.
-        if ( $basket->{'basketgroupid'} && $newbasketgroups->{$basket->{'basketgroupid'}} ){
-            $basket->{'basketgroupid'} = ${$newbasketgroups->{$basket->{'basketgroupid'}}}->{'id'};
-        }
-        ModBasket($basket);
-    }
-    foreach my $basketgroup (@$basketgroups){
-        if(! $basketgroup->{'id'}){
-            foreach my $basket (@{$basketgroup->{'baskets'}}){
-                if($input->param('basket'.$basket->{'basketno'}.'changed')){
-                    ModBasket($basket);
-                }
-            }
-        } elsif ($input->param('basketgroup-'.$basketgroup->{'id'}.'-changed')){
-            ModBasketgroup($basketgroup);
-        }
-    }
-    $basketgroups = &GetBasketgroups($booksellerid);
-    my $bookseller = &GetBookSellerFromId($booksellerid);
-    $baskets = &GetBasketsByBookseller($booksellerid);
-    # keep ungroupedbaskets
-
-    displaybasketgroups($basketgroups, $bookseller, $baskets);
 } elsif ( $op eq 'closeandprint') {
 #
 # close an open basketgroup and generates a pdf
@@ -502,7 +354,7 @@ if ( $op eq "add" ) {
 # FIXME
         }
     }else{
-    # we create a new basketgroup (whith a closed basket)
+    # we create a new basketgroup (with a closed basket)
         $basketgroup = {
             name              => $basketgroupname,
             booksellerid      => $booksellerid,
@@ -522,7 +374,7 @@ if ( $op eq "add" ) {
 }else{
 # no param : display the list of all basketgroups for a given vendor
     my $basketgroups = &GetBasketgroups($booksellerid);
-    my $bookseller = &GetBookSellerFromId($booksellerid);
+    my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
     my $baskets = &GetBasketsByBookseller($booksellerid);
 
     displaybasketgroups($basketgroups, $bookseller, $baskets);