1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
31 # Administration module
36 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($self, $myconfig, $form) = @_;
47 $form->{id} = "NULL" unless ($form->{id});
50 my $dbh = $form->dbconnect($myconfig);
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
55 WHERE c.id = $form->{id}|;
57 my $sth = $dbh->prepare($query);
58 $sth->execute || $form->dberror($query);
60 my $ref = $sth->fetchrow_hashref(NAME_lc);
62 foreach my $key (keys %$ref) {
63 $form->{"$key"} = $ref->{"$key"};
68 # get default accounts
69 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
71 $sth = $dbh->prepare($query);
72 $sth->execute || $form->dberror($query);
74 $ref = $sth->fetchrow_hashref(NAME_lc);
76 map { $form->{$_} = $ref->{$_} } keys %ref;
80 # get taxkeys and description
81 $query = qq|SELECT taxkey, taxdescription
83 $sth = $dbh->prepare($query);
84 $sth->execute || $form->dberror($query);
86 $ref = $sth->fetchrow_hashref(NAME_lc);
88 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
89 push @{ $form->{TAXKEY} }, $ref;
95 $where = " WHERE link='$form->{link}'";
99 $query = qq|SELECT id, accno,description
101 $sth = $dbh->prepare($query);
102 $sth->execute || $form->dberror($query);
104 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
105 push @{ $form->{NEWACCOUNT} }, $ref;
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);
116 ($form->{orphaned}) = $sth->fetchrow_array;
117 $form->{orphaned} = !$form->{orphaned};
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);
128 my ($count) = $sth->fetchrow_array;
130 $form->{new_chart_valid} = 1;
137 $main::lxdebug->leave_sub();
141 $main::lxdebug->enter_sub();
143 my ($self, $myconfig, $form) = @_;
145 # connect to database, turn off AutoCommit
146 my $dbh = $form->dbconnect_noauto($myconfig);
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);
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}
164 $form->{link} .= "${item}:" if ($item);
168 # if we have an id then replace the old record
169 $form->{description} =~ s/\'/\'\'/g;
171 # strip blanks from accno
172 map { $form->{$_} =~ s/ //g; } qw(accno);
176 if ($form->{id} eq "NULL") {
180 map({ $form->{$_} = "NULL" unless ($form->{$_}); }
181 qw(pos_ustva pos_bwa pos_bilanz pos_eur new_chart_id));
183 $form->{valid_from} = ($form->{valid_from}) ? "'$form->{valid_from}'" : "NULL";
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}|;
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})|;
214 $dbh->do($query) || $form->dberror($query);
216 if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) {
218 my $chart_id = $form->{id};
220 unless ($form->{id}) {
223 $query = qq|SELECT c.id
225 WHERE c.accno = '$form->{accno}'|;
226 $sth = $dbh->prepare($query);
227 $sth->execute || $form->dberror($query);
229 ($chart_id) = $sth->fetchrow_array;
233 # add account if it doesn't exist in tax
234 $query = qq|SELECT t.chart_id
236 WHERE t.chart_id = $chart_id|;
237 $sth = $dbh->prepare($query);
238 $sth->execute || $form->dberror($query);
240 my ($tax_id) = $sth->fetchrow_array;
243 # add tax if it doesn't exist
245 $query = qq|INSERT INTO tax (chart_id, rate)
246 VALUES ($chart_id, 0)|;
247 $dbh->do($query) || $form->dberror($query);
253 $query = qq|DELETE FROM tax
254 WHERE chart_id = $form->{id}|;
255 $dbh->do($query) || $form->dberror($query);
260 my $rc = $dbh->commit;
263 $main::lxdebug->leave_sub();
269 $main::lxdebug->enter_sub();
271 my ($self, $myconfig, $form) = @_;
273 # connect to database, turn off AutoCommit
274 my $dbh = $form->dbconnect_noauto($myconfig);
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);
281 if ($sth->fetchrow_array) {
284 $main::lxdebug->leave_sub();
289 # delete chart of account record
290 $query = qq|DELETE FROM chart
291 WHERE id = $form->{id}|;
292 $dbh->do($query) || $form->dberror($query);
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);
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);
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);
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);
319 # commit and redirect
320 my $rc = $dbh->commit;
323 $main::lxdebug->leave_sub();
329 $main::lxdebug->enter_sub();
331 my ($self, $myconfig, $form) = @_;
333 # connect to database
334 my $dbh = $form->dbconnect($myconfig);
336 my $query = qq|SELECT accno, description
340 $sth = $dbh->prepare($query);
341 $sth->execute || $form->dberror($query);
343 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
344 push @{ $form->{ALL} }, $ref;
350 $main::lxdebug->leave_sub();
354 $main::lxdebug->enter_sub();
356 my ($self, $myconfig, $form) = @_;
358 # connect to database
359 my $dbh = $form->dbconnect($myconfig);
361 my $query = qq|SELECT g.accno, g.description
363 WHERE g.accno = '$form->{accno}'|;
364 my $sth = $dbh->prepare($query);
365 $sth->execute || $form->dberror($query);
367 my $ref = $sth->fetchrow_hashref(NAME_lc);
369 map { $form->{$_} = $ref->{$_} } keys %$ref;
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);
381 ($form->{orphaned}) = $sth->fetchrow_array;
383 $form->{orphaned} = !$form->{orphaned};
387 $main::lxdebug->leave_sub();
391 $main::lxdebug->enter_sub();
393 my ($self, $myconfig, $form) = @_;
395 # connect to database
396 my $dbh = $form->dbconnect($myconfig);
398 $form->{description} =~ s/\'/\'\'/g;
400 # id is the old account number!
402 $query = qq|UPDATE gifi SET
403 accno = '$form->{accno}',
404 description = '$form->{description}'
405 WHERE accno = '$form->{id}'|;
407 $query = qq|INSERT INTO gifi
409 VALUES ('$form->{accno}', '$form->{description}')|;
411 $dbh->do($query) || $form->dberror($query);
415 $main::lxdebug->leave_sub();
419 $main::lxdebug->enter_sub();
421 my ($self, $myconfig, $form) = @_;
423 # connect to database
424 my $dbh = $form->dbconnect($myconfig);
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);
433 $main::lxdebug->leave_sub();
437 $main::lxdebug->enter_sub();
439 my ($self, $myconfig, $form) = @_;
441 # connect to database
442 my $dbh = $form->dbconnect($myconfig);
444 my $query = qq|SELECT id, description
448 $sth = $dbh->prepare($query);
449 $sth->execute || $form->dberror($query);
451 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
452 push @{ $form->{ALL} }, $ref;
458 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my ($self, $myconfig, $form) = @_;
466 # connect to database
467 my $dbh = $form->dbconnect($myconfig);
469 my $query = qq|SELECT w.description
471 WHERE w.id = $form->{id}|;
472 my $sth = $dbh->prepare($query);
473 $sth->execute || $form->dberror($query);
475 my $ref = $sth->fetchrow_hashref(NAME_lc);
477 map { $form->{$_} = $ref->{$_} } keys %$ref;
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);
487 ($form->{orphaned}) = $sth->fetchrow_array;
488 $form->{orphaned} = !$form->{orphaned};
493 $main::lxdebug->leave_sub();
497 $main::lxdebug->enter_sub();
499 my ($self, $myconfig, $form) = @_;
501 # connect to database
502 my $dbh = $form->dbconnect($myconfig);
504 $form->{description} =~ s/\'/\'\'/g;
507 $query = qq|UPDATE warehouse SET
508 description = '$form->{description}'
509 WHERE id = $form->{id}|;
511 $query = qq|INSERT INTO warehouse
513 VALUES ('$form->{description}')|;
515 $dbh->do($query) || $form->dberror($query);
519 $main::lxdebug->leave_sub();
522 sub delete_warehouse {
523 $main::lxdebug->enter_sub();
525 my ($self, $myconfig, $form) = @_;
527 # connect to database
528 my $dbh = $form->dbconnect($myconfig);
530 $query = qq|DELETE FROM warehouse
531 WHERE id = $form->{id}|;
532 $dbh->do($query) || $form->dberror($query);
536 $main::lxdebug->leave_sub();
540 $main::lxdebug->enter_sub();
542 my ($self, $myconfig, $form) = @_;
544 # connect to database
545 my $dbh = $form->dbconnect($myconfig);
547 my $query = qq|SELECT d.id, d.description, d.role
551 $sth = $dbh->prepare($query);
552 $sth->execute || $form->dberror($query);
554 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
555 push @{ $form->{ALL} }, $ref;
561 $main::lxdebug->leave_sub();
565 $main::lxdebug->enter_sub();
567 my ($self, $myconfig, $form) = @_;
569 # connect to database
570 my $dbh = $form->dbconnect($myconfig);
572 my $query = qq|SELECT d.description, d.role
574 WHERE d.id = $form->{id}|;
575 my $sth = $dbh->prepare($query);
576 $sth->execute || $form->dberror($query);
578 my $ref = $sth->fetchrow_hashref(NAME_lc);
580 map { $form->{$_} = $ref->{$_} } keys %$ref;
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);
590 ($form->{orphaned}) = $sth->fetchrow_array;
591 $form->{orphaned} = !$form->{orphaned};
596 $main::lxdebug->leave_sub();
599 sub save_department {
600 $main::lxdebug->enter_sub();
602 my ($self, $myconfig, $form) = @_;
604 # connect to database
605 my $dbh = $form->dbconnect($myconfig);
607 $form->{description} =~ s/\'/\'\'/g;
610 $query = qq|UPDATE department SET
611 description = '$form->{description}',
612 role = '$form->{role}'
613 WHERE id = $form->{id}|;
615 $query = qq|INSERT INTO department
617 VALUES ('$form->{description}', '$form->{role}')|;
619 $dbh->do($query) || $form->dberror($query);
623 $main::lxdebug->leave_sub();
626 sub delete_department {
627 $main::lxdebug->enter_sub();
629 my ($self, $myconfig, $form) = @_;
631 # connect to database
632 my $dbh = $form->dbconnect($myconfig);
634 $query = qq|DELETE FROM department
635 WHERE id = $form->{id}|;
636 $dbh->do($query) || $form->dberror($query);
640 $main::lxdebug->leave_sub();
644 $main::lxdebug->enter_sub();
646 my ($self, $myconfig, $form) = @_;
648 # connect to database
649 my $dbh = $form->dbconnect($myconfig);
651 my $query = qq|SELECT id, lead
655 $sth = $dbh->prepare($query);
656 $sth->execute || $form->dberror($query);
658 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
659 push @{ $form->{ALL} }, $ref;
665 $main::lxdebug->leave_sub();
669 $main::lxdebug->enter_sub();
671 my ($self, $myconfig, $form) = @_;
673 # connect to database
674 my $dbh = $form->dbconnect($myconfig);
677 qq|SELECT l.id, l.lead
679 WHERE l.id = $form->{id}|;
680 my $sth = $dbh->prepare($query);
681 $sth->execute || $form->dberror($query);
683 my $ref = $sth->fetchrow_hashref(NAME_lc);
685 map { $form->{$_} = $ref->{$_} } keys %$ref;
691 $main::lxdebug->leave_sub();
695 $main::lxdebug->enter_sub();
697 my ($self, $myconfig, $form) = @_;
699 # connect to database
700 my $dbh = $form->dbconnect($myconfig);
702 $form->{lead} =~ s/\'/\'\'/g;
704 # id is the old record
706 $query = qq|UPDATE leads SET
707 lead = '$form->{description}'
708 WHERE id = $form->{id}|;
710 $query = qq|INSERT INTO leads
712 VALUES ('$form->{description}')|;
714 $dbh->do($query) || $form->dberror($query);
718 $main::lxdebug->leave_sub();
722 $main::lxdebug->enter_sub();
724 my ($self, $myconfig, $form) = @_;
726 # connect to database
727 my $dbh = $form->dbconnect($myconfig);
729 $query = qq|DELETE FROM leads
730 WHERE id = $form->{id}|;
731 $dbh->do($query) || $form->dberror($query);
735 $main::lxdebug->leave_sub();
739 $main::lxdebug->enter_sub();
741 my ($self, $myconfig, $form) = @_;
743 # connect to database
744 my $dbh = $form->dbconnect($myconfig);
746 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
750 $sth = $dbh->prepare($query);
751 $sth->execute || $form->dberror($query);
753 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
754 push @{ $form->{ALL} }, $ref;
760 $main::lxdebug->leave_sub();
764 $main::lxdebug->enter_sub();
766 my ($self, $myconfig, $form) = @_;
768 # connect to database
769 my $dbh = $form->dbconnect($myconfig);
772 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
774 WHERE b.id = $form->{id}|;
775 my $sth = $dbh->prepare($query);
776 $sth->execute || $form->dberror($query);
778 my $ref = $sth->fetchrow_hashref(NAME_lc);
780 map { $form->{$_} = $ref->{$_} } keys %$ref;
786 $main::lxdebug->leave_sub();
790 $main::lxdebug->enter_sub();
792 my ($self, $myconfig, $form) = @_;
794 # connect to database
795 my $dbh = $form->dbconnect($myconfig);
797 $form->{description} =~ s/\'/\'\'/g;
798 $form->{discount} /= 100;
799 $form->{salesman} *= 1;
801 # id is the old record
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}|;
810 $query = qq|INSERT INTO business
811 (description, discount, customernumberinit, salesman)
812 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
814 $dbh->do($query) || $form->dberror($query);
818 $main::lxdebug->leave_sub();
821 sub delete_business {
822 $main::lxdebug->enter_sub();
824 my ($self, $myconfig, $form) = @_;
826 # connect to database
827 my $dbh = $form->dbconnect($myconfig);
829 $query = qq|DELETE FROM business
830 WHERE id = $form->{id}|;
831 $dbh->do($query) || $form->dberror($query);
835 $main::lxdebug->leave_sub();
840 $main::lxdebug->enter_sub();
842 my ($self, $myconfig, $form) = @_;
844 # connect to database
845 my $dbh = $form->dbconnect($myconfig);
847 my $query = qq|SELECT id, description, template_code, article_code
851 $sth = $dbh->prepare($query);
852 $sth->execute || $form->dberror($query);
854 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
855 push @{ $form->{ALL} }, $ref;
861 $main::lxdebug->leave_sub();
865 $main::lxdebug->enter_sub();
867 my ($self, $myconfig, $form) = @_;
869 # connect to database
870 my $dbh = $form->dbconnect($myconfig);
873 qq|SELECT l.description, l.template_code, l.article_code
875 WHERE l.id = $form->{id}|;
876 my $sth = $dbh->prepare($query);
877 $sth->execute || $form->dberror($query);
879 my $ref = $sth->fetchrow_hashref(NAME_lc);
881 map { $form->{$_} = $ref->{$_} } keys %$ref;
887 $main::lxdebug->leave_sub();
891 $main::lxdebug->enter_sub();
893 my ($self, $myconfig, $form) = @_;
895 # connect to database
896 my $dbh = $form->dbconnect($myconfig);
898 $form->{description} =~ s/\'/\'\'/g;
899 $form->{article_code} =~ s/\'/\'\'/g;
900 $form->{template_code} =~ s/\'/\'\'/g;
903 # id is the old record
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}|;
911 $query = qq|INSERT INTO language
912 (description, template_code, article_code)
913 VALUES ('$form->{description}', '$form->{template_code}', '$form->{article_code}')|;
915 $dbh->do($query) || $form->dberror($query);
919 $main::lxdebug->leave_sub();
922 sub delete_language {
923 $main::lxdebug->enter_sub();
925 my ($self, $myconfig, $form) = @_;
927 # connect to database
928 my $dbh = $form->dbconnect($myconfig);
930 $query = qq|DELETE FROM language
931 WHERE id = $form->{id}|;
932 $dbh->do($query) || $form->dberror($query);
936 $main::lxdebug->leave_sub();
941 $main::lxdebug->enter_sub();
943 my ($self, $myconfig, $form) = @_;
945 # connect to database
946 my $dbh = $form->dbconnect($myconfig);
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
952 $sth = $dbh->prepare($query);
953 $sth->execute || $form->dberror($query);
955 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
956 push @{ $form->{ALL} }, $ref;
962 $main::lxdebug->leave_sub();
965 sub get_buchungsgruppe {
966 $main::lxdebug->enter_sub();
968 my ($self, $myconfig, $form) = @_;
970 # connect to database
971 my $dbh = $form->dbconnect($myconfig);
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
977 WHERE id = $form->{id}|;
978 my $sth = $dbh->prepare($query);
979 $sth->execute || $form->dberror($query);
981 my $ref = $sth->fetchrow_hashref(NAME_lc);
983 map { $form->{$_} = $ref->{$_} } keys %$ref;
988 qq|SELECT count(id) as anzahl
990 WHERE buchungsgruppen_id = $form->{id}|;
991 my $sth = $dbh->prepare($query);
992 $sth->execute || $form->dberror($query);
994 my $ref = $sth->fetchrow_hashref(NAME_lc);
995 if (!$ref->{anzahl}) {
996 $form->{orphaned} = 1;
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%'
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",
1023 push @{ $form->{"${module}_links"}{$key} },
1024 { accno => $ref->{accno},
1025 description => $ref->{description},
1037 $main::lxdebug->leave_sub();
1040 sub save_buchungsgruppe {
1041 $main::lxdebug->enter_sub();
1043 my ($self, $myconfig, $form) = @_;
1045 # connect to database
1046 my $dbh = $form->dbconnect($myconfig);
1048 $form->{description} =~ s/\'/\'\'/g;
1051 # id is the old record
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}|;
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}')|;
1070 $dbh->do($query) || $form->dberror($query);
1074 $main::lxdebug->leave_sub();
1077 sub delete_buchungsgruppe {
1078 $main::lxdebug->enter_sub();
1080 my ($self, $myconfig, $form) = @_;
1082 # connect to database
1083 my $dbh = $form->dbconnect($myconfig);
1085 $query = qq|DELETE FROM buchungsgruppe
1086 WHERE id = $form->{id}|;
1087 $dbh->do($query) || $form->dberror($query);
1091 $main::lxdebug->leave_sub();
1095 $main::lxdebug->enter_sub();
1097 my ($self, $myconfig, $form) = @_;
1099 # connect to database
1100 my $dbh = $form->dbconnect($myconfig);
1102 my $query = qq|SELECT id, printer_description, template_code, printer_command
1106 $sth = $dbh->prepare($query);
1107 $sth->execute || $form->dberror($query);
1109 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1110 push @{ $form->{ALL} }, $ref;
1116 $main::lxdebug->leave_sub();
1120 $main::lxdebug->enter_sub();
1122 my ($self, $myconfig, $form) = @_;
1124 # connect to database
1125 my $dbh = $form->dbconnect($myconfig);
1128 qq|SELECT p.printer_description, p.template_code, p.printer_command
1130 WHERE p.id = $form->{id}|;
1131 my $sth = $dbh->prepare($query);
1132 $sth->execute || $form->dberror($query);
1134 my $ref = $sth->fetchrow_hashref(NAME_lc);
1136 map { $form->{$_} = $ref->{$_} } keys %$ref;
1142 $main::lxdebug->leave_sub();
1146 $main::lxdebug->enter_sub();
1148 my ($self, $myconfig, $form) = @_;
1150 # connect to database
1151 my $dbh = $form->dbconnect($myconfig);
1153 $form->{printer_description} =~ s/\'/\'\'/g;
1154 $form->{printer_command} =~ s/\'/\'\'/g;
1155 $form->{template_code} =~ s/\'/\'\'/g;
1158 # id is the old record
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}|;
1166 $query = qq|INSERT INTO printers
1167 (printer_description, template_code, printer_command)
1168 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1170 $dbh->do($query) || $form->dberror($query);
1174 $main::lxdebug->leave_sub();
1177 sub delete_printer {
1178 $main::lxdebug->enter_sub();
1180 my ($self, $myconfig, $form) = @_;
1182 # connect to database
1183 my $dbh = $form->dbconnect($myconfig);
1185 $query = qq|DELETE FROM printers
1186 WHERE id = $form->{id}|;
1187 $dbh->do($query) || $form->dberror($query);
1191 $main::lxdebug->leave_sub();
1195 $main::lxdebug->enter_sub();
1197 my ($self, $myconfig, $form) = @_;
1199 # connect to database
1200 my $dbh = $form->dbconnect($myconfig);
1202 my $query = qq|SELECT id, adr_description, adr_code
1206 $sth = $dbh->prepare($query);
1207 $sth->execute || $form->dberror($query);
1209 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1210 push @{ $form->{ALL} }, $ref;
1216 $main::lxdebug->leave_sub();
1220 $main::lxdebug->enter_sub();
1222 my ($self, $myconfig, $form) = @_;
1224 # connect to database
1225 my $dbh = $form->dbconnect($myconfig);
1228 qq|SELECT a.adr_description, a.adr_code
1230 WHERE a.id = $form->{id}|;
1231 my $sth = $dbh->prepare($query);
1232 $sth->execute || $form->dberror($query);
1234 my $ref = $sth->fetchrow_hashref(NAME_lc);
1236 map { $form->{$_} = $ref->{$_} } keys %$ref;
1242 $main::lxdebug->leave_sub();
1246 $main::lxdebug->enter_sub();
1248 my ($self, $myconfig, $form) = @_;
1250 # connect to database
1251 my $dbh = $form->dbconnect($myconfig);
1253 $form->{adr_description} =~ s/\'/\'\'/g;
1254 $form->{adr_code} =~ s/\'/\'\'/g;
1257 # id is the old record
1259 $query = qq|UPDATE adr SET
1260 adr_description = '$form->{adr_description}',
1261 adr_code = '$form->{adr_code}'
1262 WHERE id = $form->{id}|;
1264 $query = qq|INSERT INTO adr
1265 (adr_description, adr_code)
1266 VALUES ('$form->{adr_description}', '$form->{adr_code}')|;
1268 $dbh->do($query) || $form->dberror($query);
1272 $main::lxdebug->leave_sub();
1276 $main::lxdebug->enter_sub();
1278 my ($self, $myconfig, $form) = @_;
1280 # connect to database
1281 my $dbh = $form->dbconnect($myconfig);
1283 $query = qq|DELETE FROM adr
1284 WHERE id = $form->{id}|;
1285 $dbh->do($query) || $form->dberror($query);
1289 $main::lxdebug->leave_sub();
1293 $main::lxdebug->enter_sub();
1295 my ($self, $myconfig, $form) = @_;
1297 # connect to database
1298 my $dbh = $form->dbconnect($myconfig);
1300 my $query = qq|SELECT *
1304 $sth = $dbh->prepare($query);
1305 $sth->execute || $form->dberror($query);
1307 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1308 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1309 push @{ $form->{ALL} }, $ref;
1315 $main::lxdebug->leave_sub();
1319 $main::lxdebug->enter_sub();
1321 my ($self, $myconfig, $form) = @_;
1323 # connect to database
1324 my $dbh = $form->dbconnect($myconfig);
1329 WHERE id = $form->{id}|;
1330 my $sth = $dbh->prepare($query);
1331 $sth->execute || $form->dberror($query);
1333 my $ref = $sth->fetchrow_hashref(NAME_lc);
1334 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1336 map { $form->{$_} = $ref->{$_} } keys %$ref;
1342 $main::lxdebug->leave_sub();
1346 $main::lxdebug->enter_sub();
1348 my ($self, $myconfig, $form) = @_;
1350 # connect to database
1351 my $dbh = $form->dbconnect($myconfig);
1353 $form->{description} =~ s/\'/\'\'/g;
1354 $form->{description_long} =~ s/\'/\'\'/g;
1355 $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100;
1356 $form->{ranking} *= 1;
1357 $form->{terms_netto} *= 1;
1358 $form->{terms_skonto} *= 1;
1359 $form->{percent_skonto} *= 1;
1363 # id is the old record
1365 $query = qq|UPDATE payment_terms SET
1366 description = '$form->{description}',
1367 ranking = $form->{ranking},
1368 description_long = '$form->{description_long}',
1369 terms_netto = $form->{terms_netto},
1370 terms_skonto = $form->{terms_skonto},
1371 percent_skonto = $percentskonto
1372 WHERE id = $form->{id}|;
1374 $query = qq|INSERT INTO payment_terms
1375 (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto)
1376 VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|;
1378 $dbh->do($query) || $form->dberror($query);
1382 $main::lxdebug->leave_sub();
1385 sub delete_payment {
1386 $main::lxdebug->enter_sub();
1388 my ($self, $myconfig, $form) = @_;
1390 # connect to database
1391 my $dbh = $form->dbconnect($myconfig);
1393 $query = qq|DELETE FROM payment_terms
1394 WHERE id = $form->{id}|;
1395 $dbh->do($query) || $form->dberror($query);
1399 $main::lxdebug->leave_sub();
1403 $main::lxdebug->enter_sub();
1405 my ($self, $myconfig, $form) = @_;
1407 # connect to database
1408 my $dbh = $form->dbconnect($myconfig);
1410 my $query = qq|SELECT code, sictype, description
1414 $sth = $dbh->prepare($query);
1415 $sth->execute || $form->dberror($query);
1417 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1418 push @{ $form->{ALL} }, $ref;
1424 $main::lxdebug->leave_sub();
1428 $main::lxdebug->enter_sub();
1430 my ($self, $myconfig, $form) = @_;
1432 # connect to database
1433 my $dbh = $form->dbconnect($myconfig);
1435 my $query = qq|SELECT s.code, s.sictype, s.description
1437 WHERE s.code = '$form->{code}'|;
1438 my $sth = $dbh->prepare($query);
1439 $sth->execute || $form->dberror($query);
1441 my $ref = $sth->fetchrow_hashref(NAME_lc);
1443 map { $form->{$_} = $ref->{$_} } keys %$ref;
1449 $main::lxdebug->leave_sub();
1453 $main::lxdebug->enter_sub();
1455 my ($self, $myconfig, $form) = @_;
1457 # connect to database
1458 my $dbh = $form->dbconnect($myconfig);
1460 $form->{code} =~ s/\'/\'\'/g;
1461 $form->{description} =~ s/\'/\'\'/g;
1465 $query = qq|UPDATE sic SET
1466 code = '$form->{code}',
1467 sictype = '$form->{sictype}',
1468 description = '$form->{description}'
1469 WHERE code = '$form->{id}'|;
1471 $query = qq|INSERT INTO sic
1472 (code, sictype, description)
1473 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1475 $dbh->do($query) || $form->dberror($query);
1479 $main::lxdebug->leave_sub();
1483 $main::lxdebug->enter_sub();
1485 my ($self, $myconfig, $form) = @_;
1487 # connect to database
1488 my $dbh = $form->dbconnect($myconfig);
1490 $query = qq|DELETE FROM sic
1491 WHERE code = '$form->{code}'|;
1492 $dbh->do($query) || $form->dberror($query);
1496 $main::lxdebug->leave_sub();
1500 $main::lxdebug->enter_sub();
1502 my ($self, $form) = @_;
1504 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1506 while (<TEMPLATE>) {
1507 $form->{body} .= $_;
1512 $main::lxdebug->leave_sub();
1516 $main::lxdebug->enter_sub();
1518 my ($self, $form) = @_;
1520 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1523 $form->{body} =~ s/\r\n/\n/g;
1524 print TEMPLATE $form->{body};
1528 $main::lxdebug->leave_sub();
1531 sub save_preferences {
1532 $main::lxdebug->enter_sub();
1534 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1536 map { ($form->{$_}) = split /--/, $form->{$_} }
1537 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1540 $form->{curr} =~ s/ //g;
1541 map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
1542 $form->{curr} = join ':', @a;
1544 # connect to database
1545 my $dbh = $form->dbconnect_noauto($myconfig);
1547 # these defaults are database wide
1548 # user specific variables are in myconfig
1550 my $query = qq|UPDATE defaults SET
1551 inventory_accno_id =
1552 (SELECT c.id FROM chart c
1553 WHERE c.accno = '$form->{inventory_accno}'),
1555 (SELECT c.id FROM chart c
1556 WHERE c.accno = '$form->{income_accno}'),
1558 (SELECT c.id FROM chart c
1559 WHERE c.accno = '$form->{expense_accno}'),
1561 (SELECT c.id FROM chart c
1562 WHERE c.accno = '$form->{fxgain_accno}'),
1564 (SELECT c.id FROM chart c
1565 WHERE c.accno = '$form->{fxloss_accno}'),
1566 invnumber = '$form->{invnumber}',
1567 cnnumber = '$form->{cnnumber}',
1568 sonumber = '$form->{sonumber}',
1569 ponumber = '$form->{ponumber}',
1570 sqnumber = '$form->{sqnumber}',
1571 rfqnumber = '$form->{rfqnumber}',
1572 customernumber = '$form->{customernumber}',
1573 vendornumber = '$form->{vendornumber}',
1574 articlenumber = '$form->{articlenumber}',
1575 servicenumber = '$form->{servicenumber}',
1576 yearend = '$form->{yearend}',
1577 curr = '$form->{curr}',
1578 weightunit = '$form->{weightunit}',
1579 businessnumber = '$form->{businessnumber}'
1581 $dbh->do($query) || $form->dberror($query);
1584 my $name = $form->{name};
1585 $name =~ s/\'/\'\'/g;
1586 $query = qq|UPDATE employee
1588 WHERE login = '$form->{login}'|;
1589 $dbh->do($query) || $form->dberror($query);
1591 foreach my $item (split / /, $form->{taxaccounts}) {
1592 $query = qq|UPDATE tax
1593 SET rate = | . ($form->{$item} / 100) . qq|,
1594 taxnumber = '$form->{"taxnumber_$item"}'
1595 WHERE chart_id = $item|;
1596 $dbh->do($query) || $form->dberror($query);
1599 my $rc = $dbh->commit;
1602 # save first currency in myconfig
1603 $form->{currency} = substr($form->{curr}, 0, 3);
1605 my $myconfig = new User "$memberfile", "$form->{login}";
1607 foreach my $item (keys %$form) {
1608 $myconfig->{$item} = $form->{$item};
1611 $myconfig->save_member($memberfile, $userspath);
1615 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1616 foreach $directory (@webdavdirs) {
1617 $file = "webdav/" . $directory . "/webdav-user";
1618 if ($myconfig->{$directory}) {
1619 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1620 while (<HTACCESS>) {
1621 ($login, $password) = split(/:/, $_);
1622 if ($login ne $form->{login}) {
1627 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1628 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1629 print(HTACCESS $newfile);
1632 $form->{$directory} = 0;
1633 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1634 while (<HTACCESS>) {
1635 ($login, $password) = split(/:/, $_);
1636 if ($login ne $form->{login}) {
1641 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1642 print(HTACCESS $newfile);
1648 $main::lxdebug->leave_sub();
1653 sub defaultaccounts {
1654 $main::lxdebug->enter_sub();
1656 my ($self, $myconfig, $form) = @_;
1658 # connect to database
1659 my $dbh = $form->dbconnect($myconfig);
1661 # get defaults from defaults table
1662 my $query = qq|SELECT * FROM defaults|;
1663 my $sth = $dbh->prepare($query);
1664 $sth->execute || $form->dberror($query);
1666 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1667 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1668 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1669 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1670 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1671 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1675 $query = qq|SELECT c.id, c.accno, c.description, c.link
1677 WHERE c.link LIKE '%IC%'
1679 $sth = $dbh->prepare($query);
1680 $sth->execute || $self->dberror($query);
1682 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1683 foreach my $key (split(/:/, $ref->{link})) {
1686 if ($key =~ /cogs/) {
1687 $nkey = "IC_expense";
1689 if ($key =~ /sale/) {
1690 $nkey = "IC_income";
1692 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1694 description => $ref->{description}
1701 $query = qq|SELECT c.id, c.accno, c.description
1703 WHERE c.category = 'I'
1704 AND c.charttype = 'A'
1706 $sth = $dbh->prepare($query);
1707 $sth->execute || $self->dberror($query);
1709 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1710 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1712 description => $ref->{description}
1717 $query = qq|SELECT c.id, c.accno, c.description
1719 WHERE c.category = 'E'
1720 AND c.charttype = 'A'
1722 $sth = $dbh->prepare($query);
1723 $sth->execute || $self->dberror($query);
1725 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1726 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1728 description => $ref->{description}
1733 # now get the tax rates and numbers
1734 $query = qq|SELECT c.id, c.accno, c.description,
1735 t.rate * 100 AS rate, t.taxnumber
1737 WHERE c.id = t.chart_id|;
1739 $sth = $dbh->prepare($query);
1740 $sth->execute || $form->dberror($query);
1742 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1743 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1744 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1745 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1746 if $ref->{taxnumber};
1747 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1753 $main::lxdebug->leave_sub();
1757 $main::lxdebug->enter_sub();
1759 my ($self, $myconfig, $form, $userspath) = @_;
1763 my $boundary = time;
1765 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1766 my $out = $form->{OUT};
1767 $form->{OUT} = ">$tmpfile";
1769 if ($form->{media} eq 'email') {
1774 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1775 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1777 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1778 @{ $mail->{attachments} } = ($tmpfile);
1779 $mail->{version} = $form->{version};
1780 $mail->{fileid} = "$boundary.";
1782 $myconfig->{signature} =~ s/\\n/\r\n/g;
1783 $mail->{message} = "--\n$myconfig->{signature}";
1787 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1789 # get sequences, functions and triggers
1790 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1803 # Remove DOS and Unix style line endings.
1806 # ignore comments or empty lines
1807 next if /^(--.*|\s+)$/;
1809 for (my $i = 0; $i < length($_); $i++) {
1810 my $char = substr($_, $i, 1);
1812 # Are we inside a string?
1814 if ($char eq $quote_chars[-1]) {
1820 if (($char eq "'") || ($char eq "\"")) {
1821 push(@quote_chars, $char);
1823 } elsif ($char eq ";") {
1825 # Query is complete. Check for triggers and functions.
1826 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1827 push(@functions, $query);
1829 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1830 push(@triggers, $query);
1832 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1833 push(@sequences, $1);
1835 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1836 $tablespecs{$1} = $query;
1838 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1839 push(@indices, $query);
1853 # connect to database
1854 my $dbh = $form->dbconnect($myconfig);
1856 # get all the tables
1857 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1859 my $today = scalar localtime;
1861 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1863 print OUT qq|-- Lx-Office Backup
1864 -- Dataset: $myconfig->{dbname}
1865 -- Version: $form->{dbversion}
1866 -- Host: $myconfig->{dbhost}
1867 -- Login: $form->{login}
1868 -- User: $myconfig->{name}
1872 $myconfig->{dboptions};
1876 print OUT "-- DROP Sequences\n";
1878 foreach $item (@sequences) {
1879 print OUT qq|DROP SEQUENCE $item;\n|;
1882 print OUT "-- DROP Triggers\n";
1884 foreach $item (@triggers) {
1885 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1886 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1890 print OUT "-- DROP Functions\n";
1892 foreach $item (@functions) {
1893 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1894 print OUT qq|DROP FUNCTION "$1" ();\n|;
1898 foreach $table (@tables) {
1899 if (!($table =~ /^sql_.*/)) {
1900 my $query = qq|SELECT * FROM $table|;
1902 my $sth = $dbh->prepare($query);
1903 $sth->execute || $form->dberror($query);
1905 $query = "INSERT INTO $table (";
1906 map { $query .= qq|$sth->{NAME}->[$_],| }
1907 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1910 $query .= ") VALUES";
1912 if ($tablespecs{$table}) {
1914 print(OUT "DROP TABLE $table;\n");
1915 print(OUT $tablespecs{$table}, ";\n");
1918 print(OUT "DELETE FROM $table;\n");
1920 while (my @arr = $sth->fetchrow_array) {
1923 foreach my $item (@arr) {
1924 if (defined $item) {
1925 $item =~ s/\'/\'\'/g;
1926 $fields .= qq|'$item',|;
1935 print OUT qq|$query $fields;\n|;
1942 # create indices, sequences, functions and triggers
1944 print(OUT "-- CREATE Indices\n");
1945 map({ print(OUT "$_;\n"); } @indices);
1947 print OUT "-- CREATE Sequences\n";
1948 foreach $item (@sequences) {
1949 $query = qq|SELECT last_value FROM $item|;
1950 $sth = $dbh->prepare($query);
1951 $sth->execute || $form->dberror($query);
1952 my ($id) = $sth->fetchrow_array;
1956 CREATE SEQUENCE $item START $id;
1960 print OUT "-- CREATE Functions\n";
1963 map { print(OUT $_, ";\n"); } @functions;
1965 print OUT "-- CREATE Triggers\n";
1968 map { print(OUT $_, ";\n"); } @triggers;
1975 my @args = ("gzip", "$tmpfile");
1976 system(@args) == 0 or $form->error("$args[0] : $?");
1980 if ($form->{media} eq 'email') {
1981 @{ $mail->{attachments} } = ($tmpfile);
1982 $err = $mail->send($out);
1985 if ($form->{media} eq 'file') {
1987 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1988 open(OUT, ">-") or $form->error("STDOUT : $!");
1990 print OUT qq|Content-Type: application/x-tar-gzip;
1991 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
2006 $main::lxdebug->leave_sub();
2010 $main::lxdebug->enter_sub();
2012 my ($self, $myconfig, $form) = @_;
2014 my $dbh = $form->dbconnect($myconfig);
2016 my $query = qq|SELECT closedto, revtrans FROM defaults|;
2017 my $sth = $dbh->prepare($query);
2018 $sth->execute || $form->dberror($query);
2020 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
2026 $main::lxdebug->leave_sub();
2030 $main::lxdebug->enter_sub();
2032 my ($self, $myconfig, $form) = @_;
2034 my $dbh = $form->dbconnect($myconfig);
2036 if ($form->{revtrans}) {
2038 $query = qq|UPDATE defaults SET closedto = NULL,
2041 if ($form->{closedto}) {
2043 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
2047 $query = qq|UPDATE defaults SET closedto = NULL,
2052 # set close in defaults
2053 $dbh->do($query) || $form->dberror($query);
2057 $main::lxdebug->leave_sub();
2061 my ($self, $units, $unit_name, $factor) = @_;
2063 $factor = 1 unless ($factor);
2065 my $unit = $units->{$unit_name};
2067 if (!defined($unit) || !$unit->{"base_unit"} ||
2068 ($unit_name eq $unit->{"base_unit"})) {
2069 return ($unit_name, $factor);
2072 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
2075 sub retrieve_units {
2076 $main::lxdebug->enter_sub();
2078 my ($self, $myconfig, $form, $type, $prefix) = @_;
2080 my $dbh = $form->dbconnect($myconfig);
2082 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
2085 $query .= " WHERE (type = ?)";
2089 my $sth = $dbh->prepare($query);
2090 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2093 while (my $ref = $sth->fetchrow_hashref()) {
2094 $units->{$ref->{"name"}} = $ref;
2098 foreach my $unit (keys(%{$units})) {
2099 ($units->{$unit}->{"${prefix}base_unit"}, $units->{$unit}->{"${prefix}factor"}) = AM->get_base_unit($units, $unit);
2104 $main::lxdebug->leave_sub();
2110 $main::lxdebug->enter_sub();
2112 my ($self, $myconfig, $form, $units) = @_;
2114 my $dbh = $form->dbconnect($myconfig);
2116 foreach my $unit (values(%{$units})) {
2117 my $base_unit = $unit->{"original_base_unit"};
2118 while ($base_unit) {
2119 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
2120 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
2121 $base_unit = $units->{$base_unit}->{"original_base_unit"};
2125 foreach my $unit (values(%{$units})) {
2126 $unit->{"in_use"} = 0;
2127 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2129 foreach my $table (qw(parts invoice orderitems)) {
2130 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2132 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2133 $query .= "= " . $dbh->quote($unit->{"name"});
2135 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2138 my ($count) = $dbh->selectrow_array($query);
2139 $form->dberror($query) if ($dbh->err);
2142 $unit->{"in_use"} = 1;
2150 $main::lxdebug->leave_sub();
2153 sub unit_select_data {
2154 $main::lxdebug->enter_sub();
2156 my ($self, $units, $selected, $empty_entry) = @_;
2161 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2164 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2165 push(@{$select}, { "name" => $unit,
2166 "base_unit" => $units->{$unit}->{"base_unit"},
2167 "factor" => $units->{$unit}->{"factor"},
2168 "selected" => ($unit eq $selected) ? "selected" : "" });
2171 $main::lxdebug->leave_sub();
2176 sub unit_select_html {
2177 $main::lxdebug->enter_sub();
2179 my ($self, $units, $name, $selected, $convertible_into) = @_;
2181 my $select = "<select name=${name}>";
2183 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2184 if (!$convertible_into ||
2185 ($units->{$convertible_into} &&
2186 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2187 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2190 $select .= "</select>";
2192 $main::lxdebug->leave_sub();
2198 $main::lxdebug->enter_sub();
2200 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type) = @_;
2202 my $dbh = $form->dbconnect($myconfig);
2204 my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
2205 $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
2208 $main::lxdebug->leave_sub();
2212 $main::lxdebug->enter_sub();
2214 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2216 my $dbh = $form->dbconnect_noauto($myconfig);
2218 my ($base_unit, $unit, $sth, $query);
2220 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2221 $query = "DELETE FROM units WHERE name = ?";
2222 $sth = $dbh->prepare($query);
2223 map({ $sth->execute($_) || $form->dberror($query . " ($_)"); } @{$delete_units});
2227 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2228 $sth = $dbh->prepare($query);
2230 foreach $unit (values(%{$units})) {
2231 $unit->{"depth"} = 0;
2232 my $base_unit = $unit;
2233 while ($base_unit->{"base_unit"}) {
2235 $base_unit = $units->{$base_unit->{"base_unit"}};
2239 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2240 next if ($unit->{"unchanged_unit"});
2242 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2243 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2250 $main::lxdebug->leave_sub();