Bug 12970: Fix the footer if several tax rate exist
[srvgit] / acqui / basket.pl
index 5c0e95d..5379c37 100755 (executable)
@@ -25,10 +25,11 @@ use warnings;
 use C4::Auth;
 use C4::Koha;
 use C4::Output;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Acquisition;
 use C4::Budgets;
-use C4::Bookseller qw( GetBookSellerFromId);
+use C4::Branch;
+use C4::Contract;
 use C4::Debug;
 use C4::Biblio;
 use C4::Members qw/GetMember/;  #needed for permissions checking for changing basketgroup of a basket
@@ -70,7 +71,7 @@ my $booksellerid = $query->param('booksellerid');
 
 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
     {
-        template_name   => "acqui/basket.tmpl",
+        template_name   => "acqui/basket.tt",
         query           => $query,
         type            => "intranet",
         authnotrequired => 0,
@@ -80,13 +81,25 @@ my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
 );
 
 my $basket = GetBasket($basketno);
+$booksellerid = $basket->{booksellerid} unless $booksellerid;
+my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+
+unless (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
+    $template->param(
+        cannot_manage_basket => 1,
+        basketno => $basketno,
+        basketname => $basket->{basketname},
+        booksellerid => $booksellerid,
+        name => $bookseller->{name}
+    );
+    output_html_with_http_headers $query, $cookie, $template->output;
+    exit;
+}
 
 # FIXME : what about the "discount" percentage?
 # FIXME : the query->param('booksellerid') below is probably useless. The bookseller is always known from the basket
 # if no booksellerid in parameter, get it from basket
 # warn "=>".$basket->{booksellerid};
-$booksellerid = $basket->{booksellerid} unless $booksellerid;
-my ($bookseller) = GetBookSellerFromId($booksellerid);
 my $op = $query->param('op');
 if (!defined $op) {
     $op = q{};
@@ -97,7 +110,43 @@ $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
 
 if ( $op eq 'delete_confirm' ) {
     my $basketno = $query->param('basketno');
-    DelBasket($basketno);
+    my $delbiblio = $query->param('delbiblio');
+    my @orders = GetOrders($basketno);
+#Delete all orders included in that basket, and all items received.
+    foreach my $myorder (@orders){
+        DelOrder($myorder->{biblionumber},$myorder->{ordernumber});
+    }
+# if $delbiblio = 1, delete the records if possible
+    if ((defined $delbiblio)and ($delbiblio ==1)){
+        my @cannotdelbiblios ;
+        foreach my $myorder (@orders){
+            my $biblionumber = $myorder->{'biblionumber'};
+            my $countbiblio = CountBiblioInOrders($biblionumber);
+            my $ordernumber = $myorder->{'ordernumber'};
+            my $subscriptions = scalar GetSubscriptionsId ($biblionumber);
+            my $itemcount = GetItemsCount($biblionumber);
+            my $error;
+            if ($countbiblio == 0 && $itemcount == 0 && $subscriptions == 0) {
+                $error = DelBiblio($myorder->{biblionumber}) }
+            else {
+                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
+                                         title=> $myorder->{'title'},
+                                         author=> $myorder->{'author'},
+                                         countbiblio=> $countbiblio,
+                                         itemcount=>$itemcount,
+                                         subscriptions=>$subscriptions};
+            }
+            if ($error) {
+                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
+                                         title=> $myorder->{'title'},
+                                         author=> $myorder->{'author'},
+                                         othererror=> $error};
+            }
+        }
+        $template->param( cannotdelbiblios => \@cannotdelbiblios );
+    }
+ # delete the basket
+    DelBasket($basketno,);
     $template->param( delete_confirmed => 1 );
 } elsif ( !$bookseller ) {
     $template->param( NO_BOOKSELLER => 1 );
@@ -105,7 +154,7 @@ if ( $op eq 'delete_confirm' ) {
     $template->param( delete_confirm => 1 );
     if ( C4::Context->preference("IndependentBranches") ) {
         my $userenv = C4::Context->userenv;
-        unless ( $userenv->{flags} == 1 ) {
+        unless ( C4::Context->IsSuperLibrarian() ) {
             my $validtest = ( $basket->{creationdate} eq '' )
               || ( $userenv->{branch} eq $basket->{branch} )
               || ( $userenv->{branch} eq '' )
@@ -118,7 +167,9 @@ if ( $op eq 'delete_confirm' ) {
     }
     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
-    my $contract = &GetContract($basket->{contractnumber});
+    my $contract = GetContract({
+        contractnumber => $basket->{contractnumber}
+    });
     $template->param(
         basketno             => $basketno,
         basketname           => $basket->{'basketname'},
@@ -140,9 +191,6 @@ if ( $op eq 'delete_confirm' ) {
         address3             => $bookseller->{'address3'},
         address4             => $bookseller->{'address4'},
       );
-} elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
-      print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
-    # check if we have to "close" a basket before building page
 } elsif ($op eq 'export') {
     print $query->header(
         -type       => 'text/csv',
@@ -188,11 +236,27 @@ if ( $op eq 'delete_confirm' ) {
 } elsif ($op eq 'reopen') {
     ReopenBasket($query->param('basketno'));
     print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
+} elsif ( $op eq 'mod_users' ) {
+    my $basketusers_ids = $query->param('basketusers_ids');
+    my @basketusers = split( /:/, $basketusers_ids );
+    ModBasketUsers($basketno, @basketusers);
+    print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
+    exit;
+} elsif ( $op eq 'mod_branch' ) {
+    my $branch = $query->param('branch');
+    $branch = undef if(defined $branch and $branch eq '');
+    ModBasket({
+        basketno => $basket->{basketno},
+        branch   => $branch
+    });
+    print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
+    exit;
 } else {
+    my @branches_loop;
     # get librarian branch...
     if ( C4::Context->preference("IndependentBranches") ) {
         my $userenv = C4::Context->userenv;
-        unless ( $userenv->{flags} == 1 ) {
+        unless ( C4::Context->IsSuperLibrarian() ) {
             my $validtest = ( $basket->{creationdate} eq '' )
               || ( $userenv->{branch} eq $basket->{branch} )
               || ( $userenv->{branch} eq '' )
@@ -202,7 +266,34 @@ if ( $op eq 'delete_confirm' ) {
                 exit 1;
             }
         }
+        if (!defined $basket->{branch} or $basket->{branch} eq $userenv->{branch}) {
+            push @branches_loop, {
+                branchcode => $userenv->{branch},
+                branchname => $userenv->{branchname},
+                selected => 1,
+            };
+        }
+    } else {
+        # get branches
+        my $branches = C4::Branch::GetBranches;
+        my @branchcodes = sort {
+            $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}
+        } keys %$branches;
+        foreach my $branch (@branchcodes) {
+            my $selected = 0;
+            if (defined $basket->{branch}) {
+                $selected = 1 if $branch eq $basket->{branch};
+            } else {
+                $selected = 1 if $branch eq C4::Context->userenv->{branch};
+            }
+            push @branches_loop, {
+                branchcode => $branch,
+                branchname => $branches->{$branch}->{branchname},
+                selected => $selected
+            };
+        }
     }
+
 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
     my ($basketgroup, $basketgroups);
     my $staffuser = GetMember(borrowernumber => $loggedinuser);
@@ -214,13 +305,6 @@ if ( $op eq 'delete_confirm' ) {
                 $basketgroup = $bg;
             }
         }
-        my %emptygroup = ( id   =>   undef,
-                           name =>   "No group");
-        if ( ! $basket->{basketgroupid} ) {
-            $emptygroup{default} = 1;
-            $emptygroup{nogroup} = 1;
-        }
-        unshift( @$basketgroups, \%emptygroup );
     }
 
     # if the basket is closed, calculate estimated delivery date
@@ -239,11 +323,18 @@ if ( $op eq 'delete_confirm' ) {
       "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
       $basket->{creationdate}, $basket->{authorisedby};
 
+    my @basketusers_ids = GetBasketUsers($basketno);
+    my @basketusers;
+    foreach my $basketuser_id (@basketusers_ids) {
+        my $basketuser = GetMember(borrowernumber => $basketuser_id);
+        push @basketusers, $basketuser if $basketuser;
+    }
+
     #to get active currency
     my $cur = GetCurrency();
 
 
-    my @results = GetOrders( $basketno );
+    my @orders = GetOrders( $basketno );
     my @books_loop;
 
     my @book_foot_loop;
@@ -252,7 +343,8 @@ if ( $op eq 'delete_confirm' ) {
     my $total_gste = 0;
     my $total_gsti = 0;
     my $total_gstvalue = 0;
-    for my $order (@results) {
+    for my $order (@orders) {
+        $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $booksellerid, ordering => 1 });
         my $line = get_order_infos( $order, $bookseller);
         if ( $line->{uncertainprice} ) {
             $template->param( uncertainprices => 1 );
@@ -260,29 +352,31 @@ if ( $op eq 'delete_confirm' ) {
 
         push @books_loop, $line;
 
-        $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
-        $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
+        $foot{$$line{gstrate}}{gstrate} = $$line{gstrate};
+        $foot{$$line{gstrate}}{gstvalue} += $$line{gstvalue};
         $total_gstvalue += $$line{gstvalue};
-        $foot{$$line{gstgsti}}{quantity}  += $$line{quantity};
+        $foot{$$line{gstrate}}{quantity}  += $$line{quantity};
         $total_quantity += $$line{quantity};
-        $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
+        $foot{$$line{gstrate}}{totalgste} += $$line{totalgste};
         $total_gste += $$line{totalgste};
-        $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
+        $foot{$$line{gstrate}}{totalgsti} += $$line{totalgsti};
         $total_gsti += $$line{totalgsti};
     }
 
     push @book_foot_loop, map {$_} values %foot;
 
     # Get cancelled orders
-    @results = GetCancelledOrders($basketno);
+    my @cancelledorders = GetCancelledOrders($basketno);
     my @cancelledorders_loop;
-    for my $order (@results) {
+    for my $order (@cancelledorders) {
+        $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $booksellerid, ordering => 1 });
         my $line = get_order_infos( $order, $bookseller);
         push @cancelledorders_loop, $line;
     }
 
-    my $contract = &GetContract($basket->{contractnumber});
-    my @orders = GetOrders($basketno);
+    my $contract = GetContract({
+        contractnumber => $basket->{contractnumber}
+    });
 
     if ($basket->{basketgroupid}){
         $basketgroup = GetBasketgroup($basket->{basketgroupid});
@@ -305,13 +399,17 @@ if ( $op eq 'delete_confirm' ) {
     $template->param(
         basketno             => $basketno,
         basketname           => $basket->{'basketname'},
+        basketbranchname     => C4::Branch::GetBranchName($basket->{branch}),
         basketnote           => $basket->{note},
         basketbooksellernote => $basket->{booksellernote},
         basketcontractno     => $basket->{contractnumber},
         basketcontractname   => $contract->{contractname},
+        branches_loop        => \@branches_loop,
         creationdate         => $basket->{creationdate},
         authorisedby         => $basket->{authorisedby},
         authorisedbyname     => $basket->{authorisedbyname},
+        basketusers_ids      => join(':', @basketusers_ids),
+        basketusers          => \@basketusers,
         closedate            => $basket->{closedate},
         estimateddeliverydate=> $estimateddeliverydate,
         deliveryplace        => C4::Branch::GetBranchName( $basket->{deliveryplace} ),
@@ -349,28 +447,6 @@ sub get_order_infos {
     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
     $line{basketno}       = $basketno;
     $line{budget_name}    = $budget->{budget_name};
-    $line{rrp} = ConvertCurrency( $order->{'currency'}, $line{rrp} ); # FIXME from comm
-    if ( $bookseller->{'listincgst'} ) {
-        $line{rrpgsti} = sprintf( "%.2f", $line{rrp} );
-        $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
-        $line{rrpgste} = sprintf( "%.2f", $line{rrp} / ( 1 + ( $line{gstgsti} / 100 ) ) );
-        $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
-        $line{ecostgsti} = sprintf( "%.2f", $line{ecost} );
-        $line{ecostgste} = sprintf( "%.2f", $line{ecost} / ( 1 + ( $line{gstgsti} / 100 ) ) );
-        $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
-        $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
-        $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
-    } else {
-        $line{rrpgsti} = sprintf( "%.2f", $line{rrp} * ( 1 + ( $line{gstrate} ) ) );
-        $line{rrpgste} = sprintf( "%.2f", $line{rrp} );
-        $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
-        $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
-        $line{ecostgsti} = sprintf( "%.2f", $line{ecost} * ( 1 + ( $line{gstrate} ) ) );
-        $line{ecostgste} = sprintf( "%.2f", $line{ecost} );
-        $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
-        $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
-        $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
-    }
 
     if ( $line{uncertainprice} ) {
         $line{rrpgste} .= ' (Uncertain)';
@@ -421,7 +497,7 @@ sub get_order_infos {
         if ($line{$key}) {
             my $order = GetOrder($line{$key});
             my $basket = GetBasket($order->{basketno});
-            my $bookseller = GetBookSellerFromId($basket->{booksellerid});
+            my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
             $line{$key} = {
                 order => $order,
                 basket => $basket,