]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/SetNumberRange.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / BackgroundJob / SetNumberRange.pm
1 package SL::BackgroundJob::SetNumberRange;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::PrefixedNumber;
8
9 use DateTime::Format::Strptime;
10
11 sub create_job {
12   $_[0]->create_standard_job('59 23 31 12 *'); # one minute before new year
13 }
14
15
16 sub run {
17   my ($self, $db_obj) = @_;
18   my $data       = $db_obj->data_as_hash;
19
20   if ($data->{digits_year} && !($data->{digits_year} == 2 || $data->{digits_year} == 4)) {
21     die "No valid input for digits_year should be 2 or 4.";
22   }
23   if ($data->{multiplier}  && !($data->{multiplier} % 10 == 0)) {
24     die "No valid input for multiplier should be 10, 100, .., 1000000";
25   }
26   if ($data->{monthly} && $data->{monthly_strftime}) {
27     DateTime->today_local->strftime($data->{monthly_strftime}) // die "No valid input for montly_strftime";
28   }
29
30   # new year
31   my $running_year  =  $data->{current_year} ? DateTime->today_local->truncate(to => 'year')
32                      : DateTime->today_local->truncate(to => 'year')->add(years => 1)->year();
33
34   $running_year     = ($data->{digits_year} == 2) ? substr($running_year, 2, 2) : $running_year;
35
36   my $multiplier = $data->{multiplier} || 100;
37
38   # or new month
39   my $today_dt   = DateTime->today_local;
40
41   my $today      =  $data->{monthly_strftime}     ? $today_dt->strftime($data->{monthly_strftime})
42                   : $today_dt->strftime('%y-%m-');
43
44   $today         =  $data->{monthly_postfix} ? $today . $data->{monthly_postfix} : $today . '000';
45
46   my $defaults   = SL::DB::Default->get;
47
48   my $current_number;
49   foreach (qw(invnumber cnnumber soinumber pqinumber sonumber ponumber pocnumber
50               sqnumber rfqnumber sdonumber pdonumber sudonumber rdonumber
51               s_reclamation_record_number p_reclamation_record_number           )) {
52
53
54     if ($data->{monthly}) {
55       $current_number = SL::PrefixedNumber->new(number => $today);
56     } else {
57       $current_number = SL::PrefixedNumber->new(number => $defaults->{$_});
58       $current_number->set_to($running_year * $multiplier);
59     }
60     $defaults->{$_} = $current_number->get_current;
61   }
62   $defaults->save() || die "Could not change number ranges";
63
64   return exists $data->{result} ? $data->{result} : 1;
65 }
66
67 1;
68
69 __END__
70
71 =encoding utf8
72
73 =head1 NAME
74
75 SL::BackgroundJob::SetNumberRange —
76 Background job for setting all kivitendo number ranges for a new year or a new month
77
78 =head1 SYNOPSIS
79
80 The job can either be run annually or monthly and defaults to annually.
81
82 The background job accepts the following optional json encoded parameters in the data for monthly mode:
83
84 C<monthly>: If set to true the job assumes it is the first of a new month
85
86 C<monthly_postfix>: A user postfix can be defined as a string for the new number.
87 If nothing is set three zeros are added as a postfix string ('000').
88
89 C<monthly_strftime>: Year, month and day can optional be defined as user input in the
90 same way as the C strftime method. If nothing is set 'y%-m%' is the default. More options at the
91 time of writing can be found here: https://metacpan.org/pod/DateTime#strftime-Patterns
92
93 The backgroud accepts the following optional json encoded parameters in the data for the annually mode (default):
94
95 C<multiplier>: Multiplier to set the number range (defaults to 100)
96
97 C<digits_year>: Handles the encoding of the year (can be 2 or 4, ie 24 or 2024). Defaults to 4
98
99 C<current_year>: If set to 1 the current year will be used and not the next year.
100
101
102 The latter option is useful if the jobs runs on the 1st of the new year.
103
104 The job is deactivated by default. Administrators of installations
105 where such a feature is wanted have to create a job entry manually.
106
107 =head1 AUTHOR
108
109 Jan Büren E<lt>jan@kivitendo.deE<gt>
110
111 =cut