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.gifi_accno, 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 gifi_accno = ?, 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->{gifi_accno}, $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 gifi_accno, category, link,
210 pos_ustva, pos_bwa, pos_bilanz, pos_eur,
211 new_chart_id, valid_from)
212 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
213 @values = ($form->{accno}, $form->{description}, $form->{charttype},
214 $form->{gifi_accno}, $form->{category}, $form->{link},
216 conv_i($form->{pos_ustva}), conv_i($form->{pos_bwa}),
217 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
218 conv_i($form->{new_chart_id}),
219 conv_date($form->{valid_from}));
222 do_query($form, $dbh, $query, @values);
227 qq|INSERT INTO taxkeys | .
228 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
229 qq|VALUES ((SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
230 do_query($form, $dbh, $query,
231 $form->{accno}, conv_i($tax_id), conv_i($taxkey),
232 conv_i($form->{pos_ustva}), conv_date($startdate));
235 $query = qq|DELETE FROM taxkeys WHERE chart_id = ? AND tax_id = ?|;
236 do_query($form, $dbh, $query, $form->{id}, conv_i($tax_id));
239 qq|INSERT INTO taxkeys | .
240 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
241 qq|VALUES (?, ?, ?, ?, ?)|;
242 do_query($form, $dbh, $query,
243 $form->{id}, conv_i($tax_id), conv_i($taxkey),
244 conv_i($form->{pos_ustva}), conv_date($startdate));
248 my $rc = $dbh->commit;
251 $main::lxdebug->leave_sub();
257 $main::lxdebug->enter_sub();
259 my ($self, $myconfig, $form) = @_;
261 # connect to database, turn off AutoCommit
262 my $dbh = $form->dbconnect_noauto($myconfig);
264 my $query = qq|SELECT count(*) FROM acc_trans a
265 WHERE a.chart_id = ?|;
266 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
270 $main::lxdebug->leave_sub();
274 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
275 foreach my $type (qw(inventory income expense)) {
278 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
279 qq|WHERE ${type}_accno_id = ?|;
280 do_query($form, $dbh, $query, $form->{id});
283 foreach my $table (qw(partstax customertax vendortax tax)) {
284 $query = qq|DELETE FROM $table
286 do_query($form, $dbh, $query, $form->{id});
289 # delete chart of account record
290 $query = qq|DELETE FROM chart
292 do_query($form, $dbh, $query, $form->{id});
294 # commit and redirect
295 my $rc = $dbh->commit;
298 $main::lxdebug->leave_sub();
304 $main::lxdebug->enter_sub();
306 my ($self, $myconfig, $form) = @_;
308 # connect to database
309 my $dbh = $form->dbconnect($myconfig);
311 my $query = qq|SELECT d.id, d.description, d.role
315 $sth = $dbh->prepare($query);
316 $sth->execute || $form->dberror($query);
319 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
320 push @{ $form->{ALL} }, $ref;
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 my ($self, $myconfig, $form) = @_;
334 # connect to database
335 my $dbh = $form->dbconnect($myconfig);
337 my $query = qq|SELECT d.description, d.role
340 my $sth = $dbh->prepare($query);
341 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
343 my $ref = $sth->fetchrow_hashref(NAME_lc);
345 map { $form->{$_} = $ref->{$_} } keys %$ref;
349 # see if it is in use
350 $query = qq|SELECT count(*) FROM dpt_trans d
351 WHERE d.department_id = ?|;
352 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
354 $form->{orphaned} = !$form->{orphaned};
359 $main::lxdebug->leave_sub();
362 sub save_department {
363 $main::lxdebug->enter_sub();
365 my ($self, $myconfig, $form) = @_;
367 # connect to database
368 my $dbh = $form->dbconnect($myconfig);
370 my @values = ($form->{description}, $form->{role});
372 $query = qq|UPDATE department SET
373 description = ?, role = ?
375 push(@values, $form->{id});
377 $query = qq|INSERT INTO department
381 do_query($form, $dbh, $query, @values);
385 $main::lxdebug->leave_sub();
388 sub delete_department {
389 $main::lxdebug->enter_sub();
391 my ($self, $myconfig, $form) = @_;
393 # connect to database
394 my $dbh = $form->dbconnect($myconfig);
396 $query = qq|DELETE FROM department
398 do_query($form, $dbh, $query, $form->{id});
402 $main::lxdebug->leave_sub();
406 $main::lxdebug->enter_sub();
408 my ($self, $myconfig, $form) = @_;
410 # connect to database
411 my $dbh = $form->dbconnect($myconfig);
413 my $query = qq|SELECT id, lead
417 $sth = $dbh->prepare($query);
418 $sth->execute || $form->dberror($query);
421 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
422 push @{ $form->{ALL} }, $ref;
428 $main::lxdebug->leave_sub();
432 $main::lxdebug->enter_sub();
434 my ($self, $myconfig, $form) = @_;
436 # connect to database
437 my $dbh = $form->dbconnect($myconfig);
440 qq|SELECT l.id, l.lead | .
443 my $sth = $dbh->prepare($query);
444 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
446 my $ref = $sth->fetchrow_hashref(NAME_lc);
448 map { $form->{$_} = $ref->{$_} } keys %$ref;
454 $main::lxdebug->leave_sub();
458 $main::lxdebug->enter_sub();
460 my ($self, $myconfig, $form) = @_;
462 # connect to database
463 my $dbh = $form->dbconnect($myconfig);
465 my @values = ($form->{description});
466 # id is the old record
468 $query = qq|UPDATE leads SET
471 puhs(@values, $form->{id});
473 $query = qq|INSERT INTO leads
477 do_query($form, $dbh, $query, @values);
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my ($self, $myconfig, $form) = @_;
489 # connect to database
490 my $dbh = $form->dbconnect($myconfig);
492 $query = qq|DELETE FROM leads
494 do_query($form, $dbh, $query, $form->{id});
498 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
506 # connect to database
507 my $dbh = $form->dbconnect($myconfig);
509 my $query = qq|SELECT id, description, discount, customernumberinit
513 $sth = $dbh->prepare($query);
514 $sth->execute || $form->dberror($query);
517 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
518 push @{ $form->{ALL} }, $ref;
524 $main::lxdebug->leave_sub();
528 $main::lxdebug->enter_sub();
530 my ($self, $myconfig, $form) = @_;
532 # connect to database
533 my $dbh = $form->dbconnect($myconfig);
536 qq|SELECT b.description, b.discount, b.customernumberinit
539 my $sth = $dbh->prepare($query);
540 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
542 my $ref = $sth->fetchrow_hashref(NAME_lc);
544 map { $form->{$_} = $ref->{$_} } keys %$ref;
550 $main::lxdebug->leave_sub();
554 $main::lxdebug->enter_sub();
556 my ($self, $myconfig, $form) = @_;
558 # connect to database
559 my $dbh = $form->dbconnect($myconfig);
561 my @values = ($form->{description}, $form->{discount},
562 $form->{customernumberinit});
563 # id is the old record
565 $query = qq|UPDATE business SET
568 customernumberinit = ?
570 push(@values, $form->{id});
572 $query = qq|INSERT INTO business
573 (description, discount, customernumberinit)
576 do_query($form, $dbh, $query, @values);
580 $main::lxdebug->leave_sub();
583 sub delete_business {
584 $main::lxdebug->enter_sub();
586 my ($self, $myconfig, $form) = @_;
588 # connect to database
589 my $dbh = $form->dbconnect($myconfig);
591 $query = qq|DELETE FROM business
593 do_query($form, $dbh, $query, $form->{id});
597 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my ($self, $myconfig, $form, $return_list) = @_;
606 # connect to database
607 my $dbh = $form->dbconnect($myconfig);
610 "SELECT id, description, template_code, article_code, " .
611 " output_numberformat, output_dateformat, output_longdates " .
612 "FROM language ORDER BY description";
614 $sth = $dbh->prepare($query);
615 $sth->execute || $form->dberror($query);
619 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
620 push(@{ $ary }, $ref);
626 $main::lxdebug->leave_sub();
636 $main::lxdebug->enter_sub();
638 my ($self, $myconfig, $form) = @_;
640 # connect to database
641 my $dbh = $form->dbconnect($myconfig);
644 "SELECT description, template_code, article_code, " .
645 " output_numberformat, output_dateformat, output_longdates " .
646 "FROM language WHERE id = ?";
647 my $sth = $dbh->prepare($query);
648 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
650 my $ref = $sth->fetchrow_hashref(NAME_lc);
652 map { $form->{$_} = $ref->{$_} } keys %$ref;
658 $main::lxdebug->leave_sub();
661 sub get_language_details {
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form, $id) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
670 "SELECT template_code, " .
671 " output_numberformat, output_dateformat, output_longdates " .
672 "FROM language WHERE id = ?";
673 my @res = selectrow_query($form, $dbh, $query, $id);
676 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
684 my ($self, $myconfig, $form) = @_;
686 # connect to database
687 my $dbh = $form->dbconnect($myconfig);
688 my (@values, $query);
690 map({ push(@values, $form->{$_}); }
691 qw(description template_code article_code
692 output_numberformat output_dateformat output_longdates));
694 # id is the old record
697 "UPDATE language SET " .
698 " description = ?, template_code = ?, article_code = ?, " .
699 " output_numberformat = ?, output_dateformat = ?, " .
700 " output_longdates = ? " .
702 push(@values, $form->{id});
705 "INSERT INTO language (" .
706 " description, template_code, article_code, " .
707 " output_numberformat, output_dateformat, output_longdates" .
708 ") VALUES (?, ?, ?, ?, ?, ?)";
710 do_query($form, $dbh, $query, @values);
714 $main::lxdebug->leave_sub();
717 sub delete_language {
718 $main::lxdebug->enter_sub();
720 my ($self, $myconfig, $form) = @_;
722 # connect to database
723 my $dbh = $form->dbconnect_noauto($myconfig);
725 foreach my $table (qw(translation_payment_terms units_language)) {
726 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
727 do_query($form, $dbh, $query, $form->{"id"});
730 $query = "DELETE FROM language WHERE id = ?";
731 do_query($form, $dbh, $query, $form->{"id"});
736 $main::lxdebug->leave_sub();
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $form) = @_;
745 # connect to database
746 my $dbh = $form->dbconnect($myconfig);
748 my $query = qq|SELECT id, description,
750 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
752 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
754 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
756 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
758 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
760 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
762 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
764 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
766 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
770 $sth = $dbh->prepare($query);
771 $sth->execute || $form->dberror($query);
774 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
775 push @{ $form->{ALL} }, $ref;
781 $main::lxdebug->leave_sub();
784 sub get_buchungsgruppe {
785 $main::lxdebug->enter_sub();
787 my ($self, $myconfig, $form) = @_;
789 # connect to database
790 my $dbh = $form->dbconnect($myconfig);
794 qq|SELECT description, inventory_accno_id,
795 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
797 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
799 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
801 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
803 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
805 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
807 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
809 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
811 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
814 my $sth = $dbh->prepare($query);
815 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
817 my $ref = $sth->fetchrow_hashref(NAME_lc);
819 map { $form->{$_} = $ref->{$_} } keys %$ref;
824 qq|SELECT count(id) = 0 AS orphaned
826 WHERE buchungsgruppen_id = ?|;
827 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
830 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
832 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
833 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
836 $query = qq|SELECT c.accno, c.description, c.link, c.id,
837 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
838 FROM chart c, defaults d
839 WHERE c.link LIKE '%$module%'
843 my $sth = $dbh->prepare($query);
844 $sth->execute || $form->dberror($query);
845 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
846 foreach my $key (split(/:/, $ref->{link})) {
847 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
848 $form->{"std_inventory_accno_id"} = $ref->{"id"};
850 if ($key =~ /$module/) {
851 if ( ($ref->{id} eq $ref->{inventory_accno_id})
852 || ($ref->{id} eq $ref->{income_accno_id})
853 || ($ref->{id} eq $ref->{expense_accno_id})) {
854 push @{ $form->{"${module}_links"}{$key} },
855 { accno => $ref->{accno},
856 description => $ref->{description},
857 selected => "selected",
860 push @{ $form->{"${module}_links"}{$key} },
861 { accno => $ref->{accno},
862 description => $ref->{description},
874 $main::lxdebug->leave_sub();
877 sub save_buchungsgruppe {
878 $main::lxdebug->enter_sub();
880 my ($self, $myconfig, $form) = @_;
882 # connect to database
883 my $dbh = $form->dbconnect($myconfig);
885 my @values = ($form->{description}, $form->{inventory_accno_id},
886 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
887 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
888 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
889 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
893 # id is the old record
895 $query = qq|UPDATE buchungsgruppen SET
896 description = ?, inventory_accno_id = ?,
897 income_accno_id_0 = ?, expense_accno_id_0 = ?,
898 income_accno_id_1 = ?, expense_accno_id_1 = ?,
899 income_accno_id_2 = ?, expense_accno_id_2 = ?,
900 income_accno_id_3 = ?, expense_accno_id_3 = ?
902 push(@values, $form->{id});
904 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
905 my ($sortkey) = $dbh->selectrow_array($query);
906 $form->dberror($query) if ($dbh->err);
907 push(@values, $sortkey);
908 $query = qq|INSERT INTO buchungsgruppen
909 (description, inventory_accno_id,
910 income_accno_id_0, expense_accno_id_0,
911 income_accno_id_1, expense_accno_id_1,
912 income_accno_id_2, expense_accno_id_2,
913 income_accno_id_3, expense_accno_id_3,
915 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
917 do_query($form, $dbh, $query, @values);
921 $main::lxdebug->leave_sub();
924 sub delete_buchungsgruppe {
925 $main::lxdebug->enter_sub();
927 my ($self, $myconfig, $form) = @_;
929 # connect to database
930 my $dbh = $form->dbconnect($myconfig);
932 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
933 do_query($form, $dbh, $query, $form->{id});
937 $main::lxdebug->leave_sub();
941 $main::lxdebug->enter_sub();
943 my ($self, $myconfig, $form, $table) = @_;
945 # connect to database
946 my $dbh = $form->dbconnect_noauto($myconfig);
950 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
951 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
952 my @values = ($form->{"id1"}, $form->{"id2"});
953 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
954 $main::lxdebug->dump(0, "v", \@values);
955 $main::lxdebug->dump(0, "s", \@sortkeys);
957 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
958 my $sth = $dbh->prepare($query);
959 $sth->execute($sortkeys[1], $form->{"id1"}) ||
960 $form->dberror($query . " ($sortkeys[1], $form->{id1})");
961 $sth->execute($sortkeys[0], $form->{"id2"}) ||
962 $form->dberror($query . " ($sortkeys[0], $form->{id2})");
968 $main::lxdebug->leave_sub();
972 $main::lxdebug->enter_sub();
974 my ($self, $myconfig, $form) = @_;
976 # connect to database
977 my $dbh = $form->dbconnect($myconfig);
979 my $query = qq|SELECT id, printer_description, template_code, printer_command
983 $sth = $dbh->prepare($query);
984 $sth->execute || $form->dberror($query);
987 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
988 push @{ $form->{ALL} }, $ref;
994 $main::lxdebug->leave_sub();
998 $main::lxdebug->enter_sub();
1000 my ($self, $myconfig, $form) = @_;
1002 # connect to database
1003 my $dbh = $form->dbconnect($myconfig);
1006 qq|SELECT p.printer_description, p.template_code, p.printer_command
1009 my $sth = $dbh->prepare($query);
1010 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1012 my $ref = $sth->fetchrow_hashref(NAME_lc);
1014 map { $form->{$_} = $ref->{$_} } keys %$ref;
1020 $main::lxdebug->leave_sub();
1024 $main::lxdebug->enter_sub();
1026 my ($self, $myconfig, $form) = @_;
1028 # connect to database
1029 my $dbh = $form->dbconnect($myconfig);
1031 my @values = ($form->{printer_description},
1032 $form->{template_code},
1033 $form->{printer_command});
1035 # id is the old record
1037 $query = qq|UPDATE printers SET
1038 printer_description = ?, template_code = ?, printer_command = ?
1040 push(@values, $form->{id});
1042 $query = qq|INSERT INTO printers
1043 (printer_description, template_code, printer_command)
1046 do_query($form, $dbh, $query, @values);
1050 $main::lxdebug->leave_sub();
1053 sub delete_printer {
1054 $main::lxdebug->enter_sub();
1056 my ($self, $myconfig, $form) = @_;
1058 # connect to database
1059 my $dbh = $form->dbconnect($myconfig);
1061 $query = qq|DELETE FROM printers
1063 do_query($form, $dbh, $query, $form->{id});
1067 $main::lxdebug->leave_sub();
1071 $main::lxdebug->enter_sub();
1073 my ($self, $myconfig, $form) = @_;
1075 # connect to database
1076 my $dbh = $form->dbconnect($myconfig);
1078 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1080 $sth = $dbh->prepare($query);
1081 $sth->execute || $form->dberror($query);
1084 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1085 push @{ $form->{ALL} }, $ref;
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 * FROM payment_terms WHERE id = ?|;
1103 my $sth = $dbh->prepare($query);
1104 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1106 my $ref = $sth->fetchrow_hashref(NAME_lc);
1107 map { $form->{$_} = $ref->{$_} } keys %$ref;
1111 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1112 qq|FROM translation_payment_terms t | .
1113 qq|LEFT JOIN language l ON t.language_id = l.id | .
1114 qq|WHERE t.payment_terms_id = ? | .
1116 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1117 qq| l.description AS language | .
1118 qq|FROM language l|;
1119 $sth = $dbh->prepare($query);
1120 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1123 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1124 $mapping{ $ref->{"language_id"} } = $ref
1125 unless (defined($mapping{ $ref->{"language_id"} }));
1129 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1134 $main::lxdebug->leave_sub();
1138 $main::lxdebug->enter_sub();
1140 my ($self, $myconfig, $form) = @_;
1142 # connect to database
1143 my $dbh = $form->dbconnect_noauto($myconfig);
1148 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1149 qq|FROM payment_terms|;
1151 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1153 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1154 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1158 qq|DELETE FROM translation_payment_terms | .
1159 qq|WHERE payment_terms_id = ?|;
1160 do_query($form, $dbh, $query, $form->{"id"});
1163 $query = qq|UPDATE payment_terms SET
1164 description = ?, description_long = ?,
1166 terms_netto = ?, terms_skonto = ?,
1169 my @values = ($form->{description}, $form->{description_long},
1170 $form->{ranking} * 1,
1171 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1172 $form->{percent_skonto} * 1,
1174 do_query($form, $dbh, $query, @values);
1176 $query = qq|SELECT id FROM language|;
1178 my $sth = $dbh->prepare($query);
1179 $sth->execute() || $form->dberror($query);
1181 while (my ($id) = $sth->fetchrow_array()) {
1182 push(@language_ids, $id);
1187 qq|INSERT INTO translation_payment_terms | .
1188 qq|(language_id, payment_terms_id, description_long) | .
1189 qq|VALUES (?, ?, ?)|;
1190 $sth = $dbh->prepare($query);
1192 foreach my $language_id (@language_ids) {
1193 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1194 $form->{"description_long_${language_id}"});
1201 $main::lxdebug->leave_sub();
1204 sub delete_payment {
1205 $main::lxdebug->enter_sub();
1207 my ($self, $myconfig, $form) = @_;
1209 # connect to database
1210 my $dbh = $form->dbconnect_noauto($myconfig);
1213 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1214 do_query($form, $dbh, $query, $form->{"id"});
1216 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1217 do_query($form, $dbh, $query, $form->{"id"});
1222 $main::lxdebug->leave_sub();
1226 $main::lxdebug->enter_sub();
1228 my ($self, $form) = @_;
1230 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1232 while (<TEMPLATE>) {
1233 $form->{body} .= $_;
1238 $main::lxdebug->leave_sub();
1242 $main::lxdebug->enter_sub();
1244 my ($self, $form) = @_;
1246 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1249 $form->{body} =~ s/\r\n/\n/g;
1250 print TEMPLATE $form->{body};
1254 $main::lxdebug->leave_sub();
1257 sub save_preferences {
1258 $main::lxdebug->enter_sub();
1260 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1262 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1263 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1266 $form->{curr} =~ s/ //g;
1267 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1268 $form->{curr} = join ':', @a;
1270 # connect to database
1271 my $dbh = $form->dbconnect_noauto($myconfig);
1273 # these defaults are database wide
1274 # user specific variables are in myconfig
1277 qq|UPDATE defaults SET | .
1278 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1279 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1280 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1281 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1282 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1283 qq|invnumber = ?, | .
1284 qq|cnnumber = ?, | .
1285 qq|sonumber = ?, | .
1286 qq|ponumber = ?, | .
1287 qq|sqnumber = ?, | .
1288 qq|rfqnumber = ?, | .
1289 qq|customernumber = ?, | .
1290 qq|vendornumber = ?, | .
1291 qq|articlenumber = ?, | .
1292 qq|servicenumber = ?, | .
1295 qq|businessnumber = ?|;
1296 my @values = ($form->{inventory_accno}, $form->{income_accno},
1297 $form->{expense_accno},
1298 $form->{fxgain_accno}, $form->{fxloss_accno},
1299 $form->{invnumber}, $form->{cnnumber},
1300 $form->{sonumber}, $form->{ponumber},
1301 $form->{sqnumber}, $form->{rfqnumber},
1302 $form->{customernumber}, $form->{vendornumber},
1303 $form->{articlenumber}, $form->{servicenumber},
1304 $form->{yearend}, $form->{curr},
1305 $form->{businessnumber});
1306 do_query($form, $dbh, $query, @values);
1309 $query = qq|UPDATE employee
1312 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1314 my $rc = $dbh->commit;
1317 # save first currency in myconfig
1318 $form->{currency} = substr($form->{curr}, 0, 3);
1320 my $myconfig = new User "$memberfile", "$form->{login}";
1322 foreach my $item (keys %$form) {
1323 $myconfig->{$item} = $form->{$item};
1326 $myconfig->save_member($memberfile, $userspath);
1330 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1331 foreach $directory (@webdavdirs) {
1332 $file = "webdav/" . $directory . "/webdav-user";
1333 if ($myconfig->{$directory}) {
1334 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1335 while (<HTACCESS>) {
1336 ($login, $password) = split(/:/, $_);
1337 if ($login ne $form->{login}) {
1342 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1343 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1344 print(HTACCESS $newfile);
1347 $form->{$directory} = 0;
1348 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1349 while (<HTACCESS>) {
1350 ($login, $password) = split(/:/, $_);
1351 if ($login ne $form->{login}) {
1356 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1357 print(HTACCESS $newfile);
1363 $main::lxdebug->leave_sub();
1368 sub defaultaccounts {
1369 $main::lxdebug->enter_sub();
1371 my ($self, $myconfig, $form) = @_;
1373 # connect to database
1374 my $dbh = $form->dbconnect($myconfig);
1376 # get defaults from defaults table
1377 my $query = qq|SELECT * FROM defaults|;
1378 my $sth = $dbh->prepare($query);
1379 $sth->execute || $form->dberror($query);
1381 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1382 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1383 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1384 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1385 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1386 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1390 $query = qq|SELECT c.id, c.accno, c.description, c.link
1392 WHERE c.link LIKE '%IC%'
1394 $sth = $dbh->prepare($query);
1395 $sth->execute || $self->dberror($query);
1397 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1398 foreach my $key (split(/:/, $ref->{link})) {
1401 if ($key =~ /cogs/) {
1402 $nkey = "IC_expense";
1404 if ($key =~ /sale/) {
1405 $nkey = "IC_income";
1407 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1409 description => $ref->{description}
1416 $query = qq|SELECT c.id, c.accno, c.description
1418 WHERE c.category = 'I'
1419 AND c.charttype = 'A'
1421 $sth = $dbh->prepare($query);
1422 $sth->execute || $self->dberror($query);
1424 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1425 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1427 description => $ref->{description}
1432 $query = qq|SELECT c.id, c.accno, c.description
1434 WHERE c.category = 'E'
1435 AND c.charttype = 'A'
1437 $sth = $dbh->prepare($query);
1438 $sth->execute || $self->dberror($query);
1440 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1441 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1443 description => $ref->{description}
1448 # now get the tax rates and numbers
1449 $query = qq|SELECT c.id, c.accno, c.description,
1450 t.rate * 100 AS rate, t.taxnumber
1452 WHERE c.id = t.chart_id|;
1454 $sth = $dbh->prepare($query);
1455 $sth->execute || $form->dberror($query);
1457 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1458 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1459 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1460 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1461 if $ref->{taxnumber};
1462 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1468 $main::lxdebug->leave_sub();
1472 $main::lxdebug->enter_sub();
1474 my ($self, $myconfig, $form) = @_;
1476 my $dbh = $form->dbconnect($myconfig);
1478 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1479 my $sth = $dbh->prepare($query);
1480 $sth->execute || $form->dberror($query);
1482 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1488 $main::lxdebug->leave_sub();
1492 $main::lxdebug->enter_sub();
1494 my ($self, $myconfig, $form) = @_;
1496 my $dbh = $form->dbconnect($myconfig);
1498 my ($query, @values);
1500 if ($form->{revtrans}) {
1501 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1503 } elsif ($form->{closedto}) {
1504 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1505 @values = (conv_date($form->{closedto}));
1508 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1511 # set close in defaults
1512 do_query($form, $dbh, $query, @values);
1516 $main::lxdebug->leave_sub();
1520 my ($self, $units, $unit_name, $factor) = @_;
1522 $factor = 1 unless ($factor);
1524 my $unit = $units->{$unit_name};
1526 if (!defined($unit) || !$unit->{"base_unit"} ||
1527 ($unit_name eq $unit->{"base_unit"})) {
1528 return ($unit_name, $factor);
1531 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1534 sub retrieve_units {
1535 $main::lxdebug->enter_sub();
1537 my ($self, $myconfig, $form, $type, $prefix) = @_;
1539 my $dbh = $form->dbconnect($myconfig);
1541 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1544 $query .= " WHERE (type = ?)";
1548 my $sth = $dbh->prepare($query);
1549 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1552 while (my $ref = $sth->fetchrow_hashref()) {
1553 $units->{$ref->{"name"}} = $ref;
1557 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1558 $sth = $dbh->prepare($query_lang);
1559 $sth->execute() || $form->dberror($query_lang);
1561 while ($ref = $sth->fetchrow_hashref()) {
1562 push(@languages, $ref);
1566 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1567 "FROM units_language ul " .
1568 "LEFT JOIN language l ON ul.language_id = l.id " .
1569 "WHERE ul.unit = ?";
1570 $sth = $dbh->prepare($query_lang);
1572 foreach my $unit (values(%{$units})) {
1573 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1575 $unit->{"LANGUAGES"} = {};
1576 foreach my $lang (@languages) {
1577 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1580 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1581 while ($ref = $sth->fetchrow_hashref()) {
1582 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1589 $main::lxdebug->leave_sub();
1594 sub translate_units {
1595 $main::lxdebug->enter_sub();
1597 my ($self, $form, $template_code, $unit, $amount) = @_;
1599 my $units = $self->retrieve_units(\%main::myconfig, $form);
1601 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1602 my $new_unit = $unit;
1604 if (($amount != 1) && $h->{"localized_plural"}) {
1605 $new_unit = $h->{"localized_plural"};
1606 } elsif ($h->{"localized"}) {
1607 $new_unit = $h->{"localized"};
1611 $main::lxdebug->leave_sub();
1617 $main::lxdebug->enter_sub();
1619 my ($self, $myconfig, $form, $units) = @_;
1621 my $dbh = $form->dbconnect($myconfig);
1623 map({ $_->{"in_use"} = 0; } values(%{$units}));
1625 foreach my $unit (values(%{$units})) {
1626 my $base_unit = $unit->{"original_base_unit"};
1627 while ($base_unit) {
1628 $units->{$base_unit}->{"in_use"} = 1;
1629 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1630 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1631 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1635 foreach my $unit (values(%{$units})) {
1636 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1638 foreach my $table (qw(parts invoice orderitems)) {
1639 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1641 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1642 $query .= "= " . $dbh->quote($unit->{"name"});
1644 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1645 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1648 my ($count) = $dbh->selectrow_array($query);
1649 $form->dberror($query) if ($dbh->err);
1652 $unit->{"in_use"} = 1;
1660 $main::lxdebug->leave_sub();
1663 sub unit_select_data {
1664 $main::lxdebug->enter_sub();
1666 my ($self, $units, $selected, $empty_entry) = @_;
1671 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1674 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
1675 push(@{$select}, { "name" => $unit,
1676 "base_unit" => $units->{$unit}->{"base_unit"},
1677 "factor" => $units->{$unit}->{"factor"},
1678 "selected" => ($unit eq $selected) ? "selected" : "" });
1681 $main::lxdebug->leave_sub();
1686 sub unit_select_html {
1687 $main::lxdebug->enter_sub();
1689 my ($self, $units, $name, $selected, $convertible_into) = @_;
1691 my $select = "<select name=${name}>";
1693 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
1694 if (!$convertible_into ||
1695 ($units->{$convertible_into} &&
1696 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1697 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1700 $select .= "</select>";
1702 $main::lxdebug->leave_sub();
1708 $main::lxdebug->enter_sub();
1710 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1712 my $dbh = $form->dbconnect_noauto($myconfig);
1714 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1715 my ($sortkey) = selectrow_query($form, $dbh, $query);
1717 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1718 "VALUES (?, ?, ?, ?, ?)";
1719 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1722 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1723 my $sth = $dbh->prepare($query);
1724 foreach my $lang (@{$languages}) {
1725 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1726 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1734 $main::lxdebug->leave_sub();
1738 $main::lxdebug->enter_sub();
1740 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1742 my $dbh = $form->dbconnect_noauto($myconfig);
1744 my ($base_unit, $unit, $sth, $query);
1746 $query = "DELETE FROM units_language";
1747 $dbh->do($query) || $form->dberror($query);
1749 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1750 $query = "DELETE FROM units WHERE name IN (";
1751 map({ $query .= "?," } @{$delete_units});
1752 substr($query, -1, 1) = ")";
1753 $dbh->do($query, undef, @{$delete_units}) ||
1754 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1757 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1758 $sth = $dbh->prepare($query);
1760 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1761 my $sth_lang = $dbh->prepare($query_lang);
1763 foreach $unit (values(%{$units})) {
1764 $unit->{"depth"} = 0;
1765 my $base_unit = $unit;
1766 while ($base_unit->{"base_unit"}) {
1768 $base_unit = $units->{$base_unit->{"base_unit"}};
1772 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1773 if ($unit->{"LANGUAGES"}) {
1774 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1775 next unless ($lang->{"id"} && $lang->{"localized"});
1776 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1777 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1781 next if ($unit->{"unchanged_unit"});
1783 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1784 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1788 $sth_lang->finish();
1792 $main::lxdebug->leave_sub();
1796 $main::lxdebug->enter_sub();
1798 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
1800 my $dbh = $form->dbconnect_noauto($myconfig);
1804 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
1805 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
1808 qq|SELECT sortkey FROM units | .
1809 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
1810 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
1811 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
1813 if (defined($sortkey_1)) {
1814 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
1815 my ($name_2) = selectrow_query($form, $dbh, $query);
1817 if (defined($name_2)) {
1818 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
1819 my $sth = $dbh->prepare($query);
1821 do_statement($form, $sth, $query, $sortkey_1, $name_2);
1822 do_statement($form, $sth, $query, $sortkey_2, $name_1);
1829 $main::lxdebug->leave_sub();