1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
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 invoicing module
33 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($self, $myconfig, $form, $locale) = @_;
47 $form->{duedate} = $form->{invdate} unless ($form->{duedate});
50 my $dbh = $form->dbconnect($myconfig);
52 my $query = qq|SELECT date '$form->{duedate}' - date '$form->{invdate}'
55 my $sth = $dbh->prepare($query);
56 $sth->execute || $form->dberror($query);
58 ($form->{terms}) = $sth->fetchrow_array;
61 my (@project_ids, %projectnumbers);
63 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
65 # sort items by partsgroup
66 for $i (1 .. $form->{rowcount}) {
68 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
69 $partsgroup = $form->{"partsgroup_$i"};
71 push @partsgroup, [$i, $partsgroup];
72 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
76 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
77 join(", ", map({ "?" } @project_ids)) . ")";
78 $sth = $dbh->prepare($query);
79 $sth->execute(@project_ids) ||
80 $form->dberror($query . " (" . join(", ", @project_ids) . ")");
81 while (my $ref = $sth->fetchrow_hashref()) {
82 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
87 $form->{"globalprojectnumber"} =
88 $projectnumbers{$form->{"globalproject_id"}};
95 my %oid = ('Pg' => 'oid',
98 # sort items by partsgroup
99 for $i (1 .. $form->{rowcount}) {
101 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
102 $partsgroup = $form->{"partsgroup_$i"};
104 push @partsgroup, [$i, $partsgroup];
117 my $nodiscount_subtotal = 0;
118 my $discount_subtotal = 0;
120 my $subtotal_header = 0;
124 qw(runningnumber number description longdescription qty ship unit bin
125 deliverydate_oe ordnumber_oe transdate_oe licensenumber validuntil
126 partnotes serialnumber reqdate sellprice listprice netprice
127 discount p_discount discount_sub nodiscount_sub
128 linetotal nodiscount_linetotal tax_rate projectnumber);
131 qw(taxbase tax taxdescription taxrate taxnumber);
133 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
136 if ($item->[1] ne $sameitem) {
137 push(@{ $form->{description} }, qq|$item->[1]|);
138 $sameitem = $item->[1];
140 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
143 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
145 if ($form->{"id_$i"} != 0) {
147 # add number, description and qty to $form->{number},
148 if ($form->{"subtotal_$i"} && !$subtotal_header) {
149 $subtotal_header = $i;
150 $position = int($position);
153 } elsif ($subtotal_header) {
155 $position = int($position);
156 $position = $position.".".$subposition;
158 $position = int($position);
161 push(@{ $form->{runningnumber} }, $position);
162 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
163 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
164 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
165 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
166 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
167 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
168 push(@{ $form->{qty} },
169 $form->format_amount($myconfig, $form->{"qty_$i"}));
170 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
171 push(@{ $form->{deliverydate_oe} }, qq|$form->{"deliverydate_$i"}|);
173 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
174 push(@{ $form->{ordnumber_oe} }, qq|$form->{"ordnumber_$i"}|);
175 push(@{ $form->{transdate_oe} }, qq|$form->{"transdate_$i"}|);
176 push(@{ $form->{invnumber} }, qq|$form->{"invnumber"}|);
177 push(@{ $form->{invdate} }, qq|$form->{"invdate"}|);
179 if ($form->{lizenzen}) {
180 if ($form->{"licensenumber_$i"}) {
182 qq|SELECT l.licensenumber, l.validuntil FROM license l WHERE l.id = $form->{"licensenumber_$i"}|;
183 $sth = $dbh->prepare($query);
184 $sth->execute || $form->dberror($query);
186 ($licensenumber, $validuntil) = $sth->fetchrow_array;
187 push(@{ $form->{licensenumber} }, $licensenumber);
188 push(@{ $form->{validuntil} },
189 $locale->date($myconfig, $validuntil, 0));
192 push(@{ $form->{licensenumber} }, "");
193 push(@{ $form->{validuntil} }, "");
198 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
200 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
201 my ($dec) = ($sellprice =~ /\.(\d+)/);
203 my $decimalplaces = ($dec > 2) ? $dec : 2;
207 $sellprice * $form->parse_amount($myconfig,
208 $form->{"discount_$i"}) / 100,
212 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
214 # keep a netprice as well, (sellprice - discount)
215 $form->{"netprice_$i"} = $sellprice - $i_discount;
217 push(@{ $form->{netprice} },
218 ($form->{"netprice_$i"} != 0)
219 ? $form->format_amount(
220 $myconfig, $form->{"netprice_$i"},
226 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
228 my $nodiscount_linetotal =
229 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
233 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
235 $linetotal = ($linetotal != 0) ? $linetotal : " ";
237 push(@{ $form->{discount} }, $discount);
238 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
239 if (($form->{"discount_$i"} ne "") && ($form->{"discount_$i"} != 0)) {
240 $form->{discount_p} = $form->{"discount_$i"};
242 $form->{total} += $linetotal;
243 $discount_subtotal += $linetotal;
244 $form->{nodiscount_total} += $nodiscount_linetotal;
245 $nodiscount_subtotal += $nodiscount_linetotal;
246 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
248 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
249 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
250 push(@{ $form->{discount_sub} }, $discount_subtotal);
251 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
252 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
253 $discount_subtotal = 0;
254 $nodiscount_subtotal = 0;
255 $subtotal_header = 0;
257 push(@{ $form->{discount_sub} }, "");
258 push(@{ $form->{nodiscount_sub} }, "");
261 if ($linetotal == $netto_linetotal) {
262 $nodiscount += $linetotal;
265 push(@{ $form->{linetotal} },
266 $form->format_amount($myconfig, $linetotal, 2));
267 push(@{ $form->{nodiscount_linetotal} },
268 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
270 push(@{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
272 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
276 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
278 if ($form->{taxincluded}) {
281 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
282 $taxbase = $linetotal - $taxamount;
284 $taxamount = $linetotal * $taxrate;
285 $taxbase = $linetotal;
288 if ($form->round_amount($taxrate, 7) == 0) {
289 if ($form->{taxincluded}) {
290 foreach $item (@taxaccounts) {
292 $form->round_amount($linetotal * $form->{"${item}_rate"} /
293 (1 + abs($form->{"${item}_rate"})),
296 $taxaccounts{$item} += $taxamount;
297 $taxdiff += $taxamount;
299 $taxbase{$item} += $taxbase;
301 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
303 foreach $item (@taxaccounts) {
304 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
305 $taxbase{$item} += $taxbase;
309 foreach $item (@taxaccounts) {
310 $taxaccounts{$item} +=
311 $taxamount * $form->{"${item}_rate"} / $taxrate;
312 $taxbase{$item} += $taxbase;
315 $tax_rate = $taxrate * 100;
316 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
317 if ($form->{"assembly_$i"}) {
320 # get parts and push them onto the stack
322 if ($form->{groupitems}) {
324 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
326 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
329 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
332 JOIN parts p ON (a.parts_id = p.id)
333 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
335 AND a.id = '$form->{"id_$i"}'
337 $sth = $dbh->prepare($query);
338 $sth->execute || $form->dberror($query);
340 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
341 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
342 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
343 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
344 push(@{ $form->{description} }, $sameitem);
347 map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
349 push(@{ $form->{description} },
350 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
352 . qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
353 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
361 foreach my $item (sort keys %taxaccounts) {
362 push(@{ $form->{taxbase} },
363 $form->format_amount($myconfig, $taxbase{$item}, 2));
365 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
367 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
368 push(@{ $form->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
369 push(@{ $form->{taxrate} },
370 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
371 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
374 for my $i (1 .. $form->{paidaccounts}) {
375 if ($form->{"paid_$i"}) {
376 push(@{ $form->{payment} }, $form->{"paid_$i"});
377 my ($accno, $description) = split /--/, $form->{"AR_paid_$i"};
378 push(@{ $form->{paymentaccount} }, $description);
379 push(@{ $form->{paymentdate} }, $form->{"datepaid_$i"});
380 push(@{ $form->{paymentsource} }, $form->{"source_$i"});
382 $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
385 if($form->{taxincluded}) {
386 $form->{subtotal} = $form->format_amount($myconfig, $form->{total} - $tax, 2);
389 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
391 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
392 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
393 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
394 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
395 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
398 ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
400 $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
402 $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
403 $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
404 $form->set_payment_options($myconfig, $form->{invdate});
406 $form->{username} = $myconfig->{name};
410 $main::lxdebug->leave_sub();
413 sub project_description {
414 $main::lxdebug->enter_sub();
416 my ($self, $dbh, $id) = @_;
418 my $query = qq|SELECT p.description
421 my $sth = $dbh->prepare($query);
422 $sth->execute || $form->dberror($query);
424 ($_) = $sth->fetchrow_array;
428 $main::lxdebug->leave_sub();
433 sub customer_details {
434 $main::lxdebug->enter_sub();
436 my ($self, $myconfig, $form, @wanted_vars) = @_;
438 # connect to database
439 my $dbh = $form->dbconnect($myconfig);
441 # get contact id, set it if nessessary
445 if ($form->{cp_id}) {
446 $contact = "and cp.cp_id = $form->{cp_id}";
449 # get rest for the customer
450 my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes, ct.phone AS customerphone, ct.fax AS customerfax, ct.email AS customeremail
452 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
453 WHERE ct.id = $form->{customer_id} $contact order by cp.cp_id limit 1|;
454 my $sth = $dbh->prepare($query);
455 $sth->execute || $form->dberror($query);
457 $ref = $sth->fetchrow_hashref(NAME_lc);
459 # remove id and taxincluded before copy back
460 delete @$ref{qw(id taxincluded)};
462 @wanted_vars = grep({ $_ } @wanted_vars);
463 if (scalar(@wanted_vars) > 0) {
465 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
466 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
469 map { $form->{$_} = $ref->{$_} } keys %$ref;
472 if ($form->{delivery_customer_id}) {
473 my $query = qq|SELECT ct.*, ct.notes as customernotes
475 WHERE ct.id = $form->{delivery_customer_id} limit 1|;
476 my $sth = $dbh->prepare($query);
477 $sth->execute || $form->dberror($query);
479 $ref = $sth->fetchrow_hashref(NAME_lc);
482 map { $form->{"dc_$_"} = $ref->{$_} } keys %$ref;
485 if ($form->{delivery_vendor_id}) {
486 my $query = qq|SELECT ct.*, ct.notes as customernotes
488 WHERE ct.id = $form->{delivery_vendor_id} limit 1|;
489 my $sth = $dbh->prepare($query);
490 $sth->execute || $form->dberror($query);
492 $ref = $sth->fetchrow_hashref(NAME_lc);
495 map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
499 $main::lxdebug->leave_sub();
503 $main::lxdebug->enter_sub();
505 my ($self, $myconfig, $form) = @_;
507 # connect to database, turn off autocommit
508 my $dbh = $form->dbconnect_noauto($myconfig);
510 my ($query, $sth, $null, $project_id, $deliverydate);
511 my $exchangerate = 0;
513 ($null, $form->{employee_id}) = split /--/, $form->{employee};
514 unless ($form->{employee_id}) {
515 $form->get_employee($dbh);
518 $form->{payment_id} *= 1;
519 $form->{language_id} *= 1;
520 $form->{taxzone_id} *= 1;
521 $form->{delivery_customer_id} *= 1;
522 $form->{delivery_vendor_id} *= 1;
523 $form->{storno} *= 1;
524 $form->{shipto_id} *= 1;
527 ($null, $form->{department_id}) = split(/--/, $form->{department});
528 $form->{department_id} *= 1;
530 my $service_units = AM->retrieve_units($myconfig,$form,"service");
531 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
537 &reverse_invoice($dbh, $form);
540 my $uid = rand() . time;
542 $uid .= $form->{login};
544 $uid = substr($uid, 2, 75);
546 $query = qq|INSERT INTO ar (invnumber, employee_id)
547 VALUES ('$uid', $form->{employee_id})|;
548 $dbh->do($query) || $form->dberror($query);
550 $query = qq|SELECT a.id FROM ar a
551 WHERE a.invnumber = '$uid'|;
552 $sth = $dbh->prepare($query);
553 $sth->execute || $form->dberror($query);
555 ($form->{id}) = $sth->fetchrow_array;
558 if (!$form->{invnumber}) {
560 $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
561 "cnnumber" : "invnumber", $dbh);
565 map { $form->{$_} =~ s/\'/\'\'/g }
566 (qw(invnumber shippingpoint shipvia notes intnotes message));
568 my ($netamount, $invoicediff) = (0, 0);
569 my ($amount, $linetotal, $lastincomeaccno);
571 if ($form->{currency} eq $form->{defaultcurrency}) {
572 $form->{exchangerate} = 1;
575 $form->check_exchangerate($myconfig, $form->{currency},
576 $form->{transdate}, 'buy');
579 $form->{exchangerate} =
582 : $form->parse_amount($myconfig, $form->{exchangerate});
584 $form->{expense_inventory} = "";
586 foreach my $i (1 .. $form->{rowcount}) {
587 if ($form->{type} eq "credit_note") {
588 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}) * -1;
589 $form->{shipped} = 1;
591 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
596 if ($form->{storno}) {
597 $form->{"qty_$i"} *= -1;
600 if ($form->{"id_$i"}) {
603 $query = qq|SELECT p.unit
605 WHERE p.id = $form->{"id_$i"}|;
606 $sth = $dbh->prepare($query);
607 $sth->execute || $form->dberror($query);
609 my ($item_unit) = $sth->fetchrow_array();
612 if ($form->{"inventory_accno_$i"}) {
613 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
614 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
618 $baseqty = $form->{"qty_$i"} * $basefactor;
620 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
621 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
625 $baseqty = $form->{"qty_$i"} * $basefactor;
628 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
629 (qw(partnumber description unit));
631 # undo discount formatting
632 $form->{"discount_$i"} =
633 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
635 my ($allocated, $taxrate) = (0, 0);
638 # keep entered selling price
640 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
642 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
644 my $decimalplaces = ($dec > 2) ? $dec : 2;
648 $form->round_amount($fxsellprice * $form->{"discount_$i"},
650 $form->{"sellprice_$i"} = $fxsellprice - $discount;
653 map { $taxrate += $form->{"${_}_rate"} } split / /,
654 $form->{"taxaccounts_$i"};
656 # round linetotal to 2 decimal places
658 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
660 if ($form->{taxincluded}) {
661 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
662 $form->{"sellprice_$i"} =
663 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
665 $taxamount = $linetotal * $taxrate;
668 $netamount += $linetotal;
670 if ($taxamount != 0) {
672 $form->{amount}{ $form->{id} }{$_} +=
673 $taxamount * $form->{"${_}_rate"} / $taxrate
674 } split / /, $form->{"taxaccounts_$i"};
677 # add amount to income, $form->{amount}{trans_id}{accno}
679 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
682 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
683 $form->{exchangerate};
684 $linetotal = $form->round_amount($linetotal, 2);
686 # this is the difference from the inventory
687 $invoicediff += ($amount - $linetotal);
689 $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
692 $lastincomeaccno = $form->{"income_accno_$i"};
694 # adjust and round sellprice
695 $form->{"sellprice_$i"} =
696 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
699 if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
701 # adjust parts onhand quantity
703 if ($form->{"assembly_$i"}) {
705 # do not update if assembly consists of all services
706 $query = qq|SELECT sum(p.inventory_accno_id)
708 JOIN assembly a ON (a.parts_id = p.id)
709 WHERE a.id = $form->{"id_$i"}|;
710 $sth = $dbh->prepare($query);
711 $sth->execute || $form->dberror($query);
713 if ($sth->fetchrow_array) {
714 $form->update_balance($dbh, "parts", "onhand",
715 qq|id = $form->{"id_$i"}|,
717 unless $form->{shipped};
721 # record assembly item as allocated
722 &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
724 $form->update_balance($dbh, "parts", "onhand",
725 qq|id = $form->{"id_$i"}|,
727 unless $form->{shipped};
729 $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
734 ($form->{"deliverydate_$i"})
735 ? qq|'$form->{"deliverydate_$i"}'|
738 # get pricegroup_id and save it
739 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
741 my $subtotal = $form->{"subtotal_$i"} * 1;
743 # save detail record in invoice table
744 $query = qq|INSERT INTO invoice (trans_id, parts_id, description,longdescription, qty,
745 sellprice, fxsellprice, discount, allocated, assemblyitem,
746 unit, deliverydate, project_id, serialnumber, pricegroup_id,
747 ordnumber, transdate, cusordnumber, base_qty, subtotal)
748 VALUES ($form->{id}, $form->{"id_$i"},
749 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"},
750 $form->{"sellprice_$i"}, $fxsellprice,
751 $form->{"discount_$i"}, $allocated, 'f',
752 '$form->{"unit_$i"}', $deliverydate, | . conv_i($form->{"project_id_$i"}, 'NULL') . qq|,
753 '$form->{"serialnumber_$i"}', '$pricegroup_id',
754 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', $baseqty, '$subtotal')|;
755 $dbh->do($query) || $form->dberror($query);
757 if ($form->{lizenzen}) {
758 if ($form->{"licensenumber_$i"}) {
760 qq|SELECT i.id FROM invoice i WHERE i.trans_id=$form->{id} ORDER BY i.oid DESC LIMIT 1|;
761 $sth = $dbh->prepare($query);
762 $sth->execute || $form->dberror($query);
764 ($invoice_row_id) = $sth->fetchrow_array;
768 qq|INSERT INTO licenseinvoice (trans_id, license_id) VALUES ($invoice_row_id, $form->{"licensenumber_$i"})|;
769 $dbh->do($query) || $form->dberror($query);
776 $form->{datepaid} = $form->{invdate};
778 # total payments, don't move we need it here
779 for my $i (1 .. $form->{paidaccounts}) {
780 if ($form->{type} eq "credit_note") {
781 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
783 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
785 $form->{paid} += $form->{"paid_$i"};
786 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
789 my ($tax, $diff) = (0, 0);
791 $netamount = $form->round_amount($netamount, 2);
793 # figure out rounding errors for total amount vs netamount + taxes
794 if ($form->{taxincluded}) {
796 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
797 $diff += $amount - $netamount * $form->{exchangerate};
798 $netamount = $amount;
800 foreach my $item (split / /, $form->{taxaccounts}) {
801 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
802 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
803 $tax += $form->{amount}{ $form->{id} }{$item};
804 $netamount -= $form->{amount}{ $form->{id} }{$item};
807 $invoicediff += $diff;
808 ######## this only applies to tax included
809 if ($lastincomeaccno) {
810 $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
814 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
815 $diff = $amount - $netamount * $form->{exchangerate};
816 $netamount = $amount;
817 foreach my $item (split / /, $form->{taxaccounts}) {
818 $form->{amount}{ $form->{id} }{$item} =
819 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
822 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
825 $amount - $form->{amount}{ $form->{id} }{$item} *
826 $form->{exchangerate};
827 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
828 $tax += $form->{amount}{ $form->{id} }{$item};
832 $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
834 $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
837 $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
839 # update exchangerate
840 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
841 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
842 $form->{exchangerate}, 0);
845 $project_id = conv_i($form->{"globalproject_id"});
847 foreach my $trans_id (keys %{ $form->{amount} }) {
848 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
849 next unless ($form->{expense_inventory} =~ /$accno/);
851 ($form->{amount}{$trans_id}{$accno} =
852 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
855 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
856 transdate, taxkey, project_id)
857 VALUES ($trans_id, (SELECT c.id FROM chart c
858 WHERE c.accno = '$accno'),
859 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
860 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
861 do_query($form, $dbh, $query, $project_id);
862 $form->{amount}{$trans_id}{$accno} = 0;
866 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
868 ($form->{amount}{$trans_id}{$accno} =
869 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
872 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
873 transdate, taxkey, project_id)
874 VALUES ($trans_id, (SELECT id FROM chart
875 WHERE accno = '$accno'),
876 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
877 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
878 do_query($form, $dbh, $query, $project_id);
883 # deduct payment differences from diff
884 for my $i (1 .. $form->{paidaccounts}) {
885 if ($form->{"paid_$i"} != 0) {
887 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
888 $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
892 # force AR entry if 0
893 # $form->{amount}{$form->{id}}{$form->{AR}} = 1 if ($form->{amount}{$form->{id}}{$form->{AR}} == 0);
895 # record payments and offsetting AR
896 if (!$form->{storno}) {
897 for my $i (1 .. $form->{paidaccounts}) {
899 if ($form->{"paid_$i"} != 0) {
900 my ($accno) = split /--/, $form->{"AR_paid_$i"};
901 $form->{"datepaid_$i"} = $form->{invdate}
902 unless ($form->{"datepaid_$i"});
903 $form->{datepaid} = $form->{"datepaid_$i"};
907 if ($form->{currency} eq $form->{defaultcurrency}) {
908 $form->{"exchangerate_$i"} = 1;
911 $form->check_exchangerate($myconfig, $form->{currency},
912 $form->{"datepaid_$i"}, 'buy');
914 $form->{"exchangerate_$i"} =
917 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
922 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
925 if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
926 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
927 transdate, taxkey, project_id)
928 VALUES ($form->{id}, (SELECT c.id FROM chart c
930 $amount, '$form->{"datepaid_$i"}',
931 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
932 do_query($form, $dbh, $query, $form->{AR}, $form->{AR}, $project_id);
936 $form->{"paid_$i"} *= -1;
938 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
939 source, memo, taxkey, project_id)
940 VALUES ($form->{id}, (SELECT c.id FROM chart c
942 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
943 '$form->{"source_$i"}', '$form->{"memo_$i"}',
944 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
945 do_query($form, $dbh, $query, $accno, $accno, $project_id);
947 # exchangerate difference
948 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
949 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
953 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
954 $form->{"exchangerate_$i"};
956 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
959 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
965 # update exchange rate
966 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
967 $form->update_exchangerate($dbh, $form->{currency},
968 $form->{"datepaid_$i"},
969 $form->{"exchangerate_$i"}, 0);
975 # record exchange rate differences and gains/losses
976 foreach my $accno (keys %{ $form->{fx} }) {
977 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
979 ($form->{fx}{$accno}{$transdate} =
980 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
984 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
985 transdate, cleared, fx_transaction, taxkey, project_id)
987 (SELECT c.id FROM chart c
989 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1',
990 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
991 do_query($form, $dbh, $query, $accno, $accno, $project_id);
996 $amount = $netamount + $tax;
998 # set values which could be empty to 0
1000 $form->{taxincluded} *= 1;
1001 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
1002 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
1004 ($form->{deliverydate}) ? qq|'$form->{deliverydate}'| : "NULL";
1006 # fill in subject if there is none
1007 $form->{subject} = qq|$form->{label} $form->{invnumber}|
1008 unless $form->{subject};
1010 # if there is a message stuff it into the intnotes
1011 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
1012 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
1013 my $now = scalar localtime;
1014 $form->{intnotes} .= qq|\r
1015 \r| if $form->{intnotes};
1017 $form->{intnotes} .= qq|[email]\r
1019 To: $form->{email}\r
1020 $cc${bcc}Subject: $form->{subject}\r
1022 Message: $form->{message}\r| if $form->{message};
1025 $query = qq|UPDATE ar set
1026 invnumber = '$form->{invnumber}',
1027 ordnumber = '$form->{ordnumber}',
1028 quonumber = '$form->{quonumber}',
1029 cusordnumber = '$form->{cusordnumber}',
1030 transdate = '$form->{invdate}',
1031 orddate = | . conv_dateq($form->{orddate}) . qq|,
1032 quodate = | . conv_dateq($form->{quodate}) . qq|,
1033 customer_id = $form->{customer_id},
1035 netamount = $netamount,
1036 paid = $form->{paid},
1037 datepaid = $datepaid,
1039 deliverydate = $deliverydate,
1041 shippingpoint = '$form->{shippingpoint}',
1042 shipvia = '$form->{shipvia}',
1043 terms = $form->{terms},
1044 notes = '$form->{notes}',
1045 intnotes = '$form->{intnotes}',
1046 taxincluded = '$form->{taxincluded}',
1047 curr = '$form->{currency}',
1048 department_id = $form->{department_id},
1049 payment_id = $form->{payment_id},
1050 type = '$form->{type}',
1051 language_id = $form->{language_id},
1052 taxzone_id = $form->{taxzone_id},
1053 shipto_id = $form->{shipto_id},
1054 delivery_customer_id = $form->{delivery_customer_id},
1055 delivery_vendor_id = $form->{delivery_vendor_id},
1056 employee_id = $form->{employee_id},
1057 salesman_id = | . conv_i($form->{salesman_id}, 'NULL') . qq|,
1058 storno = '$form->{storno}',
1059 globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
1060 cp_id = | . conv_i($form->{"cp_id"}, 'NULL') . qq|
1061 WHERE id = $form->{id}
1063 $dbh->do($query) || $form->dberror($query);
1065 if ($form->{storno}) {
1066 $query = qq| update ar set paid=paid+amount where id=$form->{storno_id}|;
1067 $dbh->do($query) || $form->dberror($query);
1068 $query = qq| update ar set storno='$form->{storno}' where id=$form->{storno_id}|;
1069 $dbh->do($query) || $form->dberror($query);
1070 $query = qq§ update ar set intnotes='Rechnung storniert am $form->{invdate} ' || intnotes where id=$form->{storno_id}§;
1071 $dbh->do($query) || $form->dberror($query);
1073 $query = qq| update ar set paid=amount where id=$form->{id}|;
1074 $dbh->do($query) || $form->dberror($query);
1077 $form->{pago_total} = $amount;
1080 $form->{name} = $form->{customer};
1081 $form->{name} =~ s/--$form->{customer_id}//;
1083 if (!$form->{shipto_id}) {
1084 $form->add_shipto($dbh, $form->{id}, "AR");
1087 # save printed, emailed and queued
1088 $form->save_status($dbh);
1090 Common::webdav_folder($form) if ($main::webdav);
1092 my $rc = $dbh->commit;
1095 $main::lxdebug->leave_sub();
1101 $main::lxdebug->enter_sub() and my ($self, $myconfig, $form, $locale) = @_;
1103 # connect to database, turn off autocommit
1104 my $dbh = $form->dbconnect_noauto($myconfig);
1106 $form->{datepaid} = $form->{invdate};
1108 # total payments, don't move we need it here
1109 for my $i (1 .. $form->{paidaccounts}) {
1110 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
1111 $form->{"paid_$i"} *= -1 if ($form->{type} eq "credit_note");
1112 $form->{"paid"} += $form->{"paid_$i"};
1113 $form->{"datepaid"} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
1116 $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
1118 # record payments and offsetting AR
1119 for my $i (1 .. $form->{paidaccounts}) {
1120 if ($form->{"paid_$i"}) {
1122 my ($accno) = split /--/, $form->{"AR_paid_$i"};
1123 $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
1124 $form->{datepaid} = $form->{"datepaid_$i"};
1127 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1128 $form->{"exchangerate_$i"} = 1;
1130 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
1131 $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1135 $amount = $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"}, 2);
1137 $query = qq|DELETE FROM acc_trans WHERE trans_id = ? AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND amount = ? AND transdate = ?|;
1138 do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, $form->{"datepaid_$i"});
1139 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
1140 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1141 do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, $form->{"datepaid_$i"}, conv_i($form->{"globalproject_id"}), $accno);
1144 $form->{"paid_$i"} *= -1;
1146 $query = qq|DELETE FROM acc_trans WHERE trans_id = ? AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND amount = ? AND transdate = ? AND source = ? AND memo = ?|;
1147 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"});
1148 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey)
1149 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1150 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"},
1151 conv_i($form->{"globalproject_id"}), $accno);
1154 $amount = $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} * $form->{"exchangerate_$i"};
1155 $form->{fx}{ $form->{($amount > 0 ? 'fxgain_accno' : 'fxloss_accno')} }{ $form->{"datepaid_$i"} } += $amount;
1159 # update exchange rate
1160 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1161 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
1167 # record exchange rate differences and gains/losses
1168 foreach my $accno (keys %{ $form->{fx} }) {
1169 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1171 if ($form->{fx}{$accno}{$transdate} = $form->round_amount($form->{fx}{$accno}{$transdate}, 2)) { # '=' is no typo, it's an assignment
1172 $query = qq|DELETE FROM acc_trans WHERE trans_id = ? AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)
1173 AND amount = ? AND transdate = ? AND cleared = ? AND fx_transaction = ?|;
1174 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1);
1175 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey)
1176 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1177 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1, conv_i($form->{"globalproject_id"}), $accno);
1184 delete $form->{datepaid} unless $form->{paid};
1186 my $query = qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|;
1187 do_query($form, $dbh, $query, $form->{paid}, $form->{datepaid}, $form->{id});
1189 my $rc = $dbh->commit;
1192 $main::lxdebug->leave_sub() and return $rc;
1195 sub process_assembly {
1196 $main::lxdebug->enter_sub();
1198 my ($dbh, $form, $id, $totalqty) = @_;
1200 my $query = qq|SELECT a.parts_id, a.qty, p.assembly,
1201 p.partnumber, p.description, p.unit,
1202 p.inventory_accno_id, p.income_accno_id,
1205 JOIN parts p ON (a.parts_id = p.id)
1207 my $sth = $dbh->prepare($query);
1208 $sth->execute || $form->dberror($query);
1210 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1214 $ref->{inventory_accno_id} *= 1;
1215 $ref->{expense_accno_id} *= 1;
1217 map { $ref->{$_} =~ s/\'/\'\'/g } (qw(partnumber description unit));
1219 # multiply by number of assemblies
1220 $ref->{qty} *= $totalqty;
1222 if ($ref->{assembly}) {
1223 &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
1226 if ($ref->{inventory_accno_id}) {
1227 $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
1231 # save detail record for individual assembly item in invoice table
1232 $query = qq|INSERT INTO invoice (trans_id, description, parts_id, qty,
1233 sellprice, fxsellprice, allocated, assemblyitem, unit)
1235 ($form->{id}, '$ref->{description}',
1236 $ref->{parts_id}, $ref->{qty}, 0, 0, $allocated, 't',
1238 $dbh->do($query) || $form->dberror($query);
1244 $main::lxdebug->leave_sub();
1248 $main::lxdebug->enter_sub();
1250 my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
1251 $form->{taxzone_id} *=1;
1252 my $transdate = ($form->{invdate}) ? "'$form->{invdate}'" : "current_date";
1253 my $query = qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
1254 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1255 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1256 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid
1257 FROM invoice i, parts p
1258 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1259 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1260 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1261 WHERE i.parts_id = p.id
1262 AND i.parts_id = $id
1263 AND (i.base_qty + i.allocated) < 0
1265 my $sth = $dbh->prepare($query);
1266 $sth->execute || $form->dberror($query);
1271 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1272 if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
1276 $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|,
1279 # total expenses and inventory
1280 # sellprice is the cost of the item
1281 $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
1284 $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
1286 $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
1287 $form->{expense_inventory} .= " " . $ref->{expense_accno};
1288 $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
1290 $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
1291 $form->{expense_inventory} .= " " . $ref->{inventory_accno};
1295 $allocated += -$qty;
1297 last if (($totalqty -= $qty) <= 0);
1302 $main::lxdebug->leave_sub();
1307 sub reverse_invoice {
1308 $main::lxdebug->enter_sub();
1310 my ($dbh, $form) = @_;
1312 # reverse inventory items
1313 my $query = qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
1314 p.inventory_accno_id
1316 JOIN parts p ON (i.parts_id = p.id)
1317 WHERE i.trans_id = $form->{id}|;
1318 my $sth = $dbh->prepare($query);
1319 $sth->execute || $form->dberror($query);
1321 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1323 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1325 # if the invoice item is not an assemblyitem adjust parts onhand
1326 if (!$ref->{assemblyitem}) {
1328 # adjust onhand in parts table
1329 $form->update_balance($dbh, "parts", "onhand",
1330 qq|id = $ref->{parts_id}|,
1334 # loop if it is an assembly
1335 next if ($ref->{assembly});
1337 # de-allocated purchases
1338 $query = qq|SELECT i.id, i.trans_id, i.allocated
1340 WHERE i.parts_id = $ref->{parts_id}
1342 ORDER BY i.trans_id DESC|;
1343 my $sth = $dbh->prepare($query);
1344 $sth->execute || $form->dberror($query);
1346 while (my $inhref = $sth->fetchrow_hashref(NAME_lc)) {
1348 if (($ref->{qty} - $inhref->{allocated}) > 0) {
1349 $qty = $inhref->{allocated};
1353 $form->update_balance($dbh, "invoice", "allocated",
1354 qq|id = $inhref->{id}|,
1357 last if (($ref->{qty} -= $qty) <= 0);
1366 $query = qq|DELETE FROM acc_trans
1367 WHERE trans_id = $form->{id}|;
1368 $dbh->do($query) || $form->dberror($query);
1370 # delete invoice entries
1371 $query = qq|DELETE FROM invoice
1372 WHERE trans_id = $form->{id}|;
1373 $dbh->do($query) || $form->dberror($query);
1375 if ($form->{lizenzen}) {
1376 $query = qq|DELETE FROM licenseinvoice
1377 WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = $form->{id})|;
1378 $dbh->do($query) || $form->dberror($query);
1381 $query = qq|DELETE FROM shipto
1382 WHERE trans_id = $form->{id} AND module = 'AR'|;
1383 $dbh->do($query) || $form->dberror($query);
1385 $main::lxdebug->leave_sub();
1388 sub delete_invoice {
1389 $main::lxdebug->enter_sub();
1391 my ($self, $myconfig, $form, $spool) = @_;
1393 # connect to database
1394 my $dbh = $form->dbconnect_noauto($myconfig);
1396 &reverse_invoice($dbh, $form);
1399 my $query = qq|DELETE FROM ar
1400 WHERE id = $form->{id}|;
1401 $dbh->do($query) || $form->dberror($query);
1403 # delete spool files
1404 $query = qq|SELECT s.spoolfile FROM status s
1405 WHERE s.trans_id = $form->{id}|;
1406 my $sth = $dbh->prepare($query);
1407 $sth->execute || $self->dberror($query);
1410 my @spoolfiles = ();
1412 while (($spoolfile) = $sth->fetchrow_array) {
1413 push @spoolfiles, $spoolfile;
1417 # delete status entries
1418 $query = qq|DELETE FROM status
1419 WHERE trans_id = $form->{id}|;
1420 $dbh->do($query) || $form->dberror($query);
1422 my $rc = $dbh->commit;
1426 foreach $spoolfile (@spoolfiles) {
1427 unlink "$spool/$spoolfile" if $spoolfile;
1431 $main::lxdebug->leave_sub();
1436 sub retrieve_invoice {
1437 $main::lxdebug->enter_sub();
1439 my ($self, $myconfig, $form) = @_;
1441 # connect to database
1442 my $dbh = $form->dbconnect_noauto($myconfig);
1448 # get default accounts and last invoice number
1449 $query = qq|SELECT (SELECT c.accno FROM chart c
1450 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1451 (SELECT c.accno FROM chart c
1452 WHERE d.income_accno_id = c.id) AS income_accno,
1453 (SELECT c.accno FROM chart c
1454 WHERE d.expense_accno_id = c.id) AS expense_accno,
1455 (SELECT c.accno FROM chart c
1456 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1457 (SELECT c.accno FROM chart c
1458 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1459 d.curr AS currencies
1462 $query = qq|SELECT (SELECT c.accno FROM chart c
1463 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1464 (SELECT c.accno FROM chart c
1465 WHERE d.income_accno_id = c.id) AS income_accno,
1466 (SELECT c.accno FROM chart c
1467 WHERE d.expense_accno_id = c.id) AS expense_accno,
1468 (SELECT c.accno FROM chart c
1469 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1470 (SELECT c.accno FROM chart c
1471 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1472 d.curr AS currencies, current_date AS invdate
1475 my $sth = $dbh->prepare($query);
1476 $sth->execute || $form->dberror($query);
1478 my $ref = $sth->fetchrow_hashref(NAME_lc);
1479 map { $form->{$_} = $ref->{$_} } keys %$ref;
1485 $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
1486 a.orddate, a.quodate, a.globalproject_id,
1487 a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
1488 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
1489 a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
1490 a.employee_id, e.name AS employee, a.salesman_id, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type
1492 LEFT JOIN employee e ON (e.id = a.employee_id)
1493 WHERE a.id = $form->{id}|;
1494 $sth = $dbh->prepare($query);
1495 $sth->execute || $form->dberror($query);
1497 $ref = $sth->fetchrow_hashref(NAME_lc);
1498 map { $form->{$_} = $ref->{$_} } keys %$ref;
1501 $form->{exchangerate} =
1502 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1505 $query = qq|SELECT s.* FROM shipto s
1506 WHERE s.trans_id = $form->{id} AND s.module = 'AR'|;
1507 $sth = $dbh->prepare($query);
1508 $sth->execute || $form->dberror($query);
1510 $ref = $sth->fetchrow_hashref(NAME_lc);
1512 map { $form->{$_} = $ref->{$_} } keys %$ref;
1515 if ($form->{delivery_customer_id}) {
1516 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
1517 $sth = $dbh->prepare($query);
1518 $sth->execute || $form->dberror($query);
1519 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
1523 if ($form->{delivery_vendor_id}) {
1524 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
1525 $sth = $dbh->prepare($query);
1526 $sth->execute || $form->dberror($query);
1527 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
1531 # get printed, emailed
1532 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
1534 WHERE s.trans_id = $form->{id}|;
1535 $sth = $dbh->prepare($query);
1536 $sth->execute || $form->dberror($query);
1538 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1539 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
1540 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
1541 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
1542 if $ref->{spoolfile};
1545 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
1548 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1549 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1552 if (!$form->{taxzone_id}) {
1553 $form->{taxzone_id} = 0;
1555 # retrieve individual items
1557 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1558 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1559 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1560 i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice,
1561 i.discount, i.parts_id AS id, i.unit, i.deliverydate,
1562 i.project_id, pr.projectnumber, i.serialnumber,
1563 p.partnumber, p.assembly, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, i.id AS invoice_pos,
1564 pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup,
1565 i.ordnumber, i.transdate, i.cusordnumber, p.formel, i.subtotal
1567 JOIN parts p ON (i.parts_id = p.id)
1568 LEFT JOIN project pr ON (i.project_id = pr.id)
1569 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1570 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1571 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1572 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1573 WHERE i.trans_id = $form->{id}
1574 AND NOT i.assemblyitem = '1'
1576 $sth = $dbh->prepare($query);
1578 $sth->execute || $form->dberror($query);
1579 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1580 if (!$ref->{"part_inventory_accno_id"}) {
1581 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
1583 delete($ref->{"part_inventory_accno_id"});
1585 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1586 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}|;
1587 my $stw = $dbh->prepare($query);
1588 $stw->execute || $form->dberror($query);
1589 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1593 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1594 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}|;
1595 my $stw = $dbh->prepare($query);
1596 $stw->execute || $form->dberror($query);
1597 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1601 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1602 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}|;
1603 my $stw = $dbh->prepare($query);
1604 $stw->execute || $form->dberror($query);
1605 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1609 # get tax rates and description
1611 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1612 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1613 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1614 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)
1616 $stw = $dbh->prepare($query);
1617 $stw->execute || $form->dberror($query);
1618 $ref->{taxaccounts} = "";
1620 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1622 # if ($customertax{$ref->{accno}}) {
1623 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1627 $ref->{taxaccounts} .= "$ptr->{accno} ";
1629 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1630 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1631 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1632 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1633 $form->{taxaccounts} .= "$ptr->{accno} ";
1638 if ($form->{lizenzen}) {
1639 $query = qq|SELECT l.licensenumber, l.id AS licenseid
1640 FROM license l, licenseinvoice li
1641 WHERE l.id = li.license_id AND li.trans_id = $ref->{invoice_pos}|;
1642 $stg = $dbh->prepare($query);
1643 $stg->execute || $form->dberror($query);
1644 ($licensenumber, $licenseid) = $stg->fetchrow_array();
1646 "<option value=\"$licenseid\">$licensenumber</option>";
1649 if ($form->{type} eq "credit_note") {
1653 chop $ref->{taxaccounts};
1654 push @{ $form->{invoice_details} }, $ref;
1659 Common::webdav_folder($form) if ($main::webdav);
1662 my $rc = $dbh->commit;
1665 $main::lxdebug->leave_sub();
1671 $main::lxdebug->enter_sub();
1673 my ($self, $myconfig, $form) = @_;
1675 # connect to database
1676 my $dbh = $form->dbconnect($myconfig);
1678 my $dateformat = $myconfig->{dateformat};
1679 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1683 ? "to_date('$form->{invdate}', '$dateformat')"
1686 $form->{customer_id} *= 1;
1689 my $query = qq|SELECT c.name AS customer, c.discount, c.creditlimit, c.terms,
1690 c.email, c.cc, c.bcc, c.language_id, c.payment_id AS customer_payment_id,
1691 c.street, c.zipcode, c.city, c.country,
1692 $duedate + COALESCE(pt.terms_netto, 0) AS duedate, c.notes AS intnotes,
1693 b.discount AS tradediscount, b.description AS business, c.klass as customer_klass, c.taxzone_id,
1696 LEFT JOIN business b ON (b.id = c.business_id)
1697 LEFT JOIN payment_terms pt ON c.payment_id = pt.id
1699 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1700 map { $form->{$_} = $ref->{$_} } keys %$ref;
1702 my $query = qq|SELECT sum(a.amount - a.paid) AS dunning_amount FROM ar a
1703 WHERE a.paid < a.amount AND a.customer_id = ? AND a.dunning_config_id IS NOT NULL|;
1704 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1705 map { $form->{$_} = $ref->{$_} } keys %$ref;
1707 my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn
1708 WHERE id in (SELECT dunning_config_id from ar WHERE paid < amount AND customer_id = ? AND dunning_config_id IS NOT NULL)
1709 ORDER BY dunning_level DESC LIMIT 1|;
1710 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1711 map { $form->{$_} = $ref->{$_} } keys %$ref;
1713 #check whether payment_terms are better than old payment_terms
1714 if (($form->{payment_id} ne "") && ($form->{customer_payment_id} ne "")) {
1715 my $query = qq|SELECT (SELECT ranking from payment_terms WHERE id = $form->{payment_id}), (SELECT ranking FROM payment_terms WHERE id = $form->{customer_payment_id})|;
1716 my $stw = $dbh->prepare($query);
1717 $stw->execute || $form->dberror($query);
1718 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1720 if ($new_ranking > $old_ranking) {
1721 $form->{payment_id} =$form->{customer_payment_id};
1724 if ($form->{payment_id} eq "") {
1725 $form->{payment_id} =$form->{customer_payment_id};
1728 $form->{creditremaining} = $form->{creditlimit};
1729 $query = qq|SELECT SUM(a.amount - a.paid)
1731 WHERE a.customer_id = $form->{customer_id}|;
1732 $sth = $dbh->prepare($query);
1733 $sth->execute || $form->dberror($query);
1735 ($form->{creditremaining}) -= $sth->fetchrow_array;
1739 $query = qq|SELECT o.amount,
1740 (SELECT e.buy FROM exchangerate e
1741 WHERE e.curr = o.curr
1742 AND e.transdate = o.transdate)
1744 WHERE o.customer_id = $form->{customer_id}
1745 AND o.quotation = '0'
1746 AND o.closed = '0'|;
1747 $sth = $dbh->prepare($query);
1748 $sth->execute || $form->dberror($query);
1750 while (my ($amount, $exch) = $sth->fetchrow_array) {
1751 $exch = 1 unless $exch;
1752 $form->{creditremaining} -= $amount * $exch;
1756 # get shipto if we did not converted an order or invoice
1757 if (!$form->{shipto}) {
1758 map { delete $form->{$_} }
1759 qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1761 $query = qq|SELECT s.* FROM shipto s
1762 WHERE s.trans_id = $form->{customer_id} AND s.module = 'CT'|;
1763 $sth = $dbh->prepare($query);
1764 $sth->execute || $form->dberror($query);
1766 $ref = $sth->fetchrow_hashref(NAME_lc);
1768 map { $form->{$_} = $ref->{$_} } keys %$ref;
1772 # get taxes we charge for this customer
1773 $query = qq|SELECT c.accno
1775 JOIN customertax ct ON (ct.chart_id = c.id)
1776 WHERE ct.customer_id = $form->{customer_id}|;
1777 $sth = $dbh->prepare($query);
1778 $sth->execute || $form->dberror($query);
1780 my $customertax = ();
1781 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1782 $customertax{ $ref->{accno} } = 1;
1786 # setup last accounts used for this customer
1787 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1788 $query = qq|SELECT c.id, c.accno, c.description, c.link, c.category
1790 JOIN acc_trans ac ON (ac.chart_id = c.id)
1791 JOIN ar a ON (a.id = ac.trans_id)
1792 WHERE a.customer_id = $form->{customer_id}
1793 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1794 AND a.id IN (SELECT max(a2.id) FROM ar a2
1795 WHERE a2.customer_id = $form->{customer_id})|;
1796 $sth = $dbh->prepare($query);
1797 $sth->execute || $form->dberror($query);
1800 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1801 if ($ref->{category} eq 'I') {
1803 $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
1805 if ($form->{initial_transdate}) {
1807 qq|SELECT tk.tax_id, t.rate FROM taxkeys tk | .
1808 qq|LEFT JOIN tax t ON tk.tax_id = t.id | .
1809 qq|WHERE tk.chart_id = ? AND startdate <= ? | .
1810 qq|ORDER BY tk.startdate DESC LIMIT 1|;
1811 my ($tax_id, $rate) =
1812 selectrow_query($form, $dbh, $tax_query, $ref->{id},
1813 $form->{initial_transdate});
1814 $form->{"taxchart_$i"} = "${tax_id}--${rate}";
1817 if ($ref->{category} eq 'A') {
1818 $form->{ARselected} = $form->{AR_1} = $ref->{accno};
1822 $form->{rowcount} = $i if ($i && !$form->{type});
1827 $main::lxdebug->leave_sub();
1831 $main::lxdebug->enter_sub();
1833 my ($self, $myconfig, $form) = @_;
1835 # connect to database
1836 my $dbh = $form->dbconnect($myconfig);
1838 my $i = $form->{rowcount};
1840 my $where = "NOT p.obsolete = '1'";
1842 if ($form->{"partnumber_$i"}) {
1843 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1844 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1846 if ($form->{"description_$i"}) {
1847 my $description = $form->like(lc $form->{"description_$i"});
1848 $where .= " AND lower(p.description) LIKE '$description'";
1851 if ($form->{"partsgroup_$i"}) {
1852 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1853 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1856 if ($form->{"description_$i"}) {
1857 $where .= " ORDER BY p.description";
1859 $where .= " ORDER BY p.partnumber";
1863 if ($form->{type} eq "invoice") {
1865 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1866 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1870 $form->{transdate} ? $dbh->quote($form->{transdate}) :
1874 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1875 p.listprice, p.inventory_accno_id,
1876 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1877 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1878 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1879 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1880 pg.partsgroup, p.formel, p.payment_id AS part_payment_id
1882 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1883 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1884 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1885 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1887 my $sth = $dbh->prepare($query);
1888 $sth->execute || $form->dberror($query);
1890 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1892 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1893 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1894 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1895 if (!$ref->{inventory_accno_id}) {
1896 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1898 delete($ref->{inventory_accno_id});
1900 #set expense_accno=inventory_accno if they are different => bilanz
1903 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1904 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}|;
1905 my $stw = $dbh->prepare($query);
1906 $stw->execute || $form->dberror($query);
1907 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1911 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1912 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}|;
1913 my $stw = $dbh->prepare($query);
1914 $stw->execute || $form->dberror($query);
1915 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1919 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1920 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}|;
1921 my $stw = $dbh->prepare($query);
1922 $stw->execute || $form->dberror($query);
1923 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1927 #check whether payment_terms are better than old payment_terms
1928 if (($form->{payment_id} ne "") && ($ref->{part_payment_id} ne "")) {
1929 my $query = qq|select (select ranking from payment_terms WHERE id = $form->{payment_id}), (select ranking from payment_terms WHERE id = $ref->{part_payment_id})|;
1930 my $stw = $dbh->prepare($query);
1931 $stw->execute || $form->dberror($query);
1932 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1934 if ($new_ranking <= $old_ranking) {
1935 $ref->{part_payment_id} = "";
1939 # get tax rates and description
1941 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1942 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1943 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1944 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)
1946 $stw = $dbh->prepare($query);
1947 $stw->execute || $form->dberror($query);
1949 $ref->{taxaccounts} = "";
1951 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1953 # if ($customertax{$ref->{accno}}) {
1954 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1958 $ref->{taxaccounts} .= "$ptr->{accno} ";
1960 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1961 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1962 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1963 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1964 $form->{taxaccounts} .= "$ptr->{accno} ";
1970 chop $ref->{taxaccounts};
1971 if ($form->{language_id}) {
1972 $query = qq|SELECT tr.translation, tr.longdescription
1974 WHERE tr.language_id=$form->{language_id} AND tr.parts_id=$ref->{id}|;
1975 $stw = $dbh->prepare($query);
1976 $stw->execute || $form->dberror($query);
1977 my ($translation, $longdescription) = $stw->fetchrow_array();
1978 if ($translation ne "") {
1979 $ref->{description} = $translation;
1980 $ref->{longdescription} = $longdescription;
1983 $query = qq|SELECT tr.translation, tr.longdescription
1985 WHERE tr.language_id in (select id from language where article_code=(select article_code from language where id = $form->{language_id})) AND tr.parts_id=$ref->{id} LIMIT 1|;
1986 $stg = $dbh->prepare($query);
1987 $stg->execute || $form->dberror($query);
1988 my ($translation) = $stg->fetchrow_array();
1989 if ($translation ne "") {
1990 $ref->{description} = $translation;
1991 $ref->{longdescription} = $longdescription;
1998 push @{ $form->{item_list} }, $ref;
2000 if ($form->{lizenzen}) {
2001 if ($ref->{inventory_accno} > 0) {
2003 qq| SELECT l.* FROM license l WHERE l.parts_id = $ref->{id} AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
2004 $stw = $dbh->prepare($query);
2005 $stw->execute || $form->dberror($query);
2006 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
2007 push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
2016 $main::lxdebug->leave_sub();
2019 ##########################
2020 # get pricegroups from database
2021 # build up selected pricegroup
2022 # if an exchange rate - change price
2025 sub get_pricegroups_for_parts {
2027 $main::lxdebug->enter_sub();
2029 my ($self, $myconfig, $form) = @_;
2031 my $dbh = $form->dbconnect($myconfig);
2033 $form->{"PRICES"} = {};
2037 my $dimension_units = AM->retrieve_units($myconfig, $form, "dimension");
2038 my $service_units = AM->retrieve_units($myconfig, $form, "service");
2039 my $all_units = AM->retrieve_units($myconfig, $form);
2040 while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
2041 $form->{"PRICES"}{$i} = [];
2043 $id = $form->{"id_$i"};
2045 if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
2047 $id = $form->{"new_id_$i"};
2050 ($price, $selectedpricegroup_id) = split /--/,
2051 $form->{"sellprice_pg_$i"};
2053 $pricegroup_old = $form->{"pricegroup_old_$i"};
2054 $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
2055 $form->{"old_pricegroup_$i"} = $pricegroup_old;
2056 $price_new = $form->{"price_new_$i"};
2058 $price_old = $form->{"price_old_$i"};
2060 qq|SELECT pricegroup_id, (SELECT p.sellprice from parts p where p.id = $id) as default_sellprice,(SELECT pg.pricegroup FROM pricegroup pg WHERE id=pricegroup_id) AS pricegroup, price, '' AS selected FROM prices WHERE parts_id = $id UNION SELECT 0 as pricegroup_id,(SELECT sellprice FROM parts WHERE id=$id) as default_sellprice,'' as pricegroup, (SELECT DISTINCT sellprice from parts where id=$id) as price, 'selected' AS selected from prices ORDER BY pricegroup|;
2062 $pkq = $dbh->prepare($query);
2063 $pkq->execute || $form->dberror($query);
2064 if (!$form->{"unit_old_$i"}) {
2065 # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
2066 # Einheit, wie sie in den Stammdaten hinterlegt wurde.
2067 # Es sollte also angenommen werden, dass diese ausgewaehlt war.
2068 $form->{"unit_old_$i"} = $form->{"unit_$i"};
2071 # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
2072 # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
2073 $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
2075 my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
2076 if (!$check_units->{$form->{"selected_unit_$i"}} ||
2077 ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
2078 $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
2079 # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
2080 # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
2081 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
2082 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
2086 if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
2087 if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
2088 $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
2089 $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
2090 $all_units->{$form->{"unit_old_$i"}}->{"factor"};
2093 if (!$form->{"basefactor_$i"}) {
2094 $form->{"basefactor_$i"} = 1;
2096 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
2098 # push @{ $form->{PRICES}{$id} }, $pkr;
2099 #push @{ $form->{PRICES}{$i} }, $pkr;
2101 $pkr->{selected} = '';
2103 # if there is an exchange rate change price
2104 if (($form->{exchangerate} * 1) != 0) {
2106 $pkr->{price} /= $form->{exchangerate};
2109 $pkr->{price} *= $form->{"basefactor_$i"};
2111 $pkr->{price} *= $basefactor;
2113 $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
2115 if ($selectedpricegroup_id eq undef) {
2116 if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
2118 $pkr->{selected} = ' selected';
2120 # no customer pricesgroup set
2121 if ($pkr->{price} == $pkr->{default_sellprice}) {
2123 $pkr->{price} = $form->{"sellprice_$i"};
2127 $form->{"sellprice_$i"} = $pkr->{price};
2131 if ($pkr->{price} == $pkr->{default_sellprice}) {
2133 $pkr->{price} = $form->{"sellprice_$i"};
2134 $pkr->{selected} = ' selected';
2139 if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
2140 if ($selectedpricegroup_id ne $pricegroup_old) {
2141 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2142 $pkr->{selected} = ' selected';
2145 if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
2146 if ($pkr->{pricegroup_id} == 0) {
2147 $pkr->{price} = $form->{"sellprice_$i"};
2148 $pkr->{selected} = ' selected';
2151 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2152 $pkr->{selected} = ' selected';
2153 if ( ($pkr->{pricegroup_id} == 0)
2154 and ($pkr->{price} == $form->{"sellprice_$i"})) {
2156 # $pkr->{price} = $form->{"sellprice_$i"};
2158 $pkr->{price} = $form->{"sellprice_$i"};
2164 push @{ $form->{PRICES}{$i} }, $pkr;
2167 $form->{"basefactor_$i"} *= $basefactor;
2176 $main::lxdebug->leave_sub();
2180 $main::lxdebug->enter_sub();
2182 my ($self, $myconfig, $form, $table) = @_;
2184 $main::lxdebug->leave_sub() and return 0 unless ($form->{id});
2186 # make sure there's no funny stuff in $table
2187 # ToDO: die when this happens and throw an error
2188 $main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
2190 my $dbh = $form->dbconnect($myconfig);
2192 my $query = qq|SELECT storno FROM $table WHERE id = ?|;
2193 my ($result) = selectrow_query($form, $dbh, $query, $form->{id});
2197 $main::lxdebug->leave_sub();