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, salesman
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, b.salesman
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}, $form->{salesman} ? 't' : 'f');
563 # id is the old record
565 $query = qq|UPDATE business SET
568 customernumberinit = ?,
571 push(@values, $form->{id});
573 $query = qq|INSERT INTO business
574 (description, discount, customernumberinit, salesman)
575 VALUES (?, ?, ?, ?)|;
577 do_query($form, $dbh, $query, @values);
581 $main::lxdebug->leave_sub();
584 sub delete_business {
585 $main::lxdebug->enter_sub();
587 my ($self, $myconfig, $form) = @_;
589 # connect to database
590 my $dbh = $form->dbconnect($myconfig);
592 $query = qq|DELETE FROM business
594 do_query($form, $dbh, $query, $form->{id});
598 $main::lxdebug->leave_sub();
603 $main::lxdebug->enter_sub();
605 my ($self, $myconfig, $form, $return_list) = @_;
607 # connect to database
608 my $dbh = $form->dbconnect($myconfig);
611 "SELECT id, description, template_code, article_code, " .
612 " output_numberformat, output_dateformat, output_longdates " .
613 "FROM language ORDER BY description";
615 $sth = $dbh->prepare($query);
616 $sth->execute || $form->dberror($query);
620 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
621 push(@{ $ary }, $ref);
627 $main::lxdebug->leave_sub();
637 $main::lxdebug->enter_sub();
639 my ($self, $myconfig, $form) = @_;
641 # connect to database
642 my $dbh = $form->dbconnect($myconfig);
645 "SELECT description, template_code, article_code, " .
646 " output_numberformat, output_dateformat, output_longdates " .
647 "FROM language WHERE id = ?";
648 my $sth = $dbh->prepare($query);
649 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
651 my $ref = $sth->fetchrow_hashref(NAME_lc);
653 map { $form->{$_} = $ref->{$_} } keys %$ref;
659 $main::lxdebug->leave_sub();
662 sub get_language_details {
663 $main::lxdebug->enter_sub();
665 my ($self, $myconfig, $form, $id) = @_;
667 # connect to database
668 my $dbh = $form->dbconnect($myconfig);
671 "SELECT template_code, " .
672 " output_numberformat, output_dateformat, output_longdates " .
673 "FROM language WHERE id = ?";
674 my @res = selectrow_query($form, $dbh, $query, $id);
677 $main::lxdebug->leave_sub();
683 $main::lxdebug->enter_sub();
685 my ($self, $myconfig, $form) = @_;
687 # connect to database
688 my $dbh = $form->dbconnect($myconfig);
689 my (@values, $query);
691 map({ push(@values, $form->{$_}); }
692 qw(description template_code article_code
693 output_numberformat output_dateformat output_longdates));
695 # id is the old record
698 "UPDATE language SET " .
699 " description = ?, template_code = ?, article_code = ?, " .
700 " output_numberformat = ?, output_dateformat = ?, " .
701 " output_longdates = ? " .
703 push(@values, $form->{id});
706 "INSERT INTO language (" .
707 " description, template_code, article_code, " .
708 " output_numberformat, output_dateformat, output_longdates" .
709 ") VALUES (?, ?, ?, ?, ?, ?)";
711 do_query($form, $dbh, $query, @values);
715 $main::lxdebug->leave_sub();
718 sub delete_language {
719 $main::lxdebug->enter_sub();
721 my ($self, $myconfig, $form) = @_;
723 # connect to database
724 my $dbh = $form->dbconnect_noauto($myconfig);
726 foreach my $table (qw(translation_payment_terms units_language)) {
727 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
728 do_query($form, $dbh, $query, $form->{"id"});
731 $query = "DELETE FROM language WHERE id = ?";
732 do_query($form, $dbh, $query, $form->{"id"});
737 $main::lxdebug->leave_sub();
742 $main::lxdebug->enter_sub();
744 my ($self, $myconfig, $form) = @_;
746 # connect to database
747 my $dbh = $form->dbconnect($myconfig);
749 my $query = qq|SELECT id, description,
751 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
753 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
755 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
757 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
759 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
761 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
763 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
765 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
767 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
771 $sth = $dbh->prepare($query);
772 $sth->execute || $form->dberror($query);
775 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
776 push @{ $form->{ALL} }, $ref;
782 $main::lxdebug->leave_sub();
785 sub get_buchungsgruppe {
786 $main::lxdebug->enter_sub();
788 my ($self, $myconfig, $form) = @_;
790 # connect to database
791 my $dbh = $form->dbconnect($myconfig);
795 qq|SELECT description, inventory_accno_id,
796 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
798 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
800 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
802 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
804 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
806 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
808 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
810 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
812 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
815 my $sth = $dbh->prepare($query);
816 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
818 my $ref = $sth->fetchrow_hashref(NAME_lc);
820 map { $form->{$_} = $ref->{$_} } keys %$ref;
825 qq|SELECT count(id) = 0 AS orphaned
827 WHERE buchungsgruppen_id = ?|;
828 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
831 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
833 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
834 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
837 $query = qq|SELECT c.accno, c.description, c.link, c.id,
838 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
839 FROM chart c, defaults d
840 WHERE c.link LIKE '%$module%'
844 my $sth = $dbh->prepare($query);
845 $sth->execute || $form->dberror($query);
846 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
847 foreach my $key (split(/:/, $ref->{link})) {
848 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
849 $form->{"std_inventory_accno_id"} = $ref->{"id"};
851 if ($key =~ /$module/) {
852 if ( ($ref->{id} eq $ref->{inventory_accno_id})
853 || ($ref->{id} eq $ref->{income_accno_id})
854 || ($ref->{id} eq $ref->{expense_accno_id})) {
855 push @{ $form->{"${module}_links"}{$key} },
856 { accno => $ref->{accno},
857 description => $ref->{description},
858 selected => "selected",
861 push @{ $form->{"${module}_links"}{$key} },
862 { accno => $ref->{accno},
863 description => $ref->{description},
875 $main::lxdebug->leave_sub();
878 sub save_buchungsgruppe {
879 $main::lxdebug->enter_sub();
881 my ($self, $myconfig, $form) = @_;
883 # connect to database
884 my $dbh = $form->dbconnect($myconfig);
886 my @values = ($form->{description}, $form->{inventory_accno_id},
887 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
888 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
889 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
890 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
894 # id is the old record
896 $query = qq|UPDATE buchungsgruppen SET
897 description = ?, inventory_accno_id = ?,
898 income_accno_id_0 = ?, expense_accno_id_0 = ?,
899 income_accno_id_1 = ?, expense_accno_id_1 = ?,
900 income_accno_id_2 = ?, expense_accno_id_2 = ?,
901 income_accno_id_3 = ?, expense_accno_id_3 = ?
903 push(@values, $form->{id});
905 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
906 my ($sortkey) = $dbh->selectrow_array($query);
907 $form->dberror($query) if ($dbh->err);
908 push(@values, $sortkey);
909 $query = qq|INSERT INTO buchungsgruppen
910 (description, inventory_accno_id,
911 income_accno_id_0, expense_accno_id_0,
912 income_accno_id_1, expense_accno_id_1,
913 income_accno_id_2, expense_accno_id_2,
914 income_accno_id_3, expense_accno_id_3,
916 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
918 do_query($form, $dbh, $query, @values);
922 $main::lxdebug->leave_sub();
925 sub delete_buchungsgruppe {
926 $main::lxdebug->enter_sub();
928 my ($self, $myconfig, $form) = @_;
930 # connect to database
931 my $dbh = $form->dbconnect($myconfig);
933 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
934 do_query($form, $dbh, $query, $form->{id});
938 $main::lxdebug->leave_sub();
942 $main::lxdebug->enter_sub();
944 my ($self, $myconfig, $form, $table) = @_;
946 # connect to database
947 my $dbh = $form->dbconnect_noauto($myconfig);
951 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
952 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
953 my @values = ($form->{"id1"}, $form->{"id2"});
954 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
955 $main::lxdebug->dump(0, "v", \@values);
956 $main::lxdebug->dump(0, "s", \@sortkeys);
958 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
959 my $sth = $dbh->prepare($query);
960 $sth->execute($sortkeys[1], $form->{"id1"}) ||
961 $form->dberror($query . " ($sortkeys[1], $form->{id1})");
962 $sth->execute($sortkeys[0], $form->{"id2"}) ||
963 $form->dberror($query . " ($sortkeys[0], $form->{id2})");
969 $main::lxdebug->leave_sub();
973 $main::lxdebug->enter_sub();
975 my ($self, $myconfig, $form) = @_;
977 # connect to database
978 my $dbh = $form->dbconnect($myconfig);
980 my $query = qq|SELECT id, printer_description, template_code, printer_command
984 $sth = $dbh->prepare($query);
985 $sth->execute || $form->dberror($query);
988 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
989 push @{ $form->{ALL} }, $ref;
995 $main::lxdebug->leave_sub();
999 $main::lxdebug->enter_sub();
1001 my ($self, $myconfig, $form) = @_;
1003 # connect to database
1004 my $dbh = $form->dbconnect($myconfig);
1007 qq|SELECT p.printer_description, p.template_code, p.printer_command
1010 my $sth = $dbh->prepare($query);
1011 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1013 my $ref = $sth->fetchrow_hashref(NAME_lc);
1015 map { $form->{$_} = $ref->{$_} } keys %$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);
1032 my @values = ($form->{printer_description},
1033 $form->{template_code},
1034 $form->{printer_command});
1036 # id is the old record
1038 $query = qq|UPDATE printers SET
1039 printer_description = ?, template_code = ?, printer_command = ?
1041 push(@values, $form->{id});
1043 $query = qq|INSERT INTO printers
1044 (printer_description, template_code, printer_command)
1047 do_query($form, $dbh, $query, @values);
1051 $main::lxdebug->leave_sub();
1054 sub delete_printer {
1055 $main::lxdebug->enter_sub();
1057 my ($self, $myconfig, $form) = @_;
1059 # connect to database
1060 my $dbh = $form->dbconnect($myconfig);
1062 $query = qq|DELETE FROM printers
1064 do_query($form, $dbh, $query, $form->{id});
1068 $main::lxdebug->leave_sub();
1072 $main::lxdebug->enter_sub();
1074 my ($self, $myconfig, $form) = @_;
1076 # connect to database
1077 my $dbh = $form->dbconnect($myconfig);
1079 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1081 $sth = $dbh->prepare($query);
1082 $sth->execute || $form->dberror($query);
1085 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1086 push @{ $form->{ALL} }, $ref;
1092 $main::lxdebug->leave_sub();
1096 $main::lxdebug->enter_sub();
1098 my ($self, $myconfig, $form) = @_;
1100 # connect to database
1101 my $dbh = $form->dbconnect($myconfig);
1103 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1104 my $sth = $dbh->prepare($query);
1105 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1107 my $ref = $sth->fetchrow_hashref(NAME_lc);
1108 map { $form->{$_} = $ref->{$_} } keys %$ref;
1112 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1113 qq|FROM translation_payment_terms t | .
1114 qq|LEFT JOIN language l ON t.language_id = l.id | .
1115 qq|WHERE t.payment_terms_id = ? | .
1117 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1118 qq| l.description AS language | .
1119 qq|FROM language l|;
1120 $sth = $dbh->prepare($query);
1121 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1124 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1125 $mapping{ $ref->{"language_id"} } = $ref
1126 unless (defined($mapping{ $ref->{"language_id"} }));
1130 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1135 $main::lxdebug->leave_sub();
1139 $main::lxdebug->enter_sub();
1141 my ($self, $myconfig, $form) = @_;
1143 # connect to database
1144 my $dbh = $form->dbconnect_noauto($myconfig);
1149 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1150 qq|FROM payment_terms|;
1152 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1154 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1155 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1159 qq|DELETE FROM translation_payment_terms | .
1160 qq|WHERE payment_terms_id = ?|;
1161 do_query($form, $dbh, $query, $form->{"id"});
1164 $query = qq|UPDATE payment_terms SET
1165 description = ?, description_long = ?,
1167 terms_netto = ?, terms_skonto = ?,
1170 my @values = ($form->{description}, $form->{description_long},
1171 $form->{ranking} * 1,
1172 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1173 $form->{percent_skonto} * 1,
1175 do_query($form, $dbh, $query, @values);
1177 $query = qq|SELECT id FROM language|;
1179 my $sth = $dbh->prepare($query);
1180 $sth->execute() || $form->dberror($query);
1182 while (my ($id) = $sth->fetchrow_array()) {
1183 push(@language_ids, $id);
1188 qq|INSERT INTO translation_payment_terms | .
1189 qq|(language_id, payment_terms_id, description_long) | .
1190 qq|VALUES (?, ?, ?)|;
1191 $sth = $dbh->prepare($query);
1193 foreach my $language_id (@language_ids) {
1194 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1195 $form->{"description_long_${language_id}"});
1202 $main::lxdebug->leave_sub();
1205 sub delete_payment {
1206 $main::lxdebug->enter_sub();
1208 my ($self, $myconfig, $form) = @_;
1210 # connect to database
1211 my $dbh = $form->dbconnect_noauto($myconfig);
1214 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1215 do_query($form, $dbh, $query, $form->{"id"});
1217 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1218 do_query($form, $dbh, $query, $form->{"id"});
1223 $main::lxdebug->leave_sub();
1227 $main::lxdebug->enter_sub();
1229 my ($self, $form) = @_;
1231 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1233 while (<TEMPLATE>) {
1234 $form->{body} .= $_;
1239 $main::lxdebug->leave_sub();
1243 $main::lxdebug->enter_sub();
1245 my ($self, $form) = @_;
1247 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1250 $form->{body} =~ s/\r\n/\n/g;
1251 print TEMPLATE $form->{body};
1255 $main::lxdebug->leave_sub();
1258 sub save_preferences {
1259 $main::lxdebug->enter_sub();
1261 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1263 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1264 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1267 $form->{curr} =~ s/ //g;
1268 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1269 $form->{curr} = join ':', @a;
1271 # connect to database
1272 my $dbh = $form->dbconnect_noauto($myconfig);
1274 # these defaults are database wide
1275 # user specific variables are in myconfig
1278 qq|UPDATE defaults SET | .
1279 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1280 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1281 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1282 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1283 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1284 qq|invnumber = ?, | .
1285 qq|cnnumber = ?, | .
1286 qq|sonumber = ?, | .
1287 qq|ponumber = ?, | .
1288 qq|sqnumber = ?, | .
1289 qq|rfqnumber = ?, | .
1290 qq|customernumber = ?, | .
1291 qq|vendornumber = ?, | .
1292 qq|articlenumber = ?, | .
1293 qq|servicenumber = ?, | .
1296 qq|businessnumber = ?|;
1297 my @values = ($form->{inventory_accno}, $form->{income_accno},
1298 $form->{expense_accno},
1299 $form->{fxgain_accno}, $form->{fxloss_accno},
1300 $form->{invnumber}, $form->{cnnumber},
1301 $form->{sonumber}, $form->{ponumber},
1302 $form->{sqnumber}, $form->{rfqnumber},
1303 $form->{customernumber}, $form->{vendornumber},
1304 $form->{articlenumber}, $form->{servicenumber},
1305 $form->{yearend}, $form->{curr},
1306 $form->{businessnumber});
1307 do_query($form, $dbh, $query, @values);
1310 $query = qq|UPDATE employee
1313 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1315 my $rc = $dbh->commit;
1318 # save first currency in myconfig
1319 $form->{currency} = substr($form->{curr}, 0, 3);
1321 my $myconfig = new User "$memberfile", "$form->{login}";
1323 foreach my $item (keys %$form) {
1324 $myconfig->{$item} = $form->{$item};
1327 $myconfig->save_member($memberfile, $userspath);
1331 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1332 foreach $directory (@webdavdirs) {
1333 $file = "webdav/" . $directory . "/webdav-user";
1334 if ($myconfig->{$directory}) {
1335 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1336 while (<HTACCESS>) {
1337 ($login, $password) = split(/:/, $_);
1338 if ($login ne $form->{login}) {
1343 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1344 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1345 print(HTACCESS $newfile);
1348 $form->{$directory} = 0;
1349 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1350 while (<HTACCESS>) {
1351 ($login, $password) = split(/:/, $_);
1352 if ($login ne $form->{login}) {
1357 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1358 print(HTACCESS $newfile);
1364 $main::lxdebug->leave_sub();
1369 sub defaultaccounts {
1370 $main::lxdebug->enter_sub();
1372 my ($self, $myconfig, $form) = @_;
1374 # connect to database
1375 my $dbh = $form->dbconnect($myconfig);
1377 # get defaults from defaults table
1378 my $query = qq|SELECT * FROM defaults|;
1379 my $sth = $dbh->prepare($query);
1380 $sth->execute || $form->dberror($query);
1382 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1383 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1384 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1385 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1386 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1387 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1391 $query = qq|SELECT c.id, c.accno, c.description, c.link
1393 WHERE c.link LIKE '%IC%'
1395 $sth = $dbh->prepare($query);
1396 $sth->execute || $self->dberror($query);
1398 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1399 foreach my $key (split(/:/, $ref->{link})) {
1402 if ($key =~ /cogs/) {
1403 $nkey = "IC_expense";
1405 if ($key =~ /sale/) {
1406 $nkey = "IC_income";
1408 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1410 description => $ref->{description}
1417 $query = qq|SELECT c.id, c.accno, c.description
1419 WHERE c.category = 'I'
1420 AND c.charttype = 'A'
1422 $sth = $dbh->prepare($query);
1423 $sth->execute || $self->dberror($query);
1425 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1426 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1428 description => $ref->{description}
1433 $query = qq|SELECT c.id, c.accno, c.description
1435 WHERE c.category = 'E'
1436 AND c.charttype = 'A'
1438 $sth = $dbh->prepare($query);
1439 $sth->execute || $self->dberror($query);
1441 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1442 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1444 description => $ref->{description}
1449 # now get the tax rates and numbers
1450 $query = qq|SELECT c.id, c.accno, c.description,
1451 t.rate * 100 AS rate, t.taxnumber
1453 WHERE c.id = t.chart_id|;
1455 $sth = $dbh->prepare($query);
1456 $sth->execute || $form->dberror($query);
1458 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1459 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1460 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1461 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1462 if $ref->{taxnumber};
1463 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1469 $main::lxdebug->leave_sub();
1473 $main::lxdebug->enter_sub();
1475 my ($self, $myconfig, $form) = @_;
1477 my $dbh = $form->dbconnect($myconfig);
1479 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1480 my $sth = $dbh->prepare($query);
1481 $sth->execute || $form->dberror($query);
1483 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1489 $main::lxdebug->leave_sub();
1493 $main::lxdebug->enter_sub();
1495 my ($self, $myconfig, $form) = @_;
1497 my $dbh = $form->dbconnect($myconfig);
1499 my ($query, @values);
1501 if ($form->{revtrans}) {
1502 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1504 } elsif ($form->{closedto}) {
1505 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1506 @values = (conv_date($form->{closedto}));
1509 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1512 # set close in defaults
1513 do_query($form, $dbh, $query, @values);
1517 $main::lxdebug->leave_sub();
1521 my ($self, $units, $unit_name, $factor) = @_;
1523 $factor = 1 unless ($factor);
1525 my $unit = $units->{$unit_name};
1527 if (!defined($unit) || !$unit->{"base_unit"} ||
1528 ($unit_name eq $unit->{"base_unit"})) {
1529 return ($unit_name, $factor);
1532 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1535 sub retrieve_units {
1536 $main::lxdebug->enter_sub();
1538 my ($self, $myconfig, $form, $type, $prefix) = @_;
1540 my $dbh = $form->dbconnect($myconfig);
1542 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1545 $query .= " WHERE (type = ?)";
1549 my $sth = $dbh->prepare($query);
1550 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1553 while (my $ref = $sth->fetchrow_hashref()) {
1554 $units->{$ref->{"name"}} = $ref;
1558 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1559 $sth = $dbh->prepare($query_lang);
1560 $sth->execute() || $form->dberror($query_lang);
1562 while ($ref = $sth->fetchrow_hashref()) {
1563 push(@languages, $ref);
1567 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1568 "FROM units_language ul " .
1569 "LEFT JOIN language l ON ul.language_id = l.id " .
1570 "WHERE ul.unit = ?";
1571 $sth = $dbh->prepare($query_lang);
1573 foreach my $unit (values(%{$units})) {
1574 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1576 $unit->{"LANGUAGES"} = {};
1577 foreach my $lang (@languages) {
1578 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1581 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1582 while ($ref = $sth->fetchrow_hashref()) {
1583 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1590 $main::lxdebug->leave_sub();
1595 sub translate_units {
1596 $main::lxdebug->enter_sub();
1598 my ($self, $form, $template_code, $unit, $amount) = @_;
1600 my $units = $self->retrieve_units(\%main::myconfig, $form);
1602 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1603 my $new_unit = $unit;
1605 if (($amount != 1) && $h->{"localized_plural"}) {
1606 $new_unit = $h->{"localized_plural"};
1607 } elsif ($h->{"localized"}) {
1608 $new_unit = $h->{"localized"};
1612 $main::lxdebug->leave_sub();
1618 $main::lxdebug->enter_sub();
1620 my ($self, $myconfig, $form, $units) = @_;
1622 my $dbh = $form->dbconnect($myconfig);
1624 map({ $_->{"in_use"} = 0; } values(%{$units}));
1626 foreach my $unit (values(%{$units})) {
1627 my $base_unit = $unit->{"original_base_unit"};
1628 while ($base_unit) {
1629 $units->{$base_unit}->{"in_use"} = 1;
1630 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1631 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1632 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1636 foreach my $unit (values(%{$units})) {
1637 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1639 foreach my $table (qw(parts invoice orderitems)) {
1640 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1642 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1643 $query .= "= " . $dbh->quote($unit->{"name"});
1645 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1646 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1649 my ($count) = $dbh->selectrow_array($query);
1650 $form->dberror($query) if ($dbh->err);
1653 $unit->{"in_use"} = 1;
1661 $main::lxdebug->leave_sub();
1664 sub unit_select_data {
1665 $main::lxdebug->enter_sub();
1667 my ($self, $units, $selected, $empty_entry) = @_;
1672 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1675 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
1676 push(@{$select}, { "name" => $unit,
1677 "base_unit" => $units->{$unit}->{"base_unit"},
1678 "factor" => $units->{$unit}->{"factor"},
1679 "selected" => ($unit eq $selected) ? "selected" : "" });
1682 $main::lxdebug->leave_sub();
1687 sub unit_select_html {
1688 $main::lxdebug->enter_sub();
1690 my ($self, $units, $name, $selected, $convertible_into) = @_;
1692 my $select = "<select name=${name}>";
1694 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
1695 if (!$convertible_into ||
1696 ($units->{$convertible_into} &&
1697 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1698 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1701 $select .= "</select>";
1703 $main::lxdebug->leave_sub();
1709 $main::lxdebug->enter_sub();
1711 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1713 my $dbh = $form->dbconnect_noauto($myconfig);
1715 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1716 my ($sortkey) = selectrow_query($form, $dbh, $query);
1718 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1719 "VALUES (?, ?, ?, ?, ?)";
1720 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1723 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1724 my $sth = $dbh->prepare($query);
1725 foreach my $lang (@{$languages}) {
1726 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1727 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1735 $main::lxdebug->leave_sub();
1739 $main::lxdebug->enter_sub();
1741 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1743 my $dbh = $form->dbconnect_noauto($myconfig);
1745 my ($base_unit, $unit, $sth, $query);
1747 $query = "DELETE FROM units_language";
1748 $dbh->do($query) || $form->dberror($query);
1750 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1751 $query = "DELETE FROM units WHERE name IN (";
1752 map({ $query .= "?," } @{$delete_units});
1753 substr($query, -1, 1) = ")";
1754 $dbh->do($query, undef, @{$delete_units}) ||
1755 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1758 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1759 $sth = $dbh->prepare($query);
1761 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1762 my $sth_lang = $dbh->prepare($query_lang);
1764 foreach $unit (values(%{$units})) {
1765 $unit->{"depth"} = 0;
1766 my $base_unit = $unit;
1767 while ($base_unit->{"base_unit"}) {
1769 $base_unit = $units->{$base_unit->{"base_unit"}};
1773 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1774 if ($unit->{"LANGUAGES"}) {
1775 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1776 next unless ($lang->{"id"} && $lang->{"localized"});
1777 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1778 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1782 next if ($unit->{"unchanged_unit"});
1784 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1785 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1789 $sth_lang->finish();
1793 $main::lxdebug->leave_sub();
1797 $main::lxdebug->enter_sub();
1799 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
1801 my $dbh = $form->dbconnect_noauto($myconfig);
1805 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
1806 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
1809 qq|SELECT sortkey FROM units | .
1810 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
1811 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
1812 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
1814 if (defined($sortkey_1)) {
1815 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
1816 my ($name_2) = selectrow_query($form, $dbh, $query);
1818 if (defined($name_2)) {
1819 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
1820 my $sth = $dbh->prepare($query);
1822 do_statement($form, $sth, $query, $sortkey_1, $name_2);
1823 do_statement($form, $sth, $query, $sortkey_2, $name_1);
1830 $main::lxdebug->leave_sub();