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