|
#=====================================================================
|
|
# 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) 2001
|
|
#
|
|
# Author: Dieter Simader
|
|
# Email: dsimader@sql-ledger.org
|
|
# Web: http://www.sql-ledger.org
|
|
#
|
|
# Contributors:
|
|
#
|
|
# 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., 51 Franklin Street, Fifth Floor, Boston,
|
|
# MA 02110-1335, USA.
|
|
#======================================================================
|
|
#
|
|
# Inventory received module
|
|
#
|
|
#======================================================================
|
|
|
|
package IR;
|
|
|
|
use SL::AM;
|
|
use SL::ARAP;
|
|
use SL::Common;
|
|
use SL::CVar;
|
|
use SL::DATEV qw(:CONSTANTS);
|
|
use SL::DBUtils;
|
|
use SL::DB::Draft;
|
|
use SL::DO;
|
|
use SL::GenericTranslations;
|
|
use SL::HTML::Restrict;
|
|
use SL::IO;
|
|
use SL::Locale::String qw(t8);
|
|
use SL::MoreCommon;
|
|
use SL::DB::Default;
|
|
use SL::DB::TaxZone;
|
|
use SL::DB::MakeModel;
|
|
use SL::DB::ValidityToken;
|
|
use SL::DB;
|
|
use SL::Presenter::Part qw(type_abbreviation classification_abbreviation);
|
|
use List::Util qw(min);
|
|
|
|
use strict;
|
|
use constant PCLASS_OK => 0;
|
|
use constant PCLASS_NOTFORSALE => 1;
|
|
use constant PCLASS_NOTFORPURCHASE => 2;
|
|
|
|
sub post_invoice {
|
|
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, %params);
|
|
|
|
$::lxdebug->leave_sub;
|
|
return $rc;
|
|
}
|
|
|
|
sub _post_invoice {
|
|
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;
|
|
|
|
my $validity_token;
|
|
if (!$form->{id}) {
|
|
$validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token(
|
|
scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST(),
|
|
token => $form->{form_validity_token},
|
|
);
|
|
|
|
die $::locale->text('The form is not valid anymore.') if !$validity_token;
|
|
}
|
|
|
|
my $payments_only = $params{payments_only};
|
|
my $dbh = $provided_dbh || SL::DB->client->dbh;
|
|
my $restricter = SL::HTML::Restrict->create;
|
|
|
|
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
|
|
my $defaultcurrency = $form->{defaultcurrency};
|
|
|
|
my $ic_cvar_configs = CVar->get_configs(module => 'IC',
|
|
dbh => $dbh);
|
|
|
|
my ($query, $sth, @values, $project_id);
|
|
my ($allocated, $taxrate, $taxamount, $taxdiff, $item);
|
|
my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
|
|
my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
|
|
my $exchangerate = 0;
|
|
my ($basefactor, $baseqty, @taxaccounts, $totaltax);
|
|
|
|
my $all_units = AM->retrieve_units($myconfig, $form);
|
|
|
|
#markierung
|
|
if (!$payments_only) {
|
|
if ($form->{id}) {
|
|
&reverse_invoice($dbh, $form);
|
|
} else {
|
|
($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
|
|
do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber, currency_id, taxzone_id) VALUES (?, '', (SELECT id FROM currencies WHERE name=?), ?)|, $form->{id}, $form->{currency}, $form->{taxzone_id});
|
|
}
|
|
}
|
|
if ($form->{currency} eq $defaultcurrency) {
|
|
$form->{exchangerate} = 1;
|
|
} else {
|
|
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{invdate}, 'sell');
|
|
$form->{exchangerate} = $form->parse_amount($myconfig, $form->{exchangerate}, 5);
|
|
|
|
# if default exchangerate is not defined, define one
|
|
unless ($exchangerate) {
|
|
$form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate});
|
|
# delete records exchangerate -> if user sets new invdate for record
|
|
$query = qq|UPDATE ap set exchangerate = NULL where id = ?|;
|
|
do_query($form, $dbh, $query, $form->{"id"});
|
|
}
|
|
# update record exchangerate, if the default is set and differs from current
|
|
if ($exchangerate && ($form->{exchangerate} != $exchangerate)) {
|
|
$form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
|
|
0, $form->{exchangerate}, $form->{id}, 'ap');
|
|
}
|
|
}
|
|
my %item_units;
|
|
my $q_item_unit = qq|SELECT unit FROM parts WHERE id = ?|;
|
|
my $h_item_unit = prepare_query($form, $dbh, $q_item_unit);
|
|
|
|
$form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
|
|
my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
|
|
my $price_factor;
|
|
|
|
my @processed_invoice_ids;
|
|
for my $i (1 .. $form->{rowcount}) {
|
|
next unless $form->{"id_$i"};
|
|
|
|
my $position = $i;
|
|
|
|
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
|
|
$form->{"qty_$i"} *= -1 if $form->{storno};
|
|
|
|
if ( $::instance_conf->get_inventory_system eq 'periodic') {
|
|
# inventory account number is overwritten with expense account number, so
|
|
# never book incoming to inventory account but always to expense account
|
|
$form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"}
|
|
};
|
|
|
|
# get item baseunit
|
|
if (!$item_units{$form->{"id_$i"}}) {
|
|
do_statement($form, $h_item_unit, $q_item_unit, $form->{"id_$i"});
|
|
($item_units{$form->{"id_$i"}}) = $h_item_unit->fetchrow_array();
|
|
}
|
|
|
|
my $item_unit = $item_units{$form->{"id_$i"}};
|
|
|
|
if (defined($all_units->{$item_unit}->{factor})
|
|
&& ($all_units->{$item_unit}->{factor} ne '')
|
|
&& ($all_units->{$item_unit}->{factor} * 1 != 0)) {
|
|
$basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
|
|
} else {
|
|
$basefactor = 1;
|
|
}
|
|
$baseqty = $form->{"qty_$i"} * $basefactor;
|
|
|
|
@taxaccounts = split / /, $form->{"taxaccounts_$i"};
|
|
$taxdiff = 0;
|
|
$allocated = 0;
|
|
$taxrate = 0;
|
|
|
|
$form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
|
|
(my $fxsellprice = $form->{"sellprice_$i"}) =~ /\.(\d+)/;
|
|
my $dec = length $1;
|
|
my $decimalplaces = ($dec > 2) ? $dec : 2;
|
|
|
|
map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
|
|
|
|
$price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
|
|
# copied from IS.pm, with some changes (no decimalplaces corrections here etc)
|
|
# TODO maybe use PriceTaxCalculation or something like this for backends (IR.pm / IS.pm)
|
|
|
|
# undo discount formatting
|
|
$form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
|
|
# deduct discount
|
|
$form->{"sellprice_$i"} = $fxsellprice * (1 - $form->{"discount_$i"});
|
|
|
|
######################################################################
|
|
if ($form->{"inventory_accno_$i"}) {
|
|
|
|
$linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
|
|
|
|
if ($form->{taxincluded}) {
|
|
|
|
$taxamount = $linetotal * ($taxrate / (1 + $taxrate));
|
|
$form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
|
|
|
|
} else {
|
|
$taxamount = $linetotal * $taxrate;
|
|
}
|
|
|
|
$netamount += $linetotal;
|
|
|
|
if ($form->round_amount($taxrate, 7) == 0) {
|
|
if ($form->{taxincluded}) {
|
|
foreach $item (@taxaccounts) {
|
|
$taxamount =
|
|
$form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
|
|
$taxdiff += $taxamount;
|
|
$form->{amount}{ $form->{id} }{$item} -= $taxamount;
|
|
}
|
|
$form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
|
|
|
|
} else {
|
|
map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
|
|
}
|
|
|
|
} else {
|
|
map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
|
|
}
|
|
|
|
# add purchase to inventory, this one is without the tax!
|
|
$amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
|
|
$linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2) * $form->{exchangerate};
|
|
$linetotal = $form->round_amount($linetotal, 2);
|
|
|
|
# this is the difference for the inventory
|
|
$invoicediff += ($amount - $linetotal);
|
|
|
|
$form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -= $linetotal;
|
|
|
|
# adjust and round sellprice
|
|
$form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
|
|
|
|
$lastinventoryaccno = $form->{"inventory_accno_$i"};
|
|
|
|
next if $payments_only;
|
|
|
|
# after_save hook changes lastcost for all assemblies and assortments recursively
|
|
my $a = SL::DB::Part->load_cached(conv_i($form->{"id_$i"}));
|
|
$a->update_attributes(lastcost => abs($fxsellprice * $form->{exchangerate} / $basefactor));
|
|
$a->set_lastcost_assemblies_and_assortiments;
|
|
|
|
# check if we sold the item already and
|
|
# make an entry for the expense and inventory
|
|
my $taxzone = $form->{taxzone_id} * 1;
|
|
$query =
|
|
qq|SELECT i.id, i.qty, i.allocated, i.trans_id, i.base_qty,
|
|
bg.inventory_accno_id, tc.expense_accno_id AS expense_accno_id, a.transdate
|
|
FROM invoice i, ar a, parts p, buchungsgruppen bg, taxzone_charts tc
|
|
WHERE (i.parts_id = p.id)
|
|
AND (i.parts_id = ?)
|
|
AND ((i.base_qty + i.allocated) > 0)
|
|
AND (i.trans_id = a.id)
|
|
AND (p.buchungsgruppen_id = bg.id)
|
|
AND (tc.buchungsgruppen_id = p.buchungsgruppen_id)
|
|
AND (tc.taxzone_id = ${taxzone})
|
|
ORDER BY transdate|;
|
|
# ORDER BY transdate guarantees FIFO
|
|
|
|
# sold two items without having bought them yet, example result of query:
|
|
# id | qty | allocated | trans_id | inventory_accno_id | expense_accno_id | transdate
|
|
# ---+-----+-----------+----------+--------------------+------------------+------------
|
|
# 9 | 2 | 0 | 9 | 15 | 151 | 2011-01-05
|
|
|
|
# base_qty + allocated > 0 if article has already been sold but not bought yet
|
|
|
|
# select qty,allocated,base_qty,sellprice from invoice where trans_id = 9;
|
|
# qty | allocated | base_qty | sellprice
|
|
# -----+-----------+----------+------------
|
|
# 2 | 0 | 2 | 1000.00000
|
|
|
|
$sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
|
|
|
|
my $totalqty = $baseqty;
|
|
|
|
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
|
|
my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated});
|
|
$linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
|
|
|
|
if ( $::instance_conf->get_inventory_system eq 'perpetual' ) {
|
|
# Warenbestandsbuchungen nur bei Bestandsmethode
|
|
|
|
if ($ref->{allocated} < 0) {
|
|
|
|
# we have an entry for it already, adjust amount
|
|
$form->update_balance($dbh, "acc_trans", "amount",
|
|
qq| (trans_id = $ref->{trans_id})
|
|
AND (chart_id = $ref->{inventory_accno_id})
|
|
AND (transdate = '$ref->{transdate}')|,
|
|
$linetotal);
|
|
|
|
$form->update_balance($dbh, "acc_trans", "amount",
|
|
qq| (trans_id = $ref->{trans_id})
|
|
AND (chart_id = $ref->{expense_accno_id})
|
|
AND (transdate = '$ref->{transdate}')|,
|
|
$linetotal * -1);
|
|
|
|
} elsif ($linetotal != 0) {
|
|
|
|
# allocated >= 0
|
|
# add entry for inventory, this one is for the sold item
|
|
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) VALUES (?, ?, ?, ?,
|
|
(SELECT taxkey_id
|
|
FROM taxkeys
|
|
WHERE chart_id= ?
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT tax_id
|
|
FROM taxkeys
|
|
WHERE chart_id= ?
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT link FROM chart WHERE id = ?))|;
|
|
@values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id}, $ref->{transdate}, $ref->{inventory_accno_id}, $ref->{transdate},
|
|
$ref->{inventory_accno_id});
|
|
do_query($form, $dbh, $query, @values);
|
|
|
|
# add expense
|
|
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) VALUES (?, ?, ?, ?,
|
|
(SELECT taxkey_id
|
|
FROM taxkeys
|
|
WHERE chart_id= ?
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT tax_id
|
|
FROM taxkeys
|
|
WHERE chart_id= ?
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT link FROM chart WHERE id = ?))|;
|
|
@values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id}, $ref->{transdate}, $ref->{expense_accno_id}, $ref->{transdate},
|
|
$ref->{expense_accno_id});
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
};
|
|
|
|
# update allocated for sold item
|
|
$form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1);
|
|
|
|
$allocated += $qty;
|
|
|
|
last if ($totalqty -= $qty) <= 0;
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
} else { # if ($form->{"inventory_accno_id_$i"})
|
|
# part doesn't have an inventory_accno_id
|
|
# lastcost of the part is updated at the end
|
|
|
|
$linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
|
|
|
|
if ($form->{taxincluded}) {
|
|
$taxamount = $linetotal * ($taxrate / (1 + $taxrate));
|
|
$form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
|
|
|
|
} else {
|
|
$taxamount = $linetotal * $taxrate;
|
|
}
|
|
|
|
$netamount += $linetotal;
|
|
|
|
if ($form->round_amount($taxrate, 7) == 0) {
|
|
if ($form->{taxincluded}) {
|
|
foreach $item (@taxaccounts) {
|
|
$taxamount = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"}));
|
|
$totaltax += $taxamount;
|
|
$form->{amount}{ $form->{id} }{$item} -= $taxamount;
|
|
}
|
|
} else {
|
|
map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
|
|
}
|
|
} else {
|
|
map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
|
|
}
|
|
|
|
$amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
|
|
$linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2) * $form->{exchangerate};
|
|
$linetotal = $form->round_amount($linetotal, 2);
|
|
|
|
# this is the difference for expense
|
|
$expensediff += ($amount - $linetotal);
|
|
|
|
# add amount to expense
|
|
$form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -= $linetotal;
|
|
|
|
$lastexpenseaccno = $form->{"expense_accno_$i"};
|
|
|
|
# adjust and round sellprice
|
|
$form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
|
|
|
|
next if $payments_only;
|
|
|
|
# update lastcost
|
|
$query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
|
|
do_query($form, $dbh, $query, $form->{"sellprice_$i"} / $basefactor, conv_i($form->{"id_$i"}));
|
|
}
|
|
|
|
next if $payments_only;
|
|
|
|
CVar->get_non_editable_ic_cvars(form => $form,
|
|
dbh => $dbh,
|
|
row => $i,
|
|
sub_module => 'invoice',
|
|
may_converted_from => ['delivery_order_items', 'orderitems', 'invoice']);
|
|
|
|
if (!$form->{"invoice_id_$i"}) {
|
|
# there is no persistent id, therefore create one with all necessary constraints
|
|
my $q_invoice_id = qq|SELECT nextval('invoiceid')|;
|
|
my $h_invoice_id = prepare_query($form, $dbh, $q_invoice_id);
|
|
do_statement($form, $h_invoice_id, $q_invoice_id);
|
|
$form->{"invoice_id_$i"} = $h_invoice_id->fetchrow_array();
|
|
my $q_create_invoice_id = qq|INSERT INTO invoice (id, trans_id, position, parts_id) values (?, ?, ?, ?)|;
|
|
do_query($form, $dbh, $q_create_invoice_id, conv_i($form->{"invoice_id_$i"}),
|
|
conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}));
|
|
$h_invoice_id->finish();
|
|
}
|
|
|
|
# save detail record in invoice table
|
|
$query = <<SQL;
|
|
UPDATE invoice SET trans_id = ?, position = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
|
|
sellprice = ?, fxsellprice = ?, discount = ?, allocated = ?, unit = ?, deliverydate = ?,
|
|
project_id = ?, serialnumber = ?, price_factor_id = ?,
|
|
price_factor = (SELECT factor FROM price_factors WHERE id = ?), marge_price_factor = ?,
|
|
active_price_source = ?, active_discount_source = ?
|
|
WHERE id = ?
|
|
SQL
|
|
|
|
@values = (conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}),
|
|
$form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}), $form->{"qty_$i"} * -1,
|
|
$baseqty * -1, $form->{"sellprice_$i"}, $fxsellprice, $form->{"discount_$i"}, $allocated,
|
|
$form->{"unit_$i"}, conv_date($form->{deliverydate}),
|
|
conv_i($form->{"project_id_$i"}), $form->{"serialnumber_$i"},
|
|
conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"marge_price_factor_$i"}),
|
|
$form->{"active_price_source_$i"}, $form->{"active_discount_source_$i"},
|
|
conv_i($form->{"invoice_id_$i"}));
|
|
do_query($form, $dbh, $query, @values);
|
|
push @processed_invoice_ids, $form->{"invoice_id_$i"};
|
|
|
|
CVar->save_custom_variables(module => 'IC',
|
|
sub_module => 'invoice',
|
|
trans_id => $form->{"invoice_id_$i"},
|
|
configs => $ic_cvar_configs,
|
|
variables => $form,
|
|
name_prefix => 'ic_',
|
|
name_postfix => "_$i",
|
|
dbh => $dbh);
|
|
|
|
# link previous items with invoice items See IS.pm (no credit note -> no invoice item)
|
|
foreach (qw(delivery_order_items orderitems invoice)) {
|
|
if (!$form->{useasnew} && $form->{"converted_from_${_}_id_$i"}) {
|
|
RecordLinks->create_links('dbh' => $dbh,
|
|
'mode' => 'ids',
|
|
'from_table' => $_,
|
|
'from_ids' => $form->{"converted_from_${_}_id_$i"},
|
|
'to_table' => 'invoice',
|
|
'to_id' => $form->{"invoice_id_$i"},
|
|
);
|
|
}
|
|
delete $form->{"converted_from_${_}_id_$i"};
|
|
}
|
|
}
|
|
|
|
$h_item_unit->finish();
|
|
|
|
$project_id = conv_i($form->{"globalproject_id"});
|
|
|
|
$form->{datepaid} = $form->{invdate};
|
|
|
|
# all amounts are in natural state, netamount includes the taxes
|
|
# if tax is included, netamount is rounded to 2 decimal places,
|
|
# taxes are not
|
|
|
|
# total payments
|
|
for my $i (1 .. $form->{paidaccounts}) {
|
|
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
|
|
$form->{paid} += $form->{"paid_$i"};
|
|
$form->{datepaid} = $form->{"datepaid_$i"} if $form->{"datepaid_$i"};
|
|
}
|
|
|
|
my ($tax, $paiddiff) = (0, 0);
|
|
|
|
$netamount = $form->round_amount($netamount, 2);
|
|
|
|
# figure out rounding errors for amount paid and total amount
|
|
if ($form->{taxincluded}) {
|
|
|
|
$amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
|
|
$paiddiff = $amount - $netamount * $form->{exchangerate};
|
|
$netamount = $amount;
|
|
|
|
foreach $item (split / /, $form->{taxaccounts}) {
|
|
$amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
|
|
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
|
|
|
|
$amount = $form->{amount}{ $form->{id} }{$item} * -1;
|
|
$tax += $amount;
|
|
$netamount -= $amount;
|
|
}
|
|
|
|
$invoicediff += $paiddiff;
|
|
$expensediff += $paiddiff;
|
|
|
|
######## this only applies to tax included
|
|
|
|
# in the sales invoice case rounding errors only have to be corrected for
|
|
# income accounts, it is enough to add the total rounding error to one of
|
|
# the income accounts, with the one assigned to the last row being used
|
|
# (lastinventoryaccno)
|
|
|
|
# in the purchase invoice case rounding errors may be split between
|
|
# inventory accounts and expense accounts. After rounding, an error of 1
|
|
# cent is introduced if the total rounding error exceeds 0.005. The total
|
|
# error is made up of $invoicediff and $expensediff, however, so if both
|
|
# values are below 0.005, but add up to a total >= 0.005, correcting
|
|
# lastinventoryaccno and lastexpenseaccno separately has no effect after
|
|
# rounding. This caused bug 1579. Therefore when the combined total exceeds
|
|
# 0.005, but neither do individually, the account with the larger value
|
|
# shall receive the total rounding error, and the next time it is rounded
|
|
# the 1 cent correction will be introduced.
|
|
|
|
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno;
|
|
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno;
|
|
|
|
if ( (abs($expensediff)+abs($invoicediff)) >= 0.005 and abs($expensediff) < 0.005 and abs($invoicediff) < 0.005 ) {
|
|
|
|
# in total the rounding error adds up to 1 cent effectively, correct the
|
|
# larger of the two numbers
|
|
|
|
if ( abs($form->{amount}{ $form->{id} }{$lastinventoryaccno}) > abs($form->{amount}{ $form->{id} }{$lastexpenseaccno}) ) {
|
|
# $invoicediff has already been deducted, now also deduct expensediff
|
|
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $expensediff;
|
|
} else {
|
|
# expensediff has already been deducted, now also deduct invoicediff
|
|
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $invoicediff;
|
|
};
|
|
};
|
|
|
|
} else {
|
|
$amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
|
|
$paiddiff = $amount - $netamount * $form->{exchangerate};
|
|
$netamount = $amount;
|
|
|
|
foreach my $item (split / /, $form->{taxaccounts}) {
|
|
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
|
|
$amount = $form->round_amount( $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1, 2);
|
|
$paiddiff += $amount - $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1;
|
|
$form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount * -1, 2);
|
|
$amount = $form->{amount}{ $form->{id} }{$item} * -1;
|
|
$tax += $amount;
|
|
}
|
|
}
|
|
|
|
$form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
|
|
|
|
|
|
$form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0;
|
|
|
|
# record acc_trans transactions
|
|
my $taxdate = $form->{tax_point} || $form->{deliverydate} || $form->{invdate};
|
|
foreach my $trans_id (keys %{ $form->{amount} }) {
|
|
foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
|
|
$form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
|
|
|
|
|
|
next if $payments_only || !$form->{amount}{$trans_id}{$accno};
|
|
|
|
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link)
|
|
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
|
|
(SELECT taxkey_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart
|
|
WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
?,
|
|
(SELECT tax_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart
|
|
WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT link FROM chart WHERE accno = ?))|;
|
|
@values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno},
|
|
conv_date($form->{invdate}), $accno, conv_date($taxdate), $project_id, $accno, conv_date($taxdate), $accno);
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
}
|
|
|
|
# deduct payment differences from paiddiff
|
|
for my $i (1 .. $form->{paidaccounts}) {
|
|
if ($form->{"paid_$i"} != 0) {
|
|
$amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
|
|
$paiddiff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
|
|
}
|
|
}
|
|
|
|
# force AP entry if 0
|
|
|
|
$form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0;
|
|
|
|
my %already_cleared = %{ $params{already_cleared} // {} };
|
|
|
|
# record payments and offsetting AP
|
|
for my $i (1 .. $form->{paidaccounts}) {
|
|
if ($form->{"acc_trans_id_$i"}
|
|
&& $payments_only
|
|
&& (SL::DB::Default->get->payments_changeable == 0)) {
|
|
next;
|
|
}
|
|
|
|
next if $form->{"paid_$i"} == 0;
|
|
|
|
my ($accno) = split /--/, $form->{"AP_paid_$i"};
|
|
$form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
|
|
$form->{datepaid} = $form->{"datepaid_$i"};
|
|
|
|
$amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2) * -1;
|
|
|
|
my $new_cleared = !$form->{"acc_trans_id_$i"} ? 'f'
|
|
: !$already_cleared{$form->{"acc_trans_id_$i"}} ? 'f'
|
|
: $already_cleared{$form->{"acc_trans_id_$i"}}->{amount} != $form->{"paid_$i"} ? 'f'
|
|
: $already_cleared{$form->{"acc_trans_id_$i"}}->{accno} != $accno ? 'f'
|
|
: $already_cleared{$form->{"acc_trans_id_$i"}}->{cleared} ? 't'
|
|
: 'f';
|
|
|
|
# record AP
|
|
if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
|
|
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, cleared, tax_id, chart_link)
|
|
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
|
|
(SELECT taxkey_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart
|
|
WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
?, ?,
|
|
(SELECT tax_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart
|
|
WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT link FROM chart WHERE accno = ?))|;
|
|
@values = (conv_i($form->{id}), $form->{AP}, $amount,
|
|
$form->{"datepaid_$i"}, $form->{AP}, conv_date($form->{"datepaid_$i"}), $project_id, $new_cleared, $form->{AP}, conv_date($form->{"datepaid_$i"}), $form->{AP});
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
|
|
# record payment
|
|
my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
|
|
|
|
$query =
|
|
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, project_id, cleared, tax_id, chart_link)
|
|
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?,
|
|
(SELECT taxkey_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
?, ?,
|
|
(SELECT tax_id
|
|
FROM taxkeys
|
|
WHERE chart_id= (SELECT id
|
|
FROM chart WHERE accno = ?)
|
|
AND startdate <= ?
|
|
ORDER BY startdate DESC LIMIT 1),
|
|
(SELECT link FROM chart WHERE accno = ?))|;
|
|
@values = (conv_i($form->{id}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"},
|
|
$gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($form->{"datepaid_$i"}), $project_id, $new_cleared, $accno, conv_date($form->{"datepaid_$i"}), $accno);
|
|
do_query($form, $dbh, $query, @values);
|
|
|
|
$exchangerate = 0;
|
|
|
|
if ($form->{currency} eq $defaultcurrency) {
|
|
$form->{"exchangerate_$i"} = 1;
|
|
} else {
|
|
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
|
|
$form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
|
|
}
|
|
|
|
# exchangerate difference
|
|
$form->{fx}{$accno}{ $form->{"datepaid_$i"} } += $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff;
|
|
|
|
# gain/loss
|
|
$amount =
|
|
($form->{"paid_$i"} * $form->{exchangerate}) -
|
|
($form->{"paid_$i"} * $form->{"exchangerate_$i"});
|
|
if ($amount > 0) {
|
|
$form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } += $amount;
|
|
} else {
|
|
$form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } += $amount;
|
|
}
|
|
|
|
$paiddiff = 0;
|
|
|
|
# update exchange rate for PAYMENTS
|
|
$form->{script} = 'ir.pl';
|
|
$form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"})
|
|
if ($form->{currency} ne $defaultcurrency) && !$exchangerate;
|
|
}
|
|
|
|
# record exchange rate differences and gains/losses
|
|
foreach my $accno (keys %{ $form->{fx} }) {
|
|
foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
|
|
$form->{fx}{$accno}{$transdate} = $form->round_amount($form->{fx}{$accno}{$transdate}, 2);
|
|
next if ($form->{fx}{$accno}{$transdate} == 0);
|
|
|
|
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey, project_id, tax_id, chart_link)
|
|
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, '0', '1', 0, ?,
|
|
(SELECT id FROM tax WHERE taxkey=0 LIMIT 1),
|
|
(SELECT link FROM chart WHERE accno = ?))|;
|
|
@values = (conv_i($form->{id}), $accno, $form->{fx}{$accno}{$transdate}, conv_date($transdate), $project_id, $accno);
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
}
|
|
|
|
IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
|
|
|
|
if ($payments_only) {
|
|
$query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
|
|
do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
|
|
$form->new_lastmtime('ap');
|
|
|
|
return;
|
|
}
|
|
|
|
$amount = $netamount + $tax;
|
|
|
|
# set values which could be empty
|
|
my $taxzone_id = $form->{taxzone_id} * 1;
|
|
$taxzone_id = SL::DB::Manager::TaxZone->get_default->id unless SL::DB::Manager::TaxZone->find_by(id => $taxzone_id);
|
|
|
|
$form->{invnumber} = $form->{id} unless $form->{invnumber};
|
|
|
|
# save AP record
|
|
$query = qq|UPDATE ap SET
|
|
invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?,
|
|
orddate = ?, quodate = ?, vendor_id = ?, amount = ?,
|
|
netamount = ?, paid = ?, duedate = ?, deliverydate = ?,
|
|
invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?,
|
|
intnotes = ?, storno_id = ?, storno = ?, tax_point = ?,
|
|
cp_id = ?, employee_id = ?, department_id = ?, delivery_term_id = ?,
|
|
payment_id = ?, transaction_description = ?,
|
|
currency_id = (SELECT id FROM currencies WHERE name = ?),
|
|
globalproject_id = ?, direct_debit = ?
|
|
WHERE id = ?|;
|
|
@values = (
|
|
$form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, conv_date($form->{invdate}),
|
|
conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount,
|
|
$netamount, $form->{paid}, conv_date($form->{duedate}), conv_date($form->{deliverydate}),
|
|
'1', $taxzone_id, $restricter->process($form->{notes}), $form->{taxincluded} ? 't' : 'f',
|
|
$form->{intnotes}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f', conv_date($form->{tax_point}),
|
|
conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), conv_i($form->{delivery_term_id}),
|
|
conv_i($form->{payment_id}), $form->{transaction_description},
|
|
$form->{"currency"},
|
|
conv_i($form->{globalproject_id}),
|
|
$form->{direct_debit} ? 't' : 'f',
|
|
conv_i($form->{id})
|
|
);
|
|
do_query($form, $dbh, $query, @values);
|
|
|
|
if ($form->{storno}) {
|
|
$query = qq|UPDATE ap SET paid = paid + amount WHERE id = ?|;
|
|
do_query($form, $dbh, $query, conv_i($form->{storno_id}));
|
|
|
|
$query = qq|UPDATE ap SET storno = 't' WHERE id = ?|;
|
|
do_query($form, $dbh, $query, conv_i($form->{storno_id}));
|
|
|
|
$query = qq!UPDATE ap SET intnotes = ? || intnotes WHERE id = ?!;
|
|
do_query($form, $dbh, $query, "Rechnung storniert am $form->{invdate} ", conv_i($form->{storno_id}));
|
|
|
|
$query = qq|UPDATE ap SET paid = amount WHERE id = ?|;
|
|
do_query($form, $dbh, $query, conv_i($form->{id}));
|
|
}
|
|
|
|
$form->new_lastmtime('ap');
|
|
|
|
$form->{name} = $form->{vendor};
|
|
$form->{name} =~ s/--\Q$form->{vendor_id}\E//;
|
|
|
|
# add shipto
|
|
$form->add_shipto($dbh, $form->{id}, "AP");
|
|
|
|
# delete zero entries
|
|
do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE amount = 0 AND trans_id = ?|, $form->{id});
|
|
|
|
|
|
Common::webdav_folder($form);
|
|
|
|
# Link this record to the records it was created from order or invoice (storno)
|
|
foreach (qw(oe ap)) {
|
|
if ($form->{"convert_from_${_}_ids"}) {
|
|
RecordLinks->create_links('dbh' => $dbh,
|
|
'mode' => 'ids',
|
|
'from_table' => $_,
|
|
'from_ids' => $form->{"convert_from_${_}_ids"},
|
|
'to_table' => 'ap',
|
|
'to_id' => $form->{id},
|
|
);
|
|
delete $form->{"convert_from_${_}_ids"};
|
|
}
|
|
}
|
|
|
|
my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
|
|
if (scalar @convert_from_do_ids) {
|
|
DO->close_orders('dbh' => $dbh,
|
|
'ids' => \@convert_from_do_ids);
|
|
|
|
RecordLinks->create_links('dbh' => $dbh,
|
|
'mode' => 'ids',
|
|
'from_table' => 'delivery_orders',
|
|
'from_ids' => \@convert_from_do_ids,
|
|
'to_table' => 'ap',
|
|
'to_id' => $form->{id},
|
|
);
|
|
}
|
|
delete $form->{convert_from_do_ids};
|
|
|
|
ARAP->close_orders_if_billed('dbh' => $dbh,
|
|
'arap_id' => $form->{id},
|
|
'table' => 'ap',);
|
|
|
|
# search for orphaned invoice items
|
|
$query = sprintf 'SELECT id FROM invoice WHERE trans_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_invoice_ids;
|
|
@values = (conv_i($form->{id}), map { conv_i($_) } @processed_invoice_ids);
|
|
my @orphaned_ids = map { $_->{id} } selectall_hashref_query($form, $dbh, $query, @values);
|
|
if (scalar @orphaned_ids) {
|
|
# clean up invoice items
|
|
$query = sprintf 'DELETE FROM invoice WHERE id IN (%s)', join ', ', ("?") x scalar @orphaned_ids;
|
|
do_query($form, $dbh, $query, @orphaned_ids);
|
|
}
|
|
|
|
if ($form->{draft_id}) {
|
|
SL::DB::Manager::Draft->delete_all(where => [ id => delete($form->{draft_id}) ]);
|
|
}
|
|
|
|
# safety check datev export
|
|
if ($::instance_conf->get_datev_check_on_purchase_invoice) {
|
|
|
|
my $datev = SL::DATEV->new(
|
|
dbh => $dbh,
|
|
trans_id => $form->{id},
|
|
);
|
|
|
|
$datev->generate_datev_data;
|
|
|
|
if ($datev->errors) {
|
|
die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
|
|
}
|
|
}
|
|
|
|
$validity_token->delete if $validity_token;
|
|
delete $form->{form_validity_token};
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub reverse_invoice {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my ($dbh, $form) = @_;
|
|
|
|
# reverse inventory items
|
|
my $query =
|
|
qq|SELECT i.parts_id, p.part_type, i.qty, i.allocated, i.sellprice
|
|
FROM invoice i, parts p
|
|
WHERE (i.parts_id = p.id)
|
|
AND (i.trans_id = ?)|;
|
|
my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
|
|
|
|
my $netamount = 0;
|
|
|
|
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
|
|
$netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
|
|
|
|
next unless $ref->{part_type} eq 'part';
|
|
|
|
# if $ref->{allocated} > 0 than we sold that many items
|
|
next if ($ref->{allocated} <= 0);
|
|
|
|
# get references for sold items
|
|
$query =
|
|
qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
|
|
FROM invoice i, ar a
|
|
WHERE (i.parts_id = ?)
|
|
AND (i.allocated < 0)
|
|
AND (i.trans_id = a.id)
|
|
ORDER BY transdate DESC|;
|
|
my $sth2 = prepare_execute_query($form, $dbh, $query, $ref->{parts_id});
|
|
|
|
while (my $pthref = $sth2->fetchrow_hashref("NAME_lc")) {
|
|
my $qty = $ref->{allocated};
|
|
if (($ref->{allocated} + $pthref->{allocated}) > 0) {
|
|
$qty = $pthref->{allocated} * -1;
|
|
}
|
|
|
|
my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
|
|
|
|
#adjust allocated
|
|
$form->update_balance($dbh, "invoice", "allocated", qq|id = $pthref->{id}|, $qty);
|
|
|
|
if ( $::instance_conf->get_inventory_system eq 'perpetual' ) {
|
|
|
|
$form->update_balance($dbh, "acc_trans", "amount",
|
|
qq| (trans_id = $pthref->{trans_id})
|
|
AND (chart_id = $ref->{expense_accno_id})
|
|
AND (transdate = '$pthref->{transdate}')|,
|
|
$amount);
|
|
|
|
$form->update_balance($dbh, "acc_trans", "amount",
|
|
qq| (trans_id = $pthref->{trans_id})
|
|
AND (chart_id = $ref->{inventory_accno_id})
|
|
AND (transdate = '$pthref->{transdate}')|,
|
|
$amount * -1);
|
|
}
|
|
|
|
last if (($ref->{allocated} -= $qty) <= 0);
|
|
}
|
|
$sth2->finish();
|
|
}
|
|
$sth->finish();
|
|
|
|
my $id = conv_i($form->{id});
|
|
|
|
# delete acc_trans
|
|
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
|
|
do_query($form, $dbh, $query, $id);
|
|
|
|
$query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'AP')|;
|
|
do_query($form, $dbh, $query, $id);
|
|
|
|
$main::lxdebug->leave_sub();
|
|
}
|
|
|
|
sub delete_invoice {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my ($self, $myconfig, $form) = @_;
|
|
my $query;
|
|
# connect to database
|
|
my $dbh = SL::DB->client->dbh;
|
|
|
|
SL::DB->client->with_transaction(sub{
|
|
|
|
&reverse_invoice($dbh, $form);
|
|
|
|
my @values = (conv_i($form->{id}));
|
|
|
|
my @queries = (
|
|
qq|DELETE FROM invoice WHERE trans_id = ?|,
|
|
qq|DELETE FROM ap WHERE id = ?|,
|
|
);
|
|
|
|
map { do_query($form, $dbh, $_, @values) } @queries;
|
|
1;
|
|
}) or do { die SL::DB->client->error };
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub retrieve_invoice {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my ($self, $myconfig, $form) = @_;
|
|
|
|
# connect to database
|
|
my $dbh = SL::DB->client->dbh;
|
|
|
|
my ($query, $sth, $ref, $q_invdate);
|
|
|
|
if (!$form->{id}) {
|
|
$q_invdate = qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) AS invdate|;
|
|
if ($form->{vendor_id}) {
|
|
my $vendor_id = $dbh->quote($form->{vendor_id} * 1);
|
|
$q_invdate .=
|
|
qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) +
|
|
COALESCE((SELECT pt.terms_netto
|
|
FROM vendor v
|
|
LEFT JOIN payment_terms pt ON (v.payment_id = pt.id)
|
|
WHERE v.id = $vendor_id),
|
|
0) AS duedate|;
|
|
}
|
|
}
|
|
|
|
# get default accounts and last invoice number
|
|
|
|
$query = qq|SELECT
|
|
(SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno,
|
|
(SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno,
|
|
(SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno,
|
|
(SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
|
|
(SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
|
|
$q_invdate
|
|
FROM defaults d|;
|
|
$ref = selectfirst_hashref_query($form, $dbh, $query);
|
|
map { $form->{$_} = $ref->{$_} } keys %$ref;
|
|
|
|
if (!$form->{id}) {
|
|
$main::lxdebug->leave_sub();
|
|
|
|
return;
|
|
}
|
|
|
|
# retrieve invoice
|
|
$query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate,
|
|
orddate, quodate, deliverydate, tax_point, globalproject_id,
|
|
ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate,
|
|
mtime, itime,
|
|
intnotes, (SELECT cu.name FROM currencies cu WHERE cu.id=ap.currency_id) AS currency, direct_debit,
|
|
payment_id, delivery_term_id, transaction_description
|
|
FROM ap
|
|
WHERE id = ?|;
|
|
$ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
|
|
map { $form->{$_} = $ref->{$_} } keys %$ref;
|
|
$form->{mtime} = $form->{itime} if !$form->{mtime};
|
|
$form->{lastmtime} = $form->{mtime};
|
|
|
|
($form->{exchangerate}, $form->{record_forex}) = $form->check_exchangerate($myconfig, $form->{currency}, $form->{invdate}, "sell", conv_i($form->{id}), 'ap');
|
|
|
|
# get shipto
|
|
$query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AP')|;
|
|
$ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
|
|
delete $ref->{id};
|
|
map { $form->{$_} = $ref->{$_} } keys %$ref;
|
|
|
|
my $transdate = $form->{tax_point} ? $dbh->quote($form->{tax_point}) :$form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
|
|
|
|
my $taxzone_id = $form->{taxzone_id} * 1;
|
|
$taxzone_id = SL::DB::Manager::TaxZone->get_default->id unless SL::DB::Manager::TaxZone->find_by(id => $taxzone_id);
|
|
|
|
# retrieve individual items
|
|
$query =
|
|
qq|SELECT
|
|
c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from AS inventory_valid,
|
|
c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from AS income_valid,
|
|
c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from AS expense_valid,
|
|
|
|
i.id AS invoice_id,
|
|
i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber,
|
|
i.price_factor_id, i.price_factor, i.marge_price_factor, i.discount, i.active_price_source, i.active_discount_source,
|
|
p.partnumber, p.part_type, pr.projectnumber, pg.partsgroup
|
|
,p.classification_id
|
|
|
|
FROM invoice i
|
|
JOIN parts p ON (i.parts_id = p.id)
|
|
LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id)
|
|
LEFT JOIN chart c2 ON ((SELECT tc.income_accno_id FROM taxzone_charts tc where tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c2.id)
|
|
LEFT JOIN chart c3 ON ((SELECT tc.expense_accno_id FROM taxzone_charts tc where tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c3.id)
|
|
LEFT JOIN project pr ON (i.project_id = pr.id)
|
|
LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
|
|
|
|
WHERE i.trans_id = ?
|
|
|
|
ORDER BY i.position|;
|
|
$sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
|
|
|
|
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
|
|
# Retrieve custom variables.
|
|
my $cvars = CVar->get_custom_variables(dbh => $dbh,
|
|
module => 'IC',
|
|
sub_module => 'invoice',
|
|
trans_id => $ref->{invoice_id},
|
|
);
|