|
package SL::BackgroundJob::CreatePeriodicInvoices;
|
|
|
|
use strict;
|
|
|
|
use parent qw(SL::BackgroundJob::Base);
|
|
|
|
use Config::Std;
|
|
use DateTime::Format::Strptime;
|
|
use English qw(-no_match_vars);
|
|
use List::MoreUtils qw(uniq);
|
|
use Params::Validate qw(:all);
|
|
|
|
use SL::Common;
|
|
use SL::DB::AuthUser;
|
|
use SL::DB::Default;
|
|
use SL::DB::Order;
|
|
use SL::DB::Invoice;
|
|
use SL::DB::PeriodicInvoice;
|
|
use SL::DB::PeriodicInvoicesConfig;
|
|
use SL::File;
|
|
use SL::Helper::CreatePDF qw(create_pdf find_template);
|
|
use SL::Mailer;
|
|
use SL::Util qw(trim);
|
|
use SL::System::Process;
|
|
use SL::Locale::String qw(t8);
|
|
|
|
sub create_job {
|
|
$_[0]->create_standard_job('0 3 1 * *'); # first day of month at 3:00 am
|
|
}
|
|
|
|
sub run {
|
|
my $self = shift;
|
|
$self->{db_obj} = shift;
|
|
|
|
$self->{$_} = [] for qw(job_errors posted_invoices printed_invoices printed_failed emailed_invoices emailed_failed disabled_orders);
|
|
|
|
if (!$self->{db_obj}->db->with_transaction(sub {
|
|
1; # make Emacs happy
|
|
|
|
my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ active => 1 ]);
|
|
|
|
foreach my $config (@{ $configs }) {
|
|
my $new_end_date = $config->handle_automatic_extension;
|
|
_log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date;
|
|
}
|
|
|
|
my (@invoices_to_print, @invoices_to_email);
|
|
|
|
_log_msg("Number of configs: " . scalar(@{ $configs}));
|
|
|
|
foreach my $config (@{ $configs }) {
|
|
my $open_orders = $config->get_open_orders_for_period();
|
|
_log_msg("Dates: " . join(' ', map { $_->reqdate->to_lxoffice } @$open_orders));
|
|
|
|
foreach my $order (@$open_orders) {
|
|
my $data = $self->_create_periodic_invoice(order => $order);
|
|
my $invoice = $data->{invoice};
|
|
next unless $invoice;
|
|
|
|
_log_msg("Invoice " . $invoice->invnumber . " posted for config ID " . $config->id . ", period start date " . $::locale->format_date(\%::myconfig, $invoice->deliverydate) . "\n");
|
|
|
|
push @{ $self->{posted_invoices} }, $invoice;
|
|
push @invoices_to_print, $data if $config->print;
|
|
push @invoices_to_email, $data if $config->send_email;
|
|
|
|
my $inactive_ordnumber = $config->disable_one_time_config;
|
|
if ($inactive_ordnumber) {
|
|
# disable one time configs and skip eventual invoices
|
|
_log_msg("Order " . $inactive_ordnumber . " deavtivated \n");
|
|
push @{ $self->{disabled_orders} }, $inactive_ordnumber;
|
|
last;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach my $inv_data ( @invoices_to_print ) { $self->_print_invoice($inv_data); }
|
|
foreach my $inv_data ( @invoices_to_email ) { $self->_email_invoice($inv_data); }
|
|
|
|
$self->_send_summary_email;
|
|
|
|
1;
|
|
})) {
|
|
$::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
|
|
return undef;
|
|
}
|
|
|
|
if (@{ $self->{job_errors} }) {
|
|
my $msg = join "\n", @{ $self->{job_errors} };
|
|
_log_msg("Errors: $msg");
|
|
die $msg;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub _log_msg {
|
|
my $message = join('', 'SL::BackgroundJob::CreatePeriodicInvoices: ', @_);
|
|
$message .= "\n" unless $message =~ m/\n$/;
|
|
$::lxdebug->message(LXDebug::DEBUG1(), $message);
|
|
}
|
|
|
|
sub _generate_time_period_variables {
|
|
my $config = shift;
|
|
my $period_start_date = shift;
|
|
|
|
my $period_length = $config->periodicity eq 'o' ? $config->get_order_value_period_length : $config->get_billing_period_length;
|
|
my $period_end_date = $period_start_date->clone->add(months => $period_length)->subtract(days => 1);
|
|
|
|
my @month_names = ('',
|
|
$::locale->text('January'), $::locale->text('February'), $::locale->text('March'), $::locale->text('April'), $::locale->text('May'), $::locale->text('June'),
|
|
$::locale->text('July'), $::locale->text('August'), $::locale->text('September'), $::locale->text('October'), $::locale->text('November'), $::locale->text('December'));
|
|
|
|
my $vars = {
|
|
current_quarter => [ $period_start_date->clone->truncate(to => 'month'), sub { $_[0]->quarter } ],
|
|
previous_quarter => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 3), sub { $_[0]->quarter } ],
|
|
next_quarter => [ $period_start_date->clone->truncate(to => 'month')->add( months => 3), sub { $_[0]->quarter } ],
|
|
|
|
current_month => [ $period_start_date->clone->truncate(to => 'month'), sub { $_[0]->month } ],
|
|
previous_month => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $_[0]->month } ],
|
|
next_month => [ $period_start_date->clone->truncate(to => 'month')->add( months => 1), sub { $_[0]->month } ],
|
|
|
|
current_month_long => [ $period_start_date->clone->truncate(to => 'month'), sub { $month_names[ $_[0]->month ] } ],
|
|
previous_month_long => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $month_names[ $_[0]->month ] } ],
|
|
next_month_long => [ $period_start_date->clone->truncate(to => 'month')->add( months => 1), sub { $month_names[ $_[0]->month ] } ],
|
|
|
|
current_year => [ $period_start_date->clone->truncate(to => 'year'), sub { $_[0]->year } ],
|
|
previous_year => [ $period_start_date->clone->truncate(to => 'year')->subtract(years => 1), sub { $_[0]->year } ],
|
|
next_year => [ $period_start_date->clone->truncate(to => 'year')->add( years => 1), sub { $_[0]->year } ],
|
|
|
|
period_start_date => [ $period_start_date->clone, sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
|
|
period_end_date => [ $period_end_date, sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
|
|
};
|
|
|
|
return $vars;
|
|
}
|
|
|
|
sub _replace_vars {
|
|
my (%params) = @_;
|
|
my $sub = $params{attribute};
|
|
my $str = $params{object}->$sub // '';
|
|
my $sub_fmt = lc($params{attribute_format} // 'text');
|
|
|
|
my ($start_tag, $end_tag) = $sub_fmt eq 'html' ? ('<%', '%>') : ('<%', '%>');
|
|
my @invoice_keys = $params{invoice} ? (map { $_->name } $params{invoice}->meta->columns) : ();
|
|
my $key_name_re = join '|', map { quotemeta } (@invoice_keys, keys %{ $params{vars}
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
|
|
|
|
|
=cut
|