+ $::form->get_lists(printers => "ALL_PRINTERS",
+ charts => { key => 'ALL_CHARTS',
+ transdate => 'current_date' });
+
+ $::form->{AR} = [ grep { $_->{link} =~ m/(?:^|:)AR(?::|$)/ } @{ $::form->{ALL_CHARTS} } ];
+ $::form->{title} = $::locale->text('Edit the configuration for periodic invoices');
+
+ $::form->header(no_layout => 1);
+ print $::form->parse_html_template('oe/edit_periodic_invoices_config', $config);
+
+ $::lxdebug->leave_sub();
+}
+
+sub save_periodic_invoices_config {
+ $::lxdebug->enter_sub();
+
+ $::form->{type} = 'sales_order';
+
+ check_oe_access();
+
+ $::form->isblank('start_date_as_date', $::locale->text('The start date is missing.'));
+
+ my $config = { active => $::form->{active} ? 1 : 0,
+ terminated => $::form->{terminated} ? 1 : 0,
+ periodicity => (any { $_ eq $::form->{periodicity} } qw(m q y)) ? $::form->{periodicity} : 'm',
+ start_date_as_date => $::form->{start_date_as_date},
+ end_date_as_date => $::form->{end_date_as_date},
+ print => $::form->{print} ? 1 : 0,
+ printer_id => $::form->{print} ? $::form->{printer_id} * 1 : undef,
+ copies => $::form->{copies} * 1 ? $::form->{copies} : 1,
+ extend_automatically_by => $::form->{extend_automatically_by} * 1 || undef,
+ ar_chart_id => $::form->{ar_chart_id} * 1,
+ };
+
+ $::form->{periodic_invoices_config} = YAML::Dump($config);
+
+ $::form->{title} = $::locale->text('Edit the configuration for periodic invoices');
+ $::form->header;
+ print $::form->parse_html_template('oe/save_periodic_invoices_config', $config);
+
+ $::lxdebug->leave_sub();
+}
+
+sub _oe_remove_delivered_or_billed_rows {
+ my (%params) = @_;
+
+ return if !$params{id} || !$params{type};
+
+ my $ord_quot = SL::DB::Order->new(id => $params{id})->load;
+ return if !$ord_quot;
+
+ my %args = (
+ direction => 'to',
+ to => $params{type} eq 'delivered' ? 'DeliveryOrder' : 'Invoice',
+ via => [ $params{type} eq 'delivered' ? qw(Order) : qw(Order DeliveryOrder) ],
+ );
+
+ my %handled_base_qtys;
+ foreach my $record (@{ $ord_quot->linked_records(%args) }) {
+ next if $ord_quot->is_sales != $record->is_sales;
+
+ foreach my $item (@{ $record->items }) {
+ $handled_base_qtys{ $item->parts_id } += $item->qty * $item->unit_obj->base_factor;
+ }
+ }
+
+ _remove_billed_or_delivered_rows(quantities => \%handled_base_qtys);
+}
+
+sub dispatcher {