use SL::DBUtils;
use SL::DO;
use SL::GenericTranslations;
+use SL::HTML::Restrict;
use SL::IO;
use SL::MoreCommon;
use SL::DB::Default;
+use SL::DB::TaxZone;
use List::Util qw(min);
use strict;
# connect to database, turn off autocommit
my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+ 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 $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) VALUES (?, '')|, $form->{id});
+ 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});
}
}
- my ($currencies) = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|);
- my $defaultcurrency = (split m/:/, $currencies)[0];
-
if ($form->{currency} eq $defaultcurrency) {
$form->{exchangerate} = 1;
} else {
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"};
map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
$price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
- #####################################################################
- # das ist aus IS.pm kopiert. schlimm. jb 7.10.2009
- # ich würde mir wünschen, dass diese vier stellen zusammengefasst werden
- # ... vier stellen = (einkauf + verkauf) * (maske + backend)
- # ansonsten stolpert man immer wieder viermal statt einmal heftig
- # und auch das undo discount formatting ist nicht besonders wartungsfreundlich
-
- # keine ahnung wofür das in IS.pm gemacht wird:
- # my ($dec) = ($fxsellprice =~ /\.(\d+)/);
- # $dec = length $dec;
- # my $decimalplaces = ($dec > 2) ? $dec : 2;
+ # 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;
# update parts table by setting lastcost to current price, don't allow negative values by using abs
$query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
- @values = (abs($form->{"sellprice_$i"} / $basefactor), conv_i($form->{"id_$i"}));
+ @values = (abs($fxsellprice * $form->{exchangerate} / $basefactor), conv_i($form->{"id_$i"}));
do_query($form, $dbh, $query, @values);
# 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,
- p.inventory_accno_id, p.expense_accno_id, a.transdate
- FROM invoice i, ar a, parts p
+ 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
+ # 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
+ # 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
+ # 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"}));
if ($ref->{allocated} < 0) {
-# we have an entry for it already, adjust amount
+ # 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})
# 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) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|;
- @values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id});
+ $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) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|;
- @values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id});
+ # 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);
}
};
next if $payments_only;
- # save detail record in invoice table
- my ($invoice_id) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('invoiceid')|);
+ 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, parts_id) values (?, ?, ?)|;
+ do_query($form, $dbh, $q_create_invoice_id, conv_i($form->{"invoice_id_$i"}), conv_i($form->{id}), conv_i($form->{"id_$i"}));
+ $h_invoice_id->finish();
+ }
- $query =
- qq|INSERT INTO invoice (id, trans_id, parts_id, description, longdescription, qty, base_qty,
- sellprice, fxsellprice, discount, allocated, unit, deliverydate,
- project_id, serialnumber, price_factor_id, price_factor, marge_price_factor)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT factor FROM price_factors WHERE id = ?), ?)|;
- @values = ($invoice_id, conv_i($form->{id}), conv_i($form->{"id_$i"}),
- $form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"} * -1,
+ # save detail record in invoice table
+ $query = <<SQL;
+ UPDATE invoice SET trans_id = ?, 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($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"}));
+ 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 => $invoice_id,
+ trans_id => $form->{"invoice_id_$i"},
configs => $ic_cvar_configs,
variables => $form,
name_prefix => 'ic_',
name_postfix => "_$i",
dbh => $dbh);
+ # link oe items with invoice
+ if ($form->{"converted_from_orderitems_id_$i"}) {
+ RecordLinks->create_links('dbh' => $dbh,
+ 'mode' => 'ids',
+ 'from_table' => 'orderitems',
+ 'from_ids' => $form->{"converted_from_orderitems_id_$i"},
+ 'to_table' => 'invoice',
+ 'to_id' => $form->{"invoice_id_$i"},
+ );
+ delete $form->{"converted_from_orderitems_id_$i"};
+ }
+ # link doi items with invoice
+ if ($form->{"converted_from_delivery_order_items_id_$i"}) {
+ RecordLinks->create_links('dbh' => $dbh,
+ 'mode' => 'ids',
+ 'from_table' => 'delivery_order_items',
+ 'from_ids' => $form->{"converted_from_delivery_order_items_id_$i"},
+ 'to_table' => 'invoice',
+ 'to_id' => $form->{"invoice_id_$i"},
+ );
+ delete $form->{"converted_from_delivery_order_items_id_$i"};
+ }
}
$h_item_unit->finish();
next if $payments_only || !$form->{amount}{$trans_id}{$accno};
- $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
+ $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 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, $project_id);
+ conv_date($form->{invdate}), $accno, conv_date($form->{invdate}), $project_id, $accno, conv_date($form->{invdate}), $accno);
do_query($form, $dbh, $query, @values);
}
}
# 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)
+ $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 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}, $project_id);
+ $form->{"datepaid_$i"}, $form->{AP}, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AP}, conv_date($form->{"datepaid_$i"}), $form->{AP});
do_query($form, $dbh, $query, @values);
}
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)
+ qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, project_id, tax_id, chart_link)
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?,
- (SELECT taxkey_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, $project_id);
+ $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($form->{"datepaid_$i"}), $project_id, $accno, conv_date($form->{"datepaid_$i"}), $accno);
do_query($form, $dbh, $query, @values);
$exchangerate = 0;
$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)
- VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, '0', '1', 0, ?)|;
- @values = (conv_i($form->{id}), $accno, $form->{fx}{$accno}{$transdate}, conv_date($transdate), $project_id);
+ $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);
}
}
# 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);
- # Seit neuestem wird die department_id schon übergeben UND $form->department nicht mehr
- # korrekt zusammengebaut. Sehr wahrscheinlich beim Umstieg auf T8 kaputt gegangen
- # Ich lass den Code von 2005 erstmal noch stehen ;-) jb 03-2011
- # copy & paste von IS.pm
- if (!$form->{department_id}){
- $form->{department_id} = (split /--/, $form->{department})[1];
- }
$form->{invnumber} = $form->{id} unless $form->{invnumber};
- $taxzone_id = 0 if (3 < $taxzone_id) || (0 > $taxzone_id);
-
# save AP record
$query = qq|UPDATE ap SET
invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?,
orddate = ?, quodate = ?, vendor_id = ?, amount = ?,
netamount = ?, paid = ?, duedate = ?,
invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?,
- intnotes = ?, curr = ?, storno_id = ?, storno = ?,
- cp_id = ?, employee_id = ?, department_id = ?,
- globalproject_id = ?
+ intnotes = ?, storno_id = ?, storno = ?,
+ cp_id = ?, employee_id = ?, department_id = ?, delivery_term_id = ?,
+ 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}),
'1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f',
- $form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f',
- conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}),
+ $form->{intnotes}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f',
+ conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), conv_i($form->{delivery_term_id}),
conv_i($form->{globalproject_id}),
+ $form->{direct_debit} ? 't' : 'f',
conv_i($form->{id})
);
do_query($form, $dbh, $query, @values);
}
- # add shipto
$form->{name} = $form->{vendor};
$form->{name} =~ s/--\Q$form->{vendor_id}\E//;
+
+ # add shipto
$form->add_shipto($dbh, $form->{id}, "AP");
# delete zero entries
Common::webdav_folder($form);
# Link this record to the records it was created from.
- RecordLinks->create_links('dbh' => $dbh,
- 'mode' => 'ids',
- 'from_table' => 'oe',
- 'from_ids' => $form->{convert_from_oe_ids},
- 'to_table' => 'ap',
- 'to_id' => $form->{id},
- );
- delete $form->{convert_from_oe_ids};
+ if ($form->{convert_from_oe_ids}) {
+ RecordLinks->create_links('dbh' => $dbh,
+ 'mode' => 'ids',
+ 'from_table' => 'oe',
+ 'from_ids' => $form->{convert_from_oe_ids},
+ 'to_table' => 'ap',
+ 'to_id' => $form->{id},
+ );
+ delete $form->{convert_from_oe_ids};
+ }
my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
if (scalar @convert_from_do_ids) {
'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);
+ }
+
# safety check datev export
if ($::instance_conf->get_datev_check_on_purchase_invoice) {
my $transdate = $::form->{invdate} ? DateTime->from_lxoffice($::form->{invdate}) : undef;
dbh => $dbh,
from => $transdate,
to => $transdate,
+ trans_id => $form->{id},
);
$datev->export;
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
do_query($form, $dbh, $query, $id);
- # delete invoice entries
- $query = qq|DELETE FROM invoice 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);
&reverse_invoice($dbh, $form);
+ my @values = (conv_i($form->{id}));
+
# delete zero entries
+ # wtf? use case for this?
$query = qq|DELETE FROM acc_trans WHERE amount = 0|;
do_query($form, $dbh, $query);
- # delete AP record
- $query = qq|DELETE FROM ap WHERE id = ?|;
- do_query($form, $dbh, $query, 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;
my $rc = $dbh->commit;
$dbh->disconnect;
(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,
- d.curr AS currencies
+ (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);
$query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate,
orddate, quodate, globalproject_id,
ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate,
- intnotes, curr AS currency
+ intnotes, (SELECT cu.name FROM currencies cu WHERE cu.id=ap.currency_id) AS currency, direct_debit,
+ delivery_term_id
FROM ap
WHERE id = ?|;
$ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
map { $form->{$_} = $ref->{$_} } keys %$ref;
- # remove any trailing whitespace
- $form->{currency} =~ s/\s*$//;
-
$form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell");
# get shipto
map { $form->{$_} = $ref->{$_} } keys %$ref;
my $transdate = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
- my $taxzone_id = $form->{taxzone_id} * 1;
- $taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id));
+ 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 =
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,
- p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, p.bin, pr.projectnumber, pg.partsgroup
+ i.price_factor_id, i.price_factor, i.marge_price_factor, i.discount, i.active_price_source, i.active_discount_source,
+ p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, pr.projectnumber, pg.partsgroup
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 income_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id)
- LEFT JOIN chart c3 ON ((SELECT expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.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)
trans_id => $ref->{invoice_id},
);
map { $ref->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
- delete $ref->{invoice_id};
map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"};
delete($ref->{"part_inventory_accno_id"});
qq|SELECT
v.id AS vendor_id, v.name AS vendor, v.discount as vendor_discount,
v.creditlimit, v.terms, v.notes AS intnotes,
- v.email, v.cc, v.bcc, v.language_id, v.payment_id,
- v.street, v.zipcode, v.city, v.country, v.taxzone_id, v.curr,
+ v.email, v.cc, v.bcc, v.language_id, v.payment_id, v.delivery_term_id,
+ v.street, v.zipcode, v.city, v.country, v.taxzone_id, cu.name AS curr, v.direct_debit,
$duedate + COALESCE(pt.terms_netto, 0) AS duedate,
b.description AS business
FROM vendor v
LEFT JOIN business b ON (b.id = v.business_id)
LEFT JOIN payment_terms pt ON (v.payment_id = pt.id)
+ LEFT JOIN currencies cu ON (v.currency_id = cu.id)
WHERE 1=1 $where|;
my $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
map { $params->{$_} = $ref->{$_} } keys %$ref;
- # remove any trailing whitespace
- $form->{curr} =~ s/\s*$//;
-
- # use vendor currency if not empty
- $form->{currency} = $form->{curr} if $form->{curr};
+ # use vendor currency
+ $form->{currency} = $form->{curr};
$params->{creditremaining} = $params->{creditlimit};
$query = qq|SELECT o.amount,
(SELECT e.sell
FROM exchangerate e
- WHERE (e.curr = o.curr)
+ WHERE (e.currency_id = o.currency_id)
AND (e.transdate = o.transdate)) AS exch
FROM oe o
WHERE (o.vendor_id = ?) AND (o.quotation = '0') AND (o.closed = '0')|;
}
$sth->finish();
- # get shipto if we do not convert an order or invoice
- if (!$params->{shipto}) {
- delete @{$params}{qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail)};
-
- $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module= 'CT')|;
- $ref = selectfirst_hashref_query($form, $dbh, $query, $vid);
- @{$params}{keys %$ref} = @{$ref}{keys %$ref};
- map { $params->{$_} = $ref->{$_} } keys %$ref;
- }
-
if (!$params->{id} && $params->{type} !~ /_(order|quotation)/) {
# setup last accounts used
$query =
$where .= " AND lower(${table_column}) LIKE lower(?)";
push @values, '%' . $form->{"${field}_${i}"} . '%';
}
- #Es soll auch nach EAN gesucht werden, ohne Einschränkung durch Beschreibung
+
+ my (%mm_by_id);
if ($form->{"partnumber_$i"} && !$form->{"description_$i"}) {
- $where .= qq| OR (NOT p.obsolete = '1' AND p.ean = ? )|;
- push @values, $form->{"partnumber_$i"};
- }
+ $where .= qq| OR (NOT p.obsolete = '1' AND p.ean = ? )|;
+ push @values, $form->{"partnumber_$i"};
+
+ # also search hits in makemodels, but only cache the results by id and merge later
+ my $mm_query = qq|
+ SELECT parts_id, model FROM makemodel
+ LEFT JOIN parts ON parts.id = parts_id
+ WHERE NOT parts.obsolete AND model ILIKE ? AND (make IS NULL OR make = ?);
+ |;
+ my $mm_results = selectall_hashref_query($::form, $dbh, $mm_query, '%' . $form->{"partnumber_$i"} . '%', $::form->{vendor_id});
+ my @mm_ids = map { $_->{parts_id} } @$mm_results;
+ push @{$mm_by_id{ $_->{parts_id} } ||= []}, $_ for @$mm_results;
+
+ if (@mm_ids) {
+ $where .= qq| OR p.id IN (| . join(',', ('?') x @mm_ids) . qq|)|;
+ push @values, @mm_ids;
+ }
+ }
# Search for part ID overrides all other criteria.
if ($form->{"id_${i}"}) {
my $transdate = "";
if ($form->{type} eq "invoice") {
- $transdate = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
+ $transdate = $form->{deliverydate} ? $dbh->quote($form->{deliverydate})
+ : $form->{invdate} ? $dbh->quote($form->{invdate})
+ : "current_date";
} else {
$transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
}
my $taxzone_id = $form->{taxzone_id} * 1;
- $taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id));
+ $taxzone_id = SL::DB::Manager::TaxZone->get_default->id unless SL::DB::Manager::TaxZone->find_by(id => $taxzone_id);
my $query =
qq|SELECT
p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice,
- p.unit, p.assembly, p.bin, p.onhand, p.formel,
+ p.unit, p.assembly, p.onhand, p.formel,
p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
p.inventory_accno_id, p.price_factor_id,
+ p.ean,
pfac.factor AS price_factor,
FROM buchungsgruppen
WHERE id = p.buchungsgruppen_id) = c1.id)
LEFT JOIN chart c2 ON
- ((SELECT income_accno_id_${taxzone_id}
- FROM buchungsgruppen
- WHERE id = p.buchungsgruppen_id) = c2.id)
+ ((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 expense_accno_id_${taxzone_id}
- FROM buchungsgruppen
- WHERE id = p.buchungsgruppen_id) = c3.id)
+ ((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 partsgroup pg ON (pg.id = p.partsgroup_id)
LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)
WHERE $where|;
$form->{item_list} = [];
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
+ if ($mm_by_id{$ref->{id}}) {
+ $ref->{makemodels} = $mm_by_id{$ref->{id}};
+ push @{ $ref->{matches} ||= [] }, $::locale->text('Model') . ': ' . join ', ', map { $_->{model} } @{ $mm_by_id{$ref->{id}} };
+ }
+
+ if ($ref->{ean} eq $::form->{"partnumber_$i"}) {
+ push @{ $ref->{matches} ||= [] }, $::locale->text('EAN') . ': ' . $ref->{ean};
+ }
+
# In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
# es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
# Buchungskonto also aus dem Ergebnis rausgenommen werden.
my $i = 0;
while (my $ptr = $stw->fetchrow_hashref("NAME_lc")) {
- # if ($customertax{$ref->{accno}}) {
if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
$i++;
$ptr->{accno} = $i;
# fax and phone and email as vendor*
my $query =
qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail,
- ct.curr AS currency
+ cu.name AS currency
FROM vendor ct
LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id)
+ LEFT JOIN currencies cu ON (ct.currency_id = cu.id)
WHERE (ct.id = ?) $contact
ORDER BY cp.cp_id
LIMIT 1|;
}
map { $form->{$_} = $ref->{$_} } keys %$ref;
- # remove any trailing whitespace
- $form->{currency} =~ s/\s*$// if ($form->{currency});
my $custom_variables = CVar->get_custom_variables('dbh' => $dbh,
'module' => 'CT',