From e8db0ebad68b09e68e6353b7ddde6b47d0dcfdb2 Mon Sep 17 00:00:00 2001
From: Mathieu Saby <mathieu.saby@univ-rennes2.fr>
Date: Mon, 30 Sep 2013 23:38:35 +0200
Subject: [PATCH 3] Bug 7791 Followup: add warning after deletion if some records were not deleted
Content-Type: text/plain; charset="utf-8"

This followup add some warnings after deletion if some items were not deleted.
4 types of messages are possible :
- x item(s) attached.
- x subscription(s) attached.
- x order(s) attached.
- Unknown error.

To test :
test a
1. create a basket with
- an order using a record A which has already an item, which is used in a subscription, and in other order (in an other basket)
- an order using a record B used nowhere elese
2. click on "Delete basket"
3. choose button "Delete basket, orders and records"
4. you must see a page anouncing basket deletion, and that record A was not deleted because of its item, subscription and order.
5. check the link around the title of record B takes you to the record
6. check the link under the warning box ("Click here to go back to booksellers page") takes you to booksellers page
5. check record B is deleted

test b
1. suppress the subscription linked with record A
2. create an other basket using record A
3. delete the basket on the same way as for test a
4. you must see a page anouncing basket deletion, and that record A was not deleted because of its item and order

test c
1. suppress the item attached under record A
2. create an other basket using record A
3. delete the basket on the same way as for test a
4. you must see a page anouncing basket deletion, and that record A was not deleted because of its order

---
 acqui/basket.pl                                    |   22 ++++++++++++++++--
 .../intranet-tmpl/prog/en/modules/acqui/basket.tt  |   24 ++++++++++++++++++--
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/acqui/basket.pl b/acqui/basket.pl
index 02a36a7..4ae9488 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -105,14 +105,32 @@ if ( $op eq 'delete_confirm' ) {
     }
 # 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 = GetSubscriptionsId ($biblionumber);
+            my $subscriptions = scalar GetSubscriptionsId ($biblionumber);
             my $itemcount = GetItemsCount($biblionumber);
-            DelBiblio($myorder->{biblionumber}) if ($countbiblio == 0 && $itemcount == 0 && !(@subscriptions));
+            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,);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
index dec4890..4e17957 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
@@ -173,11 +173,11 @@
                            <p>Warning:</p>
                            <p>All orders of this basket will be cancelled and used funds will be refunded.</p>
                            <p>If items have been created when ordering or receiving, they will be deleted.</p>
-                           <p>You can choose to delete records if possible (if they don't have any item attached, any subscription and are not used in any other order).</p>
+                           <p>You can choose to delete records if possible (records with some items, used in a subscription, or an other order will not be deleted).</p>
                         </div>
                         <div class="modal-footer">
                             <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
-                            <button type="submit" class="btn btn-default" onclick="delete_basket([% basketno %],[% booksellerid %],0); return false;" >Delete basket and orders</button>
+                            <button type="submit" class="btn btn-default" onclick="delete_basket([% basketno %],[% booksellerid %],0); return false;">Delete basket and orders</button>
                             <button type="submit" class="btn btn-default" onclick="delete_basket([% basketno %],[% booksellerid %],1); return false;">Delete basket, orders and records</button>
                         </div>
                     [% END %]
@@ -198,7 +198,27 @@
     [% ELSE %]
         [% IF ( delete_confirmed ) %]
             <h3>Basket deleted</h3>
+            [% IF (cannotdelbiblios) %]
+                <div class="dialog alert">
+                    <p><strong>Warning:</strong></p>
+                    <p><strong>The following records could not be deleted:</strong></p>
+                    <ul>
+                    [% FOREACH cannotdelbiblio IN cannotdelbiblios %]
+                        <li><a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% cannotdelbiblio.biblionumber %]">[% cannotdelbiblio.title |html %]</a> by [% cannotdelbiblio.author %]:
+                            <ul>
+                            [% IF (cannotdelbiblio.itemcount) %]<li>[% cannotdelbiblio.itemcount %] item(s) attached.</li>[% END %]
+                            [% IF (cannotdelbiblio.subscriptions) %]<li>[% cannotdelbiblio.subscriptions %] subscription(s) attached.</li>[% END %]
+                            [% IF (cannotdelbiblio.countbiblio) %]<li>[% cannotdelbiblio.countbiblio %] order(s) attached.</li>[% END %]
+                            [% IF (cannotdelbiblio.othererror) %]<li>Unknown error.</li>[% END %]
+                            </ul>
+                        </li>
+                    [% END %]
+                    </ul>
+                </div>
+                <a href="booksellers.pl">Click here to go back to booksellers page</a>
+            [% ELSE %]
             <META HTTP-EQUIV=Refresh CONTENT="0; url=booksellers.pl">
+            [% END %]
         [% ELSE %]
         <h1>[% UNLESS ( basketno ) %]New [% END %][% IF ( delete_confirm ) %]Delete [% END %]Basket [% basketname|html %] ([% basketno %]) for <a href="supplier.pl?booksellerid=[% booksellerid %]">[% name|html %]</a></h1>
         [% IF ( delete_confirm ) %]
-- 
1.7.9.5