]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/SetClosedTo.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / BackgroundJob / SetClosedTo.pm
1 package SL::BackgroundJob::SetClosedTo;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::Helper::DateTime;
8
9 sub create_job {
10   $_[0]->create_standard_job('1 0 10 * *'); # always the 10th of the month
11 }
12
13
14 sub run {
15   my ($self, $db_obj) = @_;
16   my $data       = $db_obj->data_as_hash;
17
18   my $subtract_month = $data->{subtract_month} || 1;
19   my $subtract_days  = $data->{subtract_days}  || 10;
20
21   die "No integer number for days or month" unless ($subtract_month =~ m/^\d+\z/
22                                                  && $subtract_days  =~ m/^\d+\z/);
23
24   # new closedto
25   my $new_closedto = DateTime->now_local->subtract(months => $subtract_month, days => $subtract_days);
26
27   my $defaults   = SL::DB::Default->get;
28
29   # dont accidently open the books
30   return 1 if ($defaults->closedto && $defaults->closedto >= $new_closedto);
31
32   $defaults->closedto($new_closedto);
33   $defaults->save || die "Cannot save closedto!";
34
35   return 1;
36 }
37
38 1;
39
40 __END__
41
42 =encoding utf8
43
44 =head1 NAME
45
46 SL::BackgroundJob::SetClosedTo - Background job for
47 periodically setting closedto (books closed until).
48 Defaults to the end of the second last month.
49
50 =cut