Alu-Funktion aus Kundenprojekt entfernt.
[kivitendo-erp.git] / SL / AM.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # Administration module
32 #    Chart of Accounts
33 #    template routines
34 #    preferences
35 #
36 #======================================================================
37
38 package AM;
39
40 use Data::Dumper;
41
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   my $module = "IC";
1002   $query = qq|SELECT c.accno, c.description, c.link, c.id,
1003               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1004               FROM chart c, defaults d
1005               WHERE c.link LIKE '%$module%'
1006               ORDER BY c.accno|;
1007
1008
1009   my $sth = $dbh->prepare($query);
1010   $sth->execute || $form->dberror($query);
1011   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1012     foreach my $key (split /:/, $ref->{link}) {
1013       if ($key =~ /$module/) {
1014         if (   ($ref->{id} eq $ref->{inventory_accno_id})
1015             || ($ref->{id} eq $ref->{income_accno_id})
1016             || ($ref->{id} eq $ref->{expense_accno_id})) {
1017           push @{ $form->{"${module}_links"}{$key} },
1018             { accno       => $ref->{accno},
1019               description => $ref->{description},
1020               selected    => "selected",
1021               id          => $ref->{id} };
1022             } else {
1023           push @{ $form->{"${module}_links"}{$key} },
1024             { accno       => $ref->{accno},
1025               description => $ref->{description},
1026               selected    => "",
1027               id          => $ref->{id} };
1028         }
1029       }
1030     }
1031   }
1032   $sth->finish;
1033
1034
1035   $dbh->disconnect;
1036
1037   $main::lxdebug->leave_sub();
1038 }
1039
1040 sub save_buchungsgruppe {
1041   $main::lxdebug->enter_sub();
1042
1043   my ($self, $myconfig, $form) = @_;
1044
1045   # connect to database
1046   my $dbh = $form->dbconnect($myconfig);
1047
1048   $form->{description} =~ s/\'/\'\'/g;
1049
1050
1051   # id is the old record
1052   if ($form->{id}) {
1053     $query = qq|UPDATE buchungsgruppen SET
1054                 description = '$form->{description}',
1055                 inventory_accno_id = '$form->{inventory_accno_id}',
1056                 income_accno_id_0 = '$form->{income_accno_id_0}',
1057                 expense_accno_id_0 = '$form->{expense_accno_id_0}',
1058                 income_accno_id_1 = '$form->{income_accno_id_1}',
1059                 expense_accno_id_1 = '$form->{expense_accno_id_1}',
1060                 income_accno_id_2 = '$form->{income_accno_id_2}',
1061                 expense_accno_id_2 = '$form->{expense_accno_id_2}',
1062                 income_accno_id_3 = '$form->{income_accno_id_3}',
1063                 expense_accno_id_3 = '$form->{expense_accno_id_3}'
1064                 WHERE id = $form->{id}|;
1065   } else {
1066     $query = qq|INSERT INTO buchungsgruppen
1067                 (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)
1068                 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}')|;
1069   }
1070   $dbh->do($query) || $form->dberror($query);
1071
1072   $dbh->disconnect;
1073
1074   $main::lxdebug->leave_sub();
1075 }
1076
1077 sub delete_buchungsgruppe {
1078   $main::lxdebug->enter_sub();
1079
1080   my ($self, $myconfig, $form) = @_;
1081
1082   # connect to database
1083   my $dbh = $form->dbconnect($myconfig);
1084
1085   $query = qq|DELETE FROM buchungsgruppe
1086               WHERE id = $form->{id}|;
1087   $dbh->do($query) || $form->dberror($query);
1088
1089   $dbh->disconnect;
1090
1091   $main::lxdebug->leave_sub();
1092 }
1093
1094 sub printer {
1095   $main::lxdebug->enter_sub();
1096
1097   my ($self, $myconfig, $form) = @_;
1098
1099   # connect to database
1100   my $dbh = $form->dbconnect($myconfig);
1101
1102   my $query = qq|SELECT id, printer_description, template_code, printer_command
1103                  FROM printers
1104                  ORDER BY 2|;
1105
1106   $sth = $dbh->prepare($query);
1107   $sth->execute || $form->dberror($query);
1108
1109   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1110     push @{ $form->{ALL} }, $ref;
1111   }
1112
1113   $sth->finish;
1114   $dbh->disconnect;
1115
1116   $main::lxdebug->leave_sub();
1117 }
1118
1119 sub get_printer {
1120   $main::lxdebug->enter_sub();
1121
1122   my ($self, $myconfig, $form) = @_;
1123
1124   # connect to database
1125   my $dbh = $form->dbconnect($myconfig);
1126
1127   my $query =
1128     qq|SELECT p.printer_description, p.template_code, p.printer_command
1129                  FROM printers p
1130                  WHERE p.id = $form->{id}|;
1131   my $sth = $dbh->prepare($query);
1132   $sth->execute || $form->dberror($query);
1133
1134   my $ref = $sth->fetchrow_hashref(NAME_lc);
1135
1136   map { $form->{$_} = $ref->{$_} } keys %$ref;
1137
1138   $sth->finish;
1139
1140   $dbh->disconnect;
1141
1142   $main::lxdebug->leave_sub();
1143 }
1144
1145 sub save_printer {
1146   $main::lxdebug->enter_sub();
1147
1148   my ($self, $myconfig, $form) = @_;
1149
1150   # connect to database
1151   my $dbh = $form->dbconnect($myconfig);
1152
1153   $form->{printer_description} =~ s/\'/\'\'/g;
1154   $form->{printer_command} =~ s/\'/\'\'/g;
1155   $form->{template_code} =~ s/\'/\'\'/g;
1156
1157
1158   # id is the old record
1159   if ($form->{id}) {
1160     $query = qq|UPDATE printers SET
1161                 printer_description = '$form->{printer_description}',
1162                 template_code = '$form->{template_code}',
1163                 printer_command = '$form->{printer_command}'
1164                 WHERE id = $form->{id}|;
1165   } else {
1166     $query = qq|INSERT INTO printers
1167                 (printer_description, template_code, printer_command)
1168                 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1169   }
1170   $dbh->do($query) || $form->dberror($query);
1171
1172   $dbh->disconnect;
1173
1174   $main::lxdebug->leave_sub();
1175 }
1176
1177 sub delete_printer {
1178   $main::lxdebug->enter_sub();
1179
1180   my ($self, $myconfig, $form) = @_;
1181
1182   # connect to database
1183   my $dbh = $form->dbconnect($myconfig);
1184
1185   $query = qq|DELETE FROM printers
1186               WHERE id = $form->{id}|;
1187   $dbh->do($query) || $form->dberror($query);
1188
1189   $dbh->disconnect;
1190
1191   $main::lxdebug->leave_sub();
1192 }
1193
1194 sub payment {
1195   $main::lxdebug->enter_sub();
1196
1197   my ($self, $myconfig, $form) = @_;
1198
1199   # connect to database
1200   my $dbh = $form->dbconnect($myconfig);
1201
1202   my $query = qq|SELECT *
1203                  FROM payment_terms
1204                  ORDER BY id|;
1205
1206   $sth = $dbh->prepare($query);
1207   $sth->execute || $form->dberror($query);
1208
1209   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {   
1210     $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1211     push @{ $form->{ALL} }, $ref;
1212   }
1213
1214   $sth->finish;
1215   $dbh->disconnect;
1216
1217   $main::lxdebug->leave_sub();
1218 }
1219
1220 sub get_payment {
1221   $main::lxdebug->enter_sub();
1222
1223   my ($self, $myconfig, $form) = @_;
1224
1225   # connect to database
1226   my $dbh = $form->dbconnect($myconfig);
1227
1228   my $query =
1229     qq|SELECT *
1230                  FROM payment_terms
1231                  WHERE id = $form->{id}|;
1232   my $sth = $dbh->prepare($query);
1233   $sth->execute || $form->dberror($query);
1234
1235   my $ref = $sth->fetchrow_hashref(NAME_lc);
1236   $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1237
1238   map { $form->{$_} = $ref->{$_} } keys %$ref;
1239
1240   $sth->finish;
1241
1242   $dbh->disconnect;
1243
1244   $main::lxdebug->leave_sub();
1245 }
1246
1247 sub save_payment {
1248   $main::lxdebug->enter_sub();
1249
1250   my ($self, $myconfig, $form) = @_;
1251
1252   # connect to database
1253   my $dbh = $form->dbconnect($myconfig);
1254
1255   $form->{description} =~ s/\'/\'\'/g;
1256   $form->{description_long} =~ s/\'/\'\'/g;
1257   $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100;
1258   $form->{ranking} *= 1;
1259   $form->{terms_netto} *= 1;
1260   $form->{terms_skonto} *= 1;
1261   $form->{percent_skonto} *= 1;
1262
1263
1264
1265   # id is the old record
1266   if ($form->{id}) {
1267     $query = qq|UPDATE payment_terms SET
1268                 description = '$form->{description}',
1269                 ranking = $form->{ranking},
1270                 description_long = '$form->{description_long}',
1271                 terms_netto = $form->{terms_netto},
1272                 terms_skonto = $form->{terms_skonto},
1273                 percent_skonto = $percentskonto
1274                 WHERE id = $form->{id}|;
1275   } else {
1276     $query = qq|INSERT INTO payment_terms
1277                 (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto)
1278                 VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|;
1279   }
1280   $dbh->do($query) || $form->dberror($query);
1281
1282   $dbh->disconnect;
1283
1284   $main::lxdebug->leave_sub();
1285 }
1286
1287 sub delete_payment {
1288   $main::lxdebug->enter_sub();
1289
1290   my ($self, $myconfig, $form) = @_;
1291
1292   # connect to database
1293   my $dbh = $form->dbconnect($myconfig);
1294
1295   $query = qq|DELETE FROM payment_terms
1296               WHERE id = $form->{id}|;
1297   $dbh->do($query) || $form->dberror($query);
1298
1299   $dbh->disconnect;
1300
1301   $main::lxdebug->leave_sub();
1302 }
1303
1304 sub sic {
1305   $main::lxdebug->enter_sub();
1306
1307   my ($self, $myconfig, $form) = @_;
1308
1309   # connect to database
1310   my $dbh = $form->dbconnect($myconfig);
1311
1312   my $query = qq|SELECT code, sictype, description
1313                  FROM sic
1314                  ORDER BY code|;
1315
1316   $sth = $dbh->prepare($query);
1317   $sth->execute || $form->dberror($query);
1318
1319   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1320     push @{ $form->{ALL} }, $ref;
1321   }
1322
1323   $sth->finish;
1324   $dbh->disconnect;
1325
1326   $main::lxdebug->leave_sub();
1327 }
1328
1329 sub get_sic {
1330   $main::lxdebug->enter_sub();
1331
1332   my ($self, $myconfig, $form) = @_;
1333
1334   # connect to database
1335   my $dbh = $form->dbconnect($myconfig);
1336
1337   my $query = qq|SELECT s.code, s.sictype, s.description
1338                  FROM sic s
1339                  WHERE s.code = '$form->{code}'|;
1340   my $sth = $dbh->prepare($query);
1341   $sth->execute || $form->dberror($query);
1342
1343   my $ref = $sth->fetchrow_hashref(NAME_lc);
1344
1345   map { $form->{$_} = $ref->{$_} } keys %$ref;
1346
1347   $sth->finish;
1348
1349   $dbh->disconnect;
1350
1351   $main::lxdebug->leave_sub();
1352 }
1353
1354 sub save_sic {
1355   $main::lxdebug->enter_sub();
1356
1357   my ($self, $myconfig, $form) = @_;
1358
1359   # connect to database
1360   my $dbh = $form->dbconnect($myconfig);
1361
1362   $form->{code}        =~ s/\'/\'\'/g;
1363   $form->{description} =~ s/\'/\'\'/g;
1364
1365   # if there is an id
1366   if ($form->{id}) {
1367     $query = qq|UPDATE sic SET
1368                 code = '$form->{code}',
1369                 sictype = '$form->{sictype}',
1370                 description = '$form->{description}'
1371                 WHERE code = '$form->{id}'|;
1372   } else {
1373     $query = qq|INSERT INTO sic
1374                 (code, sictype, description)
1375                 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1376   }
1377   $dbh->do($query) || $form->dberror($query);
1378
1379   $dbh->disconnect;
1380
1381   $main::lxdebug->leave_sub();
1382 }
1383
1384 sub delete_sic {
1385   $main::lxdebug->enter_sub();
1386
1387   my ($self, $myconfig, $form) = @_;
1388
1389   # connect to database
1390   my $dbh = $form->dbconnect($myconfig);
1391
1392   $query = qq|DELETE FROM sic
1393               WHERE code = '$form->{code}'|;
1394   $dbh->do($query) || $form->dberror($query);
1395
1396   $dbh->disconnect;
1397
1398   $main::lxdebug->leave_sub();
1399 }
1400
1401 sub load_template {
1402   $main::lxdebug->enter_sub();
1403
1404   my ($self, $form) = @_;
1405
1406   open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1407
1408   while (<TEMPLATE>) {
1409     $form->{body} .= $_;
1410   }
1411
1412   close(TEMPLATE);
1413
1414   $main::lxdebug->leave_sub();
1415 }
1416
1417 sub save_template {
1418   $main::lxdebug->enter_sub();
1419
1420   my ($self, $form) = @_;
1421
1422   open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1423
1424   # strip
1425   $form->{body} =~ s/\r\n/\n/g;
1426   print TEMPLATE $form->{body};
1427
1428   close(TEMPLATE);
1429
1430   $main::lxdebug->leave_sub();
1431 }
1432
1433 sub save_preferences {
1434   $main::lxdebug->enter_sub();
1435
1436   my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1437
1438   map { ($form->{$_}) = split /--/, $form->{$_} }
1439     qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1440
1441   my @a;
1442   $form->{curr} =~ s/ //g;
1443   map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
1444   $form->{curr} = join ':', @a;
1445
1446   # connect to database
1447   my $dbh = $form->dbconnect_noauto($myconfig);
1448
1449   # these defaults are database wide
1450   # user specific variables are in myconfig
1451   # save defaults
1452   my $query = qq|UPDATE defaults SET
1453                  inventory_accno_id =
1454                      (SELECT c.id FROM chart c
1455                                 WHERE c.accno = '$form->{inventory_accno}'),
1456                  income_accno_id =
1457                      (SELECT c.id FROM chart c
1458                                 WHERE c.accno = '$form->{income_accno}'),
1459                  expense_accno_id =
1460                      (SELECT c.id FROM chart c
1461                                 WHERE c.accno = '$form->{expense_accno}'),
1462                  fxgain_accno_id =
1463                      (SELECT c.id FROM chart c
1464                                 WHERE c.accno = '$form->{fxgain_accno}'),
1465                  fxloss_accno_id =
1466                      (SELECT c.id FROM chart c
1467                                 WHERE c.accno = '$form->{fxloss_accno}'),
1468                  invnumber = '$form->{invnumber}',
1469                  cnnumber  = '$form->{cnnumber}',
1470                  sonumber = '$form->{sonumber}',
1471                  ponumber = '$form->{ponumber}',
1472                  sqnumber = '$form->{sqnumber}',
1473                  rfqnumber = '$form->{rfqnumber}',
1474                  customernumber = '$form->{customernumber}',
1475                  vendornumber = '$form->{vendornumber}',
1476                  articlenumber = '$form->{articlenumber}',
1477                  servicenumber = '$form->{servicenumber}',
1478                  yearend = '$form->{yearend}',
1479                  curr = '$form->{curr}',
1480                  weightunit = '$form->{weightunit}',
1481                  businessnumber = '$form->{businessnumber}'
1482                 |;
1483   $dbh->do($query) || $form->dberror($query);
1484
1485   # update name
1486   my $name = $form->{name};
1487   $name =~ s/\'/\'\'/g;
1488   $query = qq|UPDATE employee
1489               SET name = '$name'
1490               WHERE login = '$form->{login}'|;
1491   $dbh->do($query) || $form->dberror($query);
1492
1493   foreach my $item (split / /, $form->{taxaccounts}) {
1494     $query = qq|UPDATE tax
1495                 SET rate = | . ($form->{$item} / 100) . qq|,
1496                 taxnumber = '$form->{"taxnumber_$item"}'
1497                 WHERE chart_id = $item|;
1498     $dbh->do($query) || $form->dberror($query);
1499   }
1500
1501   my $rc = $dbh->commit;
1502   $dbh->disconnect;
1503
1504   # save first currency in myconfig
1505   $form->{currency} = substr($form->{curr}, 0, 3);
1506
1507   my $myconfig = new User "$memberfile", "$form->{login}";
1508
1509   foreach my $item (keys %$form) {
1510     $myconfig->{$item} = $form->{$item};
1511   }
1512
1513   $myconfig->save_member($memberfile, $userspath);
1514
1515   if ($webdav) {
1516     @webdavdirs =
1517       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1518     foreach $directory (@webdavdirs) {
1519       $file = "webdav/" . $directory . "/webdav-user";
1520       if ($myconfig->{$directory}) {
1521         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1522         while (<HTACCESS>) {
1523           ($login, $password) = split(/:/, $_);
1524           if ($login ne $form->{login}) {
1525             $newfile .= $_;
1526           }
1527         }
1528         close(HTACCESS);
1529         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1530         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1531         print(HTACCESS $newfile);
1532         close(HTACCESS);
1533       } else {
1534         $form->{$directory} = 0;
1535         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1536         while (<HTACCESS>) {
1537           ($login, $password) = split(/:/, $_);
1538           if ($login ne $form->{login}) {
1539             $newfile .= $_;
1540           }
1541         }
1542         close(HTACCESS);
1543         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1544         print(HTACCESS $newfile);
1545         close(HTACCESS);
1546       }
1547     }
1548   }
1549
1550   $main::lxdebug->leave_sub();
1551
1552   return $rc;
1553 }
1554
1555 sub defaultaccounts {
1556   $main::lxdebug->enter_sub();
1557
1558   my ($self, $myconfig, $form) = @_;
1559
1560   # connect to database
1561   my $dbh = $form->dbconnect($myconfig);
1562
1563   # get defaults from defaults table
1564   my $query = qq|SELECT * FROM defaults|;
1565   my $sth   = $dbh->prepare($query);
1566   $sth->execute || $form->dberror($query);
1567
1568   $form->{defaults}             = $sth->fetchrow_hashref(NAME_lc);
1569   $form->{defaults}{IC}         = $form->{defaults}{inventory_accno_id};
1570   $form->{defaults}{IC_income}  = $form->{defaults}{income_accno_id};
1571   $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1572   $form->{defaults}{FX_gain}    = $form->{defaults}{fxgain_accno_id};
1573   $form->{defaults}{FX_loss}    = $form->{defaults}{fxloss_accno_id};
1574
1575   $sth->finish;
1576
1577   $query = qq|SELECT c.id, c.accno, c.description, c.link
1578               FROM chart c
1579               WHERE c.link LIKE '%IC%'
1580               ORDER BY c.accno|;
1581   $sth = $dbh->prepare($query);
1582   $sth->execute || $self->dberror($query);
1583
1584   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1585     foreach my $key (split(/:/, $ref->{link})) {
1586       if ($key =~ /IC/) {
1587         $nkey = $key;
1588         if ($key =~ /cogs/) {
1589           $nkey = "IC_expense";
1590         }
1591         if ($key =~ /sale/) {
1592           $nkey = "IC_income";
1593         }
1594         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1595                                              id          => $ref->{id},
1596                                              description => $ref->{description}
1597         );
1598       }
1599     }
1600   }
1601   $sth->finish;
1602
1603   $query = qq|SELECT c.id, c.accno, c.description
1604               FROM chart c
1605               WHERE c.category = 'I'
1606               AND c.charttype = 'A'
1607               ORDER BY c.accno|;
1608   $sth = $dbh->prepare($query);
1609   $sth->execute || $self->dberror($query);
1610
1611   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1612     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1613                                              id          => $ref->{id},
1614                                              description => $ref->{description}
1615     );
1616   }
1617   $sth->finish;
1618
1619   $query = qq|SELECT c.id, c.accno, c.description
1620               FROM chart c
1621               WHERE c.category = 'E'
1622               AND c.charttype = 'A'
1623               ORDER BY c.accno|;
1624   $sth = $dbh->prepare($query);
1625   $sth->execute || $self->dberror($query);
1626
1627   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1628     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1629                                              id          => $ref->{id},
1630                                              description => $ref->{description}
1631     );
1632   }
1633   $sth->finish;
1634
1635   # now get the tax rates and numbers
1636   $query = qq|SELECT c.id, c.accno, c.description,
1637               t.rate * 100 AS rate, t.taxnumber
1638               FROM chart c, tax t
1639               WHERE c.id = t.chart_id|;
1640
1641   $sth = $dbh->prepare($query);
1642   $sth->execute || $form->dberror($query);
1643
1644   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1645     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
1646     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1647     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
1648       if $ref->{taxnumber};
1649     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1650   }
1651
1652   $sth->finish;
1653   $dbh->disconnect;
1654
1655   $main::lxdebug->leave_sub();
1656 }
1657
1658 sub backup {
1659   $main::lxdebug->enter_sub();
1660
1661   my ($self, $myconfig, $form, $userspath) = @_;
1662
1663   my $mail;
1664   my $err;
1665   my $boundary = time;
1666   my $tmpfile  =
1667     "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1668   my $out = $form->{OUT};
1669   $form->{OUT} = ">$tmpfile";
1670
1671   if ($form->{media} eq 'email') {
1672
1673     use SL::Mailer;
1674     $mail = new Mailer;
1675
1676     $mail->{to}      = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1677     $mail->{from}    = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1678     $mail->{subject} =
1679       "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1680     @{ $mail->{attachments} } = ($tmpfile);
1681     $mail->{version} = $form->{version};
1682     $mail->{fileid}  = "$boundary.";
1683
1684     $myconfig->{signature} =~ s/\\n/\r\n/g;
1685     $mail->{message} = "--\n$myconfig->{signature}";
1686
1687   }
1688
1689   open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1690
1691   # get sequences, functions and triggers
1692   open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1693
1694   my @sequences = ();
1695   my @functions = ();
1696   my @triggers  = ();
1697   my @indices   = ();
1698   my %tablespecs;
1699
1700   my $query = "";
1701   my @quote_chars;
1702
1703   while (<FH>) {
1704
1705     # Remove DOS and Unix style line endings.
1706     s/[\r\n]//g;
1707
1708     # ignore comments or empty lines
1709     next if /^(--.*|\s+)$/;
1710
1711     for (my $i = 0; $i < length($_); $i++) {
1712       my $char = substr($_, $i, 1);
1713
1714       # Are we inside a string?
1715       if (@quote_chars) {
1716         if ($char eq $quote_chars[-1]) {
1717           pop(@quote_chars);
1718         }
1719         $query .= $char;
1720
1721       } else {
1722         if (($char eq "'") || ($char eq "\"")) {
1723           push(@quote_chars, $char);
1724
1725         } elsif ($char eq ";") {
1726
1727           # Query is complete. Check for triggers and functions.
1728           if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1729             push(@functions, $query);
1730
1731           } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1732             push(@triggers, $query);
1733
1734           } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1735             push(@sequences, $1);
1736
1737           } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1738             $tablespecs{$1} = $query;
1739
1740           } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1741             push(@indices, $query);
1742
1743           }
1744
1745           $query = "";
1746           $char  = "";
1747         }
1748
1749         $query .= $char;
1750       }
1751     }
1752   }
1753   close(FH);
1754
1755   # connect to database
1756   my $dbh = $form->dbconnect($myconfig);
1757
1758   # get all the tables
1759   my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1760
1761   my $today = scalar localtime;
1762
1763   $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1764
1765   print OUT qq|-- Lx-Office Backup
1766 -- Dataset: $myconfig->{dbname}
1767 -- Version: $form->{dbversion}
1768 -- Host: $myconfig->{dbhost}
1769 -- Login: $form->{login}
1770 -- User: $myconfig->{name}
1771 -- Date: $today
1772 --
1773 -- set options
1774 $myconfig->{dboptions};
1775 --
1776 |;
1777
1778   print OUT "-- DROP Sequences\n";
1779   my $item;
1780   foreach $item (@sequences) {
1781     print OUT qq|DROP SEQUENCE $item;\n|;
1782   }
1783
1784   print OUT "-- DROP Triggers\n";
1785
1786   foreach $item (@triggers) {
1787     if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1788       print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1789     }
1790   }
1791
1792   print OUT "-- DROP Functions\n";
1793
1794   foreach $item (@functions) {
1795     if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1796       print OUT qq|DROP FUNCTION "$1" ();\n|;
1797     }
1798   }
1799
1800   foreach $table (@tables) {
1801     if (!($table =~ /^sql_.*/)) {
1802       my $query = qq|SELECT * FROM $table|;
1803
1804       my $sth = $dbh->prepare($query);
1805       $sth->execute || $form->dberror($query);
1806
1807       $query = "INSERT INTO $table (";
1808       map { $query .= qq|$sth->{NAME}->[$_],| }
1809         (0 .. $sth->{NUM_OF_FIELDS} - 1);
1810       chop $query;
1811
1812       $query .= ") VALUES";
1813
1814       if ($tablespecs{$table}) {
1815         print(OUT "--\n");
1816         print(OUT "DROP TABLE $table;\n");
1817         print(OUT $tablespecs{$table}, ";\n");
1818       } else {
1819         print(OUT "--\n");
1820         print(OUT "DELETE FROM $table;\n");
1821       }
1822       while (my @arr = $sth->fetchrow_array) {
1823
1824         $fields = "(";
1825         foreach my $item (@arr) {
1826           if (defined $item) {
1827             $item =~ s/\'/\'\'/g;
1828             $fields .= qq|'$item',|;
1829           } else {
1830             $fields .= 'NULL,';
1831           }
1832         }
1833
1834         chop $fields;
1835         $fields .= ")";
1836
1837         print OUT qq|$query $fields;\n|;
1838       }
1839
1840       $sth->finish;
1841     }
1842   }
1843
1844   # create indices, sequences, functions and triggers
1845
1846   print(OUT "-- CREATE Indices\n");
1847   map({ print(OUT "$_;\n"); } @indices);
1848
1849   print OUT "-- CREATE Sequences\n";
1850   foreach $item (@sequences) {
1851     $query = qq|SELECT last_value FROM $item|;
1852     $sth   = $dbh->prepare($query);
1853     $sth->execute || $form->dberror($query);
1854     my ($id) = $sth->fetchrow_array;
1855     $sth->finish;
1856
1857     print OUT qq|--
1858 CREATE SEQUENCE $item START $id;
1859 |;
1860   }
1861
1862   print OUT "-- CREATE Functions\n";
1863
1864   # functions
1865   map { print(OUT $_, ";\n"); } @functions;
1866
1867   print OUT "-- CREATE Triggers\n";
1868
1869   # triggers
1870   map { print(OUT $_, ";\n"); } @triggers;
1871
1872   close(OUT);
1873
1874   $dbh->disconnect;
1875
1876   # compress backup
1877   my @args = ("gzip", "$tmpfile");
1878   system(@args) == 0 or $form->error("$args[0] : $?");
1879
1880   $tmpfile .= ".gz";
1881
1882   if ($form->{media} eq 'email') {
1883     @{ $mail->{attachments} } = ($tmpfile);
1884     $err = $mail->send($out);
1885   }
1886
1887   if ($form->{media} eq 'file') {
1888
1889     open(IN,  "$tmpfile") or $form->error("$tmpfile : $!");
1890     open(OUT, ">-")       or $form->error("STDOUT : $!");
1891
1892     print OUT qq|Content-Type: application/x-tar-gzip;
1893 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1894
1895 |;
1896
1897     while (<IN>) {
1898       print OUT $_;
1899     }
1900
1901     close(IN);
1902     close(OUT);
1903
1904   }
1905
1906   unlink "$tmpfile";
1907
1908   $main::lxdebug->leave_sub();
1909 }
1910
1911 sub closedto {
1912   $main::lxdebug->enter_sub();
1913
1914   my ($self, $myconfig, $form) = @_;
1915
1916   my $dbh = $form->dbconnect($myconfig);
1917
1918   my $query = qq|SELECT closedto, revtrans FROM defaults|;
1919   my $sth   = $dbh->prepare($query);
1920   $sth->execute || $form->dberror($query);
1921
1922   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1923
1924   $sth->finish;
1925
1926   $dbh->disconnect;
1927
1928   $main::lxdebug->leave_sub();
1929 }
1930
1931 sub closebooks {
1932   $main::lxdebug->enter_sub();
1933
1934   my ($self, $myconfig, $form) = @_;
1935
1936   my $dbh = $form->dbconnect($myconfig);
1937
1938   if ($form->{revtrans}) {
1939
1940     $query = qq|UPDATE defaults SET closedto = NULL,
1941                                     revtrans = '1'|;
1942   } else {
1943     if ($form->{closedto}) {
1944
1945       $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1946                                       revtrans = '0'|;
1947     } else {
1948
1949       $query = qq|UPDATE defaults SET closedto = NULL,
1950                                       revtrans = '0'|;
1951     }
1952   }
1953
1954   # set close in defaults
1955   $dbh->do($query) || $form->dberror($query);
1956
1957   $dbh->disconnect;
1958
1959   $main::lxdebug->leave_sub();
1960 }
1961
1962 sub get_base_unit {
1963   my ($self, $units, $unit_name, $factor) = @_;
1964
1965   $factor = 1 unless ($factor);
1966
1967   my $unit = $units->{$unit_name};
1968
1969   if (!defined($unit) || !$unit->{"base_unit"} ||
1970       ($unit_name eq $unit->{"base_unit"})) {
1971     return ($unit_name, $factor);
1972   }
1973
1974   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1975 }
1976
1977 sub retrieve_units {
1978   $main::lxdebug->enter_sub();
1979
1980   my ($self, $myconfig, $form, $type, $prefix) = @_;
1981
1982   my $dbh = $form->dbconnect($myconfig);
1983
1984   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1985   my @values;
1986   if ($type) {
1987     $query .= " WHERE (type = ?)";
1988     @values = ($type);
1989   }
1990
1991   my $sth = $dbh->prepare($query);
1992   $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1993
1994   my $units = {};
1995   while (my $ref = $sth->fetchrow_hashref()) {
1996     $units->{$ref->{"name"}} = $ref;
1997   }
1998   $sth->finish();
1999
2000   foreach my $unit (keys(%{$units})) {
2001     ($units->{$unit}->{"${prefix}base_unit"}, $units->{$unit}->{"${prefix}factor"}) = AM->get_base_unit($units, $unit);
2002   }
2003
2004   $dbh->disconnect();
2005
2006   $main::lxdebug->leave_sub();
2007
2008   return $units;
2009 }
2010
2011 sub units_in_use {
2012   $main::lxdebug->enter_sub();
2013
2014   my ($self, $myconfig, $form, $units) = @_;
2015
2016   my $dbh = $form->dbconnect($myconfig);
2017
2018   foreach my $unit (values(%{$units})) {
2019     my $base_unit = $unit->{"original_base_unit"};
2020     while ($base_unit) {
2021       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
2022       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
2023       $base_unit = $units->{$base_unit}->{"original_base_unit"};
2024     }
2025   }
2026
2027   foreach my $unit (values(%{$units})) {
2028     $unit->{"in_use"} = 0;
2029     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2030
2031     foreach my $table (qw(parts invoice orderitems)) {
2032       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2033
2034       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2035         $query .= "= " . $dbh->quote($unit->{"name"});
2036       } else {
2037         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2038       }
2039
2040       my ($count) = $dbh->selectrow_array($query);
2041       $form->dberror($query) if ($dbh->err);
2042
2043       if ($count) {
2044         $unit->{"in_use"} = 1;
2045         last;
2046       }
2047     }
2048   }
2049
2050   $dbh->disconnect();
2051
2052   $main::lxdebug->leave_sub();
2053 }
2054
2055 sub unit_select_data {
2056   $main::lxdebug->enter_sub();
2057
2058   my ($self, $units, $selected, $empty_entry) = @_;
2059
2060   my $select = [];
2061
2062   if ($empty_entry) {
2063     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2064   }
2065
2066   foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2067     push(@{$select}, { "name" => $unit,
2068                        "base_unit" => $units->{$unit}->{"base_unit"},
2069                        "factor" => $units->{$unit}->{"factor"},
2070                        "selected" => ($unit eq $selected) ? "selected" : "" });
2071   }
2072
2073   $main::lxdebug->leave_sub();
2074
2075   return $select;
2076 }
2077
2078 sub unit_select_html {
2079   $main::lxdebug->enter_sub();
2080
2081   my ($self, $units, $name, $selected, $convertible_into) = @_;
2082
2083   my $select = "<select name=${name}>";
2084
2085   foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2086     if (!$convertible_into ||
2087         ($units->{$convertible_into} &&
2088          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2089       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2090     }
2091   }
2092   $select .= "</select>";
2093
2094   $main::lxdebug->leave_sub();
2095
2096   return $select;
2097 }
2098
2099 sub add_unit {
2100   $main::lxdebug->enter_sub();
2101
2102   my ($self, $myconfig, $form, $name, $base_unit, $factor, $type) = @_;
2103
2104   my $dbh = $form->dbconnect($myconfig);
2105
2106   my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
2107   $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
2108   $dbh->disconnect();
2109
2110   $main::lxdebug->leave_sub();
2111 }
2112
2113 sub save_units {
2114   $main::lxdebug->enter_sub();
2115
2116   my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2117
2118   my $dbh = $form->dbconnect_noauto($myconfig);
2119
2120   my ($base_unit, $unit, $sth, $query);
2121
2122   if ($delete_units && (0 != scalar(@{$delete_units}))) {
2123     $query = "DELETE FROM units WHERE name = ?";
2124     $sth = $dbh->prepare($query);
2125     map({ $sth->execute($_) || $form->dberror($query . " ($_)"); } @{$delete_units});
2126     $sth->finish();
2127   }
2128
2129   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2130   $sth = $dbh->prepare($query);
2131
2132   foreach $unit (values(%{$units})) {
2133     $unit->{"depth"} = 0;
2134     my $base_unit = $unit;
2135     while ($base_unit->{"base_unit"}) {
2136       $unit->{"depth"}++;
2137       $base_unit = $units->{$base_unit->{"base_unit"}};
2138     }
2139   }
2140
2141   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2142     next if ($unit->{"unchanged_unit"});
2143
2144     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2145     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2146   }
2147
2148   $sth->finish();
2149   $dbh->commit();
2150   $dbh->disconnect();
2151
2152   $main::lxdebug->leave_sub();
2153 }
2154
2155 1;