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, description, discount, customernumberinit, salesman
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 b.description, b.discount, b.customernumberinit, b.salesman
679 WHERE b.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->{description} =~ s/\'/\'\'/g;
703 $form->{discount} /= 100;
704 $form->{salesman} *= 1;
706 # id is the old record
708 $query = qq|UPDATE business SET
709 description = '$form->{description}',
710 discount = $form->{discount},
711 customernumberinit = '$form->{customernumberinit}',
712 salesman = '$form->{salesman}'
713 WHERE id = $form->{id}|;
715 $query = qq|INSERT INTO business
716 (description, discount, customernumberinit, salesman)
717 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
719 $dbh->do($query) || $form->dberror($query);
723 $main::lxdebug->leave_sub();
726 sub delete_business {
727 $main::lxdebug->enter_sub();
729 my ($self, $myconfig, $form) = @_;
731 # connect to database
732 my $dbh = $form->dbconnect($myconfig);
734 $query = qq|DELETE FROM business
735 WHERE id = $form->{id}|;
736 $dbh->do($query) || $form->dberror($query);
740 $main::lxdebug->leave_sub();
745 $main::lxdebug->enter_sub();
747 my ($self, $myconfig, $form) = @_;
749 # connect to database
750 my $dbh = $form->dbconnect($myconfig);
752 my $query = qq|SELECT id, description, template_code, article_code
756 $sth = $dbh->prepare($query);
757 $sth->execute || $form->dberror($query);
759 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
760 push @{ $form->{ALL} }, $ref;
766 $main::lxdebug->leave_sub();
770 $main::lxdebug->enter_sub();
772 my ($self, $myconfig, $form) = @_;
774 # connect to database
775 my $dbh = $form->dbconnect($myconfig);
778 qq|SELECT l.description, l.template_code, l.article_code
780 WHERE l.id = $form->{id}|;
781 my $sth = $dbh->prepare($query);
782 $sth->execute || $form->dberror($query);
784 my $ref = $sth->fetchrow_hashref(NAME_lc);
786 map { $form->{$_} = $ref->{$_} } keys %$ref;
792 $main::lxdebug->leave_sub();
796 $main::lxdebug->enter_sub();
798 my ($self, $myconfig, $form) = @_;
800 # connect to database
801 my $dbh = $form->dbconnect($myconfig);
803 $form->{description} =~ s/\'/\'\'/g;
804 $form->{article_code} =~ s/\'/\'\'/g;
805 $form->{template_code} =~ s/\'/\'\'/g;
808 # id is the old record
810 $query = qq|UPDATE language SET
811 description = '$form->{description}',
812 template_code = '$form->{template_code}',
813 article_code = '$form->{article_code}'
814 WHERE id = $form->{id}|;
816 $query = qq|INSERT INTO language
817 (description, template_code, article_code)
818 VALUES ('$form->{description}', '$form->{template_code}', '$form->{article_code}')|;
820 $dbh->do($query) || $form->dberror($query);
824 $main::lxdebug->leave_sub();
827 sub delete_language {
828 $main::lxdebug->enter_sub();
830 my ($self, $myconfig, $form) = @_;
832 # connect to database
833 my $dbh = $form->dbconnect($myconfig);
835 $query = qq|DELETE FROM language
836 WHERE id = $form->{id}|;
837 $dbh->do($query) || $form->dberror($query);
841 $main::lxdebug->leave_sub();
846 $main::lxdebug->enter_sub();
848 my ($self, $myconfig, $form) = @_;
850 # connect to database
851 my $dbh = $form->dbconnect($myconfig);
853 my $query = qq|SELECT id, description, inventory_accno_id, (select accno from chart where id=inventory_accno_id) as inventory_accno, income_accno_id_0, (select accno from chart where id=income_accno_id_0) as income_accno_0, expense_accno_id_0, (select accno from chart where id=expense_accno_id_0) as expense_accno_0, income_accno_id_1, (select accno from chart where id=income_accno_id_1) as income_accno_1, expense_accno_id_1, (select accno from chart where id=expense_accno_id_1) as expense_accno_1, income_accno_id_2, (select accno from chart where id=income_accno_id_2) as income_accno_2, expense_accno_id_2, (select accno from chart where id=expense_accno_id_2) as expense_accno_2, income_accno_id_3, (select accno from chart where id=income_accno_id_3) as income_accno_3, expense_accno_id_3, (select accno from chart where id=expense_accno_id_3) as expense_accno_3
857 $sth = $dbh->prepare($query);
858 $sth->execute || $form->dberror($query);
860 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
861 push @{ $form->{ALL} }, $ref;
867 $main::lxdebug->leave_sub();
870 sub get_buchungsgruppe {
871 $main::lxdebug->enter_sub();
873 my ($self, $myconfig, $form) = @_;
875 # connect to database
876 my $dbh = $form->dbconnect($myconfig);
880 qq|SELECT description, inventory_accno_id, (select accno from chart where id=inventory_accno_id) as inventory_accno, income_accno_id_0, (select accno from chart where id=income_accno_id_0) as income_accno_0, expense_accno_id_0, (select accno from chart where id=expense_accno_id_0) as expense_accno_0, income_accno_id_1, (select accno from chart where id=income_accno_id_1) as income_accno_1, expense_accno_id_1, (select accno from chart where id=expense_accno_id_1) as expense_accno_1, income_accno_id_2, (select accno from chart where id=income_accno_id_2) as income_accno_2, expense_accno_id_2, (select accno from chart where id=expense_accno_id_2) as expense_accno_2, income_accno_id_3, (select accno from chart where id=income_accno_id_3) as income_accno_3, expense_accno_id_3, (select accno from chart where id=expense_accno_id_3) as expense_accno_3
882 WHERE id = $form->{id}|;
883 my $sth = $dbh->prepare($query);
884 $sth->execute || $form->dberror($query);
886 my $ref = $sth->fetchrow_hashref(NAME_lc);
888 map { $form->{$_} = $ref->{$_} } keys %$ref;
893 qq|SELECT count(id) as anzahl
895 WHERE buchungsgruppen_id = $form->{id}|;
896 my $sth = $dbh->prepare($query);
897 $sth->execute || $form->dberror($query);
899 my $ref = $sth->fetchrow_hashref(NAME_lc);
900 if (!$ref->{anzahl}) {
901 $form->{orphaned} = 1;
907 $query = qq|SELECT c.accno, c.description, c.link, c.id,
908 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
909 FROM chart c, defaults d
910 WHERE c.link LIKE '%$module%'
914 my $sth = $dbh->prepare($query);
915 $sth->execute || $form->dberror($query);
916 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
917 foreach my $key (split /:/, $ref->{link}) {
918 if ($key =~ /$module/) {
919 if ( ($ref->{id} eq $ref->{inventory_accno_id})
920 || ($ref->{id} eq $ref->{income_accno_id})
921 || ($ref->{id} eq $ref->{expense_accno_id})) {
922 push @{ $form->{"${module}_links"}{$key} },
923 { accno => $ref->{accno},
924 description => $ref->{description},
925 selected => "selected",
928 push @{ $form->{"${module}_links"}{$key} },
929 { accno => $ref->{accno},
930 description => $ref->{description},
942 $main::lxdebug->leave_sub();
945 sub save_buchungsgruppe {
946 $main::lxdebug->enter_sub();
948 my ($self, $myconfig, $form) = @_;
950 # connect to database
951 my $dbh = $form->dbconnect($myconfig);
953 $form->{description} =~ s/\'/\'\'/g;
956 # id is the old record
958 $query = qq|UPDATE buchungsgruppen SET
959 description = '$form->{description}',
960 inventory_accno_id = '$form->{inventory_accno_id}',
961 income_accno_id_0 = '$form->{income_accno_id_0}',
962 expense_accno_id_0 = '$form->{expense_accno_id_0}',
963 income_accno_id_1 = '$form->{income_accno_id_1}',
964 expense_accno_id_1 = '$form->{expense_accno_id_1}',
965 income_accno_id_2 = '$form->{income_accno_id_2}',
966 expense_accno_id_2 = '$form->{expense_accno_id_2}',
967 income_accno_id_3 = '$form->{income_accno_id_3}',
968 expense_accno_id_3 = '$form->{expense_accno_id_3}'
969 WHERE id = $form->{id}|;
971 $query = qq|INSERT INTO buchungsgruppen
972 (description, inventory_accno_id, income_accno_id_0, expense_accno_id_0, income_accno_id_1, expense_accno_id_1, income_accno_id_2, expense_accno_id_2, income_accno_id_3, expense_accno_id_3)
973 VALUES ('$form->{description}', '$form->{inventory_accno_id}', '$form->{income_accno_id_0}', '$form->{expense_accno_id_0}', '$form->{income_accno_id_1}', '$form->{expense_accno_id_1}', '$form->{income_accno_id_2}', '$form->{expense_accno_id_2}', '$form->{income_accno_id_3}', '$form->{expense_accno_id_3}')|;
975 $dbh->do($query) || $form->dberror($query);
979 $main::lxdebug->leave_sub();
982 sub delete_buchungsgruppe {
983 $main::lxdebug->enter_sub();
985 my ($self, $myconfig, $form) = @_;
987 # connect to database
988 my $dbh = $form->dbconnect($myconfig);
990 $query = qq|DELETE FROM buchungsgruppe
991 WHERE id = $form->{id}|;
992 $dbh->do($query) || $form->dberror($query);
996 $main::lxdebug->leave_sub();
1000 $main::lxdebug->enter_sub();
1002 my ($self, $myconfig, $form) = @_;
1004 # connect to database
1005 my $dbh = $form->dbconnect($myconfig);
1007 my $query = qq|SELECT id, printer_description, template_code, printer_command
1011 $sth = $dbh->prepare($query);
1012 $sth->execute || $form->dberror($query);
1014 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1015 push @{ $form->{ALL} }, $ref;
1021 $main::lxdebug->leave_sub();
1025 $main::lxdebug->enter_sub();
1027 my ($self, $myconfig, $form) = @_;
1029 # connect to database
1030 my $dbh = $form->dbconnect($myconfig);
1033 qq|SELECT p.printer_description, p.template_code, p.printer_command
1035 WHERE p.id = $form->{id}|;
1036 my $sth = $dbh->prepare($query);
1037 $sth->execute || $form->dberror($query);
1039 my $ref = $sth->fetchrow_hashref(NAME_lc);
1041 map { $form->{$_} = $ref->{$_} } keys %$ref;
1047 $main::lxdebug->leave_sub();
1051 $main::lxdebug->enter_sub();
1053 my ($self, $myconfig, $form) = @_;
1055 # connect to database
1056 my $dbh = $form->dbconnect($myconfig);
1058 $form->{printer_description} =~ s/\'/\'\'/g;
1059 $form->{printer_command} =~ s/\'/\'\'/g;
1060 $form->{template_code} =~ s/\'/\'\'/g;
1063 # id is the old record
1065 $query = qq|UPDATE printers SET
1066 printer_description = '$form->{printer_description}',
1067 template_code = '$form->{template_code}',
1068 printer_command = '$form->{printer_command}'
1069 WHERE id = $form->{id}|;
1071 $query = qq|INSERT INTO printers
1072 (printer_description, template_code, printer_command)
1073 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1075 $dbh->do($query) || $form->dberror($query);
1079 $main::lxdebug->leave_sub();
1082 sub delete_printer {
1083 $main::lxdebug->enter_sub();
1085 my ($self, $myconfig, $form) = @_;
1087 # connect to database
1088 my $dbh = $form->dbconnect($myconfig);
1090 $query = qq|DELETE FROM printers
1091 WHERE id = $form->{id}|;
1092 $dbh->do($query) || $form->dberror($query);
1096 $main::lxdebug->leave_sub();
1100 $main::lxdebug->enter_sub();
1102 my ($self, $myconfig, $form) = @_;
1104 # connect to database
1105 my $dbh = $form->dbconnect($myconfig);
1107 my $query = qq|SELECT id, adr_description, adr_code
1111 $sth = $dbh->prepare($query);
1112 $sth->execute || $form->dberror($query);
1114 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1115 push @{ $form->{ALL} }, $ref;
1121 $main::lxdebug->leave_sub();
1125 $main::lxdebug->enter_sub();
1127 my ($self, $myconfig, $form) = @_;
1129 # connect to database
1130 my $dbh = $form->dbconnect($myconfig);
1133 qq|SELECT a.adr_description, a.adr_code
1135 WHERE a.id = $form->{id}|;
1136 my $sth = $dbh->prepare($query);
1137 $sth->execute || $form->dberror($query);
1139 my $ref = $sth->fetchrow_hashref(NAME_lc);
1141 map { $form->{$_} = $ref->{$_} } keys %$ref;
1147 $main::lxdebug->leave_sub();
1151 $main::lxdebug->enter_sub();
1153 my ($self, $myconfig, $form) = @_;
1155 # connect to database
1156 my $dbh = $form->dbconnect($myconfig);
1158 $form->{adr_description} =~ s/\'/\'\'/g;
1159 $form->{adr_code} =~ s/\'/\'\'/g;
1162 # id is the old record
1164 $query = qq|UPDATE adr SET
1165 adr_description = '$form->{adr_description}',
1166 adr_code = '$form->{adr_code}'
1167 WHERE id = $form->{id}|;
1169 $query = qq|INSERT INTO adr
1170 (adr_description, adr_code)
1171 VALUES ('$form->{adr_description}', '$form->{adr_code}')|;
1173 $dbh->do($query) || $form->dberror($query);
1177 $main::lxdebug->leave_sub();
1181 $main::lxdebug->enter_sub();
1183 my ($self, $myconfig, $form) = @_;
1185 # connect to database
1186 my $dbh = $form->dbconnect($myconfig);
1188 $query = qq|DELETE FROM adr
1189 WHERE id = $form->{id}|;
1190 $dbh->do($query) || $form->dberror($query);
1194 $main::lxdebug->leave_sub();
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig, $form) = @_;
1202 # connect to database
1203 my $dbh = $form->dbconnect($myconfig);
1205 my $query = qq|SELECT *
1209 $sth = $dbh->prepare($query);
1210 $sth->execute || $form->dberror($query);
1212 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1213 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1214 push @{ $form->{ALL} }, $ref;
1220 $main::lxdebug->leave_sub();
1224 $main::lxdebug->enter_sub();
1226 my ($self, $myconfig, $form) = @_;
1228 # connect to database
1229 my $dbh = $form->dbconnect($myconfig);
1234 WHERE id = $form->{id}|;
1235 my $sth = $dbh->prepare($query);
1236 $sth->execute || $form->dberror($query);
1238 my $ref = $sth->fetchrow_hashref(NAME_lc);
1239 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1241 map { $form->{$_} = $ref->{$_} } keys %$ref;
1247 $main::lxdebug->leave_sub();
1251 $main::lxdebug->enter_sub();
1253 my ($self, $myconfig, $form) = @_;
1255 # connect to database
1256 my $dbh = $form->dbconnect($myconfig);
1258 $form->{description} =~ s/\'/\'\'/g;
1259 $form->{description_long} =~ s/\'/\'\'/g;
1260 $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100;
1261 $form->{ranking} *= 1;
1262 $form->{terms_netto} *= 1;
1263 $form->{terms_skonto} *= 1;
1264 $form->{percent_skonto} *= 1;
1268 # id is the old record
1270 $query = qq|UPDATE payment_terms SET
1271 description = '$form->{description}',
1272 ranking = $form->{ranking},
1273 description_long = '$form->{description_long}',
1274 terms_netto = $form->{terms_netto},
1275 terms_skonto = $form->{terms_skonto},
1276 percent_skonto = $percentskonto
1277 WHERE id = $form->{id}|;
1279 $query = qq|INSERT INTO payment_terms
1280 (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto)
1281 VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|;
1283 $dbh->do($query) || $form->dberror($query);
1287 $main::lxdebug->leave_sub();
1290 sub delete_payment {
1291 $main::lxdebug->enter_sub();
1293 my ($self, $myconfig, $form) = @_;
1295 # connect to database
1296 my $dbh = $form->dbconnect($myconfig);
1298 $query = qq|DELETE FROM payment_terms
1299 WHERE id = $form->{id}|;
1300 $dbh->do($query) || $form->dberror($query);
1304 $main::lxdebug->leave_sub();
1308 $main::lxdebug->enter_sub();
1310 my ($self, $myconfig, $form) = @_;
1312 # connect to database
1313 my $dbh = $form->dbconnect($myconfig);
1315 my $query = qq|SELECT code, sictype, description
1319 $sth = $dbh->prepare($query);
1320 $sth->execute || $form->dberror($query);
1322 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1323 push @{ $form->{ALL} }, $ref;
1329 $main::lxdebug->leave_sub();
1333 $main::lxdebug->enter_sub();
1335 my ($self, $myconfig, $form) = @_;
1337 # connect to database
1338 my $dbh = $form->dbconnect($myconfig);
1340 my $query = qq|SELECT s.code, s.sictype, s.description
1342 WHERE s.code = '$form->{code}'|;
1343 my $sth = $dbh->prepare($query);
1344 $sth->execute || $form->dberror($query);
1346 my $ref = $sth->fetchrow_hashref(NAME_lc);
1348 map { $form->{$_} = $ref->{$_} } keys %$ref;
1354 $main::lxdebug->leave_sub();
1358 $main::lxdebug->enter_sub();
1360 my ($self, $myconfig, $form) = @_;
1362 # connect to database
1363 my $dbh = $form->dbconnect($myconfig);
1365 $form->{code} =~ s/\'/\'\'/g;
1366 $form->{description} =~ s/\'/\'\'/g;
1370 $query = qq|UPDATE sic SET
1371 code = '$form->{code}',
1372 sictype = '$form->{sictype}',
1373 description = '$form->{description}'
1374 WHERE code = '$form->{id}'|;
1376 $query = qq|INSERT INTO sic
1377 (code, sictype, description)
1378 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1380 $dbh->do($query) || $form->dberror($query);
1384 $main::lxdebug->leave_sub();
1388 $main::lxdebug->enter_sub();
1390 my ($self, $myconfig, $form) = @_;
1392 # connect to database
1393 my $dbh = $form->dbconnect($myconfig);
1395 $query = qq|DELETE FROM sic
1396 WHERE code = '$form->{code}'|;
1397 $dbh->do($query) || $form->dberror($query);
1401 $main::lxdebug->leave_sub();
1405 $main::lxdebug->enter_sub();
1407 my ($self, $form) = @_;
1409 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1411 while (<TEMPLATE>) {
1412 $form->{body} .= $_;
1417 $main::lxdebug->leave_sub();
1421 $main::lxdebug->enter_sub();
1423 my ($self, $form) = @_;
1425 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1428 $form->{body} =~ s/\r\n/\n/g;
1429 print TEMPLATE $form->{body};
1433 $main::lxdebug->leave_sub();
1436 sub save_preferences {
1437 $main::lxdebug->enter_sub();
1439 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1441 map { ($form->{$_}) = split /--/, $form->{$_} }
1442 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1445 $form->{curr} =~ s/ //g;
1446 map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
1447 $form->{curr} = join ':', @a;
1449 # connect to database
1450 my $dbh = $form->dbconnect_noauto($myconfig);
1452 # these defaults are database wide
1453 # user specific variables are in myconfig
1455 my $query = qq|UPDATE defaults SET
1456 inventory_accno_id =
1457 (SELECT c.id FROM chart c
1458 WHERE c.accno = '$form->{inventory_accno}'),
1460 (SELECT c.id FROM chart c
1461 WHERE c.accno = '$form->{income_accno}'),
1463 (SELECT c.id FROM chart c
1464 WHERE c.accno = '$form->{expense_accno}'),
1466 (SELECT c.id FROM chart c
1467 WHERE c.accno = '$form->{fxgain_accno}'),
1469 (SELECT c.id FROM chart c
1470 WHERE c.accno = '$form->{fxloss_accno}'),
1471 invnumber = '$form->{invnumber}',
1472 cnnumber = '$form->{cnnumber}',
1473 sonumber = '$form->{sonumber}',
1474 ponumber = '$form->{ponumber}',
1475 sqnumber = '$form->{sqnumber}',
1476 rfqnumber = '$form->{rfqnumber}',
1477 customernumber = '$form->{customernumber}',
1478 vendornumber = '$form->{vendornumber}',
1479 articlenumber = '$form->{articlenumber}',
1480 servicenumber = '$form->{servicenumber}',
1481 yearend = '$form->{yearend}',
1482 curr = '$form->{curr}',
1483 weightunit = '$form->{weightunit}',
1484 businessnumber = '$form->{businessnumber}'
1486 $dbh->do($query) || $form->dberror($query);
1489 my $name = $form->{name};
1490 $name =~ s/\'/\'\'/g;
1491 $query = qq|UPDATE employee
1493 WHERE login = '$form->{login}'|;
1494 $dbh->do($query) || $form->dberror($query);
1496 foreach my $item (split / /, $form->{taxaccounts}) {
1497 $query = qq|UPDATE tax
1498 SET rate = | . ($form->{$item} / 100) . qq|,
1499 taxnumber = '$form->{"taxnumber_$item"}'
1500 WHERE chart_id = $item|;
1501 $dbh->do($query) || $form->dberror($query);
1504 my $rc = $dbh->commit;
1507 # save first currency in myconfig
1508 $form->{currency} = substr($form->{curr}, 0, 3);
1510 my $myconfig = new User "$memberfile", "$form->{login}";
1512 foreach my $item (keys %$form) {
1513 $myconfig->{$item} = $form->{$item};
1516 $myconfig->save_member($memberfile, $userspath);
1520 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1521 foreach $directory (@webdavdirs) {
1522 $file = "webdav/" . $directory . "/webdav-user";
1523 if ($myconfig->{$directory}) {
1524 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1525 while (<HTACCESS>) {
1526 ($login, $password) = split(/:/, $_);
1527 if ($login ne $form->{login}) {
1532 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1533 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1534 print(HTACCESS $newfile);
1537 $form->{$directory} = 0;
1538 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1539 while (<HTACCESS>) {
1540 ($login, $password) = split(/:/, $_);
1541 if ($login ne $form->{login}) {
1546 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1547 print(HTACCESS $newfile);
1553 $main::lxdebug->leave_sub();
1558 sub defaultaccounts {
1559 $main::lxdebug->enter_sub();
1561 my ($self, $myconfig, $form) = @_;
1563 # connect to database
1564 my $dbh = $form->dbconnect($myconfig);
1566 # get defaults from defaults table
1567 my $query = qq|SELECT * FROM defaults|;
1568 my $sth = $dbh->prepare($query);
1569 $sth->execute || $form->dberror($query);
1571 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1572 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1573 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1574 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1575 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1576 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1580 $query = qq|SELECT c.id, c.accno, c.description, c.link
1582 WHERE c.link LIKE '%IC%'
1584 $sth = $dbh->prepare($query);
1585 $sth->execute || $self->dberror($query);
1587 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1588 foreach my $key (split(/:/, $ref->{link})) {
1591 if ($key =~ /cogs/) {
1592 $nkey = "IC_expense";
1594 if ($key =~ /sale/) {
1595 $nkey = "IC_income";
1597 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1599 description => $ref->{description}
1606 $query = qq|SELECT c.id, c.accno, c.description
1608 WHERE c.category = 'I'
1609 AND c.charttype = 'A'
1611 $sth = $dbh->prepare($query);
1612 $sth->execute || $self->dberror($query);
1614 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1615 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1617 description => $ref->{description}
1622 $query = qq|SELECT c.id, c.accno, c.description
1624 WHERE c.category = 'E'
1625 AND c.charttype = 'A'
1627 $sth = $dbh->prepare($query);
1628 $sth->execute || $self->dberror($query);
1630 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1631 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1633 description => $ref->{description}
1638 # now get the tax rates and numbers
1639 $query = qq|SELECT c.id, c.accno, c.description,
1640 t.rate * 100 AS rate, t.taxnumber
1642 WHERE c.id = t.chart_id|;
1644 $sth = $dbh->prepare($query);
1645 $sth->execute || $form->dberror($query);
1647 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1648 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1649 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1650 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1651 if $ref->{taxnumber};
1652 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1658 $main::lxdebug->leave_sub();
1662 $main::lxdebug->enter_sub();
1664 my ($self, $myconfig, $form, $userspath) = @_;
1668 my $boundary = time;
1670 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1671 my $out = $form->{OUT};
1672 $form->{OUT} = ">$tmpfile";
1674 if ($form->{media} eq 'email') {
1679 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1680 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1682 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1683 @{ $mail->{attachments} } = ($tmpfile);
1684 $mail->{version} = $form->{version};
1685 $mail->{fileid} = "$boundary.";
1687 $myconfig->{signature} =~ s/\\n/\r\n/g;
1688 $mail->{message} = "--\n$myconfig->{signature}";
1692 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1694 # get sequences, functions and triggers
1695 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1708 # Remove DOS and Unix style line endings.
1711 # ignore comments or empty lines
1712 next if /^(--.*|\s+)$/;
1714 for (my $i = 0; $i < length($_); $i++) {
1715 my $char = substr($_, $i, 1);
1717 # Are we inside a string?
1719 if ($char eq $quote_chars[-1]) {
1725 if (($char eq "'") || ($char eq "\"")) {
1726 push(@quote_chars, $char);
1728 } elsif ($char eq ";") {
1730 # Query is complete. Check for triggers and functions.
1731 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1732 push(@functions, $query);
1734 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1735 push(@triggers, $query);
1737 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1738 push(@sequences, $1);
1740 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1741 $tablespecs{$1} = $query;
1743 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1744 push(@indices, $query);
1758 # connect to database
1759 my $dbh = $form->dbconnect($myconfig);
1761 # get all the tables
1762 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1764 my $today = scalar localtime;
1766 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1768 print OUT qq|-- Lx-Office Backup
1769 -- Dataset: $myconfig->{dbname}
1770 -- Version: $form->{dbversion}
1771 -- Host: $myconfig->{dbhost}
1772 -- Login: $form->{login}
1773 -- User: $myconfig->{name}
1777 $myconfig->{dboptions};
1781 print OUT "-- DROP Sequences\n";
1783 foreach $item (@sequences) {
1784 print OUT qq|DROP SEQUENCE $item;\n|;
1787 print OUT "-- DROP Triggers\n";
1789 foreach $item (@triggers) {
1790 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1791 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1795 print OUT "-- DROP Functions\n";
1797 foreach $item (@functions) {
1798 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1799 print OUT qq|DROP FUNCTION "$1" ();\n|;
1803 foreach $table (@tables) {
1804 if (!($table =~ /^sql_.*/)) {
1805 my $query = qq|SELECT * FROM $table|;
1807 my $sth = $dbh->prepare($query);
1808 $sth->execute || $form->dberror($query);
1810 $query = "INSERT INTO $table (";
1811 map { $query .= qq|$sth->{NAME}->[$_],| }
1812 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1815 $query .= ") VALUES";
1817 if ($tablespecs{$table}) {
1819 print(OUT "DROP TABLE $table;\n");
1820 print(OUT $tablespecs{$table}, ";\n");
1823 print(OUT "DELETE FROM $table;\n");
1825 while (my @arr = $sth->fetchrow_array) {
1828 foreach my $item (@arr) {
1829 if (defined $item) {
1830 $item =~ s/\'/\'\'/g;
1831 $fields .= qq|'$item',|;
1840 print OUT qq|$query $fields;\n|;
1847 # create indices, sequences, functions and triggers
1849 print(OUT "-- CREATE Indices\n");
1850 map({ print(OUT "$_;\n"); } @indices);
1852 print OUT "-- CREATE Sequences\n";
1853 foreach $item (@sequences) {
1854 $query = qq|SELECT last_value FROM $item|;
1855 $sth = $dbh->prepare($query);
1856 $sth->execute || $form->dberror($query);
1857 my ($id) = $sth->fetchrow_array;
1861 CREATE SEQUENCE $item START $id;
1865 print OUT "-- CREATE Functions\n";
1868 map { print(OUT $_, ";\n"); } @functions;
1870 print OUT "-- CREATE Triggers\n";
1873 map { print(OUT $_, ";\n"); } @triggers;
1880 my @args = ("gzip", "$tmpfile");
1881 system(@args) == 0 or $form->error("$args[0] : $?");
1885 if ($form->{media} eq 'email') {
1886 @{ $mail->{attachments} } = ($tmpfile);
1887 $err = $mail->send($out);
1890 if ($form->{media} eq 'file') {
1892 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1893 open(OUT, ">-") or $form->error("STDOUT : $!");
1895 print OUT qq|Content-Type: application/x-tar-gzip;
1896 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1911 $main::lxdebug->leave_sub();
1915 $main::lxdebug->enter_sub();
1917 my ($self, $myconfig, $form) = @_;
1919 my $dbh = $form->dbconnect($myconfig);
1921 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1922 my $sth = $dbh->prepare($query);
1923 $sth->execute || $form->dberror($query);
1925 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1931 $main::lxdebug->leave_sub();
1935 $main::lxdebug->enter_sub();
1937 my ($self, $myconfig, $form) = @_;
1939 my $dbh = $form->dbconnect($myconfig);
1941 if ($form->{revtrans}) {
1943 $query = qq|UPDATE defaults SET closedto = NULL,
1946 if ($form->{closedto}) {
1948 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1952 $query = qq|UPDATE defaults SET closedto = NULL,
1957 # set close in defaults
1958 $dbh->do($query) || $form->dberror($query);
1962 $main::lxdebug->leave_sub();
1966 my ($self, $units, $unit_name, $factor) = @_;
1968 $factor = 1 unless ($factor);
1970 my $unit = $units->{$unit_name};
1972 if (!defined($unit) || !$unit->{"base_unit"} ||
1973 ($unit_name eq $unit->{"base_unit"})) {
1974 return ($unit_name, $factor);
1977 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1980 sub retrieve_units {
1981 $main::lxdebug->enter_sub();
1983 my ($self, $myconfig, $form, $type, $prefix) = @_;
1985 my $dbh = $form->dbconnect($myconfig);
1987 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1990 $query .= " WHERE (type = ?)";
1994 my $sth = $dbh->prepare($query);
1995 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1998 while (my $ref = $sth->fetchrow_hashref()) {
1999 $units->{$ref->{"name"}} = $ref;
2003 foreach my $unit (keys(%{$units})) {
2004 ($units->{$unit}->{"${prefix}base_unit"}, $units->{$unit}->{"${prefix}factor"}) = AM->get_base_unit($units, $unit);
2009 $main::lxdebug->leave_sub();
2015 $main::lxdebug->enter_sub();
2017 my ($self, $myconfig, $form, $units) = @_;
2019 my $dbh = $form->dbconnect($myconfig);
2021 foreach my $unit (values(%{$units})) {
2022 my $base_unit = $unit->{"original_base_unit"};
2023 while ($base_unit) {
2024 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
2025 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
2026 $base_unit = $units->{$base_unit}->{"original_base_unit"};
2030 foreach my $unit (values(%{$units})) {
2031 $unit->{"in_use"} = 0;
2032 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2034 foreach my $table (qw(parts invoice orderitems)) {
2035 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2037 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2038 $query .= "= " . $dbh->quote($unit->{"name"});
2040 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2043 my ($count) = $dbh->selectrow_array($query);
2044 $form->dberror($query) if ($dbh->err);
2047 $unit->{"in_use"} = 1;
2055 $main::lxdebug->leave_sub();
2058 sub unit_select_data {
2059 $main::lxdebug->enter_sub();
2061 my ($self, $units, $selected, $empty_entry) = @_;
2066 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2069 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2070 push(@{$select}, { "name" => $unit,
2071 "base_unit" => $units->{$unit}->{"base_unit"},
2072 "factor" => $units->{$unit}->{"factor"},
2073 "selected" => ($unit eq $selected) ? "selected" : "" });
2076 $main::lxdebug->leave_sub();
2081 sub unit_select_html {
2082 $main::lxdebug->enter_sub();
2084 my ($self, $units, $name, $selected, $convertible_into) = @_;
2086 my $select = "<select name=${name}>";
2088 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2089 if (!$convertible_into ||
2090 ($units->{$convertible_into} &&
2091 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2092 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2095 $select .= "</select>";
2097 $main::lxdebug->leave_sub();
2103 $main::lxdebug->enter_sub();
2105 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type) = @_;
2107 my $dbh = $form->dbconnect($myconfig);
2109 my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
2110 $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
2113 $main::lxdebug->leave_sub();
2117 $main::lxdebug->enter_sub();
2119 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2121 my $dbh = $form->dbconnect_noauto($myconfig);
2123 my ($base_unit, $unit, $sth, $query);
2125 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2126 $query = "DELETE FROM units WHERE name = ?";
2127 $sth = $dbh->prepare($query);
2128 map({ $sth->execute($_) || $form->dberror($query . " ($_)"); } @{$delete_units});
2132 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2133 $sth = $dbh->prepare($query);
2135 foreach $unit (values(%{$units})) {
2136 $unit->{"depth"} = 0;
2137 my $base_unit = $unit;
2138 while ($base_unit->{"base_unit"}) {
2140 $base_unit = $units->{$base_unit->{"base_unit"}};
2144 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2145 next if ($unit->{"unchanged_unit"});
2147 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2148 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2155 $main::lxdebug->leave_sub();