Projekt

Allgemein

Profil

Herunterladen (42,7 KB) Statistiken
| Zweig: | Markierung: | Revision:
d319704a Moritz Bunkus
#=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
# Web http://www.lx-office.org
#
#=====================================================================
# SQL-Ledger Accounting
# Copyright (c) 1998-2002
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
f7b15d43 Christian Wittmer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
d319704a Moritz Bunkus
#======================================================================
#
# Inventory invoicing module
#
#======================================================================

7a7f33b5 Moritz Bunkus
use SL::FU;
d319704a Moritz Bunkus
use SL::IS;
1465da30 Sven Schöling
use SL::OE;
87949627 Sven Schöling
use SL::MoreCommon qw(restore_form save_form);
07d71c33 Stephan Köhler
use Data::Dumper;
009b0731 Sven Schöling
use DateTime;
41adf433 Moritz Bunkus
use List::MoreUtils qw(uniq);
7a7f33b5 Moritz Bunkus
use List::Util qw(max sum);
0d37f1e8 Moritz Bunkus
use List::UtilsBy qw(sort_by);
9d07c34f Bernd Bleßmann
use English qw(-no_match_vars);
d319704a Moritz Bunkus
6e08e454 Thomas Heck
use SL::DB::Default;
use SL::DB::Customer;
63a18cca Moritz Bunkus
use SL::DB::Department;
59adfa36 Moritz Bunkus
use SL::DB::Invoice;
5bc87ade Moritz Bunkus
use SL::DB::PaymentTerm;
6e08e454 Thomas Heck
46b5ee69 Moritz Bunkus
require "bin/mozilla/common.pl";
d629acd8 Sven Schöling
require "bin/mozilla/io.pl";
d2bafad3 Thomas Kasulke
5486b4a9 Sven Schöling
use strict;

d319704a Moritz Bunkus
1;

# end of main

sub add {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
bde667c2 Moritz Bunkus
5486b4a9 Sven Schöling
my $form = $main::form;
my $locale = $main::locale;
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');

519f43a2 Moritz Bunkus
$form->{show_details} = $::myconfig{show_form_details};

54e4131e Moritz Bunkus
if ($form->{type} eq "credit_note") {
$form->{title} = $locale->text('Add Credit Note');

if ($form->{storno}) {
$form->{title} = $locale->text('Add Storno Credit Note');
}
} else {
$form->{title} = $locale->text('Add Sales Invoice');

}
d319704a Moritz Bunkus

8c7e4493 Moritz Bunkus
$form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
d319704a Moritz Bunkus
&invoice_links;
&prepare_invoice;
&display_form;

5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub edit {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
ee792a3f Sven Schöling
my $locale = $main::locale;
1c084510 Moritz Bunkus
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
519f43a2 Moritz Bunkus
$form->{show_details} = $::myconfig{show_form_details};
ad1c22fa Thomas Heck
$form->{taxincluded_changed_by_user} = 1;

2b89ec97 Thomas Kasulke
# show history button
$form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
fb4d2ffa Moritz Bunkus
5486b4a9 Sven Schöling
my ($language_id, $printer_id);
8f3dc0b4 Stephan Köhler
if ($form->{print_and_post}) {
081a4f97 Moritz Bunkus
$form->{action} = "print";
8f3dc0b4 Stephan Köhler
$form->{resubmit} = 1;
54e4131e Moritz Bunkus
$language_id = $form->{language_id};
$printer_id = $form->{printer_id};
8f3dc0b4 Stephan Köhler
}
254bf1ff Sven Schöling
d319704a Moritz Bunkus
&invoice_links;
254bf1ff Sven Schöling
if ($form->{type} eq "credit_note") {
$form->{title} = $locale->text('Edit Credit Note');
$form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
} else {
$form->{title} = $locale->text('Edit Sales Invoice');
$form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
}

d319704a Moritz Bunkus
&prepare_invoice;
54e4131e Moritz Bunkus
if ($form->{print_and_post}) {
$form->{language_id} = $language_id;
$form->{printer_id} = $printer_id;
}

d319704a Moritz Bunkus
&display_form;

5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub invoice_links {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
0bb0eb67 Stephan Köhler
5486b4a9 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;

$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
d319704a Moritz Bunkus
$form->{vc} = 'customer';

# create links
$form->create_links("AR", \%myconfig, "customer");

0e6197ea Sven Schöling
my $editing = $form->{id};

03d3d025 Bernd Bleßmann
$form->backup_vars(qw(payment_id language_id taxzone_id salesman_id
taxincluded currency cp_id intnotes id shipto_id
delivery_term_id));
f9f5330a Stephan Köhler
54e4131e Moritz Bunkus
IS->get_customer(\%myconfig, \%$form);
1f2c9572 Philip Reetz
f8529382 Sven Schöling
$form->restore_vars(qw(id));

IS->retrieve_invoice(\%myconfig, \%$form);
03d3d025 Bernd Bleßmann
$form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes
cp_id shipto_id delivery_term_id));
f8529382 Sven Schöling
$form->restore_vars(qw(taxincluded)) if $form->{id};
0e6197ea Sven Schöling
$form->restore_vars(qw(salesman_id)) if $editing;
54e4131e Moritz Bunkus
d319704a Moritz Bunkus
$form->{employee} = "$form->{employee}--$form->{employee_id}";

# forex
$form->{forex} = $form->{exchangerate};
5486b4a9 Sven Schöling
my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
d319704a Moritz Bunkus
5486b4a9 Sven Schöling
foreach my $key (keys %{ $form->{AR_links} }) {
foreach my $ref (@{ $form->{AR_links}{$key} }) {
b8916e5c Sven Schöling
$form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
d319704a Moritz Bunkus
}

if ($key eq "AR_paid") {
5486b4a9 Sven Schöling
next unless $form->{acc_trans}{$key};
for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
b8916e5c Sven Schöling
$form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
d319704a Moritz Bunkus
f45b296f Bernd Bleßmann
$form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
d319704a Moritz Bunkus
# reverse paid
b8916e5c Sven Schöling
$form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
$form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
f45b296f Bernd Bleßmann
$form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
b8916e5c Sven Schöling
$form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
$form->{"forex_$i"} = $form->{"exchangerate_$i"};
$form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
$form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
d319704a Moritz Bunkus
$form->{paidaccounts} = $i;
}
} else {
b8916e5c Sven Schöling
$form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
d319704a Moritz Bunkus
}
}

$form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});

$form->{AR} = $form->{AR_1} unless $form->{id};

b8916e5c Sven Schöling
$form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
<= $form->datetonum($form->{closedto}, \%myconfig));
d319704a Moritz Bunkus
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub prepare_invoice {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
0bb0eb67 Stephan Köhler
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
54e4131e Moritz Bunkus
if ($form->{type} eq "credit_note") {
$form->{type} = "credit_note";
$form->{formname} = "credit_note";
7ca30d76 Martin Helmling
} elsif ($form->{formname} eq "proforma" ) {
$form->{type} = "invoice";
54e4131e Moritz Bunkus
} else {
$form->{type} = "invoice";
$form->{formname} = "invoice";
}
d319704a Moritz Bunkus
if ($form->{id}) {

fcd7e932 Moritz Bunkus
my $i = 0;

5486b4a9 Sven Schöling
foreach my $ref (@{ $form->{invoice_details} }) {
d319704a Moritz Bunkus
$i++;
54e4131e Moritz Bunkus
d319704a Moritz Bunkus
map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};

a0a30f41 Sven Schöling
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
$dec = length $dec;
my $decimalplaces = ($dec > 2) ? $dec : 2;
0bb0eb67 Stephan Köhler
a0a30f41 Sven Schöling
$form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
(my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
$dec_qty = length $dec_qty;
0bb0eb67 Stephan Köhler
da804bf2 Geoffrey Richardson
$form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);

a0a30f41 Sven Schöling
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
d319704a Moritz Bunkus
7d2c6995 Bernd Bleßmann
$form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"});

a0a30f41 Sven Schöling
$form->{rowcount} = $i;
07d71c33 Stephan Köhler
d319704a Moritz Bunkus
}
}
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

e46a3e27 Moritz Bunkus
sub setup_is_action_bar {
my $form = $::form;
my $change_never = $::instance_conf->get_is_changeable == 0;
my $change_on_same_day_only = $::instance_conf->get_is_changeable == 2 && ($form->current_date(\%::myconfig) ne $form->{gldate});
my @req_trans_desc = qw(kivi.SalesPurchase.check_transaction_description) x!!$::instance_conf->get_require_transaction_description_ps;

for my $bar ($::request->layout->get('actionbar')) {
ff424b75 Moritz Bunkus
$bar->add(
action => [
t8('Update'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "update" } ],
ff424b75 Moritz Bunkus
disabled => $form->{locked} ? t8('The billing period has already been locked.') : undef,
id => 'update_button',
accesskey => 'enter',
],

combobox => [
action => [
t8('Post'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "post" } ],
ff424b75 Moritz Bunkus
checks => [ @req_trans_desc ],
disabled => $form->{locked} ? t8('The billing period has already been locked.')
: $form->{storno} ? t8('A canceled invoice cannot be posted.')
: ($form->{id} && $change_never) ? t8('Changing invoices has been disabled in the configuration.')
: ($form->{id} && $change_on_same_day_only) ? t8('Invoices can only be changed on the day they are posted.')
: undef,
],
action => [
t8('Post Payment'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "post_payment" } ],
ff424b75 Moritz Bunkus
checks => [ @req_trans_desc ],
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
36c8af52 Moritz Bunkus
action => [ t8('Mark as paid'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "mark_as_paid" } ],
ff424b75 Moritz Bunkus
confirm => t8('This will remove the invoice from showing as unpaid even if the unpaid amount does not match the amount. Proceed?'),
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
36c8af52 Moritz Bunkus
only_if => $::instance_conf->get_is_show_mark_as_paid,
],
ff424b75 Moritz Bunkus
], # end of combobox "Post"

combobox => [
action => [ t8('Storno'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "storno" } ],
ff424b75 Moritz Bunkus
confirm => t8('Do you really want to cancel this invoice?'),
checks => [ @req_trans_desc ],
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
action => [ t8('Delete'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "delete" } ],
ff424b75 Moritz Bunkus
confirm => t8('Do you really want to delete this object?'),
checks => [ @req_trans_desc ],
disabled => !$form->{id} ? t8('This invoice has not been posted yet.')
: $form->{locked} ? t8('The billing period has already been locked.')
: $change_never ? t8('Changing invoices has been disabled in the configuration.')
: $change_on_same_day_only ? t8('Invoices can only be changed on the day they are posted.')
: undef,
],
], # end of combobox "Storno"

'separator',

combobox => [
action => [ t8('Workflow') ],
action => [
t8('Use As New'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "use_as_new" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
action => [
t8('Credit Note'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "credit_note" } ],
ff424b75 Moritz Bunkus
checks => [ @req_trans_desc ],
disabled => $form->{type} eq "credit_note" ? t8('Credit notes cannot be converted into other credit notes.')
: !$form->{id} ? t8('This invoice has not been posted yet.')
: undef,
],
action => [
t8('Sales Order'),
1a9f4456 Moritz Bunkus
submit => [ '#form', { action => "order" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
], # end of combobox "Workflow"

combobox => [
action => [ t8('Export') ],
action => [
($form->{id} ? t8('Print') : t8('Preview')),
10852277 Moritz Bunkus
call => [ 'kivi.SalesPurchase.show_print_dialog', $form->{id} ? 'print' : 'preview' ],
ff424b75 Moritz Bunkus
checks => [ @req_trans_desc ],
disabled => !$form->{id} && $form->{locked} ? t8('The billing period has already been locked.') : undef,
],
ac41603d freiphone
action => [ t8('Print and Post'),
call => [ 'kivi.SalesPurchase.show_print_dialog', $form->{id} ? 'print' : 'print_and_post' ],
checks => [ @req_trans_desc ],
disabled => $form->{id} ? t8('This invoice has already been posted.') : undef,,
],
ff424b75 Moritz Bunkus
action => [ t8('E Mail'),
01ce6afc Moritz Bunkus
call => [ 'kivi.SalesPurchase.show_email_dialog' ],
ff424b75 Moritz Bunkus
checks => [ @req_trans_desc ],
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
], # end of combobox "Export"

combobox => [
action => [ t8('more') ],
action => [
t8('History'),
16fc9cde Moritz Bunkus
call => [ 'set_history_window', $form->{id} * 1, 'glid' ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
action => [
t8('Follow-Up'),
call => [ 'follow_up_window' ],
disabled => !$form->{id} ? t8('This invoice has not been posted yet.') : undef,
],
action => [
t8('Drafts'),
call => [ 'kivi.Draft.popup', 'is', 'invoice', $form->{draft_id}, $form->{draft_description} ],
disabled => $form->{id} ? t8('This invoice has already been posted.')
: $form->{locked} ? t8('The billing period has already been locked.')
: undef,
],
], # end of combobox "more"
);
e46a3e27 Moritz Bunkus
}
}

d319704a Moritz Bunkus
sub form_header {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
5494f687 Sven Schöling
my $cgi = $::request->{cgi};
ef73414c Stephan Köhler
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
662ded38 Sven Schöling
my %TMPL_VAR = ();
4cc5904b Sven Schöling
my @custom_hiddens;

a99fc9f6 Moritz Bunkus
$TMPL_VAR{customer_obj} = SL::DB::Customer->load_cached($form->{customer_id}) if $form->{customer_id};
$TMPL_VAR{invoice_obj} = SL::DB::Invoice->load_cached($form->{id}) if $form->{id};

08e820fa Moritz Bunkus
my $current_employee = SL::DB::Manager::Employee->current;
$form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
$form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
$form->{employee_id} ||= $current_employee->id;
$form->{salesman_id} ||= $current_employee->id;
0cb4ad8c Philip Reetz
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
9e117fbd Philip Reetz
6e603868 Geoffrey Richardson
$form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
1e251313 Moritz Bunkus
"currencies" => "ALL_CURRENCIES",
"price_factors" => "ALL_PRICE_FACTORS");
54e4131e Moritz Bunkus
63a18cca Moritz Bunkus
$form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
ffd06003 Geoffrey Richardson
41adf433 Moritz Bunkus
# Projects
my @old_project_ids = uniq grep { $_ } map { $_ * 1 } ($form->{"globalproject_id"}, map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
my @old_ids_cond = @old_project_ids ? (id => \@old_project_ids) : ();
my @customer_cond;
if ($::instance_conf->get_customer_projects_only_in_sales) {
@customer_cond = (
or => [
customer_id => $::form->{customer_id},
billable_customer_id => $::form->{customer_id},
]);
}
my @conditions = (
or => [
and => [ active => 1, @customer_cond ],
@old_ids_cond,
]);

fc6a8e32 Moritz Bunkus
$TMPL_VAR{ALL_PROJECTS} = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
2ad2d77d Geoffrey Richardson
$form->{ALL_PROJECTS} = $TMPL_VAR{ALL_PROJECTS}; # make projects available for second row drop-down in io.pl
d139dd31 Moritz Bunkus
$TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
$TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
5b8e9fcb Moritz Bunkus
$TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all_sorted(query => [
752fd6ad Sven Schöling
or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
9b67d27c Sven Schöling
]);
3816bb51 Moritz Bunkus
$TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
cb2abccd Sven Schöling
or => [
cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
and => [
cp_cv_id => undef,
cp_id => $::form->{cp_id} * 1
]
]
]);
4cc5904b Sven Schöling
# currencies and exchangerate
my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
$form->{currency} = $form->{defaultcurrency} unless $form->{currency};
f5863b87 Sven Schöling
$form->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
5494f687 Sven Schöling
$TMPL_VAR{currencies} = NTI($::request->{cgi}->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
69f32836 Bernd Bleßmann
'-values' => \@values, '-labels' => \%labels,
'-onchange' => "document.getElementById('update_button').click();"
)) if scalar @values;
4cc5904b Sven Schöling
push @custom_hiddens, "forex";
push @custom_hiddens, "exchangerate" if $form->{forex};

$TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
$TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
40662c08 Thomas Kasulke
4cc5904b Sven Schöling
# set option selected
ee792a3f Sven Schöling
foreach my $item (qw(AR)) {
d319704a Moritz Bunkus
$form->{"select$item"} =~ s/ selected//;
a05eead3 Sven Schöling
$form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
d319704a Moritz Bunkus
}

ee792a3f Sven Schöling
$TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
$TMPL_VAR{is_format_html} = $form->{format} eq 'html';
$TMPL_VAR{dateformat} = $myconfig{dateformat};
$TMPL_VAR{numberformat} = $myconfig{numberformat};
d319704a Moritz Bunkus
4cc5904b Sven Schöling
# hiddens
fce441db Sven Schöling
$TMPL_VAR{HIDDENS} = [qw(
01ce6afc Moritz Bunkus
id type queued printed emailed vc discount
fce441db Sven Schöling
title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
4bcc6d52 Moritz Bunkus
max_dunning_level dunning_amount dunning_description
01ce6afc Moritz Bunkus
taxaccounts cursor_fokus
b8125c17 Bernd Bleßmann
convert_from_do_ids convert_from_oe_ids convert_from_ar_ids useasnew
f087c373 Jan Büren
invoice_id
71a0f6e9 Niclas Zimmermann
show_details
4cc5904b Sven Schöling
), @custom_hiddens,
map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
d319704a Moritz Bunkus
3efb443e Sven Schöling
$::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.Draft kivi.File kivi.SalesPurchase kivi.Part kivi.CustomerVendor kivi.Validator ckeditor/ckeditor ckeditor/adapters/jquery kivi.io client_js));
5bc87ade Moritz Bunkus
$TMPL_VAR{payment_terms_obj} = get_payment_terms_for_invoice();
278eb433 Moritz Bunkus
$form->{duedate} = $TMPL_VAR{payment_terms_obj}->calc_date(reference_date => $form->{invdate}, due_date => $form->{duedate})->to_kivitendo if $TMPL_VAR{payment_terms_obj};
ff058663 Moritz Bunkus
e46a3e27 Moritz Bunkus
setup_is_action_bar();
70d63c04 Sven Schöling
4cc5904b Sven Schöling
$form->header();

print $form->parse_html_template("is/form_header", \%TMPL_VAR);
52983c08 Sven Schöling
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

0d37f1e8 Moritz Bunkus
sub _sort_payments {
my @fields = qw(acc_trans_id gldate datepaid source memo paid AR_paid);
my @payments =
grep { $_->{paid} != 0 }
map {
my $idx = $_;
+{ map { ($_ => delete($::form->{"${_}_${idx}"})) } @fields }
} (1..$::form->{paidaccounts});

@payments = sort_by { DateTime->from_kivitendo($_->{datepaid}) } @payments;

$::form->{paidaccounts} = max scalar(@payments), 1;

foreach my $idx (1 .. scalar(@payments)) {
my $payment = $payments[$idx - 1];
$::form->{"${_}_${idx}"} = $payment->{$_} for @fields;
}
}

d319704a Moritz Bunkus
sub form_footer {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
0bb0eb67 Stephan Köhler
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
de6cead5 Sven Schöling
$form->{invtotal} = $form->{invsubtotal};

# note rows
$form->{rows} = max 2,
$form->numtextrows($form->{notes}, 26, 8),
$form->numtextrows($form->{intnotes}, 35, 8);
d319704a Moritz Bunkus

de6cead5 Sven Schöling
# tax, total and subtotal calculations
5486b4a9 Sven Schöling
my ($tax, $subtotal);
3c416dd9 Thomas Heck
$form->{taxaccounts_array} = [ split(/ /, $form->{taxaccounts}) ];

2174b7ba Thomas Heck
if( $form->{customer_id} && !$form->{taxincluded_changed_by_user} ) {
a99fc9f6 Moritz Bunkus
my $customer = SL::DB::Customer->load_cached($form->{customer_id});
6e08e454 Thomas Heck
$form->{taxincluded} = defined($customer->taxincluded_checked) ? $customer->taxincluded_checked : $myconfig{taxincluded_checked};
7bff84cb Thomas Heck
}

7130d91e Sven Schöling
foreach my $item (@{ $form->{taxaccounts_array} }) {
if ($form->{"${item}_base"}) {
if ($form->{taxincluded}) {
$form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
/ (1 + $form->{"${item}_rate"})), 2);
$form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
} else {
58d115ff Sven Schöling
$form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
d319704a Moritz Bunkus
$form->{invtotal} += $form->{"${item}_total"};
}
}
}

030c2086 Rolf Fluehmann
my $grossamount = $form->{invtotal};
$form->{invtotal} = $form->round_amount( $form->{invtotal}, 2, 1 );
$form->{rounding} = $form->round_amount(
$form->{invtotal} - $form->round_amount($grossamount, 2),
2
);

b2c3a161 Sven Schöling
# follow ups
7a7f33b5 Moritz Bunkus
if ($form->{id}) {
a7b92129 Waldemar Toews
$form->{follow_ups} = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1) || [];
94b71f17 Sven Schöling
$form->{follow_ups_unfinished} = ( sum map { $_->{due} * 1 } @{ $form->{follow_ups} } ) || 0;
d319704a Moritz Bunkus
}

de6cead5 Sven Schöling
# payments
0d37f1e8 Moritz Bunkus
_sort_payments();

7130d91e Sven Schöling
my $totalpaid = 0;
$form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
$form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];

c0249f11 Jan Büren
# Standard Konto für Umlaufvermögen
my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);

7130d91e Sven Schöling
for my $i (1 .. $form->{paidaccounts}) {
f45b296f Bernd Bleßmann
$form->{"changeable_$i"} = 1;
97954312 Bernd Bleßmann
if (SL::DB::Default->get->payments_changeable == 0) {
f45b296f Bernd Bleßmann
# never
$form->{"changeable_$i"} = ($form->{"acc_trans_id_$i"})? 0 : 1;
97954312 Bernd Bleßmann
} elsif (SL::DB::Default->get->payments_changeable == 2) {
f45b296f Bernd Bleßmann
# on the same day
c79db65d Bernd Bleßmann
$form->{"changeable_$i"} = (($form->{"gldate_$i"} eq '') ||
f45b296f Bernd Bleßmann
($form->current_date(\%myconfig) eq $form->{"gldate_$i"}));
}

60fd9199 Frank Messerschmidt
#deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
if ($form->date_closed($form->{"gldate_$i"})) {
$form->{"changeable_$i"} = 0;
}

7130d91e Sven Schöling
$form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
c0249f11 Jan Büren
if (!$form->{"AR_paid_$i"}) {
$form->{"selectAR_paid_$i"} =~ s/option>$accno_arap--(.*?)</option selected>$accno_arap--$1</;
} else {
$form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
}

7130d91e Sven Schöling
$totalpaid += $form->{"paid_$i"};
}

22e89662 Sven Schöling
$form->{oldinvtotal} = $form->{invtotal};

03d3d025 Bernd Bleßmann
$form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();

de6cead5 Sven Schöling
print $form->parse_html_template('is/form_footer', {
is_type_credit_note => ($form->{type} eq "credit_note"),
totalpaid => $totalpaid,
paid_missing => $form->{invtotal} - $totalpaid,
01ce6afc Moritz Bunkus
print_options => setup_sales_purchase_print_options(),
de6cead5 Sven Schöling
show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && !$totalpaid,
4ac2976f Bernd Bleßmann
show_delete => ($::instance_conf->get_is_changeable == 2)
? ($form->current_date(\%myconfig) eq $form->{gldate})
: ($::instance_conf->get_is_changeable == 1),
5058c99d Sven Schöling
today => DateTime->today,
160fa214 Moritz Bunkus
vc_obj => $form->{customer_id} ? SL::DB::Customer->load_cached($form->{customer_id}) : undef,
de6cead5 Sven Schöling
});
##print $form->parse_html_template('is/_payments'); # parser
70ccecab Sven Schöling
##print $form->parse_html_template('webdav/_list'); # parser
7130d91e Sven Schöling
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

ce47a234 Thomas Kasulke
sub mark_as_paid {
59adfa36 Moritz Bunkus
$::auth->assert('invoice_edit');
5486b4a9 Sven Schöling
59adfa36 Moritz Bunkus
SL::DB::Invoice->new(id => $::form->{id})->load->mark_as_paid;
8c7e4493 Moritz Bunkus
59adfa36 Moritz Bunkus
$::form->redirect($::locale->text("Marked as paid"));
ce47a234 Thomas Kasulke
}

46aa98a9 Martin Helmling
sub show_draft {
42961371 Jan Büren
# unless no lazy implementation of save draft without invdate
# set the current date like in version <= 3.4.1
$::form->{invdate} = DateTime->today->to_lxoffice;
46aa98a9 Martin Helmling
update();
}

d319704a Moritz Bunkus
sub update {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
d319704a Moritz Bunkus
5486b4a9 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;

$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
b95c5658 Moritz Bunkus
my ($recursive_call) = @_;
ac517355 Philip Reetz
52983c08 Sven Schöling
$form->{print_and_post} = 0 if $form->{second_run};
74fca575 Sven Schöling
my $taxincluded = $form->{taxincluded} ? "checked" : '';
54e4131e Moritz Bunkus
$form->{update} = 1;

a99fc9f6 Moritz Bunkus
if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
$::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};

IS->get_customer(\%myconfig, $form);
}
d319704a Moritz Bunkus
52983c08 Sven Schöling
$form->{taxincluded} ||= $taxincluded;
d1d9b5a7 Thomas Kasulke
2e26ccd5 Bernd Bleßmann
if (!$form->{forex}) { # read exchangerate from input field (not hidden)
$form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate}) unless $recursive_call;
}
a53233e5 Sven Schöling
$form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
$form->{exchangerate} = $form->{forex} if $form->{forex};
d319704a Moritz Bunkus
5486b4a9 Sven Schöling
for my $i (1 .. $form->{paidaccounts}) {
52983c08 Sven Schöling
next unless $form->{"paid_$i"};
map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
175dd1fb Niclas Zimmermann
if (!$form->{"forex_$i"}) { #read exchangerate from input field (not hidden)
$form->{exchangerate} = $form->{"exchangerate_$i"};
6881b4bd Thomas Heck
}
a53233e5 Sven Schöling
$form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
$form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
d319704a Moritz Bunkus
}

5486b4a9 Sven Schöling
my $i = $form->{rowcount};
my $exchangerate = $form->{exchangerate} || 1;
d319704a Moritz Bunkus
# if last row empty, check the form otherwise retrieve new item
if ( ($form->{"partnumber_$i"} eq "")
&& ($form->{"description_$i"} eq "")
&& ($form->{"partsgroup_$i"} eq "")) {

$form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
&check_form;

54e4131e Moritz Bunkus
} else {

d319704a Moritz Bunkus
IS->retrieve_item(\%myconfig, \%$form);

5486b4a9 Sven Schöling
my $rows = scalar @{ $form->{item_list} };
d319704a Moritz Bunkus
97908d77 Bernd Bleßmann
$form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
$form->{"discount_$i"} ||= $form->{customer_discount};
d319704a Moritz Bunkus
if ($rows) {
2a61343f Thomas Heck
$form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
if( !$form->{"qty_$i"} ) {
$form->{"qty_$i"} = 1;
}
d319704a Moritz Bunkus
if ($rows > 1) {

6ea1dd9b Bernd Bleßmann
select_item(mode => 'IS', pre_entered_qty => $form->{"qty_$i"});
09479f02 Moritz Bunkus
$::dispatcher->end_request;
d319704a Moritz Bunkus
} else {

5486b4a9 Sven Schöling
my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
ef73414c Stephan Köhler
52983c08 Sven Schöling
map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
5486b4a9 Sven Schöling
52983c08 Sven Schöling
$form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
$form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
d319704a Moritz Bunkus
1e251313 Moritz Bunkus
$form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};

52983c08 Sven Schöling
($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
5486b4a9 Sven Schöling
my $decimalplaces = max 2, length $1;
d319704a Moritz Bunkus
if ($sellprice) {
$form->{"sellprice_$i"} = $sellprice;
} else {
89b26688 Sven Schöling
my $record = _make_record();
my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
my $best_price = $price_source->best_price;
my $best_discount = $price_source->best_discount;

if ($best_price) {
$::form->{"sellprice_$i"} = $best_price->price;
$::form->{"active_price_source_$i"} = $best_price->source;
}
if ($best_discount) {
$::form->{"discount_$i"} = $best_discount->discount;
$::form->{"active_discount_source_$i"} = $best_discount->source;
}

d319704a Moritz Bunkus
# if there is an exchange rate adjust sellprice
$form->{"sellprice_$i"} /= $exchangerate;
}

$form->{"listprice_$i"} /= $exchangerate;

97908d77 Bernd Bleßmann
my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
52983c08 Sven Schöling
map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
d319704a Moritz Bunkus
$form->{creditremaining} -= $amount;

58eae092 Geoffrey Richardson
map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost);
d319704a Moritz Bunkus
97908d77 Bernd Bleßmann
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
d319704a Moritz Bunkus
}

&display_form;

} else {

# ok, so this is a new part
# ask if it is a part or service item

if ( $form->{"partsgroup_$i"}
&& ($form->{"partsnumber_$i"} eq "")
&& ($form->{"description_$i"} eq "")) {
$form->{rowcount}--;
$form->{"discount_$i"} = "";
d83df93a Moritz Bunkus
display_form();
d319704a Moritz Bunkus
d83df93a Moritz Bunkus
} else {
d319704a Moritz Bunkus
$form->{"id_$i"} = 0;
d83df93a Moritz Bunkus
new_item();
d319704a Moritz Bunkus
}
}
}
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}
2b89ec97 Thomas Kasulke
54e4131e Moritz Bunkus
sub post_payment {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

$main::auth->assert('invoice_edit');
fb37acdc Moritz Bunkus
d735aab3 Martin Helmling
$form->mtime_ischanged('ar') ;
662ded38 Sven Schöling
my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
8c7e4493 Moritz Bunkus
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
5486b4a9 Sven Schöling
for my $i (1 .. $form->{paidaccounts}) {
bd32b607 Philip Reetz
if ($form->{"paid_$i"}) {
5486b4a9 Sven Schöling
my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
54e4131e Moritz Bunkus
$form->isblank("datepaid_$i", $locale->text('Payment date missing!'));


if ($form->{currency} ne $form->{defaultcurrency}) {
$form->{"exchangerate_$i"} = $form->{exchangerate}
if ($invdate == $datepaid);
$form->isblank("exchangerate_$i",
$locale->text('Exchangerate for payment missing!'));
}
60fd9199 Frank Messerschmidt
$form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));

#Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
# (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
$form->error($locale->text('Cannot post payment for a closed period!'))
if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
54e4131e Moritz Bunkus
}
}

($form->{AR}) = split /--/, $form->{AR};
($form->{AR_paid}) = split /--/, $form->{AR_paid};
b3501bdf Moritz Bunkus
relink_accounts();
7a98647b Geoffrey Richardson
if ( IS->post_payment(\%myconfig, \%$form) ) {
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
281696eb Geoffrey Richardson
$form->{what_done} = 'invoice';
7a98647b Geoffrey Richardson
$form->{addition} = "PAYMENT POSTED";
$form->save_history;
$form->redirect($locale->text('Payment posted!'))
} else {
$form->error($locale->text('Cannot post payment!'));
};
54e4131e Moritz Bunkus
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}
d319704a Moritz Bunkus
sub post {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
fb37acdc Moritz Bunkus
5486b4a9 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

$main::auth->assert('invoice_edit');
d735aab3 Martin Helmling
$form->mtime_ischanged('ar');
8c7e4493 Moritz Bunkus
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
d319704a Moritz Bunkus
$form->isblank("invdate", $locale->text('Invoice Date missing!'));
a99fc9f6 Moritz Bunkus
$form->isblank("customer_id", $locale->text('Customer missing!'));
9e06d0e4 Philip Reetz
$form->error($locale->text('Cannot post invoice for a closed period!'))
if ($form->date_closed($form->{"invdate"}, \%myconfig));
d319704a Moritz Bunkus
a426a839 Moritz Bunkus
$form->{invnumber} =~ s/^\s*//g;
$form->{invnumber} =~ s/\s*$//g;

d319704a Moritz Bunkus
# if oldcustomer ne customer redo form
a99fc9f6 Moritz Bunkus
if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) {
d319704a Moritz Bunkus
&update;
09479f02 Moritz Bunkus
$::dispatcher->end_request;
d319704a Moritz Bunkus
}
05174f19 Sven Schöling
if ($myconfig{mandatory_departments} && !$form->{department_id}) {
$form->{saved_message} = $::locale->text('You have to specify a department.');
update();
exit;
}

07d71c33 Stephan Köhler
if ($form->{second_run}) {
$form->{print_and_post} = 0;
}
d319704a Moritz Bunkus
0a5317e7 Moritz Bunkus
remove_emptied_rows();
d319704a Moritz Bunkus
&validate_items;

5486b4a9 Sven Schöling
my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
d319704a Moritz Bunkus
a11d7a85 Jan Büren
$form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
if ($form->date_max_future($invdate, \%myconfig));
d319704a Moritz Bunkus
$form->error($locale->text('Cannot post invoice for a closed period!'))
if ($invdate <= $closedto);

$form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
if ($form->{currency} ne $form->{defaultcurrency});

5486b4a9 Sven Schöling
for my $i (1 .. $form->{paidaccounts}) {
5563e116 Moritz Bunkus
if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
5486b4a9 Sven Schöling
my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
d319704a Moritz Bunkus
$form->isblank("datepaid_$i", $locale->text('Payment date missing!'));

60fd9199 Frank Messerschmidt
$form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));

#Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
# (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
d319704a Moritz Bunkus
$form->error($locale->text('Cannot post payment for a closed period!'))
60fd9199 Frank Messerschmidt
if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
d319704a Moritz Bunkus
if ($form->{currency} ne $form->{defaultcurrency}) {
$form->{"exchangerate_$i"} = $form->{exchangerate}
if ($invdate == $datepaid);
$form->isblank("exchangerate_$i",
$locale->text('Exchangerate for payment missing!'));
}
}
}

34e47354 Moritz Bunkus
($form->{AR}) = split /--/, $form->{AR};
($form->{AR_paid}) = split /--/, $form->{AR_paid};
$form->{storno} ||= 0;
d319704a Moritz Bunkus
d3d239c9 Thomas Heck
$form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice');
d319704a Moritz Bunkus
$form->{id} = 0 if $form->{postasnew};

6dce043f Stephan Köhler
# get new invnumber in sequence if no invnumber is given or if posasnew was requested
7712480e Moritz Bunkus
if ($form->{postasnew}) {
54e4131e Moritz Bunkus
if ($form->{type} eq "credit_note") {
7712480e Moritz Bunkus
undef($form->{cnnumber});
54e4131e Moritz Bunkus
} else {
7712480e Moritz Bunkus
undef($form->{invnumber});
54e4131e Moritz Bunkus
}
6dce043f Stephan Köhler
}
7712480e Moritz Bunkus
b3501bdf Moritz Bunkus
relink_accounts();
9d07c34f Bernd Bleßmann
5bc87ade Moritz Bunkus
my $terms = get_payment_terms_for_invoice();
2d4d9d7f Moritz Bunkus
$form->{duedate} = $terms->calc_date(reference_date => $form->{invdate}, due_date => $form->{duedate})->to_kivitendo if $terms;
5bc87ade Moritz Bunkus
9d07c34f Bernd Bleßmann
# If transfer_out is requested, get rose db handle and do post and
# transfer out in one transaction. Otherwise just post the invoice.
0560b39e Bernd Bleßmann
if ($::instance_conf->get_is_transfer_out && $form->{type} ne 'credit_note' && !$form->{storno}) {
9d07c34f Bernd Bleßmann
require SL::DB::Inventory;
my $rose_db = SL::DB::Inventory->new->db;
f4431e33 Bernd Bleßmann
my @errors;
9d07c34f Bernd Bleßmann
if (!$rose_db->with_transaction(sub {
if (!eval {
f4431e33 Bernd Bleßmann
if (!IS->post_invoice(\%myconfig, \%$form, $rose_db->dbh)) {
push @errors, $locale->text('Cannot post invoice!');
die 'posting error';
}
my $err = IS->transfer_out(\%$form, $rose_db->dbh);
if (@{ $err }) {
push @errors, @{ $err };
die 'transfer error';
}

9d07c34f Bernd Bleßmann
1;
}) {
f4431e33 Bernd Bleßmann
push @errors, $EVAL_ERROR;
b151cbc8 Jan Büren
$form->error($locale->text('Cannot post invoice and/or transfer out! Error message:') . "\n" . join("\n", @errors));
9d07c34f Bernd Bleßmann
}

1;
})) {
f4431e33 Bernd Bleßmann
push @errors, $rose_db->error;
$form->error($locale->text('Cannot post invoice and/or transfer out! Error message:') . "\n" . join("\n", @errors));
9d07c34f Bernd Bleßmann
}
} else {
if (!IS->post_invoice(\%myconfig, \%$form)) {
$form->error($locale->text('Cannot post invoice!'));
}
}

d946f59b Sven Schöling
if(!exists $form->{addition}) {
281696eb Geoffrey Richardson
$form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
$form->{what_done} = 'invoice';
$form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" :
$form->{storno} ? "STORNO" :
"POSTED";
a590a651 Sven Schöling
$form->save_history;
383022ae Stephan Köhler
}
fb4d2ffa Moritz Bunkus
382e874c Sven Schöling
if (!$form->{no_redirect_after_post}) {
$form->{action} = 'edit';
$form->{script} = 'is.pl';
a03aab55 Bernd Bleßmann
$form->{callback} = build_std_url(qw(action edit id callback saved_message));
177cb111 Moritz Bunkus
$form->redirect($form->{label} . " $form->{invnumber} " . $locale->text('posted!'));
382e874c Sven Schöling
}
d319704a Moritz Bunkus
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

383022ae Stephan Köhler
sub print_and_post {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
383022ae Stephan Köhler
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
c607fb40 Sven Schöling
my $old_form = Form->new;
382e874c Sven Schöling
$form->{no_redirect_after_post} = 1;
$form->{print_and_post} = 1;
383022ae Stephan Köhler
&post();
07d71c33 Stephan Köhler
8f3dc0b4 Stephan Köhler
&edit();
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
383022ae Stephan Köhler
}

3cd3e832 Moritz Bunkus
sub use_as_new {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
98764afa Philip Reetz
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
dfe15b56 Geoffrey Richardson
delete @{ $form }{qw(printed emailed queued invnumber invdate exchangerate forex deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno locked)};
98764afa Philip Reetz
$form->{rowcount}--;
dfe15b56 Geoffrey Richardson
$form->{paidaccounts} = 1;
$form->{invdate} = $form->current_date(\%myconfig);
5bc87ade Moritz Bunkus
my $terms = get_payment_terms_for_invoice();
$form->{duedate} = $terms ? $terms->calc_date(reference_date => $form->{invdate})->to_kivitendo : $form->{invdate};
dfe15b56 Geoffrey Richardson
$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};
ef92528c Geoffrey Richardson
b8125c17 Bernd Bleßmann
$form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};

$form->{useasnew} = 1;
98764afa Philip Reetz
&display_form;

5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
98764afa Philip Reetz
}

54e4131e Moritz Bunkus
sub storno {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
54e4131e Moritz Bunkus
5486b4a9 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
54e4131e Moritz Bunkus
if ($form->{storno}) {
$form->error($locale->text('Cannot storno storno invoice!'));
}

a1a3bfd8 Moritz Bunkus
if (IS->has_storno(\%myconfig, $form, "ar")) {
213317d3 Moritz Bunkus
$form->error($locale->text("Invoice has already been storno'd!"));
}
451ad2fc Sven Schöling
if ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig)) {
$form->error($locale->text('Cannot storno invoice for a closed period!'));
}
213317d3 Moritz Bunkus
ed76c91e Geoffrey Richardson
# save the history of invoice being stornoed
281696eb Geoffrey Richardson
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
$form->{what_done} = 'invoice';
$form->{addition} = "STORNO";
ed76c91e Geoffrey Richardson
$form->save_history;

0f179c9a Sven Schöling
map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form }));
7bc5598c Moritz Bunkus
3740b503 Moritz Bunkus
invoice_links();
prepare_invoice();
7bc5598c Moritz Bunkus
relink_accounts();

3740b503 Moritz Bunkus
# Payments must not be recorded for the new storno invoice.
$form->{paidaccounts} = 0;
f45b296f Bernd Bleßmann
map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
3740b503 Moritz Bunkus
210ea1a8 Jan Büren
# record link invoice to storno
$form->{convert_from_ar_ids} = $form->{id};
54e4131e Moritz Bunkus
$form->{storno_id} = $form->{id};
$form->{storno} = 1;
$form->{id} = "";
$form->{invnumber} = "Storno zu " . $form->{invnumber};
009b0731 Sven Schöling
$form->{invdate} = DateTime->today->to_lxoffice;
3740b503 Moritz Bunkus
$form->{rowcount}++;
f087c373 Jan Büren
# set new ids for storno invoice
210ea1a8 Jan Büren
# set new persistent ids for storno invoice items
$form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
54e4131e Moritz Bunkus
3740b503 Moritz Bunkus
post();
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}

383022ae Stephan Köhler
sub preview {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
383022ae Stephan Köhler
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
383022ae Stephan Köhler
$form->{preview} = 1;
c607fb40 Sven Schöling
my $old_form = Form->new;
383022ae Stephan Köhler
for (keys %$form) { $old_form->{$_} = $form->{$_} }

&print_form($old_form);
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
383022ae Stephan Köhler
}

54e4131e Moritz Bunkus
sub credit_note {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();
54e4131e Moritz Bunkus
5486b4a9 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
54e4131e Moritz Bunkus
$form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
$form->{duedate} =
$form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);

001155e4 Sven Schöling
$form->{convert_from_ar_ids} = $form->{id};
54e4131e Moritz Bunkus
$form->{id} = '';
$form->{rowcount}--;


$form->{title} = $locale->text('Add Credit Note');
$form->{script} = 'is.pl';
fb4d2ffa Moritz Bunkus
d658bec8 Jan Büren
# Bei Gutschriften bezug zur Rechnungsnummer
$form->{invnumber_for_credit_note} = $form->{invnumber};
54e4131e Moritz Bunkus
# bo creates the id, reset it
map { delete $form->{$_} }
qw(id invnumber subject message cc bcc printed emailed queued);
$form->{ $form->{vc} } =~ s/--.*//g;
$form->{type} = "credit_note";


5486b4a9 Sven Schöling
map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
54e4131e Moritz Bunkus
a2777d1a Sven Schöling
# map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
# qw(creditlimit creditremaining);
54e4131e Moritz Bunkus
991d16bb Jan Büren
# set new persistent ids for credit note and link previous invoice id
$form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
58eae092 Geoffrey Richardson
5486b4a9 Sven Schöling
my $currency = $form->{currency};
54e4131e Moritz Bunkus
&invoice_links;

$form->{currency} = $currency;
662ded38 Sven Schöling
$form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy');
a53233e5 Sven Schöling
$form->{exchangerate} = $form->{forex} || '';
54e4131e Moritz Bunkus
$form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});

3b21dee3 Geoffrey Richardson
# bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen
for my $i (1 .. $form->{paidaccounts}) {
delete $form->{"paid_$i"};
delete $form->{"source_$i"};
delete $form->{"memo_$i"};
delete $form->{"datepaid_$i"};
f45b296f Bernd Bleßmann
delete $form->{"gldate_$i"};
delete $form->{"acc_trans_id_$i"};
3b21dee3 Geoffrey Richardson
delete $form->{"AR_paid_$i"};
};
$form->{paidaccounts} = 1;

54e4131e Moritz Bunkus
&prepare_invoice;


&display_form;

5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}

a2546588 Sven Schöling
sub display_form {
$::lxdebug->enter_sub;

$::auth->assert('invoice_edit');

relink_accounts();

my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
$::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};

$::form->language_payment(\%::myconfig);

Common::webdav_folder($::form);

form_header();
display_row(++$::form->{rowcount});
form_footer();

$::lxdebug->leave_sub;
}

642d2475 Sven Schöling
sub delete {
$::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
642d2475 Sven Schöling
if (IS->delete_invoice(\%::myconfig, $::form)) {
2b89ec97 Thomas Kasulke
# saving the history
642d2475 Sven Schöling
if(!exists $::form->{addition}) {
$::form->{snumbers} = 'invnumber' .'_'. $::form->{invnumber};
$::form->{what_done} = 'invoice';
$::form->{addition} = "DELETED";
$::form->save_history;
2b89ec97 Thomas Kasulke
}
fb4d2ffa Moritz Bunkus
# /saving the history
642d2475 Sven Schöling
$::form->redirect($::locale->text('Invoice deleted!'));
2b89ec97 Thomas Kasulke
}
642d2475 Sven Schöling
$::form->error($::locale->text('Cannot delete invoice!'));
d319704a Moritz Bunkus
}
d946f59b Sven Schöling
0a50b37c Sven Schöling
sub dispatcher {
for my $action (qw(
a6ce2c7f Moritz Bunkus
print update ship_to storno post_payment use_as_new credit_note
62726dfd Sven Schöling
delete post order preview post_and_e_mail print_and_post
0a50b37c Sven Schöling
mark_as_paid
)) {
if ($::form->{"action_$action"}) {
call_sub($action);
return;
}
}

$::form->error($::locale->text('No action defined.'));
}