Debugcode entfernt.
[kivitendo-erp.git] / SL / AM.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) 2001
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 # Administration module
32 #    Chart of Accounts
33 #    template routines
34 #    preferences
35 #
36 #======================================================================
37
38 package AM;
39
40 use Data::Dumper;
41 use SL::DBUtils;
42
43 sub get_account {
44   $main::lxdebug->enter_sub();
45
46   my ($self, $myconfig, $form) = @_;
47
48   # connect to database
49   my $dbh = $form->dbconnect($myconfig);
50   my $query = qq{
51     SELECT c.accno, c.description, c.charttype, c.category,
52       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
53       c.pos_bwa, datevautomatik,
54       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
55       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
56     FROM chart c
57     LEFT JOIN taxkeys tk
58     ON (c.id=tk.chart_id AND tk.id =
59       (SELECT id FROM taxkeys
60        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
61        ORDER BY startdate DESC LIMIT 1))
62     WHERE c.id = ?
63     };
64
65
66   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
67   my $sth = $dbh->prepare($query);
68   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
69
70   my $ref = $sth->fetchrow_hashref(NAME_lc);
71
72   foreach my $key (keys %$ref) {
73     $form->{"$key"} = $ref->{"$key"};
74   }
75
76   $sth->finish;
77
78   # get default accounts
79   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
80               FROM defaults|;
81   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
82   $sth = $dbh->prepare($query);
83   $sth->execute || $form->dberror($query);
84
85   $ref = $sth->fetchrow_hashref(NAME_lc);
86
87   map { $form->{$_} = $ref->{$_} } keys %ref;
88
89   $sth->finish;
90
91
92
93   # get taxkeys and description
94   $query = qq{
95     SELECT
96       id,
97       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
98       taxkey,
99       id||'--'||taxkey AS tax,
100       taxdescription,
101       rate
102     FROM tax ORDER BY taxkey
103   };
104   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
105   $sth = $dbh->prepare($query);
106   $sth->execute || $form->dberror($query);
107
108   $form->{TAXKEY} = [];
109
110   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
111     push @{ $form->{TAXKEY} }, $ref;
112   }
113
114   $sth->finish;
115   if ($form->{id}) {
116     # get new accounts
117     $query = qq|SELECT id, accno,description
118                 FROM chart
119                 WHERE link = ?
120                 ORDER BY accno|;
121     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
122     $sth = $dbh->prepare($query);
123     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
124
125     $form->{NEWACCOUNT} = [];
126     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
127       push @{ $form->{NEWACCOUNT} }, $ref;
128     }
129
130     $sth->finish;
131
132     # get the taxkeys of account
133
134     $query = qq{
135       SELECT
136         tk.id,
137         tk.chart_id,
138         c.accno,
139         tk.tax_id,
140         t.taxdescription,
141         t.rate,
142         tk.taxkey_id,
143         tk.pos_ustva,
144         tk.startdate
145       FROM taxkeys tk
146       LEFT JOIN   tax t ON (t.id = tk.tax_id)
147       LEFT JOIN chart c ON (c.id = t.chart_id)
148
149       WHERE tk.chart_id = ?
150       ORDER BY startdate DESC
151     };
152     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
153     $sth = $dbh->prepare($query);
154
155     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
156
157     $form->{ACCOUNT_TAXKEYS} = [];
158
159     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
160       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
161     }
162
163     $sth->finish;
164
165   }
166   # check if we have any transactions
167   $query = qq|SELECT a.trans_id FROM acc_trans a
168               WHERE a.chart_id = ?|;
169   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
170   $sth = $dbh->prepare($query);
171   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
172
173   ($form->{orphaned}) = $sth->fetchrow_array;
174   $form->{orphaned} = !$form->{orphaned};
175   $sth->finish;
176
177   # check if new account is active
178   $form->{new_chart_valid} = 0;
179   if ($form->{new_chart_id}) {
180     $query = qq|SELECT current_date-valid_from FROM chart
181                 WHERE id = ?|;
182     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
183     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
184     if ($count >=0) {
185       $form->{new_chart_valid} = 1;
186     }
187     $sth->finish;
188   }
189
190   $dbh->disconnect;
191
192   $main::lxdebug->leave_sub();
193 }
194
195 sub save_account {
196   $main::lxdebug->enter_sub();
197
198   my ($self, $myconfig, $form) = @_;
199
200   # connect to database, turn off AutoCommit
201   my $dbh = $form->dbconnect_noauto($myconfig);
202
203   # sanity check, can't have AR with AR_...
204   if ($form->{AR} || $form->{AP} || $form->{IC}) {
205     map { delete $form->{$_} }
206       qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice CT_tax);
207   }
208
209   $form->{link} = "";
210   foreach my $item ($form->{AR},            $form->{AR_amount},
211                     $form->{AR_tax},        $form->{AR_paid},
212                     $form->{AP},            $form->{AP_amount},
213                     $form->{AP_tax},        $form->{AP_paid},
214                     $form->{IC},            $form->{IC_sale},
215                     $form->{IC_cogs},       $form->{IC_taxpart},
216                     $form->{IC_income},     $form->{IC_expense},
217                     $form->{IC_taxservice}, $form->{CT_tax}
218     ) {
219     $form->{link} .= "${item}:" if ($item);
220   }
221   chop $form->{link};
222
223   # strip blanks from accno
224   map { $form->{$_} =~ s/ //g; } qw(accno);
225
226   my ($query, $sth);
227
228   if ($form->{id} eq "NULL") {
229     $form->{id} = "";
230   }
231
232   my @values;
233
234   if ($form->{id}) {
235     $query = qq|UPDATE chart SET
236                   accno = ?,
237                   description = ?,
238                   charttype = ?,
239                   category = ?,
240                   link = ?,
241                   pos_bwa   = ?,
242                   pos_bilanz = ?,
243                   pos_eur = ?,
244                   new_chart_id = ?,
245                   valid_from = ?,
246                   datevautomatik = ?
247                 WHERE id = ?|;
248
249     @values = (
250                   $form->{accno},
251                   $form->{description},
252                   $form->{charttype},
253                   $form->{category},
254                   $form->{link},
255                   conv_i($form->{pos_bwa}),
256                   conv_i($form->{pos_bilanz}),
257                   conv_i($form->{pos_eur}),
258                   conv_i($form->{new_chart_id}),
259                   conv_date($form->{valid_from}),
260                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
261                 $form->{id},
262     );
263
264   }
265   elsif ($form->{id} && !$form->{new_chart_valid}) {
266
267     $query = qq|
268                   UPDATE chart
269                   SET new_chart_id = ?,
270                   valid_from = ?
271                   WHERE id = ?
272              |;
273
274     @values = (
275                   conv_i($form->{new_chart_id}),
276                   conv_date($form->{valid_from}),
277                   $form->{id}
278               );
279   }
280   else {
281
282     $query = qq|
283                   INSERT INTO chart (
284                       accno,
285                       description,
286                       charttype,
287                       category,
288                       link,
289                       pos_bwa,
290                       pos_bilanz,
291                       pos_eur,
292                       new_chart_id,
293                       valid_from,
294                       datevautomatik )
295                   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
296              |;
297
298     @values = (
299                       $form->{accno},
300                       $form->{description},
301                       $form->{charttype},
302                       $form->{category}, $form->{link},
303                       conv_i($form->{pos_bwa}),
304                       conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
305                       conv_i($form->{new_chart_id}),
306                       conv_date($form->{valid_from}),
307                       ($form->{datevautomatik} eq 'T') ? 'true':'false',
308               );
309
310   }
311
312   do_query($form, $dbh, $query, @values);
313
314   #Save Taxkeys
315
316   my @taxkeys = ();
317
318   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
319   my $tk_count;
320
321   READTAXKEYS:
322   for $tk_count (0 .. $MAX_TRIES) {
323
324     # Loop control
325
326     # Check if the account already exists, else cancel
327     last READTAXKEYS if ( $form->{'id'} == 0);
328
329     # check if there is a startdate
330     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
331       $tk_count++;
332       next READTAXKEYS;
333     }
334
335     # check if there is at least one relation to pos_ustva or tax_id
336     if ( $form->{"taxkey_pos_ustva_$tk_count"} eq '' && $form->{"taxkey_tax_$tk_count"} == 0 ) {
337       $tk_count++;
338       next READTAXKEYS;
339     }
340
341     # Add valid taxkeys into the array
342     push @taxkeys ,
343       {
344         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
345         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
346         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
347         chart_id  => conv_i($form->{"id"}),
348         pos_ustva => $form->{"taxkey_pos_ustva_$tk_count"},
349         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
350       };
351
352     $tk_count++;
353   }
354
355   TAXKEY:
356   for my $j (0 .. $#taxkeys){
357     if ( defined $taxkeys[$j]{'id'} ){
358       # delete Taxkey?
359
360       if ($taxkeys[$j]{'delete'}){
361         $query = qq{
362           DELETE FROM taxkeys WHERE id = ?
363         };
364
365         @values = ($taxkeys[$j]{'id'});
366
367         do_query($form, $dbh, $query, @values);
368
369         next TAXKEY;
370       }
371
372       # UPDATE Taxkey
373
374       $query = qq{
375         UPDATE taxkeys
376         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
377             chart_id  = ?,
378             tax_id    = ?,
379             pos_ustva = ?,
380             startdate = ?
381         WHERE id = ?
382       };
383       @values = (
384         $taxkeys[$j]{'tax_id'},
385         $taxkeys[$j]{'chart_id'},
386         $taxkeys[$j]{'tax_id'},
387         $taxkeys[$j]{'pos_ustva'},
388         $taxkeys[$j]{'startdate'},
389         $taxkeys[$j]{'id'},
390       );
391       do_query($form, $dbh, $query, @values);
392     }
393     else {
394       # INSERT Taxkey
395
396       $query = qq{
397         INSERT INTO taxkeys (
398           taxkey_id,
399           chart_id,
400           tax_id,
401           pos_ustva,
402           startdate
403         )
404         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
405       };
406       @values = (
407         $taxkeys[$j]{'tax_id'},
408         $taxkeys[$j]{'chart_id'},
409         $taxkeys[$j]{'tax_id'},
410         $taxkeys[$j]{'pos_ustva'},
411         $taxkeys[$j]{'startdate'},
412       );
413
414       do_query($form, $dbh, $query, @values);
415     }
416
417   }
418
419   # commit
420   my $rc = $dbh->commit;
421   $dbh->disconnect;
422
423   $main::lxdebug->leave_sub();
424
425   return $rc;
426 }
427
428 sub delete_account {
429   $main::lxdebug->enter_sub();
430
431   my ($self, $myconfig, $form) = @_;
432
433   # connect to database, turn off AutoCommit
434   my $dbh = $form->dbconnect_noauto($myconfig);
435
436   my $query = qq|SELECT count(*) FROM acc_trans a
437                  WHERE a.chart_id = ?|;
438   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
439
440   if ($count) {
441     $dbh->disconnect;
442     $main::lxdebug->leave_sub();
443     return;
444   }
445
446   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
447   foreach my $type (qw(inventory income expense)) {
448     $query =
449       qq|UPDATE parts | .
450       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
451       qq|WHERE ${type}_accno_id = ?|;
452     do_query($form, $dbh, $query, $form->{id});
453   }
454
455   foreach my $table (qw(partstax customertax vendortax tax)) {
456     $query = qq|DELETE FROM $table
457                 WHERE chart_id = ?|;
458     do_query($form, $dbh, $query, $form->{id});
459   }
460
461   # delete chart of account record
462   $query = qq|DELETE FROM chart
463               WHERE id = ?|;
464   do_query($form, $dbh, $query, $form->{id});
465
466   # delete account taxkeys
467   $query = qq|DELETE FROM taxkeys
468               WHERE chart_id = ?|;
469   do_query($form, $dbh, $query, $form->{id});
470
471   # commit and redirect
472   my $rc = $dbh->commit;
473   $dbh->disconnect;
474
475   $main::lxdebug->leave_sub();
476
477   return $rc;
478 }
479
480 sub departments {
481   $main::lxdebug->enter_sub();
482
483   my ($self, $myconfig, $form) = @_;
484
485   # connect to database
486   my $dbh = $form->dbconnect($myconfig);
487
488   my $query = qq|SELECT d.id, d.description, d.role
489                  FROM department d
490                  ORDER BY 2|;
491
492   $sth = $dbh->prepare($query);
493   $sth->execute || $form->dberror($query);
494
495   $form->{ALL} = [];
496   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
497     push @{ $form->{ALL} }, $ref;
498   }
499
500   $sth->finish;
501   $dbh->disconnect;
502
503   $main::lxdebug->leave_sub();
504 }
505
506 sub get_department {
507   $main::lxdebug->enter_sub();
508
509   my ($self, $myconfig, $form) = @_;
510
511   # connect to database
512   my $dbh = $form->dbconnect($myconfig);
513
514   my $query = qq|SELECT d.description, d.role
515                  FROM department d
516                  WHERE d.id = ?|;
517   my $sth = $dbh->prepare($query);
518   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
519
520   my $ref = $sth->fetchrow_hashref(NAME_lc);
521
522   map { $form->{$_} = $ref->{$_} } keys %$ref;
523
524   $sth->finish;
525
526   # see if it is in use
527   $query = qq|SELECT count(*) FROM dpt_trans d
528               WHERE d.department_id = ?|;
529   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
530
531   $form->{orphaned} = !$form->{orphaned};
532   $sth->finish;
533
534   $dbh->disconnect;
535
536   $main::lxdebug->leave_sub();
537 }
538
539 sub save_department {
540   $main::lxdebug->enter_sub();
541
542   my ($self, $myconfig, $form) = @_;
543
544   # connect to database
545   my $dbh = $form->dbconnect($myconfig);
546
547   my @values = ($form->{description}, $form->{role});
548   if ($form->{id}) {
549     $query = qq|UPDATE department SET
550                 description = ?, role = ?
551                 WHERE id = ?|;
552     push(@values, $form->{id});
553   } else {
554     $query = qq|INSERT INTO department
555                 (description, role)
556                 VALUES (?, ?)|;
557   }
558   do_query($form, $dbh, $query, @values);
559
560   $dbh->disconnect;
561
562   $main::lxdebug->leave_sub();
563 }
564
565 sub delete_department {
566   $main::lxdebug->enter_sub();
567
568   my ($self, $myconfig, $form) = @_;
569
570   # connect to database
571   my $dbh = $form->dbconnect($myconfig);
572
573   $query = qq|DELETE FROM department
574               WHERE id = ?|;
575   do_query($form, $dbh, $query, $form->{id});
576
577   $dbh->disconnect;
578
579   $main::lxdebug->leave_sub();
580 }
581
582 sub lead {
583   $main::lxdebug->enter_sub();
584
585   my ($self, $myconfig, $form) = @_;
586
587   # connect to database
588   my $dbh = $form->dbconnect($myconfig);
589
590   my $query = qq|SELECT id, lead
591                  FROM leads
592                  ORDER BY 2|;
593
594   $sth = $dbh->prepare($query);
595   $sth->execute || $form->dberror($query);
596
597   $form->{ALL};
598   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
599     push @{ $form->{ALL} }, $ref;
600   }
601
602   $sth->finish;
603   $dbh->disconnect;
604
605   $main::lxdebug->leave_sub();
606 }
607
608 sub get_lead {
609   $main::lxdebug->enter_sub();
610
611   my ($self, $myconfig, $form) = @_;
612
613   # connect to database
614   my $dbh = $form->dbconnect($myconfig);
615
616   my $query =
617     qq|SELECT l.id, l.lead | .
618     qq|FROM leads l | .
619     qq|WHERE l.id = ?|;
620   my $sth = $dbh->prepare($query);
621   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
622
623   my $ref = $sth->fetchrow_hashref(NAME_lc);
624
625   map { $form->{$_} = $ref->{$_} } keys %$ref;
626
627   $sth->finish;
628
629   $dbh->disconnect;
630
631   $main::lxdebug->leave_sub();
632 }
633
634 sub save_lead {
635   $main::lxdebug->enter_sub();
636
637   my ($self, $myconfig, $form) = @_;
638
639   # connect to database
640   my $dbh = $form->dbconnect($myconfig);
641
642   my @values = ($form->{description});
643   # id is the old record
644   if ($form->{id}) {
645     $query = qq|UPDATE leads SET
646                 lead = ?
647                 WHERE id = ?|;
648     puhs(@values, $form->{id});
649   } else {
650     $query = qq|INSERT INTO leads
651                 (lead)
652                 VALUES (?)|;
653   }
654   do_query($form, $dbh, $query, @values);
655
656   $dbh->disconnect;
657
658   $main::lxdebug->leave_sub();
659 }
660
661 sub delete_lead {
662   $main::lxdebug->enter_sub();
663
664   my ($self, $myconfig, $form) = @_;
665
666   # connect to database
667   my $dbh = $form->dbconnect($myconfig);
668
669   $query = qq|DELETE FROM leads
670               WHERE id = ?|;
671   do_query($form, $dbh, $query, $form->{id});
672
673   $dbh->disconnect;
674
675   $main::lxdebug->leave_sub();
676 }
677
678 sub business {
679   $main::lxdebug->enter_sub();
680
681   my ($self, $myconfig, $form) = @_;
682
683   # connect to database
684   my $dbh = $form->dbconnect($myconfig);
685
686   my $query = qq|SELECT id, description, discount, customernumberinit
687                  FROM business
688                  ORDER BY 2|;
689
690   $sth = $dbh->prepare($query);
691   $sth->execute || $form->dberror($query);
692
693   $form->{ALL};
694   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
695     push @{ $form->{ALL} }, $ref;
696   }
697
698   $sth->finish;
699   $dbh->disconnect;
700
701   $main::lxdebug->leave_sub();
702 }
703
704 sub get_business {
705   $main::lxdebug->enter_sub();
706
707   my ($self, $myconfig, $form) = @_;
708
709   # connect to database
710   my $dbh = $form->dbconnect($myconfig);
711
712   my $query =
713     qq|SELECT b.description, b.discount, b.customernumberinit
714        FROM business b
715        WHERE b.id = ?|;
716   my $sth = $dbh->prepare($query);
717   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
718
719   my $ref = $sth->fetchrow_hashref(NAME_lc);
720
721   map { $form->{$_} = $ref->{$_} } keys %$ref;
722
723   $sth->finish;
724
725   $dbh->disconnect;
726
727   $main::lxdebug->leave_sub();
728 }
729
730 sub save_business {
731   $main::lxdebug->enter_sub();
732
733   my ($self, $myconfig, $form) = @_;
734
735   # connect to database
736   my $dbh = $form->dbconnect($myconfig);
737
738   my @values = ($form->{description}, $form->{discount},
739                 $form->{customernumberinit});
740   # id is the old record
741   if ($form->{id}) {
742     $query = qq|UPDATE business SET
743                 description = ?,
744                 discount = ?,
745                 customernumberinit = ?
746                 WHERE id = ?|;
747     push(@values, $form->{id});
748   } else {
749     $query = qq|INSERT INTO business
750                 (description, discount, customernumberinit)
751                 VALUES (?, ?, ?)|;
752   }
753   do_query($form, $dbh, $query, @values);
754
755   $dbh->disconnect;
756
757   $main::lxdebug->leave_sub();
758 }
759
760 sub delete_business {
761   $main::lxdebug->enter_sub();
762
763   my ($self, $myconfig, $form) = @_;
764
765   # connect to database
766   my $dbh = $form->dbconnect($myconfig);
767
768   $query = qq|DELETE FROM business
769               WHERE id = ?|;
770   do_query($form, $dbh, $query, $form->{id});
771
772   $dbh->disconnect;
773
774   $main::lxdebug->leave_sub();
775 }
776
777
778 sub language {
779   $main::lxdebug->enter_sub();
780
781   my ($self, $myconfig, $form, $return_list) = @_;
782
783   # connect to database
784   my $dbh = $form->dbconnect($myconfig);
785
786   my $query =
787     "SELECT id, description, template_code, article_code, " .
788     "  output_numberformat, output_dateformat, output_longdates " .
789     "FROM language ORDER BY description";
790
791   $sth = $dbh->prepare($query);
792   $sth->execute || $form->dberror($query);
793
794   my $ary = [];
795
796   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
797     push(@{ $ary }, $ref);
798   }
799
800   $sth->finish;
801   $dbh->disconnect;
802
803   $main::lxdebug->leave_sub();
804
805   if ($return_list) {
806     return @{$ary};
807   } else {
808     $form->{ALL} = $ary;
809   }
810 }
811
812 sub get_language {
813   $main::lxdebug->enter_sub();
814
815   my ($self, $myconfig, $form) = @_;
816
817   # connect to database
818   my $dbh = $form->dbconnect($myconfig);
819
820   my $query =
821     "SELECT description, template_code, article_code, " .
822     "  output_numberformat, output_dateformat, output_longdates " .
823     "FROM language WHERE id = ?";
824   my $sth = $dbh->prepare($query);
825   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
826
827   my $ref = $sth->fetchrow_hashref(NAME_lc);
828
829   map { $form->{$_} = $ref->{$_} } keys %$ref;
830
831   $sth->finish;
832
833   $dbh->disconnect;
834
835   $main::lxdebug->leave_sub();
836 }
837
838 sub get_language_details {
839   $main::lxdebug->enter_sub();
840
841   my ($self, $myconfig, $form, $id) = @_;
842
843   # connect to database
844   my $dbh = $form->dbconnect($myconfig);
845
846   my $query =
847     "SELECT template_code, " .
848     "  output_numberformat, output_dateformat, output_longdates " .
849     "FROM language WHERE id = ?";
850   my @res = selectrow_query($form, $dbh, $query, $id);
851   $dbh->disconnect;
852
853   $main::lxdebug->leave_sub();
854
855   return @res;
856 }
857
858 sub save_language {
859   $main::lxdebug->enter_sub();
860
861   my ($self, $myconfig, $form) = @_;
862
863   # connect to database
864   my $dbh = $form->dbconnect($myconfig);
865   my (@values, $query);
866
867   map({ push(@values, $form->{$_}); }
868       qw(description template_code article_code
869          output_numberformat output_dateformat output_longdates));
870
871   # id is the old record
872   if ($form->{id}) {
873     $query =
874       "UPDATE language SET " .
875       "  description = ?, template_code = ?, article_code = ?, " .
876       "  output_numberformat = ?, output_dateformat = ?, " .
877       "  output_longdates = ? " .
878       "WHERE id = ?";
879     push(@values, $form->{id});
880   } else {
881     $query =
882       "INSERT INTO language (" .
883       "  description, template_code, article_code, " .
884       "  output_numberformat, output_dateformat, output_longdates" .
885       ") VALUES (?, ?, ?, ?, ?, ?)";
886   }
887   do_query($form, $dbh, $query, @values);
888
889   $dbh->disconnect;
890
891   $main::lxdebug->leave_sub();
892 }
893
894 sub delete_language {
895   $main::lxdebug->enter_sub();
896
897   my ($self, $myconfig, $form) = @_;
898
899   # connect to database
900   my $dbh = $form->dbconnect_noauto($myconfig);
901
902   foreach my $table (qw(translation_payment_terms units_language)) {
903     my $query = qq|DELETE FROM $table WHERE language_id = ?|;
904     do_query($form, $dbh, $query, $form->{"id"});
905   }
906
907   $query = "DELETE FROM language WHERE id = ?";
908   do_query($form, $dbh, $query, $form->{"id"});
909
910   $dbh->commit();
911   $dbh->disconnect;
912
913   $main::lxdebug->leave_sub();
914 }
915
916
917 sub buchungsgruppe {
918   $main::lxdebug->enter_sub();
919
920   my ($self, $myconfig, $form) = @_;
921
922   # connect to database
923   my $dbh = $form->dbconnect($myconfig);
924
925   my $query = qq|SELECT id, description,
926                  inventory_accno_id,
927                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
928                  income_accno_id_0,
929                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
930                  expense_accno_id_0,
931                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
932                  income_accno_id_1,
933                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
934                  expense_accno_id_1,
935                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
936                  income_accno_id_2,
937                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
938                  expense_accno_id_2,
939                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
940                  income_accno_id_3,
941                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
942                  expense_accno_id_3,
943                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
944                  FROM buchungsgruppen
945                  ORDER BY sortkey|;
946
947   $sth = $dbh->prepare($query);
948   $sth->execute || $form->dberror($query);
949
950   $form->{ALL} = [];
951   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
952     push @{ $form->{ALL} }, $ref;
953   }
954
955   $sth->finish;
956   $dbh->disconnect;
957
958   $main::lxdebug->leave_sub();
959 }
960
961 sub get_buchungsgruppe {
962   $main::lxdebug->enter_sub();
963
964   my ($self, $myconfig, $form) = @_;
965
966   # connect to database
967   my $dbh = $form->dbconnect($myconfig);
968
969   if ($form->{id}) {
970     my $query =
971       qq|SELECT description, inventory_accno_id,
972          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
973          income_accno_id_0,
974          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
975          expense_accno_id_0,
976          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
977          income_accno_id_1,
978          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
979          expense_accno_id_1,
980          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
981          income_accno_id_2,
982          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
983          expense_accno_id_2,
984          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
985          income_accno_id_3,
986          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
987          expense_accno_id_3,
988          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
989          FROM buchungsgruppen
990          WHERE id = ?|;
991     my $sth = $dbh->prepare($query);
992     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
993
994     my $ref = $sth->fetchrow_hashref(NAME_lc);
995
996     map { $form->{$_} = $ref->{$_} } keys %$ref;
997
998     $sth->finish;
999
1000     my $query =
1001       qq|SELECT count(id) = 0 AS orphaned
1002          FROM parts
1003          WHERE buchungsgruppen_id = ?|;
1004     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1005   }
1006
1007   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1008     "FROM defaults";
1009   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1010    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1011
1012   my $module = "IC";
1013   $query = qq|SELECT c.accno, c.description, c.link, c.id,
1014               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1015               FROM chart c, defaults d
1016               WHERE c.link LIKE '%$module%'
1017               ORDER BY c.accno|;
1018
1019
1020   my $sth = $dbh->prepare($query);
1021   $sth->execute || $form->dberror($query);
1022   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1023     foreach my $key (split(/:/, $ref->{link})) {
1024       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1025         $form->{"std_inventory_accno_id"} = $ref->{"id"};
1026       }
1027       if ($key =~ /$module/) {
1028         if (   ($ref->{id} eq $ref->{inventory_accno_id})
1029             || ($ref->{id} eq $ref->{income_accno_id})
1030             || ($ref->{id} eq $ref->{expense_accno_id})) {
1031           push @{ $form->{"${module}_links"}{$key} },
1032             { accno       => $ref->{accno},
1033               description => $ref->{description},
1034               selected    => "selected",
1035               id          => $ref->{id} };
1036         } else {
1037           push @{ $form->{"${module}_links"}{$key} },
1038             { accno       => $ref->{accno},
1039               description => $ref->{description},
1040               selected    => "",
1041               id          => $ref->{id} };
1042         }
1043       }
1044     }
1045   }
1046   $sth->finish;
1047
1048
1049   $dbh->disconnect;
1050
1051   $main::lxdebug->leave_sub();
1052 }
1053
1054 sub save_buchungsgruppe {
1055   $main::lxdebug->enter_sub();
1056
1057   my ($self, $myconfig, $form) = @_;
1058
1059   # connect to database
1060   my $dbh = $form->dbconnect($myconfig);
1061
1062   my @values = ($form->{description}, $form->{inventory_accno_id},
1063                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1064                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1065                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1066                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1067
1068   my $query;
1069
1070   # id is the old record
1071   if ($form->{id}) {
1072     $query = qq|UPDATE buchungsgruppen SET
1073                 description = ?, inventory_accno_id = ?,
1074                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1075                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1076                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1077                 income_accno_id_3 = ?, expense_accno_id_3 = ?
1078                 WHERE id = ?|;
1079     push(@values, $form->{id});
1080   } else {
1081     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1082     my ($sortkey) = $dbh->selectrow_array($query);
1083     $form->dberror($query) if ($dbh->err);
1084     push(@values, $sortkey);
1085     $query = qq|INSERT INTO buchungsgruppen
1086                 (description, inventory_accno_id,
1087                 income_accno_id_0, expense_accno_id_0,
1088                 income_accno_id_1, expense_accno_id_1,
1089                 income_accno_id_2, expense_accno_id_2,
1090                 income_accno_id_3, expense_accno_id_3,
1091                 sortkey)
1092                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1093   }
1094   do_query($form, $dbh, $query, @values);
1095
1096   $dbh->disconnect;
1097
1098   $main::lxdebug->leave_sub();
1099 }
1100
1101 sub delete_buchungsgruppe {
1102   $main::lxdebug->enter_sub();
1103
1104   my ($self, $myconfig, $form) = @_;
1105
1106   # connect to database
1107   my $dbh = $form->dbconnect($myconfig);
1108
1109   $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1110   do_query($form, $dbh, $query, $form->{id});
1111
1112   $dbh->disconnect;
1113
1114   $main::lxdebug->leave_sub();
1115 }
1116
1117 sub swap_sortkeys {
1118   $main::lxdebug->enter_sub();
1119
1120   my ($self, $myconfig, $form, $table) = @_;
1121
1122   # connect to database
1123   my $dbh = $form->dbconnect_noauto($myconfig);
1124
1125   my $query =
1126     qq|SELECT
1127        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1128        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1129   my @values = ($form->{"id1"}, $form->{"id2"});
1130   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1131
1132   $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1133   my $sth = $dbh->prepare($query);
1134   $sth->execute($sortkeys[1], $form->{"id1"}) ||
1135     $form->dberror($query . " ($sortkeys[1], $form->{id1})");
1136   $sth->execute($sortkeys[0], $form->{"id2"}) ||
1137     $form->dberror($query . " ($sortkeys[0], $form->{id2})");
1138   $sth->finish();
1139
1140   $dbh->commit();
1141   $dbh->disconnect;
1142
1143   $main::lxdebug->leave_sub();
1144 }
1145
1146 sub printer {
1147   $main::lxdebug->enter_sub();
1148
1149   my ($self, $myconfig, $form) = @_;
1150
1151   # connect to database
1152   my $dbh = $form->dbconnect($myconfig);
1153
1154   my $query = qq|SELECT id, printer_description, template_code, printer_command
1155                  FROM printers
1156                  ORDER BY 2|;
1157
1158   $sth = $dbh->prepare($query);
1159   $sth->execute || $form->dberror($query);
1160
1161   $form->{"ALL"} = [];
1162   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1163     push @{ $form->{ALL} }, $ref;
1164   }
1165
1166   $sth->finish;
1167   $dbh->disconnect;
1168
1169   $main::lxdebug->leave_sub();
1170 }
1171
1172 sub get_printer {
1173   $main::lxdebug->enter_sub();
1174
1175   my ($self, $myconfig, $form) = @_;
1176
1177   # connect to database
1178   my $dbh = $form->dbconnect($myconfig);
1179
1180   my $query =
1181     qq|SELECT p.printer_description, p.template_code, p.printer_command
1182        FROM printers p
1183        WHERE p.id = ?|;
1184   my $sth = $dbh->prepare($query);
1185   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1186
1187   my $ref = $sth->fetchrow_hashref(NAME_lc);
1188
1189   map { $form->{$_} = $ref->{$_} } keys %$ref;
1190
1191   $sth->finish;
1192
1193   $dbh->disconnect;
1194
1195   $main::lxdebug->leave_sub();
1196 }
1197
1198 sub save_printer {
1199   $main::lxdebug->enter_sub();
1200
1201   my ($self, $myconfig, $form) = @_;
1202
1203   # connect to database
1204   my $dbh = $form->dbconnect($myconfig);
1205
1206   my @values = ($form->{printer_description},
1207                 $form->{template_code},
1208                 $form->{printer_command});
1209
1210   # id is the old record
1211   if ($form->{id}) {
1212     $query = qq|UPDATE printers SET
1213                 printer_description = ?, template_code = ?, printer_command = ?
1214                 WHERE id = ?|;
1215     push(@values, $form->{id});
1216   } else {
1217     $query = qq|INSERT INTO printers
1218                 (printer_description, template_code, printer_command)
1219                 VALUES (?, ?, ?)|;
1220   }
1221   do_query($form, $dbh, $query, @values);
1222
1223   $dbh->disconnect;
1224
1225   $main::lxdebug->leave_sub();
1226 }
1227
1228 sub delete_printer {
1229   $main::lxdebug->enter_sub();
1230
1231   my ($self, $myconfig, $form) = @_;
1232
1233   # connect to database
1234   my $dbh = $form->dbconnect($myconfig);
1235
1236   $query = qq|DELETE FROM printers
1237               WHERE id = ?|;
1238   do_query($form, $dbh, $query, $form->{id});
1239
1240   $dbh->disconnect;
1241
1242   $main::lxdebug->leave_sub();
1243 }
1244
1245 sub payment {
1246   $main::lxdebug->enter_sub();
1247
1248   my ($self, $myconfig, $form) = @_;
1249
1250   # connect to database
1251   my $dbh = $form->dbconnect($myconfig);
1252
1253   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1254
1255   $sth = $dbh->prepare($query);
1256   $sth->execute || $form->dberror($query);
1257
1258   $form->{ALL} = [];
1259   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1260     push @{ $form->{ALL} }, $ref;
1261   }
1262
1263   $sth->finish;
1264   $dbh->disconnect;
1265
1266   $main::lxdebug->leave_sub();
1267 }
1268
1269 sub get_payment {
1270   $main::lxdebug->enter_sub();
1271
1272   my ($self, $myconfig, $form) = @_;
1273
1274   # connect to database
1275   my $dbh = $form->dbconnect($myconfig);
1276
1277   my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1278   my $sth = $dbh->prepare($query);
1279   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1280
1281   my $ref = $sth->fetchrow_hashref(NAME_lc);
1282   map { $form->{$_} = $ref->{$_} } keys %$ref;
1283   $sth->finish();
1284
1285   $query =
1286     qq|SELECT t.language_id, t.description_long, l.description AS language | .
1287     qq|FROM translation_payment_terms t | .
1288     qq|LEFT JOIN language l ON t.language_id = l.id | .
1289     qq|WHERE t.payment_terms_id = ? | .
1290     qq|UNION | .
1291     qq|SELECT l.id AS language_id, NULL AS description_long, | .
1292     qq|  l.description AS language | .
1293     qq|FROM language l|;
1294   $sth = $dbh->prepare($query);
1295   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1296
1297   my %mapping;
1298   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1299     $mapping{ $ref->{"language_id"} } = $ref
1300       unless (defined($mapping{ $ref->{"language_id"} }));
1301   }
1302   $sth->finish;
1303
1304   $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1305                                  values(%mapping))];
1306
1307   $dbh->disconnect;
1308
1309   $main::lxdebug->leave_sub();
1310 }
1311
1312 sub save_payment {
1313   $main::lxdebug->enter_sub();
1314
1315   my ($self, $myconfig, $form) = @_;
1316
1317   # connect to database
1318   my $dbh = $form->dbconnect_noauto($myconfig);
1319
1320   my $query;
1321
1322   if (!$form->{id}) {
1323     $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1324       qq|FROM payment_terms|;
1325     my $sortkey;
1326     ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1327
1328     $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1329     do_query($form, $dbh, $query, $form->{id}, $sortkey);
1330
1331   } else {
1332     $query =
1333       qq|DELETE FROM translation_payment_terms | .
1334       qq|WHERE payment_terms_id = ?|;
1335     do_query($form, $dbh, $query, $form->{"id"});
1336   }
1337
1338   $query = qq|UPDATE payment_terms SET
1339               description = ?, description_long = ?,
1340               ranking = ?,
1341               terms_netto = ?, terms_skonto = ?,
1342               percent_skonto = ?
1343               WHERE id = ?|;
1344   my @values = ($form->{description}, $form->{description_long},
1345                 $form->{ranking} * 1,
1346                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1347                 $form->{percent_skonto} * 1,
1348                 $form->{id});
1349   do_query($form, $dbh, $query, @values);
1350
1351   $query = qq|SELECT id FROM language|;
1352   my @language_ids;
1353   my $sth = $dbh->prepare($query);
1354   $sth->execute() || $form->dberror($query);
1355
1356   while (my ($id) = $sth->fetchrow_array()) {
1357     push(@language_ids, $id);
1358   }
1359   $sth->finish();
1360
1361   $query =
1362     qq|INSERT INTO translation_payment_terms | .
1363     qq|(language_id, payment_terms_id, description_long) | .
1364     qq|VALUES (?, ?, ?)|;
1365   $sth = $dbh->prepare($query);
1366
1367   foreach my $language_id (@language_ids) {
1368     do_statement($form, $sth, $query, $language_id, $form->{"id"},
1369                  $form->{"description_long_${language_id}"});
1370   }
1371   $sth->finish();
1372
1373   $dbh->commit();
1374   $dbh->disconnect;
1375
1376   $main::lxdebug->leave_sub();
1377 }
1378
1379 sub delete_payment {
1380   $main::lxdebug->enter_sub();
1381
1382   my ($self, $myconfig, $form) = @_;
1383
1384   # connect to database
1385   my $dbh = $form->dbconnect_noauto($myconfig);
1386
1387   my $query =
1388     qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1389   do_query($form, $dbh, $query, $form->{"id"});
1390
1391   $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1392   do_query($form, $dbh, $query, $form->{"id"});
1393
1394   $dbh->commit();
1395   $dbh->disconnect;
1396
1397   $main::lxdebug->leave_sub();
1398 }
1399
1400
1401 sub prepare_template_filename {
1402   $main::lxdebug->enter_sub();
1403
1404   my ($self, $myconfig, $form) = @_;
1405
1406   my ($filename, $display_filename);
1407
1408   if ($form->{type} eq "stylesheet") {
1409     $filename = "css/$myconfig->{stylesheet}";
1410     $display_filename = $myconfig->{stylesheet};
1411
1412   } else {
1413     $filename = $form->{formname};
1414
1415     if ($form->{language}) {
1416       my ($id, $template_code) = split(/--/, $form->{language});
1417       $filename .= "_${template_code}";
1418     }
1419
1420     if ($form->{printer}) {
1421       my ($id, $template_code) = split(/--/, $form->{printer});
1422       $filename .= "_${template_code}";
1423     }
1424
1425     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1426     $filename =~ s|.*/||;
1427     $display_filename = $filename;
1428     $filename = "$myconfig->{templates}/$filename";
1429   }
1430
1431   $main::lxdebug->leave_sub();
1432
1433   return ($filename, $display_filename);
1434 }
1435
1436
1437 sub load_template {
1438   $main::lxdebug->enter_sub();
1439
1440   my ($self, $filename) = @_;
1441
1442   my ($content, $lines) = ("", 0);
1443
1444   local *TEMPLATE;
1445
1446   if (open(TEMPLATE, $filename)) {
1447     while (<TEMPLATE>) {
1448       $content .= $_;
1449       $lines++;
1450     }
1451     close(TEMPLATE);
1452   }
1453
1454   $main::lxdebug->leave_sub();
1455
1456   return ($content, $lines);
1457 }
1458
1459 sub save_template {
1460   $main::lxdebug->enter_sub();
1461
1462   my ($self, $filename, $content) = @_;
1463
1464   local *TEMPLATE;
1465
1466   my $error = "";
1467
1468   if (open(TEMPLATE, ">$filename")) {
1469     $content =~ s/\r\n/\n/g;
1470     print(TEMPLATE $content);
1471     close(TEMPLATE);
1472   } else {
1473     $error = $!;
1474   }
1475
1476   $main::lxdebug->leave_sub();
1477
1478   return $error;
1479 }
1480
1481 sub save_preferences {
1482   $main::lxdebug->enter_sub();
1483
1484   my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1485
1486   map { ($form->{$_}) = split(/--/, $form->{$_}) }
1487     qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1488
1489   my @a;
1490   $form->{curr} =~ s/ //g;
1491   map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1492   $form->{curr} = join ':', @a;
1493
1494   # connect to database
1495   my $dbh = $form->dbconnect_noauto($myconfig);
1496
1497   # these defaults are database wide
1498   # user specific variables are in myconfig
1499   # save defaults
1500   my $query =
1501     qq|UPDATE defaults SET | .
1502     qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1503     qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1504     qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1505     qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1506     qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1507     qq|invnumber = ?, | .
1508     qq|cnnumber  = ?, | .
1509     qq|sonumber = ?, | .
1510     qq|ponumber = ?, | .
1511     qq|sqnumber = ?, | .
1512     qq|rfqnumber = ?, | .
1513     qq|customernumber = ?, | .
1514     qq|vendornumber = ?, | .
1515     qq|articlenumber = ?, | .
1516     qq|servicenumber = ?, | .
1517     qq|yearend = ?, | .
1518     qq|curr = ?, | .
1519     qq|businessnumber = ?|;
1520   my @values = ($form->{inventory_accno}, $form->{income_accno},
1521                 $form->{expense_accno},
1522                 $form->{fxgain_accno}, $form->{fxloss_accno},
1523                 $form->{invnumber}, $form->{cnnumber},
1524                 $form->{sonumber}, $form->{ponumber},
1525                 $form->{sqnumber}, $form->{rfqnumber},
1526                 $form->{customernumber}, $form->{vendornumber},
1527                 $form->{articlenumber}, $form->{servicenumber},
1528                 $form->{yearend}, $form->{curr},
1529                 $form->{businessnumber});
1530   do_query($form, $dbh, $query, @values);
1531
1532   # update name
1533   $query = qq|UPDATE employee
1534               SET name = ?
1535               WHERE login = ?|;
1536   do_query($form, $dbh, $query, $form->{name}, $form->{login});
1537
1538   my $rc = $dbh->commit;
1539   $dbh->disconnect;
1540
1541   # save first currency in myconfig
1542   $form->{currency} = substr($form->{curr}, 0, 3);
1543
1544   my $myconfig = new User "$memberfile", "$form->{login}";
1545
1546   foreach my $item (keys %$form) {
1547     $myconfig->{$item} = $form->{$item};
1548   }
1549
1550   $myconfig->save_member($memberfile, $userspath);
1551
1552   if ($webdav) {
1553     @webdavdirs =
1554       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1555     foreach $directory (@webdavdirs) {
1556       $file = "webdav/" . $directory . "/webdav-user";
1557       if ($myconfig->{$directory}) {
1558         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1559         while (<HTACCESS>) {
1560           ($login, $password) = split(/:/, $_);
1561           if ($login ne $form->{login}) {
1562             $newfile .= $_;
1563           }
1564         }
1565         close(HTACCESS);
1566         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1567         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1568         print(HTACCESS $newfile);
1569         close(HTACCESS);
1570       } else {
1571         $form->{$directory} = 0;
1572         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1573         while (<HTACCESS>) {
1574           ($login, $password) = split(/:/, $_);
1575           if ($login ne $form->{login}) {
1576             $newfile .= $_;
1577           }
1578         }
1579         close(HTACCESS);
1580         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1581         print(HTACCESS $newfile);
1582         close(HTACCESS);
1583       }
1584     }
1585   }
1586
1587   $main::lxdebug->leave_sub();
1588
1589   return $rc;
1590 }
1591
1592 sub defaultaccounts {
1593   $main::lxdebug->enter_sub();
1594
1595   my ($self, $myconfig, $form) = @_;
1596
1597   # connect to database
1598   my $dbh = $form->dbconnect($myconfig);
1599
1600   # get defaults from defaults table
1601   my $query = qq|SELECT * FROM defaults|;
1602   my $sth   = $dbh->prepare($query);
1603   $sth->execute || $form->dberror($query);
1604
1605   $form->{defaults}             = $sth->fetchrow_hashref(NAME_lc);
1606   $form->{defaults}{IC}         = $form->{defaults}{inventory_accno_id};
1607   $form->{defaults}{IC_income}  = $form->{defaults}{income_accno_id};
1608   $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1609   $form->{defaults}{FX_gain}    = $form->{defaults}{fxgain_accno_id};
1610   $form->{defaults}{FX_loss}    = $form->{defaults}{fxloss_accno_id};
1611
1612   $sth->finish;
1613
1614   $query = qq|SELECT c.id, c.accno, c.description, c.link
1615               FROM chart c
1616               WHERE c.link LIKE '%IC%'
1617               ORDER BY c.accno|;
1618   $sth = $dbh->prepare($query);
1619   $sth->execute || $self->dberror($query);
1620
1621   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1622     foreach my $key (split(/:/, $ref->{link})) {
1623       if ($key =~ /IC/) {
1624         $nkey = $key;
1625         if ($key =~ /cogs/) {
1626           $nkey = "IC_expense";
1627         }
1628         if ($key =~ /sale/) {
1629           $nkey = "IC_income";
1630         }
1631         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1632                                              id          => $ref->{id},
1633                                              description => $ref->{description}
1634         );
1635       }
1636     }
1637   }
1638   $sth->finish;
1639
1640   $query = qq|SELECT c.id, c.accno, c.description
1641               FROM chart c
1642               WHERE c.category = 'I'
1643               AND c.charttype = 'A'
1644               ORDER BY c.accno|;
1645   $sth = $dbh->prepare($query);
1646   $sth->execute || $self->dberror($query);
1647
1648   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1649     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1650                                              id          => $ref->{id},
1651                                              description => $ref->{description}
1652     );
1653   }
1654   $sth->finish;
1655
1656   $query = qq|SELECT c.id, c.accno, c.description
1657               FROM chart c
1658               WHERE c.category = 'E'
1659               AND c.charttype = 'A'
1660               ORDER BY c.accno|;
1661   $sth = $dbh->prepare($query);
1662   $sth->execute || $self->dberror($query);
1663
1664   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1665     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1666                                              id          => $ref->{id},
1667                                              description => $ref->{description}
1668     );
1669   }
1670   $sth->finish;
1671
1672   # now get the tax rates and numbers
1673   $query = qq|SELECT c.id, c.accno, c.description,
1674               t.rate * 100 AS rate, t.taxnumber
1675               FROM chart c, tax t
1676               WHERE c.id = t.chart_id|;
1677
1678   $sth = $dbh->prepare($query);
1679   $sth->execute || $form->dberror($query);
1680
1681   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1682     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
1683     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1684     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
1685       if $ref->{taxnumber};
1686     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1687   }
1688
1689   $sth->finish;
1690   $dbh->disconnect;
1691
1692   $main::lxdebug->leave_sub();
1693 }
1694
1695 sub closedto {
1696   $main::lxdebug->enter_sub();
1697
1698   my ($self, $myconfig, $form) = @_;
1699
1700   my $dbh = $form->dbconnect($myconfig);
1701
1702   my $query = qq|SELECT closedto, revtrans FROM defaults|;
1703   my $sth   = $dbh->prepare($query);
1704   $sth->execute || $form->dberror($query);
1705
1706   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1707
1708   $sth->finish;
1709
1710   $dbh->disconnect;
1711
1712   $main::lxdebug->leave_sub();
1713 }
1714
1715 sub closebooks {
1716   $main::lxdebug->enter_sub();
1717
1718   my ($self, $myconfig, $form) = @_;
1719
1720   my $dbh = $form->dbconnect($myconfig);
1721
1722   my ($query, @values);
1723
1724   if ($form->{revtrans}) {
1725     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1726
1727   } elsif ($form->{closedto}) {
1728     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1729     @values = (conv_date($form->{closedto}));
1730
1731   } else {
1732     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1733   }
1734
1735   # set close in defaults
1736   do_query($form, $dbh, $query, @values);
1737
1738   $dbh->disconnect;
1739
1740   $main::lxdebug->leave_sub();
1741 }
1742
1743 sub get_base_unit {
1744   my ($self, $units, $unit_name, $factor) = @_;
1745
1746   $factor = 1 unless ($factor);
1747
1748   my $unit = $units->{$unit_name};
1749
1750   if (!defined($unit) || !$unit->{"base_unit"} ||
1751       ($unit_name eq $unit->{"base_unit"})) {
1752     return ($unit_name, $factor);
1753   }
1754
1755   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1756 }
1757
1758 sub retrieve_units {
1759   $main::lxdebug->enter_sub();
1760
1761   my ($self, $myconfig, $form, $type, $prefix) = @_;
1762
1763   my $dbh = $form->dbconnect($myconfig);
1764
1765   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1766   my @values;
1767   if ($type) {
1768     $query .= " WHERE (type = ?)";
1769     @values = ($type);
1770   }
1771
1772   my $sth = $dbh->prepare($query);
1773   $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1774
1775   my $units = {};
1776   while (my $ref = $sth->fetchrow_hashref()) {
1777     $units->{$ref->{"name"}} = $ref;
1778   }
1779   $sth->finish();
1780
1781   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1782   $sth = $dbh->prepare($query_lang);
1783   $sth->execute() || $form->dberror($query_lang);
1784   my @languages;
1785   while ($ref = $sth->fetchrow_hashref()) {
1786     push(@languages, $ref);
1787   }
1788   $sth->finish();
1789
1790   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1791     "FROM units_language ul " .
1792     "LEFT JOIN language l ON ul.language_id = l.id " .
1793     "WHERE ul.unit = ?";
1794   $sth = $dbh->prepare($query_lang);
1795
1796   foreach my $unit (values(%{$units})) {
1797     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1798
1799     $unit->{"LANGUAGES"} = {};
1800     foreach my $lang (@languages) {
1801       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1802     }
1803
1804     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1805     while ($ref = $sth->fetchrow_hashref()) {
1806       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1807     }
1808   }
1809   $sth->finish();
1810
1811   $dbh->disconnect();
1812
1813   $main::lxdebug->leave_sub();
1814
1815   return $units;
1816 }
1817
1818 sub translate_units {
1819   $main::lxdebug->enter_sub();
1820
1821   my ($self, $form, $template_code, $unit, $amount) = @_;
1822
1823   my $units = $self->retrieve_units(\%main::myconfig, $form);
1824
1825   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1826   my $new_unit = $unit;
1827   if ($h) {
1828     if (($amount != 1) && $h->{"localized_plural"}) {
1829       $new_unit = $h->{"localized_plural"};
1830     } elsif ($h->{"localized"}) {
1831       $new_unit = $h->{"localized"};
1832     }
1833   }
1834
1835   $main::lxdebug->leave_sub();
1836
1837   return $new_unit;
1838 }
1839
1840 sub units_in_use {
1841   $main::lxdebug->enter_sub();
1842
1843   my ($self, $myconfig, $form, $units) = @_;
1844
1845   my $dbh = $form->dbconnect($myconfig);
1846
1847   map({ $_->{"in_use"} = 0; } values(%{$units}));
1848
1849   foreach my $unit (values(%{$units})) {
1850     my $base_unit = $unit->{"original_base_unit"};
1851     while ($base_unit) {
1852       $units->{$base_unit}->{"in_use"} = 1;
1853       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1854       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1855       $base_unit = $units->{$base_unit}->{"original_base_unit"};
1856     }
1857   }
1858
1859   foreach my $unit (values(%{$units})) {
1860     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1861
1862     foreach my $table (qw(parts invoice orderitems)) {
1863       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1864
1865       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1866         $query .= "= " . $dbh->quote($unit->{"name"});
1867       } else {
1868         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1869           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1870       }
1871
1872       my ($count) = $dbh->selectrow_array($query);
1873       $form->dberror($query) if ($dbh->err);
1874
1875       if ($count) {
1876         $unit->{"in_use"} = 1;
1877         last;
1878       }
1879     }
1880   }
1881
1882   $dbh->disconnect();
1883
1884   $main::lxdebug->leave_sub();
1885 }
1886
1887 sub unit_select_data {
1888   $main::lxdebug->enter_sub();
1889
1890   my ($self, $units, $selected, $empty_entry) = @_;
1891
1892   my $select = [];
1893
1894   if ($empty_entry) {
1895     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1896   }
1897
1898   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1899     push(@{$select}, { "name" => $unit,
1900                        "base_unit" => $units->{$unit}->{"base_unit"},
1901                        "factor" => $units->{$unit}->{"factor"},
1902                        "selected" => ($unit eq $selected) ? "selected" : "" });
1903   }
1904
1905   $main::lxdebug->leave_sub();
1906
1907   return $select;
1908 }
1909
1910 sub unit_select_html {
1911   $main::lxdebug->enter_sub();
1912
1913   my ($self, $units, $name, $selected, $convertible_into) = @_;
1914
1915   my $select = "<select name=${name}>";
1916
1917   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1918     if (!$convertible_into ||
1919         ($units->{$convertible_into} &&
1920          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1921       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1922     }
1923   }
1924   $select .= "</select>";
1925
1926   $main::lxdebug->leave_sub();
1927
1928   return $select;
1929 }
1930
1931 sub add_unit {
1932   $main::lxdebug->enter_sub();
1933
1934   my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1935
1936   my $dbh = $form->dbconnect_noauto($myconfig);
1937
1938   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1939   my ($sortkey) = selectrow_query($form, $dbh, $query);
1940
1941   $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1942     "VALUES (?, ?, ?, ?, ?)";
1943   do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1944
1945   if ($languages) {
1946     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1947     my $sth = $dbh->prepare($query);
1948     foreach my $lang (@{$languages}) {
1949       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1950       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1951     }
1952     $sth->finish();
1953   }
1954
1955   $dbh->commit();
1956   $dbh->disconnect();
1957
1958   $main::lxdebug->leave_sub();
1959 }
1960
1961 sub save_units {
1962   $main::lxdebug->enter_sub();
1963
1964   my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1965
1966   my $dbh = $form->dbconnect_noauto($myconfig);
1967
1968   my ($base_unit, $unit, $sth, $query);
1969
1970   $query = "DELETE FROM units_language";
1971   $dbh->do($query) || $form->dberror($query);
1972
1973   if ($delete_units && (0 != scalar(@{$delete_units}))) {
1974     $query = "DELETE FROM units WHERE name IN (";
1975     map({ $query .= "?," } @{$delete_units});
1976     substr($query, -1, 1) = ")";
1977     $dbh->do($query, undef, @{$delete_units}) ||
1978       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1979   }
1980
1981   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1982   $sth = $dbh->prepare($query);
1983
1984   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1985   my $sth_lang = $dbh->prepare($query_lang);
1986
1987   foreach $unit (values(%{$units})) {
1988     $unit->{"depth"} = 0;
1989     my $base_unit = $unit;
1990     while ($base_unit->{"base_unit"}) {
1991       $unit->{"depth"}++;
1992       $base_unit = $units->{$base_unit->{"base_unit"}};
1993     }
1994   }
1995
1996   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1997     if ($unit->{"LANGUAGES"}) {
1998       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1999         next unless ($lang->{"id"} && $lang->{"localized"});
2000         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2001         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2002       }
2003     }
2004
2005     next if ($unit->{"unchanged_unit"});
2006
2007     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2008     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2009   }
2010
2011   $sth->finish();
2012   $sth_lang->finish();
2013   $dbh->commit();
2014   $dbh->disconnect();
2015
2016   $main::lxdebug->leave_sub();
2017 }
2018
2019 sub swap_units {
2020   $main::lxdebug->enter_sub();
2021
2022   my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2023
2024   my $dbh = $form->dbconnect_noauto($myconfig);
2025
2026   my $query;
2027
2028   $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2029   my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2030
2031   $query =
2032     qq|SELECT sortkey FROM units | .
2033     qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2034     qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2035   my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2036
2037   if (defined($sortkey_1)) {
2038     $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2039     my ($name_2) = selectrow_query($form, $dbh, $query);
2040
2041     if (defined($name_2)) {
2042       $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2043       my $sth = $dbh->prepare($query);
2044
2045       do_statement($form, $sth, $query, $sortkey_1, $name_2);
2046       do_statement($form, $sth, $query, $sortkey_2, $name_1);
2047     }
2048   }
2049
2050   $dbh->commit();
2051   $dbh->disconnect();
2052
2053   $main::lxdebug->leave_sub();
2054 }
2055
2056 sub taxes {
2057   $main::lxdebug->enter_sub();
2058
2059   my ($self, $myconfig, $form) = @_;
2060
2061   # connect to database
2062   my $dbh = $form->dbconnect($myconfig);
2063
2064   my $query = qq|SELECT
2065                    t.id,
2066                    t.taxkey,
2067                    t.taxdescription,
2068                    round(t.rate * 100, 2) AS rate,
2069                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2070                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
2071                  FROM tax t
2072                  ORDER BY taxkey|;
2073
2074   $sth = $dbh->prepare($query);
2075   $sth->execute || $form->dberror($query);
2076
2077   $form->{TAX} = [];
2078   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2079     push @{ $form->{TAX} }, $ref;
2080   }
2081
2082   $sth->finish;
2083   $dbh->disconnect;
2084
2085   $main::lxdebug->leave_sub();
2086 }
2087
2088 sub get_tax_accounts {
2089   $main::lxdebug->enter_sub();
2090
2091   my ($self, $myconfig, $form) = @_;
2092
2093   my $dbh = $form->dbconnect($myconfig);
2094
2095   # get Accounts from chart
2096   my $query = qq{ SELECT
2097                  id,
2098                  accno || ' - ' || description AS taxaccount
2099                FROM chart
2100                WHERE link LIKE '%_tax%'
2101                ORDER BY accno
2102              };
2103
2104   $sth = $dbh->prepare($query);
2105   $sth->execute || $form->dberror($query);
2106
2107   $form->{ACCOUNTS} = [];
2108   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2109     push @{ $form->{ACCOUNTS} }, $ref;
2110   }
2111
2112   $sth->finish;
2113
2114   $dbh->disconnect;
2115
2116   $main::lxdebug->leave_sub();
2117 }
2118
2119 sub get_tax {
2120   $main::lxdebug->enter_sub();
2121
2122   my ($self, $myconfig, $form) = @_;
2123
2124   # connect to database
2125   my $dbh = $form->dbconnect($myconfig);
2126
2127   my $query = qq|SELECT
2128                    taxkey,
2129                    taxdescription,
2130                    round(rate * 100, 2) AS rate,
2131                    chart_id
2132                  FROM tax
2133                  WHERE id = ? |;
2134
2135   my $sth = $dbh->prepare($query);
2136   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2137
2138   my $ref = $sth->fetchrow_hashref(NAME_lc);
2139
2140   map { $form->{$_} = $ref->{$_} } keys %$ref;
2141
2142   $sth->finish;
2143
2144   # see if it is used by a taxkey
2145   $query = qq|SELECT count(*) FROM taxkeys
2146               WHERE tax_id = ? AND chart_id >0|;
2147
2148   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2149
2150   $form->{orphaned} = !$form->{orphaned};
2151   $sth->finish;
2152
2153   if (!$form->{orphaned} ) {
2154     $query = qq|SELECT DISTINCT c.id, c.accno
2155                 FROM taxkeys tk
2156                 JOIN   tax t ON (t.id = tk.tax_id)
2157                 JOIN chart c ON (c.id = tk.chart_id)
2158                 WHERE tk.tax_id = ?|;
2159
2160     $sth = $dbh->prepare($query);
2161     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2162
2163     $form->{TAXINUSE} = [];
2164     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2165       push @{ $form->{TAXINUSE} }, $ref;
2166     }
2167
2168     $sth->finish;
2169   }
2170
2171   $dbh->disconnect;
2172
2173   $main::lxdebug->leave_sub();
2174 }
2175
2176 sub save_tax {
2177   $main::lxdebug->enter_sub();
2178
2179   my ($self, $myconfig, $form) = @_;
2180
2181   # connect to database
2182   my $dbh = $form->get_standard_dbh($myconfig);
2183
2184   $form->{rate} = $form->{rate} / 100;
2185
2186   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2187   if ($form->{id} ne "") {
2188     $query = qq|UPDATE tax SET
2189                   taxkey         = ?,
2190                   taxdescription = ?,
2191                   rate           = ?,
2192                   chart_id       = ?,
2193                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
2194                 WHERE id = ?|;
2195     push(@values, $form->{id});
2196
2197   } else {
2198     #ok
2199     $query = qq|INSERT INTO tax (
2200                   taxkey,
2201                   taxdescription,
2202                   rate,
2203                   chart_id,
2204                   taxnumber
2205                 )
2206                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2207   }
2208   do_query($form, $dbh, $query, @values);
2209
2210   $dbh->commit();
2211
2212   $main::lxdebug->leave_sub();
2213 }
2214
2215 sub delete_tax {
2216   $main::lxdebug->enter_sub();
2217
2218   my ($self, $myconfig, $form) = @_;
2219
2220   # connect to database
2221   my $dbh = $form->get_standard_dbh($myconfig);
2222
2223   $query = qq|DELETE FROM tax
2224               WHERE id = ?|;
2225   do_query($form, $dbh, $query, $form->{id});
2226
2227   $dbh->commit();
2228
2229   $main::lxdebug->leave_sub();
2230 }
2231
2232
2233
2234 1;