X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FBackgroundJob%2FCreatePeriodicInvoices.pm;h=431225f9c6592c3e3bf368680e11959026e1e815;hb=2a51537014d67c1f97a37b18548506f3e15549b5;hp=5055083ac5a233f4650a12467c8566001394f3b7;hpb=34420ddb07d6ebea006aa96f131aaa050a7037bc;p=kivitendo-erp.git diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index 5055083ac..431225f9c 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -66,7 +66,7 @@ sub run { } sub _log_msg { - my $message = join('', @_); + my $message = join('', 'SL::BackgroundJob::CreatePeriodicInvoices: ', @_); $message .= "\n" unless $message =~ m/\n$/; $::lxdebug->message(LXDebug::DEBUG1(), $message); } @@ -74,7 +74,7 @@ sub _log_msg { sub _generate_time_period_variables { my $config = shift; my $period_start_date = shift; - my $period_end_date = $period_start_date->clone->truncate(to => 'month')->add(months => $config->get_period_length)->subtract(days => 1); + my $period_end_date = $period_start_date->clone->truncate(to => 'month')->add(months => $config->get_billing_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'), @@ -107,7 +107,7 @@ sub _generate_time_period_variables { sub _replace_vars { my (%params) = @_; my $sub = $params{attribute}; - my $str = $params{object}->$sub; + my $str = $params{object}->$sub // ''; my $sub_fmt = lc($params{attribute_format} // 'text'); my ($start_tag, $end_tag) = $sub_fmt eq 'html' ? ('<%', '%>') : ('<%', '%>'); @@ -142,6 +142,40 @@ sub _replace_vars { $params{object}->$sub($str); } +sub _adjust_sellprices_for_period_lengths { + my (%params) = @_; + + my $billing_len = $params{config}->get_billing_period_length; + my $order_value_len = $params{config}->get_order_value_period_length; + + return if $billing_len == $order_value_len; + + my $is_last_invoice_in_cycle = $params{config}->is_last_bill_date_in_order_value_cycle(date => $params{period_start_date}); + + _log_msg("_adjust_sellprices_for_period_lengths: period_start_date $params{period_start_date} is_last_invoice_in_cycle $is_last_invoice_in_cycle billing_len $billing_len order_value_len $order_value_len"); + + if ($order_value_len < $billing_len) { + my $num_orders_per_invoice = $billing_len / $order_value_len; + + $_->sellprice($_->sellprice * $num_orders_per_invoice) for @{ $params{invoice}->items }; + + return; + } + + my $num_invoices_in_cycle = $order_value_len / $billing_len; + + foreach my $item (@{ $params{invoice}->items }) { + my $sellprice_one_invoice = $::form->round_amount($item->sellprice * $billing_len / $order_value_len, 2); + + if ($is_last_invoice_in_cycle) { + $item->sellprice($item->sellprice - ($num_invoices_in_cycle - 1) * $sellprice_one_invoice); + + } else { + $item->sellprice($sellprice_one_invoice); + } + } +} + sub _create_periodic_invoice { $main::lxdebug->enter_sub(); @@ -166,27 +200,18 @@ sub _create_periodic_invoice { $invoice->assign_attributes(deliverydate => $period_start_date, intnotes => $intnotes, employee => $order->employee, # new_from sets employee to import user + direct_debit => $config->direct_debit, ); - _replace_vars(object => $invoice, vars => $time_period_vars, attribute => $_) for qw(notes intnotes transaction_description); + _replace_vars(object => $invoice, vars => $time_period_vars, attribute => $_, attribute_format => ($_ eq 'notes' ? 'html' : 'text')) for qw(notes intnotes transaction_description); foreach my $item (@{ $invoice->items }) { _replace_vars(object => $item, vars => $time_period_vars, attribute => $_, attribute_format => ($_ eq 'longdescription' ? 'html' : 'text')) for qw(description longdescription); } - $invoice->post(ar_id => $config->ar_chart_id) || die; - - # like $form->add_shipto, but we don't need to check for a manual exception, - # because we can already assume this (otherwise no shipto_id from order) - if ($order->shipto_id) { + _adjust_sellprices_for_period_lengths(invoice => $invoice, config => $config, period_start_date => $period_start_date); - my $shipto_oe = SL::DB::Manager::Shipto->find_by(shipto_id => $order->shipto_id); - my $shipto_ar = $shipto_oe->clone_and_reset; - - $shipto_ar->module('AR'); # alter module OE -> AR - $shipto_ar->trans_id($invoice->id); # alter trans_id -> new id from invoice - $shipto_ar->save; - } + $invoice->post(ar_id => $config->ar_chart_id) || die; $order->link_to_record($invoice); @@ -210,6 +235,8 @@ sub _create_periodic_invoice { period_start_date => $period_start_date) ->save; + _log_msg("_create_invoice created for period start date $period_start_date id " . $invoice->id . " number " . $invoice->invnumber . " netamount " . $invoice->netamount . " amount " . $invoice->amount); + # die $invoice->transaction_description; })) { $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2])); @@ -278,6 +305,14 @@ sub _print_invoice { $form->{OUT} = $config->printer->printer_command; $form->{OUT_MODE} = '|-'; + $form->{TEMPLATE_DRIVER_OPTIONS} = { + variable_content_types => { + longdescription => 'html', + partnotes => 'html', + notes => 'html', + }, + }; + $form->prepare_for_printing; $form->throw_on_error(sub {