Bug 7291: Adds new field aqbooksellers.deliverytime
authorJonathan Druart <jonathan.druart@biblibre.com>
Mon, 13 Feb 2012 09:05:17 +0000 (10:05 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Tue, 3 Apr 2012 16:19:46 +0000 (18:19 +0200)
New field deliverytime in aqbooksellers table. It is an estimated
delivery time for orders (in days).
You can set this delay on the supplier modification page.
It is used in the late orders search.

The order estimated date is the aqbasket.closedate +
aqbooksellers.deliverytime

If you set a delay, the query check if closedate <= today - delay

If you set a "delivery time from" and a "delivery time to", the query check if
$delivery_time_from <= aqbooksellers.deliverytime is not NULL and if
closedate + deliverytime >= $delivery_time_to
if there is not a time_to then $delivery_time_to = the current date.

C4/Acquisition.pm
C4/Bookseller.pm
acqui/basket.pl
acqui/lateorders.pl
acqui/supplier.pl
acqui/updatesupplier.pl
installer/data/mysql/kohastructure.sql
installer/data/mysql/updatedatabase.pl
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/lateorders.tt
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tt

index 4fd5cc2..bebc3a8 100644 (file)
@@ -1473,13 +1473,15 @@ sub GetLateOrders {
     my $delay      = shift;
     my $supplierid = shift;
     my $branch     = shift;
+    my $estimateddeliverydatefrom = shift;
+    my $estimateddeliverydateto = shift;
 
     my $dbh = C4::Context->dbh;
 
     #BEWARE, order of parenthesis and LEFT JOIN is important for speed
     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
 
-    my @query_params = ($delay);       # delay is the first argument regardless
+    my @query_params = ();
     my $select = "
     SELECT aqbasket.basketno,
         aqorders.ordernumber,
@@ -1495,6 +1497,7 @@ sub GetLateOrders {
         biblio.author, biblio.title,
         biblioitems.publishercode AS publisher,
         biblioitems.publicationyear,
+        ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
     ";
     my $from = "
     FROM
@@ -1508,6 +1511,7 @@ sub GetLateOrders {
             OR datereceived IS NULL
             OR aqorders.quantityreceived < aqorders.quantity
         )
+        AND aqbasket.closedate IS NOT NULL
         AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
     ";
     my $having = "";
@@ -1515,9 +1519,12 @@ sub GetLateOrders {
         $select .= "
         aqorders.quantity - IFNULL(aqorders.quantityreceived,0)                 AS quantity,
         (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
-        DATEDIFF(CURDATE( ),closedate) AS latesince
+        DATEDIFF(CAST(now() AS date),closedate) AS latesince
         ";
-        $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
+        if ( defined $delay ) {
+            $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
+            push @query_params, $delay;
+        }
         $having = "
         HAVING quantity          <> 0
             AND unitpricesupplier <> 0
@@ -1528,9 +1535,12 @@ sub GetLateOrders {
         $select .= "
                 aqorders.quantity                AS quantity,
                 aqorders.quantity * aqorders.rrp AS subtotal,
-                (CURDATE - closedate)            AS latesince
+                (CAST(now() AS date) - closedate)            AS latesince
         ";
-        $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
+        if ( defined $delay ) {
+            $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
+            push @query_params, $delay;
+        }
     }
     if (defined $supplierid) {
         $from .= ' AND aqbasket.booksellerid = ? ';
@@ -1540,6 +1550,18 @@ sub GetLateOrders {
         $from .= ' AND borrowers.branchcode LIKE ? ';
         push @query_params, $branch;
     }
+    if ( defined $estimateddeliverydatefrom ) {
+        $from .= '
+            AND aqbooksellers.deliverytime IS NOT NULL
+            AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
+        push @query_params, $estimateddeliverydatefrom;
+    }
+    if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) {
+        $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
+        push @query_params, $estimateddeliverydateto;
+    } elsif ( defined $estimateddeliverydatefrom ) {
+        $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
+    }
     if (C4::Context->preference("IndependantBranches")
             && C4::Context->userenv
             && C4::Context->userenv->{flags} != 1 ) {
index f14bd02..3fded79 100644 (file)
@@ -102,19 +102,48 @@ Searches for suppliers with late orders.
 =cut
 
 sub GetBooksellersWithLateOrders {
-    my $delay = shift;
-    my $dbh   = C4::Context->dbh;
-
-    # TODO delay should be verified
-    my $query_string =
-      "SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
-    FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
-    LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
-    WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
-    AND (datereceived = '' OR datereceived IS NULL))";
-
-    my $sth = $dbh->prepare($query_string);
-    $sth->execute;
+    my ( $delay, $branch, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;    # FIXME: Branch argument unused.
+    my $dbh = C4::Context->dbh;
+
+    # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
+    # should be tested with other DBMs
+
+    my $strsth;
+    my @query_params = ();
+    my $dbdriver = C4::Context->config("db_scheme") || "mysql";
+    $strsth = "
+        SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
+        FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
+        LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
+        WHERE
+            ( datereceived = ''
+            OR datereceived IS NULL
+            OR aqorders.quantityreceived < aqorders.quantity
+            )
+            AND aqorders.rrp <> 0
+            AND aqorders.ecost <> 0
+            AND aqorders.quantity - IFNULL(aqorders.quantityreceived,0) <> 0
+            AND aqbasket.closedate IS NOT NULL
+    ";
+    if ( defined $delay ) {
+        $strsth .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) ";
+        push @query_params, $delay;
+    }
+    if ( defined $estimateddeliverydatefrom ) {
+        $strsth .= '
+            AND aqbooksellers.deliverytime IS NOT NULL
+            AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
+        push @query_params, $estimateddeliverydatefrom;
+    }
+    if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) {
+        $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
+        push @query_params, $estimateddeliverydateto;
+    } elsif ( defined $estimateddeliverydatefrom ) {
+        $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
+    }
+
+    my $sth = $dbh->prepare($strsth);
+    $sth->execute( @query_params );
     my %supplierlist;
     while ( my ( $id, $name ) = $sth->fetchrow ) {
         $supplierlist{$id} = $name;
@@ -201,7 +230,7 @@ sub ModBookseller {
             contphone=?,contfax=?,contaltphone=?,contemail=?,
             contnotes=?,active=?,listprice=?, invoiceprice=?,
             gstreg=?,listincgst=?,invoiceincgst=?,
-            discount=?,notes=?,gstrate=?
+            discount=?,notes=?,gstrate=?,deliverytime=?
         WHERE id=?';
     my $sth = $dbh->prepare($query);
     $sth->execute(
@@ -218,7 +247,9 @@ sub ModBookseller {
         $data->{'invoiceprice'}, $data->{'gstreg'},
         $data->{'listincgst'},   $data->{'invoiceincgst'},
         $data->{'discount'},     $data->{'notes'},
-        $data->{'gstrate'},      $data->{'id'}
+        $data->{'gstrate'},
+        $data->{deliverytime},
+        $data->{'id'}
     );
     return;
 }
index 4386125..de66891 100755 (executable)
@@ -34,6 +34,7 @@ use C4::Biblio;
 use C4::Members qw/GetMember/;  #needed for permissions checking for changing basketgroup of a basket
 use C4::Items;
 use C4::Suggestions;
+use Date::Calc qw/Add_Delta_Days/;
 
 =head1 NAME
 
@@ -213,6 +214,15 @@ if ( $op eq 'delete_confirm' ) {
         }
         unshift( @$basketgroups, \%emptygroup );
     }
+
+    # if the basket is closed, calculate estimated delivery date
+    my $estimateddeliverydate;
+    if( $basket->{closedate} ) {
+        my ($year, $month, $day) = ($basket->{closedate} =~ /(\d+)-(\d+)-(\d+)/);
+        ($year, $month, $day) = Add_Delta_Days($year, $month, $day, $bookseller->{deliverytime});
+        $estimateddeliverydate = "$year-$month-$day";
+    }
+
     # if new basket, pre-fill infos
     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
@@ -367,6 +377,7 @@ my $total_est_gste;
         authorisedby         => $basket->{authorisedby},
         authorisedbyname     => $basket->{authorisedbyname},
         closedate            => $basket->{closedate},
+        estimateddeliverydate=> $estimateddeliverydate,
         active               => $bookseller->{'active'},
         booksellerid         => $bookseller->{'id'},
         name                 => $bookseller->{'name'},
index 86b1300..21ee186 100755 (executable)
@@ -66,14 +66,14 @@ my ($template, $loggedinuser, $cookie) = get_template_and_user({
 
 my $booksellerid = $input->param('booksellerid') || undef; # we don't want "" or 0
 my $delay      = $input->param('delay');
+my $estimateddeliverydatefrom = $input->param('estimateddeliverydatefrom');
+my $estimateddeliverydateto   = $input->param('estimateddeliverydateto');
 my $branch     = $input->param('branch');
 my $op         = $input->param('op');
 
 my @errors = ();
-$delay = 30 unless defined $delay;
-unless ($delay =~ /^\d{1,3}$/) {
-       push @errors, {delay_digits => 1, bad_delay => $delay};
-       $delay = 30;    #default value for delay
+if ( defined $delay and not $delay =~ /^\d{1,3}$/ ) {
+    push @errors, {delay_digits => 1, bad_delay => $delay};
 }
 
 if ($op and $op eq "send_alert"){
@@ -92,7 +92,13 @@ if ($op and $op eq "send_alert"){
     }
 }
 
-my %supplierlist = GetBooksellersWithLateOrders($delay);
+my %supplierlist = GetBooksellersWithLateOrders(
+    $delay,
+    $branch,
+    C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
+    C4::Dates->new($estimateddeliverydateto)->output("iso")
+);
+
 my (@sloopy);  # supplier loop
 foreach (keys %supplierlist){
        push @sloopy, (($booksellerid and $booksellerid eq $_ )            ?
@@ -104,7 +110,13 @@ $template->param(SUPPLIER_LOOP => \@sloopy);
 $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid);
 $template->param(booksellerid=>$booksellerid) if ($booksellerid);
 
-my @lateorders = GetLateOrders($delay,$booksellerid,$branch);
+my @lateorders = GetLateOrders(
+    $delay,
+    $booksellerid,
+    $branch,
+    C4::Dates->new($estimateddeliverydatefrom)->output("iso"),
+    C4::Dates->new($estimateddeliverydateto)->output("iso")
+);
 
 my $total;
 foreach (@lateorders){
@@ -122,7 +134,10 @@ $template->param(ERROR_LOOP => \@errors) if (@errors);
 $template->param(
        lateorders => \@lateorders,
        delay => $delay,
+    estimateddeliverydatefrom => $estimateddeliverydatefrom,
+    estimateddeliverydateto   => $estimateddeliverydateto,
        total => $total,
        intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
+    DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
 );
 output_html_with_http_headers $input, $cookie, $template->output;
index 6746d8d..0e2ab40 100755 (executable)
@@ -103,6 +103,7 @@ if ( $op eq 'display' ) {
         listincgst    => $supplier->{'listincgst'},
         invoiceincgst => $supplier->{'invoiceincgst'},
         discount      => $supplier->{'discount'},
+        deliverytime  => $supplier->{deliverytime},
         invoiceprice  => $supplier->{'invoiceprice'},
         listprice     => $supplier->{'listprice'},
         GST           => $tax_rate,
@@ -170,6 +171,7 @@ if ( $op eq 'display' ) {
         invoiceincgst => $supplier->{'invoiceincgst'},
         gstrate       => $gstrate,
         discount      => $supplier->{'discount'},
+        deliverytime  => $supplier->{deliverytime},
         loop_currency => $loop_currency,
         GST           => $tax_rate,
         enter         => 1,
index 844e504..3dc6964 100755 (executable)
@@ -107,6 +107,7 @@ if ($gstrate eq '') {
     $data{'gstrate'} = $input->param('gstrate')/100;
 }
 $data{'discount'}=$input->param('discount');
+$data{deliverytime} = $input->param('deliverytime');
 $data{'active'}=$input->param('status');
 if($data{'name'}) {
        if ($data{'id'}){
index fda0bac..70ddc23 100644 (file)
@@ -2602,6 +2602,7 @@ CREATE TABLE `aqbooksellers` ( -- information about the vendors listed in acquis
   `gstrate` decimal(6,4) default NULL, -- the tax rate the library is charged
   `discount` float(6,4) default NULL, -- discount offered on all items ordered from this vendor
   `fax` varchar(50) default NULL, -- vendor fax number
+  `deliverytime` int(11) default NULL, -- vendor delivery time
   PRIMARY KEY  (`id`),
   KEY `listprice` (`listprice`),
   KEY `invoiceprice` (`invoiceprice`),
index c297677..3c570c5 100755 (executable)
@@ -5139,6 +5139,13 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
     SetVersion($DBversion);
 }
 
+$DBversion = "3.07.00.XXX";
+if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
+    $dbh->do("ALTER TABLE aqbooksellers ADD deliverytime INT DEFAULT NULL");
+    print "Upgrade to $DBversion done (Add deliverytime field in aqbooksellers table)";
+    SetVersion($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 DropAllForeignKeys($table)
index 401e484..7b93de6 100644 (file)
                 [% IF ( authorisedbyname ) %]<li><span class="label">Managed by:</span>  [% authorisedbyname %]</li>[% END %]
                 [% IF ( creationdate ) %]<li><span class="label">Opened on:</span>  [% creationdate | $KohaDates %]</li>[% END %]
                 [% IF ( closedate ) %]<li><span class="label">Closed on:</span> [% closedate | $KohaDates %]</li>[% END %]
+                [% IF ( estimateddeliverydate ) %]<li><span class="label">Estimated delivery date:</span> [% estimateddeliverydate | $KohaDates  %]</li>[% END %]
 
                 </ol>
                 </div>
index 33473f6..9305ae5 100644 (file)
@@ -1,7 +1,9 @@
+[% USE KohaDates %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Acquisitions &rsaquo; Late orders</title>
 [% INCLUDE 'doc-head-close.inc' %]
 <script type="text/javascript" src="[% themelang %]/lib/jquery/plugins/jquery.checkboxes.min.js"></script>
+[% INCLUDE 'calendar.inc' %]
 <script type="text/javascript">
 //<![CDATA[
 $(document).ready(function() {
@@ -60,6 +62,7 @@ $(document).ready(function() {
     <table id="late_orders">
         <tr>
             <th>Order Date</th>
+            <th>Estimated delivery date</th>
             <th>Vendor</th>
             <th>Information</th>
             <th>Total cost</th>
@@ -80,6 +83,11 @@ $(document).ready(function() {
                 [% lateorder.orderdate %]
                 ([% lateorder.latesince %] days)
             </td>
+            <td>
+                [% IF ( lateorder.estimateddeliverydate ) %]
+                    [% lateorder.estimateddeliverydate | $KohaDates  %]
+                [% END %]
+            </td>
             <td>
                 [% lateorder.supplier %]
             </td>
@@ -117,7 +125,7 @@ $(document).ready(function() {
         [% END %]
         <tr> 
             <th>Total</th>
-            <th colspan="2">&nbsp;</th>
+            <th colspan="3">&nbsp;</th>
             <th>[% total %]</th>
             <th>&nbsp;</th>
             <th>&nbsp;</th>
@@ -140,7 +148,38 @@ $(document).ready(function() {
 [% FOREACH ERROR_LOO IN ERROR_LOOP %]
 [% IF ( ERROR_LOO.delay_digits ) %]<p class="error">The number of days ([% ERROR_LOO.bad_delay %]) must be a number between 0 and 999.</p>[% END %]
 [% END %]
-<ol><li><label for="delay">Order date:</label><input size="3" maxlength="3" id="delay" type="text" name="delay" value="[% delay %]" /> days ago</li>
+<ol>
+    <li><label for="delay">Order date:</label><input size="3" maxlength="3" id="delay" type="text" name="delay" value="[% delay %]" /> days ago</li>
+    <li><label for="delay">Estimated Delivery date from: </label>
+        <input type="text" size="10" id="estimateddeliverydatefrom" name="estimateddeliverydatefrom" value="[% estimateddeliverydatefrom %]" />
+        <img src="[% themelang %]/lib/calendar/cal.gif" id="openCalendarFrom" style="cursor: pointer;" alt="Show Calendar" />
+        <div class="hint">[% INCLUDE 'date-format.inc' %]</div>
+    </li>
+    <li><label for="delay">To: </label>
+        <input type="text" size="10" id="estimateddeliverydateto" name="estimateddeliverydateto" value="[% estimateddeliverydateto %]" />
+        <img src="[% themelang %]/lib/calendar/cal.gif" id="openCalendarTo" style="cursor: pointer;" alt="Show Calendar" />
+        <script type="text/javascript">
+            // return true if the date is blocked.
+            function disable_from(date) {var limit = get_Calendar_limit(date,'estimateddeliverydatefrom'); return (limit && limit < date);}
+            function disable_to(date) {var limit = get_Calendar_limit(date,'estimateddeliverydateto'); return (limit && limit < date);}
+            Calendar.setup({
+                inputField : "estimateddeliverydatefrom",
+                ifFormat : "[% DHTMLcalendar_dateformat %]",
+                button : "openCalendarFrom",
+                disableFunc : disable_from,
+                dateStatusFunc : disable_from
+            });
+            Calendar.setup({
+                inputField : "estimateddeliverydateto",
+                ifFormat : "[% DHTMLcalendar_dateformat %]",
+                button : "openCalendarTo",
+                disableFunc : disable_to,
+                dateStatusFunc : disable_to
+             });
+        </script>
+        <div class="hint">[% INCLUDE 'date-format.inc' %]</div>
+    </li>
+
        <li><label for="booksellerid">Vendor:</label>
                <select id="booksellerid" size="1" tabindex="" name="booksellerid">
                        <option value=""/>
index 6ace8da..b671f48 100644 (file)
@@ -138,6 +138,10 @@ if (f.company.value == "") {
             <li><label for="discount">Discount</label>
                 <input type="text" size="6" id="discount" name="discount" value="[% discount %]" />%</li>
             <li><label for="gstrate">Tax rate</label><input type="text" name="gstrate" id="gstrate" size="6" value="[% gstrate %]"/>% (leave blank for default tax of [% default_gst_rate %]%)</li>
+            <li>
+                <label for="deliverytime">Delivery time</label>
+                <input type="text" size="2" id="deliverytime" name="deliverytime" value="[% deliverytime %]" /> days
+            </li>
             <li><label for="notes">Notes</label>
                 <textarea cols="40" rows="4" id="notes" name="notes" >[% notes %]</textarea></li></ol>
         </fieldset>
@@ -198,6 +202,10 @@ if (f.company.value == "") {
                 [% discount %] %</p>
             <p><strong>Tax rate: </strong>
                 [% GST %]%[% UNLESS ( default_tax ) %] (default)[% END %]</p>
+            [% IF deliverytime.defined %]
+                <p><strong>Delivery time: </strong>
+                    [% deliverytime %] days</p>
+            [% END %]
             [% IF ( notes ) %]<p><strong>Notes: </strong>
                 [% notes %]</p>[% END %]
         </div>