Uebernahme der kompletten Version, so wie sie Philip als "Demo-Version" gezeigt hat...
[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 business {
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, description, discount, customernumberinit, salesman
652                  FROM business
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_business {
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 b.description, b.discount, b.customernumberinit, b.salesman
678                  FROM business b
679                  WHERE b.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_business {
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->{description} =~ s/\'/\'\'/g;
703   $form->{discount} /= 100;
704   $form->{salesman} *= 1;
705
706   # id is the old record
707   if ($form->{id}) {
708     $query = qq|UPDATE business SET
709                 description = '$form->{description}',
710                 discount = $form->{discount},
711                 customernumberinit = '$form->{customernumberinit}',
712                 salesman = '$form->{salesman}'
713                 WHERE id = $form->{id}|;
714   } else {
715     $query = qq|INSERT INTO business
716                 (description, discount, customernumberinit, salesman)
717                 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
718   }
719   $dbh->do($query) || $form->dberror($query);
720
721   $dbh->disconnect;
722
723   $main::lxdebug->leave_sub();
724 }
725
726 sub delete_business {
727   $main::lxdebug->enter_sub();
728
729   my ($self, $myconfig, $form) = @_;
730
731   # connect to database
732   my $dbh = $form->dbconnect($myconfig);
733
734   $query = qq|DELETE FROM business
735               WHERE id = $form->{id}|;
736   $dbh->do($query) || $form->dberror($query);
737
738   $dbh->disconnect;
739
740   $main::lxdebug->leave_sub();
741 }
742
743
744 sub language {
745   $main::lxdebug->enter_sub();
746
747   my ($self, $myconfig, $form) = @_;
748
749   # connect to database
750   my $dbh = $form->dbconnect($myconfig);
751
752   my $query = qq|SELECT id, description, template_code, article_code
753                  FROM language
754                  ORDER BY 2|;
755
756   $sth = $dbh->prepare($query);
757   $sth->execute || $form->dberror($query);
758
759   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
760     push @{ $form->{ALL} }, $ref;
761   }
762
763   $sth->finish;
764   $dbh->disconnect;
765
766   $main::lxdebug->leave_sub();
767 }
768
769 sub get_language {
770   $main::lxdebug->enter_sub();
771
772   my ($self, $myconfig, $form) = @_;
773
774   # connect to database
775   my $dbh = $form->dbconnect($myconfig);
776
777   my $query =
778     qq|SELECT l.description, l.template_code, l.article_code
779                  FROM language l
780                  WHERE l.id = $form->{id}|;
781   my $sth = $dbh->prepare($query);
782   $sth->execute || $form->dberror($query);
783
784   my $ref = $sth->fetchrow_hashref(NAME_lc);
785
786   map { $form->{$_} = $ref->{$_} } keys %$ref;
787
788   $sth->finish;
789
790   $dbh->disconnect;
791
792   $main::lxdebug->leave_sub();
793 }
794
795 sub save_language {
796   $main::lxdebug->enter_sub();
797
798   my ($self, $myconfig, $form) = @_;
799
800   # connect to database
801   my $dbh = $form->dbconnect($myconfig);
802
803   $form->{description} =~ s/\'/\'\'/g;
804   $form->{article_code} =~ s/\'/\'\'/g;
805   $form->{template_code} =~ s/\'/\'\'/g;
806
807
808   # id is the old record
809   if ($form->{id}) {
810     $query = qq|UPDATE language SET
811                 description = '$form->{description}',
812                 template_code = '$form->{template_code}',
813                 article_code = '$form->{article_code}'
814                 WHERE id = $form->{id}|;
815   } else {
816     $query = qq|INSERT INTO language
817                 (description, template_code, article_code)
818                 VALUES ('$form->{description}', '$form->{template_code}', '$form->{article_code}')|;
819   }
820   $dbh->do($query) || $form->dberror($query);
821
822   $dbh->disconnect;
823
824   $main::lxdebug->leave_sub();
825 }
826
827 sub delete_language {
828   $main::lxdebug->enter_sub();
829
830   my ($self, $myconfig, $form) = @_;
831
832   # connect to database
833   my $dbh = $form->dbconnect($myconfig);
834
835   $query = qq|DELETE FROM language
836               WHERE id = $form->{id}|;
837   $dbh->do($query) || $form->dberror($query);
838
839   $dbh->disconnect;
840
841   $main::lxdebug->leave_sub();
842 }
843
844
845 sub buchungsgruppe {
846   $main::lxdebug->enter_sub();
847
848   my ($self, $myconfig, $form) = @_;
849
850   # connect to database
851   my $dbh = $form->dbconnect($myconfig);
852
853   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
854                   FROM buchungsgruppen
855                  ORDER BY id|;
856
857   $sth = $dbh->prepare($query);
858   $sth->execute || $form->dberror($query);
859
860   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
861     push @{ $form->{ALL} }, $ref;
862   }
863
864   $sth->finish;
865   $dbh->disconnect;
866
867   $main::lxdebug->leave_sub();
868 }
869
870 sub get_buchungsgruppe {
871   $main::lxdebug->enter_sub();
872
873   my ($self, $myconfig, $form) = @_;
874
875   # connect to database
876   my $dbh = $form->dbconnect($myconfig);
877
878   if ($form->{id}) {
879     my $query =
880       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
881                   FROM buchungsgruppen
882                   WHERE id = $form->{id}|;
883     my $sth = $dbh->prepare($query);
884     $sth->execute || $form->dberror($query);
885   
886     my $ref = $sth->fetchrow_hashref(NAME_lc);
887   
888     map { $form->{$_} = $ref->{$_} } keys %$ref;
889   
890     $sth->finish;
891   
892     my $query =
893       qq|SELECT count(id) as anzahl
894                   FROM parts
895                   WHERE buchungsgruppen_id = $form->{id}|;
896     my $sth = $dbh->prepare($query);
897     $sth->execute || $form->dberror($query);
898   
899     my $ref = $sth->fetchrow_hashref(NAME_lc);
900     if (!$ref->{anzahl}) {
901       $form->{orphaned} = 1;
902     }
903     $sth->finish;
904
905   }
906   my $module = "IC";
907   $query = qq|SELECT c.accno, c.description, c.link, c.id,
908               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
909               FROM chart c, defaults d
910               WHERE c.link LIKE '%$module%'
911               ORDER BY c.accno|;
912
913
914   my $sth = $dbh->prepare($query);
915   $sth->execute || $form->dberror($query);
916   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
917     foreach my $key (split /:/, $ref->{link}) {
918       if ($key =~ /$module/) {
919         if (   ($ref->{id} eq $ref->{inventory_accno_id})
920             || ($ref->{id} eq $ref->{income_accno_id})
921             || ($ref->{id} eq $ref->{expense_accno_id})) {
922           push @{ $form->{"${module}_links"}{$key} },
923             { accno       => $ref->{accno},
924               description => $ref->{description},
925               selected    => "selected",
926               id          => $ref->{id} };
927             } else {
928           push @{ $form->{"${module}_links"}{$key} },
929             { accno       => $ref->{accno},
930               description => $ref->{description},
931               selected    => "",
932               id          => $ref->{id} };
933         }
934       }
935     }
936   }
937   $sth->finish;
938
939
940   $dbh->disconnect;
941
942   $main::lxdebug->leave_sub();
943 }
944
945 sub save_buchungsgruppe {
946   $main::lxdebug->enter_sub();
947
948   my ($self, $myconfig, $form) = @_;
949
950   # connect to database
951   my $dbh = $form->dbconnect($myconfig);
952
953   $form->{description} =~ s/\'/\'\'/g;
954
955
956   # id is the old record
957   if ($form->{id}) {
958     $query = qq|UPDATE buchungsgruppen SET
959                 description = '$form->{description}',
960                 inventory_accno_id = '$form->{inventory_accno_id}',
961                 income_accno_id_0 = '$form->{income_accno_id_0}',
962                 expense_accno_id_0 = '$form->{expense_accno_id_0}',
963                 income_accno_id_1 = '$form->{income_accno_id_1}',
964                 expense_accno_id_1 = '$form->{expense_accno_id_1}',
965                 income_accno_id_2 = '$form->{income_accno_id_2}',
966                 expense_accno_id_2 = '$form->{expense_accno_id_2}',
967                 income_accno_id_3 = '$form->{income_accno_id_3}',
968                 expense_accno_id_3 = '$form->{expense_accno_id_3}'
969                 WHERE id = $form->{id}|;
970   } else {
971     $query = qq|INSERT INTO buchungsgruppen
972                 (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)
973                 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}')|;
974   }
975   $dbh->do($query) || $form->dberror($query);
976
977   $dbh->disconnect;
978
979   $main::lxdebug->leave_sub();
980 }
981
982 sub delete_buchungsgruppe {
983   $main::lxdebug->enter_sub();
984
985   my ($self, $myconfig, $form) = @_;
986
987   # connect to database
988   my $dbh = $form->dbconnect($myconfig);
989
990   $query = qq|DELETE FROM buchungsgruppe
991               WHERE id = $form->{id}|;
992   $dbh->do($query) || $form->dberror($query);
993
994   $dbh->disconnect;
995
996   $main::lxdebug->leave_sub();
997 }
998
999 sub printer {
1000   $main::lxdebug->enter_sub();
1001
1002   my ($self, $myconfig, $form) = @_;
1003
1004   # connect to database
1005   my $dbh = $form->dbconnect($myconfig);
1006
1007   my $query = qq|SELECT id, printer_description, template_code, printer_command
1008                  FROM printers
1009                  ORDER BY 2|;
1010
1011   $sth = $dbh->prepare($query);
1012   $sth->execute || $form->dberror($query);
1013
1014   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1015     push @{ $form->{ALL} }, $ref;
1016   }
1017
1018   $sth->finish;
1019   $dbh->disconnect;
1020
1021   $main::lxdebug->leave_sub();
1022 }
1023
1024 sub get_printer {
1025   $main::lxdebug->enter_sub();
1026
1027   my ($self, $myconfig, $form) = @_;
1028
1029   # connect to database
1030   my $dbh = $form->dbconnect($myconfig);
1031
1032   my $query =
1033     qq|SELECT p.printer_description, p.template_code, p.printer_command
1034                  FROM printers p
1035                  WHERE p.id = $form->{id}|;
1036   my $sth = $dbh->prepare($query);
1037   $sth->execute || $form->dberror($query);
1038
1039   my $ref = $sth->fetchrow_hashref(NAME_lc);
1040
1041   map { $form->{$_} = $ref->{$_} } keys %$ref;
1042
1043   $sth->finish;
1044
1045   $dbh->disconnect;
1046
1047   $main::lxdebug->leave_sub();
1048 }
1049
1050 sub save_printer {
1051   $main::lxdebug->enter_sub();
1052
1053   my ($self, $myconfig, $form) = @_;
1054
1055   # connect to database
1056   my $dbh = $form->dbconnect($myconfig);
1057
1058   $form->{printer_description} =~ s/\'/\'\'/g;
1059   $form->{printer_command} =~ s/\'/\'\'/g;
1060   $form->{template_code} =~ s/\'/\'\'/g;
1061
1062
1063   # id is the old record
1064   if ($form->{id}) {
1065     $query = qq|UPDATE printers SET
1066                 printer_description = '$form->{printer_description}',
1067                 template_code = '$form->{template_code}',
1068                 printer_command = '$form->{printer_command}'
1069                 WHERE id = $form->{id}|;
1070   } else {
1071     $query = qq|INSERT INTO printers
1072                 (printer_description, template_code, printer_command)
1073                 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1074   }
1075   $dbh->do($query) || $form->dberror($query);
1076
1077   $dbh->disconnect;
1078
1079   $main::lxdebug->leave_sub();
1080 }
1081
1082 sub delete_printer {
1083   $main::lxdebug->enter_sub();
1084
1085   my ($self, $myconfig, $form) = @_;
1086
1087   # connect to database
1088   my $dbh = $form->dbconnect($myconfig);
1089
1090   $query = qq|DELETE FROM printers
1091               WHERE id = $form->{id}|;
1092   $dbh->do($query) || $form->dberror($query);
1093
1094   $dbh->disconnect;
1095
1096   $main::lxdebug->leave_sub();
1097 }
1098
1099 sub adr {
1100   $main::lxdebug->enter_sub();
1101
1102   my ($self, $myconfig, $form) = @_;
1103
1104   # connect to database
1105   my $dbh = $form->dbconnect($myconfig);
1106
1107   my $query = qq|SELECT id, adr_description, adr_code
1108                  FROM adr
1109                  ORDER BY adr_code|;
1110
1111   $sth = $dbh->prepare($query);
1112   $sth->execute || $form->dberror($query);
1113
1114   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1115     push @{ $form->{ALL} }, $ref;
1116   }
1117
1118   $sth->finish;
1119   $dbh->disconnect;
1120
1121   $main::lxdebug->leave_sub();
1122 }
1123
1124 sub get_adr {
1125   $main::lxdebug->enter_sub();
1126
1127   my ($self, $myconfig, $form) = @_;
1128
1129   # connect to database
1130   my $dbh = $form->dbconnect($myconfig);
1131
1132   my $query =
1133     qq|SELECT a.adr_description, a.adr_code
1134                  FROM adr a
1135                  WHERE a.id = $form->{id}|;
1136   my $sth = $dbh->prepare($query);
1137   $sth->execute || $form->dberror($query);
1138
1139   my $ref = $sth->fetchrow_hashref(NAME_lc);
1140
1141   map { $form->{$_} = $ref->{$_} } keys %$ref;
1142
1143   $sth->finish;
1144
1145   $dbh->disconnect;
1146
1147   $main::lxdebug->leave_sub();
1148 }
1149
1150 sub save_adr {
1151   $main::lxdebug->enter_sub();
1152
1153   my ($self, $myconfig, $form) = @_;
1154
1155   # connect to database
1156   my $dbh = $form->dbconnect($myconfig);
1157
1158   $form->{adr_description} =~ s/\'/\'\'/g;
1159   $form->{adr_code} =~ s/\'/\'\'/g;
1160
1161
1162   # id is the old record
1163   if ($form->{id}) {
1164     $query = qq|UPDATE adr SET
1165                 adr_description = '$form->{adr_description}',
1166                 adr_code = '$form->{adr_code}'
1167                 WHERE id = $form->{id}|;
1168   } else {
1169     $query = qq|INSERT INTO adr
1170                 (adr_description, adr_code)
1171                 VALUES ('$form->{adr_description}', '$form->{adr_code}')|;
1172   }
1173   $dbh->do($query) || $form->dberror($query);
1174
1175   $dbh->disconnect;
1176
1177   $main::lxdebug->leave_sub();
1178 }
1179
1180 sub delete_adr {
1181   $main::lxdebug->enter_sub();
1182
1183   my ($self, $myconfig, $form) = @_;
1184
1185   # connect to database
1186   my $dbh = $form->dbconnect($myconfig);
1187
1188   $query = qq|DELETE FROM adr
1189               WHERE id = $form->{id}|;
1190   $dbh->do($query) || $form->dberror($query);
1191
1192   $dbh->disconnect;
1193
1194   $main::lxdebug->leave_sub();
1195 }
1196
1197 sub payment {
1198   $main::lxdebug->enter_sub();
1199
1200   my ($self, $myconfig, $form) = @_;
1201
1202   # connect to database
1203   my $dbh = $form->dbconnect($myconfig);
1204
1205   my $query = qq|SELECT *
1206                  FROM payment_terms
1207                  ORDER BY id|;
1208
1209   $sth = $dbh->prepare($query);
1210   $sth->execute || $form->dberror($query);
1211
1212   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {   
1213     $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1214     push @{ $form->{ALL} }, $ref;
1215   }
1216
1217   $sth->finish;
1218   $dbh->disconnect;
1219
1220   $main::lxdebug->leave_sub();
1221 }
1222
1223 sub get_payment {
1224   $main::lxdebug->enter_sub();
1225
1226   my ($self, $myconfig, $form) = @_;
1227
1228   # connect to database
1229   my $dbh = $form->dbconnect($myconfig);
1230
1231   my $query =
1232     qq|SELECT *
1233                  FROM payment_terms
1234                  WHERE id = $form->{id}|;
1235   my $sth = $dbh->prepare($query);
1236   $sth->execute || $form->dberror($query);
1237
1238   my $ref = $sth->fetchrow_hashref(NAME_lc);
1239   $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1240
1241   map { $form->{$_} = $ref->{$_} } keys %$ref;
1242
1243   $sth->finish;
1244
1245   $dbh->disconnect;
1246
1247   $main::lxdebug->leave_sub();
1248 }
1249
1250 sub save_payment {
1251   $main::lxdebug->enter_sub();
1252
1253   my ($self, $myconfig, $form) = @_;
1254
1255   # connect to database
1256   my $dbh = $form->dbconnect($myconfig);
1257
1258   $form->{description} =~ s/\'/\'\'/g;
1259   $form->{description_long} =~ s/\'/\'\'/g;
1260   $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100;
1261   $form->{ranking} *= 1;
1262   $form->{terms_netto} *= 1;
1263   $form->{terms_skonto} *= 1;
1264   $form->{percent_skonto} *= 1;
1265
1266
1267
1268   # id is the old record
1269   if ($form->{id}) {
1270     $query = qq|UPDATE payment_terms SET
1271                 description = '$form->{description}',
1272                 ranking = $form->{ranking},
1273                 description_long = '$form->{description_long}',
1274                 terms_netto = $form->{terms_netto},
1275                 terms_skonto = $form->{terms_skonto},
1276                 percent_skonto = $percentskonto
1277                 WHERE id = $form->{id}|;
1278   } else {
1279     $query = qq|INSERT INTO payment_terms
1280                 (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto)
1281                 VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|;
1282   }
1283   $dbh->do($query) || $form->dberror($query);
1284
1285   $dbh->disconnect;
1286
1287   $main::lxdebug->leave_sub();
1288 }
1289
1290 sub delete_payment {
1291   $main::lxdebug->enter_sub();
1292
1293   my ($self, $myconfig, $form) = @_;
1294
1295   # connect to database
1296   my $dbh = $form->dbconnect($myconfig);
1297
1298   $query = qq|DELETE FROM payment_terms
1299               WHERE id = $form->{id}|;
1300   $dbh->do($query) || $form->dberror($query);
1301
1302   $dbh->disconnect;
1303
1304   $main::lxdebug->leave_sub();
1305 }
1306
1307 sub sic {
1308   $main::lxdebug->enter_sub();
1309
1310   my ($self, $myconfig, $form) = @_;
1311
1312   # connect to database
1313   my $dbh = $form->dbconnect($myconfig);
1314
1315   my $query = qq|SELECT code, sictype, description
1316                  FROM sic
1317                  ORDER BY code|;
1318
1319   $sth = $dbh->prepare($query);
1320   $sth->execute || $form->dberror($query);
1321
1322   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1323     push @{ $form->{ALL} }, $ref;
1324   }
1325
1326   $sth->finish;
1327   $dbh->disconnect;
1328
1329   $main::lxdebug->leave_sub();
1330 }
1331
1332 sub get_sic {
1333   $main::lxdebug->enter_sub();
1334
1335   my ($self, $myconfig, $form) = @_;
1336
1337   # connect to database
1338   my $dbh = $form->dbconnect($myconfig);
1339
1340   my $query = qq|SELECT s.code, s.sictype, s.description
1341                  FROM sic s
1342                  WHERE s.code = '$form->{code}'|;
1343   my $sth = $dbh->prepare($query);
1344   $sth->execute || $form->dberror($query);
1345
1346   my $ref = $sth->fetchrow_hashref(NAME_lc);
1347
1348   map { $form->{$_} = $ref->{$_} } keys %$ref;
1349
1350   $sth->finish;
1351
1352   $dbh->disconnect;
1353
1354   $main::lxdebug->leave_sub();
1355 }
1356
1357 sub save_sic {
1358   $main::lxdebug->enter_sub();
1359
1360   my ($self, $myconfig, $form) = @_;
1361
1362   # connect to database
1363   my $dbh = $form->dbconnect($myconfig);
1364
1365   $form->{code}        =~ s/\'/\'\'/g;
1366   $form->{description} =~ s/\'/\'\'/g;
1367
1368   # if there is an id
1369   if ($form->{id}) {
1370     $query = qq|UPDATE sic SET
1371                 code = '$form->{code}',
1372                 sictype = '$form->{sictype}',
1373                 description = '$form->{description}'
1374                 WHERE code = '$form->{id}'|;
1375   } else {
1376     $query = qq|INSERT INTO sic
1377                 (code, sictype, description)
1378                 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1379   }
1380   $dbh->do($query) || $form->dberror($query);
1381
1382   $dbh->disconnect;
1383
1384   $main::lxdebug->leave_sub();
1385 }
1386
1387 sub delete_sic {
1388   $main::lxdebug->enter_sub();
1389
1390   my ($self, $myconfig, $form) = @_;
1391
1392   # connect to database
1393   my $dbh = $form->dbconnect($myconfig);
1394
1395   $query = qq|DELETE FROM sic
1396               WHERE code = '$form->{code}'|;
1397   $dbh->do($query) || $form->dberror($query);
1398
1399   $dbh->disconnect;
1400
1401   $main::lxdebug->leave_sub();
1402 }
1403
1404 sub load_template {
1405   $main::lxdebug->enter_sub();
1406
1407   my ($self, $form) = @_;
1408
1409   open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1410
1411   while (<TEMPLATE>) {
1412     $form->{body} .= $_;
1413   }
1414
1415   close(TEMPLATE);
1416
1417   $main::lxdebug->leave_sub();
1418 }
1419
1420 sub save_template {
1421   $main::lxdebug->enter_sub();
1422
1423   my ($self, $form) = @_;
1424
1425   open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1426
1427   # strip
1428   $form->{body} =~ s/\r\n/\n/g;
1429   print TEMPLATE $form->{body};
1430
1431   close(TEMPLATE);
1432
1433   $main::lxdebug->leave_sub();
1434 }
1435
1436 sub save_preferences {
1437   $main::lxdebug->enter_sub();
1438
1439   my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1440
1441   map { ($form->{$_}) = split /--/, $form->{$_} }
1442     qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1443
1444   my @a;
1445   $form->{curr} =~ s/ //g;
1446   map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
1447   $form->{curr} = join ':', @a;
1448
1449   # connect to database
1450   my $dbh = $form->dbconnect_noauto($myconfig);
1451
1452   # these defaults are database wide
1453   # user specific variables are in myconfig
1454   # save defaults
1455   my $query = qq|UPDATE defaults SET
1456                  inventory_accno_id =
1457                      (SELECT c.id FROM chart c
1458                                 WHERE c.accno = '$form->{inventory_accno}'),
1459                  income_accno_id =
1460                      (SELECT c.id FROM chart c
1461                                 WHERE c.accno = '$form->{income_accno}'),
1462                  expense_accno_id =
1463                      (SELECT c.id FROM chart c
1464                                 WHERE c.accno = '$form->{expense_accno}'),
1465                  fxgain_accno_id =
1466                      (SELECT c.id FROM chart c
1467                                 WHERE c.accno = '$form->{fxgain_accno}'),
1468                  fxloss_accno_id =
1469                      (SELECT c.id FROM chart c
1470                                 WHERE c.accno = '$form->{fxloss_accno}'),
1471                  invnumber = '$form->{invnumber}',
1472                  cnnumber  = '$form->{cnnumber}',
1473                  sonumber = '$form->{sonumber}',
1474                  ponumber = '$form->{ponumber}',
1475                  sqnumber = '$form->{sqnumber}',
1476                  rfqnumber = '$form->{rfqnumber}',
1477                  customernumber = '$form->{customernumber}',
1478                  vendornumber = '$form->{vendornumber}',
1479                  articlenumber = '$form->{articlenumber}',
1480                  servicenumber = '$form->{servicenumber}',
1481                  yearend = '$form->{yearend}',
1482                  curr = '$form->{curr}',
1483                  weightunit = '$form->{weightunit}',
1484                  businessnumber = '$form->{businessnumber}'
1485                 |;
1486   $dbh->do($query) || $form->dberror($query);
1487
1488   # update name
1489   my $name = $form->{name};
1490   $name =~ s/\'/\'\'/g;
1491   $query = qq|UPDATE employee
1492               SET name = '$name'
1493               WHERE login = '$form->{login}'|;
1494   $dbh->do($query) || $form->dberror($query);
1495
1496   foreach my $item (split / /, $form->{taxaccounts}) {
1497     $query = qq|UPDATE tax
1498                 SET rate = | . ($form->{$item} / 100) . qq|,
1499                 taxnumber = '$form->{"taxnumber_$item"}'
1500                 WHERE chart_id = $item|;
1501     $dbh->do($query) || $form->dberror($query);
1502   }
1503
1504   my $rc = $dbh->commit;
1505   $dbh->disconnect;
1506
1507   # save first currency in myconfig
1508   $form->{currency} = substr($form->{curr}, 0, 3);
1509
1510   my $myconfig = new User "$memberfile", "$form->{login}";
1511
1512   foreach my $item (keys %$form) {
1513     $myconfig->{$item} = $form->{$item};
1514   }
1515
1516   $myconfig->save_member($memberfile, $userspath);
1517
1518   if ($webdav) {
1519     @webdavdirs =
1520       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1521     foreach $directory (@webdavdirs) {
1522       $file = "webdav/" . $directory . "/webdav-user";
1523       if ($myconfig->{$directory}) {
1524         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1525         while (<HTACCESS>) {
1526           ($login, $password) = split(/:/, $_);
1527           if ($login ne $form->{login}) {
1528             $newfile .= $_;
1529           }
1530         }
1531         close(HTACCESS);
1532         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1533         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1534         print(HTACCESS $newfile);
1535         close(HTACCESS);
1536       } else {
1537         $form->{$directory} = 0;
1538         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1539         while (<HTACCESS>) {
1540           ($login, $password) = split(/:/, $_);
1541           if ($login ne $form->{login}) {
1542             $newfile .= $_;
1543           }
1544         }
1545         close(HTACCESS);
1546         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1547         print(HTACCESS $newfile);
1548         close(HTACCESS);
1549       }
1550     }
1551   }
1552
1553   $main::lxdebug->leave_sub();
1554
1555   return $rc;
1556 }
1557
1558 sub defaultaccounts {
1559   $main::lxdebug->enter_sub();
1560
1561   my ($self, $myconfig, $form) = @_;
1562
1563   # connect to database
1564   my $dbh = $form->dbconnect($myconfig);
1565
1566   # get defaults from defaults table
1567   my $query = qq|SELECT * FROM defaults|;
1568   my $sth   = $dbh->prepare($query);
1569   $sth->execute || $form->dberror($query);
1570
1571   $form->{defaults}             = $sth->fetchrow_hashref(NAME_lc);
1572   $form->{defaults}{IC}         = $form->{defaults}{inventory_accno_id};
1573   $form->{defaults}{IC_income}  = $form->{defaults}{income_accno_id};
1574   $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1575   $form->{defaults}{FX_gain}    = $form->{defaults}{fxgain_accno_id};
1576   $form->{defaults}{FX_loss}    = $form->{defaults}{fxloss_accno_id};
1577
1578   $sth->finish;
1579
1580   $query = qq|SELECT c.id, c.accno, c.description, c.link
1581               FROM chart c
1582               WHERE c.link LIKE '%IC%'
1583               ORDER BY c.accno|;
1584   $sth = $dbh->prepare($query);
1585   $sth->execute || $self->dberror($query);
1586
1587   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1588     foreach my $key (split(/:/, $ref->{link})) {
1589       if ($key =~ /IC/) {
1590         $nkey = $key;
1591         if ($key =~ /cogs/) {
1592           $nkey = "IC_expense";
1593         }
1594         if ($key =~ /sale/) {
1595           $nkey = "IC_income";
1596         }
1597         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1598                                              id          => $ref->{id},
1599                                              description => $ref->{description}
1600         );
1601       }
1602     }
1603   }
1604   $sth->finish;
1605
1606   $query = qq|SELECT c.id, c.accno, c.description
1607               FROM chart c
1608               WHERE c.category = 'I'
1609               AND c.charttype = 'A'
1610               ORDER BY c.accno|;
1611   $sth = $dbh->prepare($query);
1612   $sth->execute || $self->dberror($query);
1613
1614   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1615     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1616                                              id          => $ref->{id},
1617                                              description => $ref->{description}
1618     );
1619   }
1620   $sth->finish;
1621
1622   $query = qq|SELECT c.id, c.accno, c.description
1623               FROM chart c
1624               WHERE c.category = 'E'
1625               AND c.charttype = 'A'
1626               ORDER BY c.accno|;
1627   $sth = $dbh->prepare($query);
1628   $sth->execute || $self->dberror($query);
1629
1630   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1631     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1632                                              id          => $ref->{id},
1633                                              description => $ref->{description}
1634     );
1635   }
1636   $sth->finish;
1637
1638   # now get the tax rates and numbers
1639   $query = qq|SELECT c.id, c.accno, c.description,
1640               t.rate * 100 AS rate, t.taxnumber
1641               FROM chart c, tax t
1642               WHERE c.id = t.chart_id|;
1643
1644   $sth = $dbh->prepare($query);
1645   $sth->execute || $form->dberror($query);
1646
1647   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1648     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
1649     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1650     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
1651       if $ref->{taxnumber};
1652     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1653   }
1654
1655   $sth->finish;
1656   $dbh->disconnect;
1657
1658   $main::lxdebug->leave_sub();
1659 }
1660
1661 sub backup {
1662   $main::lxdebug->enter_sub();
1663
1664   my ($self, $myconfig, $form, $userspath) = @_;
1665
1666   my $mail;
1667   my $err;
1668   my $boundary = time;
1669   my $tmpfile  =
1670     "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1671   my $out = $form->{OUT};
1672   $form->{OUT} = ">$tmpfile";
1673
1674   if ($form->{media} eq 'email') {
1675
1676     use SL::Mailer;
1677     $mail = new Mailer;
1678
1679     $mail->{to}      = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1680     $mail->{from}    = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1681     $mail->{subject} =
1682       "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1683     @{ $mail->{attachments} } = ($tmpfile);
1684     $mail->{version} = $form->{version};
1685     $mail->{fileid}  = "$boundary.";
1686
1687     $myconfig->{signature} =~ s/\\n/\r\n/g;
1688     $mail->{message} = "--\n$myconfig->{signature}";
1689
1690   }
1691
1692   open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1693
1694   # get sequences, functions and triggers
1695   open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1696
1697   my @sequences = ();
1698   my @functions = ();
1699   my @triggers  = ();
1700   my @indices   = ();
1701   my %tablespecs;
1702
1703   my $query = "";
1704   my @quote_chars;
1705
1706   while (<FH>) {
1707
1708     # Remove DOS and Unix style line endings.
1709     s/[\r\n]//g;
1710
1711     # ignore comments or empty lines
1712     next if /^(--.*|\s+)$/;
1713
1714     for (my $i = 0; $i < length($_); $i++) {
1715       my $char = substr($_, $i, 1);
1716
1717       # Are we inside a string?
1718       if (@quote_chars) {
1719         if ($char eq $quote_chars[-1]) {
1720           pop(@quote_chars);
1721         }
1722         $query .= $char;
1723
1724       } else {
1725         if (($char eq "'") || ($char eq "\"")) {
1726           push(@quote_chars, $char);
1727
1728         } elsif ($char eq ";") {
1729
1730           # Query is complete. Check for triggers and functions.
1731           if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1732             push(@functions, $query);
1733
1734           } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1735             push(@triggers, $query);
1736
1737           } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1738             push(@sequences, $1);
1739
1740           } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1741             $tablespecs{$1} = $query;
1742
1743           } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1744             push(@indices, $query);
1745
1746           }
1747
1748           $query = "";
1749           $char  = "";
1750         }
1751
1752         $query .= $char;
1753       }
1754     }
1755   }
1756   close(FH);
1757
1758   # connect to database
1759   my $dbh = $form->dbconnect($myconfig);
1760
1761   # get all the tables
1762   my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1763
1764   my $today = scalar localtime;
1765
1766   $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1767
1768   print OUT qq|-- Lx-Office Backup
1769 -- Dataset: $myconfig->{dbname}
1770 -- Version: $form->{dbversion}
1771 -- Host: $myconfig->{dbhost}
1772 -- Login: $form->{login}
1773 -- User: $myconfig->{name}
1774 -- Date: $today
1775 --
1776 -- set options
1777 $myconfig->{dboptions};
1778 --
1779 |;
1780
1781   print OUT "-- DROP Sequences\n";
1782   my $item;
1783   foreach $item (@sequences) {
1784     print OUT qq|DROP SEQUENCE $item;\n|;
1785   }
1786
1787   print OUT "-- DROP Triggers\n";
1788
1789   foreach $item (@triggers) {
1790     if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1791       print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1792     }
1793   }
1794
1795   print OUT "-- DROP Functions\n";
1796
1797   foreach $item (@functions) {
1798     if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1799       print OUT qq|DROP FUNCTION "$1" ();\n|;
1800     }
1801   }
1802
1803   foreach $table (@tables) {
1804     if (!($table =~ /^sql_.*/)) {
1805       my $query = qq|SELECT * FROM $table|;
1806
1807       my $sth = $dbh->prepare($query);
1808       $sth->execute || $form->dberror($query);
1809
1810       $query = "INSERT INTO $table (";
1811       map { $query .= qq|$sth->{NAME}->[$_],| }
1812         (0 .. $sth->{NUM_OF_FIELDS} - 1);
1813       chop $query;
1814
1815       $query .= ") VALUES";
1816
1817       if ($tablespecs{$table}) {
1818         print(OUT "--\n");
1819         print(OUT "DROP TABLE $table;\n");
1820         print(OUT $tablespecs{$table}, ";\n");
1821       } else {
1822         print(OUT "--\n");
1823         print(OUT "DELETE FROM $table;\n");
1824       }
1825       while (my @arr = $sth->fetchrow_array) {
1826
1827         $fields = "(";
1828         foreach my $item (@arr) {
1829           if (defined $item) {
1830             $item =~ s/\'/\'\'/g;
1831             $fields .= qq|'$item',|;
1832           } else {
1833             $fields .= 'NULL,';
1834           }
1835         }
1836
1837         chop $fields;
1838         $fields .= ")";
1839
1840         print OUT qq|$query $fields;\n|;
1841       }
1842
1843       $sth->finish;
1844     }
1845   }
1846
1847   # create indices, sequences, functions and triggers
1848
1849   print(OUT "-- CREATE Indices\n");
1850   map({ print(OUT "$_;\n"); } @indices);
1851
1852   print OUT "-- CREATE Sequences\n";
1853   foreach $item (@sequences) {
1854     $query = qq|SELECT last_value FROM $item|;
1855     $sth   = $dbh->prepare($query);
1856     $sth->execute || $form->dberror($query);
1857     my ($id) = $sth->fetchrow_array;
1858     $sth->finish;
1859
1860     print OUT qq|--
1861 CREATE SEQUENCE $item START $id;
1862 |;
1863   }
1864
1865   print OUT "-- CREATE Functions\n";
1866
1867   # functions
1868   map { print(OUT $_, ";\n"); } @functions;
1869
1870   print OUT "-- CREATE Triggers\n";
1871
1872   # triggers
1873   map { print(OUT $_, ";\n"); } @triggers;
1874
1875   close(OUT);
1876
1877   $dbh->disconnect;
1878
1879   # compress backup
1880   my @args = ("gzip", "$tmpfile");
1881   system(@args) == 0 or $form->error("$args[0] : $?");
1882
1883   $tmpfile .= ".gz";
1884
1885   if ($form->{media} eq 'email') {
1886     @{ $mail->{attachments} } = ($tmpfile);
1887     $err = $mail->send($out);
1888   }
1889
1890   if ($form->{media} eq 'file') {
1891
1892     open(IN,  "$tmpfile") or $form->error("$tmpfile : $!");
1893     open(OUT, ">-")       or $form->error("STDOUT : $!");
1894
1895     print OUT qq|Content-Type: application/x-tar-gzip;
1896 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1897
1898 |;
1899
1900     while (<IN>) {
1901       print OUT $_;
1902     }
1903
1904     close(IN);
1905     close(OUT);
1906
1907   }
1908
1909   unlink "$tmpfile";
1910
1911   $main::lxdebug->leave_sub();
1912 }
1913
1914 sub closedto {
1915   $main::lxdebug->enter_sub();
1916
1917   my ($self, $myconfig, $form) = @_;
1918
1919   my $dbh = $form->dbconnect($myconfig);
1920
1921   my $query = qq|SELECT closedto, revtrans FROM defaults|;
1922   my $sth   = $dbh->prepare($query);
1923   $sth->execute || $form->dberror($query);
1924
1925   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1926
1927   $sth->finish;
1928
1929   $dbh->disconnect;
1930
1931   $main::lxdebug->leave_sub();
1932 }
1933
1934 sub closebooks {
1935   $main::lxdebug->enter_sub();
1936
1937   my ($self, $myconfig, $form) = @_;
1938
1939   my $dbh = $form->dbconnect($myconfig);
1940
1941   if ($form->{revtrans}) {
1942
1943     $query = qq|UPDATE defaults SET closedto = NULL,
1944                                     revtrans = '1'|;
1945   } else {
1946     if ($form->{closedto}) {
1947
1948       $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1949                                       revtrans = '0'|;
1950     } else {
1951
1952       $query = qq|UPDATE defaults SET closedto = NULL,
1953                                       revtrans = '0'|;
1954     }
1955   }
1956
1957   # set close in defaults
1958   $dbh->do($query) || $form->dberror($query);
1959
1960   $dbh->disconnect;
1961
1962   $main::lxdebug->leave_sub();
1963 }
1964
1965 sub get_base_unit {
1966   my ($self, $units, $unit_name, $factor) = @_;
1967
1968   $factor = 1 unless ($factor);
1969
1970   my $unit = $units->{$unit_name};
1971
1972   if (!defined($unit) || !$unit->{"base_unit"} ||
1973       ($unit_name eq $unit->{"base_unit"})) {
1974     return ($unit_name, $factor);
1975   }
1976
1977   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1978 }
1979
1980 sub retrieve_units {
1981   $main::lxdebug->enter_sub();
1982
1983   my ($self, $myconfig, $form, $type, $prefix) = @_;
1984
1985   my $dbh = $form->dbconnect($myconfig);
1986
1987   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1988   my @values;
1989   if ($type) {
1990     $query .= " WHERE (type = ?)";
1991     @values = ($type);
1992   }
1993
1994   my $sth = $dbh->prepare($query);
1995   $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1996
1997   my $units = {};
1998   while (my $ref = $sth->fetchrow_hashref()) {
1999     $units->{$ref->{"name"}} = $ref;
2000   }
2001   $sth->finish();
2002
2003   foreach my $unit (keys(%{$units})) {
2004     ($units->{$unit}->{"${prefix}base_unit"}, $units->{$unit}->{"${prefix}factor"}) = AM->get_base_unit($units, $unit);
2005   }
2006
2007   $dbh->disconnect();
2008
2009   $main::lxdebug->leave_sub();
2010
2011   return $units;
2012 }
2013
2014 sub units_in_use {
2015   $main::lxdebug->enter_sub();
2016
2017   my ($self, $myconfig, $form, $units) = @_;
2018
2019   my $dbh = $form->dbconnect($myconfig);
2020
2021   foreach my $unit (values(%{$units})) {
2022     my $base_unit = $unit->{"original_base_unit"};
2023     while ($base_unit) {
2024       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
2025       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
2026       $base_unit = $units->{$base_unit}->{"original_base_unit"};
2027     }
2028   }
2029
2030   foreach my $unit (values(%{$units})) {
2031     $unit->{"in_use"} = 0;
2032     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2033
2034     foreach my $table (qw(parts invoice orderitems)) {
2035       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2036
2037       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2038         $query .= "= " . $dbh->quote($unit->{"name"});
2039       } else {
2040         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2041       }
2042
2043       my ($count) = $dbh->selectrow_array($query);
2044       $form->dberror($query) if ($dbh->err);
2045
2046       if ($count) {
2047         $unit->{"in_use"} = 1;
2048         last;
2049       }
2050     }
2051   }
2052
2053   $dbh->disconnect();
2054
2055   $main::lxdebug->leave_sub();
2056 }
2057
2058 sub unit_select_data {
2059   $main::lxdebug->enter_sub();
2060
2061   my ($self, $units, $selected, $empty_entry) = @_;
2062
2063   my $select = [];
2064
2065   if ($empty_entry) {
2066     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2067   }
2068
2069   foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2070     push(@{$select}, { "name" => $unit,
2071                        "base_unit" => $units->{$unit}->{"base_unit"},
2072                        "factor" => $units->{$unit}->{"factor"},
2073                        "selected" => ($unit eq $selected) ? "selected" : "" });
2074   }
2075
2076   $main::lxdebug->leave_sub();
2077
2078   return $select;
2079 }
2080
2081 sub unit_select_html {
2082   $main::lxdebug->enter_sub();
2083
2084   my ($self, $units, $name, $selected, $convertible_into) = @_;
2085
2086   my $select = "<select name=${name}>";
2087
2088   foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2089     if (!$convertible_into ||
2090         ($units->{$convertible_into} &&
2091          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2092       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2093     }
2094   }
2095   $select .= "</select>";
2096
2097   $main::lxdebug->leave_sub();
2098
2099   return $select;
2100 }
2101
2102 sub add_unit {
2103   $main::lxdebug->enter_sub();
2104
2105   my ($self, $myconfig, $form, $name, $base_unit, $factor, $type) = @_;
2106
2107   my $dbh = $form->dbconnect($myconfig);
2108
2109   my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
2110   $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
2111   $dbh->disconnect();
2112
2113   $main::lxdebug->leave_sub();
2114 }
2115
2116 sub save_units {
2117   $main::lxdebug->enter_sub();
2118
2119   my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2120
2121   my $dbh = $form->dbconnect_noauto($myconfig);
2122
2123   my ($base_unit, $unit, $sth, $query);
2124
2125   if ($delete_units && (0 != scalar(@{$delete_units}))) {
2126     $query = "DELETE FROM units WHERE name = ?";
2127     $sth = $dbh->prepare($query);
2128     map({ $sth->execute($_) || $form->dberror($query . " ($_)"); } @{$delete_units});
2129     $sth->finish();
2130   }
2131
2132   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2133   $sth = $dbh->prepare($query);
2134
2135   foreach $unit (values(%{$units})) {
2136     $unit->{"depth"} = 0;
2137     my $base_unit = $unit;
2138     while ($base_unit->{"base_unit"}) {
2139       $unit->{"depth"}++;
2140       $base_unit = $units->{$base_unit->{"base_unit"}};
2141     }
2142   }
2143
2144   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2145     next if ($unit->{"unchanged_unit"});
2146
2147     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2148     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2149   }
2150
2151   $sth->finish();
2152   $dbh->commit();
2153   $dbh->disconnect();
2154
2155   $main::lxdebug->leave_sub();
2156 }
2157
2158 1;