Bug 8682 - Renewals do not appear on quick slips
[koha.git] / C4 / Bookseller.pm
index 4ad42cb..c8389a3 100644 (file)
@@ -24,7 +24,7 @@ use warnings;
 use base qw( Exporter );
 
 # set the version for version checking
-our $VERSION   = 4.01;
+our $VERSION   = 3.07.00.049;
 our @EXPORT_OK = qw(
   GetBookSeller GetBooksellersWithLateOrders GetBookSellerFromId
   ModBookseller
@@ -63,9 +63,12 @@ aqbooksellers table in the Koha database.
 sub GetBookSeller {
     my $searchstring = shift;
     $searchstring = q{%} . $searchstring . q{%};
-    my $query =
-'select aqbooksellers.*, count(*) as basketcount from aqbooksellers left join aqbasket '
-      . 'on aqbasket.booksellerid = aqbooksellers.id where name lIke ? group by aqbooksellers.id order by name';
+    my $query = "
+        SELECT aqbooksellers.*, count(*) AS basketcount
+        FROM aqbooksellers
+        LEFT JOIN aqbasket ON aqbasket.booksellerid = aqbooksellers.id
+        WHERE name LIKE ? GROUP BY aqbooksellers.id ORDER BY name
+    ";
 
     my $dbh           = C4::Context->dbh;
     my $sth           = $dbh->prepare($query);
@@ -84,6 +87,9 @@ sub GetBookSellerFromId {
         ( $vendor->{basketcount} ) = $dbh->selectrow_array(
             'SELECT count(*) FROM aqbasket where booksellerid = ?',
             {}, $id );
+        ( $vendor->{subscriptioncount} ) = $dbh->selectrow_array(
+            'SELECT count(*) FROM subscription WHERE aqbooksellerid = ?',
+            {}, $id );
     }
     return $vendor;
 }
@@ -99,19 +105,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 - COALESCE(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;
@@ -141,20 +176,22 @@ sub AddBookseller {
         INSERT INTO aqbooksellers
             (
                 name,      address1,      address2,   address3,      address4,
-                postal,    phone,         fax,        url,           contact,
+                postal,    phone,         accountnumber,   fax,      url,           
+                contact,
                 contpos,   contphone,     contfax,    contaltphone,  contemail,
                 contnotes, active,        listprice,  invoiceprice,  gstreg,
                 listincgst,invoiceincgst, gstrate,    discount,
                 notes
             )
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) |
       ;
     my $sth = $dbh->prepare($query);
     $sth->execute(
         $data->{'name'},         $data->{'address1'},
         $data->{'address2'},     $data->{'address3'},
         $data->{'address4'},     $data->{'postal'},
-        $data->{'phone'},        $data->{'fax'},
+        $data->{'phone'},        $data->{'accountnumber'},
+        $data->{'fax'},
         $data->{'url'},          $data->{'contact'},
         $data->{'contpos'},      $data->{'contphone'},
         $data->{'contfax'},      $data->{'contaltphone'},
@@ -192,18 +229,19 @@ sub ModBookseller {
     my $dbh    = C4::Context->dbh;
     my $query  = 'UPDATE aqbooksellers
         SET name=?,address1=?,address2=?,address3=?,address4=?,
-            postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
+            postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?,
             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(
         $data->{'name'},         $data->{'address1'},
         $data->{'address2'},     $data->{'address3'},
         $data->{'address4'},     $data->{'postal'},
-        $data->{'phone'},        $data->{'fax'},
+        $data->{'phone'},        $data->{'accountnumber'},
+        $data->{'fax'},
         $data->{'url'},          $data->{'contact'},
         $data->{'contpos'},      $data->{'contphone'},
         $data->{'contfax'},      $data->{'contaltphone'},
@@ -212,7 +250,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;
 }