Beim Wechsel des Lieferanten das Konto und den Steuerschlüssel richtig vorbelegen.
[kivitendo-erp.git] / SL / IS.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Inventory invoicing module
32 #
33 #======================================================================
34
35 package IS;
36
37 #use strict;
38
39 use List::Util qw(max);
40
41 use SL::AM;
42 use SL::ARAP;
43 use SL::CVar;
44 use SL::Common;
45 use SL::DBUtils;
46 use SL::DO;
47 use SL::MoreCommon;
48 use SL::IC;
49 use Data::Dumper;
50
51 sub invoice_details {
52   $main::lxdebug->enter_sub();
53
54   my ($self, $myconfig, $form, $locale) = @_;
55
56   $form->{duedate} ||= $form->{invdate};
57
58   # connect to database
59   my $dbh = $form->dbconnect($myconfig);
60   my $sth;
61
62   my $query = qq|SELECT date | . conv_dateq($form->{duedate}) . qq| - date | . conv_dateq($form->{invdate}) . qq| AS terms|;
63   ($form->{terms}) = selectrow_query($form, $dbh, $query);
64
65   my (@project_ids, %projectnumbers);
66
67   push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
68
69   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
70   my %price_factors;
71
72   foreach my $pfac (@{ $form->{ALL_PRICE_FACTORS} }) {
73     $price_factors{$pfac->{id}}  = $pfac;
74     $pfac->{factor}             *= 1;
75     $pfac->{formatted_factor}    = $form->format_amount($myconfig, $pfac->{factor});
76   }
77
78   # sort items by partsgroup
79   for my $i (1 .. $form->{rowcount}) {
80 #    $partsgroup = "";
81 #    if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
82 #      $partsgroup = $form->{"partsgroup_$i"};
83 #    }
84 #    push @partsgroup, [$i, $partsgroup];
85     push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
86   }
87
88   if (@project_ids) {
89     $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
90       join(", ", map({ "?" } @project_ids)) . ")";
91     $sth = $dbh->prepare($query);
92     $sth->execute(@project_ids) ||
93       $form->dberror($query . " (" . join(", ", @project_ids) . ")");
94     while (my $ref = $sth->fetchrow_hashref()) {
95       $projectnumbers{$ref->{id}} = $ref->{projectnumber};
96     }
97     $sth->finish();
98   }
99
100   $form->{"globalprojectnumber"} =
101     $projectnumbers{$form->{"globalproject_id"}};
102
103   my $tax = 0;
104   my $item;
105   my $i;
106   my @partsgroup = ();
107   my $partsgroup;
108   my %oid = ('Pg'     => 'oid',
109              'Oracle' => 'rowid');
110
111   # sort items by partsgroup
112   for $i (1 .. $form->{rowcount}) {
113     $partsgroup = "";
114     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
115       $partsgroup = $form->{"partsgroup_$i"};
116     }
117     push @partsgroup, [$i, $partsgroup];
118   }
119
120   my $sameitem = "";
121   my @taxaccounts;
122   my %taxaccounts;
123   my %taxbase;
124   my $taxrate;
125   my $taxamount;
126   my $taxbase;
127   my $taxdiff;
128   my $nodiscount;
129   my $yesdiscount;
130   my $nodiscount_subtotal = 0;
131   my $discount_subtotal = 0;
132   my $position = 0;
133   my $subtotal_header = 0;
134   my $subposition = 0;
135
136   $form->{discount} = [];
137
138   IC->prepare_parts_for_printing();
139
140   my @arrays =
141     qw(runningnumber number description longdescription qty ship unit bin
142        deliverydate_oe ordnumber_oe transdate_oe licensenumber validuntil
143        partnotes serialnumber reqdate sellprice listprice netprice
144        discount p_discount discount_sub nodiscount_sub
145        linetotal  nodiscount_linetotal tax_rate projectnumber
146        price_factor price_factor_name partsgroup);
147
148   my @tax_arrays = qw(taxbase tax taxdescription taxrate taxnumber);
149
150   my @payment_arrays = qw(payment paymentaccount paymentdate paymentsource paymentmemo);
151
152   $form->{TEMPLATE_ARRAYS} = { map { $_ => [] } (@arrays, @tax_arrays, @payment_arrays) };
153
154   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
155     $i = $item->[0];
156
157     if ($item->[1] ne $sameitem) {
158       push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, qq|$item->[1]|);
159       $sameitem = $item->[1];
160
161       map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
162     }
163
164     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
165
166     if ($form->{"id_$i"} != 0) {
167
168       # add number, description and qty to $form->{number},
169       if ($form->{"subtotal_$i"} && !$subtotal_header) {
170         $subtotal_header = $i;
171         $position = int($position);
172         $subposition = 0;
173         $position++;
174       } elsif ($subtotal_header) {
175         $subposition += 1;
176         $position = int($position);
177         $position = $position.".".$subposition;
178       } else {
179         $position = int($position);
180         $position++;
181       }
182
183       my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
184
185       push @{ $form->{TEMPLATE_ARRAYS}->{runningnumber} },     $position;
186       push @{ $form->{TEMPLATE_ARRAYS}->{number} },            $form->{"partnumber_$i"};
187       push @{ $form->{TEMPLATE_ARRAYS}->{serialnumber} },      $form->{"serialnumber_$i"};
188       push @{ $form->{TEMPLATE_ARRAYS}->{bin} },               $form->{"bin_$i"};
189       push @{ $form->{TEMPLATE_ARRAYS}->{partnotes} },         $form->{"partnotes_$i"};
190       push @{ $form->{TEMPLATE_ARRAYS}->{description} },       $form->{"description_$i"};
191       push @{ $form->{TEMPLATE_ARRAYS}->{longdescription} },   $form->{"longdescription_$i"};
192       push @{ $form->{TEMPLATE_ARRAYS}->{qty} },               $form->format_amount($myconfig, $form->{"qty_$i"});
193       push @{ $form->{TEMPLATE_ARRAYS}->{unit} },              $form->{"unit_$i"};
194       push @{ $form->{TEMPLATE_ARRAYS}->{deliverydate_oe} },   $form->{"deliverydate_$i"};
195       push @{ $form->{TEMPLATE_ARRAYS}->{sellprice} },         $form->{"sellprice_$i"};
196       push @{ $form->{TEMPLATE_ARRAYS}->{ordnumber_oe} },      $form->{"ordnumber_$i"};
197       push @{ $form->{TEMPLATE_ARRAYS}->{transdate_oe} },      $form->{"transdate_$i"};
198       push @{ $form->{TEMPLATE_ARRAYS}->{invnumber} },         $form->{"invnumber"};
199       push @{ $form->{TEMPLATE_ARRAYS}->{invdate} },           $form->{"invdate"};
200       push @{ $form->{TEMPLATE_ARRAYS}->{price_factor} },      $price_factor->{formatted_factor};
201       push @{ $form->{TEMPLATE_ARRAYS}->{price_factor_name} }, $price_factor->{description};
202       push @{ $form->{TEMPLATE_ARRAYS}->{partsgroup} },        $form->{"partsgroup_$i"};
203       push @{ $form->{TEMPLATE_ARRAYS}->{reqdate} },           $form->{"reqdate_$i"};
204
205       if ($form->{lizenzen}) {
206         if ($form->{"licensenumber_$i"}) {
207           $query = qq|SELECT licensenumber, validuntil FROM license WHERE id = ?|;
208           my ($licensenumber, $validuntil) = selectrow_query($form, $dbh, $query, conv_i($form->{"licensenumber_$i"}));
209           push(@{ $form->{TEMPLATE_ARRAYS}->{licensenumber} }, $licensenumber);
210           push(@{ $form->{TEMPLATE_ARRAYS}->{validuntil} }, $locale->date($myconfig, $validuntil, 0));
211
212         } else {
213           push(@{ $form->{TEMPLATE_ARRAYS}->{licensenumber} }, "");
214           push(@{ $form->{TEMPLATE_ARRAYS}->{validuntil} },    "");
215         }
216       }
217
218       # listprice
219       push(@{ $form->{TEMPLATE_ARRAYS}->{listprice} }, $form->{"listprice_$i"});
220
221       my $sellprice     = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
222       my ($dec)         = ($sellprice =~ /\.(\d+)/);
223       my $decimalplaces = max 2, length($dec);
224
225       my $parsed_discount      = $form->parse_amount($myconfig, $form->{"discount_$i"});
226       my $linetotal_exact      =                     $form->{"qty_$i"} * $sellprice * (100 - $parsed_discount) / 100 / $price_factor->{factor};
227       my $linetotal            = $form->round_amount($linetotal_exact, 2);
228       my $discount             = $form->round_amount($form->{"qty_$i"} * $sellprice * $parsed_discount / 100 / $price_factor->{factor} - ($linetotal - $linetotal_exact),
229                                                      $decimalplaces);
230       my $nodiscount_linetotal = $form->round_amount($form->{"qty_$i"} * $sellprice / $price_factor->{factor}, 2);
231       $form->{"netprice_$i"}   = $form->round_amount($form->{"qty_$i"} ? ($linetotal / $form->{"qty_$i"}) : 0, 2);
232
233       push @{ $form->{TEMPLATE_ARRAYS}->{netprice} }, ($form->{"netprice_$i"} != 0) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : '';
234
235       $linetotal = ($linetotal != 0) ? $linetotal : '';
236
237       push @{ $form->{TEMPLATE_ARRAYS}->{discount} },   ($discount  != 0) ? $form->format_amount($myconfig, $discount * -1, 2) : '';
238       push @{ $form->{TEMPLATE_ARRAYS}->{p_discount} }, $form->{"discount_$i"};
239
240       $form->{total}            += $linetotal;
241       $form->{nodiscount_total} += $nodiscount_linetotal;
242       $form->{discount_total}   += $discount;
243
244       if ($subtotal_header) {
245         $discount_subtotal   += $linetotal;
246         $nodiscount_subtotal += $nodiscount_linetotal;
247       }
248
249       if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
250         push @{ $form->{TEMPLATE_ARRAYS}->{discount_sub} },   $form->format_amount($myconfig, $discount_subtotal,   2);
251         push @{ $form->{TEMPLATE_ARRAYS}->{nodiscount_sub} }, $form->format_amount($myconfig, $nodiscount_subtotal, 2);
252
253         $discount_subtotal   = 0;
254         $nodiscount_subtotal = 0;
255         $subtotal_header     = 0;
256
257       } else {
258         push @{ $form->{TEMPLATE_ARRAYS}->{discount_sub} },   "";
259         push @{ $form->{TEMPLATE_ARRAYS}->{nodiscount_sub} }, "";
260       }
261
262       if (!$form->{"discount_$i"}) {
263         $nodiscount += $linetotal;
264       }
265
266       push @{ $form->{TEMPLATE_ARRAYS}->{linetotal} }, $form->format_amount($myconfig, $linetotal, 2);
267       push @{ $form->{TEMPLATE_ARRAYS}->{nodiscount_linetotal} }, $form->format_amount($myconfig, $nodiscount_linetotal, 2);
268
269       push(@{ $form->{TEMPLATE_ARRAYS}->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
270
271       @taxaccounts = split(/ /, $form->{"taxaccounts_$i"});
272       $taxrate     = 0;
273       $taxdiff     = 0;
274
275       map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
276
277       if ($form->{taxincluded}) {
278
279         # calculate tax
280         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
281         $taxbase = $linetotal - $taxamount;
282       } else {
283         $taxamount = $linetotal * $taxrate;
284         $taxbase   = $linetotal;
285       }
286
287       if ($form->round_amount($taxrate, 7) == 0) {
288         if ($form->{taxincluded}) {
289           foreach my $accno (@taxaccounts) {
290             $taxamount            = $form->round_amount($linetotal * $form->{"${accno}_rate"} / (1 + abs($form->{"${accno}_rate"})), 2);
291
292             $taxaccounts{$accno} += $taxamount;
293             $taxdiff             += $taxamount;
294
295             $taxbase{$accno}     += $taxbase;
296           }
297           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
298         } else {
299           foreach my $accno (@taxaccounts) {
300             $taxaccounts{$accno} += $linetotal * $form->{"${accno}_rate"};
301             $taxbase{$accno}     += $taxbase;
302           }
303         }
304       } else {
305         foreach my $accno (@taxaccounts) {
306           $taxaccounts{$accno} += $taxamount * $form->{"${accno}_rate"} / $taxrate;
307           $taxbase{$accno}     += $taxbase;
308         }
309       }
310       my $tax_rate = $taxrate * 100;
311       push(@{ $form->{TEMPLATE_ARRAYS}->{tax_rate} }, qq|$tax_rate|);
312       if ($form->{"assembly_$i"}) {
313         $sameitem = "";
314
315         # get parts and push them onto the stack
316         my $sortorder = "";
317         if ($form->{groupitems}) {
318           $sortorder =
319             qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
320         } else {
321           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
322         }
323
324         $query =
325           qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
326              FROM assembly a
327              JOIN parts p ON (a.parts_id = p.id)
328              LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
329              WHERE (a.bom = '1') AND (a.id = ?) $sortorder|;
330         $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
331
332         while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
333           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
334             map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
335             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
336             push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
337           }
338
339           map { $form->{"a_$_"} = $ref->{$_} } qw(partnumber description);
340
341           push(@{ $form->{TEMPLATE_ARRAYS}->{description} },
342                $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
343                  )
344                  . qq| -- $form->{"a_partnumber"}, $form->{"a_description"}|);
345           map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
346
347         }
348         $sth->finish;
349       }
350     }
351   }
352
353   foreach my $item (sort keys %taxaccounts) {
354     $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
355
356     push(@{ $form->{TEMPLATE_ARRAYS}->{taxbase} },        $form->format_amount($myconfig, $taxbase{$item}, 2));
357     push(@{ $form->{TEMPLATE_ARRAYS}->{tax} },            $form->format_amount($myconfig, $taxamount,      2));
358     push(@{ $form->{TEMPLATE_ARRAYS}->{taxrate} },        $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
359     push(@{ $form->{TEMPLATE_ARRAYS}->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
360     push(@{ $form->{TEMPLATE_ARRAYS}->{taxnumber} },      $form->{"${item}_taxnumber"});
361   }
362
363   for my $i (1 .. $form->{paidaccounts}) {
364     if ($form->{"paid_$i"}) {
365       my ($accno, $description) = split(/--/, $form->{"AR_paid_$i"});
366
367       push(@{ $form->{TEMPLATE_ARRAYS}->{payment} },        $form->{"paid_$i"});
368       push(@{ $form->{TEMPLATE_ARRAYS}->{paymentaccount} }, $description);
369       push(@{ $form->{TEMPLATE_ARRAYS}->{paymentdate} },    $form->{"datepaid_$i"});
370       push(@{ $form->{TEMPLATE_ARRAYS}->{paymentsource} },  $form->{"source_$i"});
371       push(@{ $form->{TEMPLATE_ARRAYS}->{paymentmemo} },    $form->{"memo_$i"});
372
373       $form->{paid} += $form->parse_amount($myconfig, $form->{"paid_$i"});
374     }
375   }
376   if($form->{taxincluded}) {
377     $form->{subtotal} = $form->format_amount($myconfig, $form->{total} - $tax, 2);
378   }
379   else {
380     $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
381   }
382
383   $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
384   $form->{discount_total}      = $form->format_amount($myconfig, $form->{discount_total}, 2);
385   $form->{nodiscount}          = $form->format_amount($myconfig, $nodiscount, 2);
386   $form->{yesdiscount}         = $form->format_amount($myconfig, $form->{nodiscount_total} - $nodiscount, 2);
387
388   $form->{invtotal} = ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
389   $form->{total}    = $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
390
391   $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
392   $form->{paid}     = $form->format_amount($myconfig, $form->{paid}, 2);
393
394   $form->set_payment_options($myconfig, $form->{invdate});
395
396   $form->{username} = $myconfig->{name};
397
398   $dbh->disconnect;
399
400   $main::lxdebug->leave_sub();
401 }
402
403 sub project_description {
404   $main::lxdebug->enter_sub();
405
406   my ($self, $dbh, $id) = @_;
407   my $form = \%main::form;
408
409   my $query = qq|SELECT description FROM project WHERE id = ?|;
410   my ($description) = selectrow_query($form, $dbh, $query, conv_i($id));
411
412   $main::lxdebug->leave_sub();
413
414   return $_;
415 }
416
417 sub customer_details {
418   $main::lxdebug->enter_sub();
419
420   my ($self, $myconfig, $form, @wanted_vars) = @_;
421
422   # connect to database
423   my $dbh = $form->dbconnect($myconfig);
424
425   # get contact id, set it if nessessary
426   $form->{cp_id} *= 1;
427
428   my @values =  (conv_i($form->{customer_id}));
429
430   my $where = "";
431   if ($form->{cp_id}) {
432     $where = qq| AND (cp.cp_id = ?) |;
433     push(@values, conv_i($form->{cp_id}));
434   }
435
436   # get rest for the customer
437   my $query =
438     qq|SELECT ct.*, cp.*, ct.notes as customernotes,
439          ct.phone AS customerphone, ct.fax AS customerfax, ct.email AS customeremail
440        FROM customer ct
441        LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
442        WHERE (ct.id = ?) $where
443        ORDER BY cp.cp_id
444        LIMIT 1|;
445   my $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
446
447   # remove id and taxincluded before copy back
448   delete @$ref{qw(id taxincluded)};
449
450   @wanted_vars = grep({ $_ } @wanted_vars);
451   if (scalar(@wanted_vars) > 0) {
452     my %h_wanted_vars;
453     map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
454     map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
455   }
456
457   map { $form->{$_} = $ref->{$_} } keys %$ref;
458
459   if ($form->{delivery_customer_id}) {
460     $query =
461       qq|SELECT *, notes as customernotes
462          FROM customer
463          WHERE id = ?
464          LIMIT 1|;
465     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{delivery_customer_id}));
466
467     map { $form->{"dc_$_"} = $ref->{$_} } keys %$ref;
468   }
469
470   if ($form->{delivery_vendor_id}) {
471     $query =
472       qq|SELECT *, notes as customernotes
473          FROM customer
474          WHERE id = ?
475          LIMIT 1|;
476     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{delivery_vendor_id}));
477
478     map { $form->{"dv_$_"} = $ref->{$_} } keys %$ref;
479   }
480
481   my $custom_variables = CVar->get_custom_variables('dbh'      => $dbh,
482                                                     'module'   => 'CT',
483                                                     'trans_id' => $form->{customer_id});
484   map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables };
485
486   $dbh->disconnect;
487
488   $main::lxdebug->leave_sub();
489 }
490
491 sub post_invoice {
492   $main::lxdebug->enter_sub();
493
494   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
495
496   # connect to database, turn off autocommit
497   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
498
499   my ($query, $sth, $null, $project_id, @values);
500   my $exchangerate = 0;
501
502   if (!$form->{employee_id}) {
503     $form->get_employee($dbh);
504   }
505
506   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
507
508   ($null, $form->{department_id}) = split(/--/, $form->{department});
509
510   my $all_units = AM->retrieve_units($myconfig, $form);
511
512   if (!$payments_only) {
513     if ($form->{id}) {
514       &reverse_invoice($dbh, $form);
515
516     } else {
517       $query = qq|SELECT nextval('glid')|;
518       ($form->{"id"}) = selectrow_query($form, $dbh, $query);
519
520       $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
521       do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
522
523       if (!$form->{invnumber}) {
524         $form->{invnumber} =
525           $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
526                                  "cnnumber" : "invnumber", $dbh);
527       }
528     }
529   }
530
531   my ($netamount, $invoicediff) = (0, 0);
532   my ($amount, $linetotal, $lastincomeaccno);
533
534   my ($currencies)    = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|);
535   my $defaultcurrency = (split m/:/, $currencies)[0];
536
537   if ($form->{currency} eq $defaultcurrency) {
538     $form->{exchangerate} = 1;
539   } else {
540     $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
541   }
542
543   $form->{exchangerate} =
544     ($exchangerate)
545     ? $exchangerate
546     : $form->parse_amount($myconfig, $form->{exchangerate});
547
548   $form->{expense_inventory} = "";
549
550   my %baseunits;
551
552   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
553   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
554   my $price_factor;
555
556   $form->{amount}      = {};
557   $form->{amount_cogs} = {};
558
559   foreach my $i (1 .. $form->{rowcount}) {
560     if ($form->{type} eq "credit_note") {
561       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}) * -1;
562       $form->{shipped} = 1;
563     } else {
564       $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
565     }
566     my $basefactor;
567     my $baseqty;
568
569     $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
570     $form->{"marge_total_$i"} = $form->parse_amount($myconfig, $form->{"marge_total_$i"}) * 1;
571     $form->{"lastcost_$i"} = $form->{"lastcost_$i"} * 1;
572
573     if ($form->{storno}) {
574       $form->{"qty_$i"} *= -1;
575     }
576
577     if ($form->{"id_$i"}) {
578       my $item_unit;
579
580       if (defined($baseunits{$form->{"id_$i"}})) {
581         $item_unit = $baseunits{$form->{"id_$i"}};
582       } else {
583         # get item baseunit
584         $query = qq|SELECT unit FROM parts WHERE id = ?|;
585         ($item_unit) = selectrow_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
586         $baseunits{$form->{"id_$i"}} = $item_unit;
587       }
588
589       if (defined($all_units->{$item_unit}->{factor})
590           && ($all_units->{$item_unit}->{factor} ne '')
591           && ($all_units->{$item_unit}->{factor} != 0)) {
592         $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
593       } else {
594         $basefactor = 1;
595       }
596       $baseqty = $form->{"qty_$i"} * $basefactor;
597
598       my ($allocated, $taxrate) = (0, 0);
599       my $taxamount;
600
601       # add tax rates
602       map { $taxrate += $form->{"${_}_rate"} } split(/ /, $form->{"taxaccounts_$i"});
603
604       # keep entered selling price
605       my $fxsellprice =
606         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
607
608       my ($dec) = ($fxsellprice =~ /\.(\d+)/);
609       $dec = length $dec;
610       my $decimalplaces = ($dec > 2) ? $dec : 2;
611
612       # undo discount formatting
613       $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
614
615       # deduct discount
616       $form->{"sellprice_$i"} = $fxsellprice * (1 - $form->{"discount_$i"});
617
618       # round linetotal to 2 decimal places
619       $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
620       $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
621
622       if ($form->{taxincluded}) {
623         $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
624         $form->{"sellprice_$i"} =
625           $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
626       } else {
627         $taxamount = $linetotal * $taxrate;
628       }
629
630       $netamount += $linetotal;
631
632       if ($taxamount != 0) {
633         map {
634           $form->{amount}{ $form->{id} }{$_} +=
635             $taxamount * $form->{"${_}_rate"} / $taxrate
636         } split(/ /, $form->{"taxaccounts_$i"});
637       }
638
639       # add amount to income, $form->{amount}{trans_id}{accno}
640       $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate} / $price_factor;
641
642       $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2) * $form->{exchangerate};
643       $linetotal = $form->round_amount($linetotal, 2);
644
645       # this is the difference from the inventory
646       $invoicediff += ($amount - $linetotal);
647
648       $form->{amount}{ $form->{id} }{ $form->{"income_accno_$i"} } +=
649         $linetotal;
650
651       $lastincomeaccno = $form->{"income_accno_$i"};
652
653       # adjust and round sellprice
654       $form->{"sellprice_$i"} =
655         $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
656                             $decimalplaces);
657
658       next if $payments_only;
659
660       if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
661
662         if ($form->{"assembly_$i"}) {
663           # record assembly item as allocated
664           &process_assembly($dbh, $form, $form->{"id_$i"}, $baseqty);
665
666         } else {
667           $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
668         }
669       }
670
671       # get pricegroup_id and save it
672       ($null, my $pricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
673       $pricegroup_id *= 1;
674
675       # save detail record in invoice table
676       $query =
677         qq|INSERT INTO invoice (trans_id, parts_id, description, longdescription, qty,
678                                 sellprice, fxsellprice, discount, allocated, assemblyitem,
679                                 unit, deliverydate, project_id, serialnumber, pricegroup_id,
680                                 ordnumber, transdate, cusordnumber, base_qty, subtotal,
681                                 marge_percent, marge_total, lastcost,
682                                 price_factor_id, price_factor, marge_price_factor)
683            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
684                    (SELECT factor FROM price_factors WHERE id = ?), ?)|;
685
686       @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
687                  $form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"},
688                  $form->{"sellprice_$i"}, $fxsellprice,
689                  $form->{"discount_$i"}, $allocated, 'f',
690                  $form->{"unit_$i"}, conv_date($form->{"reqdate_$i"}), conv_i($form->{"project_id_$i"}),
691                  $form->{"serialnumber_$i"}, conv_i($pricegroup_id),
692                  $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
693                  $form->{"cusordnumber_$i"}, $baseqty, $form->{"subtotal_$i"} ? 't' : 'f',
694                  $form->{"marge_percent_$i"}, $form->{"marge_total_$i"},
695                  $form->{"lastcost_$i"},
696                  conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
697                  conv_i($form->{"marge_price_factor_$i"}));
698       do_query($form, $dbh, $query, @values);
699
700       if ($form->{lizenzen} && $form->{"licensenumber_$i"}) {
701         $query =
702           qq|INSERT INTO licenseinvoice (trans_id, license_id)
703              VALUES ((SELECT id FROM invoice WHERE trans_id = ? ORDER BY oid DESC LIMIT 1), ?)|;
704         @values = (conv_i($form->{"id"}), conv_i($form->{"licensenumber_$i"}));
705         do_query($form, $dbh, $query, @values);
706       }
707     }
708   }
709
710   $form->{datepaid} = $form->{invdate};
711
712   # total payments, don't move we need it here
713   for my $i (1 .. $form->{paidaccounts}) {
714     if ($form->{type} eq "credit_note") {
715       $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
716     } else {
717       $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
718     }
719     $form->{paid} += $form->{"paid_$i"};
720     $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
721   }
722
723   my ($tax, $diff) = (0, 0);
724
725   $netamount = $form->round_amount($netamount, 2);
726
727   # figure out rounding errors for total amount vs netamount + taxes
728   if ($form->{taxincluded}) {
729
730     $amount = $form->round_amount($netamount * $form->{exchangerate}, 2);
731     $diff += $amount - $netamount * $form->{exchangerate};
732     $netamount = $amount;
733
734     foreach my $item (split(/ /, $form->{taxaccounts})) {
735       $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
736       $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
737       $tax += $form->{amount}{ $form->{id} }{$item};
738       $netamount -= $form->{amount}{ $form->{id} }{$item};
739     }
740
741     $invoicediff += $diff;
742     ######## this only applies to tax included
743     if ($lastincomeaccno) {
744       $form->{amount}{ $form->{id} }{$lastincomeaccno} += $invoicediff;
745     }
746
747   } else {
748     $amount    = $form->round_amount($netamount * $form->{exchangerate}, 2);
749     $diff      = $amount - $netamount * $form->{exchangerate};
750     $netamount = $amount;
751     foreach my $item (split(/ /, $form->{taxaccounts})) {
752       $form->{amount}{ $form->{id} }{$item} =
753         $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
754       $amount =
755         $form->round_amount(
756                  $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate},
757                  2);
758       $diff +=
759         $amount - $form->{amount}{ $form->{id} }{$item} *
760         $form->{exchangerate};
761       $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
762       $tax += $form->{amount}{ $form->{id} }{$item};
763     }
764   }
765
766   $form->{amount}{ $form->{id} }{ $form->{AR} } = $netamount + $tax;
767   $form->{paid} =
768     $form->round_amount($form->{paid} * $form->{exchangerate} + $diff, 2);
769
770   # reverse AR
771   $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
772
773   # update exchangerate
774   if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
775     $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
776                                $form->{exchangerate}, 0);
777   }
778
779   $project_id = conv_i($form->{"globalproject_id"});
780
781   foreach my $trans_id (keys %{ $form->{amount_cogs} }) {
782     foreach my $accno (keys %{ $form->{amount_cogs}{$trans_id} }) {
783       next unless ($form->{expense_inventory} =~ /\Q$accno\E/);
784
785       $form->{amount_cogs}{$trans_id}{$accno} = $form->round_amount($form->{amount_cogs}{$trans_id}{$accno}, 2);
786
787       if (!$payments_only && ($form->{amount_cogs}{$trans_id}{$accno} != 0)) {
788         $query =
789           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
790                VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 0, ?)|;
791         @values = (conv_i($trans_id), $accno, $form->{amount_cogs}{$trans_id}{$accno}, conv_date($form->{invdate}), conv_i($project_id));
792         do_query($form, $dbh, $query, @values);
793         $form->{amount_cogs}{$trans_id}{$accno} = 0;
794       }
795     }
796
797     foreach my $accno (keys %{ $form->{amount_cogs}{$trans_id} }) {
798       $form->{amount_cogs}{$trans_id}{$accno} = $form->round_amount($form->{amount_cogs}{$trans_id}{$accno}, 2);
799
800       if (!$payments_only && ($form->{amount_cogs}{$trans_id}{$accno} != 0)) {
801         $query =
802           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
803                VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 0, ?)|;
804         @values = (conv_i($trans_id), $accno, $form->{amount_cogs}{$trans_id}{$accno}, conv_date($form->{invdate}), conv_i($project_id));
805         do_query($form, $dbh, $query, @values);
806       }
807     }
808   }
809
810   foreach my $trans_id (keys %{ $form->{amount} }) {
811     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
812       next unless ($form->{expense_inventory} =~ /\Q$accno\E/);
813
814       $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
815
816       if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
817         $query =
818           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
819              VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
820                      (SELECT taxkey_id  FROM chart WHERE accno = ?), ?)|;
821         @values = (conv_i($trans_id), $accno, $form->{amount}{$trans_id}{$accno}, conv_date($form->{invdate}), $accno, conv_i($project_id));
822         do_query($form, $dbh, $query, @values);
823         $form->{amount}{$trans_id}{$accno} = 0;
824       }
825     }
826
827     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
828       $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
829
830       if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
831         $query =
832           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
833              VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
834                      (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
835         @values = (conv_i($trans_id), $accno, $form->{amount}{$trans_id}{$accno}, conv_date($form->{invdate}), $accno, conv_i($project_id));
836         do_query($form, $dbh, $query, @values);
837       }
838     }
839   }
840
841   # deduct payment differences from diff
842   for my $i (1 .. $form->{paidaccounts}) {
843     if ($form->{"paid_$i"} != 0) {
844       $amount =
845         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
846       $diff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
847     }
848   }
849
850   # record payments and offsetting AR
851   if (!$form->{storno}) {
852     for my $i (1 .. $form->{paidaccounts}) {
853
854       next if ($form->{"paid_$i"} == 0);
855
856       my ($accno) = split(/--/, $form->{"AR_paid_$i"});
857       $form->{"datepaid_$i"} = $form->{invdate}
858       unless ($form->{"datepaid_$i"});
859       $form->{datepaid} = $form->{"datepaid_$i"};
860
861       $exchangerate = 0;
862
863       if ($form->{currency} eq $defaultcurrency) {
864         $form->{"exchangerate_$i"} = 1;
865       } else {
866         $exchangerate              = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
867         $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
868       }
869
870       # record AR
871       $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff, 2);
872
873       if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
874         $query =
875         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
876            VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
877                    (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
878         @values = (conv_i($form->{"id"}), $form->{AR}, $amount, $form->{"datepaid_$i"}, $form->{AR}, $project_id);
879         do_query($form, $dbh, $query, @values);
880       }
881
882       # record payment
883       $form->{"paid_$i"} *= -1;
884
885       $query =
886       qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, taxkey, project_id)
887          VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?,
888                  (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
889       @values = (conv_i($form->{"id"}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"},
890                  $form->{"source_$i"}, $form->{"memo_$i"}, $accno, $project_id);
891       do_query($form, $dbh, $query, @values);
892
893       # exchangerate difference
894       $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
895       $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
896
897       # gain/loss
898       $amount =
899       $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
900       $form->{"exchangerate_$i"};
901       if ($amount > 0) {
902         $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
903         $amount;
904       } else {
905         $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
906         $amount;
907       }
908
909       $diff = 0;
910
911       # update exchange rate
912       if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
913         $form->update_exchangerate($dbh, $form->{currency},
914                                    $form->{"datepaid_$i"},
915                                    $form->{"exchangerate_$i"}, 0);
916       }
917     }
918
919   } else {                      # if (!$form->{storno})
920     $form->{marge_total} *= -1;
921   }
922
923   if ($payments_only) {
924     $query = qq|UPDATE ar SET paid = ?, datepaid = ? WHERE id = ?|;
925     do_query($form, $dbh, $query,  $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
926
927     if (!$provided_dbh) {
928       $dbh->commit();
929       $dbh->disconnect();
930     }
931
932     $main::lxdebug->leave_sub();
933     return;
934   }
935
936   # record exchange rate differences and gains/losses
937   foreach my $accno (keys %{ $form->{fx} }) {
938     foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
939       if (
940           ($form->{fx}{$accno}{$transdate} =
941            $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
942           ) != 0
943         ) {
944
945         $query =
946           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey, project_id)
947              VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, '0', '1',
948              (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
949         @values = (conv_i($form->{"id"}), $accno, $form->{fx}{$accno}{$transdate}, conv_date($transdate), $accno, $project_id);
950         do_query($form, $dbh, $query, @values);
951       }
952     }
953   }
954
955   $amount = $netamount + $tax;
956
957   # save AR record
958   #erweiterung fuer lieferscheinnummer (donumber) 12.02.09 jb
959
960   $query = qq|UPDATE ar set
961                 invnumber   = ?, ordnumber     = ?, quonumber     = ?, cusordnumber  = ?,
962                 transdate   = ?, orddate       = ?, quodate       = ?, customer_id   = ?,
963                 amount      = ?, netamount     = ?, paid          = ?, datepaid      = ?,
964                 duedate     = ?, deliverydate  = ?, invoice       = ?, shippingpoint = ?,
965                 shipvia     = ?, terms         = ?, notes         = ?, intnotes      = ?,
966                 curr        = ?, department_id = ?, payment_id    = ?, taxincluded   = ?,
967                 type        = ?, language_id   = ?, taxzone_id    = ?, shipto_id     = ?,
968                 employee_id = ?, salesman_id   = ?, storno_id     = ?, storno        = ?,
969                 cp_id       = ?, marge_total   = ?, marge_percent = ?,
970                 globalproject_id               = ?, delivery_customer_id             = ?,
971                 transaction_description        = ?, delivery_vendor_id               = ?,
972                 donumber    = ?
973               WHERE id = ?|;
974   @values = (          $form->{"invnumber"},           $form->{"ordnumber"},             $form->{"quonumber"},          $form->{"cusordnumber"},
975              conv_date($form->{"invdate"}),  conv_date($form->{"orddate"}),    conv_date($form->{"quodate"}),    conv_i($form->{"customer_id"}),
976                        $amount,                        $netamount,                       $form->{"paid"},     conv_date($form->{"datepaid"}),
977              conv_date($form->{"duedate"}),  conv_date($form->{"deliverydate"}),    '1',                                $form->{"shippingpoint"},
978                        $form->{"shipvia"},      conv_i($form->{"terms"}),                $form->{"notes"},              $form->{"intnotes"},
979                        $form->{"currency"},     conv_i($form->{"department_id"}), conv_i($form->{"payment_id"}),        $form->{"taxincluded"} ? 't' : 'f',
980                        $form->{"type"},         conv_i($form->{"language_id"}),   conv_i($form->{"taxzone_id"}), conv_i($form->{"shipto_id"}),
981                 conv_i($form->{"employee_id"}), conv_i($form->{"salesman_id"}),   conv_i($form->{storno_id}),           $form->{"storno"} ? 't' : 'f',
982                 conv_i($form->{"cp_id"}),            1 * $form->{marge_total} ,      1 * $form->{marge_percent},
983                 conv_i($form->{"globalproject_id"}),                              conv_i($form->{"delivery_customer_id"}),
984                        $form->{transaction_description},                          conv_i($form->{"delivery_vendor_id"}),
985                        $form->{"donumber"}, #das entsprechende feld lieferscheinnummer aus der html-form 12.02.09 jb
986                 conv_i($form->{"id"}));
987   do_query($form, $dbh, $query, @values);
988
989   if($form->{"formname"} eq "credit_note") {
990     for my $i (1 .. $form->{rowcount}) {
991       $query = qq|UPDATE parts SET onhand = onhand - ? WHERE id = ?|;
992       @values = (conv_i($form->{"qty_$i"}), conv_i($form->{"id_$i"}));
993       do_query($form, $dbh, $query, @values);
994     }
995   }
996
997   if ($form->{storno}) {
998     $query =
999       qq!UPDATE ar SET
1000            paid = paid + amount,
1001            storno = 't',
1002            intnotes = ? || intnotes
1003          WHERE id = ?!;
1004     do_query($form, $dbh, $query, "Rechnung storniert am $form->{invdate} ", conv_i($form->{"storno_id"}));
1005     do_query($form, $dbh, qq|UPDATE ar SET paid = amount WHERE id = ?|, conv_i($form->{"id"}));
1006   }
1007
1008   # add shipto
1009   $form->{name} = $form->{customer};
1010   $form->{name} =~ s/--\Q$form->{customer_id}\E//;
1011
1012   if (!$form->{shipto_id}) {
1013     $form->add_shipto($dbh, $form->{id}, "AR");
1014   }
1015
1016   # save printed, emailed and queued
1017   $form->save_status($dbh);
1018
1019   Common::webdav_folder($form) if ($main::webdav);
1020
1021   # Link this record to the records it was created from.
1022   RecordLinks->create_links('dbh'        => $dbh,
1023                             'mode'       => 'ids',
1024                             'from_table' => 'oe',
1025                             'from_ids'   => $form->{convert_from_oe_ids},
1026                             'to_table'   => 'ar',
1027                             'to_id'      => $form->{id},
1028     );
1029   delete $form->{convert_from_oe_ids};
1030
1031   my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
1032
1033   if (scalar @convert_from_do_ids) {
1034     DO->close_orders('dbh' => $dbh,
1035                      'ids' => \@convert_from_do_ids);
1036
1037     RecordLinks->create_links('dbh'        => $dbh,
1038                               'mode'       => 'ids',
1039                               'from_table' => 'delivery_orders',
1040                               'from_ids'   => \@convert_from_do_ids,
1041                               'to_table'   => 'ar',
1042                               'to_id'      => $form->{id},
1043       );
1044   }
1045   delete $form->{convert_from_do_ids};
1046
1047   ARAP->close_orders_if_billed('dbh'     => $dbh,
1048                                'arap_id' => $form->{id},
1049                                'table'   => 'ar',);
1050
1051   my $rc = 1;
1052   if (!$provided_dbh) {
1053     $dbh->commit();
1054     $dbh->disconnect();
1055   }
1056
1057   $main::lxdebug->leave_sub();
1058
1059   return $rc;
1060 }
1061
1062 sub _delete_payments {
1063   $main::lxdebug->enter_sub();
1064
1065   my ($self, $form, $dbh) = @_;
1066
1067   my @delete_acc_trans_ids;
1068
1069   # Delete old payment entries from acc_trans.
1070   my $query =
1071     qq|SELECT acc_trans_id
1072        FROM acc_trans
1073        WHERE (trans_id = ?) AND fx_transaction
1074
1075        UNION
1076
1077        SELECT at.acc_trans_id
1078        FROM acc_trans at
1079        LEFT JOIN chart c ON (at.chart_id = c.id)
1080        WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|;
1081   push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
1082
1083   $query =
1084     qq|SELECT at.acc_trans_id
1085        FROM acc_trans at
1086        LEFT JOIN chart c ON (at.chart_id = c.id)
1087        WHERE (trans_id = ?)
1088          AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
1089        ORDER BY at.acc_trans_id
1090        OFFSET 1|;
1091   push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
1092
1093   if (@delete_acc_trans_ids) {
1094     $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
1095     do_query($form, $dbh, $query);
1096   }
1097
1098   $main::lxdebug->leave_sub();
1099 }
1100
1101 sub post_payment {
1102   $main::lxdebug->enter_sub();
1103
1104   my ($self, $myconfig, $form, $locale) = @_;
1105
1106   # connect to database, turn off autocommit
1107   my $dbh = $form->dbconnect_noauto($myconfig);
1108
1109   my (%payments, $old_form, $row, $item, $query, %keep_vars);
1110
1111   $old_form = save_form();
1112
1113   # Delete all entries in acc_trans from prior payments.
1114   $self->_delete_payments($form, $dbh);
1115
1116   # Save the new payments the user made before cleaning up $form.
1117   map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AR_paid_\d+$|^paidaccounts$/, keys %{ $form };
1118
1119   # Clean up $form so that old content won't tamper the results.
1120   %keep_vars = map { $_, 1 } qw(login password id);
1121   map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
1122
1123   # Retrieve the invoice from the database.
1124   $self->retrieve_invoice($myconfig, $form);
1125
1126   # Set up the content of $form in the way that IS::post_invoice() expects.
1127   $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
1128
1129   for $row (1 .. scalar @{ $form->{invoice_details} }) {
1130     $item = $form->{invoice_details}->[$row - 1];
1131
1132     map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice discount);
1133
1134     map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
1135   }
1136
1137   $form->{rowcount} = scalar @{ $form->{invoice_details} };
1138
1139   delete @{$form}{qw(invoice_details paidaccounts storno paid)};
1140
1141   # Restore the payment options from the user input.
1142   map { $form->{$_} = $payments{$_} } keys %payments;
1143
1144   # Get the AR accno (which is normally done by Form::create_links()).
1145   $query =
1146     qq|SELECT c.accno
1147        FROM acc_trans at
1148        LEFT JOIN chart c ON (at.chart_id = c.id)
1149        WHERE (trans_id = ?)
1150          AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
1151        ORDER BY at.acc_trans_id
1152        LIMIT 1|;
1153
1154   ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
1155
1156   # Post the new payments.
1157   $self->post_invoice($myconfig, $form, $dbh, 1);
1158
1159   restore_form($old_form);
1160
1161   my $rc = $dbh->commit();
1162   $dbh->disconnect();
1163
1164   $main::lxdebug->leave_sub();
1165
1166   return $rc;
1167 }
1168
1169 sub process_assembly {
1170   $main::lxdebug->enter_sub();
1171
1172   my ($dbh, $form, $id, $totalqty) = @_;
1173
1174   my $query =
1175     qq|SELECT a.parts_id, a.qty, p.assembly, p.partnumber, p.description, p.unit,
1176          p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
1177        FROM assembly a
1178        JOIN parts p ON (a.parts_id = p.id)
1179        WHERE (a.id = ?)|;
1180   my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
1181
1182   while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
1183
1184     my $allocated = 0;
1185
1186     $ref->{inventory_accno_id} *= 1;
1187     $ref->{expense_accno_id}   *= 1;
1188
1189     # multiply by number of assemblies
1190     $ref->{qty} *= $totalqty;
1191
1192     if ($ref->{assembly}) {
1193       &process_assembly($dbh, $form, $ref->{parts_id}, $ref->{qty});
1194       next;
1195     } else {
1196       if ($ref->{inventory_accno_id}) {
1197         $allocated = &cogs($dbh, $form, $ref->{parts_id}, $ref->{qty});
1198       }
1199     }
1200
1201     # save detail record for individual assembly item in invoice table
1202     $query =
1203       qq|INSERT INTO invoice (trans_id, description, parts_id, qty, sellprice, fxsellprice, allocated, assemblyitem, unit)
1204          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1205     my @values = (conv_i($form->{id}), $ref->{description}, conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
1206     do_query($form, $dbh, $query, @values);
1207
1208   }
1209
1210   $sth->finish;
1211
1212   $main::lxdebug->leave_sub();
1213 }
1214
1215 sub cogs {
1216   $main::lxdebug->enter_sub();
1217
1218   my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
1219
1220   $basefactor ||= 1;
1221
1222   $form->{taxzone_id} *=1;
1223   my $transdate  = $form->{invdate} ? $dbh->quote($form->{invdate}) : "current_date";
1224   my $taxzone_id = $form->{"taxzone_id"} * 1;
1225   my $query =
1226     qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
1227          c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from AS inventory_valid,
1228          c2.accno AS    income_accno, c2.new_chart_id AS    income_new_chart, date($transdate) - c2.valid_from AS    income_valid,
1229          c3.accno AS   expense_accno, c3.new_chart_id AS   expense_new_chart, date($transdate) - c3.valid_from AS   expense_valid
1230        FROM invoice i, parts p
1231        LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id)
1232        LEFT JOIN chart c2 ON ((SELECT income_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id)
1233        LEFT JOIN chart c3 ON ((select expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id)
1234        WHERE (i.parts_id = p.id)
1235          AND (i.parts_id = ?)
1236          AND ((i.base_qty + i.allocated) < 0)
1237        ORDER BY trans_id|;
1238   my $sth = prepare_execute_query($form, $dbh, $query, conv_i($id));
1239
1240   my $allocated = 0;
1241   my $qty;
1242
1243   while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
1244     if (($qty = (($ref->{base_qty} * -1) - $ref->{allocated})) > $totalqty) {
1245       $qty = $totalqty;
1246     }
1247
1248     $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty);
1249
1250     # total expenses and inventory
1251     # sellprice is the cost of the item
1252     my $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / ( $basefactor || 1 ), 2);
1253
1254     if (!$main::eur) {
1255       $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
1256       # add to expense
1257       $form->{amount_cogs}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
1258       $form->{expense_inventory} .= " " . $ref->{expense_accno};
1259       $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
1260       # deduct inventory
1261       $form->{amount_cogs}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
1262       $form->{expense_inventory} .= " " . $ref->{inventory_accno};
1263     }
1264
1265     # add allocated
1266     $allocated -= $qty;
1267
1268     last if (($totalqty -= $qty) <= 0);
1269   }
1270
1271   $sth->finish;
1272
1273   $main::lxdebug->leave_sub();
1274
1275   return $allocated;
1276 }
1277
1278 sub reverse_invoice {
1279   $main::lxdebug->enter_sub();
1280
1281   my ($dbh, $form) = @_;
1282
1283   # reverse inventory items
1284   my $query =
1285     qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly, p.inventory_accno_id
1286        FROM invoice i
1287        JOIN parts p ON (i.parts_id = p.id)
1288        WHERE i.trans_id = ?|;
1289   my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id"}));
1290
1291   while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
1292
1293     if ($ref->{inventory_accno_id}) {
1294       # de-allocated purchases
1295       $query =
1296         qq|SELECT i.id, i.trans_id, i.allocated
1297            FROM invoice i
1298            WHERE (i.parts_id = ?) AND (i.allocated > 0)
1299            ORDER BY i.trans_id DESC|;
1300       my $sth2 = prepare_execute_query($form, $dbh, $query, conv_i($ref->{"parts_id"}));
1301
1302       while (my $inhref = $sth2->fetchrow_hashref('NAME_lc')) {
1303         my $qty = $ref->{qty};
1304         if (($ref->{qty} - $inhref->{allocated}) > 0) {
1305           $qty = $inhref->{allocated};
1306         }
1307
1308         # update invoice
1309         $form->update_balance($dbh, "invoice", "allocated", qq|id = $inhref->{id}|, $qty * -1);
1310
1311         last if (($ref->{qty} -= $qty) <= 0);
1312       }
1313       $sth2->finish;
1314     }
1315   }
1316
1317   $sth->finish;
1318
1319   # delete acc_trans
1320   my @values = (conv_i($form->{id}));
1321   do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
1322   do_query($form, $dbh, qq|DELETE FROM invoice WHERE trans_id = ?|, @values);
1323
1324   if ($form->{lizenzen}) {
1325     $query =
1326       qq|DELETE FROM licenseinvoice
1327          WHERE trans_id in (SELECT id FROM invoice WHERE trans_id = ?)|;
1328     do_query($form, $dbh, $query, @values);
1329   }
1330
1331   do_query($form, $dbh, qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|, @values);
1332
1333   $main::lxdebug->leave_sub();
1334 }
1335
1336 sub delete_invoice {
1337   $main::lxdebug->enter_sub();
1338
1339   my ($self, $myconfig, $form, $spool) = @_;
1340
1341   # connect to database
1342   my $dbh = $form->dbconnect_noauto($myconfig);
1343
1344   &reverse_invoice($dbh, $form);
1345
1346   my @values = (conv_i($form->{id}));
1347
1348   # delete AR record
1349   do_query($form, $dbh, qq|DELETE FROM ar WHERE id = ?|, @values);
1350
1351   # delete spool files
1352   my @spoolfiles = selectall_array_query($form, $dbh, qq|SELECT spoolfile FROM status WHERE trans_id = ?|, @values);
1353
1354   # delete status entries
1355   do_query($form, $dbh, qq|DELETE FROM status WHERE trans_id = ?|, @values);
1356
1357   my $rc = $dbh->commit;
1358   $dbh->disconnect;
1359
1360   if ($rc) {
1361     map { unlink "$spool/$_" if -f "$spool/$_"; } @spoolfiles;
1362   }
1363
1364   $main::lxdebug->leave_sub();
1365
1366   return $rc;
1367 }
1368
1369 sub retrieve_invoice {
1370   $main::lxdebug->enter_sub();
1371
1372   my ($self, $myconfig, $form) = @_;
1373
1374   # connect to database
1375   my $dbh = $form->dbconnect_noauto($myconfig);
1376
1377   my ($sth, $ref, $query);
1378
1379   my $query_transdate = ", current_date AS invdate" if !$form->{id};
1380
1381   $query =
1382     qq|SELECT
1383          (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno,
1384          (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id)    AS income_accno,
1385          (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id)   AS expense_accno,
1386          (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id)    AS fxgain_accno,
1387          (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id)    AS fxloss_accno,
1388          d.curr AS currencies
1389          ${query_transdate}
1390        FROM defaults d|;
1391
1392   $ref = selectfirst_hashref_query($form, $dbh, $query);
1393   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1394
1395   if ($form->{id}) {
1396     my $id = conv_i($form->{id});
1397
1398     # retrieve invoice
1399     #erweiterung um das entsprechende feld lieferscheinnummer (a.donumber) in der html-maske anzuzeigen 12.02.2009 jb
1400
1401     $query =
1402       qq|SELECT
1403            a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
1404            a.orddate, a.quodate, a.globalproject_id,
1405            a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
1406            a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
1407            a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
1408            a.employee_id, a.salesman_id, a.payment_id,
1409            a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type,
1410            a.transaction_description,
1411            a.marge_total, a.marge_percent,
1412            e.name AS employee, a.donumber
1413          FROM ar a
1414          LEFT JOIN employee e ON (e.id = a.employee_id)
1415          WHERE a.id = ?|;
1416     $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1417     map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1418
1419
1420     $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
1421
1422     # get shipto
1423     $query = qq|SELECT * FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|;
1424     $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1425     delete $ref->{id};
1426     map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1427
1428     foreach my $vc (qw(customer vendor)) {
1429       next if !$form->{"delivery_${vc}_id"};
1430       ($form->{"delivery_${vc}_string"}) = selectrow_query($form, $dbh, qq|SELECT name FROM customer WHERE id = ?|, $id);
1431     }
1432
1433     # get printed, emailed
1434     $query = qq|SELECT printed, emailed, spoolfile, formname FROM status WHERE trans_id = ?|;
1435     $sth = prepare_execute_query($form, $dbh, $query, $id);
1436
1437     while ($ref = $sth->fetchrow_hashref('NAME_lc')) {
1438       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
1439       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
1440       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
1441     }
1442     $sth->finish;
1443     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
1444
1445     my $transdate = $form->{deliverydate} ? $dbh->quote($form->{deliverydate})
1446                   : $form->{invdate}      ? $dbh->quote($form->{invdate})
1447                   :                         "current_date";
1448
1449
1450     my $taxzone_id = $form->{taxzone_id} *= 1;
1451     $taxzone_id = 0 if (0 > $taxzone_id) || (3 < $taxzone_id);
1452
1453     # retrieve individual items
1454     $query =
1455       qq|SELECT
1456            c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from AS inventory_valid,
1457            c2.accno AS income_accno,    c2.new_chart_id AS income_new_chart,    date($transdate) - c2.valid_from as income_valid,
1458            c3.accno AS expense_accno,   c3.new_chart_id AS expense_new_chart,   date($transdate) - c3.valid_from AS expense_valid,
1459
1460            i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.discount, i.parts_id AS id, i.unit, i.deliverydate AS reqdate,
1461            i.project_id, i.serialnumber, i.id AS invoice_pos, i.pricegroup_id, i.ordnumber, i.transdate, i.cusordnumber, i.subtotal, i.lastcost,
1462            i.price_factor_id, i.price_factor, i.marge_price_factor,
1463            p.partnumber, p.assembly, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, p.formel,
1464            pr.projectnumber, pg.partsgroup, prg.pricegroup
1465
1466          FROM invoice i
1467          LEFT JOIN parts p ON (i.parts_id = p.id)
1468          LEFT JOIN project pr ON (i.project_id = pr.id)
1469          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1470          LEFT JOIN pricegroup prg ON (i.pricegroup_id = prg.id)
1471
1472          LEFT JOIN chart c1 ON ((SELECT inventory_accno_id             FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c1.id)
1473          LEFT JOIN chart c2 ON ((SELECT income_accno_id_${taxzone_id}  FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c2.id)
1474          LEFT JOIN chart c3 ON ((SELECT expense_accno_id_${taxzone_id} FROM buchungsgruppen WHERE id = p.buchungsgruppen_id) = c3.id)
1475
1476          WHERE (i.trans_id = ?) AND NOT (i.assemblyitem = '1') ORDER BY i.id|;
1477
1478     $sth = prepare_execute_query($form, $dbh, $query, $id);
1479
1480     while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
1481       map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"};
1482       delete($ref->{"part_inventory_accno_id"});
1483
1484       foreach my $type (qw(inventory income expense)) {
1485         while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
1486           my $query = qq|SELECT accno, new_chart_id, date($transdate) - valid_from FROM chart WHERE id = ?|;
1487           @$ref{ map $type.$_, qw(_accno _new_chart _valid) } = selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"});
1488         }
1489       }
1490
1491       # get tax rates and description
1492       my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1493       $query =
1494         qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber FROM tax t
1495            LEFT JOIN chart c ON (c.id = t.chart_id)
1496            WHERE t.id IN
1497              (SELECT tk.tax_id FROM taxkeys tk
1498               WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?)
1499                 AND startdate <= date($transdate)
1500               ORDER BY startdate DESC LIMIT 1)
1501            ORDER BY c.accno|;
1502       my $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
1503       $ref->{taxaccounts} = "";
1504       my $i=0;
1505       while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
1506
1507         if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1508           $i++;
1509           $ptr->{accno} = $i;
1510         }
1511         $ref->{taxaccounts} .= "$ptr->{accno} ";
1512
1513         if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) {
1514           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
1515           $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1516           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
1517           $form->{taxaccounts} .= "$ptr->{accno} ";
1518         }
1519
1520       }
1521
1522       if ($form->{lizenzen}) {
1523         $query = qq|SELECT l.licensenumber, l.id AS licenseid FROM license l, licenseinvoice li WHERE l.id = li.license_id AND li.trans_id = ?|;
1524         my ($licensenumber, $licenseid) = selectrow_query($form, $dbh, $query, conv_i($ref->{invoice_pos}));
1525         $ref->{lizenzen} = "<option value=\"$licenseid\">$licensenumber</option>";
1526       }
1527
1528       $ref->{qty} *= -1 if $form->{type} eq "credit_note";
1529
1530       chop $ref->{taxaccounts};
1531       push @{ $form->{invoice_details} }, $ref;
1532       $stw->finish;
1533     }
1534     $sth->finish;
1535
1536     Common::webdav_folder($form) if ($main::webdav);
1537   }
1538
1539   my $rc = $dbh->commit;
1540   $dbh->disconnect;
1541
1542   $main::lxdebug->leave_sub();
1543
1544   return $rc;
1545 }
1546
1547 sub get_customer {
1548   $main::lxdebug->enter_sub();
1549
1550   my ($self, $myconfig, $form) = @_;
1551
1552   # connect to database
1553   my $dbh = $form->dbconnect($myconfig);
1554
1555   my $dateformat = $myconfig->{dateformat};
1556   $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1557
1558   my (@values, $duedate, $ref, $query);
1559
1560   if ($form->{invdate}) {
1561     $duedate = "to_date(?, '$dateformat')";
1562     push @values, $form->{invdate};
1563   } else {
1564     $duedate = "current_date";
1565   }
1566
1567   my $cid = conv_i($form->{customer_id});
1568   my $payment_id;
1569
1570   if ($form->{payment_id}) {
1571     $payment_id = "(pt.id = ?) OR";
1572     push @values, conv_i($form->{payment_id});
1573   }
1574
1575   # get customer
1576   $query =
1577     qq|SELECT
1578          c.id AS customer_id, c.name AS customer, c.discount as customer_discount, c.creditlimit, c.terms,
1579          c.email, c.cc, c.bcc, c.language_id, c.payment_id,
1580          c.street, c.zipcode, c.city, c.country,
1581          c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id,
1582          $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
1583          b.discount AS tradediscount, b.description AS business
1584        FROM customer c
1585        LEFT JOIN business b ON (b.id = c.business_id)
1586        LEFT JOIN payment_terms pt ON ($payment_id (c.payment_id = pt.id))
1587        WHERE c.id = ?|;
1588   push @values, $cid;
1589   $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
1590   map { $form->{$_} = $ref->{$_} } keys %$ref;
1591
1592   $query =
1593     qq|SELECT sum(amount - paid) AS dunning_amount
1594        FROM ar
1595        WHERE (paid < amount)
1596          AND (customer_id = ?)
1597          AND (dunning_config_id IS NOT NULL)|;
1598   $ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
1599   map { $form->{$_} = $ref->{$_} } keys %$ref;
1600
1601   $query =
1602     qq|SELECT dnn.dunning_description AS max_dunning_level
1603        FROM dunning_config dnn
1604        WHERE id IN (SELECT dunning_config_id
1605                     FROM ar
1606                     WHERE (paid < amount) AND (customer_id = ?) AND (dunning_config_id IS NOT NULL))
1607        ORDER BY dunning_level DESC LIMIT 1|;
1608   $ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
1609   map { $form->{$_} = $ref->{$_} } keys %$ref;
1610
1611   $form->{creditremaining} = $form->{creditlimit};
1612   $query = qq|SELECT SUM(amount - paid) FROM ar WHERE customer_id = ?|;
1613   my ($value) = selectrow_query($form, $dbh, $query, $cid);
1614   $form->{creditremaining} -= $value;
1615
1616   $query =
1617     qq|SELECT o.amount,
1618          (SELECT e.buy FROM exchangerate e
1619           WHERE e.curr = o.curr
1620             AND e.transdate = o.transdate)
1621        FROM oe o
1622        WHERE o.customer_id = ?
1623          AND o.quotation = '0'
1624          AND o.closed = '0'|;
1625   my $sth = prepare_execute_query($form, $dbh, $query, $cid);
1626
1627   while (my ($amount, $exch) = $sth->fetchrow_array) {
1628     $exch = 1 unless $exch;
1629     $form->{creditremaining} -= $amount * $exch;
1630   }
1631   $sth->finish;
1632
1633   # get shipto if we did not converted an order or invoice
1634   if (!$form->{shipto}) {
1635     map { delete $form->{$_} }
1636       qw(shiptoname shiptodepartment_1 shiptodepartment_2
1637          shiptostreet shiptozipcode shiptocity shiptocountry
1638          shiptocontact shiptophone shiptofax shiptoemail);
1639
1640     $query = qq|SELECT * FROM shipto WHERE trans_id = ? AND module = 'CT'|;
1641     $ref = selectfirst_hashref_query($form, $dbh, $query, $cid);
1642     delete $ref->{id};
1643     map { $form->{$_} = $ref->{$_} } keys %$ref;
1644   }
1645
1646   # setup last accounts used for this customer
1647   if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
1648     $query =
1649       qq|SELECT c.id, c.accno, c.description, c.link, c.category
1650          FROM chart c
1651          JOIN acc_trans ac ON (ac.chart_id = c.id)
1652          JOIN ar a ON (a.id = ac.trans_id)
1653          WHERE a.customer_id = ?
1654            AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
1655            AND a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?)|;
1656     $sth = prepare_execute_query($form, $dbh, $query, $cid, $cid);
1657
1658     my $i = 0;
1659     while ($ref = $sth->fetchrow_hashref('NAME_lc')) {
1660       if ($ref->{category} eq 'I') {
1661         $i++;
1662         $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}";
1663
1664         if ($form->{initial_transdate}) {
1665           my $tax_query =
1666             qq|SELECT tk.tax_id, t.rate
1667                FROM taxkeys tk
1668                LEFT JOIN tax t ON tk.tax_id = t.id
1669                WHERE (tk.chart_id = ?) AND (startdate <= date(?))
1670                ORDER BY tk.startdate DESC
1671                LIMIT 1|;
1672           my ($tax_id, $rate) =
1673             selectrow_query($form, $dbh, $tax_query, $ref->{id},
1674                             $form->{initial_transdate});
1675           $form->{"taxchart_$i"} = "${tax_id}--${rate}";
1676         }
1677       }
1678       if ($ref->{category} eq 'A') {
1679         $form->{ARselected} = $form->{AR_1} = $ref->{accno};
1680       }
1681     }
1682     $sth->finish;
1683     $form->{rowcount} = $i if ($i && !$form->{type});
1684   }
1685
1686   $dbh->disconnect;
1687
1688   $main::lxdebug->leave_sub();
1689 }
1690
1691 sub retrieve_item {
1692   $main::lxdebug->enter_sub();
1693
1694   my ($self, $myconfig, $form) = @_;
1695
1696   # connect to database
1697   my $dbh = $form->dbconnect($myconfig);
1698
1699   my $i = $form->{rowcount};
1700
1701   my $where = qq|NOT p.obsolete = '1'|;
1702   my @values;
1703
1704   foreach my $column (qw(p.partnumber p.description pgpartsgroup )) {
1705     my ($table, $field) = split m/\./, $column;
1706     next if !$form->{"${field}_${i}"};
1707     $where .= qq| AND lower(${column}) ILIKE ?|;
1708     push @values, '%' . $form->{"${field}_${i}"} . '%';
1709   }
1710
1711   #Es soll auch nach EAN gesucht werden, ohne Einschränkung durch Beschreibung
1712   if ($form->{"partnumber_$i"} && !$form->{"description_$i"}) {
1713         $where .= qq| OR (NOT p.obsolete = '1' AND p.ean = ? )|;
1714         push @values, $form->{"partnumber_$i"};
1715   }
1716
1717   if ($form->{"description_$i"}) {
1718     $where .= qq| ORDER BY p.description|;
1719   } else {
1720     $where .= qq| ORDER BY p.partnumber|;
1721   }
1722
1723   my $transdate;
1724   if ($form->{type} eq "invoice") {
1725     $transdate =
1726       $form->{deliverydate} ? $dbh->quote($form->{deliverydate}) :
1727       $form->{invdate}      ? $dbh->quote($form->{invdate}) :
1728                               "current_date";
1729   } else {
1730     $transdate =
1731       $form->{transdate}    ? $dbh->quote($form->{transdate}) :
1732                               "current_date";
1733   }
1734
1735   my $taxzone_id = $form->{taxzone_id} * 1;
1736   $taxzone_id = 0 if (0 > $taxzone_id) || (3 < $taxzone_id);
1737
1738   my $query =
1739     qq|SELECT
1740          p.id, p.partnumber, p.description, p.sellprice,
1741          p.listprice, p.inventory_accno_id, p.lastcost,
1742
1743          c1.accno AS inventory_accno,
1744          c1.new_chart_id AS inventory_new_chart,
1745          date($transdate) - c1.valid_from AS inventory_valid,
1746
1747          c2.accno AS income_accno,
1748          c2.new_chart_id AS income_new_chart,
1749          date($transdate)  - c2.valid_from AS income_valid,
1750
1751          c3.accno AS expense_accno,
1752          c3.new_chart_id AS expense_new_chart,
1753          date($transdate) - c3.valid_from AS expense_valid,
1754
1755          p.unit, p.assembly, p.bin, p.onhand,
1756          p.notes AS partnotes, p.notes AS longdescription,
1757          p.not_discountable, p.formel, p.payment_id AS part_payment_id,
1758          p.price_factor_id,
1759
1760          pfac.factor AS price_factor,
1761
1762          pg.partsgroup
1763
1764        FROM parts p
1765        LEFT JOIN chart c1 ON
1766          ((SELECT inventory_accno_id
1767            FROM buchungsgruppen
1768            WHERE id = p.buchungsgruppen_id) = c1.id)
1769        LEFT JOIN chart c2 ON
1770          ((SELECT income_accno_id_${taxzone_id}
1771            FROM buchungsgruppen
1772            WHERE id = p.buchungsgruppen_id) = c2.id)
1773        LEFT JOIN chart c3 ON
1774          ((SELECT expense_accno_id_${taxzone_id}
1775            FROM buchungsgruppen
1776            WHERE id = p.buchungsgruppen_id) = c3.id)
1777        LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1778        LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)
1779        WHERE $where|;
1780   my $sth = prepare_execute_query($form, $dbh, $query, @values);
1781
1782   while (my $ref = $sth->fetchrow_hashref('NAME_lc')) {
1783
1784     # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
1785     # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
1786     # Buchungskonto also aus dem Ergebnis rausgenommen werden.
1787     if (!$ref->{inventory_accno_id}) {
1788       map({ delete($ref->{"inventory_${_}"}); } qw(accno new_chart valid));
1789     }
1790     delete($ref->{inventory_accno_id});
1791
1792     foreach my $type (qw(inventory income expense)) {
1793       while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
1794         my $query =
1795           qq|SELECT accno, new_chart_id, date($transdate) - valid_from
1796              FROM chart
1797              WHERE id = ?|;
1798         ($ref->{"${type}_accno"},
1799          $ref->{"${type}_new_chart"},
1800          $ref->{"${type}_valid"})
1801           = selectrow_query($form, $dbh, $query, $ref->{"${type}_new_chart"});
1802       }
1803     }
1804
1805     if ($form->{payment_id} eq "") {
1806       $form->{payment_id} = $form->{part_payment_id};
1807     }
1808
1809     # get tax rates and description
1810     my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
1811     $query =
1812       qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
1813          FROM tax t
1814          LEFT JOIN chart c ON (c.id = t.chart_id)
1815          WHERE t.id in
1816            (SELECT tk.tax_id
1817             FROM taxkeys tk
1818             WHERE tk.chart_id = (SELECT id from chart WHERE accno = ?)
1819               AND startdate <= ?
1820             ORDER BY startdate DESC
1821             LIMIT 1)
1822          ORDER BY c.accno|;
1823     @values = ($accno_id, $transdate eq "current_date" ? "now" : $transdate);
1824     my $stw = $dbh->prepare($query);
1825     $stw->execute(@values) || $form->dberror($query);
1826
1827     $ref->{taxaccounts} = "";
1828     my $i = 0;
1829     while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
1830
1831       #    if ($customertax{$ref->{accno}})
1832       if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
1833         $i++;
1834         $ptr->{accno} = $i;
1835       }
1836       $ref->{taxaccounts} .= "$ptr->{accno} ";
1837
1838       if (!($form->{taxaccounts} =~ /\Q$ptr->{accno}\E/)) {
1839         $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
1840         $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
1841         $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
1842         $form->{taxaccounts} .= "$ptr->{accno} ";
1843       }
1844
1845     }
1846
1847     $stw->finish;
1848     chop $ref->{taxaccounts};
1849     if ($form->{language_id}) {
1850       $query =
1851         qq|SELECT tr.translation, tr.longdescription
1852            FROM translation tr
1853            WHERE tr.language_id = ? AND tr.parts_id = ?|;
1854       @values = (conv_i($form->{language_id}), conv_i($ref->{id}));
1855       my ($translation, $longdescription) = selectrow_query($form, $dbh, $query, @values);
1856       if ($translation ne "") {
1857         $ref->{description} = $translation;
1858         $ref->{longdescription} = $longdescription;
1859
1860       } else {
1861         $query =
1862           qq|SELECT tr.translation, tr.longdescription
1863              FROM translation tr
1864              WHERE tr.language_id IN
1865                (SELECT id
1866                 FROM language
1867                 WHERE article_code = (SELECT article_code FROM language WHERE id = ?))
1868                AND tr.parts_id = ?
1869              LIMIT 1|;
1870         @values = (conv_i($form->{language_id}), conv_i($ref->{id}));
1871         my ($translation, $longdescription) = selectrow_query($form, $dbh, $query, @values);
1872         if ($translation ne "") {
1873           $ref->{description} = $translation;
1874           $ref->{longdescription} = $longdescription;
1875         }
1876       }
1877     }
1878
1879     $ref->{onhand} *= 1;
1880
1881     push @{ $form->{item_list} }, $ref;
1882
1883     if ($form->{lizenzen}) {
1884       if ($ref->{inventory_accno} > 0) {
1885         $query =
1886           qq|SELECT l.*
1887              FROM license l
1888              WHERE l.parts_id = ? AND NOT l.id IN (SELECT li.license_id FROM licenseinvoice li)|;
1889         my $stw = prepare_execute_query($form, $dbh, $query, conv_i($ref->{id}));
1890         while (my $ptr = $stw->fetchrow_hashref('NAME_lc')) {
1891           push @{ $form->{LIZENZEN}{ $ref->{id} } }, $ptr;
1892         }
1893         $stw->finish;
1894       }
1895     }
1896   }
1897   $sth->finish;
1898   $dbh->disconnect;
1899
1900   $main::lxdebug->leave_sub();
1901 }
1902
1903 ##########################
1904 # get pricegroups from database
1905 # build up selected pricegroup
1906 # if an exchange rate - change price
1907 # for each part
1908 #
1909 sub get_pricegroups_for_parts {
1910
1911   $main::lxdebug->enter_sub();
1912
1913   my ($self, $myconfig, $form) = @_;
1914
1915   my $dbh = $form->dbconnect($myconfig);
1916
1917   $form->{"PRICES"} = {};
1918
1919   my $i  = 1;
1920   my $id = 0;
1921   my $all_units = AM->retrieve_units($myconfig, $form);
1922   while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
1923     $form->{"PRICES"}{$i} = [];
1924
1925     $id = $form->{"id_$i"};
1926
1927     if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
1928
1929       $id = $form->{"new_id_$i"};
1930     }
1931
1932     my ($price, $selectedpricegroup_id) = split(/--/,
1933       $form->{"sellprice_pg_$i"});
1934
1935     my $pricegroup_old = $form->{"pricegroup_old_$i"};
1936     $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
1937     $form->{"old_pricegroup_$i"} = $pricegroup_old;
1938
1939     my $price_new = $form->{"price_new_$i"};
1940     my $price_old = $form->{"price_old_$i"};
1941
1942     if (!$form->{"unit_old_$i"}) {
1943       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
1944       # Einheit, wie sie in den Stammdaten hinterlegt wurde.
1945       # Es sollte also angenommen werden, dass diese ausgewaehlt war.
1946       $form->{"unit_old_$i"} = $form->{"unit_$i"};
1947     }
1948
1949     # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
1950     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
1951     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
1952
1953     if (!$all_units->{$form->{"selected_unit_$i"}} ||
1954         ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
1955          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
1956       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
1957       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
1958       # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
1959       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
1960     }
1961
1962     my $basefactor = 1;
1963
1964     if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
1965       if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
1966           $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
1967         $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
1968           $all_units->{$form->{"unit_old_$i"}}->{"factor"};
1969       }
1970     }
1971
1972     if (!$form->{"basefactor_$i"}) {
1973       $form->{"basefactor_$i"} = 1;
1974     }
1975
1976     my $query =
1977       qq|SELECT
1978            pricegroup_id,
1979            (SELECT p.sellprice FROM parts p WHERE p.id = ?) AS default_sellprice,
1980            (SELECT pg.pricegroup FROM pricegroup pg WHERE id = pricegroup_id) AS pricegroup,
1981            price,
1982            '' AS selected
1983           FROM prices
1984           WHERE parts_id = ?
1985
1986           UNION
1987
1988           SELECT
1989             0 as pricegroup_id,
1990             (SELECT sellprice FROM parts WHERE id = ?) AS default_sellprice,
1991             '' AS pricegroup,
1992             (SELECT DISTINCT sellprice FROM parts where id = ?) AS price,
1993             'selected' AS selected
1994           FROM prices
1995
1996           ORDER BY pricegroup|;
1997     my @values = (conv_i($id), conv_i($id), conv_i($id), conv_i($id));
1998     my $pkq = prepare_execute_query($form, $dbh, $query, @values);
1999
2000     while (my $pkr = $pkq->fetchrow_hashref('NAME_lc')) {
2001       $pkr->{id}       = $id;
2002       $pkr->{selected} = '';
2003
2004       # if there is an exchange rate change price
2005       if (($form->{exchangerate} * 1) != 0) {
2006
2007         $pkr->{price} /= $form->{exchangerate};
2008       }
2009
2010       $pkr->{price} *= $form->{"basefactor_$i"};
2011
2012       $pkr->{price} *= $basefactor;
2013
2014       $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
2015
2016       if ($selectedpricegroup_id eq undef) {
2017         if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
2018
2019           $pkr->{selected}  = ' selected';
2020
2021           # no customer pricesgroup set
2022           if ($pkr->{price} == $pkr->{default_sellprice}) {
2023
2024             $pkr->{price} = $form->{"sellprice_$i"};
2025
2026           } else {
2027
2028 # this sub should not set anything and only return. --sschoeling, 20090506
2029 #            $form->{"sellprice_$i"} = $pkr->{price};
2030           }
2031
2032         } elsif ($pkr->{price} == $pkr->{default_sellprice}) {
2033           $pkr->{price}    = $form->{"sellprice_$i"};
2034           $pkr->{selected} = ' selected';
2035         }
2036       }
2037
2038       if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
2039         if ($selectedpricegroup_id ne $pricegroup_old) {
2040           if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2041             $pkr->{selected}  = ' selected';
2042           }
2043         } elsif (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
2044           if ($pkr->{pricegroup_id} == 0) {
2045             $pkr->{price}     = $form->{"sellprice_$i"};
2046             $pkr->{selected}  = ' selected';
2047           }
2048         } elsif ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
2049           $pkr->{selected}  = ' selected';
2050           if (    ($pkr->{pricegroup_id} == 0)
2051               and ($pkr->{price} == $form->{"sellprice_$i"})) {
2052             # $pkr->{price}                         = $form->{"sellprice_$i"};
2053           } else {
2054             $pkr->{price} = $form->{"sellprice_$i"};
2055           }
2056         }
2057       }
2058       push @{ $form->{PRICES}{$i} }, $pkr;
2059
2060     }
2061     $form->{"basefactor_$i"} *= $basefactor;
2062
2063     $i++;
2064
2065     $pkq->finish;
2066   }
2067
2068   $dbh->disconnect;
2069
2070   $main::lxdebug->leave_sub();
2071 }
2072
2073 sub has_storno {
2074   $main::lxdebug->enter_sub();
2075
2076   my ($self, $myconfig, $form, $table) = @_;
2077
2078   $main::lxdebug->leave_sub() and return 0 unless ($form->{id});
2079
2080   # make sure there's no funny stuff in $table
2081   # ToDO: die when this happens and throw an error
2082   $main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
2083
2084   my $dbh = $form->dbconnect($myconfig);
2085
2086   my $query = qq|SELECT storno FROM $table WHERE storno_id = ?|;
2087   my ($result) = selectrow_query($form, $dbh, $query, $form->{id});
2088
2089   $dbh->disconnect();
2090
2091   $main::lxdebug->leave_sub();
2092
2093   return $result;
2094 }
2095
2096 sub is_storno {
2097   $main::lxdebug->enter_sub();
2098
2099   my ($self, $myconfig, $form, $table, $id) = @_;
2100
2101   $main::lxdebug->leave_sub() and return 0 unless ($id);
2102
2103   # make sure there's no funny stuff in $table
2104   # ToDO: die when this happens and throw an error
2105   $main::lxdebug->leave_sub() and return 0 if ($table =~ /\W/);
2106
2107   my $dbh = $form->dbconnect($myconfig);
2108
2109   my $query = qq|SELECT storno FROM $table WHERE id = ?|;
2110   my ($result) = selectrow_query($form, $dbh, $query, $id);
2111
2112   $dbh->disconnect();
2113
2114   $main::lxdebug->leave_sub();
2115
2116   return $result;
2117 }
2118
2119 1;