Projekt

Allgemein

Profil

Herunterladen (26,3 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 received module
#
#======================================================================

7a7f33b5 Moritz Bunkus
use SL::FU;
d319704a Moritz Bunkus
use SL::IR;
a1a3bfd8 Moritz Bunkus
use SL::IS;
d319704a Moritz Bunkus
use SL::PE;
7a7f33b5 Moritz Bunkus
use List::Util qw(max sum);
d319704a Moritz Bunkus
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";
require "bin/mozilla/common.pl";
bde667c2 Moritz Bunkus
require "bin/mozilla/drafts.pl";
d319704a Moritz Bunkus
1668bc2d Sven Schöling
use strict;

d319704a Moritz Bunkus
1;

# end of main

sub add {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my $locale = $main::locale;
58b14f96 Stephan Köhler
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
1668bc2d Sven Schöling
return $main::lxdebug->leave_sub() if (load_draft_maybe());
bde667c2 Moritz Bunkus
4b937d1a Sven Donath
$form->{title} = $locale->text('Record Vendor Invoice');
d319704a Moritz Bunkus
&invoice_links;
&prepare_invoice;
&display_form;

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

sub edit {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();
6ebad56e Moritz Bunkus
1668bc2d Sven Schöling
my $form = $main::form;
my $locale = $main::locale;

$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
ec93aab7 Thomas Kasulke
# show history button
$form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
#/show hhistory button
f83ca506 Sven Schöling
d319704a Moritz Bunkus
$form->{title} = $locale->text('Edit Vendor Invoice');

&invoice_links;
&prepare_invoice;
&display_form;

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

sub invoice_links {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
58b14f96 Stephan Köhler
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
67b0f6a6 Sven Schöling
$form->{vc} = 'vendor';

d319704a Moritz Bunkus
# create links
606e7e25 Moritz Bunkus
$form->{webdav} = $::lx_office_conf{features}->{webdav};
d7f06042 Moritz Bunkus
$form->{jsscript} = 1;
d319704a Moritz Bunkus
$form->create_links("AP", \%myconfig, "vendor");

27562848 Moritz Bunkus
#quote all_vendor Bug 133
1668bc2d Sven Schöling
foreach my $ref (@{ $form->{all_vendor} }) {
27562848 Moritz Bunkus
$ref->{name} = $form->quote($ref->{name});
}

d319704a Moritz Bunkus
if ($form->{all_vendor}) {
unless ($form->{vendor_id}) {
$form->{vendor_id} = $form->{all_vendor}->[0]->{id};
}
}
1668bc2d Sven Schöling
2879330b Bernd Bleßmann
my ($payment_id, $language_id, $taxzone_id, $currency);
54e4131e Moritz Bunkus
if ($form->{payment_id}) {
$payment_id = $form->{payment_id};
}
if ($form->{language_id}) {
$language_id = $form->{language_id};
}
if ($form->{taxzone_id}) {
$taxzone_id = $form->{taxzone_id};
}
2879330b Bernd Bleßmann
if ($form->{currency}) {
$currency = $form->{currency};
}
d319704a Moritz Bunkus
1668bc2d Sven Schöling
my $cp_id = $form->{cp_id};
d319704a Moritz Bunkus
IR->get_vendor(\%myconfig, \%$form);
IR->retrieve_invoice(\%myconfig, \%$form);
$form->{cp_id} = $cp_id;
f83ca506 Sven Schöling
54e4131e Moritz Bunkus
if ($payment_id) {
$form->{payment_id} = $payment_id;
}
if ($language_id) {
$form->{language_id} = $language_id;
}
if ($taxzone_id) {
$form->{taxzone_id} = $taxzone_id;
}
2879330b Bernd Bleßmann
if ($currency) {
$form->{currency} = $currency;
}
d319704a Moritz Bunkus
1668bc2d Sven Schöling
my @curr = split(/:/, $form->{currencies}); #seems to be missing
d319704a Moritz Bunkus
map { $form->{selectcurrency} .= "<option>$_\n" } @curr;

$form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";

fdfd7f20 Sven Schöling
# build vendor/customer drop down comatibility... don't ask
if (@{ $form->{"all_vendor"} || [] }) {
$form->{"selectvendor"} = 1;
$form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
}

d319704a Moritz Bunkus
# departments
if ($form->{all_departments}) {
$form->{selectdepartment} = "<option>\n";
$form->{department} = "$form->{department}--$form->{department_id}";

map {
$form->{selectdepartment} .=
"<option>$_->{description}--$_->{id}\n"
e2b9e173 Sven Schöling
} (@{ $form->{all_departments} || [] });
d319704a Moritz Bunkus
}

# forex
$form->{forex} = $form->{exchangerate};
1668bc2d Sven Schöling
my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
d319704a Moritz Bunkus
1668bc2d Sven Schöling
foreach my $key (keys %{ $form->{AP_links} }) {
d319704a Moritz Bunkus
1668bc2d Sven Schöling
foreach my $ref (@{ $form->{AP_links}{$key} }) {
0e0ff150 Jan Büren
$form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
d319704a Moritz Bunkus
}

56e9d812 Sven Schöling
next unless $form->{acc_trans}{$key};

d319704a Moritz Bunkus
if ($key eq "AP_paid") {
1668bc2d Sven Schöling
for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
d319704a Moritz Bunkus
$form->{"AP_paid_$i"} =
"$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";

b56c0cd7 Bernd Bleßmann
$form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
d319704a Moritz Bunkus
# reverse paid
$form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
$form->{"datepaid_$i"} =
$form->{acc_trans}{$key}->[$i - 1]->{transdate};
b56c0cd7 Bernd Bleßmann
$form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
d319704a Moritz Bunkus
$form->{"forex_$i"} = $form->{"exchangerate_$i"} =
$form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
$form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
$form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};

$form->{paidaccounts} = $i;
}
} else {
$form->{$key} =
"$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
}

}

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

$form->{AP} = $form->{AP_1} unless $form->{id};

$form->{locked} =
($form->datetonum($form->{invdate}, \%myconfig) <=
$form->datetonum($form->{closedto}, \%myconfig));

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

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

my $form = $main::form;
my %myconfig = %main::myconfig;
58b14f96 Stephan Köhler
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
d319704a Moritz Bunkus
if ($form->{id}) {

map { $form->{$_} =~ s/\"/&quot;/g } qw(invnumber ordnumber quonumber);

ef255c4e Moritz Bunkus
my $i = 0;
1668bc2d Sven Schöling
foreach my $ref (@{ $form->{invoice_details} }) {
d319704a Moritz Bunkus
$i++;
map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
cbe2c598 Jan Büren
# übernommen aus is.pl Fix für Bug 1642. Nebenwirkungen? jb 12.5.2011
# getestet: Lieferantenauftrag -> Rechnung i.O.
# Lieferantenauftrag -> Lieferschein -> Rechnung i.O.
cb253140 Moritz Bunkus
# Werte: 20% (Lieferantenrabatt), 12,4% individuell und 0,4 individuell s.a.
cbe2c598 Jan Büren
# Screenshot zu Bug 1642
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
d319704a Moritz Bunkus
1668bc2d Sven Schöling
my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
d319704a Moritz Bunkus
$dec = length $dec;
1668bc2d Sven Schöling
my $decimalplaces = ($dec > 2) ? $dec : 2;
d319704a Moritz Bunkus
$form->{"sellprice_$i"} =
$form->format_amount(\%myconfig, $form->{"sellprice_$i"},
$decimalplaces);
58b14f96 Stephan Köhler
7933b6bd Udo Spallek
(my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
58b14f96 Stephan Köhler
$dec_qty = length $dec_qty;

d319704a Moritz Bunkus
$form->{"qty_$i"} =
7933b6bd Udo Spallek
$form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
d319704a Moritz Bunkus
$form->{rowcount} = $i;
}
}

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

sub form_header {
1668bc2d 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};
58b14f96 Stephan Köhler
4d59fd5d Moritz Bunkus
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
e6ffa928 Moritz Bunkus
my %TMPL_VAR = ();
67b0f6a6 Sven Schöling
my @custom_hiddens;
bbc63419 Moritz Bunkus
67b0f6a6 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};
d319704a Moritz Bunkus
a326f385 Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
67b0f6a6 Sven Schöling
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"};
cfc6a60d Moritz Bunkus
9b67d27c Sven Schöling
$form->get_lists("projects" => { "key" => "ALL_PROJECTS",
67b0f6a6 Sven Schöling
"all" => 0,
"old_id" => \@old_project_ids },
1e251313 Moritz Bunkus
"taxzones" => "ALL_TAXZONES",
"currencies" => "ALL_CURRENCIES",
"vendors" => "ALL_VENDORS",
e78a0281 Sven Schöling
"departments" => "all_departments",
1e251313 Moritz Bunkus
"price_factors" => "ALL_PRICE_FACTORS");
15682dc4 Moritz Bunkus
43e56b93 Sven Schöling
$TMPL_VAR{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
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
]
]
]);
67b0f6a6 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};
$TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
push @custom_hiddens, "vendor_id";
push @custom_hiddens, "oldvendor";
push @custom_hiddens, "selectvendor";

# currencies and exchangerate
my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
$form->{currency} = $form->{defaultcurrency} unless $form->{currency};
$TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
$TMPL_VAR{currencies} = NTI($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;
67b0f6a6 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} =~ /-/;

$form->{fokus} = "invoice.vendor";
848ff82d Moritz Bunkus
67b0f6a6 Sven Schöling
my $follow_up_vc = $form->{vendor};
$follow_up_vc =~ s/--\d*\s*$//;
$TMPL_VAR{vendor_name} = $follow_up_vc;
54e4131e Moritz Bunkus
67b0f6a6 Sven Schöling
# set option selected
foreach my $item (qw(AP)) {
$form->{"select$item"} =~ s/ selected//;
$form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
ddbe3ea5 Moritz Bunkus
}
54e4131e Moritz Bunkus
67b0f6a6 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};

# hiddens
$TMPL_VAR{HIDDENS} = [qw(
id action type media format queued printed emailed title vc discount
title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
max_dunning_level dunning_amount
shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
convert_from_do_ids convert_from_oe_ids
), @custom_hiddens,
map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
d319704a Moritz Bunkus
d7f06042 Moritz Bunkus
$form->{jsscript} = 1;
67b0f6a6 Sven Schöling
$form->header();
d319704a Moritz Bunkus
67b0f6a6 Sven Schöling
print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
d319704a Moritz Bunkus
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub form_footer {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
4d59fd5d Moritz Bunkus
$main::auth->assert('vendor_invoice_edit');
d319704a Moritz Bunkus
e50a00d2 Sven Schöling
$form->{invtotal} = $form->{invsubtotal};
$form->{oldinvtotal} = $form->{invtotal};
d319704a Moritz Bunkus
e50a00d2 Sven Schöling
# note rows
$form->{rows} = max 2,
$form->numtextrows($form->{notes}, 26, 8),
$form->numtextrows($form->{intnotes}, 35, 8);
d319704a Moritz Bunkus

e50a00d2 Sven Schöling
# tax, total and subtotal calculations
my ($tax, $subtotal);
$form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];

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 {
$form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
$form->{invtotal} += $form->{"${item}_total"};
d319704a Moritz Bunkus
}
}
}

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

e50a00d2 Sven Schöling
# payments
4d20c181 Moritz Bunkus
my $totalpaid = 0;
d319704a Moritz Bunkus
$form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
e50a00d2 Sven Schöling
$form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
d319704a Moritz Bunkus
0e0ff150 Jan Büren
# Standard Konto für Umlaufvermögen
my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);

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

d319704a Moritz Bunkus
$form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
0e0ff150 Jan Büren
if (!$form->{"AP_paid_$i"}) {
$form->{"selectAP_paid_$i"} =~ s/option>$accno_arap--(.*?)>/option selected>$accno_arap--$1>/;
} else {
$form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
}

4d20c181 Moritz Bunkus
$totalpaid += $form->{"paid_$i"};
9e117fbd Philip Reetz
}
d77b981f Moritz Bunkus
e50a00d2 Sven Schöling
print $form->parse_html_template('ir/form_footer', {
is_type_credit_note => ($form->{type} eq "credit_note"),
totalpaid => $totalpaid,
paid_missing => $form->{invtotal} - $totalpaid,
show_storno => $form->{id} && !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && !$totalpaid,
show_delete => ($form->current_date(\%myconfig) eq $form->{gldate}),
});
##print $form->parse_html_template('ir/_payments'); # parser
##print $form->parse_html_template('webdav/_list'); # parser
d319704a Moritz Bunkus
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

ce47a234 Thomas Kasulke
sub mark_as_paid {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
8c7e4493 Moritz Bunkus
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
f83ca506 Sven Schöling
&mark_as_paid_common(\%myconfig,"ap");
8c7e4493 Moritz Bunkus
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
ce47a234 Thomas Kasulke
}

d319704a Moritz Bunkus
sub update {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
d319704a Moritz Bunkus
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
1668bc2d Sven Schöling
&check_name('vendor');
d319704a Moritz Bunkus
2e26ccd5 Bernd Bleßmann
if (!$form->{forex}) { # read exchangerate from input field (not hidden)
$form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
}
a53233e5 Sven Schöling
$form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
$form->{exchangerate} = $form->{forex} if $form->{forex};
d319704a Moritz Bunkus
1668bc2d 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);
a53233e5 Sven Schöling
$form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
$form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
d319704a Moritz Bunkus
}

1668bc2d Sven Schöling
my $i = $form->{rowcount};
my $exchangerate = ($form->{exchangerate} * 1) || 1;
d319704a Moritz Bunkus
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
IR->retrieve_item(\%myconfig, \%$form);

my $rows = scalar @{ $form->{item_list} };

if ($rows) {
400e3ab0 Moritz Bunkus
$form->{"qty_$i"} = 1 unless $form->parse_amount(\%myconfig, $form->{"qty_$i"});
d319704a Moritz Bunkus
if ($rows > 1) {

cb253140 Moritz Bunkus
select_item(mode => 'IR');
b2945bf6 Sven Schöling
::end_of_request();
d319704a Moritz Bunkus
} else {

# override sellprice if there is one entered
1668bc2d Sven Schöling
my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
d319704a Moritz Bunkus
b65a230d Sven Schöling
# ergaenzung fuer bug 736 Lieferanten-Rabatt auch in Einkaufsrechnungen vorbelegen jb
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{vendor_discount} * 100 );
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] };
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+)/;
1668bc2d Sven Schöling
my $dec_qty = length $1;
my $decimalplaces = max 2, $dec_qty;
d319704a Moritz Bunkus
if ($sellprice) {
$form->{"sellprice_$i"} = $sellprice;
} else {
# if there is an exchange rate adjust sellprice
$form->{"sellprice_$i"} /= $exchangerate;
}

1668bc2d Sven Schöling
my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
d319704a Moritz Bunkus
$form->{creditremaining} -= $amount;
52983c08 Sven Schöling
$form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
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
}
}
}
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

54e4131e Moritz Bunkus
sub storno {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
85f5b26d Thomas Kasulke
1668bc2d Sven Schöling
$main::auth->assert('vendor_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, "ap")) {
5ceaf343 Moritz Bunkus
$form->error($locale->text("Invoice has already been storno'd!"));
}

408a65ca Moritz Bunkus
my $employee_id = $form->{employee_id};
379aa61a Moritz Bunkus
invoice_links();
prepare_invoice();
relink_accounts();
3740b503 Moritz Bunkus
# Payments must not be recorded for the new storno invoice.
$form->{paidaccounts} = 0;
b56c0cd7 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 };
f83ca506 Sven Schöling
18db0f54 Thomas Kasulke
# saving the history
if(!exists $form->{addition} && $form->{id} ne "") {
f83ca506 Sven Schöling
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
18db0f54 Thomas Kasulke
$form->{addition} = "CANCELED";
a590a651 Sven Schöling
$form->save_history;
18db0f54 Thomas Kasulke
}
# /saving the history
f83ca506 Sven Schöling
54e4131e Moritz Bunkus
$form->{storno_id} = $form->{id};
$form->{storno} = 1;
$form->{id} = "";
$form->{invnumber} = "Storno zu " . $form->{invnumber};
18db0f54 Thomas Kasulke
$form->{rowcount}++;
408a65ca Moritz Bunkus
$form->{employee_id} = $employee_id;
post();
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}

98764afa Philip Reetz
sub use_as_template {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();
98764afa Philip Reetz
1668bc2d Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;

$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
b56c0cd7 Bernd Bleßmann
map { 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);
98764afa Philip Reetz
$form->{paidaccounts} = 1;
$form->{rowcount}--;
$form->{invdate} = $form->current_date(\%myconfig);
&display_form;

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

54e4131e Moritz Bunkus
sub post_payment {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
a326f385 Moritz Bunkus
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
a326f385 Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1668bc2d Sven Schöling
for my $i (1 .. $form->{paidaccounts}) {
bd32b607 Philip Reetz
if ($form->{"paid_$i"}) {
1668bc2d Sven Schöling
my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
54e4131e 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));
54e4131e Moritz Bunkus
if ($form->{currency} ne $form->{defaultcurrency}) {
1668bc2d Sven Schöling
# $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
$form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
54e4131e Moritz Bunkus
}
}
}

($form->{AP}) = split /--/, $form->{AP};
($form->{AP_paid}) = split /--/, $form->{AP_paid};
ec93aab7 Thomas Kasulke
if (IR->post_payment(\%myconfig, \%$form)){
b65a230d Sven Schöling
if (!exists $form->{addition} && $form->{id} ne "") {
# saving the history
d5c1e4fa Moritz Bunkus
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
b65a230d Sven Schöling
$form->{addition} = "PAYMENT POSTED";
cf927461 Thomas Kasulke
$form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
a590a651 Sven Schöling
$form->save_history;
b65a230d Sven Schöling
# /saving the history
}
d5c1e4fa Moritz Bunkus
$form->redirect($locale->text('Payment posted!'));
ec93aab7 Thomas Kasulke
}
54e4131e Moritz Bunkus
d5c1e4fa Moritz Bunkus
$form->error($locale->text('Cannot post payment!'));
54e4131e Moritz Bunkus
1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}

d03b2877 Moritz Bunkus
sub _max_datepaid {
my $form = $main::form;

my @dates = sort { $b->[1] cmp $a->[1] }
map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
grep { $_ }
map { $form->{"datepaid_${_}"} }
(1..$form->{rowcount});

return @dates ? $dates[0]->[0] : undef;
}


d319704a Moritz Bunkus
sub post {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
fa640585 Philip Reetz
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);

168c0a5f Sven Schöling
$form->isblank("invdate", $locale->text('Invoice Date missing!'));
$form->isblank("vendor", $locale->text('Vendor missing!'));
$form->isblank("invnumber", $locale->text('Invnumber missing!'));
d319704a Moritz Bunkus
a426a839 Moritz Bunkus
$form->{invnumber} =~ s/^\s*//g;
$form->{invnumber} =~ s/\s*$//g;

d319704a Moritz Bunkus
# if the vendor changed get new values
1668bc2d Sven Schöling
if (&check_name('vendor')) {
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;
}

0a5317e7 Moritz Bunkus
remove_emptied_rows();
d319704a Moritz Bunkus
&validate_items;

d03b2877 Moritz Bunkus
my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
my $max_datepaid = _max_datepaid();
d319704a Moritz Bunkus
d03b2877 Moritz Bunkus
$form->error($locale->text('Cannot post invoice for a closed period!')) if $max_datepaid && $form->date_closed($max_datepaid, \%myconfig);
d319704a Moritz Bunkus
$form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
if ($form->{currency} ne $form->{defaultcurrency});

d03b2877 Moritz Bunkus
my $i;
d319704a Moritz Bunkus
for $i (1 .. $form->{paidaccounts}) {
5563e116 Moritz Bunkus
if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1668bc2d 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!'));
}
}
}

($form->{AP}) = split /--/, $form->{AP};
($form->{AP_paid}) = split /--/, $form->{AP_paid};
dd88b408 Sven Schöling
$form->{storno} ||= 0;
d319704a Moritz Bunkus
$form->{id} = 0 if $form->{postasnew};

5f760f30 Moritz Bunkus
b3501bdf Moritz Bunkus
relink_accounts();
bde667c2 Moritz Bunkus
if (IR->post_invoice(\%myconfig, \%$form)){
b65a230d Sven Schöling
# saving the history
if(!exists $form->{addition} && $form->{id} ne "") {
f83ca506 Sven Schöling
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
a0f6a00c Thomas Kasulke
$form->{addition} = "POSTED";
b65a230d Sven Schöling
#$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
a590a651 Sven Schöling
$form->save_history;
b65a230d Sven Schöling
}
# /saving the history
db68b6fd Sven Schöling
remove_draft() if $form->{remove_draft};
b65a230d Sven Schöling
$form->redirect( $locale->text('Invoice')
d319704a Moritz Bunkus
. " $form->{invnumber} "
ec93aab7 Thomas Kasulke
. $locale->text('posted!'));
}
d319704a Moritz Bunkus
$form->error($locale->text('Cannot post invoice!'));

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

sub delete {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();
d319704a Moritz Bunkus
1668bc2d Sven Schöling
my $form = $main::form;
my $locale = $main::locale;

$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
d319704a Moritz Bunkus
$form->header;
print qq|
<body>

<form method=post action=$form->{script}>
|;

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

1668bc2d 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;
print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
}

print qq|
<h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>

<h4>|
. $locale->text('Are you sure you want to delete Invoice Number')
. qq| $form->{invnumber}</h4>
<p>
<input name=action class=submit type=submit value="|
. $locale->text('Yes') . qq|">
</form>
|;

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

sub yes {
1668bc2d Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
8c7e4493 Moritz Bunkus
1668bc2d Sven Schöling
$main::auth->assert('vendor_invoice_edit');
8c7e4493 Moritz Bunkus
ec93aab7 Thomas Kasulke
if (IR->delete_invoice(\%myconfig, \%$form)) {
# saving the history
if(!exists $form->{addition}) {
f83ca506 Sven Schöling
$form->{snumbers} = qq|invnumber_| . $form->{invnumber};
b65a230d Sven Schöling
$form->{addition} = "DELETED";
a590a651 Sven Schöling
$form->save_history;
ec93aab7 Thomas Kasulke
}
f83ca506 Sven Schöling
# /saving the history
ec93aab7 Thomas Kasulke
$form->redirect($locale->text('Invoice deleted!'));
}
d319704a Moritz Bunkus
$form->error($locale->text('Cannot delete invoice!'));

1668bc2d Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}
bbc63419 Moritz Bunkus
4e5c422e Sven Schöling
sub get_duedate_vendor {
$::lxdebug->enter_sub;
bbc63419 Moritz Bunkus
4e5c422e Sven Schöling
my $result = IR->get_duedate(
vendor_id => $::form->{vendor_id},
invdate => $::form->{invdate},
default => $::form->{old_duedate},
);
bbc63419 Moritz Bunkus
4e5c422e Sven Schöling
print $::form->ajax_response_header, $result;
$::lxdebug->leave_sub;
bbc63419 Moritz Bunkus
}