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"});
386 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
387 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
388 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
389 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
390 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
391 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
394 ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
396 $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
398 $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
399 $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
400 $form->set_payment_options($myconfig, $form->{invdate});
402 $form->{username} = $myconfig->{name};
406 $main::lxdebug->leave_sub();
409 sub project_description {
410 $main::lxdebug->enter_sub();
412 my ($self, $dbh, $id) = @_;
414 my $query = qq|SELECT p.description
417 my $sth = $dbh->prepare($query);
418 $sth->execute || $form->dberror($query);
420 ($_) = $sth->fetchrow_array;
424 $main::lxdebug->leave_sub();
429 sub customer_details {
430 $main::lxdebug->enter_sub();
432 my ($self, $myconfig, $form, @wanted_vars) = @_;
434 # connect to database
435 my $dbh = $form->dbconnect($myconfig);
437 # get contact id, set it if nessessary
441 if ($form->{cp_id}) {
442 $contact = "and cp.cp_id = $form->{cp_id}";
445 # get rest for the customer
446 my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes, ct.phone AS customerphone, ct.fax AS customerfax, ct.email AS customeremail
448 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
449 WHERE ct.id = $form->{customer_id} $contact order by cp.cp_id limit 1|;
450 my $sth = $dbh->prepare($query);
451 $sth->execute || $form->dberror($query);
453 $ref = $sth->fetchrow_hashref(NAME_lc);
455 # remove id and taxincluded before copy back
456 delete @$ref{qw(id taxincluded)};
458 @wanted_vars = grep({ $_ } @wanted_vars);
459 if (scalar(@wanted_vars) > 0) {
461 map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
462 map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
465 map { $form->{$_} = $ref->{$_} } keys %$ref;
468 if ($form->{delivery_customer_id}) {
469 my $query = qq|SELECT ct.*, ct.notes as customernotes
471 WHERE ct.id = $form->{delivery_customer_id} limit 1|;
472 my $sth = $dbh->prepare($query);
473 $sth->execute || $form->dberror($query);
475 $ref = $sth->fetchrow_hashref(NAME_lc);
478 map { $form->{"dc_$_"} = $ref->{$_} } keys %$ref;
481 if ($form->{delivery_vendor_id}) {
482 my $query = qq|SELECT ct.*, ct.notes as customernotes
484 WHERE ct.id = $form->{delivery_vendor_id} limit 1|;
485 my $sth = $dbh->prepare($query);
486 $sth->execute || $form->dberror($query);
488 $ref = $sth->fetchrow_hashref(NAME_lc);
491 map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
495 $main::lxdebug->leave_sub();
499 $main::lxdebug->enter_sub();
501 my ($self, $myconfig, $form) = @_;
503 # connect to database, turn off autocommit
504 my $dbh = $form->dbconnect_noauto($myconfig);
506 my ($query, $sth, $null, $project_id, $deliverydate);
507 my $exchangerate = 0;
509 ($null, $form->{employee_id}) = split /--/, $form->{employee};
510 unless ($form->{employee_id}) {
511 $form->get_employee($dbh);
514 $form->{payment_id} *= 1;
515 $form->{language_id} *= 1;
516 $form->{taxzone_id} *= 1;
517 $form->{delivery_customer_id} *= 1;
518 $form->{delivery_vendor_id} *= 1;
519 $form->{storno} *= 1;
520 $form->{shipto_id} *= 1;
523 ($null, $form->{department_id}) = split(/--/, $form->{department});
524 $form->{department_id} *= 1;
526 my $service_units = AM->retrieve_units($myconfig,$form,"service");
527 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
533 &reverse_invoice($dbh, $form);
536 my $uid = rand() . time;
538 $uid .= $form->{login};
540 $uid = substr($uid, 2, 75);
542 $query = qq|INSERT INTO ar (invnumber, employee_id)
543 VALUES ('$uid', $form->{employee_id})|;
544 $dbh->do($query) || $form->dberror($query);
546 $query = qq|SELECT a.id FROM ar a
547 WHERE a.invnumber = '$uid'|;
548 $sth = $dbh->prepare($query);
549 $sth->execute || $form->dberror($query);
551 ($form->{id}) = $sth->fetchrow_array;
554 if (!$form->{invnumber}) {
556 $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
557 "cnnumber" : "invnumber", $dbh);
561 map { $form->{$_} =~ s/\'/\'\'/g }
562 (qw(invnumber shippingpoint shipvia notes intnotes message));
564 my ($netamount, $invoicediff) = (0, 0);
565 my ($amount, $linetotal, $lastincomeaccno);
567 if ($form->{currency} eq $form->{defaultcurrency}) {
568 $form->{exchangerate} = 1;
571 $form->check_exchangerate($myconfig, $form->{currency},
572 $form->{transdate}, 'buy');
575 $form->{exchangerate} =
578 : $form->parse_amount($myconfig, $form->{exchangerate});
580 $form->{expense_inventory} = "";
582 foreach my $i (1 .. $form->{rowcount}) {
583 if ($form->{type} eq "credit_note") {
584 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}) * -1;
585 $form->{shipped} = 1;
587 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
592 if ($form->{storno}) {
593 $form->{"qty_$i"} *= -1;
596 if ($form->{"id_$i"}) {
599 $query = qq|SELECT p.unit
601 WHERE p.id = $form->{"id_$i"}|;
602 $sth = $dbh->prepare($query);
603 $sth->execute || $form->dberror($query);
605 my ($item_unit) = $sth->fetchrow_array();
608 if ($form->{"inventory_accno_$i"}) {
609 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
610 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
614 $baseqty = $form->{"qty_$i"} * $basefactor;
616 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
617 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
621 $baseqty = $form->{"qty_$i"} * $basefactor;
624 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
625 (qw(partnumber description unit));
627 # undo discount formatting
628 $form->{"discount_$i"} =
629 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
631 my ($allocated, $taxrate) = (0, 0);
634 # keep entered selling price
636 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
638 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
640 my $decimalplaces = ($dec > 2) ? $dec : 2;
644 $form->round_amount($fxsellprice * $form->{"discount_$i"},
646 $form->{"sellprice_$i"} = $fxsellprice - $discount;
649 map { $taxrate += $form->{"${_}_rate"} } split / /,
650 $form->{"taxaccounts_$i"};
652 # round linetotal to 2 decimal places
654 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
656 if ($form->{taxincluded}) {
657 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
658 $form->{"sellprice_$i"} =
659 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
661 $taxamount = $linetotal * $taxrate;
664 $netamount += $linetotal;
666 if ($taxamount != 0) {
668 $form->{amount}{ $form->{id} }{$_} +=
669 $taxamount * $form->{"${_}_rate"} / $taxrate
670 } split / /, $form->{"taxaccounts_$i"};
673 # add amount to income, $form->{amount}{trans_id}{accno}
675 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
678 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
679 $form->{exchangerate};
680 $linetotal = $form->round_amount($linetotal, 2);
682 # this is the difference from the inventory
683 $invoicediff += ($amount - $linetotal);
685 $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
688 $lastincomeaccno = $form->{"income_accno_$i"};
690 # adjust and round sellprice
691 $form->{"sellprice_$i"} =
692 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
695 if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
697 # adjust parts onhand quantity
699 if ($form->{"assembly_$i"}) {
701 # do not update if assembly consists of all services
702 $query = qq|SELECT sum(p.inventory_accno_id)
704 JOIN assembly a ON (a.parts_id = p.id)
705 WHERE a.id = $form->{"id_$i"}|;
706 $sth = $dbh->prepare($query);
707 $sth->execute || $form->dberror($query);
709 if ($sth->fetchrow_array) {
710 $form->update_balance($dbh, "parts", "onhand",
711 qq|id = $form->{"id_$i"}|,
713 unless $form->{shipped};
717 # record assembly item as allocated
718 &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
720 $form->update_balance($dbh, "parts", "onhand",
721 qq|id = $form->{"id_$i"}|,
723 unless $form->{shipped};
725 $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
730 ($form->{"deliverydate_$i"})
731 ? qq|'$form->{"deliverydate_$i"}'|
734 # get pricegroup_id and save it
735 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
737 my $subtotal = $form->{"subtotal_$i"} * 1;
739 # save detail record in invoice table
740 $query = qq|INSERT INTO invoice (trans_id, parts_id, description,longdescription, qty,
741 sellprice, fxsellprice, discount, allocated, assemblyitem,
742 unit, deliverydate, project_id, serialnumber, pricegroup_id,
743 ordnumber, transdate, cusordnumber, base_qty, subtotal)
744 VALUES ($form->{id}, $form->{"id_$i"},
745 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"},
746 $form->{"sellprice_$i"}, $fxsellprice,
747 $form->{"discount_$i"}, $allocated, 'f',
748 '$form->{"unit_$i"}', $deliverydate, | . conv_i($form->{"project_id_$i"}, 'NULL') . qq|,
749 '$form->{"serialnumber_$i"}', '$pricegroup_id',
750 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', $baseqty, '$subtotal')|;
751 $dbh->do($query) || $form->dberror($query);
753 if ($form->{lizenzen}) {
754 if ($form->{"licensenumber_$i"}) {
756 qq|SELECT i.id FROM invoice i WHERE i.trans_id=$form->{id} ORDER BY i.oid DESC LIMIT 1|;
757 $sth = $dbh->prepare($query);
758 $sth->execute || $form->dberror($query);
760 ($invoice_row_id) = $sth->fetchrow_array;
764 qq|INSERT INTO licenseinvoice (trans_id, license_id) VALUES ($invoice_row_id, $form->{"licensenumber_$i"})|;
765 $dbh->do($query) || $form->dberror($query);
772 $form->{datepaid} = $form->{invdate};
774 # total payments, don't move we need it here
775 for my $i (1 .. $form->{paidaccounts}) {
776 if ($form->{type} eq "credit_note") {
777 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
779 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
781 $form->{paid} += $form->{"paid_$i"};
782 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
785 my ($tax, $diff) = (0, 0);
787 $netamount = $form->round_amount($netamount, 2);
789 # figure out rounding errors for total amount vs netamount + taxes
790 if ($form->{taxincluded}) {
792 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
793 $diff += $amount - $netamount * $form->{exchangerate};
794 $netamount = $amount;
796 foreach my $item (split / /, $form->{taxaccounts}) {
797 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
798 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
799 $tax += $form->{amount}{ $form->{id} }{$item};
800 $netamount -= $form->{amount}{ $form->{id} }{$item};
803 $invoicediff += $diff;
804 ######## this only applies to tax included
805 if ($lastincomeaccno) {
806 $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
810 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
811 $diff = $amount - $netamount * $form->{exchangerate};
812 $netamount = $amount;
813 foreach my $item (split / /, $form->{taxaccounts}) {
814 $form->{amount}{ $form->{id} }{$item} =
815 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
818 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
821 $amount - $form->{amount}{ $form->{id} }{$item} *
822 $form->{exchangerate};
823 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
824 $tax += $form->{amount}{ $form->{id} }{$item};
828 $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
830 $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
833 $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
835 # update exchangerate
836 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
837 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
838 $form->{exchangerate}, 0);
841 $project_id = conv_i($form->{"globalproject_id"});
843 foreach my $trans_id (keys %{ $form->{amount} }) {
844 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
845 next unless ($form->{expense_inventory} =~ /$accno/);
847 ($form->{amount}{$trans_id}{$accno} =
848 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
851 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
852 transdate, taxkey, project_id)
853 VALUES ($trans_id, (SELECT c.id FROM chart c
854 WHERE c.accno = '$accno'),
855 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
856 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
857 do_query($form, $dbh, $query, $project_id);
858 $form->{amount}{$trans_id}{$accno} = 0;
862 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
864 ($form->{amount}{$trans_id}{$accno} =
865 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
868 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
869 transdate, taxkey, project_id)
870 VALUES ($trans_id, (SELECT id FROM chart
871 WHERE accno = '$accno'),
872 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
873 (SELECT taxkey_id FROM chart WHERE accno = '$accno'), ?)|;
874 do_query($form, $dbh, $query, $project_id);
879 # deduct payment differences from diff
880 for my $i (1 .. $form->{paidaccounts}) {
881 if ($form->{"paid_$i"} != 0) {
883 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
884 $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
888 # force AR entry if 0
889 # $form->{amount}{$form->{id}}{$form->{AR}} = 1 if ($form->{amount}{$form->{id}}{$form->{AR}} == 0);
891 # record payments and offsetting AR
892 if (!$form->{storno}) {
893 for my $i (1 .. $form->{paidaccounts}) {
895 if ($form->{"paid_$i"} != 0) {
896 my ($accno) = split /--/, $form->{"AR_paid_$i"};
897 $form->{"datepaid_$i"} = $form->{invdate}
898 unless ($form->{"datepaid_$i"});
899 $form->{datepaid} = $form->{"datepaid_$i"};
903 if ($form->{currency} eq $form->{defaultcurrency}) {
904 $form->{"exchangerate_$i"} = 1;
907 $form->check_exchangerate($myconfig, $form->{currency},
908 $form->{"datepaid_$i"}, 'buy');
910 $form->{"exchangerate_$i"} =
913 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
918 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
921 if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
922 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
923 transdate, taxkey, project_id)
924 VALUES ($form->{id}, (SELECT c.id FROM chart c
926 $amount, '$form->{"datepaid_$i"}',
927 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
928 do_query($form, $dbh, $query, $form->{AR}, $form->{AR}, $project_id);
932 $form->{"paid_$i"} *= -1;
934 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
935 source, memo, taxkey, project_id)
936 VALUES ($form->{id}, (SELECT c.id FROM chart c
938 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
939 '$form->{"source_$i"}', '$form->{"memo_$i"}',
940 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
941 do_query($form, $dbh, $query, $accno, $accno, $project_id);
943 # exchangerate difference
944 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
945 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
949 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
950 $form->{"exchangerate_$i"};
952 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
955 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
961 # update exchange rate
962 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
963 $form->update_exchangerate($dbh, $form->{currency},
964 $form->{"datepaid_$i"},
965 $form->{"exchangerate_$i"}, 0);
971 # record exchange rate differences and gains/losses
972 foreach my $accno (keys %{ $form->{fx} }) {
973 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
975 ($form->{fx}{$accno}{$transdate} =
976 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
980 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
981 transdate, cleared, fx_transaction, taxkey, project_id)
983 (SELECT c.id FROM chart c
985 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1',
986 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
987 do_query($form, $dbh, $query, $accno, $accno, $project_id);
992 $amount = $netamount + $tax;
994 # set values which could be empty to 0
996 $form->{taxincluded} *= 1;
997 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
998 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
1000 ($form->{deliverydate}) ? qq|'$form->{deliverydate}'| : "NULL";
1002 # fill in subject if there is none
1003 $form->{subject} = qq|$form->{label} $form->{invnumber}|
1004 unless $form->{subject};
1006 # if there is a message stuff it into the intnotes
1007 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
1008 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
1009 my $now = scalar localtime;
1010 $form->{intnotes} .= qq|\r
1011 \r| if $form->{intnotes};
1013 $form->{intnotes} .= qq|[email]\r
1015 To: $form->{email}\r
1016 $cc${bcc}Subject: $form->{subject}\r
1018 Message: $form->{message}\r| if $form->{message};
1021 $query = qq|UPDATE ar set
1022 invnumber = '$form->{invnumber}',
1023 ordnumber = '$form->{ordnumber}',
1024 quonumber = '$form->{quonumber}',
1025 cusordnumber = '$form->{cusordnumber}',
1026 transdate = '$form->{invdate}',
1027 orddate = | . conv_dateq($form->{orddate}) . qq|,
1028 quodate = | . conv_dateq($form->{quodate}) . qq|,
1029 customer_id = $form->{customer_id},
1031 netamount = $netamount,
1032 paid = $form->{paid},
1033 datepaid = $datepaid,
1035 deliverydate = $deliverydate,
1037 shippingpoint = '$form->{shippingpoint}',
1038 shipvia = '$form->{shipvia}',
1039 terms = $form->{terms},
1040 notes = '$form->{notes}',
1041 intnotes = '$form->{intnotes}',
1042 taxincluded = '$form->{taxincluded}',
1043 curr = '$form->{currency}',
1044 department_id = $form->{department_id},
1045 payment_id = $form->{payment_id},
1046 type = '$form->{type}',
1047 language_id = $form->{language_id},
1048 taxzone_id = $form->{taxzone_id},
1049 shipto_id = $form->{shipto_id},
1050 delivery_customer_id = $form->{delivery_customer_id},
1051 delivery_vendor_id = $form->{delivery_vendor_id},
1052 employee_id = $form->{employee_id},
1053 salesman_id = | . conv_i($form->{salesman_id}, 'NULL') . qq|,
1054 storno = '$form->{storno}',
1055 globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
1056 cp_id = | . conv_i($form->{"cp_id"}, 'NULL') . qq|
1057 WHERE id = $form->{id}
1059 $dbh->do($query) || $form->dberror($query);
1061 if ($form->{storno}) {
1062 $query = qq| update ar set paid=paid+amount where id=$form->{storno_id}|;
1063 $dbh->do($query) || $form->dberror($query);
1064 $query = qq| update ar set storno='$form->{storno}' where id=$form->{storno_id}|;
1065 $dbh->do($query) || $form->dberror($query);
1066 $query = qq§ update ar set intnotes='Rechnung storniert am $form->{invdate} ' || intnotes where id=$form->{storno_id}§;
1067 $dbh->do($query) || $form->dberror($query);
1069 $query = qq| update ar set paid=amount where id=$form->{id}|;
1070 $dbh->do($query) || $form->dberror($query);
1073 $form->{pago_total} = $amount;
1076 $form->{name} = $form->{customer};
1077 $form->{name} =~ s/--$form->{customer_id}//;
1079 if (!$form->{shipto_id}) {
1080 $form->add_shipto($dbh, $form->{id}, "AR");
1083 # save printed, emailed and queued
1084 $form->save_status($dbh);
1086 Common::webdav_folder($form) if ($main::webdav);
1088 my $rc = $dbh->commit;
1091 $main::lxdebug->leave_sub();
1097 $main::lxdebug->enter_sub() and my ($self, $myconfig, $form, $locale) = @_;
1099 # connect to database, turn off autocommit
1100 my $dbh = $form->dbconnect_noauto($myconfig);
1102 $form->{datepaid} = $form->{invdate};
1104 # total payments, don't move we need it here
1105 for my $i (1 .. $form->{paidaccounts}) {
1106 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
1107 $form->{"paid_$i"} *= -1 if ($form->{type} eq "credit_note");
1108 $form->{"paid"} += $form->{"paid_$i"};
1109 $form->{"datepaid"} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
1112 $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
1114 # record payments and offsetting AR
1115 for my $i (1 .. $form->{paidaccounts}) {
1116 if ($form->{"paid_$i"}) {
1118 my ($accno) = split /--/, $form->{"AR_paid_$i"};
1119 $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
1120 $form->{datepaid} = $form->{"datepaid_$i"};
1123 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1124 $form->{"exchangerate_$i"} = 1;
1126 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
1127 $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1131 $amount = $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"}, 2);
1133 $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 = ?|;
1134 do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, $form->{"datepaid_$i"});
1135 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
1136 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1137 do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, $form->{"datepaid_$i"}, conv_i($form->{"globalproject_id"}), $accno);
1140 $form->{"paid_$i"} *= -1;
1142 $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 = ?|;
1143 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"});
1144 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey)
1145 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1146 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"},
1147 conv_i($form->{"globalproject_id"}), $accno);
1150 $amount = $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} * $form->{"exchangerate_$i"};
1151 $form->{fx}{ $form->{($amount > 0 ? 'fxgain_accno' : 'fxloss_accno')} }{ $form->{"datepaid_$i"} } += $amount;
1155 # update exchange rate
1156 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1157 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
1163 # record exchange rate differences and gains/losses
1164 foreach my $accno (keys %{ $form->{fx} }) {
1165 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1167 if ($form->{fx}{$accno}{$transdate} = $form->round_amount($form->{fx}{$accno}{$transdate}, 2)) { # '=' is no typo, it's an assignment
1168 $query = qq|DELETE FROM acc_trans WHERE trans_id = ? AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)
1169 AND amount = ? AND transdate = ? AND cleared = ? AND fx_transaction = ?|;
1170 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1);
1171 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey)
1172 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1173 do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1, conv_i($form->{"globalproject_id"}), $accno);
1180 delete $form->{datepaid} unless $form->{paid};
1182 my $query = qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|;
1183 do_query($form, $dbh, $query, $form->{paid}, $form->{datepaid}, $form->{id});
1185 my $rc = $dbh->commit;
1188 $main::lxdebug->leave_sub() and return $rc;
1191 sub process_assembly {
1192 $main::lxdebug->enter_sub();
1194 my ($dbh, $form, $id, $totalqty) = @_;
1196 my $query = qq|SELECT a.parts_id, a.qty, p.assembly,
1197 p.partnumber, p.description, p.unit,
1198 p.inventory_accno_id, p.income_accno_id,
1201 JOIN parts p ON (a.parts_id = p.id)
1203 my $sth = $dbh->prepare($query);
1204 $sth->execute || $form->dberror($query);
1206 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1210 $ref->{inventory_accno_id} *= 1;
1211 $ref->{expense_accno_id} *= 1;
1213 map { $ref->{$_} =~ s/\'/\'\'/g } (qw(partnumber description unit));
1215 # multiply by number of assemblies
1216 $ref->{qty} *= $totalqty;
1218 if ($ref->{assembly}) {
1219 &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
1222 if ($ref->{inventory_accno_id}) {
1223 $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
1227 # save detail record for individual assembly item in invoice table
1228 $query = qq|INSERT INTO invoice (trans_id, description, parts_id, qty,
1229 sellprice, fxsellprice, allocated, assemblyitem, unit)
1231 ($form->{id}, '$ref->{description}',
1232 $ref->{parts_id}, $ref->{qty}, 0, 0, $allocated, 't',
1234 $dbh->do($query) || $form->dberror($query);
1240 $main::lxdebug->leave_sub();
1244 $main::lxdebug->enter_sub();
1246 my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
1247 $form->{taxzone_id} *=1;
1248 my $transdate = ($form->{invdate}) ? "'$form->{invdate}'" : "current_date";
1249 my $query = qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
1250 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1251 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1252 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid
1253 FROM invoice i, parts p
1254 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1255 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1256 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1257 WHERE i.parts_id = p.id
1258 AND i.parts_id = $id
1259 AND (i.base_qty + i.allocated) < 0
1261 my $sth = $dbh->prepare($query);
1262 $sth->execute || $form->dberror($query);
1267 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1268 if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
1272 $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|,
1275 # total expenses and inventory
1276 # sellprice is the cost of the item
1277 $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
1280 $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
1282 $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
1283 $form->{expense_inventory} .= " " . $ref->{expense_accno};
1284 $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
1286 $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
1287 $form->{expense_inventory} .= " " . $ref->{inventory_accno};
1291 $allocated += -$qty;
1293 last if (($totalqty -= $qty) <= 0);
1298 $main::lxdebug->leave_sub();
1303 sub reverse_invoice {
1304 $main::lxdebug->enter_sub();
1306 my ($dbh, $form) = @_;
1308 # reverse inventory items
1309 my $query = qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
1310 p.inventory_accno_id
1312 JOIN parts p ON (i.parts_id = p.id)
1313 WHERE i.trans_id = $form->{id}|;
1314 my $sth = $dbh->prepare($query);
1315 $sth->execute || $form->dberror($query);
1317 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1319 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1321 # if the invoice item is not an assemblyitem adjust parts onhand
1322 if (!$ref->{assemblyitem}) {
1324 # adjust onhand in parts table
1325 $form->update_balance($dbh, "parts", "onhand",
1326 qq|id = $ref->{parts_id}|,
1330 # loop if it is an assembly
1331 next if ($ref->{assembly});
1333 # de-allocated purchases
1334 $query = qq|SELECT i.id, i.trans_id, i.allocated
1336 WHERE i.parts_id = $ref->{parts_id}
1338 ORDER BY i.trans_id DESC|;
1339 my $sth = $dbh->prepare($query);
1340 $sth->execute || $form->dberror($query);
1342 while (my $inhref = $sth->fetchrow_hashref(NAME_lc)) {
1344 if (($ref->{qty} - $inhref->{allocated}) > 0) {
1345 $qty = $inhref->{allocated};
1349 $form->update_balance($dbh, "invoice", "allocated",
1350 qq|id = $inhref->{id}|,
1353 last if (($ref->{qty} -= $qty) <= 0);
1362 $query = qq|DELETE FROM acc_trans
1363 WHERE trans_id = $form->{id}|;
1364 $dbh->do($query) || $form->dberror($query);
1366 # delete invoice entries
1367 $query = qq|DELETE FROM invoice
1368 WHERE trans_id = $form->{id}|;
1369 $dbh->do($query) || $form->dberror($query);
1371 if ($form->{lizenzen}) {
1372 $query = qq|DELETE FROM licenseinvoice
1373 WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = $form->{id})|;
1374 $dbh->do($query) || $form->dberror($query);
1377 $query = qq|DELETE FROM shipto
1378 WHERE trans_id = $form->{id} AND module = 'AR'|;
1379 $dbh->do($query) || $form->dberror($query);
1381 $main::lxdebug->leave_sub();
1384 sub delete_invoice {
1385 $main::lxdebug->enter_sub();
1387 my ($self, $myconfig, $form, $spool) = @_;
1389 # connect to database
1390 my $dbh = $form->dbconnect_noauto($myconfig);
1392 &reverse_invoice($dbh, $form);
1395 my $query = qq|DELETE FROM ar
1396 WHERE id = $form->{id}|;
1397 $dbh->do($query) || $form->dberror($query);
1399 # delete spool files
1400 $query = qq|SELECT s.spoolfile FROM status s
1401 WHERE s.trans_id = $form->{id}|;
1402 my $sth = $dbh->prepare($query);
1403 $sth->execute || $self->dberror($query);
1406 my @spoolfiles = ();
1408 while (($spoolfile) = $sth->fetchrow_array) {
1409 push @spoolfiles, $spoolfile;
1413 # delete status entries
1414 $query = qq|DELETE FROM status
1415 WHERE trans_id = $form->{id}|;
1416 $dbh->do($query) || $form->dberror($query);
1418 my $rc = $dbh->commit;
1422 foreach $spoolfile (@spoolfiles) {
1423 unlink "$spool/$spoolfile" if $spoolfile;
1427 $main::lxdebug->leave_sub();
1432 sub retrieve_invoice {
1433 $main::lxdebug->enter_sub();
1435 my ($self, $myconfig, $form) = @_;
1437 # connect to database
1438 my $dbh = $form->dbconnect_noauto($myconfig);
1444 # get default accounts and last invoice number
1445 $query = qq|SELECT (SELECT c.accno FROM chart c
1446 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1447 (SELECT c.accno FROM chart c
1448 WHERE d.income_accno_id = c.id) AS income_accno,
1449 (SELECT c.accno FROM chart c
1450 WHERE d.expense_accno_id = c.id) AS expense_accno,
1451 (SELECT c.accno FROM chart c
1452 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1453 (SELECT c.accno FROM chart c
1454 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1455 d.curr AS currencies
1458 $query = qq|SELECT (SELECT c.accno FROM chart c
1459 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1460 (SELECT c.accno FROM chart c
1461 WHERE d.income_accno_id = c.id) AS income_accno,
1462 (SELECT c.accno FROM chart c
1463 WHERE d.expense_accno_id = c.id) AS expense_accno,
1464 (SELECT c.accno FROM chart c
1465 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1466 (SELECT c.accno FROM chart c
1467 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1468 d.curr AS currencies, current_date AS invdate
1471 my $sth = $dbh->prepare($query);
1472 $sth->execute || $form->dberror($query);
1474 my $ref = $sth->fetchrow_hashref(NAME_lc);
1475 map { $form->{$_} = $ref->{$_} } keys %$ref;
1481 $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
1482 a.orddate, a.quodate, a.globalproject_id,
1483 a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
1484 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
1485 a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
1486 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
1488 LEFT JOIN employee e ON (e.id = a.employee_id)
1489 WHERE a.id = $form->{id}|;
1490 $sth = $dbh->prepare($query);
1491 $sth->execute || $form->dberror($query);
1493 $ref = $sth->fetchrow_hashref(NAME_lc);
1494 map { $form->{$_} = $ref->{$_} } keys %$ref;
1497 $form->{exchangerate} =
1498 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1501 $query = qq|SELECT s.* FROM shipto s
1502 WHERE s.trans_id = $form->{id} AND s.module = 'AR'|;
1503 $sth = $dbh->prepare($query);
1504 $sth->execute || $form->dberror($query);
1506 $ref = $sth->fetchrow_hashref(NAME_lc);
1508 map { $form->{$_} = $ref->{$_} } keys %$ref;
1511 if ($form->{delivery_customer_id}) {
1512 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
1513 $sth = $dbh->prepare($query);
1514 $sth->execute || $form->dberror($query);
1515 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
1519 if ($form->{delivery_vendor_id}) {
1520 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
1521 $sth = $dbh->prepare($query);
1522 $sth->execute || $form->dberror($query);
1523 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
1527 # get printed, emailed
1528 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
1530 WHERE s.trans_id = $form->{id}|;
1531 $sth = $dbh->prepare($query);
1532 $sth->execute || $form->dberror($query);
1534 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1535 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
1536 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
1537 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
1538 if $ref->{spoolfile};
1541 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
1544 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1545 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1548 if (!$form->{taxzone_id}) {
1549 $form->{taxzone_id} = 0;
1551 # retrieve individual items
1553 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1554 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1555 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1556 i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice,
1557 i.discount, i.parts_id AS id, i.unit, i.deliverydate,
1558 i.project_id, pr.projectnumber, i.serialnumber,
1559 p.partnumber, p.assembly, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, i.id AS invoice_pos,
1560 pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup,
1561 i.ordnumber, i.transdate, i.cusordnumber, p.formel, i.subtotal
1563 JOIN parts p ON (i.parts_id = p.id)
1564 LEFT JOIN project pr ON (i.project_id = pr.id)
1565 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1566 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1567 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1568 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1569 WHERE i.trans_id = $form->{id}
1570 AND NOT i.assemblyitem = '1'
1572 $sth = $dbh->prepare($query);
1574 $sth->execute || $form->dberror($query);
1575 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1576 if (!$ref->{"part_inventory_accno_id"}) {
1577 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
1579 delete($ref->{"part_inventory_accno_id"});
1581 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1582 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}|;
1583 my $stw = $dbh->prepare($query);
1584 $stw->execute || $form->dberror($query);
1585 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1589 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1590 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}|;
1591 my $stw = $dbh->prepare($query);
1592 $stw->execute || $form->dberror($query);
1593 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1597 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1598 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}|;
1599 my $stw = $dbh->prepare($query);
1600 $stw->execute || $form->dberror($query);
1601 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1605 # get tax rates and description
1607 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1608 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1609 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1610 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)
1612 $stw = $dbh->prepare($query);
1613 $stw->execute || $form->dberror($query);
1614 $ref->{taxaccounts} = "";
1616 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1618 # if ($customertax{$ref->{accno}}) {
1619 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1623 $ref->{taxaccounts} .= "$ptr->{accno} ";
1625 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1626 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1627 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1628 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1629 $form->{taxaccounts} .= "$ptr->{accno} ";
1634 if ($form->{lizenzen}) {
1635 $query = qq|SELECT l.licensenumber, l.id AS licenseid
1636 FROM license l, licenseinvoice li
1637 WHERE l.id = li.license_id AND li.trans_id = $ref->{invoice_pos}|;
1638 $stg = $dbh->prepare($query);
1639 $stg->execute || $form->dberror($query);
1640 ($licensenumber, $licenseid) = $stg->fetchrow_array();
1642 "<option value=\"$licenseid\">$licensenumber</option>";
1645 if ($form->{type} eq "credit_note") {
1649 chop $ref->{taxaccounts};
1650 push @{ $form->{invoice_details} }, $ref;
1655 Common::webdav_folder($form) if ($main::webdav);
1658 my $rc = $dbh->commit;
1661 $main::lxdebug->leave_sub();
1667 $main::lxdebug->enter_sub();
1669 my ($self, $myconfig, $form) = @_;
1671 # connect to database
1672 my $dbh = $form->dbconnect($myconfig);
1674 my $dateformat = $myconfig->{dateformat};
1675 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1679 ? "to_date('$form->{invdate}', '$dateformat')"
1682 $form->{customer_id} *= 1;
1685 my $query = qq|SELECT c.name AS customer, c.discount, c.creditlimit, c.terms,
1686 c.email, c.cc, c.bcc, c.language_id, c.payment_id AS customer_payment_id,
1687 c.street, c.zipcode, c.city, c.country,
1688 $duedate + COALESCE(pt.terms_netto, 0) AS duedate, c.notes AS intnotes,
1689 b.discount AS tradediscount, b.description AS business, c.klass as customer_klass, c.taxzone_id,
1692 LEFT JOIN business b ON (b.id = c.business_id)
1693 LEFT JOIN payment_terms pt ON c.payment_id = pt.id
1695 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1696 map { $form->{$_} = $ref->{$_} } keys %$ref;
1698 my $query = qq|SELECT sum(a.amount - a.paid) AS dunning_amount FROM ar a
1699 WHERE a.paid < a.amount AND a.customer_id = ? AND a.dunning_id IS NOT NULL|;
1700 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1701 map { $form->{$_} = $ref->{$_} } keys %$ref;
1703 my $query = qq|SELECT dnn.dunning_description AS max_dunning_level FROM dunning_config dnn
1704 WHERE id in (SELECT dunning_id from ar WHERE paid < amount AND customer_id = ? AND dunning_id IS NOT NULL)
1705 ORDER BY dunning_level DESC LIMIT 1|;
1706 $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{customer_id});
1707 map { $form->{$_} = $ref->{$_} } keys %$ref;
1709 #check whether payment_terms are better than old payment_terms
1710 if (($form->{payment_id} ne "") && ($form->{customer_payment_id} ne "")) {
1711 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})|;
1712 my $stw = $dbh->prepare($query);
1713 $stw->execute || $form->dberror($query);
1714 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1716 if ($new_ranking > $old_ranking) {
1717 $form->{payment_id} =$form->{customer_payment_id};
1720 if ($form->{payment_id} eq "") {
1721 $form->{payment_id} =$form->{customer_payment_id};
1724 $form->{creditremaining} = $form->{creditlimit};
1725 $query = qq|SELECT SUM(a.amount - a.paid)
1727 WHERE a.customer_id = $form->{customer_id}|;
1728 $sth = $dbh->prepare($query);
1729 $sth->execute || $form->dberror($query);
1731 ($form->{creditremaining}) -= $sth->fetchrow_array;
1735 $query = qq|SELECT o.amount,
1736 (SELECT e.buy FROM exchangerate e
1737 WHERE e.curr = o.curr
1738 AND e.transdate = o.transdate)
1740 WHERE o.customer_id = $form->{customer_id}
1741 AND o.quotation = '0'
1742 AND o.closed = '0'|;
1743 $sth = $dbh->prepare($query);
1744 $sth->execute || $form->dberror($query);
1746 while (my ($amount, $exch) = $sth->fetchrow_array) {
1747 $exch = 1 unless $exch;
1748 $form->{creditremaining} -= $amount * $exch;
1752 # get shipto if we did not converted an order or invoice
1753 if (!$form->{shipto}) {
1754 map { delete $form->{$_} }
1755 qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1757 $query = qq|SELECT s.* FROM shipto s
1758 WHERE s.trans_id = $form->{customer_id} AND s.module = 'CT'|;
1759 $sth = $dbh->prepare($query);
1760 $sth->execute || $form->dberror($query);
1762 $ref = $sth->fetchrow_hashref(NAME_lc);
1764 map { $form->{$_} = $ref->{$_} } keys %$ref;
1768 # get taxes we charge for this customer
1769 $query = qq|SELECT c.accno
1771 JOIN customertax ct ON (ct.chart_id = c.id)
1772 WHERE ct.customer_id = $form->{customer_id}|;
1773 $sth = $dbh->prepare($query);
1774 $sth->execute || $form->dberror($query);
1776 my $customertax = ();
1777 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1778 $customertax{ $ref->{accno} } = 1;
1782 # setup last accounts used for this customer
1783 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1784 $query = qq|SELECT c.id, c.accno, c.description, c.link, c.category
1786 JOIN acc_trans ac ON (ac.chart_id = c.id)
1787 JOIN ar a ON (a.id = ac.trans_id)
1788 WHERE a.customer_id = $form->{customer_id}
1789 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1790 AND a.id IN (SELECT max(a2.id) FROM ar a2
1791 WHERE a2.customer_id = $form->{customer_id})|;
1792 $sth = $dbh->prepare($query);
1793 $sth->execute || $form->dberror($query);
1796 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1797 if ($ref->{category} eq 'I') {
1799 $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
1801 if ($form->{initial_transdate}) {
1803 qq|SELECT tk.tax_id, t.rate FROM taxkeys tk | .
1804 qq|LEFT JOIN tax t ON tk.tax_id = t.id | .
1805 qq|WHERE tk.chart_id = ? AND startdate <= ? | .
1806 qq|ORDER BY tk.startdate DESC LIMIT 1|;
1807 my ($tax_id, $rate) =
1808 selectrow_query($form, $dbh, $tax_query, $ref->{id},
1809 $form->{initial_transdate});
1810 $form->{"taxchart_$i"} = "${tax_id}--${rate}";
1813 if ($ref->{category} eq 'A') {
1814 $form->{ARselected} = $form->{AR_1} = $ref->{accno};
1818 $form->{rowcount} = $i if ($i && !$form->{type});
1823 $main::lxdebug->leave_sub();
1827 $main::lxdebug->enter_sub();
1829 my ($self, $myconfig, $form) = @_;
1831 # connect to database
1832 my $dbh = $form->dbconnect($myconfig);
1834 my $i = $form->{rowcount};
1836 my $where = "NOT p.obsolete = '1'";
1838 if ($form->{"partnumber_$i"}) {
1839 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1840 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1842 if ($form->{"description_$i"}) {
1843 my $description = $form->like(lc $form->{"description_$i"});
1844 $where .= " AND lower(p.description) LIKE '$description'";
1847 if ($form->{"partsgroup_$i"}) {
1848 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1849 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1852 if ($form->{"description_$i"}) {
1853 $where .= " ORDER BY p.description";
1855 $where .= " ORDER BY p.partnumber";
1859 if ($form->{type} eq "invoice") {
1861 $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1862 $form->{invdate} ? $dbh->quote($form->{invdate}) :
1866 $form->{transdate} ? $dbh->quote($form->{transdate}) :
1870 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1871 p.listprice, p.inventory_accno_id,
1872 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
1873 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
1874 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
1875 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
1876 pg.partsgroup, p.formel, p.payment_id AS part_payment_id
1878 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
1879 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
1880 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
1881 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1883 my $sth = $dbh->prepare($query);
1884 $sth->execute || $form->dberror($query);
1886 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1888 # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1889 # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1890 # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1891 if (!$ref->{inventory_accno_id}) {
1892 map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1894 delete($ref->{inventory_accno_id});
1896 #set expense_accno=inventory_accno if they are different => bilanz
1899 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
1900 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}|;
1901 my $stw = $dbh->prepare($query);
1902 $stw->execute || $form->dberror($query);
1903 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
1907 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
1908 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}|;
1909 my $stw = $dbh->prepare($query);
1910 $stw->execute || $form->dberror($query);
1911 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
1915 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
1916 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}|;
1917 my $stw = $dbh->prepare($query);
1918 $stw->execute || $form->dberror($query);
1919 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
1923 #check whether payment_terms are better than old payment_terms
1924 if (($form->{payment_id} ne "") && ($ref->{part_payment_id} ne "")) {
1925 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})|;
1926 my $stw = $dbh->prepare($query);
1927 $stw->execute || $form->dberror($query);
1928 ($old_ranking, $new_ranking) = $stw->fetchrow_array;
1930 if ($new_ranking <= $old_ranking) {
1931 $ref->{part_payment_id} = "";
1935 # get tax rates and description
1937 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1938 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1939 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
1940 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)
1942 $stw = $dbh->prepare($query);
1943 $stw->execute || $form->dberror($query);
1945 $ref->{taxaccounts} = "";
1947 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1949 # if ($customertax{$ref->{accno}}) {
1950 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1954 $ref->{taxaccounts} .= "$ptr->{accno} ";
1956 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1957 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1958 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1959 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1960 $form->{taxaccounts} .= "$ptr->{accno} ";
1966 chop $ref->{taxaccounts};
1967 if ($form->{language_id}) {
1968 $query = qq|SELECT tr.translation, tr.longdescription
1970 WHERE tr.language_id=$form->{language_id} AND tr.parts_id=$ref->{id}|;
1971 $stw = $dbh->prepare($query);
1972 $stw->execute || $form->dberror($query);
1973 my ($translation, $longdescription) = $stw->fetchrow_array();
1974 if ($translation ne "") {
1975 $ref->{description} = $translation;
1976 $ref->{longdescription} = $longdescription;
1979 $query = qq|SELECT tr.translation, tr.longdescription
1981 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|;
1982 $stg = $dbh->prepare($query);
1983 $stg->execute || $form->dberror($query);
1984 my ($translation) = $stg->fetchrow_array();
1985 if ($translation ne "") {
1986 $ref->{description} = $translation;
1987 $ref->{longdescription} = $longdescription;
1994 push @{ $form->{item_list} }, $ref;
1996 if ($form->{lizenzen}) {
1997 if ($ref->{inventory_accno} > 0) {
1999 qq| SELECT l.* FROM license l WHERE l.parts_id = $ref->{id} AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
2000 $stw = $dbh->prepare($query);
2001 $stw->execute || $form->dberror($query);
2002 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
2003 push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
2012 $main::lxdebug->leave_sub();
2015 ##########################
2016 # get pricegroups from database
2017 # build up selected pricegroup
2018 # if an exchange rate - change price
2021 sub get_pricegroups_for_parts {
2023 $main::lxdebug->enter_sub();
2025 my ($self, $myconfig, $form) = @_;
2027 my $dbh = $form->dbconnect($myconfig);
2029 $form->{"PRICES"} = {};
2033 my $dimension_units = AM->retrieve_units($myconfig, $form, "dimension");
2034 my $service_units = AM->retrieve_units($myconfig, $form, "service");
2035 my $all_units = AM->retrieve_units($myconfig, $form);
2036 while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
2037 $form->{"PRICES"}{$i} = [];
2039 $id = $form->{"id_$i"};
2041 if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
2043 $id = $form->{"new_id_$i"};
2046 ($price, $selectedpricegroup_id) = split /--/,
2047 $form->{"sellprice_pg_$i"};
2049 $pricegroup_old = $form->{"pricegroup_old_$i"};
2050 $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
2051 $form->{"old_pricegroup_$i"} = $pricegroup_old;
2052 $price_new = $form->{"price_new_$i"};
2054 $price_old = $form->{"price_old_$i"};
2056 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|;
2058 $pkq = $dbh->prepare($query);
2059 $pkq->execute || $form->dberror($query);
2060 if (!$form->{"unit_old_$i"}) {
2061 # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
2062 # Einheit, wie sie in den Stammdaten hinterlegt wurde.
2063 # Es sollte also angenommen werden, dass diese ausgewaehlt war.
2064 $form->{"unit_old_$i"} = $form->{"unit_$i"};
2067 # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
2068 # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
2069 $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
2071 my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
2072 if (!$check_units->{$form->{"selected_unit_$i"}} ||
2073 ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
2074 $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
2075 # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
2076 # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
2077 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
2078 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
2082 if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
2083 if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
2084 $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
2085 $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
2086 $all_units->{$form->{"unit_old_$i"}}->{"factor"};
2089 if (!$form->{"basefactor_$i"}) {
2090 $form->{"basefactor_$i"} = 1;
2092 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
2094 # push @{ $form->{PRICES}{$id} }, $pkr;
2095 #push @{ $form->{PRICES}{$i} }, $pkr;
2097 $pkr->{selected} = '';
2099 # if there is an exchange rate change price
2100 if (($form->{exchangerate} * 1) != 0) {
2102 $pkr->{price} /= $form->{exchangerate};
2105 $pkr->{price} *= $form->{"basefactor_$i"};
2107 $pkr->{price} *= $basefactor;
2109 $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
2111 if ($selectedpricegroup_id eq undef) {
2112 if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
2114 $pkr->{selected} = ' selected';
2116 # no customer pricesgroup set
2117 if ($pkr->{price} == $pkr->{default_sellprice}) {
2119 $pkr->{price} = $form->{"sellprice_$i"};
2123 $form->{"sellprice_$i"} = $pkr->{price};
2127 if ($pkr->{price} == $pkr->{default_sellprice}) {
2129 $pkr->{price} = $form->{"sellprice_$i"};
2130 $pkr->{selected} = ' selected';
2135 if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
2136 if ($selectedpricegroup_id ne $pricegroup_old) {
2137 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2138 $pkr->{selected} = ' selected';
2141 if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
2142 if ($pkr->{pricegroup_id} == 0) {
2143 $pkr->{price} = $form->{"sellprice_$i"};
2144 $pkr->{selected} = ' selected';
2147 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2148 $pkr->{selected} = ' selected';
2149 if ( ($pkr->{pricegroup_id} == 0)
2150 and ($pkr->{price} == $form->{"sellprice_$i"})) {
2152 # $pkr->{price} = $form->{"sellprice_$i"};
2154 $pkr->{price} = $form->{"sellprice_$i"};
2160 push @{ $form->{PRICES}{$i} }, $pkr;
2163 $form->{"basefactor_$i"} *= $basefactor;
2172 $main::lxdebug->leave_sub();
2176 $main::lxdebug->enter_sub();
2178 my ($self, $myconfig, $form, $table) = @_;
2180 $main::lxdebug->leave_sub() and return 0 unless ($form->{id});
2182 # make sure there's no funny stuff in $table
2183 # ToDO: die when this happens and throw an error
2184 $main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
2186 my $dbh = $form->dbconnect($myconfig);
2188 my $query = qq|SELECT storno FROM $table WHERE id = ?|;
2189 my ($result) = selectrow_query($form, $dbh, $query, $form->{id});
2193 $main::lxdebug->leave_sub();