MT 1487 : Ability to cancel orders when receiving shipments
authorMatthias Meusburger <matthias.meusburger@biblibre.com>
Wed, 5 Aug 2009 13:37:22 +0000 (15:37 +0200)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Wed, 30 Sep 2009 09:30:30 +0000 (11:30 +0200)
Related items are also suppressed, as well as the record if there are no more items associed with it

C4/Acquisition.pm
acqui/parcel.pl
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tmpl

index 59cc2a5..c4fc0ca 100644 (file)
@@ -53,6 +53,7 @@ BEGIN {
                &GetContracts &GetContract
 
         &GetOrderFromItemnumber
+        &GetItemnumbersFromOrder
        );
 }
 
@@ -80,6 +81,22 @@ sub GetOrderFromItemnumber {
 
 }
 
+# Returns the itemnumber(s) associated with the ordernumber given in parameter 
+sub GetItemnumbersFromOrder {
+    my ($ordernumber) = @_;
+    my $dbh          = C4::Context->dbh;
+    my $query        = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($ordernumber);
+    my @tab;
+
+    while (my $order = $sth->fetchrow_hashref) {
+       push @tab, $order->{'itemnumber'}; 
+    }
+
+    return @tab;
+
+}
 
 
 
index d2296ec..3a161eb 100755 (executable)
@@ -54,8 +54,10 @@ To filter the results list on this given date.
 
 use C4::Auth;
 use C4::Acquisition;
+use C4::Budgets;
 use C4::Bookseller;
 use C4::Biblio;
+use C4::Items;
 use CGI;
 use C4::Output;
 use C4::Dates qw/format_date format_date_in_iso/;
@@ -90,6 +92,46 @@ my ($template, $loggedinuser, $cookie)
                  debug => 1,
 });
 
+my $action = $input->param('action');
+my $ordernumber = $input->param('ordernumber');
+my $biblionumber = $input->param('biblionumber');
+
+# If canceling an order
+if ($action eq "cancelorder") {
+
+    my $error_delitem;
+    my $error_delbiblio;
+
+    # We delete the order
+    DelOrder($biblionumber, $ordernumber);
+
+    # We delete all the items related to this order
+    my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
+    foreach (@itemnumbers) {
+       my $delcheck = DelItemCheck(C4::Context->dbh, $biblionumber, $_);
+       # (should always success, as no issue should exist on item on order)
+       if ($delcheck != 1) { $error_delitem = 1; }
+    }
+
+    # We get the number of remaining items
+    my $itemcount = GetItemsCount($biblionumber);
+    
+    # If there are no items left,
+    if ($itemcount eq 0) {
+       # We delete the record
+       $error_delbiblio = DelBiblio($biblionumber);    
+    }
+
+    if ($error_delitem || $error_delbiblio) {
+       warn $error_delitem;
+       warn $error_delbiblio;
+       if ($error_delitem)   { $template->param(error_delitem => 1); }
+       if ($error_delbiblio) { $template->param(error_delbiblio => 1); }
+    } else {
+       $template->param(success_delorder => 1);
+    }
+}
+
 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
 if( scalar(@rcv_err) ) {
        my $cnt=0;
index c624f5b..1dfaee7 100644 (file)
             Receive orders from <!-- TMPL_VAR NAME="name" -->
         <!-- /TMPL_IF -->
     </h1>
+
+    <!-- TMPL_IF NAME="success_delorder" -->
+    <div class="dialog message">The order has been successfully canceled.</div>
+    <!-- TMPL_ELSE -->
+       <!-- TMPL_IF NAME="error_delitem" -->
+           <div class="dialog alert">The order has been canceled, although one or more items could not have been deleted.</div>
+       <!-- /TMPL_IF -->
+       <!-- TMPL_IF NAME="error_delbiblio" -->
+           <div class="dialog alert">The order has been canceled, although the record has not been deleted.</div>
+       <!-- /TMPL_IF -->
+    <!-- /TMPL_IF -->
+
 <div id="acqui_receive_summary">
     <p>Invoice number: <!-- TMPL_VAR NAME="invoice" --></p>
     <p>Received by: <!-- TMPL_VAR NAME="loggedinusername" --></p>
             <th>Still on order</th>
             <th>Unit cost</th>
             <th>Order cost</th>
-            <th>Receive</th>
+            <th>Order</th>
         </tr>
     </thead>
     <tbody class="filterclass">
                 <td><!-- TMPL_VAR NAME="quantrem" --> / <!-- TMPL_VAR NAME="quantity" --></td>
                 <td><!-- TMPL_VAR NAME="ecost" --></td>
                 <td><!-- TMPL_VAR NAME="ordertotal" --></td>
-                               <td><a href="orderreceive.pl?ordernumber=<!-- TMPL_VAR NAME="ordernumber" -->&amp;datereceived=<!-- TMPL_VAR NAME="invoicedatereceived" -->&amp;invoice=<!-- TMPL_VAR NAME="invoice" -->&amp;gst=<!-- TMPL_VAR NAME="gst" -->&amp;freight=<!-- TMPL_VAR NAME="freight" -->&amp;supplierid=<!-- TMPL_VAR NAME="supplierid" -->">Receive</a></td>
+                               <td>
+                                   <a href="orderreceive.pl?ordernumber=<!-- TMPL_VAR NAME="ordernumber" -->&amp;datereceived=<!-- TMPL_VAR NAME="invoicedatereceived" -->&amp;invoice=<!-- TMPL_VAR NAME="invoice" -->&amp;gst=<!-- TMPL_VAR NAME="gst" -->&amp;freight=<!-- TMPL_VAR NAME="freight" -->&amp;supplierid=<!-- TMPL_VAR NAME="supplierid" -->">Receive</a> / 
+                                   <a href="parcel.pl?type=intra&amp;ordernumber=<!-- TMPL_VAR NAME="ordernumber" -->&amp;biblionumber=<!-- TMPL_VAR NAME="biblionumber" -->&amp;action=cancelorder&amp;supplierid=<!-- TMPL_VAR NAME="supplierid" -->&amp;datereceived=<!-- TMPL_VAR NAME="invoicedatereceived" -->&amp;invoice=<!-- TMPL_VAR NAME="invoice" -->" onclick="return confirm(_('Are you sure you want to cancel this order?'));">Cancel</a>
+                               </td>
             </tr>
         <!-- /TMPL_LOOP -->
     </tbody>