Bug 29290: Add GET /biblios/:biblio_id/checkouts
[koha-ffzg.git] / Koha / Objects.pm
index 6a272cf..3ee0a55 100644 (file)
@@ -19,11 +19,13 @@ package Koha::Objects;
 
 use Modern::Perl;
 
-use Carp;
+use Carp qw( carp );
 use List::MoreUtils qw( none );
 use Class::Inspector;
 
 use Koha::Database;
+use Koha::Exceptions::Object;
+use Koha::DateUtils qw( dt_from_string );
 
 =head1 NAME
 
@@ -118,9 +120,21 @@ sub find_or_create {
     return $object;
 }
 
-=head3 Koha::Objects->search();
+=head3 search
 
-my @objects = Koha::Objects->search($params);
+    # list context
+    my @objects = Koha::Objects->search([$params, $attributes]);
+    # scalar context
+    my $objects = Koha::Objects->search([$params, $attributes]);
+    while (my $object = $objects->next) {
+        do_stuff($object);
+    }
+
+This B<instantiates> the I<Koha::Objects> class, and generates a resultset
+based on the query I<$params> and I<$attributes> that are passed (like in DBIC).
+
+In B<list context> it returns an array of I<Koha::Object> objects.
+In B<scalar context> it returns an iterator.
 
 =cut
 
@@ -196,7 +210,8 @@ sub delete {
 
 =head3 update
 
-    $object->update( $fields, [ { no_triggers => 0/1 } ] );
+    my $objects = Koha::Objects->new; # or Koha::Objects->search
+    $objects->update( $fields, [ { no_triggers => 0/1 } ] );
 
 This method overloads the DBIC inherited one so if code-level triggers exist
 (through the use of an overloaded I<update> or I<store> method in the Koha::Object
@@ -212,6 +227,11 @@ catch wrong uses as well.
 sub update {
     my ($self, $fields, $options) = @_;
 
+    Koha::Exceptions::Object::NotInstantiated->throw(
+        method => 'update',
+        class  => $self
+    ) unless ref $self;
+
     my $no_triggers = $options->{no_triggers};
 
     if (
@@ -233,6 +253,49 @@ sub update {
     return $self->_resultset->update($fields);
 }
 
+=head3 filter_by_last_update
+
+my $filtered_objects = $objects->filter_by_last_update
+
+days exclusive by default (will be inclusive if days_inclusive is passed and set)
+from inclusive
+to   inclusive
+
+=cut
+
+sub filter_by_last_update {
+    my ( $self, $params ) = @_;
+    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
+    my $days_inclusive = $params->{days_inclusive} || 0;
+    my $conditions;
+    Koha::Exceptions::MissingParameter->throw(
+        "Missing mandatory parameter: days or from or to")
+      unless exists $params->{days}
+          or exists $params->{from}
+          or exists $params->{to};
+
+    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+    if ( exists $params->{days} ) {
+        my $dt = Koha::DateUtils::dt_from_string();
+        my $operator = $days_inclusive ? '<=' : '<';
+        $conditions->{$operator} = $dtf->format_date( $dt->subtract( days => $params->{days} ) );
+    }
+    if ( exists $params->{from} ) {
+        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
+        $conditions->{'>='} = $dtf->format_date( $from );
+    }
+    if ( exists $params->{to} ) {
+        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
+        $conditions->{'<='} = $dtf->format_date( $to );
+    }
+
+    return $self->search(
+        {
+            $timestamp_column_name => $conditions
+        }
+    );
+}
+
 =head3 single
 
 my $object = Koha::Objects->search({}, { rows => 1 })->single
@@ -312,10 +375,12 @@ an iterator).
 sub empty {
     my ($self) = @_;
 
-    unless (ref($self)) {
-        $self = $self->new;
-    }
+    Koha::Exceptions::Object::NotInstantiated->throw(
+        method => 'empty',
+        class  => $self
+    ) unless ref $self;
 
+    $self = $self->search(\'0 = 1');
     $self->_resultset()->set_cache([]);
 
     return $self;