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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form, $locale) = @_;
46 $form->{duedate} = $form->{invdate} unless ($form->{duedate});
49 my $dbh = $form->dbconnect($myconfig);
51 my $query = qq|SELECT date '$form->{duedate}' - date '$form->{invdate}'
54 my $sth = $dbh->prepare($query);
55 $sth->execute || $form->dberror($query);
57 ($form->{terms}) = $sth->fetchrow_array;
65 my %oid = ('Pg' => 'oid',
68 # sort items by partsgroup
69 for $i (1 .. $form->{rowcount}) {
71 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
72 $partsgroup = $form->{"partsgroup_$i"};
74 push @partsgroup, [$i, $partsgroup];
87 my $nodiscount_subtotal = 0;
88 my $discount_subtotal = 0;
90 my $subtotal_header = 0;
94 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
97 if ($item->[1] ne $sameitem) {
98 push(@{ $form->{description} }, qq|$item->[1]|);
99 $sameitem = $item->[1];
101 map { push(@{ $form->{$_} }, "") }
102 qw(runningnumber number serialnumber bin partnotes qty unit deliverydate sellprice listprice netprice discount linetotal);
105 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
107 if ($form->{"id_$i"} != 0) {
109 # add number, description and qty to $form->{number},
110 if ($form->{"subtotal_$i"} && !$subtotal_header) {
111 $subtotal_header = $i;
112 $position = int($position);
115 } elsif ($subtotal_header) {
117 $position = int($position);
118 $position = $position.".".$subposition;
120 $position = int($position);
123 push(@{ $form->{runningnumber} }, $position);
124 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
125 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
126 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
127 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
128 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
129 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
130 push(@{ $form->{qty} },
131 $form->format_amount($myconfig, $form->{"qty_$i"}));
132 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
133 push(@{ $form->{deliverydate_oe} }, qq|$form->{"deliverydate_$i"}|);
135 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
136 push(@{ $form->{ordnumber_oe} }, qq|$form->{"ordnumber_$i"}|);
137 push(@{ $form->{transdate_oe} }, qq|$form->{"transdate_$i"}|);
138 push(@{ $form->{invnumber} }, qq|$form->{"invnumber"}|);
139 push(@{ $form->{invdate} }, qq|$form->{"invdate"}|);
141 if ($form->{lizenzen}) {
142 if ($form->{"licensenumber_$i"}) {
144 qq|SELECT l.licensenumber, l.validuntil FROM license l WHERE l.id = $form->{"licensenumber_$i"}|;
145 $sth = $dbh->prepare($query);
146 $sth->execute || $form->dberror($query);
148 ($licensenumber, $validuntil) = $sth->fetchrow_array;
149 push(@{ $form->{licensenumber} }, $licensenumber);
150 push(@{ $form->{validuntil} },
151 $locale->date($myconfig, $validuntil, 0));
154 push(@{ $form->{licensenumber} }, "");
155 push(@{ $form->{validuntil} }, "");
160 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
162 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
163 my ($dec) = ($sellprice =~ /\.(\d+)/);
165 my $decimalplaces = ($dec > 2) ? $dec : 2;
169 $sellprice * $form->parse_amount($myconfig,
170 $form->{"discount_$i"}) / 100,
174 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
176 # keep a netprice as well, (sellprice - discount)
177 $form->{"netprice_$i"} = $sellprice - $i_discount;
179 push(@{ $form->{netprice} },
180 ($form->{"netprice_$i"} != 0)
181 ? $form->format_amount(
182 $myconfig, $form->{"netprice_$i"},
188 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
190 my $nodiscount_linetotal =
191 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
195 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
197 $linetotal = ($linetotal != 0) ? $linetotal : " ";
199 push(@{ $form->{discount} }, $discount);
200 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
201 if (($form->{"discount_$i"} ne "") && ($form->{"discount_$i"} != 0)) {
202 $form->{discount_p} = $form->{"discount_$i"};
204 $form->{total} += $linetotal;
205 $discount_subtotal += $linetotal;
206 $form->{nodiscount_total} += $nodiscount_linetotal;
207 $nodiscount_subtotal += $nodiscount_linetotal;
208 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
210 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
211 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
212 push(@{ $form->{discount_sub} }, $discount_subtotal);
213 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
214 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
215 $discount_subtotal = 0;
216 $nodiscount_subtotal = 0;
217 $subtotal_header = 0;
219 push(@{ $form->{discount_sub} }, "");
220 push(@{ $form->{nodiscount_sub} }, "");
223 if ($linetotal == $netto_linetotal) {
224 $nodiscount += $linetotal;
227 push(@{ $form->{linetotal} },
228 $form->format_amount($myconfig, $linetotal, 2));
229 push(@{ $form->{nodiscount_linetotal} },
230 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
234 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
238 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
240 if ($form->{taxincluded}) {
243 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
244 $taxbase = $linetotal - $taxamount;
246 $taxamount = $linetotal * $taxrate;
247 $taxbase = $linetotal;
250 if ($form->round_amount($taxrate, 7) == 0) {
251 if ($form->{taxincluded}) {
252 foreach $item (@taxaccounts) {
254 $form->round_amount($linetotal * $form->{"${item}_rate"} /
255 (1 + abs($form->{"${item}_rate"})),
258 $taxaccounts{$item} += $taxamount;
259 $taxdiff += $taxamount;
261 $taxbase{$item} += $taxbase;
263 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
265 foreach $item (@taxaccounts) {
266 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
267 $taxbase{$item} += $taxbase;
271 foreach $item (@taxaccounts) {
272 $taxaccounts{$item} +=
273 $taxamount * $form->{"${item}_rate"} / $taxrate;
274 $taxbase{$item} += $taxbase;
277 $tax_rate = $taxrate * 100;
278 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
279 if ($form->{"assembly_$i"}) {
282 # get parts and push them onto the stack
284 if ($form->{groupitems}) {
286 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
288 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
291 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
294 JOIN parts p ON (a.parts_id = p.id)
295 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
297 AND a.id = '$form->{"id_$i"}'
299 $sth = $dbh->prepare($query);
300 $sth->execute || $form->dberror($query);
302 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
303 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
304 map { push(@{ $form->{$_} }, "") }
305 qw(runningnumber number serialnumber unit qty bin sellprice listprice netprice discount linetotal nodiscount_linetotal);
306 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
307 push(@{ $form->{description} }, $sameitem);
310 map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
312 push(@{ $form->{description} },
313 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
315 . qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
316 map { push(@{ $form->{$_} }, "") }
317 qw(number unit qty runningnumber serialnumber bin sellprice listprice netprice discount linetotal nodiscount_linetotal);
325 foreach my $item (sort keys %taxaccounts) {
326 push(@{ $form->{taxbase} },
327 $form->format_amount($myconfig, $taxbase{$item}, 2));
329 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
331 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
332 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
333 push(@{ $form->{taxrate} },
334 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
335 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
338 for my $i (1 .. $form->{paidaccounts}) {
339 if ($form->{"paid_$i"}) {
340 push(@{ $form->{payment} }, $form->{"paid_$i"});
341 my ($accno, $description) = split /--/, $form->{"AR_paid_$i"};
342 push(@{ $form->{paymentaccount} }, $description);
343 push(@{ $form->{paymentdate} }, $form->{"datepaid_$i"});
344 push(@{ $form->{paymentsource} }, $form->{"source_$i"});
346 $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
350 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
351 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
352 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
353 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
354 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
355 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
358 ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
360 $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
362 $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
363 $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
364 $form->set_payment_options($myconfig, $form->{invdate});
366 $form->{username} = $myconfig->{name};
370 $main::lxdebug->leave_sub();
373 sub project_description {
374 $main::lxdebug->enter_sub();
376 my ($self, $dbh, $id) = @_;
378 my $query = qq|SELECT p.description
381 my $sth = $dbh->prepare($query);
382 $sth->execute || $form->dberror($query);
384 ($_) = $sth->fetchrow_array;
388 $main::lxdebug->leave_sub();
393 sub customer_details {
394 $main::lxdebug->enter_sub();
396 my ($self, $myconfig, $form, @wanted_vars) = @_;
398 # connect to database
399 my $dbh = $form->dbconnect($myconfig);
401 # get contact id, set it if nessessary
405 if ($form->{cp_id}) {
406 $contact = "and cp.cp_id = $form->{cp_id}";
409 # get rest for the customer
410 my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes
412 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
413 WHERE ct.id = $form->{customer_id} $contact order by cp.cp_id limit 1|;
414 my $sth = $dbh->prepare($query);
415 $sth->execute || $form->dberror($query);
417 $ref = $sth->fetchrow_hashref(NAME_lc);
419 # remove id and taxincluded before copy back
420 delete @$ref{qw(id taxincluded)};
422 @wanted_vars = grep({ $_ } @wanted_vars);
423 if (scalar(@wanted_vars) > 0) {
425 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
426 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
429 map { $form->{$_} = $ref->{$_} } keys %$ref;
432 if ($form->{delivery_customer_id}) {
433 my $query = qq|SELECT ct.*, ct.notes as customernotes
435 WHERE ct.id = $form->{delivery_customer_id} limit 1|;
436 my $sth = $dbh->prepare($query);
437 $sth->execute || $form->dberror($query);
439 $ref = $sth->fetchrow_hashref(NAME_lc);
442 map { $form->{"dc_$_"} = $ref->{$_} } keys %$ref;
445 if ($form->{delivery_vendor_id}) {
446 my $query = qq|SELECT ct.*, ct.notes as customernotes
448 WHERE ct.id = $form->{delivery_vendor_id} limit 1|;
449 my $sth = $dbh->prepare($query);
450 $sth->execute || $form->dberror($query);
452 $ref = $sth->fetchrow_hashref(NAME_lc);
455 map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
459 $main::lxdebug->leave_sub();
463 $main::lxdebug->enter_sub();
465 my ($self, $myconfig, $form) = @_;
467 # connect to database, turn off autocommit
468 my $dbh = $form->dbconnect_noauto($myconfig);
470 my ($query, $sth, $null, $project_id, $deliverydate);
471 my $exchangerate = 0;
473 ($null, $form->{employee_id}) = split /--/, $form->{employee};
474 unless ($form->{employee_id}) {
475 $form->get_employee($dbh);
478 $form->{payment_id} *= 1;
479 $form->{language_id} *= 1;
480 $form->{taxzone_id} *= 1;
481 $form->{delivery_customer_id} *= 1;
482 $form->{delivery_vendor_id} *= 1;
483 $form->{storno} *= 1;
484 $form->{shipto_id} *= 1;
487 ($null, $form->{department_id}) = split(/--/, $form->{department});
488 $form->{department_id} *= 1;
490 my $service_units = AM->retrieve_units($myconfig,$form,"service");
491 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
497 &reverse_invoice($dbh, $form);
500 my $uid = rand() . time;
502 $uid .= $form->{login};
504 $uid = substr($uid, 2, 75);
506 $query = qq|INSERT INTO ar (invnumber, employee_id)
507 VALUES ('$uid', $form->{employee_id})|;
508 $dbh->do($query) || $form->dberror($query);
510 $query = qq|SELECT a.id FROM ar a
511 WHERE a.invnumber = '$uid'|;
512 $sth = $dbh->prepare($query);
513 $sth->execute || $form->dberror($query);
515 ($form->{id}) = $sth->fetchrow_array;
519 map { $form->{$_} =~ s/\'/\'\'/g }
520 (qw(invnumber shippingpoint shipvia notes intnotes message));
522 my ($netamount, $invoicediff) = (0, 0);
523 my ($amount, $linetotal, $lastincomeaccno);
525 if ($form->{currency} eq $form->{defaultcurrency}) {
526 $form->{exchangerate} = 1;
529 $form->check_exchangerate($myconfig, $form->{currency},
530 $form->{transdate}, 'buy');
533 $form->{exchangerate} =
536 : $form->parse_amount($myconfig, $form->{exchangerate});
538 $form->{expense_inventory} = "";
540 foreach my $i (1 .. $form->{rowcount}) {
541 if ($form->{type} eq "credit_note") {
542 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}) * -1;
543 $form->{shipped} = 1;
545 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
550 if ($form->{storno}) {
551 $form->{"qty_$i"} *= -1;
554 if ($form->{"id_$i"}) {
557 $query = qq|SELECT p.unit
559 WHERE p.id = $form->{"id_$i"}|;
560 $sth = $dbh->prepare($query);
561 $sth->execute || $form->dberror($query);
563 my ($item_unit) = $sth->fetchrow_array();
566 if ($form->{"inventory_accno_$i"}) {
567 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
568 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
572 $baseqty = $form->{"qty_$i"} * $basefactor;
574 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
575 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
579 $baseqty = $form->{"qty_$i"} * $basefactor;
582 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
583 (qw(partnumber description unit));
585 # undo discount formatting
586 $form->{"discount_$i"} =
587 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
589 my ($allocated, $taxrate) = (0, 0);
592 # keep entered selling price
594 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
596 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
598 my $decimalplaces = ($dec > 2) ? $dec : 2;
602 $form->round_amount($fxsellprice * $form->{"discount_$i"},
604 $form->{"sellprice_$i"} = $fxsellprice - $discount;
607 map { $taxrate += $form->{"${_}_rate"} } split / /,
608 $form->{"taxaccounts_$i"};
610 # round linetotal to 2 decimal places
612 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
614 if ($form->{taxincluded}) {
615 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
616 $form->{"sellprice_$i"} =
617 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
619 $taxamount = $linetotal * $taxrate;
622 $netamount += $linetotal;
624 if ($taxamount != 0) {
626 $form->{amount}{ $form->{id} }{$_} +=
627 $taxamount * $form->{"${_}_rate"} / $taxrate
628 } split / /, $form->{"taxaccounts_$i"};
631 # add amount to income, $form->{amount}{trans_id}{accno}
633 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
636 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
637 $form->{exchangerate};
638 $linetotal = $form->round_amount($linetotal, 2);
640 # this is the difference from the inventory
641 $invoicediff += ($amount - $linetotal);
643 $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
646 $lastincomeaccno = $form->{"income_accno_$i"};
648 # adjust and round sellprice
649 $form->{"sellprice_$i"} =
650 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
653 if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
655 # adjust parts onhand quantity
657 if ($form->{"assembly_$i"}) {
659 # do not update if assembly consists of all services
660 $query = qq|SELECT sum(p.inventory_accno_id)
662 JOIN assembly a ON (a.parts_id = p.id)
663 WHERE a.id = $form->{"id_$i"}|;
664 $sth = $dbh->prepare($query);
665 $sth->execute || $form->dberror($query);
667 if ($sth->fetchrow_array) {
668 $form->update_balance($dbh, "parts", "onhand",
669 qq|id = $form->{"id_$i"}|,
671 unless $form->{shipped};
675 # record assembly item as allocated
676 &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
678 $form->update_balance($dbh, "parts", "onhand",
679 qq|id = $form->{"id_$i"}|,
681 unless $form->{shipped};
683 $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
687 $project_id = 'NULL';
688 if ($form->{"projectnumber_$i"}) {
689 $project_id = $form->{"projectnumber_$i"};
692 ($form->{"deliverydate_$i"})
693 ? qq|'$form->{"deliverydate_$i"}'|
696 # get pricegroup_id and save it
697 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
699 my $subtotal = $form->{"subtotal_$i"} * 1;
701 # save detail record in invoice table
702 $query = qq|INSERT INTO invoice (trans_id, parts_id, description,longdescription, qty,
703 sellprice, fxsellprice, discount, allocated, assemblyitem,
704 unit, deliverydate, project_id, serialnumber, pricegroup_id,
705 ordnumber, transdate, cusordnumber, base_qty, subtotal)
706 VALUES ($form->{id}, $form->{"id_$i"},
707 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"},
708 $form->{"sellprice_$i"}, $fxsellprice,
709 $form->{"discount_$i"}, $allocated, 'f',
710 '$form->{"unit_$i"}', $deliverydate, (SELECT id from project where projectnumber = '$project_id'),
711 '$form->{"serialnumber_$i"}', '$pricegroup_id',
712 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', $baseqty, '$subtotal')|;
713 $dbh->do($query) || $form->dberror($query);
715 if ($form->{lizenzen}) {
716 if ($form->{"licensenumber_$i"}) {
718 qq|SELECT i.id FROM invoice i WHERE i.trans_id=$form->{id} ORDER BY i.oid DESC LIMIT 1|;
719 $sth = $dbh->prepare($query);
720 $sth->execute || $form->dberror($query);
722 ($invoice_row_id) = $sth->fetchrow_array;
726 qq|INSERT INTO licenseinvoice (trans_id, license_id) VALUES ($invoice_row_id, $form->{"licensenumber_$i"})|;
727 $dbh->do($query) || $form->dberror($query);
734 $form->{datepaid} = $form->{invdate};
736 # total payments, don't move we need it here
737 for my $i (1 .. $form->{paidaccounts}) {
738 if ($form->{type} eq "credit_note") {
739 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
741 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
743 $form->{paid} += $form->{"paid_$i"};
744 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
747 my ($tax, $diff) = (0, 0);
749 $netamount = $form->round_amount($netamount, 2);
751 # figure out rounding errors for total amount vs netamount + taxes
752 if ($form->{taxincluded}) {
754 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
755 $diff += $amount - $netamount * $form->{exchangerate};
756 $netamount = $amount;
758 foreach my $item (split / /, $form->{taxaccounts}) {
759 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
760 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
761 $tax += $form->{amount}{ $form->{id} }{$item};
762 $netamount -= $form->{amount}{ $form->{id} }{$item};
765 $invoicediff += $diff;
766 ######## this only applies to tax included
767 if ($lastincomeaccno) {
768 $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
772 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
773 $diff = $amount - $netamount * $form->{exchangerate};
774 $netamount = $amount;
775 foreach my $item (split / /, $form->{taxaccounts}) {
776 $form->{amount}{ $form->{id} }{$item} =
777 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
780 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
783 $amount - $form->{amount}{ $form->{id} }{$item} *
784 $form->{exchangerate};
785 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
786 $tax += $form->{amount}{ $form->{id} }{$item};
790 $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
792 $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
795 $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
797 # update exchangerate
798 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
799 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
800 $form->{exchangerate}, 0);
803 foreach my $trans_id (keys %{ $form->{amount} }) {
804 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
805 next unless ($form->{expense_inventory} =~ /$accno/);
807 ($form->{amount}{$trans_id}{$accno} =
808 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
811 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
813 VALUES ($trans_id, (SELECT c.id FROM chart c
814 WHERE c.accno = '$accno'),
815 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
816 (SELECT taxkey_id FROM chart WHERE accno = '$accno'))|;
817 $dbh->do($query) || $form->dberror($query);
818 $form->{amount}{$trans_id}{$accno} = 0;
822 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
824 ($form->{amount}{$trans_id}{$accno} =
825 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
828 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
830 VALUES ($trans_id, (SELECT id FROM chart
831 WHERE accno = '$accno'),
832 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
833 (SELECT taxkey_id FROM chart WHERE accno = '$accno'))|;
834 $dbh->do($query) || $form->dberror($query);
839 # deduct payment differences from diff
840 for my $i (1 .. $form->{paidaccounts}) {
841 if ($form->{"paid_$i"} != 0) {
843 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
844 $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
848 # force AR entry if 0
849 # $form->{amount}{$form->{id}}{$form->{AR}} = 1 if ($form->{amount}{$form->{id}}{$form->{AR}} == 0);
851 # record payments and offsetting AR
852 if (!$form->{storno}) {
853 for my $i (1 .. $form->{paidaccounts}) {
855 if ($form->{"paid_$i"} != 0) {
856 my ($accno) = split /--/, $form->{"AR_paid_$i"};
857 $form->{"datepaid_$i"} = $form->{invdate}
858 unless ($form->{"datepaid_$i"});
859 $form->{datepaid} = $form->{"datepaid_$i"};
863 if ($form->{currency} eq $form->{defaultcurrency}) {
864 $form->{"exchangerate_$i"} = 1;
867 $form->check_exchangerate($myconfig, $form->{currency},
868 $form->{"datepaid_$i"}, 'buy');
870 $form->{"exchangerate_$i"} =
873 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
878 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
881 if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
882 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
884 VALUES ($form->{id}, (SELECT c.id FROM chart c
885 WHERE c.accno = '$form->{AR}'),
886 $amount, '$form->{"datepaid_$i"}')|;
887 $dbh->do($query) || $form->dberror($query);
891 $form->{"paid_$i"} *= -1;
893 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
895 VALUES ($form->{id}, (SELECT c.id FROM chart c
896 WHERE c.accno = '$accno'),
897 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
898 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
899 $dbh->do($query) || $form->dberror($query);
901 # exchangerate difference
902 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
903 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
907 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
908 $form->{"exchangerate_$i"};
910 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
913 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
919 # update exchange rate
920 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
921 $form->update_exchangerate($dbh, $form->{currency},
922 $form->{"datepaid_$i"},
923 $form->{"exchangerate_$i"}, 0);
929 # record exchange rate differences and gains/losses
930 foreach my $accno (keys %{ $form->{fx} }) {
931 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
933 ($form->{fx}{$accno}{$transdate} =
934 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
938 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
939 transdate, cleared, fx_transaction)
941 (SELECT c.id FROM chart c
942 WHERE c.accno = '$accno'),
943 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1')|;
944 $dbh->do($query) || $form->dberror($query);
949 $amount = $netamount + $tax;
951 # set values which could be empty to 0
953 $form->{taxincluded} *= 1;
954 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
955 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
957 ($form->{deliverydate}) ? qq|'$form->{deliverydate}'| : "NULL";
959 # fill in subject if there is none
960 $form->{subject} = qq|$form->{label} $form->{invnumber}|
961 unless $form->{subject};
963 # if there is a message stuff it into the intnotes
964 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
965 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
966 my $now = scalar localtime;
967 $form->{intnotes} .= qq|\r
968 \r| if $form->{intnotes};
970 $form->{intnotes} .= qq|[email]\r
973 $cc${bcc}Subject: $form->{subject}\r
975 Message: $form->{message}\r| if $form->{message};
978 $query = qq|UPDATE ar set
979 invnumber = '$form->{invnumber}',
980 ordnumber = '$form->{ordnumber}',
981 quonumber = '$form->{quonumber}',
982 cusordnumber = '$form->{cusordnumber}',
983 transdate = '$form->{invdate}',
984 orddate = | . conv_dateq($form->{orddate}) . qq|,
985 quodate = | . conv_dateq($form->{quodate}) . qq|,
986 customer_id = $form->{customer_id},
988 netamount = $netamount,
989 paid = $form->{paid},
990 datepaid = $datepaid,
992 deliverydate = $deliverydate,
994 shippingpoint = '$form->{shippingpoint}',
995 shipvia = '$form->{shipvia}',
996 terms = $form->{terms},
997 notes = '$form->{notes}',
998 intnotes = '$form->{intnotes}',
999 taxincluded = '$form->{taxincluded}',
1000 curr = '$form->{currency}',
1001 department_id = $form->{department_id},
1002 payment_id = $form->{payment_id},
1003 type = '$form->{type}',
1004 language_id = $form->{language_id},
1005 taxzone_id = $form->{taxzone_id},
1006 shipto_id = $form->{shipto_id},
1007 delivery_customer_id = $form->{delivery_customer_id},
1008 delivery_vendor_id = $form->{delivery_vendor_id},
1009 employee_id = $form->{employee_id},
1010 storno = '$form->{storno}',
1011 cp_id = | . conv_i($form->{"cp_id"}, 'NULL') . qq|
1012 WHERE id = $form->{id}
1014 $dbh->do($query) || $form->dberror($query);
1016 if ($form->{storno}) {
1017 $query = qq| update ar set paid=paid+amount where id=$form->{storno_id}|;
1018 $dbh->do($query) || $form->dberror($query);
1019 $query = qq| update ar set storno='$form->{storno}' where id=$form->{storno_id}|;
1020 $dbh->do($query) || $form->dberror($query);
1021 $query = qq§ update ar set intnotes='Rechnung storniert am $form->{invdate} ' || intnotes where id=$form->{storno_id}§;
1022 $dbh->do($query) || $form->dberror($query);
1024 $query = qq| update ar set paid=amount where id=$form->{id}|;
1025 $dbh->do($query) || $form->dberror($query);
1028 $form->{pago_total} = $amount;
1031 $form->{name} = $form->{customer};
1032 $form->{name} =~ s/--$form->{customer_id}//;
1034 if (!$form->{shipto_id}) {
1035 $form->add_shipto($dbh, $form->{id}, "AR");
1038 # save printed, emailed and queued
1039 $form->save_status($dbh);
1041 if ($form->{webdav}) {
1042 &webdav_folder($myconfig, $form);
1045 my $rc = $dbh->commit;
1048 $main::lxdebug->leave_sub();
1054 $main::lxdebug->enter_sub();
1056 my ($self, $myconfig, $form, $locale) = @_;
1058 # connect to database, turn off autocommit
1059 my $dbh = $form->dbconnect_noauto($myconfig);
1061 $form->{datepaid} = $form->{invdate};
1063 # total payments, don't move we need it here
1064 for my $i (1 .. $form->{paidaccounts}) {
1065 if ($form->{type} eq "credit_note") {
1066 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
1068 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
1070 $form->{paid} += $form->{"paid_$i"};
1071 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
1074 $form->{exchangerate} =
1075 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1078 # record payments and offsetting AR
1079 for my $i (1 .. $form->{paidaccounts}) {
1081 if ($form->{"paid_$i"} != 0) {
1082 my ($accno) = split /--/, $form->{"AR_paid_$i"};
1083 $form->{"datepaid_$i"} = $form->{invdate}
1084 unless ($form->{"datepaid_$i"});
1085 $form->{datepaid} = $form->{"datepaid_$i"};
1088 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1089 $form->{"exchangerate_$i"} = 1;
1092 $form->check_exchangerate($myconfig, $form->{currency},
1093 $form->{"datepaid_$i"}, 'buy');
1095 $form->{"exchangerate_$i"} =
1098 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1103 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
1107 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1108 WHERE c.accno = '$form->{AR}') AND amount=$amount AND transdate='$form->{"datepaid_$i"}'|;
1109 $dbh->do($query) || $form->dberror($query);
1111 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1113 VALUES ($form->{id}, (SELECT c.id FROM chart c
1114 WHERE c.accno = '$form->{AR}'),
1115 $amount, '$form->{"datepaid_$i"}')|;
1116 $dbh->do($query) || $form->dberror($query);
1120 $form->{"paid_$i"} *= -1;
1122 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1123 WHERE c.accno = '$accno') AND amount=$form->{"paid_$i"} AND transdate='$form->{"datepaid_$i"}' AND source='$form->{"source_$i"}' AND memo='$form->{"memo_$i"}'|;
1124 $dbh->do($query) || $form->dberror($query);
1126 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
1128 VALUES ($form->{id}, (SELECT c.id FROM chart c
1129 WHERE c.accno = '$accno'),
1130 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
1131 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
1132 $dbh->do($query) || $form->dberror($query);
1137 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
1138 $form->{"exchangerate_$i"};
1140 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
1143 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
1149 # update exchange rate
1150 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1151 $form->update_exchangerate($dbh, $form->{currency},
1152 $form->{"datepaid_$i"},
1153 $form->{"exchangerate_$i"}, 0);
1158 # record exchange rate differences and gains/losses
1159 foreach my $accno (keys %{ $form->{fx} }) {
1160 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1162 ($form->{fx}{$accno}{$transdate} =
1163 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
1166 $query = qq|DELETE FROM acc_trans WHERE trans_id=$form->{id} AND chart_id=(SELECT c.id FROM chart c
1167 WHERE c.accno = '$accno') AND amount=$form->{fx}{$accno}{$transdate} AND transdate='$transdate' AND cleared='0' AND fx_transaction='1'|;
1168 $dbh->do($query) || $form->dberror($query);
1169 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
1170 transdate, cleared, fx_transaction)
1171 VALUES ($form->{id},
1172 (SELECT c.id FROM chart c
1173 WHERE c.accno = '$accno'),
1174 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1')|;
1175 $dbh->do($query) || $form->dberror($query);
1179 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
1182 my $query = qq|UPDATE ar set
1183 paid = $form->{paid},
1184 datepaid = $datepaid
1185 WHERE id=$form->{id}|;
1187 $dbh->do($query) || $form->dberror($query);
1189 my $rc = $dbh->commit;
1192 $main::lxdebug->leave_sub();
1197 sub process_assembly {
1198 $main::lxdebug->enter_sub();
1200 my ($dbh, $form, $id, $totalqty) = @_;
1202 my $query = qq|SELECT a.parts_id, a.qty, p.assembly,
1203 p.partnumber, p.description, p.unit,
1204 p.inventory_accno_id, p.income_accno_id,
1207 JOIN parts p ON (a.parts_id = p.id)
1209 my $sth = $dbh->prepare($query);
1210 $sth->execute || $form->dberror($query);
1212 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1216 $ref->{inventory_accno_id} *= 1;
1217 $ref->{expense_accno_id} *= 1;
1219 map { $ref->{$_} =~ s/\'/\'\'/g } (qw(partnumber description unit));
1221 # multiply by number of assemblies
1222 $ref->{qty} *= $totalqty;
1224 if ($ref->{assembly}) {
1225 &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
1228 if ($ref->{inventory_accno_id}) {
1229 $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
1233 # save detail record for individual assembly item in invoice table
1234 $query = qq|INSERT INTO invoice (trans_id, description, parts_id, qty,
1235 sellprice, fxsellprice, allocated, assemblyitem, unit)
1237 ($form->{id}, '$ref->{description}',
1238 $ref->{parts_id}, $ref->{qty}, 0, 0, $allocated, 't',
1240 $dbh->do($query) || $form->dberror($query);
1246 $main::lxdebug->leave_sub();
1250 $main::lxdebug->enter_sub();
1252 my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
1253 $form->{taxzone_id} *=1;
1254 my $transdate = ($form->{invdate}) ? "'$form->{invdate}'" : "current_date";
1255 my $query = qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
1256 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1257 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1258 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid
1259 FROM invoice i, parts p
1260 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1261 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1262 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1263 WHERE i.parts_id = p.id
1264 AND i.parts_id = $id
1265 AND (i.base_qty + i.allocated) < 0
1267 my $sth = $dbh->prepare($query);
1268 $sth->execute || $form->dberror($query);
1273 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1274 if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
1278 $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|,
1281 # total expenses and inventory
1282 # sellprice is the cost of the item
1283 $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
1286 $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
1288 $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
1289 $form->{expense_inventory} .= " " . $ref->{expense_accno};
1290 $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
1292 $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
1293 $form->{expense_inventory} .= " " . $ref->{inventory_accno};
1297 $allocated += -$qty;
1299 last if (($totalqty -= $qty) <= 0);
1304 $main::lxdebug->leave_sub();
1309 sub reverse_invoice {
1310 $main::lxdebug->enter_sub();
1312 my ($dbh, $form) = @_;
1314 # reverse inventory items
1315 my $query = qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
1316 p.inventory_accno_id
1318 JOIN parts p ON (i.parts_id = p.id)
1319 WHERE i.trans_id = $form->{id}|;
1320 my $sth = $dbh->prepare($query);
1321 $sth->execute || $form->dberror($query);
1323 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1325 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1327 # if the invoice item is not an assemblyitem adjust parts onhand
1328 if (!$ref->{assemblyitem}) {
1330 # adjust onhand in parts table
1331 $form->update_balance($dbh, "parts", "onhand",
1332 qq|id = $ref->{parts_id}|,
1336 # loop if it is an assembly
1337 next if ($ref->{assembly});
1339 # de-allocated purchases
1340 $query = qq|SELECT i.id, i.trans_id, i.allocated
1342 WHERE i.parts_id = $ref->{parts_id}
1344 ORDER BY i.trans_id DESC|;
1345 my $sth = $dbh->prepare($query);
1346 $sth->execute || $form->dberror($query);
1348 while (my $inhref = $sth->fetchrow_hashref(NAME_lc)) {
1350 if (($ref->{qty} - $inhref->{allocated}) > 0) {
1351 $qty = $inhref->{allocated};
1355 $form->update_balance($dbh, "invoice", "allocated",
1356 qq|id = $inhref->{id}|,
1359 last if (($ref->{qty} -= $qty) <= 0);
1368 $query = qq|DELETE FROM acc_trans
1369 WHERE trans_id = $form->{id}|;
1370 $dbh->do($query) || $form->dberror($query);
1372 # delete invoice entries
1373 $query = qq|DELETE FROM invoice
1374 WHERE trans_id = $form->{id}|;
1375 $dbh->do($query) || $form->dberror($query);
1377 if ($form->{lizenzen}) {
1378 $query = qq|DELETE FROM licenseinvoice
1379 WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = $form->{id})|;
1380 $dbh->do($query) || $form->dberror($query);
1383 $query = qq|DELETE FROM shipto
1384 WHERE trans_id = $form->{id} AND module = 'AR'|;
1385 $dbh->do($query) || $form->dberror($query);
1387 $main::lxdebug->leave_sub();
1390 sub delete_invoice {
1391 $main::lxdebug->enter_sub();
1393 my ($self, $myconfig, $form, $spool) = @_;
1395 # connect to database
1396 my $dbh = $form->dbconnect_noauto($myconfig);
1398 &reverse_invoice($dbh, $form);
1401 my $query = qq|DELETE FROM ar
1402 WHERE id = $form->{id}|;
1403 $dbh->do($query) || $form->dberror($query);
1405 # delete spool files
1406 $query = qq|SELECT s.spoolfile FROM status s
1407 WHERE s.trans_id = $form->{id}|;
1408 my $sth = $dbh->prepare($query);
1409 $sth->execute || $self->dberror($query);
1412 my @spoolfiles = ();
1414 while (($spoolfile) = $sth->fetchrow_array) {
1415 push @spoolfiles, $spoolfile;
1419 # delete status entries
1420 $query = qq|DELETE FROM status
1421 WHERE trans_id = $form->{id}|;
1422 $dbh->do($query) || $form->dberror($query);
1424 my $rc = $dbh->commit;
1428 foreach $spoolfile (@spoolfiles) {
1429 unlink "$spool/$spoolfile" if $spoolfile;
1433 $main::lxdebug->leave_sub();
1438 sub retrieve_invoice {
1439 $main::lxdebug->enter_sub();
1441 my ($self, $myconfig, $form) = @_;
1443 # connect to database
1444 my $dbh = $form->dbconnect_noauto($myconfig);
1450 # get default accounts and last invoice number
1451 $query = qq|SELECT (SELECT c.accno FROM chart c
1452 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1453 (SELECT c.accno FROM chart c
1454 WHERE d.income_accno_id = c.id) AS income_accno,
1455 (SELECT c.accno FROM chart c
1456 WHERE d.expense_accno_id = c.id) AS expense_accno,
1457 (SELECT c.accno FROM chart c
1458 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1459 (SELECT c.accno FROM chart c
1460 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1461 d.curr AS currencies
1464 $query = qq|SELECT (SELECT c.accno FROM chart c
1465 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1466 (SELECT c.accno FROM chart c
1467 WHERE d.income_accno_id = c.id) AS income_accno,
1468 (SELECT c.accno FROM chart c
1469 WHERE d.expense_accno_id = c.id) AS expense_accno,
1470 (SELECT c.accno FROM chart c
1471 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1472 (SELECT c.accno FROM chart c
1473 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1474 d.curr AS currencies, current_date AS invdate
1477 my $sth = $dbh->prepare($query);
1478 $sth->execute || $form->dberror($query);
1480 my $ref = $sth->fetchrow_hashref(NAME_lc);
1481 map { $form->{$_} = $ref->{$_} } keys %$ref;
1487 $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
1488 a.orddate, a.quodate,
1489 a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
1490 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
1491 a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
1492 a.employee_id, e.name AS employee, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type
1494 LEFT JOIN employee e ON (e.id = a.employee_id)
1495 WHERE a.id = $form->{id}|;
1496 $sth = $dbh->prepare($query);
1497 $sth->execute || $form->dberror($query);
1499 $ref = $sth->fetchrow_hashref(NAME_lc);
1500 map { $form->{$_} = $ref->{$_} } keys %$ref;
1503 $form->{exchangerate} =
1504 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1507 $query = qq|SELECT s.* FROM shipto s
1508 WHERE s.trans_id = $form->{id} AND s.module = 'AR'|;
1509 $sth = $dbh->prepare($query);
1510 $sth->execute || $form->dberror($query);
1512 $ref = $sth->fetchrow_hashref(NAME_lc);
1514 map { $form->{$_} = $ref->{$_} } keys %$ref;
1517 if ($form->{delivery_customer_id}) {
1518 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
1519 $sth = $dbh->prepare($query);
1520 $sth->execute || $form->dberror($query);
1521 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
1525 if ($form->{delivery_vendor_id}) {
1526 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
1527 $sth = $dbh->prepare($query);
1528 $sth->execute || $form->dberror($query);
1529 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
1533 # get printed, emailed
1534 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
1536 WHERE s.trans_id = $form->{id}|;
1537 $sth = $dbh->prepare($query);
1538 $sth->execute || $form->dberror($query);
1540 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1541 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
1542 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
1543 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
1544 if $ref->{spoolfile};
1547 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
1550 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1551 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1554 if (!$form->{taxzone_id}) {
1555 $form->{taxzone_id} = 0;
1557 # retrieve individual items
1559 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1560 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1561 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1562 i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice,
1563 i.discount, i.parts_id AS id, i.unit, i.deliverydate,
1564 i.project_id, pr.projectnumber, i.serialnumber,
1565 p.partnumber, p.assembly, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, i.id AS invoice_pos,
1566 pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup,
1567 i.ordnumber, i.transdate, i.cusordnumber, p.formel, i.subtotal
1569 JOIN parts p ON (i.parts_id = p.id)
1570 LEFT JOIN project pr ON (i.project_id = pr.id)
1571 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1572 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1573 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1574 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1575 WHERE i.trans_id = $form->{id}
1576 AND NOT i.assemblyitem = '1'
1578 $sth = $dbh->prepare($query);
1580 $sth->execute || $form->dberror($query);
1581 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1582 if (!$ref->{"part_inventory_accno_id"}) {
1583 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
1585 delete($ref->{"part_inventory_accno_id"});
1587 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1588 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}|;
1589 my $stw = $dbh->prepare($query);
1590 $stw->execute || $form->dberror($query);
1591 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1595 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1596 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}|;
1597 my $stw = $dbh->prepare($query);
1598 $stw->execute || $form->dberror($query);
1599 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1603 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1604 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}|;
1605 my $stw = $dbh->prepare($query);
1606 $stw->execute || $form->dberror($query);
1607 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1611 # get tax rates and description
1613 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1614 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1615 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1616 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)
1618 $stw = $dbh->prepare($query);
1619 $stw->execute || $form->dberror($query);
1620 $ref->{taxaccounts} = "";
1622 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1624 # if ($customertax{$ref->{accno}}) {
1625 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1629 $ref->{taxaccounts} .= "$ptr->{accno} ";
1631 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1632 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1633 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1634 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1635 $form->{taxaccounts} .= "$ptr->{accno} ";
1640 if ($form->{lizenzen}) {
1641 $query = qq|SELECT l.licensenumber, l.id AS licenseid
1642 FROM license l, licenseinvoice li
1643 WHERE l.id = li.license_id AND li.trans_id = $ref->{invoice_pos}|;
1644 $stg = $dbh->prepare($query);
1645 $stg->execute || $form->dberror($query);
1646 ($licensenumber, $licenseid) = $stg->fetchrow_array();
1648 "<option value=\"$licenseid\">$licensenumber</option>";
1651 if ($form->{type} eq "credit_note") {
1655 chop $ref->{taxaccounts};
1656 push @{ $form->{invoice_details} }, $ref;
1661 if ($form->{webdav}) {
1662 &webdav_folder($myconfig, $form);
1666 my $rc = $dbh->commit;
1669 $main::lxdebug->leave_sub();
1675 $main::lxdebug->enter_sub();
1677 my ($self, $myconfig, $form) = @_;
1679 # connect to database
1680 my $dbh = $form->dbconnect($myconfig);
1682 my $dateformat = $myconfig->{dateformat};
1683 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1687 ? "to_date('$form->{invdate}', '$dateformat')"
1690 $form->{customer_id} *= 1;
1693 my $query = qq|SELECT c.name AS customer, c.discount, c.creditlimit, c.terms,
1694 c.email, c.cc, c.bcc, c.language_id, c.payment_id AS customer_payment_id,
1695 c.street, c.zipcode, c.city, c.country,
1696 $duedate + c.terms AS duedate, c.notes AS intnotes,
1697 b.discount AS tradediscount, b.description AS business, c.klass as customer_klass, c.taxzone_id
1699 LEFT JOIN business b ON (b.id = c.business_id)
1700 WHERE c.id = $form->{customer_id}|;
1701 my $sth = $dbh->prepare($query);
1702 $sth->execute || $form->dberror($query);
1704 $ref = $sth->fetchrow_hashref(NAME_lc);
1706 map { $form->{$_} = $ref->{$_} } keys %$ref;
1709 my $query = qq|SELECT sum(a.amount-a.paid) AS dunning_amount FROM ar a WHERE a.paid < a.amount AND a.customer_id=$form->{customer_id} AND a.dunning_id IS NOT NULL|;
1710 my $sth = $dbh->prepare($query);
1712 $sth->execute || $form->dberror($query);
1714 $ref = $sth->fetchrow_hashref(NAME_lc);
1716 map { $form->{$_} = $ref->{$_} } keys %$ref;
1719 #print(STDERR "DUNNING AMOUTN $form->{dunning_amount}\n");
1721 my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn WHERE id in (select dunning_id from ar WHERE paid < amount AND customer_id=$form->{customer_id} AND dunning_id IS NOT NULL) ORDER BY dunning_level DESC LIMIT 1|;
1722 my $sth = $dbh->prepare($query);
1724 $sth->execute || $form->dberror($query);
1726 $ref = $sth->fetchrow_hashref(NAME_lc);
1728 map { $form->{$_} = $ref->{$_} } keys %$ref;
1730 #print(STDERR "LEVEL $form->{max_dunning_level}\n");
1733 #check whether payment_terms are better than old payment_terms
1734 if (($form->{payment_id} ne "") && ($form->{customer_payment_id} ne "")) {
1735 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})|;
1736 my $stw = $dbh->prepare($query);
1737 $stw->execute || $form->dberror($query);
1738 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1740 if ($new_ranking > $old_ranking) {
1741 $form->{payment_id} =$form->{customer_payment_id};
1744 if ($form->{payment_id} eq "") {
1745 $form->{payment_id} =$form->{customer_payment_id};
1748 $form->{creditremaining} = $form->{creditlimit};
1749 $query = qq|SELECT SUM(a.amount - a.paid)
1751 WHERE a.customer_id = $form->{customer_id}|;
1752 $sth = $dbh->prepare($query);
1753 $sth->execute || $form->dberror($query);
1755 ($form->{creditremaining}) -= $sth->fetchrow_array;
1759 $query = qq|SELECT o.amount,
1760 (SELECT e.buy FROM exchangerate e
1761 WHERE e.curr = o.curr
1762 AND e.transdate = o.transdate)
1764 WHERE o.customer_id = $form->{customer_id}
1765 AND o.quotation = '0'
1766 AND o.closed = '0'|;
1767 $sth = $dbh->prepare($query);
1768 $sth->execute || $form->dberror($query);
1770 while (my ($amount, $exch) = $sth->fetchrow_array) {
1771 $exch = 1 unless $exch;
1772 $form->{creditremaining} -= $amount * $exch;
1776 # get shipto if we did not converted an order or invoice
1777 if (!$form->{shipto}) {
1778 map { delete $form->{$_} }
1779 qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1781 $query = qq|SELECT s.* FROM shipto s
1782 WHERE s.trans_id = $form->{customer_id} AND s.module = 'CT'|;
1783 $sth = $dbh->prepare($query);
1784 $sth->execute || $form->dberror($query);
1786 $ref = $sth->fetchrow_hashref(NAME_lc);
1788 map { $form->{$_} = $ref->{$_} } keys %$ref;
1792 # get taxes we charge for this customer
1793 $query = qq|SELECT c.accno
1795 JOIN customertax ct ON (ct.chart_id = c.id)
1796 WHERE ct.customer_id = $form->{customer_id}|;
1797 $sth = $dbh->prepare($query);
1798 $sth->execute || $form->dberror($query);
1800 my $customertax = ();
1801 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1802 $customertax{ $ref->{accno} } = 1;
1806 # setup last accounts used for this customer
1807 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1808 $query = qq|SELECT c.accno, c.description, c.link, c.category
1810 JOIN acc_trans ac ON (ac.chart_id = c.id)
1811 JOIN ar a ON (a.id = ac.trans_id)
1812 WHERE a.customer_id = $form->{customer_id}
1813 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1814 AND a.id IN (SELECT max(a2.id) FROM ar a2
1815 WHERE a2.customer_id = $form->{customer_id})|;
1816 $sth = $dbh->prepare($query);
1817 $sth->execute || $form->dberror($query);
1820 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1821 if ($ref->{category} eq 'I') {
1823 $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
1825 if ($ref->{category} eq 'A') {
1826 $form->{ARselected} = $form->{AR_1} =
1827 "$ref->{accno}--$ref->{description}";
1831 $form->{rowcount} = $i if ($i && !$form->{type});
1836 $main::lxdebug->leave_sub();
1840 $main::lxdebug->enter_sub();
1842 my ($self, $myconfig, $form) = @_;
1844 # connect to database
1845 my $dbh = $form->dbconnect($myconfig);
1847 my $i = $form->{rowcount};
1849 my $where = "NOT p.obsolete = '1'";
1851 if ($form->{"partnumber_$i"}) {
1852 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1853 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1855 if ($form->{"description_$i"}) {
1856 my $description = $form->like(lc $form->{"description_$i"});
1857 $where .= " AND lower(p.description) LIKE '$description'";
1860 if ($form->{"partsgroup_$i"}) {
1861 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1862 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1865 if ($form->{"description_$i"}) {
1866 $where .= " ORDER BY p.description";
1868 $where .= " ORDER BY p.partnumber";
1872 if ($form->{type} eq "invoice") {
1874 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1875 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1879 $form->{transdate} ? $dbh->quote($form->{transdate}) :
1883 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1884 p.listprice, p.inventory_accno_id,
1885 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1886 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1887 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1888 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1889 pg.partsgroup, p.formel, p.payment_id AS part_payment_id
1891 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1892 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1893 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1894 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1896 my $sth = $dbh->prepare($query);
1897 $sth->execute || $form->dberror($query);
1899 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1901 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1902 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1903 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1904 if (!$ref->{inventory_accno_id}) {
1905 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1907 delete($ref->{inventory_accno_id});
1909 #set expense_accno=inventory_accno if they are different => bilanz
1912 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1913 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}|;
1914 my $stw = $dbh->prepare($query);
1915 $stw->execute || $form->dberror($query);
1916 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1920 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1921 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}|;
1922 my $stw = $dbh->prepare($query);
1923 $stw->execute || $form->dberror($query);
1924 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1928 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1929 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}|;
1930 my $stw = $dbh->prepare($query);
1931 $stw->execute || $form->dberror($query);
1932 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1936 #check whether payment_terms are better than old payment_terms
1937 if (($form->{payment_id} ne "") && ($ref->{part_payment_id} ne "")) {
1938 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})|;
1939 my $stw = $dbh->prepare($query);
1940 $stw->execute || $form->dberror($query);
1941 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1943 if ($new_ranking <= $old_ranking) {
1944 $ref->{part_payment_id} = "";
1948 # get tax rates and description
1950 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1951 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1952 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1953 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)
1955 $stw = $dbh->prepare($query);
1956 $stw->execute || $form->dberror($query);
1958 $ref->{taxaccounts} = "";
1960 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1962 # if ($customertax{$ref->{accno}}) {
1963 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1967 $ref->{taxaccounts} .= "$ptr->{accno} ";
1969 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1970 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1971 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1972 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1973 $form->{taxaccounts} .= "$ptr->{accno} ";
1979 chop $ref->{taxaccounts};
1980 if ($form->{language_id}) {
1981 $query = qq|SELECT tr.translation, tr.longdescription
1983 WHERE tr.language_id=$form->{language_id} AND tr.parts_id=$ref->{id}|;
1984 $stw = $dbh->prepare($query);
1985 $stw->execute || $form->dberror($query);
1986 my ($translation, $longdescription) = $stw->fetchrow_array();
1987 if ($translation ne "") {
1988 $ref->{description} = $translation;
1989 $ref->{longdescription} = $longdescription;
1992 $query = qq|SELECT tr.translation, tr.longdescription
1994 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|;
1995 $stg = $dbh->prepare($query);
1996 $stg->execute || $form->dberror($query);
1997 my ($translation) = $stg->fetchrow_array();
1998 if ($translation ne "") {
1999 $ref->{description} = $translation;
2000 $ref->{longdescription} = $longdescription;
2007 push @{ $form->{item_list} }, $ref;
2009 if ($form->{lizenzen}) {
2010 if ($ref->{inventory_accno} > 0) {
2012 qq| SELECT l.* FROM license l WHERE l.parts_id = $ref->{id} AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
2013 $stw = $dbh->prepare($query);
2014 $stw->execute || $form->dberror($query);
2015 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
2016 push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
2025 $main::lxdebug->leave_sub();
2028 ##########################
2029 # get pricegroups from database
2030 # build up selected pricegroup
2031 # if an exchange rate - change price
2034 sub get_pricegroups_for_parts {
2036 $main::lxdebug->enter_sub();
2038 my ($self, $myconfig, $form) = @_;
2040 my $dbh = $form->dbconnect($myconfig);
2042 $form->{"PRICES"} = {};
2046 my $dimension_units = AM->retrieve_units($myconfig, $form, "dimension");
2047 my $service_units = AM->retrieve_units($myconfig, $form, "service");
2048 my $all_units = AM->retrieve_units($myconfig, $form);
2049 while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
2050 $form->{"PRICES"}{$i} = [];
2052 $id = $form->{"id_$i"};
2054 if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
2056 $id = $form->{"new_id_$i"};
2059 ($price, $selectedpricegroup_id) = split /--/,
2060 $form->{"sellprice_pg_$i"};
2062 $pricegroup_old = $form->{"pricegroup_old_$i"};
2063 $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
2064 $form->{"old_pricegroup_$i"} = $pricegroup_old;
2065 $price_new = $form->{"price_new_$i"};
2067 $price_old = $form->{"price_old_$i"};
2069 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|;
2071 $pkq = $dbh->prepare($query);
2072 $pkq->execute || $form->dberror($query);
2073 if (!$form->{"unit_old_$i"}) {
2074 # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
2075 # Einheit, wie sie in den Stammdaten hinterlegt wurde.
2076 # Es sollte also angenommen werden, dass diese ausgewaehlt war.
2077 $form->{"unit_old_$i"} = $form->{"unit_$i"};
2080 # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
2081 # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
2082 $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
2084 my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
2085 if (!$check_units->{$form->{"selected_unit_$i"}} ||
2086 ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
2087 $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
2088 # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
2089 # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
2090 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
2091 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
2095 if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
2096 if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
2097 $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
2098 $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
2099 $all_units->{$form->{"unit_old_$i"}}->{"factor"};
2102 if (!$form->{"basefactor_$i"}) {
2103 $form->{"basefactor_$i"} = 1;
2105 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
2107 # push @{ $form->{PRICES}{$id} }, $pkr;
2108 #push @{ $form->{PRICES}{$i} }, $pkr;
2110 $pkr->{selected} = '';
2112 # if there is an exchange rate change price
2113 if (($form->{exchangerate} * 1) != 0) {
2115 $pkr->{price} /= $form->{exchangerate};
2118 $pkr->{price} *= $form->{"basefactor_$i"};
2120 $pkr->{price} *= $basefactor;
2122 $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
2124 if ($selectedpricegroup_id eq undef) {
2125 if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
2127 $pkr->{selected} = ' selected';
2129 # no customer pricesgroup set
2130 if ($pkr->{price} == $pkr->{default_sellprice}) {
2132 $pkr->{price} = $form->{"sellprice_$i"};
2136 $form->{"sellprice_$i"} = $pkr->{price};
2140 if ($pkr->{price} == $pkr->{default_sellprice}) {
2142 $pkr->{price} = $form->{"sellprice_$i"};
2143 $pkr->{selected} = ' selected';
2148 if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
2149 if ($selectedpricegroup_id ne $pricegroup_old) {
2150 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2151 $pkr->{selected} = ' selected';
2154 if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
2155 if ($pkr->{pricegroup_id} == 0) {
2156 $pkr->{price} = $form->{"sellprice_$i"};
2157 $pkr->{selected} = ' selected';
2160 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2161 $pkr->{selected} = ' selected';
2162 if ( ($pkr->{pricegroup_id} == 0)
2163 and ($pkr->{price} == $form->{"sellprice_$i"})) {
2165 # $pkr->{price} = $form->{"sellprice_$i"};
2167 $pkr->{price} = $form->{"sellprice_$i"};
2173 push @{ $form->{PRICES}{$i} }, $pkr;
2176 $form->{"basefactor_$i"} *= $basefactor;
2185 $main::lxdebug->leave_sub();
2189 $main::lxdebug->enter_sub();
2191 my ($myconfig, $form) = @_;
2194 $path = "webdav/rechnungen/" . $form->{invnumber}, last SWITCH
2195 if ($form->{vc} eq "customer");
2196 $path = "webdav/einkaufsrechnungen/" . $form->{invnumber}, last SWITCH
2197 if ($form->{vc} eq "vendor");
2201 mkdir($path, 0770) or die "can't make directory $!\n";
2205 foreach $file (@files) {
2206 $file =~ /\/([^\/]*)$/;
2208 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
2210 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
2211 $form->{WEBDAV}{$fname} = $link;
2216 $main::lxdebug->leave_sub();