<commit_before>=pod
Lenio - Web-based Facilities Management Software
Copyright (C) 2016 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package Lenio::Calendar;

use Log::Report;
use Moo;
use MooX::Types::MooseLike::Base qw/:all/;
use MooX::Types::MooseLike::DateTime qw/DateAndTime/;

has schema => (
    is       => 'ro',
    required => 1,
);

has from => (
    is  => 'ro',
    isa => DateAndTime,
);

has to => (
    is  => 'ro',
    isa => DateAndTime,
);

has dateformat => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

# Allow override of today for tests
has today => (
    is      => 'ro',
    isa     => DateAndTime,
    default => sub { DateTime->now },
);

has site => (
    is  => 'ro',
);

has multiple_sites => (
    is  => 'ro',
    isa => Bool,
);

has contractors => (
    is     => 'ro',
    isa    => ArrayRef,
    coerce => sub {
        my $value = shift;
        $value ||= [];
        if (ref $value eq 'HASH')
        {
            $value = [keys %$value];
        }
        $value;
    },
);

has login => (
    is  => 'ro',
);

has task_summary => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_task_summary
{   my $self = shift;
    my %options = (
        site_id => $self->site->id,
    );
    $options{global} = 1 if $self->login->is_admin;
    [$self->schema->resultset('Task')->summary(%options, onlysite => 1)];
}

# The task summary before the start date. This is used to find out
# the previous completed task dates, to fill in missing tasks.
has task_summary_previous => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_task_summary_previous
{   my $self = shift;
    my %options = (
        to             => $self->from,
        site_id        => $self->site->id,
        contractor_ids => $self->contractors,
    );
    $options{global} = 1 if $self->login->is_admin;
    [$self->schema->resultset('Task')->summary(%options, onlysite => 1)];
}

has ticket_summary => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_ticket_summary
{   my $self = shift;
    [
        $self->schema->resultset('Ticket')->summary(
            site_id        => $self->site->id,
            contractor_ids => $self->contractors,
            from           => $self->from,
            to             => $self->to,
            login          => $self->login,
        )
    ];
}

sub tasks
{   my $self = shift;

    my @calendar1; my @calendar2;

    my $planned;

    my $done; my $lastcomp;
    # First add all the tickets in this time period. 
    foreach my $ticket (@{$self->ticket_summary})
    {
        my $task_id = $ticket->task_id;
        push @calendar1, $self->_cal_item(
            name        => $ticket->name,
            description => $ticket->description,
            task_id     => $task_id,
            ticket_id   => $ticket->id,
            completed   => $ticket->completed,
            planned     => $ticket->planned,
            provisional => $ticket->provisional,
        );
        if ($task_id)
        {
            # Take the completed date if it exists, otherwise take the planned
            # date, otherwise take the provisional date. Future items and
            # warnings will be mapped out from this. If the planned date is in
            # the past, however, ignore it and plonk any overdue dates wherever
            # they would have been (otherwise they won't be shown)
            my $completed_planned = $ticket->completed;
            $completed_planned ||= $ticket->planned
                if $ticket->planned && $ticket->planned > $self->today;
            $completed_planned ||= $ticket->provisional<commit_msg>Do not show provisional tickets on calendar<commit_after>=pod
Lenio - Web-based Facilities Management Software
Copyright (C) 2016 Ctrl O Ltd

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package Lenio::Calendar;

use Log::Report;
use Moo;
use MooX::Types::MooseLike::Base qw/:all/;
use MooX::Types::MooseLike::DateTime qw/DateAndTime/;

has schema => (
    is       => 'ro',
    required => 1,
);

has from => (
    is  => 'ro',
    isa => DateAndTime,
);

has to => (
    is  => 'ro',
    isa => DateAndTime,
);

has dateformat => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

# Allow override of today for tests
has today => (
    is      => 'ro',
    isa     => DateAndTime,
    default => sub { DateTime->now },
);

has site => (
    is  => 'ro',
);

has multiple_sites => (
    is  => 'ro',
    isa => Bool,
);

has contractors => (
    is     => 'ro',
    isa    => ArrayRef,
    coerce => sub {
        my $value = shift;
        $value ||= [];
        if (ref $value eq 'HASH')
        {
            $value = [keys %$value];
        }
        $value;
    },
);

has login => (
    is  => 'ro',
);

has task_summary => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_task_summary
{   my $self = shift;
    my %options = (
        site_id => $self->site->id,
    );
    $options{global} = 1 if $self->login->is_admin;
    [$self->schema->resultset('Task')->summary(%options, onlysite => 1)];
}

# The task summary before the start date. This is used to find out
# the previous completed task dates, to fill in missing tasks.
has task_summary_previous => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_task_summary_previous
{   my $self = shift;
    my %options = (
        to             => $self->from,
        site_id        => $self->site->id,
        contractor_ids => $self->contractors,
    );
    $options{global} = 1 if $self->login->is_admin;
    [$self->schema->resultset('Task')->summary(%options, onlysite => 1)];
}

has ticket_summary => (
    is  => 'lazy',
    isa => ArrayRef,
);

sub _build_ticket_summary
{   my $self = shift;
    [
        $self->schema->resultset('Ticket')->summary(
            site_id        => $self->site->id,
            contractor_ids => $self->contractors,
            from           => $self->from,
            to             => $self->to,
            login          => $self->login,
        )
    ];
}

sub tasks
{   my $self = shift;

    my @calendar1; my @calendar2;

    my $planned;

    my $done; my $lastcomp;
    # First add all the tickets in this time period. 
    foreach my $ticket (@{$self->ticket_summary})
    {
        # Remove provisional tickets for the time being
        next if !$ticket->completed && !$ticket->planned;

        my $task_id = $ticket->task_id;
        push @calendar1, $self->_cal_item(
            name        => $ticket->name,
            description => $ticket->description,
            task_id     => $task_id,
            ticket_id   => $ticket->id,
            completed   => $ticket->completed,
            planned     => $ticket->planned,
            provisional => $ticket->provisional,
        );
        if ($task_id)
        {
            # Take the completed date if it exists, otherwise take the planned
            # date, otherwise take the provisional date. Future items and
            # warnings will be mapped out from this. If the planned date is in
            # the past, however, ignore it and plonk any overdue dates wherever
            # they would have been (otherwise they won't be shown)
            my $completed_planned = $ticket->completed;
            $completed_planned ||= $ticket->planned
                if $ticket->planned && $ticket->planned > $self->today;
            $completed_planned ||= $ticket->provisional
{'fraction_non_alphanumeric': 0.12844036697247707, 'fraction_numerical': 0.0032516548600627104, 'mean_word_length': 2.1979205347196435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 201, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4293859', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2534, 'n_words': 836}