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 #======================================================================
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->dbconnect($myconfig);
51 qq!SELECT c.accno, c.description, c.charttype, c.category,! .
52 qq! c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from, ! .
54 qq! tk.taxkey_id, tk.pos_ustva, tk.tax_id, ! .
55 qq! tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate ! .
57 qq!LEFT JOIN taxkeys tk ! .
58 qq!ON (c.id=tk.chart_id AND tk.id = ! .
59 qq! (SELECT id FROM taxkeys ! .
60 qq! WHERE taxkeys.chart_id = c.id AND startdate <= current_date ! .
61 qq! ORDER BY startdate DESC LIMIT 1)) ! .
64 my $sth = $dbh->prepare($query);
65 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
67 my $ref = $sth->fetchrow_hashref(NAME_lc);
69 foreach my $key (keys %$ref) {
70 $form->{"$key"} = $ref->{"$key"};
75 # get default accounts
76 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
78 $sth = $dbh->prepare($query);
79 $sth->execute || $form->dberror($query);
81 $ref = $sth->fetchrow_hashref(NAME_lc);
83 map { $form->{$_} = $ref->{$_} } keys %ref;
87 # get taxkeys and description
88 $query = qq§SELECT id, taxkey,id||'--'||taxkey AS tax, taxdescription
89 FROM tax ORDER BY taxkey§;
90 $sth = $dbh->prepare($query);
91 $sth->execute || $form->dberror($query);
95 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
96 push @{ $form->{TAXKEY} }, $ref;
102 $query = qq|SELECT id, accno,description
103 FROM chart WHERE link = ?|;
104 $sth = $dbh->prepare($query);
105 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
107 $form->{NEWACCOUNT} = [];
108 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
109 push @{ $form->{NEWACCOUNT} }, $ref;
114 # check if we have any transactions
115 $query = qq|SELECT a.trans_id FROM acc_trans a
116 WHERE a.chart_id = ?|;
117 $sth = $dbh->prepare($query);
118 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
120 ($form->{orphaned}) = $sth->fetchrow_array;
121 $form->{orphaned} = !$form->{orphaned};
124 # check if new account is active
125 $form->{new_chart_valid} = 0;
126 if ($form->{new_chart_id}) {
127 $query = qq|SELECT current_date-valid_from FROM chart
129 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
131 $form->{new_chart_valid} = 1;
138 $main::lxdebug->leave_sub();
142 $main::lxdebug->enter_sub();
144 my ($self, $myconfig, $form) = @_;
146 # connect to database, turn off AutoCommit
147 my $dbh = $form->dbconnect_noauto($myconfig);
149 # sanity check, can't have AR with AR_...
150 if ($form->{AR} || $form->{AP} || $form->{IC}) {
151 map { delete $form->{$_} }
152 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);
156 foreach my $item ($form->{AR}, $form->{AR_amount},
157 $form->{AR_tax}, $form->{AR_paid},
158 $form->{AP}, $form->{AP_amount},
159 $form->{AP_tax}, $form->{AP_paid},
160 $form->{IC}, $form->{IC_sale},
161 $form->{IC_cogs}, $form->{IC_taxpart},
162 $form->{IC_income}, $form->{IC_expense},
163 $form->{IC_taxservice}, $form->{CT_tax}
165 $form->{link} .= "${item}:" if ($item);
169 # strip blanks from accno
170 map { $form->{$_} =~ s/ //g; } qw(accno);
174 if ($form->{id} eq "NULL") {
180 my ($tax_id, $taxkey) = split(/--/, $form->{tax});
181 my $startdate = $form->{startdate} ? $form->{startdate} : "1970-01-01";
184 $query = qq|UPDATE chart SET
185 accno = ?, description = ?, charttype = ?,
186 category = ?, link = ?,
188 pos_ustva = ?, pos_bwa = ?, pos_bilanz = ?,
189 pos_eur = ?, new_chart_id = ?, valid_from = ?
191 @values = ($form->{accno}, $form->{description}, $form->{charttype},
192 $form->{category}, $form->{link},
194 conv_i($form->{pos_ustva}), conv_i($form->{pos_bwa}),
195 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
196 conv_i($form->{new_chart_id}),
197 conv_date($form->{valid_from}),
200 } elsif ($form->{id} && !$form->{new_chart_valid}) {
201 $query = qq|UPDATE chart SET new_chart_id = ?, valid_from = ?
203 @values = (conv_i($form->{new_chart_id}), conv_date($form->{valid_from}),
206 $query = qq|INSERT INTO chart
207 (accno, description, charttype,
208 category, link, taxkey_id,
209 pos_ustva, pos_bwa, pos_bilanz, pos_eur,
210 new_chart_id, valid_from)
211 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
212 @values = ($form->{accno}, $form->{description}, $form->{charttype},
213 $form->{category}, $form->{link}, conv_i($taxkey),
214 conv_i($form->{pos_ustva}), conv_i($form->{pos_bwa}),
215 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
216 conv_i($form->{new_chart_id}),
217 conv_date($form->{valid_from}));
220 do_query($form, $dbh, $query, @values);
225 qq|INSERT INTO taxkeys | .
226 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
227 qq|VALUES ((SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
228 do_query($form, $dbh, $query,
229 $form->{accno}, conv_i($tax_id), conv_i($taxkey),
230 conv_i($form->{pos_ustva}), conv_date($startdate));
233 $query = qq|DELETE FROM taxkeys WHERE chart_id = ? AND tax_id = ?|;
234 do_query($form, $dbh, $query, $form->{id}, conv_i($tax_id));
237 qq|INSERT INTO taxkeys | .
238 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
239 qq|VALUES (?, ?, ?, ?, ?)|;
240 do_query($form, $dbh, $query,
241 $form->{id}, conv_i($tax_id), conv_i($taxkey),
242 conv_i($form->{pos_ustva}), conv_date($startdate));
246 my $rc = $dbh->commit;
249 $main::lxdebug->leave_sub();
255 $main::lxdebug->enter_sub();
257 my ($self, $myconfig, $form) = @_;
259 # connect to database, turn off AutoCommit
260 my $dbh = $form->dbconnect_noauto($myconfig);
262 my $query = qq|SELECT count(*) FROM acc_trans a
263 WHERE a.chart_id = ?|;
264 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
268 $main::lxdebug->leave_sub();
272 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
273 foreach my $type (qw(inventory income expense)) {
276 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
277 qq|WHERE ${type}_accno_id = ?|;
278 do_query($form, $dbh, $query, $form->{id});
281 foreach my $table (qw(partstax customertax vendortax tax)) {
282 $query = qq|DELETE FROM $table
284 do_query($form, $dbh, $query, $form->{id});
287 # delete chart of account record
288 $query = qq|DELETE FROM chart
290 do_query($form, $dbh, $query, $form->{id});
292 # commit and redirect
293 my $rc = $dbh->commit;
296 $main::lxdebug->leave_sub();
302 $main::lxdebug->enter_sub();
304 my ($self, $myconfig, $form) = @_;
306 # connect to database
307 my $dbh = $form->dbconnect($myconfig);
309 my $query = qq|SELECT d.id, d.description, d.role
313 $sth = $dbh->prepare($query);
314 $sth->execute || $form->dberror($query);
317 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
318 push @{ $form->{ALL} }, $ref;
324 $main::lxdebug->leave_sub();
328 $main::lxdebug->enter_sub();
330 my ($self, $myconfig, $form) = @_;
332 # connect to database
333 my $dbh = $form->dbconnect($myconfig);
335 my $query = qq|SELECT d.description, d.role
338 my $sth = $dbh->prepare($query);
339 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
341 my $ref = $sth->fetchrow_hashref(NAME_lc);
343 map { $form->{$_} = $ref->{$_} } keys %$ref;
347 # see if it is in use
348 $query = qq|SELECT count(*) FROM dpt_trans d
349 WHERE d.department_id = ?|;
350 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
352 $form->{orphaned} = !$form->{orphaned};
357 $main::lxdebug->leave_sub();
360 sub save_department {
361 $main::lxdebug->enter_sub();
363 my ($self, $myconfig, $form) = @_;
365 # connect to database
366 my $dbh = $form->dbconnect($myconfig);
368 my @values = ($form->{description}, $form->{role});
370 $query = qq|UPDATE department SET
371 description = ?, role = ?
373 push(@values, $form->{id});
375 $query = qq|INSERT INTO department
379 do_query($form, $dbh, $query, @values);
383 $main::lxdebug->leave_sub();
386 sub delete_department {
387 $main::lxdebug->enter_sub();
389 my ($self, $myconfig, $form) = @_;
391 # connect to database
392 my $dbh = $form->dbconnect($myconfig);
394 $query = qq|DELETE FROM department
396 do_query($form, $dbh, $query, $form->{id});
400 $main::lxdebug->leave_sub();
404 $main::lxdebug->enter_sub();
406 my ($self, $myconfig, $form) = @_;
408 # connect to database
409 my $dbh = $form->dbconnect($myconfig);
411 my $query = qq|SELECT id, lead
415 $sth = $dbh->prepare($query);
416 $sth->execute || $form->dberror($query);
419 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
420 push @{ $form->{ALL} }, $ref;
426 $main::lxdebug->leave_sub();
430 $main::lxdebug->enter_sub();
432 my ($self, $myconfig, $form) = @_;
434 # connect to database
435 my $dbh = $form->dbconnect($myconfig);
438 qq|SELECT l.id, l.lead | .
441 my $sth = $dbh->prepare($query);
442 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
444 my $ref = $sth->fetchrow_hashref(NAME_lc);
446 map { $form->{$_} = $ref->{$_} } keys %$ref;
452 $main::lxdebug->leave_sub();
456 $main::lxdebug->enter_sub();
458 my ($self, $myconfig, $form) = @_;
460 # connect to database
461 my $dbh = $form->dbconnect($myconfig);
463 my @values = ($form->{description});
464 # id is the old record
466 $query = qq|UPDATE leads SET
469 puhs(@values, $form->{id});
471 $query = qq|INSERT INTO leads
475 do_query($form, $dbh, $query, @values);
479 $main::lxdebug->leave_sub();
483 $main::lxdebug->enter_sub();
485 my ($self, $myconfig, $form) = @_;
487 # connect to database
488 my $dbh = $form->dbconnect($myconfig);
490 $query = qq|DELETE FROM leads
492 do_query($form, $dbh, $query, $form->{id});
496 $main::lxdebug->leave_sub();
500 $main::lxdebug->enter_sub();
502 my ($self, $myconfig, $form) = @_;
504 # connect to database
505 my $dbh = $form->dbconnect($myconfig);
507 my $query = qq|SELECT id, description, discount, customernumberinit
511 $sth = $dbh->prepare($query);
512 $sth->execute || $form->dberror($query);
515 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
516 push @{ $form->{ALL} }, $ref;
522 $main::lxdebug->leave_sub();
526 $main::lxdebug->enter_sub();
528 my ($self, $myconfig, $form) = @_;
530 # connect to database
531 my $dbh = $form->dbconnect($myconfig);
534 qq|SELECT b.description, b.discount, b.customernumberinit
537 my $sth = $dbh->prepare($query);
538 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
540 my $ref = $sth->fetchrow_hashref(NAME_lc);
542 map { $form->{$_} = $ref->{$_} } keys %$ref;
548 $main::lxdebug->leave_sub();
552 $main::lxdebug->enter_sub();
554 my ($self, $myconfig, $form) = @_;
556 # connect to database
557 my $dbh = $form->dbconnect($myconfig);
559 my @values = ($form->{description}, $form->{discount},
560 $form->{customernumberinit});
561 # id is the old record
563 $query = qq|UPDATE business SET
566 customernumberinit = ?
568 push(@values, $form->{id});
570 $query = qq|INSERT INTO business
571 (description, discount, customernumberinit)
574 do_query($form, $dbh, $query, @values);
578 $main::lxdebug->leave_sub();
581 sub delete_business {
582 $main::lxdebug->enter_sub();
584 my ($self, $myconfig, $form) = @_;
586 # connect to database
587 my $dbh = $form->dbconnect($myconfig);
589 $query = qq|DELETE FROM business
591 do_query($form, $dbh, $query, $form->{id});
595 $main::lxdebug->leave_sub();
600 $main::lxdebug->enter_sub();
602 my ($self, $myconfig, $form, $return_list) = @_;
604 # connect to database
605 my $dbh = $form->dbconnect($myconfig);
608 "SELECT id, description, template_code, article_code, " .
609 " output_numberformat, output_dateformat, output_longdates " .
610 "FROM language ORDER BY description";
612 $sth = $dbh->prepare($query);
613 $sth->execute || $form->dberror($query);
617 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
618 push(@{ $ary }, $ref);
624 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub();
636 my ($self, $myconfig, $form) = @_;
638 # connect to database
639 my $dbh = $form->dbconnect($myconfig);
642 "SELECT description, template_code, article_code, " .
643 " output_numberformat, output_dateformat, output_longdates " .
644 "FROM language WHERE id = ?";
645 my $sth = $dbh->prepare($query);
646 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
648 my $ref = $sth->fetchrow_hashref(NAME_lc);
650 map { $form->{$_} = $ref->{$_} } keys %$ref;
656 $main::lxdebug->leave_sub();
659 sub get_language_details {
660 $main::lxdebug->enter_sub();
662 my ($self, $myconfig, $form, $id) = @_;
664 # connect to database
665 my $dbh = $form->dbconnect($myconfig);
668 "SELECT template_code, " .
669 " output_numberformat, output_dateformat, output_longdates " .
670 "FROM language WHERE id = ?";
671 my @res = selectrow_query($form, $dbh, $query, $id);
674 $main::lxdebug->leave_sub();
680 $main::lxdebug->enter_sub();
682 my ($self, $myconfig, $form) = @_;
684 # connect to database
685 my $dbh = $form->dbconnect($myconfig);
686 my (@values, $query);
688 map({ push(@values, $form->{$_}); }
689 qw(description template_code article_code
690 output_numberformat output_dateformat output_longdates));
692 # id is the old record
695 "UPDATE language SET " .
696 " description = ?, template_code = ?, article_code = ?, " .
697 " output_numberformat = ?, output_dateformat = ?, " .
698 " output_longdates = ? " .
700 push(@values, $form->{id});
703 "INSERT INTO language (" .
704 " description, template_code, article_code, " .
705 " output_numberformat, output_dateformat, output_longdates" .
706 ") VALUES (?, ?, ?, ?, ?, ?)";
708 do_query($form, $dbh, $query, @values);
712 $main::lxdebug->leave_sub();
715 sub delete_language {
716 $main::lxdebug->enter_sub();
718 my ($self, $myconfig, $form) = @_;
720 # connect to database
721 my $dbh = $form->dbconnect_noauto($myconfig);
723 foreach my $table (qw(translation_payment_terms units_language)) {
724 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
725 do_query($form, $dbh, $query, $form->{"id"});
728 $query = "DELETE FROM language WHERE id = ?";
729 do_query($form, $dbh, $query, $form->{"id"});
734 $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,
748 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
750 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
752 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
754 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
756 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
758 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
760 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
762 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
764 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
768 $sth = $dbh->prepare($query);
769 $sth->execute || $form->dberror($query);
772 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
773 push @{ $form->{ALL} }, $ref;
779 $main::lxdebug->leave_sub();
782 sub get_buchungsgruppe {
783 $main::lxdebug->enter_sub();
785 my ($self, $myconfig, $form) = @_;
787 # connect to database
788 my $dbh = $form->dbconnect($myconfig);
792 qq|SELECT description, inventory_accno_id,
793 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
795 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
797 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
799 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
801 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
803 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
805 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
807 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
809 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
812 my $sth = $dbh->prepare($query);
813 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
815 my $ref = $sth->fetchrow_hashref(NAME_lc);
817 map { $form->{$_} = $ref->{$_} } keys %$ref;
822 qq|SELECT count(id) = 0 AS orphaned
824 WHERE buchungsgruppen_id = ?|;
825 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
828 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
830 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
831 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
834 $query = qq|SELECT c.accno, c.description, c.link, c.id,
835 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
836 FROM chart c, defaults d
837 WHERE c.link LIKE '%$module%'
841 my $sth = $dbh->prepare($query);
842 $sth->execute || $form->dberror($query);
843 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
844 foreach my $key (split(/:/, $ref->{link})) {
845 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
846 $form->{"std_inventory_accno_id"} = $ref->{"id"};
848 if ($key =~ /$module/) {
849 if ( ($ref->{id} eq $ref->{inventory_accno_id})
850 || ($ref->{id} eq $ref->{income_accno_id})
851 || ($ref->{id} eq $ref->{expense_accno_id})) {
852 push @{ $form->{"${module}_links"}{$key} },
853 { accno => $ref->{accno},
854 description => $ref->{description},
855 selected => "selected",
858 push @{ $form->{"${module}_links"}{$key} },
859 { accno => $ref->{accno},
860 description => $ref->{description},
872 $main::lxdebug->leave_sub();
875 sub save_buchungsgruppe {
876 $main::lxdebug->enter_sub();
878 my ($self, $myconfig, $form) = @_;
880 # connect to database
881 my $dbh = $form->dbconnect($myconfig);
883 my @values = ($form->{description}, $form->{inventory_accno_id},
884 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
885 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
886 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
887 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
891 # id is the old record
893 $query = qq|UPDATE buchungsgruppen SET
894 description = ?, inventory_accno_id = ?,
895 income_accno_id_0 = ?, expense_accno_id_0 = ?,
896 income_accno_id_1 = ?, expense_accno_id_1 = ?,
897 income_accno_id_2 = ?, expense_accno_id_2 = ?,
898 income_accno_id_3 = ?, expense_accno_id_3 = ?
900 push(@values, $form->{id});
902 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
903 my ($sortkey) = $dbh->selectrow_array($query);
904 $form->dberror($query) if ($dbh->err);
905 push(@values, $sortkey);
906 $query = qq|INSERT INTO buchungsgruppen
907 (description, inventory_accno_id,
908 income_accno_id_0, expense_accno_id_0,
909 income_accno_id_1, expense_accno_id_1,
910 income_accno_id_2, expense_accno_id_2,
911 income_accno_id_3, expense_accno_id_3,
913 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
915 do_query($form, $dbh, $query, @values);
919 $main::lxdebug->leave_sub();
922 sub delete_buchungsgruppe {
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 buchungsgruppen WHERE id = ?|;
931 do_query($form, $dbh, $query, $form->{id});
935 $main::lxdebug->leave_sub();
939 $main::lxdebug->enter_sub();
941 my ($self, $myconfig, $form, $table) = @_;
943 # connect to database
944 my $dbh = $form->dbconnect_noauto($myconfig);
948 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
949 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
950 my @values = ($form->{"id1"}, $form->{"id2"});
951 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
952 $main::lxdebug->dump(0, "v", \@values);
953 $main::lxdebug->dump(0, "s", \@sortkeys);
955 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
956 my $sth = $dbh->prepare($query);
957 $sth->execute($sortkeys[1], $form->{"id1"}) ||
958 $form->dberror($query . " ($sortkeys[1], $form->{id1})");
959 $sth->execute($sortkeys[0], $form->{"id2"}) ||
960 $form->dberror($query . " ($sortkeys[0], $form->{id2})");
966 $main::lxdebug->leave_sub();
970 $main::lxdebug->enter_sub();
972 my ($self, $myconfig, $form) = @_;
974 # connect to database
975 my $dbh = $form->dbconnect($myconfig);
977 my $query = qq|SELECT id, printer_description, template_code, printer_command
981 $sth = $dbh->prepare($query);
982 $sth->execute || $form->dberror($query);
985 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
986 push @{ $form->{ALL} }, $ref;
992 $main::lxdebug->leave_sub();
996 $main::lxdebug->enter_sub();
998 my ($self, $myconfig, $form) = @_;
1000 # connect to database
1001 my $dbh = $form->dbconnect($myconfig);
1004 qq|SELECT p.printer_description, p.template_code, p.printer_command
1007 my $sth = $dbh->prepare($query);
1008 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1010 my $ref = $sth->fetchrow_hashref(NAME_lc);
1012 map { $form->{$_} = $ref->{$_} } keys %$ref;
1018 $main::lxdebug->leave_sub();
1022 $main::lxdebug->enter_sub();
1024 my ($self, $myconfig, $form) = @_;
1026 # connect to database
1027 my $dbh = $form->dbconnect($myconfig);
1029 my @values = ($form->{printer_description},
1030 $form->{template_code},
1031 $form->{printer_command});
1033 # id is the old record
1035 $query = qq|UPDATE printers SET
1036 printer_description = ?, template_code = ?, printer_command = ?
1038 push(@values, $form->{id});
1040 $query = qq|INSERT INTO printers
1041 (printer_description, template_code, printer_command)
1044 do_query($form, $dbh, $query, @values);
1048 $main::lxdebug->leave_sub();
1051 sub delete_printer {
1052 $main::lxdebug->enter_sub();
1054 my ($self, $myconfig, $form) = @_;
1056 # connect to database
1057 my $dbh = $form->dbconnect($myconfig);
1059 $query = qq|DELETE FROM printers
1061 do_query($form, $dbh, $query, $form->{id});
1065 $main::lxdebug->leave_sub();
1069 $main::lxdebug->enter_sub();
1071 my ($self, $myconfig, $form) = @_;
1073 # connect to database
1074 my $dbh = $form->dbconnect($myconfig);
1076 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1078 $sth = $dbh->prepare($query);
1079 $sth->execute || $form->dberror($query);
1082 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1083 push @{ $form->{ALL} }, $ref;
1089 $main::lxdebug->leave_sub();
1093 $main::lxdebug->enter_sub();
1095 my ($self, $myconfig, $form) = @_;
1097 # connect to database
1098 my $dbh = $form->dbconnect($myconfig);
1100 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1101 my $sth = $dbh->prepare($query);
1102 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1104 my $ref = $sth->fetchrow_hashref(NAME_lc);
1105 map { $form->{$_} = $ref->{$_} } keys %$ref;
1109 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1110 qq|FROM translation_payment_terms t | .
1111 qq|LEFT JOIN language l ON t.language_id = l.id | .
1112 qq|WHERE t.payment_terms_id = ? | .
1114 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1115 qq| l.description AS language | .
1116 qq|FROM language l|;
1117 $sth = $dbh->prepare($query);
1118 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1121 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1122 $mapping{ $ref->{"language_id"} } = $ref
1123 unless (defined($mapping{ $ref->{"language_id"} }));
1127 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1132 $main::lxdebug->leave_sub();
1136 $main::lxdebug->enter_sub();
1138 my ($self, $myconfig, $form) = @_;
1140 # connect to database
1141 my $dbh = $form->dbconnect_noauto($myconfig);
1146 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1147 qq|FROM payment_terms|;
1149 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1151 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1152 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1156 qq|DELETE FROM translation_payment_terms | .
1157 qq|WHERE payment_terms_id = ?|;
1158 do_query($form, $dbh, $query, $form->{"id"});
1161 $query = qq|UPDATE payment_terms SET
1162 description = ?, description_long = ?,
1164 terms_netto = ?, terms_skonto = ?,
1167 my @values = ($form->{description}, $form->{description_long},
1168 $form->{ranking} * 1,
1169 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1170 $form->{percent_skonto} * 1,
1172 do_query($form, $dbh, $query, @values);
1174 $query = qq|SELECT id FROM language|;
1176 my $sth = $dbh->prepare($query);
1177 $sth->execute() || $form->dberror($query);
1179 while (my ($id) = $sth->fetchrow_array()) {
1180 push(@language_ids, $id);
1185 qq|INSERT INTO translation_payment_terms | .
1186 qq|(language_id, payment_terms_id, description_long) | .
1187 qq|VALUES (?, ?, ?)|;
1188 $sth = $dbh->prepare($query);
1190 foreach my $language_id (@language_ids) {
1191 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1192 $form->{"description_long_${language_id}"});
1199 $main::lxdebug->leave_sub();
1202 sub delete_payment {
1203 $main::lxdebug->enter_sub();
1205 my ($self, $myconfig, $form) = @_;
1207 # connect to database
1208 my $dbh = $form->dbconnect_noauto($myconfig);
1211 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1212 do_query($form, $dbh, $query, $form->{"id"});
1214 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1215 do_query($form, $dbh, $query, $form->{"id"});
1220 $main::lxdebug->leave_sub();
1224 $main::lxdebug->enter_sub();
1226 my ($self, $form) = @_;
1228 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1230 while (<TEMPLATE>) {
1231 $form->{body} .= $_;
1236 $main::lxdebug->leave_sub();
1240 $main::lxdebug->enter_sub();
1242 my ($self, $form) = @_;
1244 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1247 $form->{body} =~ s/\r\n/\n/g;
1248 print TEMPLATE $form->{body};
1252 $main::lxdebug->leave_sub();
1255 sub save_preferences {
1256 $main::lxdebug->enter_sub();
1258 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1260 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1261 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1264 $form->{curr} =~ s/ //g;
1265 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1266 $form->{curr} = join ':', @a;
1268 # connect to database
1269 my $dbh = $form->dbconnect_noauto($myconfig);
1271 # these defaults are database wide
1272 # user specific variables are in myconfig
1275 qq|UPDATE defaults SET | .
1276 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1277 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1278 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1279 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1280 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1281 qq|invnumber = ?, | .
1282 qq|cnnumber = ?, | .
1283 qq|sonumber = ?, | .
1284 qq|ponumber = ?, | .
1285 qq|sqnumber = ?, | .
1286 qq|rfqnumber = ?, | .
1287 qq|customernumber = ?, | .
1288 qq|vendornumber = ?, | .
1289 qq|articlenumber = ?, | .
1290 qq|servicenumber = ?, | .
1293 qq|businessnumber = ?|;
1294 my @values = ($form->{inventory_accno}, $form->{income_accno},
1295 $form->{expense_accno},
1296 $form->{fxgain_accno}, $form->{fxloss_accno},
1297 $form->{invnumber}, $form->{cnnumber},
1298 $form->{sonumber}, $form->{ponumber},
1299 $form->{sqnumber}, $form->{rfqnumber},
1300 $form->{customernumber}, $form->{vendornumber},
1301 $form->{articlenumber}, $form->{servicenumber},
1302 $form->{yearend}, $form->{curr},
1303 $form->{businessnumber});
1304 do_query($form, $dbh, $query, @values);
1307 $query = qq|UPDATE employee
1310 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1312 my $rc = $dbh->commit;
1315 # save first currency in myconfig
1316 $form->{currency} = substr($form->{curr}, 0, 3);
1318 my $myconfig = new User "$memberfile", "$form->{login}";
1320 foreach my $item (keys %$form) {
1321 $myconfig->{$item} = $form->{$item};
1324 $myconfig->save_member($memberfile, $userspath);
1328 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1329 foreach $directory (@webdavdirs) {
1330 $file = "webdav/" . $directory . "/webdav-user";
1331 if ($myconfig->{$directory}) {
1332 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1333 while (<HTACCESS>) {
1334 ($login, $password) = split(/:/, $_);
1335 if ($login ne $form->{login}) {
1340 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1341 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1342 print(HTACCESS $newfile);
1345 $form->{$directory} = 0;
1346 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1347 while (<HTACCESS>) {
1348 ($login, $password) = split(/:/, $_);
1349 if ($login ne $form->{login}) {
1354 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1355 print(HTACCESS $newfile);
1361 $main::lxdebug->leave_sub();
1366 sub defaultaccounts {
1367 $main::lxdebug->enter_sub();
1369 my ($self, $myconfig, $form) = @_;
1371 # connect to database
1372 my $dbh = $form->dbconnect($myconfig);
1374 # get defaults from defaults table
1375 my $query = qq|SELECT * FROM defaults|;
1376 my $sth = $dbh->prepare($query);
1377 $sth->execute || $form->dberror($query);
1379 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1380 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1381 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1382 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1383 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1384 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1388 $query = qq|SELECT c.id, c.accno, c.description, c.link
1390 WHERE c.link LIKE '%IC%'
1392 $sth = $dbh->prepare($query);
1393 $sth->execute || $self->dberror($query);
1395 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1396 foreach my $key (split(/:/, $ref->{link})) {
1399 if ($key =~ /cogs/) {
1400 $nkey = "IC_expense";
1402 if ($key =~ /sale/) {
1403 $nkey = "IC_income";
1405 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1407 description => $ref->{description}
1414 $query = qq|SELECT c.id, c.accno, c.description
1416 WHERE c.category = 'I'
1417 AND c.charttype = 'A'
1419 $sth = $dbh->prepare($query);
1420 $sth->execute || $self->dberror($query);
1422 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1423 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1425 description => $ref->{description}
1430 $query = qq|SELECT c.id, c.accno, c.description
1432 WHERE c.category = 'E'
1433 AND c.charttype = 'A'
1435 $sth = $dbh->prepare($query);
1436 $sth->execute || $self->dberror($query);
1438 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1439 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1441 description => $ref->{description}
1446 # now get the tax rates and numbers
1447 $query = qq|SELECT c.id, c.accno, c.description,
1448 t.rate * 100 AS rate, t.taxnumber
1450 WHERE c.id = t.chart_id|;
1452 $sth = $dbh->prepare($query);
1453 $sth->execute || $form->dberror($query);
1455 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1456 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1457 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1458 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1459 if $ref->{taxnumber};
1460 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1466 $main::lxdebug->leave_sub();
1470 $main::lxdebug->enter_sub();
1472 my ($self, $myconfig, $form) = @_;
1474 my $dbh = $form->dbconnect($myconfig);
1476 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1477 my $sth = $dbh->prepare($query);
1478 $sth->execute || $form->dberror($query);
1480 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1486 $main::lxdebug->leave_sub();
1490 $main::lxdebug->enter_sub();
1492 my ($self, $myconfig, $form) = @_;
1494 my $dbh = $form->dbconnect($myconfig);
1496 my ($query, @values);
1498 if ($form->{revtrans}) {
1499 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1501 } elsif ($form->{closedto}) {
1502 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1503 @values = (conv_date($form->{closedto}));
1506 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1509 # set close in defaults
1510 do_query($form, $dbh, $query, @values);
1514 $main::lxdebug->leave_sub();
1518 my ($self, $units, $unit_name, $factor) = @_;
1520 $factor = 1 unless ($factor);
1522 my $unit = $units->{$unit_name};
1524 if (!defined($unit) || !$unit->{"base_unit"} ||
1525 ($unit_name eq $unit->{"base_unit"})) {
1526 return ($unit_name, $factor);
1529 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1532 sub retrieve_units {
1533 $main::lxdebug->enter_sub();
1535 my ($self, $myconfig, $form, $type, $prefix) = @_;
1537 my $dbh = $form->dbconnect($myconfig);
1539 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1542 $query .= " WHERE (type = ?)";
1546 my $sth = $dbh->prepare($query);
1547 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1550 while (my $ref = $sth->fetchrow_hashref()) {
1551 $units->{$ref->{"name"}} = $ref;
1555 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1556 $sth = $dbh->prepare($query_lang);
1557 $sth->execute() || $form->dberror($query_lang);
1559 while ($ref = $sth->fetchrow_hashref()) {
1560 push(@languages, $ref);
1564 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1565 "FROM units_language ul " .
1566 "LEFT JOIN language l ON ul.language_id = l.id " .
1567 "WHERE ul.unit = ?";
1568 $sth = $dbh->prepare($query_lang);
1570 foreach my $unit (values(%{$units})) {
1571 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1573 $unit->{"LANGUAGES"} = {};
1574 foreach my $lang (@languages) {
1575 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1578 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1579 while ($ref = $sth->fetchrow_hashref()) {
1580 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1587 $main::lxdebug->leave_sub();
1592 sub translate_units {
1593 $main::lxdebug->enter_sub();
1595 my ($self, $form, $template_code, $unit, $amount) = @_;
1597 my $units = $self->retrieve_units(\%main::myconfig, $form);
1599 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1600 my $new_unit = $unit;
1602 if (($amount != 1) && $h->{"localized_plural"}) {
1603 $new_unit = $h->{"localized_plural"};
1604 } elsif ($h->{"localized"}) {
1605 $new_unit = $h->{"localized"};
1609 $main::lxdebug->leave_sub();
1615 $main::lxdebug->enter_sub();
1617 my ($self, $myconfig, $form, $units) = @_;
1619 my $dbh = $form->dbconnect($myconfig);
1621 map({ $_->{"in_use"} = 0; } values(%{$units}));
1623 foreach my $unit (values(%{$units})) {
1624 my $base_unit = $unit->{"original_base_unit"};
1625 while ($base_unit) {
1626 $units->{$base_unit}->{"in_use"} = 1;
1627 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1628 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1629 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1633 foreach my $unit (values(%{$units})) {
1634 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1636 foreach my $table (qw(parts invoice orderitems)) {
1637 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1639 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1640 $query .= "= " . $dbh->quote($unit->{"name"});
1642 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1643 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1646 my ($count) = $dbh->selectrow_array($query);
1647 $form->dberror($query) if ($dbh->err);
1650 $unit->{"in_use"} = 1;
1658 $main::lxdebug->leave_sub();
1661 sub unit_select_data {
1662 $main::lxdebug->enter_sub();
1664 my ($self, $units, $selected, $empty_entry) = @_;
1669 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1672 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1673 push(@{$select}, { "name" => $unit,
1674 "base_unit" => $units->{$unit}->{"base_unit"},
1675 "factor" => $units->{$unit}->{"factor"},
1676 "selected" => ($unit eq $selected) ? "selected" : "" });
1679 $main::lxdebug->leave_sub();
1684 sub unit_select_html {
1685 $main::lxdebug->enter_sub();
1687 my ($self, $units, $name, $selected, $convertible_into) = @_;
1689 my $select = "<select name=${name}>";
1691 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1692 if (!$convertible_into ||
1693 ($units->{$convertible_into} &&
1694 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1695 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1698 $select .= "</select>";
1700 $main::lxdebug->leave_sub();
1706 $main::lxdebug->enter_sub();
1708 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1710 my $dbh = $form->dbconnect_noauto($myconfig);
1712 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1713 my ($sortkey) = selectrow_query($form, $dbh, $query);
1715 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1716 "VALUES (?, ?, ?, ?, ?)";
1717 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1720 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1721 my $sth = $dbh->prepare($query);
1722 foreach my $lang (@{$languages}) {
1723 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1724 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1732 $main::lxdebug->leave_sub();
1736 $main::lxdebug->enter_sub();
1738 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1740 my $dbh = $form->dbconnect_noauto($myconfig);
1742 my ($base_unit, $unit, $sth, $query);
1744 $query = "DELETE FROM units_language";
1745 $dbh->do($query) || $form->dberror($query);
1747 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1748 $query = "DELETE FROM units WHERE name IN (";
1749 map({ $query .= "?," } @{$delete_units});
1750 substr($query, -1, 1) = ")";
1751 $dbh->do($query, undef, @{$delete_units}) ||
1752 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1755 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1756 $sth = $dbh->prepare($query);
1758 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1759 my $sth_lang = $dbh->prepare($query_lang);
1761 foreach $unit (values(%{$units})) {
1762 $unit->{"depth"} = 0;
1763 my $base_unit = $unit;
1764 while ($base_unit->{"base_unit"}) {
1766 $base_unit = $units->{$base_unit->{"base_unit"}};
1770 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1771 if ($unit->{"LANGUAGES"}) {
1772 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1773 next unless ($lang->{"id"} && $lang->{"localized"});
1774 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1775 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1779 next if ($unit->{"unchanged_unit"});
1781 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1782 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1786 $sth_lang->finish();
1790 $main::lxdebug->leave_sub();
1794 $main::lxdebug->enter_sub();
1796 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
1798 my $dbh = $form->dbconnect_noauto($myconfig);
1802 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
1803 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
1806 qq|SELECT sortkey FROM units | .
1807 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
1808 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
1809 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
1811 if (defined($sortkey_1)) {
1812 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
1813 my ($name_2) = selectrow_query($form, $dbh, $query);
1815 if (defined($name_2)) {
1816 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
1817 my $sth = $dbh->prepare($query);
1819 do_statement($form, $sth, $query, $sortkey_1, $name_2);
1820 do_statement($form, $sth, $query, $sortkey_2, $name_1);
1827 $main::lxdebug->leave_sub();