1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Inventory received module
33 #======================================================================
43 use List::Util qw(min);
46 $main::lxdebug->enter_sub();
48 my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
50 # connect to database, turn off autocommit
51 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
52 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
54 my ($query, $sth, @values, $project_id);
55 my ($allocated, $taxrate, $taxamount, $taxdiff, $item);
56 my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
57 my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
60 my $all_units = AM->retrieve_units($myconfig, $form);
62 if (!$payments_only) {
64 &reverse_invoice($dbh, $form);
66 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
67 do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber) VALUES (?, '')|, $form->{id});
71 my ($currencies) = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|);
72 my $defaultcurrency = (split m/:/, $currencies)[0];
74 if ($form->{currency} eq $defaultcurrency) {
75 $form->{exchangerate} = 1;
77 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
80 $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
81 $form->{exchangerate} = 1 unless ($form->{exchangerate} * 1);
84 my $q_item_unit = qq|SELECT unit FROM parts WHERE id = ?|;
85 my $h_item_unit = prepare_query($form, $dbh, $q_item_unit);
87 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
88 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
91 for my $i (1 .. $form->{rowcount}) {
92 next unless $form->{"id_$i"};
94 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
95 $form->{"qty_$i"} *= -1 if $form->{storno};
97 $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $main::eur;
100 if (!$item_units{$form->{"id_$i"}}) {
101 do_statement($form, $h_item_unit, $q_item_unit, $form->{"id_$i"});
102 ($item_units{$form->{"id_$i"}}) = $h_item_unit->fetchrow_array();
105 my $item_unit = $item_units{$form->{"id_$i"}};
107 if (defined($all_units->{$item_unit}->{factor})
108 && ($all_units->{$item_unit}->{factor} ne '')
109 && ($all_units->{$item_unit}->{factor} * 1 != 0)) {
110 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
114 $baseqty = $form->{"qty_$i"} * $basefactor;
116 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
121 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
122 (my $fxsellprice = $form->{"sellprice_$i"}) =~ /\.(\d+)/;
124 my $decimalplaces = ($dec > 2) ? $dec : 2;
126 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
128 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
130 if ($form->{"inventory_accno_$i"}) {
132 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
134 if ($form->{taxincluded}) {
135 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
136 $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
138 $taxamount = $linetotal * $taxrate;
141 $netamount += $linetotal;
143 if ($form->round_amount($taxrate, 7) == 0) {
144 if ($form->{taxincluded}) {
145 foreach $item (@taxaccounts) {
147 $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
148 $taxdiff += $taxamount;
149 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
151 $form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
154 map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
158 map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
161 # add purchase to inventory, this one is without the tax!
162 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
163 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2) * $form->{exchangerate};
164 $linetotal = $form->round_amount($linetotal, 2);
166 # this is the difference for the inventory
167 $invoicediff += ($amount - $linetotal);
169 $form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -= $linetotal;
171 # adjust and round sellprice
172 $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
174 $lastinventoryaccno = $form->{"inventory_accno_$i"};
176 next if $payments_only;
179 $query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
180 @values = ($form->{"sellprice_$i"}, conv_i($form->{"id_$i"}));
181 do_query($form, $dbh, $query, @values);
183 # check if we sold the item already and
184 # make an entry for the expense and inventory
186 qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
187 p.inventory_accno_id, p.expense_accno_id, a.transdate
188 FROM invoice i, ar a, parts p
189 WHERE (i.parts_id = p.id)
191 AND ((i.base_qty + i.allocated) > 0)
192 AND (i.trans_id = a.id)
194 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
196 my $totalqty = $base_qty;
198 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
199 my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated});
200 $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
202 if ($ref->{allocated} < 0) {
204 # we have an entry for it already, adjust amount
205 $form->update_balance($dbh, "acc_trans", "amount",
206 qq| (trans_id = $ref->{trans_id})
207 AND (chart_id = $ref->{inventory_accno_id})
208 AND (transdate = '$ref->{transdate}')|,
211 $form->update_balance($dbh, "acc_trans", "amount",
212 qq| (trans_id = $ref->{trans_id})
213 AND (chart_id = $ref->{expense_accno_id})
214 AND (transdate = '$ref->{transdate}')|,
217 } elsif ($linetotal != 0) {
218 # add entry for inventory, this one is for the sold item
219 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|;
220 @values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id});
221 do_query($form, $dbh, $query, @values);
224 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|;
225 @values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id});
226 do_query($form, $dbh, $query, @values);
229 # update allocated for sold item
230 $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1);
234 last if ($totalqty -= $qty) <= 0;
239 } else { # if ($form->{"inventory_accno_id_$i"})
241 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
243 if ($form->{taxincluded}) {
244 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
245 $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
248 $taxamount = $linetotal * $taxrate;
251 $netamount += $linetotal;
253 if ($form->round_amount($taxrate, 7) == 0) {
254 if ($form->{taxincluded}) {
255 foreach $item (@taxaccounts) {
256 $taxamount = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"}));
257 $totaltax += $taxamount;
258 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
261 map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts;
264 map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts;
267 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
268 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2) * $form->{exchangerate};
269 $linetotal = $form->round_amount($linetotal, 2);
271 # this is the difference for expense
272 $expensediff += ($amount - $linetotal);
274 # add amount to expense
275 $form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -= $linetotal;
277 $lastexpenseaccno = $form->{"expense_accno_$i"};
279 # adjust and round sellprice
280 $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces);
282 next if $payments_only;
285 $query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
286 do_query($form, $dbh, $query, $form->{"sellprice_$i"}, conv_i($form->{"id_$i"}));
289 next if $payments_only;
291 # save detail record in invoice table
293 qq|INSERT INTO invoice (trans_id, parts_id, description, qty, base_qty,
294 sellprice, fxsellprice, allocated, unit, deliverydate,
295 project_id, serialnumber, price_factor_id, price_factor, marge_price_factor)
296 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT factor FROM price_factors WHERE id = ?), ?)|;
297 @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
298 $form->{"description_$i"}, $form->{"qty_$i"} * -1,
299 $baseqty * -1, $form->{"sellprice_$i"}, $fxsellprice, $allocated,
300 $form->{"unit_$i"}, conv_date($form->{deliverydate}),
301 conv_i($form->{"project_id_$i"}), $form->{"serialnumber_$i"},
302 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"marge_price_factor_$i"}));
303 do_query($form, $dbh, $query, @values);
306 $h_item_unit->finish();
308 $project_id = conv_i($form->{"globalproject_id"});
310 $form->{datepaid} = $form->{invdate};
312 # all amounts are in natural state, netamount includes the taxes
313 # if tax is included, netamount is rounded to 2 decimal places,
317 for my $i (1 .. $form->{paidaccounts}) {
318 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
319 $form->{paid} += $form->{"paid_$i"};
320 $form->{datepaid} = $form->{"datepaid_$i"} if $form->{"datepaid_$i"};
323 my ($tax, $paiddiff) = (0, 0);
325 $netamount = $form->round_amount($netamount, 2);
327 # figure out rounding errors for amount paid and total amount
328 if ($form->{taxincluded}) {
330 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
331 $paiddiff = $amount - $netamount * $form->{exchangerate};
332 $netamount = $amount;
334 foreach $item (split / /, $form->{taxaccounts}) {
335 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
336 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
338 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
340 $netamount -= $amount;
343 $invoicediff += $paiddiff;
344 $expensediff += $paiddiff;
346 ######## this only applies to tax included
348 $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno;
349 $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno;
352 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
353 $paiddiff = $amount - $netamount * $form->{exchangerate};
354 $netamount = $amount;
356 foreach my $item (split / /, $form->{taxaccounts}) {
357 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
358 $amount = $form->round_amount( $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1, 2);
359 $paiddiff += $amount - $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1;
360 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount * -1, 2);
361 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
366 $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
369 $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0;
371 # update exchangerate
373 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate})
374 if ($form->{currency} ne $defaultcurrency) && !$exchangerate;
376 # record acc_trans transactions
377 foreach my $trans_id (keys %{ $form->{amount} }) {
378 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
379 $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
381 next if $payments_only || !$form->{amount}{$trans_id}{$accno};
383 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
384 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
385 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
386 @values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno},
387 conv_date($form->{invdate}), $accno, $project_id);
388 do_query($form, $dbh, $query, @values);
392 # deduct payment differences from paiddiff
393 for my $i (1 .. $form->{paidaccounts}) {
394 if ($form->{"paid_$i"} != 0) {
395 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
396 $paiddiff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
400 # force AP entry if 0
402 $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0;
404 # record payments and offsetting AP
405 for my $i (1 .. $form->{paidaccounts}) {
406 next if $form->{"paid_$i"} == 0;
408 my ($accno) = split /--/, $form->{"AP_paid_$i"};
409 $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
410 $form->{datepaid} = $form->{"datepaid_$i"};
412 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2) * -1;
415 if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
416 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
417 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
418 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
419 @values = (conv_i($form->{id}), $form->{AP}, $amount,
420 $form->{"datepaid_$i"}, $form->{AP}, $project_id);
421 do_query($form, $dbh, $query, @values);
425 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, taxkey, project_id)
426 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?,
427 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
428 @values = (conv_i($form->{id}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"},
429 $form->{"source_$i"}, $form->{"memo_$i"}, $accno, $project_id);
430 do_query($form, $dbh, $query, @values);
434 if ($form->{currency} eq $defaultcurrency) {
435 $form->{"exchangerate_$i"} = 1;
437 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
438 $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
441 # exchangerate difference
442 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } += $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff;
446 ($form->{"paid_$i"} * $form->{exchangerate}) -
447 ($form->{"paid_$i"} * $form->{"exchangerate_$i"});
449 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } += $amount;
451 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } += $amount;
456 # update exchange rate
457 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"})
458 if ($form->{currency} ne $defaultcurrency) && !$exchangerate;
461 # record exchange rate differences and gains/losses
462 foreach my $accno (keys %{ $form->{fx} }) {
463 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
464 $form->{fx}{$accno}{$transdate} = $form->round_amount($form->{fx}{$accno}{$transdate}, 2);
465 next if ($form->{fx}{$accno}{$transdate} == 0);
467 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey, project_id)
468 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, '0', '1', 0, ?)|;
469 @values = (conv_i($form->{id}), $accno, $form->{fx}{$accno}{$transdate}, conv_date($transdate), $project_id);
470 do_query($form, $dbh, $query, @values);
474 if ($payments_only) {
475 $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
476 do_query($form, $dbh, $query, $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
478 if (!$provided_dbh) {
483 $main::lxdebug->leave_sub();
487 $amount = $netamount + $tax;
489 # set values which could be empty
490 my $taxzone_id = $form->{taxzone_id} * 1;
491 $form->{department_id} = (split /--/, $form->{department})[1];
492 $form->{invnumber} = $form->{id} unless $form->{invnumber};
494 $taxzone_id = 0 if (3 < $taxzone_id) || (0 > $taxzone_id);
497 $query = qq|UPDATE ap SET
498 invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?,
499 orddate = ?, quodate = ?, vendor_id = ?, amount = ?,
500 netamount = ?, paid = ?, duedate = ?, datepaid = ?,
501 invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?,
502 intnotes = ?, curr = ?, storno_id = ?, storno = ?,
503 cp_id = ?, employee_id = ?, department_id = ?,
507 $form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, conv_date($form->{invdate}),
508 conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount,
509 $netamount, $form->{paid}, conv_date($form->{duedate}), $form->{paid} ? conv_date($form->{datepaid}) : undef,
510 '1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f',
511 $form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f',
512 conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}),
513 conv_i($form->{globalproject_id}),
516 do_query($form, $dbh, $query, @values);
518 if ($form->{storno}) {
519 $query = qq|UPDATE ap SET paid = paid + amount WHERE id = ?|;
520 do_query($form, $dbh, $query, conv_i($form->{storno_id}));
522 $query = qq|UPDATE ap SET storno = 't' WHERE id = ?|;
523 do_query($form, $dbh, $query, conv_i($form->{storno_id}));
525 $query = qq!UPDATE ap SET intnotes = ? || intnotes WHERE id = ?!;
526 do_query($form, $dbh, $query, "Rechnung storniert am $form->{invdate} ", conv_i($form->{storno_id}));
528 $query = qq|UPDATE ap SET paid = amount WHERE id = ?|;
529 do_query($form, $dbh, $query, conv_i($form->{id}));
534 $form->{name} = $form->{vendor};
535 $form->{name} =~ s/--\Q$form->{vendor_id}\E//;
536 $form->add_shipto($dbh, $form->{id}, "AP");
538 # delete zero entries
539 do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE amount = 0|);
541 Common::webdav_folder($form) if ($main::webdav);
543 # Link this record to the records it was created from.
544 RecordLinks->create_links('dbh' => $dbh,
546 'from_table' => 'oe',
547 'from_ids' => $form->{convert_from_oe_ids},
549 'to_id' => $form->{id},
551 delete $form->{convert_from_oe_ids};
553 my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
554 if (scalar @convert_from_do_ids) {
555 DO->close_orders('dbh' => $dbh,
556 'ids' => \@convert_from_do_ids);
558 RecordLinks->create_links('dbh' => $dbh,
560 'from_table' => 'delivery_orders',
561 'from_ids' => \@convert_from_do_ids,
563 'to_id' => $form->{id},
566 delete $form->{convert_from_do_ids};
568 ARAP->close_orders_if_billed('dbh' => $dbh,
569 'arap_id' => $form->{id},
573 if (!$provided_dbh) {
574 $rc = $dbh->commit();
578 $main::lxdebug->leave_sub();
583 sub reverse_invoice {
584 $main::lxdebug->enter_sub();
586 my ($dbh, $form) = @_;
588 # reverse inventory items
590 qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id, i.qty, i.allocated, i.sellprice
591 FROM invoice i, parts p
592 WHERE (i.parts_id = p.id)
593 AND (i.trans_id = ?)|;
594 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
598 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
599 $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
601 next unless $ref->{inventory_accno_id};
603 # if $ref->{allocated} > 0 than we sold that many items
604 next if ($ref->{allocated} <= 0);
606 # get references for sold items
608 qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
610 WHERE (i.parts_id = ?)
611 AND (i.allocated < 0)
612 AND (i.trans_id = a.id)
613 ORDER BY transdate DESC|;
614 my $sth2 = prepare_execute_query($form, $dbh, $query, $ref->{parts_id});
616 while (my $pthref = $sth2->fetchrow_hashref(NAME_lc)) {
617 my $qty = $ref->{allocated};
618 if (($ref->{allocated} + $pthref->{allocated}) > 0) {
619 $qty = $pthref->{allocated} * -1;
622 my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
625 $form->update_balance($dbh, "invoice", "allocated", qq|id = $pthref->{id}|, $qty);
627 $form->update_balance($dbh, "acc_trans", "amount",
628 qq| (trans_id = $pthref->{trans_id})
629 AND (chart_id = $ref->{expense_accno_id})
630 AND (transdate = '$pthref->{transdate}')|,
633 $form->update_balance($dbh, "acc_trans", "amount",
634 qq| (trans_id = $pthref->{trans_id})
635 AND (chart_id = $ref->{inventory_accno_id})
636 AND (transdate = '$pthref->{transdate}')|,
639 last if (($ref->{allocated} -= $qty) <= 0);
645 my $id = conv_i($form->{id});
648 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
649 do_query($form, $dbh, $query, $id);
651 # delete invoice entries
652 $query = qq|DELETE FROM invoice WHERE trans_id = ?|;
653 do_query($form, $dbh, $query, $id);
655 $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'AP')|;
656 do_query($form, $dbh, $query, $id);
658 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect_noauto($myconfig);
669 &reverse_invoice($dbh, $form);
671 # delete zero entries
672 $query = qq|DELETE FROM acc_trans WHERE amount = 0|;
673 do_query($form, $dbh, $query);
676 $query = qq|DELETE FROM ap WHERE id = ?|;
677 do_query($form, $dbh, $query, conv_i($form->{id}));
679 my $rc = $dbh->commit;
682 $main::lxdebug->leave_sub();
687 sub retrieve_invoice {
688 $main::lxdebug->enter_sub();
690 my ($self, $myconfig, $form) = @_;
692 # connect to database
693 my $dbh = $form->dbconnect($myconfig);
695 my ($query, $sth, $ref, $q_invdate);
698 $q_invdate = qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) AS invdate|;
699 if ($form->{vendor_id}) {
700 my $vendor_id = $dbh->quote($form->{vendor_id} * 1);
702 qq|, COALESCE((SELECT transdate FROM ar WHERE id = (SELECT MAX(id) FROM ar)), current_date) +
703 COALESCE((SELECT pt.terms_netto
705 LEFT JOIN payment_terms pt ON (v.payment_id = pt.id)
706 WHERE v.id = $vendor_id),
711 # get default accounts and last invoice number
714 (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno,
715 (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno,
716 (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno,
717 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
718 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
722 $ref = selectfirst_hashref_query($form, $dbh, $query);
723 map { $form->{$_} = $ref->{$_} } keys %$ref;
727 $main::lxdebug->leave_sub();
733 $query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate,
734 orddate, quodate, globalproject_id,
735 ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate,
736 intnotes, curr AS currency
739 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
740 map { $form->{$_} = $ref->{$_} } keys %$ref;
742 $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell");
745 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AP')|;
746 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
748 map { $form->{$_} = $ref->{$_} } keys %$ref;
750 my $transdate = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
751 my $taxzone_id = $form->{taxzone_id} * 1;
753 $taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id));
755 # retrieve individual items
758 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from AS inventory_valid,
759 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from AS income_valid,
760 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from AS expense_valid,
762 i.description, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber,
763 i.price_factor_id, i.price_factor, i.marge_price_factor,
764 p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, p.bin, pr.projectnumber, pg.partsgroup
767 JOIN parts p ON (i.parts_id = p.id)
768 LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id)
769 LEFT JOIN chart c2 ON ((SELECT income_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id)
770 LEFT JOIN chart c3 ON ((SELECT expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id)
771 LEFT JOIN project pr ON (i.project_id = pr.id)
772 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
777 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
779 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
780 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"};
781 delete($ref->{"part_inventory_accno_id"});
783 foreach my $type (qw(inventory income expense)) {
784 while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
785 my $query = qq|SELECT accno, new_chart_id, date($transdate) - valid_from FROM chart WHERE id = ?|;
786 @$ref{ map $type.$_, qw(_accno _new_chart _valid) } = selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"});
790 # get tax rates and description
791 my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
793 qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber FROM tax t
794 LEFT JOIN chart c ON (c.id = t.chart_id)
796 (SELECT tk.tax_id FROM taxkeys tk
797 WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?)
798 AND (startdate <= $transdate)
799 ORDER BY startdate DESC
802 my $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
803 $ref->{taxaccounts} = "";
806 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
807 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
812 $ref->{taxaccounts} .= "$ptr->{accno} ";
814 if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) {
815 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
816 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
817 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
818 $form->{taxaccounts} .= "$ptr->{accno} ";
823 chop $ref->{taxaccounts};
824 push @{ $form->{invoice_details} }, $ref;
829 Common::webdav_folder($form) if ($main::webdav);
833 $main::lxdebug->leave_sub();
837 $main::lxdebug->enter_sub();
839 my ($self, $myconfig, $form, $params) = @_;
841 $params = $form unless defined $params && ref $params eq "HASH";
843 # connect to database
844 my $dbh = $form->dbconnect($myconfig);
846 my $dateformat = $myconfig->{dateformat};
847 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
849 my $vid = conv_i($params->{vendor_id});
850 my $vnr = conv_i($params->{vendornumber});
854 ? "to_date(" . $dbh->quote($params->{invdate}) . ", '$dateformat')"
860 $where .= 'AND v.id = ?';
864 $where .= 'AND v.vendornumber = ?';
869 v.id AS vendor_id, v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes,
870 v.email, v.cc, v.bcc, v.language_id, v.payment_id,
871 v.street, v.zipcode, v.city, v.country, v.taxzone_id,
872 $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
873 b.description AS business
875 LEFT JOIN business b ON (b.id = v.business_id)
876 LEFT JOIN payment_terms pt ON (v.payment_id = pt.id)
878 $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
879 map { $params->{$_} = $ref->{$_} } keys %$ref;
881 $params->{creditremaining} = $params->{creditlimit};
883 $query = qq|SELECT SUM(amount - paid) FROM ap WHERE vendor_id = ?|;
884 my ($unpaid_invoices) = selectfirst_array_query($form, $dbh, $query, $vid);
885 $params->{creditremaining} -= $unpaid_invoices;
887 $query = qq|SELECT o.amount,
890 WHERE (e.curr = o.curr)
891 AND (e.transdate = o.transdate)) AS exch
893 WHERE (o.vendor_id = ?) AND (o.quotation = '0') AND (o.closed = '0')|;
894 my $sth = prepare_execute_query($form, $dbh, $query, $vid);
895 while (my ($amount, $exch) = $sth->fetchrow_array()) {
896 $exch = 1 unless $exch;
897 $params->{creditremaining} -= $amount * $exch;
901 # get shipto if we do not convert an order or invoice
902 if (!$params->{shipto}) {
903 delete @{$params}{qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail)};
905 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module= 'CT')|;
906 $ref = selectfirst_hashref_query($form, $dbh, $query, $vid);
907 @{$params}{keys %$ref} = @{$ref}{keys %$ref};
908 map { $params->{$_} = $ref->{$_} } keys %$ref;
911 if (!$params->{id} && $params->{type} !~ /_(order|quotation)/) {
912 # setup last accounts used
914 qq|SELECT c.id, c.accno, c.description, c.link, c.category
916 JOIN acc_trans ac ON (ac.chart_id = c.id)
917 JOIN ap a ON (a.id = ac.trans_id)
918 WHERE (a.vendor_id = ?)
919 AND (NOT ((c.link LIKE '%_tax%') OR (c.link LIKE '%_paid%')))
920 AND (a.id IN (SELECT max(a2.id) FROM ap a2 WHERE a2.vendor_id = ?))|;
921 my $refs = selectall_hashref_query($form, $dbh, $query, $vid, $vid);
925 if ($ref->{category} eq 'E') {
928 if ($params->{initial_transdate}) {
929 my $tax_query = qq|SELECT tk.tax_id, t.rate FROM taxkeys tk
930 LEFT JOIN tax t ON (tk.tax_id = t.id)
931 WHERE (tk.chart_id = ?) AND (startdate <= ?)
932 ORDER BY tk.startdate DESC
934 my ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id}, $params->{initial_transdate});
935 $params->{"taxchart_$i"} = "${tax_id}--${rate}";
938 $params->{"AP_amount_$i"} = "$ref->{accno}--$tax_id";
941 if ($ref->{category} eq 'L') {
942 $params->{APselected} = $params->{AP_1} = $ref->{accno};
945 $params->{rowcount} = $i if ($i && !$params->{type});
950 $main::lxdebug->leave_sub();
954 $main::lxdebug->enter_sub();
956 my ($self, $myconfig, $form) = @_;
958 # connect to database
959 my $dbh = $form->dbconnect($myconfig);
961 my $i = $form->{rowcount};
963 # don't include assemblies or obsolete parts
964 my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'";
967 foreach my $table_column (qw(p.partnumber p.description pg.partsgroup)) {
968 my $field = (split m{\.}, $table_column)[1];
969 next unless $form->{"${field}_${i}"};
970 $where .= " AND lower(${table_column}) LIKE lower(?)";
971 push @values, '%' . $form->{"${field}_${i}"} . '%';
973 #Es soll auch nach EAN gesucht werden, ohne Einschränkung durch Beschreibung
974 if ($form->{"partnumber_$i"} && !$form->{"description_$i"}) {
975 $where .= qq| OR (NOT p.obsolete = '1' AND p.ean = ? )|;
976 push @values, $form->{"partnumber_$i"};
979 if ($form->{"description_$i"}) {
980 $where .= " ORDER BY p.description";
982 $where .= " ORDER BY p.partnumber";
986 if ($form->{type} eq "invoice") {
987 $transdate = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
989 $transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
992 my $taxzone_id = $form->{taxzone_id} * 1;
993 $taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id));
997 p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice,
998 p.unit, p.assembly, p.bin, p.onhand, p.formel,
999 p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1000 p.inventory_accno_id, p.price_factor_id,
1002 pfac.factor AS price_factor,
1004 c1.accno AS inventory_accno,
1005 c1.new_chart_id AS inventory_new_chart,
1006 date($transdate) - c1.valid_from AS inventory_valid,
1008 c2.accno AS income_accno,
1009 c2.new_chart_id AS income_new_chart,
1010 date($transdate) - c2.valid_from AS income_valid,
1012 c3.accno AS expense_accno,
1013 c3.new_chart_id AS expense_new_chart,
1014 date($transdate) - c3.valid_from AS expense_valid,
1019 LEFT JOIN chart c1 ON
1020 ((SELECT inventory_accno_id
1021 FROM buchungsgruppen
1022 WHERE id = p.buchungsgruppen_id) = c1.id)
1023 LEFT JOIN chart c2 ON
1024 ((SELECT income_accno_id_${taxzone_id}
1025 FROM buchungsgruppen
1026 WHERE id = p.buchungsgruppen_id) = c2.id)
1027 LEFT JOIN chart c3 ON
1028 ((SELECT expense_accno_id_${taxzone_id}
1029 FROM buchungsgruppen
1030 WHERE id = p.buchungsgruppen_id) = c3.id)
1031 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1032 LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)
1034 my $sth = prepare_execute_query($form, $dbh, $query, @values);
1036 $form->{item_list} = [];
1037 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1039 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1040 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1041 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1042 if (!$ref->{inventory_accno_id}) {
1043 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1045 delete($ref->{inventory_accno_id});
1047 # get tax rates and description
1048 $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1050 qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1052 LEFT JOIN chart c on (c.id = t.chart_id)
1060 AND (startdate <= $transdate)
1061 ORDER BY startdate DESC
1064 my $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
1066 $ref->{taxaccounts} = "";
1068 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1070 # if ($customertax{$ref->{accno}}) {
1071 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1076 $ref->{taxaccounts} .= "$ptr->{accno} ";
1078 if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) {
1079 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1080 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1081 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1082 $form->{taxaccounts} .= "$ptr->{accno} ";
1088 chop $ref->{taxaccounts};
1090 $ref->{onhand} *= 1;
1092 push @{ $form->{item_list} }, $ref;
1099 $main::lxdebug->leave_sub();
1102 sub vendor_details {
1103 $main::lxdebug->enter_sub();
1105 my ($self, $myconfig, $form, @wanted_vars) = @_;
1107 # connect to database
1108 my $dbh = $form->dbconnect($myconfig);
1112 # get contact id, set it if nessessary
1113 $form->{cp_id} *= 1;
1115 if ($form->{cp_id}) {
1116 $contact = "AND cp.cp_id = ?";
1117 push @values, $form->{cp_id};
1120 # get rest for the vendor
1121 # fax and phone and email as vendor*
1123 qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail
1125 LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id)
1126 WHERE (ct.id = ?) $contact
1129 my $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id}, @values);
1131 # remove id and taxincluded before copy back
1132 delete @$ref{qw(id taxincluded)};
1134 @wanted_vars = grep({ $_ } @wanted_vars);
1135 if (scalar(@wanted_vars) > 0) {
1137 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
1138 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
1141 map { $form->{$_} = $ref->{$_} } keys %$ref;
1143 my $custom_variables = CVar->get_custom_variables('dbh' => $dbh,
1145 'trans_id' => $form->{vendor_id});
1146 map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
1150 $main::lxdebug->leave_sub();
1154 $main::lxdebug->enter_sub();
1156 my ($self, $myconfig, $form) = @_;
1158 # connect to database
1159 my $dbh = $form->dbconnect($myconfig);
1162 qq|SELECT accno, description, link
1164 WHERE link LIKE '%IC%'
1166 my $sth = prepare_execute_query($query, $dbh, $query);
1168 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1169 foreach my $key (split(/:/, $ref->{link})) {
1171 push @{ $form->{IC_links}{$key} },
1172 { accno => $ref->{accno},
1173 description => $ref->{description} };
1181 $main::lxdebug->leave_sub();
1184 sub _delete_payments {
1185 $main::lxdebug->enter_sub();
1187 my ($self, $form, $dbh) = @_;
1191 # Delete old payment entries from acc_trans.
1195 WHERE (trans_id = ?) AND fx_transaction
1201 LEFT JOIN chart c ON (at.chart_id = c.id)
1202 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
1203 push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
1208 LEFT JOIN chart c ON (at.chart_id = c.id)
1209 WHERE (trans_id = ?)
1210 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
1213 push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
1216 $query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
1217 do_query($form, $dbh, $query);
1220 $main::lxdebug->leave_sub();
1224 $main::lxdebug->enter_sub();
1226 my ($self, $myconfig, $form, $locale) = @_;
1228 # connect to database, turn off autocommit
1229 my $dbh = $form->dbconnect_noauto($myconfig);
1231 my (%payments, $old_form, $row, $item, $query, %keep_vars);
1233 $old_form = save_form();
1235 # Delete all entries in acc_trans from prior payments.
1236 $self->_delete_payments($form, $dbh);
1238 # Save the new payments the user made before cleaning up $form.
1239 map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AP_paid_\d+$|^paidaccounts$/, keys %{ $form };
1241 # Clean up $form so that old content won't tamper the results.
1242 %keep_vars = map { $_, 1 } qw(login password id);
1243 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
1245 # Retrieve the invoice from the database.
1246 $self->retrieve_invoice($myconfig, $form);
1248 # Set up the content of $form in the way that IR::post_invoice() expects.
1249 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
1251 for $row (1 .. scalar @{ $form->{invoice_details} }) {
1252 $item = $form->{invoice_details}->[$row - 1];
1254 map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice);
1256 map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
1259 $form->{rowcount} = scalar @{ $form->{invoice_details} };
1261 delete @{$form}{qw(invoice_details paidaccounts storno paid)};
1263 # Restore the payment options from the user input.
1264 map { $form->{$_} = $payments{$_} } keys %payments;
1266 # Get the AP accno (which is normally done by Form::create_links()).
1270 LEFT JOIN chart c ON (at.chart_id = c.id)
1271 WHERE (trans_id = ?)
1272 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
1276 ($form->{AP}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
1278 # Post the new payments.
1279 $self->post_invoice($myconfig, $form, $dbh, 1);
1281 restore_form($old_form);
1283 my $rc = $dbh->commit();
1286 $main::lxdebug->leave_sub();
1292 $main::lxdebug->enter_sub();
1297 if (!$params{vendor_id} || !$params{invdate}) {
1298 $main::lxdebug->leave_sub();
1299 return $params{default};
1302 my $myconfig = \%main::myconfig;
1303 my $form = $main::form;
1305 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1307 my $query = qq|SELECT ?::date + pt.terms_netto
1309 LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)
1312 my ($sth, $duedate);
1314 if (($sth = $dbh->prepare($query)) && $sth->execute($params{invdate}, conv_i($params{vendor_id}))) {
1315 ($duedate) = $sth->fetchrow_array();
1321 $duedate ||= $params{default};
1323 $main::lxdebug->leave_sub();