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