Projekt

Allgemein

Profil

Herunterladen (33,2 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
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
#
# Inventory invoicing module
#
#======================================================================

7a7f33b5 Moritz Bunkus
use SL::FU;
d319704a Moritz Bunkus
use SL::IS;
use SL::PE;
1465da30 Sven Schöling
use SL::OE;
07d71c33 Stephan Köhler
use Data::Dumper;
7a7f33b5 Moritz Bunkus
use List::Util qw(max sum);
d319704a Moritz Bunkus
6e08e454 Thomas Heck
use SL::DB::Default;
use SL::DB::Customer;

d629acd8 Sven Schöling
require "bin/mozilla/io.pl";
b6dc5623 Sven Schöling
require "bin/mozilla/invoice_io.pl";
d629acd8 Sven Schöling
require "bin/mozilla/arap.pl";
bde667c2 Moritz Bunkus
require "bin/mozilla/drafts.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');

return $main::lxdebug->leave_sub() if (load_draft_maybe());
bde667c2 Moritz Bunkus
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
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>|;
#/show hhistory button
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
9a578c8b Jan Büren
$form->{webdav} = $::instance_conf->get_webdav;
d319704a Moritz Bunkus
$form->create_links("AR", \%myconfig, "customer");

if ($form->{all_customer}) {
unless ($form->{customer_id}) {
$form->{customer_id} = $form->{all_customer}->[0]->{id};
2ff471a7 Moritz Bunkus
$form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
d319704a Moritz Bunkus
}
}

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

2879330b Bernd Bleßmann
$form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded currency cp_id intnotes id shipto_id));
f9f5330a Stephan Köhler
54e4131e Moritz Bunkus
IS->get_customer(\%myconfig, \%$form);
1f2c9572 Philip Reetz
27562848 Moritz Bunkus
#quote all_customer Bug 133
5486b4a9 Sven Schöling
foreach my $ref (@{ $form->{all_customer} }) {
27562848 Moritz Bunkus
$ref->{name} = $form->quote($ref->{name});
}
d319704a Moritz Bunkus
f8529382 Sven Schöling
$form->restore_vars(qw(id));

IS->retrieve_invoice(\%myconfig, \%$form);
2879330b Bernd Bleßmann
$form->restore_vars(qw(payment_id language_id taxzone_id currency intnotes cp_id shipto_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
55e9890a David Ohlbrecht
fce441db Sven Schöling
# build vendor/customer drop down comatibility... don't ask
if (@{ $form->{"all_customer"} }) {
$form->{"selectcustomer"} = 1;
$form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
}

a818dbfa Sven Schöling
$form->{"oldcustomer"} = $form->{customer};

if ($form->{"oldcustomer"} !~ m/--\d+$/ && $form->{"customer_id"}) {
$form->{"oldcustomer"} .= qq|--$form->{"customer_id"}|
}


# $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
# $form->{selectcustomer} = 1;
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";
} 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
}
a0a30f41 Sven Schöling
# get pricegroups for parts
IS->get_pricegroups_for_parts(\%myconfig, \%$form);
ef92528c Geoffrey Richardson
# Problem: set_pricegroup resets the sellprice of old invoices to the price
# currently defined in the pricegroup, which is a problem if the price has
# changed, as the old invoice gets the new price
# set_pricegroup must never be called, when an old invoice is initially loaded

# set_pricegroup($_) for 1 .. $form->{rowcount};
d319704a Moritz Bunkus
}
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
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;

a05eead3 Sven Schöling
$form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
$form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
0cb4ad8c Philip Reetz
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
9e117fbd Philip Reetz
cfc6a60d Moritz Bunkus
my @old_project_ids = ($form->{"globalproject_id"});
a05eead3 Sven Schöling
map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};

9b67d27c Sven Schöling
$form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1e251313 Moritz Bunkus
"all" => 0,
"old_id" => \@old_project_ids },
"taxzones" => "ALL_TAXZONES",
"currencies" => "ALL_CURRENCIES",
"customers" => "ALL_CUSTOMERS",
e78a0281 Sven Schöling
"departments" => "all_departments",
1e251313 Moritz Bunkus
"price_factors" => "ALL_PRICE_FACTORS");
54e4131e Moritz Bunkus
6ad851ab Sven Schöling
$TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
$TMPL_VAR{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
9b67d27c Sven Schöling
$TMPL_VAR{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all(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
]);
cb2abccd Sven Schöling
$TMPL_VAR{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all(query => [
or => [
cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
and => [
cp_cv_id => undef,
cp_id => $::form->{cp_id} * 1
]
]
]);
4cc5904b Sven Schöling
$TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };

# customer
$TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
$TMPL_VAR{vclimit} = $myconfig{vclimit};
58d115ff Sven Schöling
$TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
push @custom_hiddens, "customer_id";
push @custom_hiddens, "oldcustomer";
push @custom_hiddens, "selectcustomer";
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
my $follow_up_vc = $form->{customer};
$follow_up_vc =~ s/--\d*\s*$//;
$TMPL_VAR{customer_name} = $follow_up_vc;
ddbe3ea5 Moritz Bunkus
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(
4cc5904b Sven Schöling
id action type media format queued printed emailed title vc discount
fce441db Sven Schöling
title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
4cc5904b Sven Schöling
max_dunning_level dunning_amount
shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
aad6637b Moritz Bunkus
shiptoemail shiptodepartment_1 shiptodepartment_2 shiptocp_gender message email subject cc bcc taxaccounts cursor_fokus
4cc5904b Sven Schöling
convert_from_do_ids convert_from_oe_ids
), @custom_hiddens,
map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
d319704a Moritz Bunkus
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
}

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} ) {
6e08e454 Thomas Heck
my $customer = SL::DB::Customer->new(id => $form->{customer_id})->load();
$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"};
}
}
}

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

de6cead5 Sven Schöling
# 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"}));
}

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};

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,
print_options => print_options(inline => 1),
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),
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 {
5486b4a9 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
&mark_as_paid_common(\%myconfig,"ar");
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
ce47a234 Thomas Kasulke
}

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;

5486b4a9 Sven Schöling
&check_name("customer");
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
ecc629cc Jan Büren
# Falls kein Kundenrabatt vorhanden ist, den aktuellen Rabatt nicht mit 0% überschreiben,
# da hier der Anwender schon manual einen Wert eingetragen haben könnte (analog zu qty) Bugfix: 1412
if ($form->{customer_discount}){
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
}
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) {

cb253140 Moritz Bunkus
select_item(mode => 'IS');
b2945bf6 Sven Schöling
::end_of_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 {
# if there is an exchange rate adjust sellprice
$form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
$form->{"sellprice_$i"} /= $exchangerate;
}

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

5486b4a9 Sven Schöling
my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
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
52983c08 Sven Schöling
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
d319704a Moritz Bunkus
07d71c33 Stephan Köhler
# get pricegroups for parts
ef73414c Stephan Köhler
IS->get_pricegroups_for_parts(\%myconfig, \%$form);
07d71c33 Stephan Köhler
# build up html code for prices_$i
&set_pricegroup($i);
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
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!'));
}
}
}
b2a7bb87 Jan Büren
# Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen
# Details s.a. Bug 1502
# Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann
# Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen
# keinen Unterschied macht.
# Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ...
# Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form
# erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs-
# belegs (neue Zahlung aber nichts anderes) zu speichern ...
24f530ce Moritz Bunkus
# Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren
b2a7bb87 Jan Büren
$form->error($locale->text('Cannot post payment for a closed period!'))
if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig));
54e4131e Moritz Bunkus
($form->{AR}) = split /--/, $form->{AR};
($form->{AR_paid}) = split /--/, $form->{AR_paid};
b3501bdf Moritz Bunkus
relink_accounts();
d5c1e4fa Moritz Bunkus
$form->redirect($locale->text('Payment posted!'))
54e4131e Moritz Bunkus
if (IS->post_payment(\%myconfig, \%$form));
$form->error($locale->text('Cannot post payment!'));


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');
8c7e4493 Moritz Bunkus
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
d319704a Moritz Bunkus
$form->isblank("invdate", $locale->text('Invoice Date missing!'));
$form->isblank("customer", $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
5486b4a9 Sven Schöling
if (&check_name('customer')) {
d319704a Moritz Bunkus
&update;
b2945bf6 Sven Schöling
::end_of_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!'));

$form->error($locale->text('Cannot post payment for a closed period!'))
9e06d0e4 Philip Reetz
if ($form->date_closed($form->{"datepaid_$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();
d946f59b Sven Schöling
$form->error($locale->text('Cannot post invoice!'))
unless IS->post_invoice(\%myconfig, \%$form);
remove_draft() if $form->{remove_draft};

if(!exists $form->{addition}) {
d3d239c9 Thomas Heck
$form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
382e874c Sven Schöling
$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';
$form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!');
$form->{callback} = build_std_url(qw(action edit id saved_message));
$form->redirect;
}
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
382e874c Sven Schöling
my $old_form = new Form;
$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
c25716f8 Moritz Bunkus
delete @{ $form }{qw(printed emailed queued invnumber invdate 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->{paidaccounts} = 1;
$form->{rowcount}--;
$form->{invdate} = $form->current_date(\%myconfig);
fa4c865c Moritz Bunkus
$form->{duedate} = $form->get_duedate(\%myconfig, $form->{invdate}) || $form->{invdate};
ef92528c Geoffrey Richardson
3cd3e832 Moritz Bunkus
# remember pricegroups for "use as new"
ef92528c Geoffrey Richardson
IS->get_pricegroups_for_parts(\%myconfig, \%$form);
set_pricegroup($_) for 1 .. $form->{rowcount};

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!"));
}

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
54e4131e Moritz Bunkus
$form->{storno_id} = $form->{id};
$form->{storno} = 1;
$form->{id} = "";
$form->{invnumber} = "Storno zu " . $form->{invnumber};
3740b503 Moritz Bunkus
$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;
5486b4a9 Sven Schöling
my $old_form = new Form;
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
}

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

my $form = $main::form;
my $locale = $main::locale;
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
07d71c33 Stephan Köhler
if ($form->{second_run}) {
$form->{print_and_post} = 0;
}
d319704a Moritz Bunkus
$form->header;

print qq|
da10bf75 Thomas Kasulke
<form method="post" action="$form->{script}">
d319704a Moritz Bunkus
|;

# delete action variable
map { delete $form->{$_} } qw(action header);

5486b4a9 Sven Schöling
foreach my $key (keys %$form) {
8c7e4493 Moritz Bunkus
next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
d319704a Moritz Bunkus
$form->{$key} =~ s/\"/&quot;/g;
da10bf75 Thomas Kasulke
print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
d319704a Moritz Bunkus
}

print qq|
da10bf75 Thomas Kasulke
<h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
d319704a Moritz Bunkus
<h4>|
. $locale->text('Are you sure you want to delete Invoice Number')
. qq| $form->{invnumber}
</h4>

<p>
da10bf75 Thomas Kasulke
<input name="action" class="submit" type="submit" value="|
d319704a Moritz Bunkus
. $locale->text('Yes') . qq|">
</form>
|;

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

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);

$form->{id} = '';
$form->{rowcount}--;
$form->{shipto} = 1;


$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
58eae092 Geoffrey Richardson
for my $i (1 .. $form->{rowcount}) {
for (qw(listprice)) {
$form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
}
}

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
}

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

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
8c7e4493 Moritz Bunkus
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
8cd05ad6 Moritz Bunkus
if (IS->delete_invoice(\%myconfig, \%$form)) {
2b89ec97 Thomas Kasulke
# saving the history
b65a230d Sven Schöling
if(!exists $form->{addition}) {
d3d239c9 Thomas Heck
$form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber};
b65a230d Sven Schöling
$form->{addition} = "DELETED";
a590a651 Sven Schöling
$form->save_history;
2b89ec97 Thomas Kasulke
}
fb4d2ffa Moritz Bunkus
# /saving the history
$form->redirect($locale->text('Invoice deleted!'));
2b89ec97 Thomas Kasulke
}
d319704a Moritz Bunkus
$form->error($locale->text('Cannot delete invoice!'));

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

my $form = $main::form;
d946f59b Sven Schöling
5486b4a9 Sven Schöling
$main::auth->assert('invoice_edit');
8c7e4493 Moritz Bunkus
ffea1346 Moritz Bunkus
if (!$form->{id}) {
382e874c Sven Schöling
$form->{no_redirect_after_post} = 1;
d946f59b Sven Schöling
ffea1346 Moritz Bunkus
my $saved_form = save_form();
d946f59b Sven Schöling
ffea1346 Moritz Bunkus
post();

8c7e4493 Moritz Bunkus
restore_form($saved_form, 0, qw(id invnumber));
ffea1346 Moritz Bunkus
}

edit_e_mail();
d946f59b Sven Schöling
5486b4a9 Sven Schöling
$main::lxdebug->leave_sub();
d946f59b Sven Schöling
}