+ # valid parameters with default values
+ my %valid_params = (
+ from_date => DateTime->new( day => 1, month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1)->to_kivitendo,
+ to_date => DateTime->last_day_of_month(month => DateTime->today_local->month, year => DateTime->today_local->year)->subtract(months => 1)->to_kivitendo,
+ customernumbers => [],
+ part_id => undef,
+ project_id => undef,
+ rounding => 1,
+ link_order => 0,
+ );
+
+
+ # check user input param names
+ foreach my $param (keys %$data) {
+ die "Not a valid parameter: $param" unless exists $valid_params{$param};
+ }
+
+ $self->params(
+ { map { ($_ => $data->{$_} // $valid_params{$_}) } keys %valid_params }
+ );
+
+
+ # convert date from string to object
+ my $from_date;
+ my $to_date;
+ $from_date = DateTime->from_kivitendo($self->params->{from_date});
+ $to_date = DateTime->from_kivitendo($self->params->{to_date});
+ # DateTime->from_kivitendo returns undef if the string cannot be parsed. Therefore test the result.
+ die 'Cannot convert date from string "' . $self->params->{from_date} . '"' if !$from_date;
+ die 'Cannot convert date to string "' . $self->params->{to_date} . '"' if !$to_date;
+
+ $to_date->add(days => 1); # to get all from the to_date, because of the time part (15.12.2020 23.59 > 15.12.2020)
+
+ $self->params->{from_date} = $from_date;
+ $self->params->{to_date} = $to_date;
+
+
+ # check if customernumbers are valid
+ die 'Customer numbers must be given in an array' if 'ARRAY' ne ref $self->params->{customernumbers};
+
+ my $customers = [];
+ if (scalar @{ $self->params->{customernumbers} }) {
+ $customers = SL::DB::Manager::Customer->get_all(where => [ customernumber => $self->params->{customernumbers},
+ or => [obsolete => undef, obsolete => 0] ]);
+ }
+ die 'Not all customer numbers are valid' if scalar @$customers != scalar @{ $self->params->{customernumbers} };
+
+ # return customer ids
+ $self->params->{customer_ids} = [ map { $_->id } @$customers ];
+
+
+ # check part
+ if ($self->params->{part_id} && !SL::DB::Manager::Part->find_by(id => $self->params->{part_id},
+ or => [obsolete => undef, obsolete => 0])) {
+ die 'No valid part found by given part id';
+ }
+
+
+ # check project
+ if ($self->params->{project_id} && !SL::DB::Manager::Project->find_by(id => $self->params->{project_id},
+ active => 1, valid => 1)) {
+ die 'No valid project found by given project id';
+ }