package LATMOS::Accounts::Bases::Sql::Userstatus;

use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases::Sql::Employment);

our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Accounts::Bases::Sql::Userstatus - Virtual object about only futur employment

=head1 DESCRIPTION

This module handle object for office address, allowing to support multiple
address per people.

=cut

sub list {
    my ($class, $base) = @_;

    my $sth = $base->db->prepare_cached(
        sprintf(
            q{select %s.%s as k from %s
              join "user" on %s."user" = "user".name
              where (%s.lastday is null or %s.lastday >= now() - '1 days'::interval)
              %s %s
              order by %s.%s
            },
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            ($base->{wexported} ? '' : 'and "user".exported = true'),
            ($base->{wexported} ? '' : 'and ' .
                $base->db->quote_identifier($class->_object_table) . '.exported = true'),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
        )
    );
    $sth->execute;
    my @keys;
    while(my $res = $sth->fetchrow_hashref) {
        push(@keys, $res->{k});
    }
    @keys
}

sub list_from_rev {
    my ($class, $base, $rev) = @_;

    my $sth = $base->db->prepare_cached(
        sprintf(
            q{select %s.%s as k from %s
              join "user" on %s."user" = "user".name
              where (%s.lastday is null or %s.lastday >= now() - '1 days'::interval)
              and rev > ?
              %s %s
              order by %s.%s
            },
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_object_table),
            ($base->{wexported} ? '' : 'and "user".exported = true'),
            ($base->{wexported} ? '' : 'and ' .
                $base->db->quote_identifier($class->_object_table) . '.exported = true'),
            $base->db->quote_identifier($class->_object_table),
            $base->db->quote_identifier($class->_key_field),
        )
    );
    $sth->execute($rev);
    my @keys;
    while(my $res = $sth->fetchrow_hashref) {
        push(@keys, $res->{k});
    }
    @keys
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::Sql>

L<LATMOS::Accounts::Bases::Sql::Address>

=head1 AUTHOR

Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
