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', '$form->{employee_id}')|;
73 $dbh->do($query) || $form->dberror($query);
75 $query = qq|SELECT a.id FROM ap a
76 WHERE a.invnumber = '$uid'|;
77 $sth = $dbh->prepare($query);
78 $sth->execute || $form->dberror($query);
80 ($form->{id}) = $sth->fetchrow_array;
84 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber quonumber);
86 my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
87 my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
89 if ($form->{currency} eq $form->{defaultcurrency}) {
90 $form->{exchangerate} = 1;
93 $form->check_exchangerate($myconfig, $form->{currency},
94 $form->{transdate}, 'sell');
97 $form->{exchangerate} =
100 : $form->parse_amount($myconfig, $form->{exchangerate});
102 $form->{exchangerate} = 1 unless ($form->{exchangerate} * 1);
104 for my $i (1 .. $form->{rowcount}) {
105 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
107 if ($form->{storno}) {
108 $form->{"qty_$i"} *= -1;
112 $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"};
115 if ($form->{"id_$i"}) {
118 $query = qq|SELECT p.unit
120 WHERE p.id = $form->{"id_$i"}|;
121 $sth = $dbh->prepare($query);
122 $sth->execute || $form->dberror($query);
124 my ($item_unit) = $sth->fetchrow_array();
127 if ($form->{"inventory_accno_$i"}) {
128 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
129 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
133 $baseqty = $form->{"qty_$i"} * $basefactor;
135 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
136 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
140 $baseqty = $form->{"qty_$i"} * $basefactor;
143 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
144 qw(partnumber description unit);
146 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
151 $form->{"sellprice_$i"} =
152 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
153 my $fxsellprice = $form->{"sellprice_$i"};
155 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
157 my $decimalplaces = ($dec > 2) ? $dec : 2;
159 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
161 if ($form->{"inventory_accno_$i"}) {
164 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
166 if ($form->{taxincluded}) {
167 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
168 $form->{"sellprice_$i"} =
169 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
171 $taxamount = $linetotal * $taxrate;
174 $netamount += $linetotal;
176 if ($form->round_amount($taxrate, 7) == 0) {
177 if ($form->{taxincluded}) {
178 foreach $item (@taxaccounts) {
180 $form->round_amount($linetotal * $form->{"${item}_rate"} /
181 (1 + abs($form->{"${item}_rate"})),
183 $taxdiff += $taxamount;
184 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
186 $form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
189 $form->{amount}{ $form->{id} }{$_} -=
190 $linetotal * $form->{"${_}_rate"}
195 $form->{amount}{ $form->{id} }{$_} -=
196 $taxamount * $form->{"${_}_rate"} / $taxrate
200 # add purchase to inventory, this one is without the tax!
202 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
204 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
205 $form->{exchangerate};
206 $linetotal = $form->round_amount($linetotal, 2);
208 # this is the difference for the inventory
209 $invoicediff += ($amount - $linetotal);
211 $form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -=
214 # adjust and round sellprice
215 $form->{"sellprice_$i"} =
216 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
220 $query = qq|UPDATE parts SET
221 lastcost = $form->{"sellprice_$i"}
222 WHERE id = $form->{"id_$i"}|;
224 $dbh->do($query) || $form->dberror($query);
226 $form->update_balance($dbh, "parts", "onhand", qq|id = ?|,
227 $baseqty, $form->{"id_$i"})
228 unless $form->{shipped};
230 # check if we sold the item already and
231 # make an entry for the expense and inventory
232 $query = qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
233 p.inventory_accno_id, p.expense_accno_id, a.transdate
234 FROM invoice i, ar a, parts p
235 WHERE i.parts_id = p.id
236 AND i.parts_id = $form->{"id_$i"}
237 AND (i.base_qty + i.allocated) > 0
238 AND i.trans_id = a.id
240 $sth = $dbh->prepare($query);
241 $sth->execute || $form->dberror($query);
243 my $totalqty = $base_qty;
245 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
247 my $qty = $ref->{base_qty} + $ref->{allocated};
249 if (($qty - $totalqty) > 0) {
253 $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
255 if ($ref->{allocated} < 0) {
257 # we have an entry for it already, adjust amount
258 $form->update_balance(
262 qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$ref->{transdate}'|,
265 $form->update_balance(
269 qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$ref->{transdate}'|,
274 # add entry for inventory, this one is for the sold item
275 if ($linetotal != 0) {
276 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
278 VALUES ($ref->{trans_id}, $ref->{inventory_accno_id},
279 $linetotal, '$ref->{transdate}', (SELECT taxkey_id FROM chart WHERE id = $ref->{inventory_accno_id}))|;
280 $dbh->do($query) || $form->dberror($query);
283 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
285 VALUES ($ref->{trans_id}, $ref->{expense_accno_id},
286 | . ($linetotal * -1) . qq|, '$ref->{transdate}',
287 (SELECT taxkey from tax WHERE chart_id = $ref->{expense_accno_id}))|;
288 $dbh->do($query) || $form->dberror($query);
292 # update allocated for sold item
293 $form->update_balance($dbh, "invoice", "allocated",
299 last if (($totalqty -= $qty) <= 0);
304 $lastinventoryaccno = $form->{"inventory_accno_$i"};
309 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
311 if ($form->{taxincluded}) {
312 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
314 $form->{"sellprice_$i"} =
315 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
317 $taxamount = $linetotal * $taxrate;
320 $netamount += $linetotal;
322 if ($form->round_amount($taxrate, 7) == 0) {
323 if ($form->{taxincluded}) {
324 foreach $item (@taxaccounts) {
326 $linetotal * $form->{"${item}_rate"} /
327 (1 + abs($form->{"${item}_rate"}));
328 $totaltax += $taxamount;
329 $form->{amount}{ $form->{id} }{$item} -= $taxamount;
333 $form->{amount}{ $form->{id} }{$_} -=
334 $linetotal * $form->{"${_}_rate"}
339 $form->{amount}{ $form->{id} }{$_} -=
340 $taxamount * $form->{"${_}_rate"} / $taxrate
345 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
347 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
348 $form->{exchangerate};
349 $linetotal = $form->round_amount($linetotal, 2);
351 # this is the difference for expense
352 $expensediff += ($amount - $linetotal);
354 # add amount to expense
355 $form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -=
358 $lastexpenseaccno = $form->{"expense_accno_$i"};
360 # adjust and round sellprice
361 $form->{"sellprice_$i"} =
362 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
366 $query = qq|UPDATE parts SET
367 lastcost = $form->{"sellprice_$i"}
368 WHERE id = $form->{"id_$i"}|;
370 $dbh->do($query) || $form->dberror($query);
375 ($form->{"deliverydate_$i"})
376 ? qq|'$form->{"deliverydate_$i"}'|
379 # save detail record in invoice table
380 $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty, base_qty,
381 sellprice, fxsellprice, allocated, unit, deliverydate,
382 project_id, serialnumber)
383 VALUES ($form->{id}, $form->{"id_$i"},
384 '$form->{"description_$i"}', | . ($form->{"qty_$i"} * -1) . qq|, | . ($baseqty * -1) . qq|,
385 $form->{"sellprice_$i"}, $fxsellprice, $allocated,
386 '$form->{"unit_$i"}', $deliverydate, | . conv_i($form->{"project_id_$i"}, 'NULL') . qq|,
387 '$form->{"serialnumber_$i"}')|;
388 $dbh->do($query) || $form->dberror($query);
392 $project_id = conv_i($form->{"globalproject_id"});
394 $form->{datepaid} = $form->{invdate};
396 # all amounts are in natural state, netamount includes the taxes
397 # if tax is included, netamount is rounded to 2 decimal places,
401 for my $i (1 .. $form->{paidaccounts}) {
402 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
403 $form->{paid} += $form->{"paid_$i"};
404 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
407 my ($tax, $paiddiff) = (0, 0);
409 $netamount = $form->round_amount($netamount, 2);
411 # figure out rounding errors for amount paid and total amount
412 if ($form->{taxincluded}) {
414 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
415 $paiddiff = $amount - $netamount * $form->{exchangerate};
416 $netamount = $amount;
418 foreach $item (split / /, $form->{taxaccounts}) {
419 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
420 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
421 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
423 $netamount -= $amount;
426 $invoicediff += $paiddiff;
427 $expensediff += $paiddiff;
429 ######## this only applies to tax included
430 if ($lastinventoryaccno) {
431 $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff;
433 if ($lastexpenseaccno) {
434 $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff;
438 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
439 $paiddiff = $amount - $netamount * $form->{exchangerate};
440 $netamount = $amount;
441 foreach my $item (split / /, $form->{taxaccounts}) {
442 $form->{amount}{ $form->{id} }{$item} =
443 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
446 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1,
449 $amount - $form->{amount}{ $form->{id} }{$item} *
450 $form->{exchangerate} * -1;
451 $form->{amount}{ $form->{id} }{$item} =
452 $form->round_amount($amount * -1, 2);
453 $amount = $form->{amount}{ $form->{id} }{$item} * -1;
458 $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
460 if ($form->{paid} != 0) {
462 $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff,
466 # update exchangerate
467 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
468 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0,
469 $form->{exchangerate});
472 # record acc_trans transactions
473 foreach my $trans_id (keys %{ $form->{amount} }) {
474 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
476 ($form->{amount}{$trans_id}{$accno} =
477 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
480 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
481 transdate, taxkey, project_id)
482 VALUES ($trans_id, (SELECT c.id FROM chart c
483 WHERE c.accno = '$accno'),
484 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
485 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
486 do_query($form, $dbh, $query, $project_id);
491 # deduct payment differences from paiddiff
492 for my $i (1 .. $form->{paidaccounts}) {
493 if ($form->{"paid_$i"} != 0) {
495 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
496 $paiddiff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
500 # force AP entry if 0
501 $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid}
502 if ($form->{amount}{ $form->{id} }{ $form->{AP} } == 0);
504 # record payments and offsetting AP
505 for my $i (1 .. $form->{paidaccounts}) {
507 if ($form->{"paid_$i"} != 0) {
508 my ($accno) = split /--/, $form->{"AP_paid_$i"};
509 $form->{"datepaid_$i"} = $form->{invdate}
510 unless ($form->{"datepaid_$i"});
511 $form->{datepaid} = $form->{"datepaid_$i"};
515 $form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2
521 if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
522 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
523 transdate, taxkey, project_id)
524 VALUES ($form->{id}, (SELECT c.id FROM chart c
526 $amount, '$form->{"datepaid_$i"}',
527 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
528 do_query($form, $dbh, $query, $form->{AP}, $form->{AP}, $project_id);
533 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
534 source, memo, taxkey, project_id)
535 VALUES ($form->{id}, (SELECT c.id FROM chart c
537 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
538 '$form->{"source_$i"}', '$form->{"memo_$i"}',
539 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
540 do_query($form, $dbh, $query, $accno, $accno, $project_id);
544 if ($form->{currency} eq $form->{defaultcurrency}) {
545 $form->{"exchangerate_$i"} = 1;
548 $form->check_exchangerate($myconfig, $form->{currency},
549 $form->{"datepaid_$i"}, 'sell');
551 $form->{"exchangerate_$i"} =
554 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
557 # exchangerate difference
558 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
559 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff;
563 ($form->{"paid_$i"} * $form->{exchangerate}) -
564 ($form->{"paid_$i"} * $form->{"exchangerate_$i"});
566 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
569 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
575 # update exchange rate
576 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
577 $form->update_exchangerate($dbh, $form->{currency},
578 $form->{"datepaid_$i"},
579 0, $form->{"exchangerate_$i"});
584 # record exchange rate differences and gains/losses
585 foreach my $accno (keys %{ $form->{fx} }) {
586 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
588 ($form->{fx}{$accno}{$transdate} =
589 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
593 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
594 transdate, cleared, fx_transaction, taxkey, project_id)
595 VALUES ($form->{id}, (SELECT c.id FROM chart c
596 WHERE c.accno = '$accno'),
597 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1', 0, ?)|;
598 do_query($form, $dbh, $query, $project_id);
603 $amount = $netamount + $tax;
605 # set values which could be empty
606 $form->{taxincluded} *= 1;
607 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
608 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
610 ($null, $form->{department_id}) = split(/--/, $form->{department});
611 $form->{department_id} *= 1;
612 $form->{payment_id} *= 1;
613 $form->{language_id} *= 1;
614 $form->{taxzone_id} *= 1;
615 $form->{storno} *= 1;
618 $form->{invnumber} = $form->{id} unless $form->{invnumber};
621 $query = qq|UPDATE ap set
622 invnumber = '$form->{invnumber}',
623 ordnumber = '$form->{ordnumber}',
624 quonumber = '$form->{quonumber}',
625 transdate = '$form->{invdate}',
626 orddate = | . conv_dateq($form->{"orddate"}) . qq|,
627 quodate = | . conv_dateq($form->{"quodate"}) . qq|,
628 vendor_id = $form->{vendor_id},
630 netamount = $netamount,
631 paid = $form->{paid},
632 datepaid = $datepaid,
635 taxzone_id = '$form->{taxzone_id}',
636 taxincluded = '$form->{taxincluded}',
637 notes = '$form->{notes}',
638 intnotes = '$form->{intnotes}',
639 curr = '$form->{currency}',
640 department_id = $form->{department_id},
641 storno = '$form->{storno}',
642 globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
643 cp_id = | . conv_i($form->{cp_id}, 'NULL') . qq|
644 WHERE id = $form->{id}|;
645 $dbh->do($query) || $form->dberror($query);
647 if ($form->{storno}) {
648 $query = qq| UPDATE ap SET paid = paid+amount WHERE id = $form->{storno_id}|;
649 $dbh->do($query) || $form->dberror($query);
650 $query = qq| UPDATE ap SET storno = '$form->{storno}' WHERE id = $form->{storno_id}|;
651 $dbh->do($query) || $form->dberror($query);
652 $query = qq§ UPDATE ap SET intnotes = 'Rechnung storniert am $form->{invdate} ' || intnotes WHERE id = $form->{storno_id}§;
653 $dbh->do($query) || $form->dberror($query);
655 $query = qq| UPDATE ap SET paid = amount WHERE id = $form->{id}|;
656 $dbh->do($query) || $form->dberror($query);
661 $form->{name} = $form->{vendor};
662 $form->{name} =~ s/--$form->{vendor_id}//;
663 $form->add_shipto($dbh, $form->{id}, "AP");
665 # delete zero entries
666 $query = qq|DELETE FROM acc_trans
668 $dbh->do($query) || $form->dberror($query);
670 Common::webdav_folder($form) if ($main::webdav);
672 my $rc = $dbh->commit;
675 $main::lxdebug->leave_sub();
680 sub reverse_invoice {
681 $main::lxdebug->enter_sub();
683 my ($dbh, $form) = @_;
685 # reverse inventory items
686 my $query = qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id,
687 i.qty, i.allocated, i.sellprice
688 FROM invoice i, parts p
689 WHERE i.parts_id = p.id
690 AND i.trans_id = $form->{id}|;
691 my $sth = $dbh->prepare($query);
692 $sth->execute || $form->dberror($query);
696 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
697 $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
699 if ($ref->{inventory_accno_id}) {
702 $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|,
705 # if $ref->{allocated} > 0 than we sold that many items
706 if ($ref->{allocated} > 0) {
708 # get references for sold items
709 $query = qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
711 WHERE i.parts_id = $ref->{parts_id}
713 AND i.trans_id = a.id
714 ORDER BY transdate DESC|;
715 my $sth = $dbh->prepare($query);
716 $sth->execute || $form->dberror($query);
718 while (my $pthref = $sth->fetchrow_hashref(NAME_lc)) {
719 my $qty = $ref->{allocated};
720 if (($ref->{allocated} + $pthref->{allocated}) > 0) {
721 $qty = $pthref->{allocated} * -1;
724 my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
727 $form->update_balance($dbh, "invoice", "allocated",
728 qq|id = $pthref->{id}|, $qty);
730 $form->update_balance(
734 qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$pthref->{transdate}'|,
737 $form->update_balance(
741 qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$pthref->{transdate}'|,
744 last if (($ref->{allocated} -= $qty) <= 0);
753 $query = qq|DELETE FROM acc_trans
754 WHERE trans_id = $form->{id}|;
755 $dbh->do($query) || $form->dberror($query);
757 # delete invoice entries
758 $query = qq|DELETE FROM invoice
759 WHERE trans_id = $form->{id}|;
760 $dbh->do($query) || $form->dberror($query);
762 $query = qq|DELETE FROM shipto
763 WHERE trans_id = $form->{id} AND module = 'AP'|;
764 $dbh->do($query) || $form->dberror($query);
766 $main::lxdebug->leave_sub();
770 $main::lxdebug->enter_sub();
772 my ($self, $myconfig, $form) = @_;
774 # connect to database
775 my $dbh = $form->dbconnect_noauto($myconfig);
777 &reverse_invoice($dbh, $form);
779 # delete zero entries
780 my $query = qq|DELETE FROM acc_trans
782 $dbh->do($query) || $form->dberror($query);
785 my $query = qq|DELETE FROM ap
786 WHERE id = $form->{id}|;
787 $dbh->do($query) || $form->dberror($query);
789 my $rc = $dbh->commit;
792 $main::lxdebug->leave_sub();
797 sub retrieve_invoice {
798 $main::lxdebug->enter_sub();
800 my ($self, $myconfig, $form) = @_;
802 # connect to database
803 my $dbh = $form->dbconnect_noauto($myconfig);
809 # get default accounts and last invoice number
810 $query = qq|SELECT (SELECT c.accno FROM chart c
811 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
812 (SELECT c.accno FROM chart c
813 WHERE d.income_accno_id = c.id) AS income_accno,
814 (SELECT c.accno FROM chart c
815 WHERE d.expense_accno_id = c.id) AS expense_accno,
816 (SELECT c.accno FROM chart c
817 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
818 (SELECT c.accno FROM chart c
819 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
823 $query = qq|SELECT (SELECT c.accno FROM chart c
824 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
825 (SELECT c.accno FROM chart c
826 WHERE d.income_accno_id = c.id) AS income_accno,
827 (SELECT c.accno FROM chart c
828 WHERE d.expense_accno_id = c.id) AS expense_accno,
829 (SELECT c.accno FROM chart c
830 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
831 (SELECT c.accno FROM chart c
832 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
833 d.curr AS currencies,
834 current_date AS invdate
837 my $sth = $dbh->prepare($query);
838 $sth->execute || $form->dberror($query);
840 my $ref = $sth->fetchrow_hashref(NAME_lc);
841 map { $form->{$_} = $ref->{$_} } keys %$ref;
847 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate AS invdate, a.duedate,
848 a.orddate, a.quodate, a.globalproject_id,
849 a.ordnumber, a.quonumber, a.paid, a.taxincluded, a.notes, a.taxzone_id, a.storno, a.gldate,
850 a.intnotes, a.curr AS currency
852 WHERE a.id = $form->{id}|;
853 $sth = $dbh->prepare($query);
854 $sth->execute || $form->dberror($query);
856 $ref = $sth->fetchrow_hashref(NAME_lc);
857 map { $form->{$_} = $ref->{$_} } keys %$ref;
860 $form->{exchangerate} =
861 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
865 $query = qq|SELECT s.* FROM shipto s
866 WHERE s.trans_id = $form->{id} AND s.module = 'AP'|;
867 $sth = $dbh->prepare($query);
868 $sth->execute || $form->dberror($query);
870 $ref = $sth->fetchrow_hashref(NAME_lc);
872 map { $form->{$_} = $ref->{$_} } keys %$ref;
876 $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
878 if(!$form->{taxzone_id}) {
879 $form->{taxzone_id} = 0;
881 # retrieve individual items
882 $query = qq|SELECT c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
883 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
884 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
885 p.partnumber, i.description, i.qty, i.fxsellprice AS sellprice, p.inventory_accno_id AS part_inventory_accno_id,
886 i.parts_id AS id, i.unit, p.bin, i.deliverydate,
888 i.project_id, i.serialnumber,
891 JOIN parts p ON (i.parts_id = p.id)
892 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
893 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
894 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
895 LEFT JOIN project pr ON (i.project_id = pr.id)
896 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
897 WHERE i.trans_id = $form->{id}
899 $sth = $dbh->prepare($query);
900 $sth->execute || $form->dberror($query);
902 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
903 if (!$ref->{"part_inventory_accno_id"}) {
904 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
906 delete($ref->{"part_inventory_accno_id"});
908 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
909 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}|;
910 my $stw = $dbh->prepare($query);
911 $stw->execute || $form->dberror($query);
912 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
916 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
917 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}|;
918 my $stw = $dbh->prepare($query);
919 $stw->execute || $form->dberror($query);
920 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
924 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
925 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}|;
926 my $stw = $dbh->prepare($query);
927 $stw->execute || $form->dberror($query);
928 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
932 # get tax rates and description
934 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
935 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
936 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
937 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)
939 $stw = $dbh->prepare($query);
940 $stw->execute || $form->dberror($query);
941 $ref->{taxaccounts} = "";
943 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
945 # if ($customertax{$ref->{accno}}) {
946 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
950 $ref->{taxaccounts} .= "$ptr->{accno} ";
951 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
952 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
953 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
954 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
955 $form->{taxaccounts} .= "$ptr->{accno} ";
960 chop $ref->{taxaccounts};
961 push @{ $form->{invoice_details} }, $ref;
966 Common::webdav_folder($form) if ($main::webdav);
969 my $rc = $dbh->commit;
972 $main::lxdebug->leave_sub();
978 $main::lxdebug->enter_sub();
980 my ($self, $myconfig, $form) = @_;
982 # connect to database
983 my $dbh = $form->dbconnect($myconfig);
985 my $dateformat = $myconfig->{dateformat};
986 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
990 ? "to_date('$form->{invdate}', '$dateformat')"
993 $form->{vendor_id} *= 1;
996 my $query = qq|SELECT v.name AS vendor, v.creditlimit, v.terms, v.notes AS intnotes,
997 v.email, v.cc, v.bcc, v.language_id, v.payment_id,
998 v.street, v.zipcode, v.city, v.country, v.taxzone_id,
999 $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
1000 b.description AS business
1002 LEFT JOIN business b ON b.id = v.business_id
1003 LEFT JOIN payment_terms pt ON v.payment_id = pt.id
1005 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id});
1006 map { $form->{$_} = $ref->{$_} } keys %$ref;
1008 $form->{creditremaining} = $form->{creditlimit};
1009 $query = qq|SELECT SUM(a.amount - a.paid) FROM ap a WHERE a.vendor_id = ?|;
1010 my ($unpaid_invoices) = selectfirst_array_query($form, $dbh, $query, $form->{vendor_id});
1011 $form->{creditremaining} -= $unpaid_invoices;
1013 $query = qq|SELECT o.amount,
1014 (SELECT e.sell FROM exchangerate e
1015 WHERE e.curr = o.curr AND e.transdate = o.transdate) AS exch
1016 FROM oe o WHERE o.vendor_id = ?
1017 AND o.quotation = '0' AND o.closed = '0'|;
1018 my $sth = prepare_execute_query($form, $dbh, $query, $form->{vendor_id});
1019 while (my ($amount, $exch) = $sth->fetchrow_array()) {
1020 $exch = 1 unless $exch;
1021 $form->{creditremaining} -= $amount * $exch;
1025 # get shipto if we do not convert an order or invoice
1026 if (!$form->{shipto}) {
1027 map { delete $form->{$_} }
1028 qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1030 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module= 'CT'|;
1031 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id});
1032 map { $form->{$_} = $ref->{$_} } keys %$ref;
1035 # get taxes for vendor
1036 $query = qq|SELECT c.accno FROM chart c
1037 JOIN vendortax v ON (v.chart_id = c.id)
1038 WHERE v.vendor_id = ?|;
1040 $ref = selectall_hashref_query($form, $dbh, $query, $form->{vendor_id});
1041 map { $vendortax{ $_->{accno} } = 1 } @$ref;
1043 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1045 # setup last accounts used
1046 $query = qq|SELECT c.id, c.accno, c.description, c.link, c.category
1048 JOIN acc_trans ac ON (ac.chart_id = c.id)
1049 JOIN ap a ON (a.id = ac.trans_id)
1050 WHERE a.vendor_id = ?
1051 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1052 AND a.id IN (SELECT max(a2.id) FROM ap a2
1053 WHERE a2.vendor_id = ?)|;
1054 $refs = selectall_hashref_query($form, $dbh, $query, $form->{vendor_id}, $form->{vendor_id});
1058 if ($ref->{category} eq 'E') {
1061 if ($form->{initial_transdate}) {
1062 my $tax_query = qq|SELECT tk.tax_id, t.rate FROM taxkeys tk
1063 LEFT JOIN tax t ON tk.tax_id = t.id
1064 WHERE tk.chart_id = ? AND startdate <= ?
1065 ORDER BY tk.startdate DESC LIMIT 1|;
1066 my ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id},
1067 $form->{initial_transdate});
1068 $form->{"taxchart_$i"} = "${tax_id}--${rate}";
1071 $form->{"AP_amount_$i"} = "$ref->{accno}--$tax_id";
1073 if ($ref->{category} eq 'L') {
1074 $form->{APselected} = $form->{AP_1} = $ref->{accno};
1077 $form->{rowcount} = $i if ($i && !$form->{type});
1082 $main::lxdebug->leave_sub();
1086 $main::lxdebug->enter_sub();
1088 my ($self, $myconfig, $form) = @_;
1090 # connect to database
1091 my $dbh = $form->dbconnect($myconfig);
1093 my $i = $form->{rowcount};
1095 # don't include assemblies or obsolete parts
1096 my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'";
1098 if ($form->{"partnumber_$i"}) {
1099 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1100 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1103 if ($form->{"description_$i"}) {
1104 my $description = $form->like(lc $form->{"description_$i"});
1105 $where .= " AND lower(p.description) LIKE '$description'";
1108 if ($form->{"partsgroup_$i"}) {
1109 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1110 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1113 if ($form->{"description_$i"}) {
1114 $where .= " ORDER BY p.description";
1116 $where .= " ORDER BY p.partnumber";
1120 if ($form->{type} eq "invoice") {
1122 $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
1125 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
1128 my $query = qq|SELECT p.id, p.partnumber, p.description, p.lastcost AS sellprice,
1129 p.listprice, p.inventory_accno_id,
1130 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1131 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1132 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1133 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1134 pg.partsgroup, p.formel
1136 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1137 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1138 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1139 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1141 my $sth = $dbh->prepare($query);
1142 $sth->execute || $form->dberror($query);
1144 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1146 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1147 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1148 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1149 if (!$ref->{inventory_accno_id}) {
1150 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1152 delete($ref->{inventory_accno_id});
1154 # get tax rates and description
1156 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1157 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1158 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1159 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)
1161 $stw = $dbh->prepare($query);
1162 $stw->execute || $form->dberror($query);
1164 $ref->{taxaccounts} = "";
1166 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1168 # if ($customertax{$ref->{accno}}) {
1169 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1173 $ref->{taxaccounts} .= "$ptr->{accno} ";
1174 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1175 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1176 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1177 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1178 $form->{taxaccounts} .= "$ptr->{accno} ";
1184 chop $ref->{taxaccounts};
1186 push @{ $form->{item_list} }, $ref;
1193 $main::lxdebug->leave_sub();
1196 sub vendor_details {
1197 $main::lxdebug->enter_sub();
1199 my ($self, $myconfig, $form, @wanted_vars) = @_;
1201 # connect to database
1202 my $dbh = $form->dbconnect($myconfig);
1204 # get contact id, set it if nessessary
1205 ($null, $form->{cp_id}) = split /--/, $form->{contact};
1208 if ($form->{cp_id}) {
1209 $contact = "and cp.cp_id = $form->{cp_id}";
1212 # get rest for the vendor
1213 # fax and phone and email as vendor*
1215 qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail
1217 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
1218 WHERE ct.id = $form->{vendor_id} $contact order by cp.cp_id limit 1|;
1219 my $sth = $dbh->prepare($query);
1220 $sth->execute || $form->dberror($query);
1222 $ref = $sth->fetchrow_hashref(NAME_lc);
1224 # remove id and taxincluded before copy back
1225 delete @$ref{qw(id taxincluded)};
1227 @wanted_vars = grep({ $_ } @wanted_vars);
1228 if (scalar(@wanted_vars) > 0) {
1230 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
1231 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
1234 map { $form->{$_} = $ref->{$_} } keys %$ref;
1239 $main::lxdebug->leave_sub();
1243 $main::lxdebug->enter_sub();
1245 my ($self, $myconfig, $form) = @_;
1247 # connect to database
1248 my $dbh = $form->dbconnect($myconfig);
1250 my $query = qq|SELECT c.accno, c.description, c.link
1252 WHERE c.link LIKE '%IC%'
1254 my $sth = $dbh->prepare($query);
1255 $sth->execute || $form->dberror($query);
1257 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1258 foreach my $key (split(/:/, $ref->{link})) {
1260 push @{ $form->{IC_links}{$key} },
1261 { accno => $ref->{accno},
1262 description => $ref->{description} };
1268 $main::lxdebug->leave_sub();
1272 $main::lxdebug->enter_sub();
1274 my ($self, $myconfig, $form, $locale) = @_;
1276 # connect to database, turn off autocommit
1277 my $dbh = $form->dbconnect_noauto($myconfig);
1279 $form->{datepaid} = $form->{invdate};
1281 # total payments, don't move we need it here
1282 for my $i (1 .. $form->{paidaccounts}) {
1283 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
1284 $form->{paid} += $form->{"paid_$i"};
1285 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
1288 $form->{exchangerate} =
1289 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1292 my $project_id = conv_i($form->{"globalproject_id"});
1294 # record payments and offsetting AP
1295 for my $i (1 .. $form->{paidaccounts}) {
1297 if ($form->{"paid_$i"} != 0) {
1298 my ($accno) = split /--/, $form->{"AP_paid_$i"};
1299 $form->{"datepaid_$i"} = $form->{invdate}
1300 unless ($form->{"datepaid_$i"});
1301 $form->{datepaid} = $form->{"datepaid_$i"};
1304 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1305 $form->{"exchangerate_$i"} = 1;
1308 $form->check_exchangerate($myconfig, $form->{currency},
1309 $form->{"datepaid_$i"}, 'buy');
1311 $form->{"exchangerate_$i"} =
1314 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1319 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
1323 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1324 WHERE c.accno = '$form->{AP}') AND amount=$amount AND transdate='$form->{"datepaid_$i"}'|;
1325 $dbh->do($query) || $form->dberror($query);
1327 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1328 transdate, taxkey, project_id)
1329 VALUES ($form->{id}, (SELECT c.id FROM chart c
1331 $amount, '$form->{"datepaid_$i"}',
1332 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1333 do_query($form, $dbh, $query, $form->{AP}, $form->{AP}, $project_id);
1337 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1338 WHERE c.accno = '$accno') AND amount=$form->{"paid_$i"} AND transdate='$form->{"datepaid_$i"}' AND source='$form->{"source_$i"}' AND memo='$form->{"memo_$i"}'|;
1339 $dbh->do($query) || $form->dberror($query);
1341 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
1342 source, memo, taxkey, project_id)
1343 VALUES ($form->{id}, (SELECT c.id FROM chart c
1345 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
1346 '$form->{"source_$i"}', '$form->{"memo_$i"}',
1347 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1348 do_query($form, $dbh, $query, $accno, $accno, $project_id);
1353 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
1354 $form->{"exchangerate_$i"};
1356 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
1359 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
1365 # update exchange rate
1366 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1367 $form->update_exchangerate($dbh, $form->{currency},
1368 $form->{"datepaid_$i"},
1369 $form->{"exchangerate_$i"}, 0);
1374 # record exchange rate differences and gains/losses
1375 foreach my $accno (keys %{ $form->{fx} }) {
1376 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1378 ($form->{fx}{$accno}{$transdate} =
1379 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
1382 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1383 WHERE c.accno = '$accno') AND amount=$form->{fx}{$accno}{$transdate} AND transdate='$transdate' AND cleared='0' AND fx_transaction='1'|;
1384 $dbh->do($query) || $form->dberror($query);
1385 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1386 transdate, cleared, fx_transaction, taxkey, project_id)
1387 VALUES ($form->{id},
1388 (SELECT c.id FROM chart c
1390 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1',
1391 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
1392 do_query($form, $dbh, $query, $accno, $accno, $project_id);
1396 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
1399 my $query = qq|UPDATE ap set
1400 paid = $form->{paid},
1401 datepaid = $datepaid
1402 WHERE id=$form->{id}|;
1404 $dbh->do($query) || $form->dberror($query);
1406 my $rc = $dbh->commit;
1409 $main::lxdebug->leave_sub();