use SL::IS;
use SL::OE;
use SL::MoreCommon qw(restore_form save_form);
+use SL::RecordLinks;
+
use Data::Dumper;
use DateTime;
use List::MoreUtils qw(any uniq);
$has_further_invoice_for_advance_payment = any {'SL::DB::Invoice' eq ref $_ && "invoice_for_advance_payment" eq $_->type} @$lr;
}
+ my $has_final_invoice;
+ if ($form->{id} && $form->{type} eq "invoice_for_advance_payment") {
+ my $invoice_obj = SL::DB::Invoice->load_cached($form->{id});
+ my $lr = $invoice_obj->linked_records(direction => 'to', to => ['Invoice']);
+ $has_final_invoice = any {'SL::DB::Invoice' eq ref $_ && "invoice" eq $_->invoice_type} @$lr;
+ }
+
for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
disabled => !$may_edit_create ? t8('You must not change this invoice.')
: !$form->{id} ? t8('This invoice has not been posted yet.')
: $has_further_invoice_for_advance_payment ? t8('This invoice has already a further invoice for advanced payment.')
+ : $has_final_invoice ? t8('This invoice has already a final invoice.')
+ : undef,
+ only_if => $form->{type} eq "invoice_for_advance_payment",
+ ],
+ action => [
+ t8('Final Invoice'),
+ submit => [ '#form', { action => "final_invoice" } ],
+ checks => [ 'kivi.validate_form' ],
+ disabled => !$may_edit_create ? t8('You must not change this invoice.')
+ : !$form->{id} ? t8('This invoice has not been posted yet.')
+ : $has_further_invoice_for_advance_payment ? t8('This invoice has a further invoice for advanced payment.')
+ : $has_final_invoice ? t8('This invoice has already a final invoice.')
: undef,
only_if => $form->{type} eq "invoice_for_advance_payment",
],
&display_form;
}
+sub final_invoice {
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+
+ $main::auth->assert('invoice_edit');
+
+ # search all related invoices for advance payment
+ #
+ # (order) -> invoice for adv. payment 1 -> invoice for adv. payment 2 -> invoice for adv. payment 3 -> final invoice
+ #
+ # we are currently in the last invoice for adv. payment (3 in this example)
+ my $invoice_obj = SL::DB::Invoice->load_cached($form->{id});
+ my $links = $invoice_obj->linked_records(direction => 'from', from => ['Invoice'], recursive => 1);
+ my @related_invoices = grep {'SL::DB::Invoice' eq ref $_ && "invoice_for_advance_payment" eq $_->type} @$links;
+
+ push @related_invoices, $invoice_obj;
+
+ delete @{ $form }{qw(printed emailed queued invnumber invdate exchangerate forex deliverydate datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno locked)};
+
+ $form->{convert_from_ar_ids} = $form->{id};
+ $form->{id} = '';
+ $form->{type} = 'invoice';
+ $form->{title} = t8('Edit Final Invoice');
+ $form->{paidaccounts} = 1;
+ $form->{invdate} = $form->current_date(\%myconfig);
+ my $terms = get_payment_terms_for_invoice();
+ $form->{duedate} = $terms ? $terms->calc_date(reference_date => $form->{invdate})->to_kivitendo : $form->{invdate};
+ $form->{employee_id} = SL::DB::Manager::Employee->current->id;
+ $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
+ $form->{exchangerate} = $form->{forex} if $form->{forex};
+
+ foreach my $i (1 .. $form->{"rowcount"}) {
+ delete $form->{"id_$i"};
+ delete $form->{"invoice_id_$i"};
+ delete $form->{"parts_id_$i"};
+ delete $form->{"partnumber_$i"};
+ delete $form->{"description_$i"};
+ }
+
+ remove_emptied_rows(1);
+
+ my $i = 0;
+ foreach my $ri (@related_invoices) {
+ foreach my $item (@{$ri->items_sorted}) {
+ $i++;
+ $form->{"id_$i"} = $item->parts_id;
+ $form->{"partnumber_$i"} = $item->part->partnumber;
+ $form->{"discount_$i"} = $item->discount*100.0;
+ $form->{"sellprice_$i"} = $item->fxsellprice;
+ $form->{$_ . "_" . $i} = $item->$_ for qw(description longdescription qty price_factor_id unit sellprice active_price_source active_discount_source);
+
+ $form->{$_ . "_" . $i} = $form->format_amount(\%myconfig, $form->{$_ . "_" . $i}) for qw(qty sellprice discount);
+ }
+ }
+ $form->{rowcount} = $i;
+
+ update();
+ $::dispatcher->end_request;
+}
+
sub storno {
$main::lxdebug->enter_sub();
'Edit Dunning Process Config' => 'Mahnwesenkonfiguration bearbeiten',
'Edit Employee #1' => 'Benutzer #1 bearbeiten',
'Edit Factur-X/ZUGFeRD notes' => 'Factur-X-/ZUGFeRD-Notizen bearbeiten',
+ 'Edit Final Invoice' => 'Schlussrechnung bearbeiten',
'Edit Follow-Up' => 'Wiedervorlage bearbeiten',
'Edit Follow-Up for #1' => 'Wiedervorlage für #1 bearbeiten',
'Edit General Ledger Transaction' => 'Buchung im Hauptbuch bearbeiten',
'Filter for item variables' => 'Filter für benutzerdefinierte Artikelvariablen',
'Filter parts' => 'Artikel filtern',
'Filter record template' => 'Filter für Buchungsvorlagen',
+ 'Final Invoice' => 'Schlussrechnung',
'Financial Controlling' => 'Finanzcontrolling',
'Financial Controlling Report' => 'Finanzcontrollingbericht',
'Financial Overview' => 'Finanzübersicht',
'This group is valid for the following clients' => 'Diese Gruppe ist für die folgenden Mandanten gültig',
'This has been changed in this version, therefore please change the "old" bins to some real warehouse bins.' => 'Das wurde in dieser Version umgestellt, bitte ändern Sie die Freitext-Lagerplätze auf vorhandene Lagerplätze.',
'This has been changed in this version.' => 'Ab dieser Version ist dies nicht mehr so.',
+ 'This invoice has a further invoice for advanced payment.' => 'Diese Rechnung hat eine weitere Anzahlungsrechnung.',
+ 'This invoice has already a final invoice.' => 'Diese Rechnung hat schon eine Schlussrechnung.',
'This invoice has already a further invoice for advanced payment.' => 'Diese Rechnung hat schon eine weitere Anzahlungsrechnung.',
'This invoice has already been posted.' => 'Die Rechnung wurde bereits gebucht.',
'This invoice has been canceled already.' => 'Die Rechnung wurde bereits storniert.',
'Edit Dunning Process Config' => '',
'Edit Employee #1' => '',
'Edit Factur-X/ZUGFeRD notes' => '',
+ 'Edit Final Invoice' => '',
'Edit Follow-Up' => '',
'Edit Follow-Up for #1' => '',
'Edit General Ledger Transaction' => '',
'Filter for item variables' => '',
'Filter parts' => '',
'Filter record template' => '',
+ 'Final Invoice' => '',
'Financial Controlling' => '',
'Financial Controlling Report' => '',
'Financial Overview' => '',
'This group is valid for the following clients' => '',
'This has been changed in this version, therefore please change the "old" bins to some real warehouse bins.' => '',
'This has been changed in this version.' => '',
+ 'This invoice has a further invoice for advanced payment.' => '',
+ 'This invoice has already a final invoice.' => '',
'This invoice has already a further invoice for advanced payment.' => '',
'This invoice has already been posted.' => '',
'This invoice has been canceled already.' => '',