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 *
1206 $sth = $dbh->prepare($query);
1207 $sth->execute || $form->dberror($query);
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;
1217 $main::lxdebug->leave_sub();
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig, $form) = @_;
1225 # connect to database
1226 my $dbh = $form->dbconnect($myconfig);
1231 WHERE id = $form->{id}|;
1232 my $sth = $dbh->prepare($query);
1233 $sth->execute || $form->dberror($query);
1235 my $ref = $sth->fetchrow_hashref(NAME_lc);
1236 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1238 map { $form->{$_} = $ref->{$_} } keys %$ref;
1244 $main::lxdebug->leave_sub();
1248 $main::lxdebug->enter_sub();
1250 my ($self, $myconfig, $form) = @_;
1252 # connect to database
1253 my $dbh = $form->dbconnect($myconfig);
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;
1265 # id is the old record
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}|;
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)|;
1280 $dbh->do($query) || $form->dberror($query);
1284 $main::lxdebug->leave_sub();
1287 sub delete_payment {
1288 $main::lxdebug->enter_sub();
1290 my ($self, $myconfig, $form) = @_;
1292 # connect to database
1293 my $dbh = $form->dbconnect($myconfig);
1295 $query = qq|DELETE FROM payment_terms
1296 WHERE id = $form->{id}|;
1297 $dbh->do($query) || $form->dberror($query);
1301 $main::lxdebug->leave_sub();
1305 $main::lxdebug->enter_sub();
1307 my ($self, $myconfig, $form) = @_;
1309 # connect to database
1310 my $dbh = $form->dbconnect($myconfig);
1312 my $query = qq|SELECT code, sictype, description
1316 $sth = $dbh->prepare($query);
1317 $sth->execute || $form->dberror($query);
1319 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1320 push @{ $form->{ALL} }, $ref;
1326 $main::lxdebug->leave_sub();
1330 $main::lxdebug->enter_sub();
1332 my ($self, $myconfig, $form) = @_;
1334 # connect to database
1335 my $dbh = $form->dbconnect($myconfig);
1337 my $query = qq|SELECT s.code, s.sictype, s.description
1339 WHERE s.code = '$form->{code}'|;
1340 my $sth = $dbh->prepare($query);
1341 $sth->execute || $form->dberror($query);
1343 my $ref = $sth->fetchrow_hashref(NAME_lc);
1345 map { $form->{$_} = $ref->{$_} } keys %$ref;
1351 $main::lxdebug->leave_sub();
1355 $main::lxdebug->enter_sub();
1357 my ($self, $myconfig, $form) = @_;
1359 # connect to database
1360 my $dbh = $form->dbconnect($myconfig);
1362 $form->{code} =~ s/\'/\'\'/g;
1363 $form->{description} =~ s/\'/\'\'/g;
1367 $query = qq|UPDATE sic SET
1368 code = '$form->{code}',
1369 sictype = '$form->{sictype}',
1370 description = '$form->{description}'
1371 WHERE code = '$form->{id}'|;
1373 $query = qq|INSERT INTO sic
1374 (code, sictype, description)
1375 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1377 $dbh->do($query) || $form->dberror($query);
1381 $main::lxdebug->leave_sub();
1385 $main::lxdebug->enter_sub();
1387 my ($self, $myconfig, $form) = @_;
1389 # connect to database
1390 my $dbh = $form->dbconnect($myconfig);
1392 $query = qq|DELETE FROM sic
1393 WHERE code = '$form->{code}'|;
1394 $dbh->do($query) || $form->dberror($query);
1398 $main::lxdebug->leave_sub();
1402 $main::lxdebug->enter_sub();
1404 my ($self, $form) = @_;
1406 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1408 while (<TEMPLATE>) {
1409 $form->{body} .= $_;
1414 $main::lxdebug->leave_sub();
1418 $main::lxdebug->enter_sub();
1420 my ($self, $form) = @_;
1422 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1425 $form->{body} =~ s/\r\n/\n/g;
1426 print TEMPLATE $form->{body};
1430 $main::lxdebug->leave_sub();
1433 sub save_preferences {
1434 $main::lxdebug->enter_sub();
1436 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1438 map { ($form->{$_}) = split /--/, $form->{$_} }
1439 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1442 $form->{curr} =~ s/ //g;
1443 map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
1444 $form->{curr} = join ':', @a;
1446 # connect to database
1447 my $dbh = $form->dbconnect_noauto($myconfig);
1449 # these defaults are database wide
1450 # user specific variables are in myconfig
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}'),
1457 (SELECT c.id FROM chart c
1458 WHERE c.accno = '$form->{income_accno}'),
1460 (SELECT c.id FROM chart c
1461 WHERE c.accno = '$form->{expense_accno}'),
1463 (SELECT c.id FROM chart c
1464 WHERE c.accno = '$form->{fxgain_accno}'),
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}'
1483 $dbh->do($query) || $form->dberror($query);
1486 my $name = $form->{name};
1487 $name =~ s/\'/\'\'/g;
1488 $query = qq|UPDATE employee
1490 WHERE login = '$form->{login}'|;
1491 $dbh->do($query) || $form->dberror($query);
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);
1501 my $rc = $dbh->commit;
1504 # save first currency in myconfig
1505 $form->{currency} = substr($form->{curr}, 0, 3);
1507 my $myconfig = new User "$memberfile", "$form->{login}";
1509 foreach my $item (keys %$form) {
1510 $myconfig->{$item} = $form->{$item};
1513 $myconfig->save_member($memberfile, $userspath);
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}) {
1529 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1530 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1531 print(HTACCESS $newfile);
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}) {
1543 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1544 print(HTACCESS $newfile);
1550 $main::lxdebug->leave_sub();
1555 sub defaultaccounts {
1556 $main::lxdebug->enter_sub();
1558 my ($self, $myconfig, $form) = @_;
1560 # connect to database
1561 my $dbh = $form->dbconnect($myconfig);
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);
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};
1577 $query = qq|SELECT c.id, c.accno, c.description, c.link
1579 WHERE c.link LIKE '%IC%'
1581 $sth = $dbh->prepare($query);
1582 $sth->execute || $self->dberror($query);
1584 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1585 foreach my $key (split(/:/, $ref->{link})) {
1588 if ($key =~ /cogs/) {
1589 $nkey = "IC_expense";
1591 if ($key =~ /sale/) {
1592 $nkey = "IC_income";
1594 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1596 description => $ref->{description}
1603 $query = qq|SELECT c.id, c.accno, c.description
1605 WHERE c.category = 'I'
1606 AND c.charttype = 'A'
1608 $sth = $dbh->prepare($query);
1609 $sth->execute || $self->dberror($query);
1611 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1612 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1614 description => $ref->{description}
1619 $query = qq|SELECT c.id, c.accno, c.description
1621 WHERE c.category = 'E'
1622 AND c.charttype = 'A'
1624 $sth = $dbh->prepare($query);
1625 $sth->execute || $self->dberror($query);
1627 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1628 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1630 description => $ref->{description}
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
1639 WHERE c.id = t.chart_id|;
1641 $sth = $dbh->prepare($query);
1642 $sth->execute || $form->dberror($query);
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};
1655 $main::lxdebug->leave_sub();
1659 $main::lxdebug->enter_sub();
1661 my ($self, $myconfig, $form, $userspath) = @_;
1665 my $boundary = time;
1667 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1668 my $out = $form->{OUT};
1669 $form->{OUT} = ">$tmpfile";
1671 if ($form->{media} eq 'email') {
1676 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1677 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1679 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1680 @{ $mail->{attachments} } = ($tmpfile);
1681 $mail->{version} = $form->{version};
1682 $mail->{fileid} = "$boundary.";
1684 $myconfig->{signature} =~ s/\\n/\r\n/g;
1685 $mail->{message} = "--\n$myconfig->{signature}";
1689 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1691 # get sequences, functions and triggers
1692 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1705 # Remove DOS and Unix style line endings.
1708 # ignore comments or empty lines
1709 next if /^(--.*|\s+)$/;
1711 for (my $i = 0; $i < length($_); $i++) {
1712 my $char = substr($_, $i, 1);
1714 # Are we inside a string?
1716 if ($char eq $quote_chars[-1]) {
1722 if (($char eq "'") || ($char eq "\"")) {
1723 push(@quote_chars, $char);
1725 } elsif ($char eq ";") {
1727 # Query is complete. Check for triggers and functions.
1728 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1729 push(@functions, $query);
1731 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1732 push(@triggers, $query);
1734 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1735 push(@sequences, $1);
1737 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1738 $tablespecs{$1} = $query;
1740 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1741 push(@indices, $query);
1755 # connect to database
1756 my $dbh = $form->dbconnect($myconfig);
1758 # get all the tables
1759 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1761 my $today = scalar localtime;
1763 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
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}
1774 $myconfig->{dboptions};
1778 print OUT "-- DROP Sequences\n";
1780 foreach $item (@sequences) {
1781 print OUT qq|DROP SEQUENCE $item;\n|;
1784 print OUT "-- DROP Triggers\n";
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|;
1792 print OUT "-- DROP Functions\n";
1794 foreach $item (@functions) {
1795 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1796 print OUT qq|DROP FUNCTION "$1" ();\n|;
1800 foreach $table (@tables) {
1801 if (!($table =~ /^sql_.*/)) {
1802 my $query = qq|SELECT * FROM $table|;
1804 my $sth = $dbh->prepare($query);
1805 $sth->execute || $form->dberror($query);
1807 $query = "INSERT INTO $table (";
1808 map { $query .= qq|$sth->{NAME}->[$_],| }
1809 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1812 $query .= ") VALUES";
1814 if ($tablespecs{$table}) {
1816 print(OUT "DROP TABLE $table;\n");
1817 print(OUT $tablespecs{$table}, ";\n");
1820 print(OUT "DELETE FROM $table;\n");
1822 while (my @arr = $sth->fetchrow_array) {
1825 foreach my $item (@arr) {
1826 if (defined $item) {
1827 $item =~ s/\'/\'\'/g;
1828 $fields .= qq|'$item',|;
1837 print OUT qq|$query $fields;\n|;
1844 # create indices, sequences, functions and triggers
1846 print(OUT "-- CREATE Indices\n");
1847 map({ print(OUT "$_;\n"); } @indices);
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;
1858 CREATE SEQUENCE $item START $id;
1862 print OUT "-- CREATE Functions\n";
1865 map { print(OUT $_, ";\n"); } @functions;
1867 print OUT "-- CREATE Triggers\n";
1870 map { print(OUT $_, ";\n"); } @triggers;
1877 my @args = ("gzip", "$tmpfile");
1878 system(@args) == 0 or $form->error("$args[0] : $?");
1882 if ($form->{media} eq 'email') {
1883 @{ $mail->{attachments} } = ($tmpfile);
1884 $err = $mail->send($out);
1887 if ($form->{media} eq 'file') {
1889 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1890 open(OUT, ">-") or $form->error("STDOUT : $!");
1892 print OUT qq|Content-Type: application/x-tar-gzip;
1893 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1908 $main::lxdebug->leave_sub();
1912 $main::lxdebug->enter_sub();
1914 my ($self, $myconfig, $form) = @_;
1916 my $dbh = $form->dbconnect($myconfig);
1918 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1919 my $sth = $dbh->prepare($query);
1920 $sth->execute || $form->dberror($query);
1922 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1928 $main::lxdebug->leave_sub();
1932 $main::lxdebug->enter_sub();
1934 my ($self, $myconfig, $form) = @_;
1936 my $dbh = $form->dbconnect($myconfig);
1938 if ($form->{revtrans}) {
1940 $query = qq|UPDATE defaults SET closedto = NULL,
1943 if ($form->{closedto}) {
1945 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1949 $query = qq|UPDATE defaults SET closedto = NULL,
1954 # set close in defaults
1955 $dbh->do($query) || $form->dberror($query);
1959 $main::lxdebug->leave_sub();
1963 my ($self, $units, $unit_name, $factor) = @_;
1965 $factor = 1 unless ($factor);
1967 my $unit = $units->{$unit_name};
1969 if (!defined($unit) || !$unit->{"base_unit"} ||
1970 ($unit_name eq $unit->{"base_unit"})) {
1971 return ($unit_name, $factor);
1974 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1977 sub retrieve_units {
1978 $main::lxdebug->enter_sub();
1980 my ($self, $myconfig, $form, $type, $prefix) = @_;
1982 my $dbh = $form->dbconnect($myconfig);
1984 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1987 $query .= " WHERE (type = ?)";
1991 my $sth = $dbh->prepare($query);
1992 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1995 while (my $ref = $sth->fetchrow_hashref()) {
1996 $units->{$ref->{"name"}} = $ref;
2000 foreach my $unit (keys(%{$units})) {
2001 ($units->{$unit}->{"${prefix}base_unit"}, $units->{$unit}->{"${prefix}factor"}) = AM->get_base_unit($units, $unit);
2006 $main::lxdebug->leave_sub();
2012 $main::lxdebug->enter_sub();
2014 my ($self, $myconfig, $form, $units) = @_;
2016 my $dbh = $form->dbconnect($myconfig);
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"};
2027 foreach my $unit (values(%{$units})) {
2028 $unit->{"in_use"} = 0;
2029 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2031 foreach my $table (qw(parts invoice orderitems)) {
2032 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2034 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2035 $query .= "= " . $dbh->quote($unit->{"name"});
2037 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2040 my ($count) = $dbh->selectrow_array($query);
2041 $form->dberror($query) if ($dbh->err);
2044 $unit->{"in_use"} = 1;
2052 $main::lxdebug->leave_sub();
2055 sub unit_select_data {
2056 $main::lxdebug->enter_sub();
2058 my ($self, $units, $selected, $empty_entry) = @_;
2063 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
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" : "" });
2073 $main::lxdebug->leave_sub();
2078 sub unit_select_html {
2079 $main::lxdebug->enter_sub();
2081 my ($self, $units, $name, $selected, $convertible_into) = @_;
2083 my $select = "<select name=${name}>";
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>";
2092 $select .= "</select>";
2094 $main::lxdebug->leave_sub();
2100 $main::lxdebug->enter_sub();
2102 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type) = @_;
2104 my $dbh = $form->dbconnect($myconfig);
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)");
2110 $main::lxdebug->leave_sub();
2114 $main::lxdebug->enter_sub();
2116 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2118 my $dbh = $form->dbconnect_noauto($myconfig);
2120 my ($base_unit, $unit, $sth, $query);
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});
2129 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2130 $sth = $dbh->prepare($query);
2132 foreach $unit (values(%{$units})) {
2133 $unit->{"depth"} = 0;
2134 my $base_unit = $unit;
2135 while ($base_unit->{"base_unit"}) {
2137 $base_unit = $units->{$base_unit->{"base_unit"}};
2141 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2142 next if ($unit->{"unchanged_unit"});
2144 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2145 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2152 $main::lxdebug->leave_sub();