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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
46 # connect to database, turn off autocommit
47 my $dbh = $form->dbconnect_noauto($myconfig);
49 my ($query, $sth, $null, $project_id);
57 my $service_units = AM->retrieve_units($myconfig,$form,"service");
58 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
62 &reverse_invoice($dbh, $form);
65 my $uid = rand() . time;
67 $uid .= $form->{login};
69 $uid = substr($uid, 2, 75);
71 $query = qq|INSERT INTO ap (invnumber, employee_id)
72 VALUES ('$uid', (SELECT e.id FROM employee e
73 WHERE e.login = '$form->{login}'))|;
74 $dbh->do($query) || $form->dberror($query);
76 $query = qq|SELECT a.id FROM ap a
77 WHERE a.invnumber = '$uid'|;
78 $sth = $dbh->prepare($query);
79 $sth->execute || $form->dberror($query);
81 ($form->{id}) = $sth->fetchrow_array;
85 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber quonumber);
87 my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
88 my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
90 if ($form->{currency} eq $form->{defaultcurrency}) {
91 $form->{exchangerate} = 1;
94 $form->check_exchangerate($myconfig, $form->{currency},
95 $form->{transdate}, 'sell');
98 $form->{exchangerate} =
101 : $form->parse_amount($myconfig, $form->{exchangerate});
103 $form->{exchangerate} = 1 unless ($form->{exchangerate} * 1);
105 for my $i (1 .. $form->{rowcount}) {
106 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
108 if ($form->{storno}) {
109 $form->{"qty_$i"} *= -1;
113 $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"};
116 if ($form->{"id_$i"}) {
119 $query = qq|SELECT p.unit
121 WHERE p.id = $form->{"id_$i"}|;
122 $sth = $dbh->prepare($query);
123 $sth->execute || $form->dberror($query);
125 my ($item_unit) = $sth->fetchrow_array();
128 if ($form->{"inventory_accno_$i"}) {
129 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
130 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
134 $baseqty = $form->{"qty_$i"} * $basefactor;
136 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
137 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
141 $baseqty = $form->{"qty_$i"} * $basefactor;
144 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
145 qw(partnumber description unit);
147 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
152 $form->{"sellprice_$i"} =
153 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
154 my $fxsellprice = $form->{"sellprice_$i"};
156 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
158 my $decimalplaces = ($dec > 2) ? $dec : 2;
160 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
162 if ($form->{"inventory_accno_$i"}) {
165 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
167 if ($form->{taxincluded}) {
168 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
169 $form->{"sellprice_$i"} =
170 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
172 $taxamount = $linetotal * $taxrate;
175 $netamount += $linetotal;
177 if ($form->round_amount($taxrate, 7) == 0) {
178 if ($form->{taxincluded}) {
179 foreach $item (@taxaccounts) {
181 $form->round_amount($linetotal * $form->{"${item}_rate"} /
182 (1 + abs($form->{"${item}_rate"})),
184 $taxdiff += $taxamount;
185 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
187 $form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
190 $form->{amount}{ $form->{id} }{$_} -=
191 $linetotal * $form->{"${_}_rate"}
196 $form->{amount}{ $form->{id} }{$_} -=
197 $taxamount * $form->{"${_}_rate"} / $taxrate
201 # add purchase to inventory, this one is without the tax!
203 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
205 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
206 $form->{exchangerate};
207 $linetotal = $form->round_amount($linetotal, 2);
209 # this is the difference for the inventory
210 $invoicediff += ($amount - $linetotal);
212 $form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -=
215 # adjust and round sellprice
216 $form->{"sellprice_$i"} =
217 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
221 $query = qq|UPDATE parts SET
222 lastcost = $form->{"sellprice_$i"}
223 WHERE id = $form->{"id_$i"}|;
225 $dbh->do($query) || $form->dberror($query);
227 $form->update_balance($dbh, "parts", "onhand",
228 qq|id = $form->{"id_$i"}|,
230 unless $form->{shipped};
232 # check if we sold the item already and
233 # make an entry for the expense and inventory
234 $query = qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
235 p.inventory_accno_id, p.expense_accno_id, a.transdate
236 FROM invoice i, ar a, parts p
237 WHERE i.parts_id = p.id
238 AND i.parts_id = $form->{"id_$i"}
239 AND (i.base_qty + i.allocated) > 0
240 AND i.trans_id = a.id
242 $sth = $dbh->prepare($query);
243 $sth->execute || $form->dberror($query);
245 my $totalqty = $base_qty;
247 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
249 my $qty = $ref->{base_qty} + $ref->{allocated};
251 if (($qty - $totalqty) > 0) {
255 $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
257 if ($ref->{allocated} < 0) {
259 # we have an entry for it already, adjust amount
260 $form->update_balance(
264 qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$ref->{transdate}'|,
267 $form->update_balance(
271 qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$ref->{transdate}'|,
276 # add entry for inventory, this one is for the sold item
277 if ($linetotal != 0) {
278 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
280 VALUES ($ref->{trans_id}, $ref->{inventory_accno_id},
281 $linetotal, '$ref->{transdate}', (SELECT taxkey_id FROM chart WHERE id = $ref->{inventory_accno_id}))|;
282 $dbh->do($query) || $form->dberror($query);
285 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
287 VALUES ($ref->{trans_id}, $ref->{expense_accno_id},
288 | . ($linetotal * -1) . qq|, '$ref->{transdate}',
289 (SELECT taxkey from tax WHERE chart_id = $ref->{expense_accno_id}))|;
290 $dbh->do($query) || $form->dberror($query);
294 # update allocated for sold item
295 $form->update_balance($dbh, "invoice", "allocated",
301 last if (($totalqty -= $qty) <= 0);
306 $lastinventoryaccno = $form->{"inventory_accno_$i"};
311 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
313 if ($form->{taxincluded}) {
314 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
316 $form->{"sellprice_$i"} =
317 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
319 $taxamount = $linetotal * $taxrate;
322 $netamount += $linetotal;
324 if ($form->round_amount($taxrate, 7) == 0) {
325 if ($form->{taxincluded}) {
326 foreach $item (@taxaccounts) {
328 $linetotal * $form->{"${item}_rate"} /
329 (1 + abs($form->{"${item}_rate"}));
330 $totaltax += $taxamount;
331 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
335 $form->{amount}{ $form->{id} }{$_} -=
336 $linetotal * $form->{"${_}_rate"}
341 $form->{amount}{ $form->{id} }{$_} -=
342 $taxamount * $form->{"${_}_rate"} / $taxrate
347 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
349 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
350 $form->{exchangerate};
351 $linetotal = $form->round_amount($linetotal, 2);
353 # this is the difference for expense
354 $expensediff += ($amount - $linetotal);
356 # add amount to expense
357 $form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -=
360 $lastexpenseaccno = $form->{"expense_accno_$i"};
362 # adjust and round sellprice
363 $form->{"sellprice_$i"} =
364 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
368 $query = qq|UPDATE parts SET
369 lastcost = $form->{"sellprice_$i"}
370 WHERE id = $form->{"id_$i"}|;
372 $dbh->do($query) || $form->dberror($query);
377 ($form->{"deliverydate_$i"})
378 ? qq|'$form->{"deliverydate_$i"}'|
381 # save detail record in invoice table
382 $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty, base_qty,
383 sellprice, fxsellprice, allocated, unit, deliverydate,
384 project_id, serialnumber)
385 VALUES ($form->{id}, $form->{"id_$i"},
386 '$form->{"description_$i"}', | . ($form->{"qty_$i"} * -1) . qq|, | . ($baseqty * -1) . qq|,
387 $form->{"sellprice_$i"}, $fxsellprice, $allocated,
388 '$form->{"unit_$i"}', $deliverydate, | . conv_i($form->{"project_id_$i"}, 'NULL') . qq|,
389 '$form->{"serialnumber_$i"}')|;
390 $dbh->do($query) || $form->dberror($query);
394 $project_id = conv_i($form->{"globalproject_id"});
396 $form->{datepaid} = $form->{invdate};
398 # all amounts are in natural state, netamount includes the taxes
399 # if tax is included, netamount is rounded to 2 decimal places,
403 for my $i (1 .. $form->{paidaccounts}) {
404 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
405 $form->{paid} += $form->{"paid_$i"};
406 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
409 my ($tax, $paiddiff) = (0, 0);
411 $netamount = $form->round_amount($netamount, 2);
413 # figure out rounding errors for amount paid and total amount
414 if ($form->{taxincluded}) {
416 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
417 $paiddiff = $amount - $netamount * $form->{exchangerate};
418 $netamount = $amount;
420 foreach $item (split / /, $form->{taxaccounts}) {
421 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
422 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
423 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
425 $netamount -= $amount;
428 $invoicediff += $paiddiff;
429 $expensediff += $paiddiff;
431 ######## this only applies to tax included
432 if ($lastinventoryaccno) {
433 $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff;
435 if ($lastexpenseaccno) {
436 $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff;
440 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
441 $paiddiff = $amount - $netamount * $form->{exchangerate};
442 $netamount = $amount;
443 foreach my $item (split / /, $form->{taxaccounts}) {
444 $form->{amount}{ $form->{id} }{$item} =
445 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
448 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1,
451 $amount - $form->{amount}{ $form->{id} }{$item} *
452 $form->{exchangerate} * -1;
453 $form->{amount}{ $form->{id} }{$item} =
454 $form->round_amount($amount * -1, 2);
455 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
460 $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
462 if ($form->{paid} != 0) {
464 $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff,
468 # update exchangerate
469 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
470 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0,
471 $form->{exchangerate});
474 # record acc_trans transactions
475 foreach my $trans_id (keys %{ $form->{amount} }) {
476 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
478 ($form->{amount}{$trans_id}{$accno} =
479 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
482 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
483 transdate, taxkey, project_id)
484 VALUES ($trans_id, (SELECT c.id FROM chart c
485 WHERE c.accno = '$accno'),
486 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
487 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
488 do_query($form, $dbh, $query, $project_id);
493 # deduct payment differences from paiddiff
494 for my $i (1 .. $form->{paidaccounts}) {
495 if ($form->{"paid_$i"} != 0) {
497 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
498 $paiddiff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
502 # force AP entry if 0
503 $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid}
504 if ($form->{amount}{ $form->{id} }{ $form->{AP} } == 0);
506 # record payments and offsetting AP
507 for my $i (1 .. $form->{paidaccounts}) {
509 if ($form->{"paid_$i"} != 0) {
510 my ($accno) = split /--/, $form->{"AP_paid_$i"};
511 $form->{"datepaid_$i"} = $form->{invdate}
512 unless ($form->{"datepaid_$i"});
513 $form->{datepaid} = $form->{"datepaid_$i"};
517 $form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2
523 if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
524 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
525 transdate, taxkey, project_id)
526 VALUES ($form->{id}, (SELECT c.id FROM chart c
528 $amount, '$form->{"datepaid_$i"}',
529 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
530 do_query($form, $dbh, $query, $form->{AP}, $form->{AP}, $project_id);
535 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
536 source, memo, taxkey, project_id)
537 VALUES ($form->{id}, (SELECT c.id FROM chart c
539 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
540 '$form->{"source_$i"}', '$form->{"memo_$i"}',
541 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
542 do_query($form, $dbh, $query, $accno, $accno, $project_id);
546 if ($form->{currency} eq $form->{defaultcurrency}) {
547 $form->{"exchangerate_$i"} = 1;
550 $form->check_exchangerate($myconfig, $form->{currency},
551 $form->{"datepaid_$i"}, 'sell');
553 $form->{"exchangerate_$i"} =
556 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
559 # exchangerate difference
560 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
561 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff;
565 ($form->{"paid_$i"} * $form->{exchangerate}) -
566 ($form->{"paid_$i"} * $form->{"exchangerate_$i"});
568 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
571 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
577 # update exchange rate
578 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
579 $form->update_exchangerate($dbh, $form->{currency},
580 $form->{"datepaid_$i"},
581 0, $form->{"exchangerate_$i"});
586 # record exchange rate differences and gains/losses
587 foreach my $accno (keys %{ $form->{fx} }) {
588 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
590 ($form->{fx}{$accno}{$transdate} =
591 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
595 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
596 transdate, cleared, fx_transaction, taxkey, project_id)
597 VALUES ($form->{id}, (SELECT c.id FROM chart c
598 WHERE c.accno = '$accno'),
599 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1', 0, ?)|;
600 do_query($form, $dbh, $query, $project_id);
605 $amount = $netamount + $tax;
607 # set values which could be empty
608 $form->{taxincluded} *= 1;
609 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
610 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
612 ($null, $form->{department_id}) = split(/--/, $form->{department});
613 $form->{department_id} *= 1;
614 $form->{payment_id} *= 1;
615 $form->{language_id} *= 1;
616 $form->{taxzone_id} *= 1;
617 $form->{storno} *= 1;
620 $form->{invnumber} = $form->{id} unless $form->{invnumber};
623 $query = qq|UPDATE ap set
624 invnumber = '$form->{invnumber}',
625 ordnumber = '$form->{ordnumber}',
626 quonumber = '$form->{quonumber}',
627 transdate = '$form->{invdate}',
628 orddate = | . conv_dateq($form->{"orddate"}) . qq|,
629 quodate = | . conv_dateq($form->{"quodate"}) . qq|,
630 vendor_id = $form->{vendor_id},
632 netamount = $netamount,
633 paid = $form->{paid},
634 datepaid = $datepaid,
637 taxzone_id = '$form->{taxzone_id}',
638 taxincluded = '$form->{taxincluded}',
639 notes = '$form->{notes}',
640 intnotes = '$form->{intnotes}',
641 curr = '$form->{currency}',
642 department_id = $form->{department_id},
643 storno = '$form->{storno}',
644 globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
645 cp_id = | . conv_i($form->{cp_id}, 'NULL') . qq|
646 WHERE id = $form->{id}|;
647 $dbh->do($query) || $form->dberror($query);
649 if ($form->{storno}) {
650 $query = qq| update ap set paid=paid+amount where id=$form->{storno_id}|;
651 $dbh->do($query) || $form->dberror($query);
652 $query = qq| update ap set storno='$form->{storno}' where id=$form->{storno_id}|;
653 $dbh->do($query) || $form->dberror($query);
654 $query = qq§ update ap set intnotes='Rechnung storniert am $form->{invdate} ' || intnotes where id=$form->{storno_id}§;
655 $dbh->do($query) || $form->dberror($query);
657 $query = qq| update ap set paid=amount where id=$form->{id}|;
658 $dbh->do($query) || $form->dberror($query);
663 $form->{name} = $form->{vendor};
664 $form->{name} =~ s/--$form->{vendor_id}//;
665 $form->add_shipto($dbh, $form->{id}, "AP");
667 # delete zero entries
668 $query = qq|DELETE FROM acc_trans
670 $dbh->do($query) || $form->dberror($query);
672 Common::webdav_folder($form) if ($main::webdav);
674 my $rc = $dbh->commit;
677 $main::lxdebug->leave_sub();
682 sub reverse_invoice {
683 $main::lxdebug->enter_sub();
685 my ($dbh, $form) = @_;
687 # reverse inventory items
688 my $query = qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id,
689 i.qty, i.allocated, i.sellprice
690 FROM invoice i, parts p
691 WHERE i.parts_id = p.id
692 AND i.trans_id = $form->{id}|;
693 my $sth = $dbh->prepare($query);
694 $sth->execute || $form->dberror($query);
698 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
699 $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
701 if ($ref->{inventory_accno_id}) {
704 $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|,
707 # if $ref->{allocated} > 0 than we sold that many items
708 if ($ref->{allocated} > 0) {
710 # get references for sold items
711 $query = qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
713 WHERE i.parts_id = $ref->{parts_id}
715 AND i.trans_id = a.id
716 ORDER BY transdate DESC|;
717 my $sth = $dbh->prepare($query);
718 $sth->execute || $form->dberror($query);
720 while (my $pthref = $sth->fetchrow_hashref(NAME_lc)) {
721 my $qty = $ref->{allocated};
722 if (($ref->{allocated} + $pthref->{allocated}) > 0) {
723 $qty = $pthref->{allocated} * -1;
726 my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
729 $form->update_balance($dbh, "invoice", "allocated",
730 qq|id = $pthref->{id}|, $qty);
732 $form->update_balance(
736 qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$pthref->{transdate}'|,
739 $form->update_balance(
743 qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$pthref->{transdate}'|,
746 last if (($ref->{allocated} -= $qty) <= 0);
755 $query = qq|DELETE FROM acc_trans
756 WHERE trans_id = $form->{id}|;
757 $dbh->do($query) || $form->dberror($query);
759 # delete invoice entries
760 $query = qq|DELETE FROM invoice
761 WHERE trans_id = $form->{id}|;
762 $dbh->do($query) || $form->dberror($query);
764 $query = qq|DELETE FROM shipto
765 WHERE trans_id = $form->{id} AND module = 'AP'|;
766 $dbh->do($query) || $form->dberror($query);
768 $main::lxdebug->leave_sub();
772 $main::lxdebug->enter_sub();
774 my ($self, $myconfig, $form) = @_;
776 # connect to database
777 my $dbh = $form->dbconnect_noauto($myconfig);
779 &reverse_invoice($dbh, $form);
781 # delete zero entries
782 my $query = qq|DELETE FROM acc_trans
784 $dbh->do($query) || $form->dberror($query);
787 my $query = qq|DELETE FROM ap
788 WHERE id = $form->{id}|;
789 $dbh->do($query) || $form->dberror($query);
791 my $rc = $dbh->commit;
794 $main::lxdebug->leave_sub();
799 sub retrieve_invoice {
800 $main::lxdebug->enter_sub();
802 my ($self, $myconfig, $form) = @_;
804 # connect to database
805 my $dbh = $form->dbconnect_noauto($myconfig);
811 # get default accounts and last invoice number
812 $query = qq|SELECT (SELECT c.accno FROM chart c
813 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
814 (SELECT c.accno FROM chart c
815 WHERE d.income_accno_id = c.id) AS income_accno,
816 (SELECT c.accno FROM chart c
817 WHERE d.expense_accno_id = c.id) AS expense_accno,
818 (SELECT c.accno FROM chart c
819 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
820 (SELECT c.accno FROM chart c
821 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
825 $query = qq|SELECT (SELECT c.accno FROM chart c
826 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
827 (SELECT c.accno FROM chart c
828 WHERE d.income_accno_id = c.id) AS income_accno,
829 (SELECT c.accno FROM chart c
830 WHERE d.expense_accno_id = c.id) AS expense_accno,
831 (SELECT c.accno FROM chart c
832 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
833 (SELECT c.accno FROM chart c
834 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
835 d.curr AS currencies,
836 current_date AS invdate
839 my $sth = $dbh->prepare($query);
840 $sth->execute || $form->dberror($query);
842 my $ref = $sth->fetchrow_hashref(NAME_lc);
843 map { $form->{$_} = $ref->{$_} } keys %$ref;
849 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate AS invdate, a.duedate,
850 a.orddate, a.quodate, a.globalproject_id,
851 a.ordnumber, a.quonumber, a.paid, a.taxincluded, a.notes, a.taxzone_id, a.storno, a.gldate,
852 a.intnotes, a.curr AS currency
854 WHERE a.id = $form->{id}|;
855 $sth = $dbh->prepare($query);
856 $sth->execute || $form->dberror($query);
858 $ref = $sth->fetchrow_hashref(NAME_lc);
859 map { $form->{$_} = $ref->{$_} } keys %$ref;
862 $form->{exchangerate} =
863 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
867 $query = qq|SELECT s.* FROM shipto s
868 WHERE s.trans_id = $form->{id} AND s.module = 'AP'|;
869 $sth = $dbh->prepare($query);
870 $sth->execute || $form->dberror($query);
872 $ref = $sth->fetchrow_hashref(NAME_lc);
874 map { $form->{$_} = $ref->{$_} } keys %$ref;
878 $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
880 if(!$form->{taxzone_id}) {
881 $form->{taxzone_id} = 0;
883 # retrieve individual items
884 $query = qq|SELECT c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
885 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
886 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
887 p.partnumber, i.description, i.qty, i.fxsellprice AS sellprice, p.inventory_accno_id AS part_inventory_accno_id,
888 i.parts_id AS id, i.unit, p.bin, i.deliverydate,
890 i.project_id, i.serialnumber,
893 JOIN parts p ON (i.parts_id = p.id)
894 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
895 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
896 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
897 LEFT JOIN project pr ON (i.project_id = pr.id)
898 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
899 WHERE i.trans_id = $form->{id}
901 $sth = $dbh->prepare($query);
902 $sth->execute || $form->dberror($query);
904 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
905 if (!$ref->{"part_inventory_accno_id"}) {
906 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
908 delete($ref->{"part_inventory_accno_id"});
910 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
911 my $query = qq| SELECT accno AS inventory_accno, new_chart_id AS inventory_new_chart, date($transdate) - valid_from AS inventory_valid FROM chart WHERE id = $ref->{inventory_new_chart}|;
912 my $stw = $dbh->prepare($query);
913 $stw->execute || $form->dberror($query);
914 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
918 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
919 my $query = qq| SELECT accno AS income_accno, new_chart_id AS income_new_chart, date($transdate) - valid_from AS income_valid FROM chart WHERE id = $ref->{income_new_chart}|;
920 my $stw = $dbh->prepare($query);
921 $stw->execute || $form->dberror($query);
922 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
926 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
927 my $query = qq| SELECT accno AS expense_accno, new_chart_id AS expense_new_chart, date($transdate) - valid_from AS expense_valid FROM chart WHERE id = $ref->{expense_new_chart}|;
928 my $stw = $dbh->prepare($query);
929 $stw->execute || $form->dberror($query);
930 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
934 # get tax rates and description
936 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
937 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
938 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
939 WHERE t.id in (SELECT tk.tax_id from taxkeys tk where tk.chart_id = (SELECT id from chart WHERE accno='$accno_id') AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
941 $stw = $dbh->prepare($query);
942 $stw->execute || $form->dberror($query);
943 $ref->{taxaccounts} = "";
945 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
947 # if ($customertax{$ref->{accno}}) {
948 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
952 $ref->{taxaccounts} .= "$ptr->{accno} ";
953 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
954 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
955 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
956 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
957 $form->{taxaccounts} .= "$ptr->{accno} ";
962 chop $ref->{taxaccounts};
963 push @{ $form->{invoice_details} }, $ref;
968 Common::webdav_folder($form) if ($main::webdav);
971 my $rc = $dbh->commit;
974 $main::lxdebug->leave_sub();
980 $main::lxdebug->enter_sub();
982 my ($self, $myconfig, $form) = @_;
984 # connect to database
985 my $dbh = $form->dbconnect($myconfig);
987 my $dateformat = $myconfig->{dateformat};
988 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
992 ? "to_date('$form->{invdate}', '$dateformat')"
995 $form->{vendor_id} *= 1;
998 my $query = qq|SELECT v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes,
999 v.email, v.cc, v.bcc, v.language_id, v.payment_id,
1000 v.street, v.zipcode, v.city, v.country, v.taxzone_id,
1001 $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
1002 b.description AS business
1004 LEFT JOIN business b ON b.id = v.business_id
1005 LEFT JOIN payment_terms pt ON v.payment_id = pt.id
1007 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id});
1008 map { $form->{$_} = $ref->{$_} } keys %$ref;
1010 $form->{creditremaining} = $form->{creditlimit};
1011 $query = qq|SELECT SUM(a.amount - a.paid) FROM ap a WHERE a.vendor_id = ?|;
1012 ($form->{creditremaining}) -= selectfirst_array_query($form, $dbh, $query, $form->{vendor_id});
1014 $query = qq|SELECT o.amount,
1015 (SELECT e.sell FROM exchangerate e
1016 WHERE e.curr = o.curr AND e.transdate = o.transdate) AS exch
1017 FROM oe o WHERE o.vendor_id = ?
1018 AND o.quotation = '0' AND o.closed = '0'|;
1019 my $sth = prepare_execute_query($form, $dbh, $query, $form->{vendor_id});
1020 while (my ($amount, $exch) = $sth->fetchrow_array()) {
1021 $exch = 1 unless $exch;
1022 $form->{creditremaining} -= $amount * $exch;
1026 # get shipto if we do not convert an order or invoice
1027 if (!$form->{shipto}) {
1028 map { delete $form->{$_} }
1029 qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1031 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module= 'CT'|;
1032 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id});
1033 map { $form->{$_} = $ref->{$_} } keys %$ref;
1036 # get taxes for vendor
1037 $query = qq|SELECT c.accno FROM chart c
1038 JOIN vendortax v ON (v.chart_id = c.id)
1039 WHERE v.vendor_id = ?|;
1041 $ref = selectall_hashref_query($form, $dbh, $query, $form->{vendor_id});
1042 map { $vendortax{ $_->{accno} } = 1 } @$ref;
1044 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1046 # setup last accounts used
1047 $query = qq|SELECT c.id, c.accno, c.description, c.link, c.category
1049 JOIN acc_trans ac ON (ac.chart_id = c.id)
1050 JOIN ap a ON (a.id = ac.trans_id)
1051 WHERE a.vendor_id = ?
1052 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1053 AND a.id IN (SELECT max(a2.id) FROM ap a2
1054 WHERE a2.vendor_id = ?)|;
1055 $refs = selectall_hashref_query($form, $dbh, $query, $form->{vendor_id}, $form->{vendor_id});
1059 if ($ref->{category} eq 'E') {
1062 if ($form->{initial_transdate}) {
1063 my $tax_query = qq|SELECT tk.tax_id, t.rate FROM taxkeys tk
1064 LEFT JOIN tax t ON tk.tax_id = t.id
1065 WHERE tk.chart_id = ? AND startdate <= ?
1066 ORDER BY tk.startdate DESC LIMIT 1|;
1067 my ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id},
1068 $form->{initial_transdate});
1069 $form->{"taxchart_$i"} = "${tax_id}--${rate}";
1072 $form->{"AP_amount_$i"} = "$ref->{accno}--$tax_id";
1074 if ($ref->{category} eq 'L') {
1075 $form->{APselected} = $form->{AP_1} = $ref->{accno};
1078 $form->{rowcount} = $i if ($i && !$form->{type});
1083 $main::lxdebug->leave_sub();
1087 $main::lxdebug->enter_sub();
1089 my ($self, $myconfig, $form) = @_;
1091 # connect to database
1092 my $dbh = $form->dbconnect($myconfig);
1094 my $i = $form->{rowcount};
1096 # don't include assemblies or obsolete parts
1097 my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'";
1099 if ($form->{"partnumber_$i"}) {
1100 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1101 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1104 if ($form->{"description_$i"}) {
1105 my $description = $form->like(lc $form->{"description_$i"});
1106 $where .= " AND lower(p.description) LIKE '$description'";
1109 if ($form->{"partsgroup_$i"}) {
1110 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1111 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1114 if ($form->{"description_$i"}) {
1115 $where .= " ORDER BY p.description";
1117 $where .= " ORDER BY p.partnumber";
1121 if ($form->{type} eq "invoice") {
1123 $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
1126 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
1129 my $query = qq|SELECT p.id, p.partnumber, p.description, p.lastcost AS sellprice,
1130 p.listprice, p.inventory_accno_id,
1131 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1132 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1133 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1134 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1135 pg.partsgroup, p.formel
1137 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1138 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1139 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1140 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1142 my $sth = $dbh->prepare($query);
1143 $sth->execute || $form->dberror($query);
1145 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1147 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1148 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1149 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1150 if (!$ref->{inventory_accno_id}) {
1151 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1153 delete($ref->{inventory_accno_id});
1155 # get tax rates and description
1157 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1158 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1159 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1160 WHERE t.id in (SELECT tk.tax_id from taxkeys tk where tk.chart_id = (SELECT id from chart WHERE accno='$accno_id') AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
1162 $stw = $dbh->prepare($query);
1163 $stw->execute || $form->dberror($query);
1165 $ref->{taxaccounts} = "";
1167 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1169 # if ($customertax{$ref->{accno}}) {
1170 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1174 $ref->{taxaccounts} .= "$ptr->{accno} ";
1175 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1176 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1177 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1178 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1179 $form->{taxaccounts} .= "$ptr->{accno} ";
1185 chop $ref->{taxaccounts};
1187 push @{ $form->{item_list} }, $ref;
1194 $main::lxdebug->leave_sub();
1197 sub vendor_details {
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig, $form, @wanted_vars) = @_;
1202 # connect to database
1203 my $dbh = $form->dbconnect($myconfig);
1205 # get contact id, set it if nessessary
1206 ($null, $form->{cp_id}) = split /--/, $form->{contact};
1209 if ($form->{cp_id}) {
1210 $contact = "and cp.cp_id = $form->{cp_id}";
1213 # get rest for the vendor
1214 # fax and phone and email as vendor*
1216 qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail
1218 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
1219 WHERE ct.id = $form->{vendor_id} $contact order by cp.cp_id limit 1|;
1220 my $sth = $dbh->prepare($query);
1221 $sth->execute || $form->dberror($query);
1223 $ref = $sth->fetchrow_hashref(NAME_lc);
1225 # remove id and taxincluded before copy back
1226 delete @$ref{qw(id taxincluded)};
1228 @wanted_vars = grep({ $_ } @wanted_vars);
1229 if (scalar(@wanted_vars) > 0) {
1231 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
1232 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
1235 map { $form->{$_} = $ref->{$_} } keys %$ref;
1240 $main::lxdebug->leave_sub();
1244 $main::lxdebug->enter_sub();
1246 my ($self, $myconfig, $form) = @_;
1248 # connect to database
1249 my $dbh = $form->dbconnect($myconfig);
1251 my $query = qq|SELECT c.accno, c.description, c.link
1253 WHERE c.link LIKE '%IC%'
1255 my $sth = $dbh->prepare($query);
1256 $sth->execute || $form->dberror($query);
1258 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1259 foreach my $key (split(/:/, $ref->{link})) {
1261 push @{ $form->{IC_links}{$key} },
1262 { accno => $ref->{accno},
1263 description => $ref->{description} };
1269 $main::lxdebug->leave_sub();
1273 $main::lxdebug->enter_sub();
1275 my ($self, $myconfig, $form, $locale) = @_;
1277 # connect to database, turn off autocommit
1278 my $dbh = $form->dbconnect_noauto($myconfig);
1280 $form->{datepaid} = $form->{invdate};
1282 # total payments, don't move we need it here
1283 for my $i (1 .. $form->{paidaccounts}) {
1284 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
1285 $form->{paid} += $form->{"paid_$i"};
1286 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
1289 $form->{exchangerate} =
1290 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1293 my $project_id = conv_i($form->{"globalproject_id"});
1295 # record payments and offsetting AP
1296 for my $i (1 .. $form->{paidaccounts}) {
1298 if ($form->{"paid_$i"} != 0) {
1299 my ($accno) = split /--/, $form->{"AP_paid_$i"};
1300 $form->{"datepaid_$i"} = $form->{invdate}
1301 unless ($form->{"datepaid_$i"});
1302 $form->{datepaid} = $form->{"datepaid_$i"};
1305 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1306 $form->{"exchangerate_$i"} = 1;
1309 $form->check_exchangerate($myconfig, $form->{currency},
1310 $form->{"datepaid_$i"}, 'buy');
1312 $form->{"exchangerate_$i"} =
1315 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1320 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
1324 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1325 WHERE c.accno = '$form->{AP}') AND amount=$amount AND transdate='$form->{"datepaid_$i"}'|;
1326 $dbh->do($query) || $form->dberror($query);
1328 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1329 transdate, taxkey, project_id)
1330 VALUES ($form->{id}, (SELECT c.id FROM chart c
1332 $amount, '$form->{"datepaid_$i"}',
1333 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1334 do_query($form, $dbh, $query, $form->{AP}, $form->{AP}, $project_id);
1338 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1339 WHERE c.accno = '$accno') AND amount=$form->{"paid_$i"} AND transdate='$form->{"datepaid_$i"}' AND source='$form->{"source_$i"}' AND memo='$form->{"memo_$i"}'|;
1340 $dbh->do($query) || $form->dberror($query);
1342 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
1343 source, memo, taxkey, project_id)
1344 VALUES ($form->{id}, (SELECT c.id FROM chart c
1346 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
1347 '$form->{"source_$i"}', '$form->{"memo_$i"}',
1348 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1349 do_query($form, $dbh, $query, $accno, $accno, $project_id);
1354 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
1355 $form->{"exchangerate_$i"};
1357 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
1360 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
1366 # update exchange rate
1367 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1368 $form->update_exchangerate($dbh, $form->{currency},
1369 $form->{"datepaid_$i"},
1370 $form->{"exchangerate_$i"}, 0);
1375 # record exchange rate differences and gains/losses
1376 foreach my $accno (keys %{ $form->{fx} }) {
1377 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1379 ($form->{fx}{$accno}{$transdate} =
1380 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
1383 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1384 WHERE c.accno = '$accno') AND amount=$form->{fx}{$accno}{$transdate} AND transdate='$transdate' AND cleared='0' AND fx_transaction='1'|;
1385 $dbh->do($query) || $form->dberror($query);
1386 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1387 transdate, cleared, fx_transaction, taxkey, project_id)
1388 VALUES ($form->{id},
1389 (SELECT c.id FROM chart c
1391 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1',
1392 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1393 do_query($form, $dbh, $query, $accno, $accno, $project_id);
1397 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
1400 my $query = qq|UPDATE ap set
1401 paid = $form->{paid},
1402 datepaid = $datepaid
1403 WHERE id=$form->{id}|;
1405 $dbh->do($query) || $form->dberror($query);
1407 my $rc = $dbh->commit;
1410 $main::lxdebug->leave_sub();