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 #======================================================================
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form, $locale) = @_;
44 $form->{duedate} = $form->{invdate} unless ($form->{duedate});
47 my $dbh = $form->dbconnect($myconfig);
49 my $query = qq|SELECT date '$form->{duedate}' - date '$form->{invdate}'
52 my $sth = $dbh->prepare($query);
53 $sth->execute || $form->dberror($query);
55 ($form->{terms}) = $sth->fetchrow_array;
63 my %oid = ('Pg' => 'oid',
66 # sort items by partsgroup
67 for $i (1 .. $form->{rowcount}) {
69 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
70 $form->format_string("partsgroup_$i");
71 $partsgroup = $form->{"partsgroup_$i"};
73 push @partsgroup, [$i, $partsgroup];
85 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
88 if ($item->[1] ne $sameitem) {
89 push(@{ $form->{description} }, qq|$item->[1]|);
90 $sameitem = $item->[1];
92 map { push(@{ $form->{$_} }, "") }
93 qw(runningnumber number serialnumber bin partnotes qty unit deliverydate sellprice listprice netprice discount linetotal);
96 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
98 if ($form->{"qty_$i"} != 0) {
100 # add number, description and qty to $form->{number}, ....
101 push(@{ $form->{runningnumber} }, $i);
102 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
103 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
104 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
105 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
106 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
107 push(@{ $form->{qty} },
108 $form->format_amount($myconfig, $form->{"qty_$i"}));
109 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
110 push(@{ $form->{deliverydate} }, qq|$form->{"deliverydate_$i"}|);
112 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
114 if ($form->{lizenzen}) {
115 if ($form->{"licensenumber_$i"}) {
117 qq|SELECT l.licensenumber, l.validuntil FROM license l WHERE l.id = $form->{"licensenumber_$i"}|;
118 $sth = $dbh->prepare($query);
119 $sth->execute || $form->dberror($query);
121 ($licensenumber, $validuntil) = $sth->fetchrow_array;
122 push(@{ $form->{licensenumber} }, $licensenumber);
123 push(@{ $form->{validuntil} },
124 $locale->date($myconfig, $validuntil, 0));
125 $licensenumber = "Lizenz: " . $licensenumber;
127 push(@{ $form->{licensenumber} }, $licensenumber);
129 push(@{ $form->{licensenumber} }, "");
130 push(@{ $form->{validuntil} }, "");
135 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
137 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
138 my ($dec) = ($sellprice =~ /\.(\d+)/);
140 my $decimalplaces = ($dec > 2) ? $dec : 2;
144 $sellprice * $form->parse_amount($myconfig,
145 $form->{"discount_$i"}) / 100,
148 # keep a netprice as well, (sellprice - discount)
149 $form->{"netprice_$i"} = $sellprice - $discount;
150 push(@{ $form->{netprice} },
151 ($form->{"netprice_$i"} != 0)
152 ? $form->format_amount(
153 $myconfig, $form->{"netprice_$i"},
159 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
163 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
165 $linetotal = ($linetotal != 0) ? $linetotal : " ";
167 push(@{ $form->{discount} }, $discount);
169 $form->{total} += $linetotal;
171 push(@{ $form->{linetotal} },
172 $form->format_amount($myconfig, $linetotal, 2));
174 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
178 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
180 if ($form->{taxincluded}) {
183 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
184 $taxbase = $linetotal - $taxamount;
186 $taxamount = $linetotal * $taxrate;
187 $taxbase = $linetotal;
190 if ($form->round_amount($taxrate, 7) == 0) {
191 if ($form->{taxincluded}) {
192 foreach $item (@taxaccounts) {
194 $form->round_amount($linetotal * $form->{"${item}_rate"} /
195 (1 + abs($form->{"${item}_rate"})),
198 $taxaccounts{$item} += $taxamount;
199 $taxdiff += $taxamount;
201 $taxbase{$item} += $taxbase;
203 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
205 foreach $item (@taxaccounts) {
206 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
207 $taxbase{$item} += $taxbase;
211 foreach $item (@taxaccounts) {
212 $taxaccounts{$item} +=
213 $taxamount * $form->{"${item}_rate"} / $taxrate;
214 $taxbase{$item} += $taxbase;
217 $tax_rate = $taxrate*100;
218 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
219 if ($form->{"assembly_$i"}) {
222 # get parts and push them onto the stack
224 if ($form->{groupitems}) {
226 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
228 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
231 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
234 JOIN parts p ON (a.parts_id = p.id)
235 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
237 AND a.id = '$form->{"id_$i"}'
239 $sth = $dbh->prepare($query);
240 $sth->execute || $form->dberror($query);
242 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
243 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
244 map { push(@{ $form->{$_} }, "") }
245 qw(runningnumber number serialnumber unit qty bin sellprice listprice netprice discount linetotal);
246 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
247 push(@{ $form->{description} }, $sameitem);
250 map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
251 $form->format_string("a_partnumber", "a_description");
253 push(@{ $form->{description} },
254 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
256 . qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
257 map { push(@{ $form->{$_} }, "") }
258 qw(number unit qty runningnumber serialnumber bin sellprice listprice netprice discount linetotal);
266 foreach my $item (sort keys %taxaccounts) {
267 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
268 push(@{ $form->{taxbase} },
269 $form->format_amount($myconfig, $taxbase{$item}, 2));
271 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
273 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
274 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
275 push(@{ $form->{taxrate} },
276 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
277 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
281 for my $i (1 .. $form->{paidaccounts}) {
282 if ($form->{"paid_$i"}) {
283 push(@{ $form->{payment} }, $form->{"paid_$i"});
284 my ($accno, $description) = split /--/, $form->{"AR_paid_$i"};
285 push(@{ $form->{paymentaccount} }, $description);
286 push(@{ $form->{paymentdate} }, $form->{"datepaid_$i"});
287 push(@{ $form->{paymentsource} }, $form->{"source_$i"});
289 $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
293 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
295 ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
297 $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
298 $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
300 $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
303 map { $form->{$_} = $myconfig->{$_} }
304 (qw(company address tel fax signature businessnumber));
305 $form->{username} = $myconfig->{name};
309 $main::lxdebug->leave_sub();
312 sub project_description {
313 $main::lxdebug->enter_sub();
315 my ($self, $dbh, $id) = @_;
317 my $query = qq|SELECT p.description
320 my $sth = $dbh->prepare($query);
321 $sth->execute || $form->dberror($query);
323 ($_) = $sth->fetchrow_array;
327 $main::lxdebug->leave_sub();
332 sub customer_details {
333 $main::lxdebug->enter_sub();
335 my ($self, $myconfig, $form) = @_;
337 # connect to database
338 my $dbh = $form->dbconnect($myconfig);
340 # get contact id, set it if nessessary
341 ($null, $form->{cp_id}) = split /--/, $form->{contact};
344 if ($form->{cp_id}) {
345 $contact = "and cp.cp_id = $form->{cp_id}";
348 $taxincluded = $form->{taxincluded};
350 # get rest for the customer
351 my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes
353 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
354 WHERE ct.id = $form->{customer_id} $contact order by cp.cp_id limit 1|;
355 my $sth = $dbh->prepare($query);
356 $sth->execute || $form->dberror($query);
358 $ref = $sth->fetchrow_hashref(NAME_lc);
359 map { $form->{$_} = $ref->{$_} } keys %$ref;
361 $form->{taxincluded} = $taxincluded;
366 $main::lxdebug->leave_sub();
370 $main::lxdebug->enter_sub();
371 print STDERR "IS.pm-post_invoice\n";
372 my ($self, $myconfig, $form) = @_;
374 # connect to database, turn off autocommit
375 my $dbh = $form->dbconnect_noauto($myconfig);
377 my ($query, $sth, $null, $project_id, $deliverydate);
378 my $exchangerate = 0;
380 ($null, $form->{employee_id}) = split /--/, $form->{employee};
381 unless ($form->{employee_id}) {
382 $form->get_employee($dbh);
385 ($null, $form->{contact_id}) = split /--/, $form->{contact};
386 $form->{contact_id} *= 1;
388 ($null, $form->{department_id}) = split(/--/, $form->{department});
389 $form->{department_id} *= 1;
393 &reverse_invoice($dbh, $form);
396 my $uid = rand() . time;
398 $uid .= $form->{login};
400 $uid = substr($uid, 2, 75);
402 $query = qq|INSERT INTO ar (invnumber, employee_id)
403 VALUES ('$uid', $form->{employee_id})|;
404 $dbh->do($query) || $form->dberror($query);
406 $query = qq|SELECT a.id FROM ar a
407 WHERE a.invnumber = '$uid'|;
408 $sth = $dbh->prepare($query);
409 $sth->execute || $form->dberror($query);
411 ($form->{id}) = $sth->fetchrow_array;
415 map { $form->{$_} =~ s/\'/\'\'/g }
416 (qw(invnumber shippingpoint shipvia notes intnotes message));
418 my ($netamount, $invoicediff) = (0, 0);
419 my ($amount, $linetotal, $lastincomeaccno);
421 if ($form->{currency} eq $form->{defaultcurrency}) {
422 $form->{exchangerate} = 1;
425 $form->check_exchangerate($myconfig, $form->{currency},
426 $form->{transdate}, 'buy');
429 $form->{exchangerate} =
432 : $form->parse_amount($myconfig, $form->{exchangerate});
434 $form->{expense_inventory} = "";
436 foreach my $i (1 .. $form->{rowcount}) {
437 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
439 if ($form->{"qty_$i"} != 0) {
441 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
442 (qw(partnumber description unit));
444 # undo discount formatting
445 $form->{"discount_$i"} =
446 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
448 my ($allocated, $taxrate) = (0, 0);
451 # keep entered selling price
453 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
455 my ($dec) = ($fxsellprice =~ /\.(\d+)/);
457 my $decimalplaces = ($dec > 2) ? $dec : 2;
461 $form->round_amount($fxsellprice * $form->{"discount_$i"},
463 $form->{"sellprice_$i"} = $fxsellprice - $discount;
466 map { $taxrate += $form->{"${_}_rate"} } split / /,
467 $form->{"taxaccounts_$i"};
469 # round linetotal to 2 decimal places
471 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
473 if ($form->{taxincluded}) {
474 $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
475 $form->{"sellprice_$i"} =
476 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
478 $taxamount = $linetotal * $taxrate;
481 $netamount += $linetotal;
483 if ($taxamount != 0) {
485 $form->{amount}{ $form->{id} }{$_} +=
486 $taxamount * $form->{"${_}_rate"} / $taxrate
487 } split / /, $form->{"taxaccounts_$i"};
490 # add amount to income, $form->{amount}{trans_id}{accno}
492 $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
495 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
496 $form->{exchangerate};
497 $linetotal = $form->round_amount($linetotal, 2);
499 # this is the difference from the inventory
500 $invoicediff += ($amount - $linetotal);
502 $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
505 $lastincomeaccno = $form->{"income_accno_$i"};
507 # adjust and round sellprice
508 $form->{"sellprice_$i"} =
509 $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
512 if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
514 # adjust parts onhand quantity
516 if ($form->{"assembly_$i"}) {
518 # do not update if assembly consists of all services
519 $query = qq|SELECT sum(p.inventory_accno_id)
521 JOIN assembly a ON (a.parts_id = p.id)
522 WHERE a.id = $form->{"id_$i"}|;
523 $sth = $dbh->prepare($query);
524 $sth->execute || $form->dberror($query);
526 if ($sth->fetchrow_array) {
527 $form->update_balance($dbh, "parts", "onhand",
528 qq|id = $form->{"id_$i"}|,
529 $form->{"qty_$i"} * -1)
530 unless $form->{shipped};
534 # record assembly item as allocated
535 &process_assembly($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
537 $form->update_balance($dbh, "parts", "onhand",
538 qq|id = $form->{"id_$i"}|,
539 $form->{"qty_$i"} * -1)
540 unless $form->{shipped};
542 $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $form->{"qty_$i"});
546 $project_id = 'NULL';
547 if ($form->{"projectnumber_$i"}) {
548 $project_id = $form->{"projectnumber_$i"};
551 ($form->{"deliverydate_$i"})
552 ? qq|'$form->{"deliverydate_$i"}'|
556 # get pricegroup_id and save ist
557 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
560 # save detail record in invoice table
561 $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty,
562 sellprice, fxsellprice, discount, allocated, assemblyitem,
563 unit, deliverydate, project_id, serialnumber, pricegroup_id)
564 VALUES ($form->{id}, $form->{"id_$i"},
565 '$form->{"description_$i"}', $form->{"qty_$i"},
566 $form->{"sellprice_$i"}, $fxsellprice,
567 $form->{"discount_$i"}, $allocated, 'f',
568 '$form->{"unit_$i"}', $deliverydate, (SELECT id from project where projectnumber = '$project_id'),
569 '$form->{"serialnumber_$i"}',
571 $dbh->do($query) || $form->dberror($query);
573 if ($form->{lizenzen}) {
574 if ($form->{"licensenumber_$i"}) {
576 qq|SELECT i.id FROM invoice i WHERE i.trans_id=$form->{id} ORDER BY i.oid DESC LIMIT 1|;
577 $sth = $dbh->prepare($query);
578 $sth->execute || $form->dberror($query);
580 ($invoice_row_id) = $sth->fetchrow_array;
584 qq|INSERT INTO licenseinvoice (trans_id, license_id) VALUES ($invoice_row_id, $form->{"licensenumber_$i"})|;
585 $dbh->do($query) || $form->dberror($query);
592 $form->{datepaid} = $form->{invdate};
594 # total payments, don't move we need it here
595 for my $i (1 .. $form->{paidaccounts}) {
596 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
597 $form->{paid} += $form->{"paid_$i"};
598 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
601 my ($tax, $diff) = (0, 0);
603 $netamount = $form->round_amount($netamount, 2);
605 # figure out rounding errors for total amount vs netamount + taxes
606 if ($form->{taxincluded}) {
608 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
609 $diff += $amount - $netamount * $form->{exchangerate};
610 $netamount = $amount;
612 foreach my $item (split / /, $form->{taxaccounts}) {
613 $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
614 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
615 $tax += $form->{amount}{ $form->{id} }{$item};
616 $netamount -= $form->{amount}{ $form->{id} }{$item};
619 $invoicediff += $diff;
620 ######## this only applies to tax included
621 if ($lastincomeaccno) {
622 $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
626 $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
627 $diff = $amount - $netamount * $form->{exchangerate};
628 $netamount = $amount;
629 foreach my $item (split / /, $form->{taxaccounts}) {
630 $form->{amount}{ $form->{id} }{$item} =
631 $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
634 $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
637 $amount - $form->{amount}{ $form->{id} }{$item} *
638 $form->{exchangerate};
639 $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
640 $tax += $form->{amount}{ $form->{id} }{$item};
644 $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
646 $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
649 $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
651 # update exchangerate
652 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
653 $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
654 $form->{exchangerate}, 0);
657 foreach my $trans_id (keys %{ $form->{amount} }) {
658 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
659 next unless ($form->{expense_inventory} =~ /$accno/);
661 ($form->{amount}{$trans_id}{$accno} =
662 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
665 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
667 VALUES ($trans_id, (SELECT c.id FROM chart c
668 WHERE c.accno = '$accno'),
669 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
670 (SELECT taxkey_id FROM chart WHERE accno = '$accno'))|;
671 $dbh->do($query) || $form->dberror($query);
672 $form->{amount}{$trans_id}{$accno} = 0;
676 foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
678 ($form->{amount}{$trans_id}{$accno} =
679 $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
682 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
684 VALUES ($trans_id, (SELECT id FROM chart
685 WHERE accno = '$accno'),
686 $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
687 (SELECT taxkey_id FROM chart WHERE accno = '$accno'))|;
688 $dbh->do($query) || $form->dberror($query);
693 # deduct payment differences from diff
694 for my $i (1 .. $form->{paidaccounts}) {
695 if ($form->{"paid_$i"} != 0) {
697 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
698 $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
702 # force AR entry if 0
703 # $form->{amount}{$form->{id}}{$form->{AR}} = 1 if ($form->{amount}{$form->{id}}{$form->{AR}} == 0);
705 # record payments and offsetting AR
706 for my $i (1 .. $form->{paidaccounts}) {
708 if ($form->{"paid_$i"} != 0) {
709 my ($accno) = split /--/, $form->{"AR_paid_$i"};
710 $form->{"datepaid_$i"} = $form->{invdate}
711 unless ($form->{"datepaid_$i"});
712 $form->{datepaid} = $form->{"datepaid_$i"};
716 if ($form->{currency} eq $form->{defaultcurrency}) {
717 $form->{"exchangerate_$i"} = 1;
720 $form->check_exchangerate($myconfig, $form->{currency},
721 $form->{"datepaid_$i"}, 'buy');
723 $form->{"exchangerate_$i"} =
726 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
731 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
734 if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
735 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
737 VALUES ($form->{id}, (SELECT c.id FROM chart c
738 WHERE c.accno = '$form->{AR}'),
739 $amount, '$form->{"datepaid_$i"}')|;
740 $dbh->do($query) || $form->dberror($query);
744 $form->{"paid_$i"} *= -1;
746 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
748 VALUES ($form->{id}, (SELECT c.id FROM chart c
749 WHERE c.accno = '$accno'),
750 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
751 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
752 $dbh->do($query) || $form->dberror($query);
754 # exchangerate difference
755 $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
756 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
760 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
761 $form->{"exchangerate_$i"};
763 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
766 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
772 # update exchange rate
773 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
774 $form->update_exchangerate($dbh, $form->{currency},
775 $form->{"datepaid_$i"},
776 $form->{"exchangerate_$i"}, 0);
781 # record exchange rate differences and gains/losses
782 foreach my $accno (keys %{ $form->{fx} }) {
783 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
785 ($form->{fx}{$accno}{$transdate} =
786 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
790 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
791 transdate, cleared, fx_transaction)
793 (SELECT c.id FROM chart c
794 WHERE c.accno = '$accno'),
795 $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1')|;
796 $dbh->do($query) || $form->dberror($query);
801 $amount = $netamount + $tax;
803 # set values which could be empty to 0
805 $form->{taxincluded} *= 1;
806 my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
807 my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
809 # fill in subject if there is none
810 $form->{subject} = qq|$form->{label} $form->{invnumber}|
811 unless $form->{subject};
813 # if there is a message stuff it into the intnotes
814 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
815 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
816 my $now = scalar localtime;
817 $form->{intnotes} .= qq|\r
818 \r| if $form->{intnotes};
820 $form->{intnotes} .= qq|[email]\r
823 $cc${bcc}Subject: $form->{subject}\r
825 Message: $form->{message}\r| if $form->{message};
828 $query = qq|UPDATE ar set
829 invnumber = '$form->{invnumber}',
830 ordnumber = '$form->{ordnumber}',
831 quonumber = '$form->{quonumber}',
832 cusordnumber = '$form->{cusordnumber}',
833 transdate = '$form->{invdate}',
834 customer_id = $form->{customer_id},
836 netamount = $netamount,
837 paid = $form->{paid},
838 datepaid = $datepaid,
841 shippingpoint = '$form->{shippingpoint}',
842 shipvia = '$form->{shipvia}',
843 terms = $form->{terms},
844 notes = '$form->{notes}',
845 intnotes = '$form->{intnotes}',
846 taxincluded = '$form->{taxincluded}',
847 curr = '$form->{currency}',
848 department_id = $form->{department_id},
849 employee_id = $form->{employee_id},
850 cp_id = $form->{contact_id}
851 WHERE id = $form->{id}
853 $dbh->do($query) || $form->dberror($query);
855 $form->{pago_total} = $amount;
858 $form->{name} = $form->{customer};
859 $form->{name} =~ s/--$form->{customer_id}//;
860 $form->add_shipto($dbh, $form->{id});
862 # save printed, emailed and queued
863 $form->save_status($dbh);
865 if ($form->{webdav}) {
866 &webdav_folder($myconfig, $form);
869 my $rc = $dbh->commit;
872 $main::lxdebug->leave_sub();
877 sub process_assembly {
878 $main::lxdebug->enter_sub();
880 my ($dbh, $form, $id, $totalqty) = @_;
882 my $query = qq|SELECT a.parts_id, a.qty, p.assembly,
883 p.partnumber, p.description, p.unit,
884 p.inventory_accno_id, p.income_accno_id,
887 JOIN parts p ON (a.parts_id = p.id)
889 my $sth = $dbh->prepare($query);
890 $sth->execute || $form->dberror($query);
892 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
896 $ref->{inventory_accno_id} *= 1;
897 $ref->{expense_accno_id} *= 1;
899 map { $ref->{$_} =~ s/\'/\'\'/g } (qw(partnumber description unit));
901 # multiply by number of assemblies
902 $ref->{qty} *= $totalqty;
904 if ($ref->{assembly}) {
905 &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
908 if ($ref->{inventory_accno_id}) {
909 $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
913 # save detail record for individual assembly item in invoice table
914 $query = qq|INSERT INTO invoice (trans_id, description, parts_id, qty,
915 sellprice, fxsellprice, allocated, assemblyitem, unit)
917 ($form->{id}, '$ref->{description}',
918 $ref->{parts_id}, $ref->{qty}, 0, 0, $allocated, 't',
920 $dbh->do($query) || $form->dberror($query);
926 $main::lxdebug->leave_sub();
930 $main::lxdebug->enter_sub();
932 my ($dbh, $form, $id, $totalqty) = @_;
934 my $query = qq|SELECT i.id, i.trans_id, i.qty, i.allocated, i.sellprice,
935 (SELECT c.accno FROM chart c
936 WHERE p.inventory_accno_id = c.id) AS inventory_accno,
937 (SELECT c.accno FROM chart c
938 WHERE p.expense_accno_id = c.id) AS expense_accno
939 FROM invoice i, parts p
940 WHERE i.parts_id = p.id
942 AND (i.qty + i.allocated) < 0
944 my $sth = $dbh->prepare($query);
945 $sth->execute || $form->dberror($query);
950 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
951 if (($qty = (($ref->{qty} * -1) - $ref->{allocated})) > $totalqty) {
955 $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|,
958 # total expenses and inventory
959 # sellprice is the cost of the item
960 $linetotal = $form->round_amount($ref->{sellprice} * $qty, 2);
965 $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
966 $form->{expense_inventory} .= " " . $ref->{expense_accno};
969 $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
970 $form->{expense_inventory} .= " " . $ref->{inventory_accno};
976 last if (($totalqty -= $qty) <= 0);
981 $main::lxdebug->leave_sub();
986 sub reverse_invoice {
987 $main::lxdebug->enter_sub();
989 my ($dbh, $form) = @_;
991 # reverse inventory items
992 my $query = qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
995 JOIN parts p ON (i.parts_id = p.id)
996 WHERE i.trans_id = $form->{id}|;
997 my $sth = $dbh->prepare($query);
998 $sth->execute || $form->dberror($query);
1000 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1002 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1004 # if the invoice item is not an assemblyitem adjust parts onhand
1005 if (!$ref->{assemblyitem}) {
1007 # adjust onhand in parts table
1008 $form->update_balance($dbh, "parts", "onhand",
1009 qq|id = $ref->{parts_id}|,
1013 # loop if it is an assembly
1014 next if ($ref->{assembly});
1016 # de-allocated purchases
1017 $query = qq|SELECT i.id, i.trans_id, i.allocated
1019 WHERE i.parts_id = $ref->{parts_id}
1021 ORDER BY i.trans_id DESC|;
1022 my $sth = $dbh->prepare($query);
1023 $sth->execute || $form->dberror($query);
1025 while (my $inhref = $sth->fetchrow_hashref(NAME_lc)) {
1027 if (($ref->{qty} - $inhref->{allocated}) > 0) {
1028 $qty = $inhref->{allocated};
1032 $form->update_balance($dbh, "invoice", "allocated",
1033 qq|id = $inhref->{id}|,
1036 last if (($ref->{qty} -= $qty) <= 0);
1045 $query = qq|DELETE FROM acc_trans
1046 WHERE trans_id = $form->{id}|;
1047 $dbh->do($query) || $form->dberror($query);
1049 # delete invoice entries
1050 $query = qq|DELETE FROM invoice
1051 WHERE trans_id = $form->{id}|;
1052 $dbh->do($query) || $form->dberror($query);
1054 if ($form->{lizenzen}) {
1055 $query = qq|DELETE FROM licenseinvoice
1056 WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = $form->{id})|;
1057 $dbh->do($query) || $form->dberror($query);
1060 $query = qq|DELETE FROM shipto
1061 WHERE trans_id = $form->{id}|;
1062 $dbh->do($query) || $form->dberror($query);
1064 $main::lxdebug->leave_sub();
1067 sub delete_invoice {
1068 $main::lxdebug->enter_sub();
1070 my ($self, $myconfig, $form, $spool) = @_;
1072 # connect to database
1073 my $dbh = $form->dbconnect_noauto($myconfig);
1075 &reverse_invoice($dbh, $form);
1078 my $query = qq|DELETE FROM ar
1079 WHERE id = $form->{id}|;
1080 $dbh->do($query) || $form->dberror($query);
1082 # delete spool files
1083 $query = qq|SELECT s.spoolfile FROM status s
1084 WHERE s.trans_id = $form->{id}|;
1085 my $sth = $dbh->prepare($query);
1086 $sth->execute || $self->dberror($query);
1089 my @spoolfiles = ();
1091 while (($spoolfile) = $sth->fetchrow_array) {
1092 push @spoolfiles, $spoolfile;
1096 # delete status entries
1097 $query = qq|DELETE FROM status
1098 WHERE trans_id = $form->{id}|;
1099 $dbh->do($query) || $form->dberror($query);
1101 my $rc = $dbh->commit;
1105 foreach $spoolfile (@spoolfiles) {
1106 unlink "$spool/$spoolfile" if $spoolfile;
1110 $main::lxdebug->leave_sub();
1115 sub retrieve_invoice {
1116 $main::lxdebug->enter_sub();
1117 print STDERR "IS.pm-retrieve_invoice\n";
1118 my ($self, $myconfig, $form) = @_;
1120 # connect to database
1121 my $dbh = $form->dbconnect_noauto($myconfig);
1127 # get default accounts and last invoice number
1128 $query = qq|SELECT (SELECT c.accno FROM chart c
1129 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1130 (SELECT c.accno FROM chart c
1131 WHERE d.income_accno_id = c.id) AS income_accno,
1132 (SELECT c.accno FROM chart c
1133 WHERE d.expense_accno_id = c.id) AS expense_accno,
1134 (SELECT c.accno FROM chart c
1135 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1136 (SELECT c.accno FROM chart c
1137 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1138 d.curr AS currencies
1141 $query = qq|SELECT (SELECT c.accno FROM chart c
1142 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1143 (SELECT c.accno FROM chart c
1144 WHERE d.income_accno_id = c.id) AS income_accno,
1145 (SELECT c.accno FROM chart c
1146 WHERE d.expense_accno_id = c.id) AS expense_accno,
1147 (SELECT c.accno FROM chart c
1148 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1149 (SELECT c.accno FROM chart c
1150 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
1151 d.curr AS currencies, current_date AS invdate
1154 my $sth = $dbh->prepare($query);
1155 $sth->execute || $form->dberror($query);
1157 my $ref = $sth->fetchrow_hashref(NAME_lc);
1158 map { $form->{$_} = $ref->{$_} } keys %$ref;
1164 $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
1165 a.transdate AS invdate, a.paid,
1166 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes,
1167 a.duedate, a.taxincluded, a.curr AS currency,
1168 a.employee_id, e.name AS employee
1170 LEFT JOIN employee e ON (e.id = a.employee_id)
1171 WHERE a.id = $form->{id}|;
1172 $sth = $dbh->prepare($query);
1173 $sth->execute || $form->dberror($query);
1175 $ref = $sth->fetchrow_hashref(NAME_lc);
1176 map { $form->{$_} = $ref->{$_} } keys %$ref;
1179 $form->{exchangerate} =
1180 $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
1184 $query = qq|SELECT s.* FROM shipto s
1185 WHERE s.trans_id = $form->{id}|;
1186 $sth = $dbh->prepare($query);
1187 $sth->execute || $form->dberror($query);
1189 $ref = $sth->fetchrow_hashref(NAME_lc);
1190 map { $form->{$_} = $ref->{$_} } keys %$ref;
1193 # get printed, emailed
1194 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
1196 WHERE s.trans_id = $form->{id}|;
1197 $sth = $dbh->prepare($query);
1198 $sth->execute || $form->dberror($query);
1200 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1201 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
1202 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
1203 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
1204 if $ref->{spoolfile};
1207 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
1209 # retrieve individual items
1210 $query = qq|SELECT (SELECT c.accno FROM chart c
1211 WHERE p.inventory_accno_id = c.id)
1213 (SELECT c.accno FROM chart c
1214 WHERE p.income_accno_id = c.id)
1216 (SELECT c.accno FROM chart c
1217 WHERE p.expense_accno_id = c.id)
1219 i.description, i.qty, i.fxsellprice AS sellprice,
1220 i.discount, i.parts_id AS id, i.unit, i.deliverydate,
1221 i.project_id, pr.projectnumber, i.serialnumber,
1222 p.partnumber, p.assembly, p.bin, p.notes AS partnotes, i.id AS invoice_pos,
1223 pg.partsgroup, i.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=i.pricegroup_id) as pricegroup
1225 JOIN parts p ON (i.parts_id = p.id)
1226 LEFT JOIN project pr ON (i.project_id = pr.id)
1227 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1228 WHERE i.trans_id = $form->{id}
1229 AND NOT i.assemblyitem = '1'
1231 $sth = $dbh->prepare($query);
1232 $sth->execute || $form->dberror($query);
1233 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1235 #set expense_accno=inventory_accno if they are different => bilanz
1237 ($ref->{expense_accno} != $ref->{inventory_accno})
1238 ? $ref->{inventory_accno}
1239 : $ref->{expense_accno};
1241 # get tax rates and description
1243 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
1244 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1246 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1248 $stw = $dbh->prepare($query);
1249 $stw->execute || $form->dberror($query);
1250 $ref->{taxaccounts} = "";
1251 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1253 # if ($customertax{$ref->{accno}}) {
1254 $ref->{taxaccounts} .= "$ptr->{accno} ";
1255 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1256 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1257 $form->{"$ptr->{accno}_description"} = $ptr->{description};
1258 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1259 $form->{taxaccounts} .= "$ptr->{accno} ";
1264 if ($form->{lizenzen}) {
1265 $query = qq|SELECT l.licensenumber, l.id AS licenseid
1266 FROM license l, licenseinvoice li
1267 WHERE l.id = li.license_id AND li.trans_id = $ref->{invoice_pos}|;
1268 $stg = $dbh->prepare($query);
1269 $stg->execute || $form->dberror($query);
1270 ($licensenumber, $licenseid) = $stg->fetchrow_array();
1272 "<option value=\"$licenseid\">$licensenumber</option>";
1276 chop $ref->{taxaccounts};
1277 push @{ $form->{invoice_details} }, $ref;
1282 if ($form->{webdav}) {
1283 &webdav_folder($myconfig, $form);
1287 my $rc = $dbh->commit;
1290 $main::lxdebug->leave_sub();
1296 $main::lxdebug->enter_sub();
1298 my ($self, $myconfig, $form) = @_;
1300 # connect to database
1301 my $dbh = $form->dbconnect($myconfig);
1303 my $dateformat = $myconfig->{dateformat};
1304 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1308 ? "to_date('$form->{invdate}', '$dateformat')"
1311 $form->{customer_id} *= 1;
1314 my $query = qq|SELECT c.name AS customer, c.discount, c.creditlimit, c.terms,
1315 c.email, c.cc, c.bcc, c.language,
1316 c.street, c.zipcode, c.city, c.country,
1317 $duedate + c.terms AS duedate, c.notes AS intnotes,
1318 b.discount AS tradediscount, b.description AS business, c.klass as customer_klass
1320 LEFT JOIN business b ON (b.id = c.business_id)
1321 WHERE c.id = $form->{customer_id}|;
1322 my $sth = $dbh->prepare($query);
1323 $sth->execute || $form->dberror($query);
1325 $ref = $sth->fetchrow_hashref(NAME_lc);
1327 map { $form->{$_} = $ref->{$_} } keys %$ref;
1330 $form->{creditremaining} = $form->{creditlimit};
1331 $query = qq|SELECT SUM(a.amount - a.paid)
1333 WHERE a.customer_id = $form->{customer_id}|;
1334 $sth = $dbh->prepare($query);
1335 $sth->execute || $form->dberror($query);
1337 ($form->{creditremaining}) -= $sth->fetchrow_array;
1341 $query = qq|SELECT o.amount,
1342 (SELECT e.buy FROM exchangerate e
1343 WHERE e.curr = o.curr
1344 AND e.transdate = o.transdate)
1346 WHERE o.customer_id = $form->{customer_id}
1347 AND o.quotation = '0'
1348 AND o.closed = '0'|;
1349 $sth = $dbh->prepare($query);
1350 $sth->execute || $form->dberror($query);
1352 while (my ($amount, $exch) = $sth->fetchrow_array) {
1353 $exch = 1 unless $exch;
1354 $form->{creditremaining} -= $amount * $exch;
1358 $form->get_contacts($dbh, $form->{customer_id});
1359 ($null, $form->{cp_id}) = split /--/, $form->{contact};
1361 # get contact if selected
1362 if ($form->{contact} ne "--" && $form->{contact} ne "") {
1363 $form->get_contact($dbh, $form->{cp_id});
1366 # get shipto if we did not converted an order or invoice
1367 if (!$form->{shipto}) {
1368 map { delete $form->{$_} }
1369 qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
1371 $query = qq|SELECT s.* FROM shipto s
1372 WHERE s.trans_id = $form->{customer_id}|;
1373 $sth = $dbh->prepare($query);
1374 $sth->execute || $form->dberror($query);
1376 $ref = $sth->fetchrow_hashref(NAME_lc);
1377 map { $form->{$_} = $ref->{$_} } keys %$ref;
1381 # get taxes we charge for this customer
1382 $query = qq|SELECT c.accno
1384 JOIN customertax ct ON (ct.chart_id = c.id)
1385 WHERE ct.customer_id = $form->{customer_id}|;
1386 $sth = $dbh->prepare($query);
1387 $sth->execute || $form->dberror($query);
1389 my $customertax = ();
1390 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1391 $customertax{ $ref->{accno} } = 1;
1395 # setup last accounts used for this customer
1396 if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1397 $query = qq|SELECT c.accno, c.description, c.link, c.category
1399 JOIN acc_trans ac ON (ac.chart_id = c.id)
1400 JOIN ar a ON (a.id = ac.trans_id)
1401 WHERE a.customer_id = $form->{customer_id}
1402 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1403 AND a.id IN (SELECT max(a2.id) FROM ar a2
1404 WHERE a2.customer_id = $form->{customer_id})|;
1405 $sth = $dbh->prepare($query);
1406 $sth->execute || $form->dberror($query);
1409 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1410 if ($ref->{category} eq 'I') {
1412 $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
1414 if ($ref->{category} eq 'A') {
1415 $form->{ARselected} = $form->{AR_1} =
1416 "$ref->{accno}--$ref->{description}";
1420 $form->{rowcount} = $i if ($i && !$form->{type});
1425 $main::lxdebug->leave_sub();
1429 $main::lxdebug->enter_sub();
1431 my ($self, $myconfig, $form) = @_;
1433 my $i = $form->{rowcount};
1435 my $where = "NOT p.obsolete = '1'";
1437 if ($form->{"partnumber_$i"}) {
1438 my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1439 $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1441 if ($form->{"description_$i"}) {
1442 my $description = $form->like(lc $form->{"description_$i"});
1443 $where .= " AND lower(p.description) LIKE '$description'";
1446 if ($form->{"partsgroup_$i"}) {
1447 my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1448 $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1451 if ($form->{"description_$i"}) {
1452 $where .= " ORDER BY p.description";
1454 $where .= " ORDER BY p.partnumber";
1457 # connect to database
1458 my $dbh = $form->dbconnect($myconfig);
1460 my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
1462 c1.accno AS inventory_accno,
1463 c2.accno AS income_accno,
1464 c3.accno AS expense_accno,
1465 p.unit, p.assembly, p.bin, p.onhand, p.notes AS partnotes,
1468 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1469 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1470 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1471 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1473 my $sth = $dbh->prepare($query);
1474 $sth->execute || $form->dberror($query);
1476 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1478 #set expense_accno=inventory_accno if they are different => bilanz
1480 ($ref->{expense_accno} != $ref->{inventory_accno})
1481 ? $ref->{inventory_accno}
1482 : $ref->{expense_accno};
1484 # get tax rates and description
1486 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
1487 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1489 WHERE c.id=t.chart_id AND t.taxkey in (SELECT c2.taxkey_id from chart c2 where c2.accno = '$accno_id')
1491 $stw = $dbh->prepare($query);
1492 $stw->execute || $form->dberror($query);
1494 $ref->{taxaccounts} = "";
1495 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1497 # if ($customertax{$ref->{accno}}) {
1498 $ref->{taxaccounts} .= "$ptr->{accno} ";
1499 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1500 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
1501 $form->{"$ptr->{accno}_description"} = $ptr->{description};
1502 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
1503 $form->{taxaccounts} .= "$ptr->{accno} ";
1509 chop $ref->{taxaccounts};
1511 push @{ $form->{item_list} }, $ref;
1513 if ($form->{lizenzen}) {
1514 if ($ref->{inventory_accno} > 0) {
1516 qq| SELECT l.* FROM license l WHERE l.parts_id = $ref->{id} AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
1517 $stw = $dbh->prepare($query);
1518 $stw->execute || $form->dberror($query);
1519 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1520 push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
1529 $main::lxdebug->leave_sub();
1532 ##########################
1533 # get pricegroups from database
1534 # build up selected pricegroup
1535 # if an exchange rate - change price
1538 sub get_pricegroups_for_parts {
1539 print STDERR "IS.pm - get_pricegroups_for_parts\n";
1540 $main::lxdebug->enter_sub();
1542 my ($self, $myconfig, $form) = @_;
1544 my $dbh = $form->dbconnect($myconfig);
1549 while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
1551 $id = $form->{"id_$i"};
1553 if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
1555 $id = $form->{"new_id_$i"};
1558 ($price, $selectedpricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
1559 # print (STDERR "sellprice_drag_$i", Dumper($form->{"sellprice_drag_$i"}));
1561 $pricegroup_old = $form->{"pricegroup_old_$i"};
1562 # print (STDERR "pricegroup_old_i-$i", Dumper($pricegroup_old));
1564 $price_new = $form->{"price_new_$i"};
1566 $price_old = $form->{"price_old_$i"};
1569 $query = 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|;
1571 $pkq = $dbh->prepare($query);
1572 $pkq->execute || $form->dberror($query);
1573 while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
1574 # push @{ $form->{PRICES}{$id} }, $pkr;
1575 push @{ $form->{PRICES}{$i} }, $pkr;
1577 $pkr->{selected} = '';
1579 # if there is an exchange rate change price
1580 if (($form->{exchangerate} * 1) != 0) {
1581 # print STDERR "WECHSELKURS?-$form->{exchangerate}\n";
1582 $pkr->{price} /= $form->{exchangerate};
1584 $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
1587 if ($selectedpricegroup_id eq undef) {
1588 if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
1589 print STDERR " INIT ROW \n";
1590 #print (STDERR " PREIS", Dumper($pkr->{price}));
1591 $pkr->{selected} = ' selected';
1592 $last->{selected} = '';
1593 # print (STDERR " SELLPRICE", Dumper($form->{"sellprice_$i"}));
1595 # no customer pricesgroup set
1596 if ($pkr->{price} == $pkr->{default_sellprice}) {
1597 print (STDERR " PREIS IST DEFAULT-SELLPRICE", Dumper($form->{"sellprice_$i"}));
1598 $pkr->{price} = $form->{"sellprice_$i"};
1600 # if ($form->{tradediscount}){
1601 # print (STDERR "TRADE--", Dumper($pkr->{price}));
1602 # $pkr->{price} =$pkr->{price} * (1 - $form->{tradediscount});
1603 # $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
1604 # print (STDERR "TRADE--", Dumper($pkr->{price}));
1608 print STDERR " PREIS IST NICHT NULL\n";
1609 $form->{"sellprice_$i"} = $pkr->{price};
1611 # print (STDERR " PRICE", Dumper($pkr->{price}));
1614 print STDERR " INIT ROW but what\n";
1615 print (STDERR " PREIS -", Dumper($pkr->{price}), "Default", Dumper($pkr->{default_sellprice}));
1616 if ($pkr->{price} == $pkr->{default_sellprice}) {
1617 print (STDERR " PREIS IST DEFAULT-", Dumper($form->{"sellprice_$i"}));
1618 $pkr->{price} = $form->{"sellprice_$i"};
1619 $pkr->{selected} = ' selected';
1623 if ($selectedpricegroup_id or $selectedpricegroup_id == 0){
1624 if ($selectedpricegroup_id ne $pricegroup_old) {
1625 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
1626 if ($price_new != $form->{"sellprice_$i"}) {
1627 print STDERR " MANUELLEN PREIS WÄHLEN\n";
1629 print STDERR " UPDATE CHANGE PRICEGROUP\n";
1630 $pkr->{selected} = ' selected';
1631 $last->{selected} = '';
1632 #$form->{"pricegroup_old_$i"} = $pkr->{$pricegroup_id};
1636 if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
1637 if ($pkr->{pricegroup_id} == 0) {
1638 print STDERR " UPDATE CHANGE PRICEGROUP with price manuelly\n";
1639 print (STDERR " SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
1640 print (STDERR " NEWPRICE??? ---", Dumper($price_new));
1641 $pkr->{price} = $form->{"sellprice_$i"};
1642 $pkr->{selected} = ' selected';
1643 $last->{selected} = '';
1644 #$form->{"sellprice_$i"} = $form->format_amount($myconfig, $price_new, 2);
1645 # print (STDERR "----5555---", Dumper($pkr));
1648 if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
1649 print STDERR " UPDATE NO CHANGE\n";
1650 $pkr->{selected} = ' selected';
1651 $last->{selected} = '';
1652 print STDERR " DEFAULTPRICE??? ---$pkr->{default_sellprice}\n";
1653 print (STDERR " SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
1654 # print (STDERR " HIER DER SELLPRICE DEFAULT??? ---", Dumper($form));
1655 print STDERR " NEWPRICE??? ---$price_new_\n";
1656 if (($pkr->{pricegroup_id} == 0) and ($pkr->{price} == $form->{"sellprice_$i"})) {
1657 print (STDERR " UPDATE NO CHANGE BUT PRICE MANUELLY SET", Dumper($pkr->{price}));
1658 # $pkr->{price} = $form->{"sellprice_$i"};
1660 $pkr->{price} = $form->{"sellprice_$i"};
1662 #print (STDERR " FEHLER", Dumper($form->{"sellprice_$i"}));
1675 # print (STDERR "TEST", Dumper($form->{PRICES}));
1676 # print (STDERR "TEST id_$i", Dumper($form->{"id_$i"}));
1677 $main::lxdebug->leave_sub();
1681 $main::lxdebug->enter_sub();
1683 my ($myconfig, $form) = @_;
1686 $path = "webdav/rechnungen/" . $form->{invnumber}, last SWITCH
1687 if ($form->{vc} eq "customer");
1688 $path = "webdav/einkaufsrechnungen/" . $form->{invnumber}, last SWITCH
1689 if ($form->{vc} eq "vendor");
1693 mkdir($path, 0770) or die "can't make directory $!\n";
1697 foreach $file (@files) {
1698 $file =~ /\/([^\/]*)$/;
1700 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1702 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1703 $form->{WEBDAV}{$fname} = $link;
1708 $main::lxdebug->leave_sub();