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