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 sub prepare_template_filename {
1225 $main::lxdebug->enter_sub();
1227 my ($self, $myconfig, $form) = @_;
1229 my ($filename, $display_filename);
1231 if ($form->{type} eq "stylesheet") {
1232 $filename = "css/$myconfig->{stylesheet}";
1233 $display_filename = $myconfig->{stylesheet};
1236 $filename = $form->{formname};
1238 if ($form->{language}) {
1239 my ($id, $template_code) = split(/--/, $form->{language});
1240 $filename .= "_${template_code}";
1243 if ($form->{printer}) {
1244 my ($id, $template_code) = split(/--/, $form->{printer});
1245 $filename .= "_${template_code}";
1248 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1249 $filename =~ s|.*/||;
1250 $display_filename = $filename;
1251 $filename = "$myconfig->{templates}/$filename";
1254 $main::lxdebug->leave_sub();
1256 return ($filename, $display_filename);
1261 $main::lxdebug->enter_sub();
1263 my ($self, $filename) = @_;
1265 my ($content, $lines) = ("", 0);
1269 if (open(TEMPLATE, $filename)) {
1270 while (<TEMPLATE>) {
1277 $main::lxdebug->leave_sub();
1279 return ($content, $lines);
1283 $main::lxdebug->enter_sub();
1285 my ($self, $filename, $content) = @_;
1291 if (open(TEMPLATE, ">$filename")) {
1292 $content =~ s/\r\n/\n/g;
1293 print(TEMPLATE $content);
1299 $main::lxdebug->leave_sub();
1304 sub save_preferences {
1305 $main::lxdebug->enter_sub();
1307 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1309 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1310 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1313 $form->{curr} =~ s/ //g;
1314 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1315 $form->{curr} = join ':', @a;
1317 # connect to database
1318 my $dbh = $form->dbconnect_noauto($myconfig);
1320 # these defaults are database wide
1321 # user specific variables are in myconfig
1324 qq|UPDATE defaults SET | .
1325 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1326 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1327 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1328 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1329 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1330 qq|invnumber = ?, | .
1331 qq|cnnumber = ?, | .
1332 qq|sonumber = ?, | .
1333 qq|ponumber = ?, | .
1334 qq|sqnumber = ?, | .
1335 qq|rfqnumber = ?, | .
1336 qq|customernumber = ?, | .
1337 qq|vendornumber = ?, | .
1338 qq|articlenumber = ?, | .
1339 qq|servicenumber = ?, | .
1342 qq|businessnumber = ?|;
1343 my @values = ($form->{inventory_accno}, $form->{income_accno},
1344 $form->{expense_accno},
1345 $form->{fxgain_accno}, $form->{fxloss_accno},
1346 $form->{invnumber}, $form->{cnnumber},
1347 $form->{sonumber}, $form->{ponumber},
1348 $form->{sqnumber}, $form->{rfqnumber},
1349 $form->{customernumber}, $form->{vendornumber},
1350 $form->{articlenumber}, $form->{servicenumber},
1351 $form->{yearend}, $form->{curr},
1352 $form->{businessnumber});
1353 do_query($form, $dbh, $query, @values);
1356 $query = qq|UPDATE employee
1359 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1361 my $rc = $dbh->commit;
1364 # save first currency in myconfig
1365 $form->{currency} = substr($form->{curr}, 0, 3);
1367 my $myconfig = new User "$memberfile", "$form->{login}";
1369 foreach my $item (keys %$form) {
1370 $myconfig->{$item} = $form->{$item};
1373 $myconfig->save_member($memberfile, $userspath);
1377 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1378 foreach $directory (@webdavdirs) {
1379 $file = "webdav/" . $directory . "/webdav-user";
1380 if ($myconfig->{$directory}) {
1381 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1382 while (<HTACCESS>) {
1383 ($login, $password) = split(/:/, $_);
1384 if ($login ne $form->{login}) {
1389 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1390 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1391 print(HTACCESS $newfile);
1394 $form->{$directory} = 0;
1395 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1396 while (<HTACCESS>) {
1397 ($login, $password) = split(/:/, $_);
1398 if ($login ne $form->{login}) {
1403 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1404 print(HTACCESS $newfile);
1410 $main::lxdebug->leave_sub();
1415 sub defaultaccounts {
1416 $main::lxdebug->enter_sub();
1418 my ($self, $myconfig, $form) = @_;
1420 # connect to database
1421 my $dbh = $form->dbconnect($myconfig);
1423 # get defaults from defaults table
1424 my $query = qq|SELECT * FROM defaults|;
1425 my $sth = $dbh->prepare($query);
1426 $sth->execute || $form->dberror($query);
1428 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1429 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1430 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1431 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1432 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1433 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1437 $query = qq|SELECT c.id, c.accno, c.description, c.link
1439 WHERE c.link LIKE '%IC%'
1441 $sth = $dbh->prepare($query);
1442 $sth->execute || $self->dberror($query);
1444 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1445 foreach my $key (split(/:/, $ref->{link})) {
1448 if ($key =~ /cogs/) {
1449 $nkey = "IC_expense";
1451 if ($key =~ /sale/) {
1452 $nkey = "IC_income";
1454 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1456 description => $ref->{description}
1463 $query = qq|SELECT c.id, c.accno, c.description
1465 WHERE c.category = 'I'
1466 AND c.charttype = 'A'
1468 $sth = $dbh->prepare($query);
1469 $sth->execute || $self->dberror($query);
1471 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1472 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1474 description => $ref->{description}
1479 $query = qq|SELECT c.id, c.accno, c.description
1481 WHERE c.category = 'E'
1482 AND c.charttype = 'A'
1484 $sth = $dbh->prepare($query);
1485 $sth->execute || $self->dberror($query);
1487 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1488 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1490 description => $ref->{description}
1495 # now get the tax rates and numbers
1496 $query = qq|SELECT c.id, c.accno, c.description,
1497 t.rate * 100 AS rate, t.taxnumber
1499 WHERE c.id = t.chart_id|;
1501 $sth = $dbh->prepare($query);
1502 $sth->execute || $form->dberror($query);
1504 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1505 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1506 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1507 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1508 if $ref->{taxnumber};
1509 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1515 $main::lxdebug->leave_sub();
1519 $main::lxdebug->enter_sub();
1521 my ($self, $myconfig, $form) = @_;
1523 my $dbh = $form->dbconnect($myconfig);
1525 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1526 my $sth = $dbh->prepare($query);
1527 $sth->execute || $form->dberror($query);
1529 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1535 $main::lxdebug->leave_sub();
1539 $main::lxdebug->enter_sub();
1541 my ($self, $myconfig, $form) = @_;
1543 my $dbh = $form->dbconnect($myconfig);
1545 my ($query, @values);
1547 if ($form->{revtrans}) {
1548 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1550 } elsif ($form->{closedto}) {
1551 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1552 @values = (conv_date($form->{closedto}));
1555 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1558 # set close in defaults
1559 do_query($form, $dbh, $query, @values);
1563 $main::lxdebug->leave_sub();
1567 my ($self, $units, $unit_name, $factor) = @_;
1569 $factor = 1 unless ($factor);
1571 my $unit = $units->{$unit_name};
1573 if (!defined($unit) || !$unit->{"base_unit"} ||
1574 ($unit_name eq $unit->{"base_unit"})) {
1575 return ($unit_name, $factor);
1578 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1581 sub retrieve_units {
1582 $main::lxdebug->enter_sub();
1584 my ($self, $myconfig, $form, $type, $prefix) = @_;
1586 my $dbh = $form->dbconnect($myconfig);
1588 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1591 $query .= " WHERE (type = ?)";
1595 my $sth = $dbh->prepare($query);
1596 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1599 while (my $ref = $sth->fetchrow_hashref()) {
1600 $units->{$ref->{"name"}} = $ref;
1604 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1605 $sth = $dbh->prepare($query_lang);
1606 $sth->execute() || $form->dberror($query_lang);
1608 while ($ref = $sth->fetchrow_hashref()) {
1609 push(@languages, $ref);
1613 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1614 "FROM units_language ul " .
1615 "LEFT JOIN language l ON ul.language_id = l.id " .
1616 "WHERE ul.unit = ?";
1617 $sth = $dbh->prepare($query_lang);
1619 foreach my $unit (values(%{$units})) {
1620 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1622 $unit->{"LANGUAGES"} = {};
1623 foreach my $lang (@languages) {
1624 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1627 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1628 while ($ref = $sth->fetchrow_hashref()) {
1629 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1636 $main::lxdebug->leave_sub();
1641 sub translate_units {
1642 $main::lxdebug->enter_sub();
1644 my ($self, $form, $template_code, $unit, $amount) = @_;
1646 my $units = $self->retrieve_units(\%main::myconfig, $form);
1648 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1649 my $new_unit = $unit;
1651 if (($amount != 1) && $h->{"localized_plural"}) {
1652 $new_unit = $h->{"localized_plural"};
1653 } elsif ($h->{"localized"}) {
1654 $new_unit = $h->{"localized"};
1658 $main::lxdebug->leave_sub();
1664 $main::lxdebug->enter_sub();
1666 my ($self, $myconfig, $form, $units) = @_;
1668 my $dbh = $form->dbconnect($myconfig);
1670 map({ $_->{"in_use"} = 0; } values(%{$units}));
1672 foreach my $unit (values(%{$units})) {
1673 my $base_unit = $unit->{"original_base_unit"};
1674 while ($base_unit) {
1675 $units->{$base_unit}->{"in_use"} = 1;
1676 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1677 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1678 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1682 foreach my $unit (values(%{$units})) {
1683 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1685 foreach my $table (qw(parts invoice orderitems)) {
1686 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1688 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1689 $query .= "= " . $dbh->quote($unit->{"name"});
1691 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1692 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1695 my ($count) = $dbh->selectrow_array($query);
1696 $form->dberror($query) if ($dbh->err);
1699 $unit->{"in_use"} = 1;
1707 $main::lxdebug->leave_sub();
1710 sub unit_select_data {
1711 $main::lxdebug->enter_sub();
1713 my ($self, $units, $selected, $empty_entry) = @_;
1718 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1721 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1722 push(@{$select}, { "name" => $unit,
1723 "base_unit" => $units->{$unit}->{"base_unit"},
1724 "factor" => $units->{$unit}->{"factor"},
1725 "selected" => ($unit eq $selected) ? "selected" : "" });
1728 $main::lxdebug->leave_sub();
1733 sub unit_select_html {
1734 $main::lxdebug->enter_sub();
1736 my ($self, $units, $name, $selected, $convertible_into) = @_;
1738 my $select = "<select name=${name}>";
1740 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1741 if (!$convertible_into ||
1742 ($units->{$convertible_into} &&
1743 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1744 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1747 $select .= "</select>";
1749 $main::lxdebug->leave_sub();
1755 $main::lxdebug->enter_sub();
1757 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1759 my $dbh = $form->dbconnect_noauto($myconfig);
1761 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1762 my ($sortkey) = selectrow_query($form, $dbh, $query);
1764 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1765 "VALUES (?, ?, ?, ?, ?)";
1766 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1769 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1770 my $sth = $dbh->prepare($query);
1771 foreach my $lang (@{$languages}) {
1772 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1773 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1781 $main::lxdebug->leave_sub();
1785 $main::lxdebug->enter_sub();
1787 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1789 my $dbh = $form->dbconnect_noauto($myconfig);
1791 my ($base_unit, $unit, $sth, $query);
1793 $query = "DELETE FROM units_language";
1794 $dbh->do($query) || $form->dberror($query);
1796 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1797 $query = "DELETE FROM units WHERE name IN (";
1798 map({ $query .= "?," } @{$delete_units});
1799 substr($query, -1, 1) = ")";
1800 $dbh->do($query, undef, @{$delete_units}) ||
1801 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1804 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1805 $sth = $dbh->prepare($query);
1807 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1808 my $sth_lang = $dbh->prepare($query_lang);
1810 foreach $unit (values(%{$units})) {
1811 $unit->{"depth"} = 0;
1812 my $base_unit = $unit;
1813 while ($base_unit->{"base_unit"}) {
1815 $base_unit = $units->{$base_unit->{"base_unit"}};
1819 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1820 if ($unit->{"LANGUAGES"}) {
1821 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1822 next unless ($lang->{"id"} && $lang->{"localized"});
1823 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1824 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1828 next if ($unit->{"unchanged_unit"});
1830 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1831 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1835 $sth_lang->finish();
1839 $main::lxdebug->leave_sub();
1843 $main::lxdebug->enter_sub();
1845 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
1847 my $dbh = $form->dbconnect_noauto($myconfig);
1851 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
1852 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
1855 qq|SELECT sortkey FROM units | .
1856 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
1857 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
1858 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
1860 if (defined($sortkey_1)) {
1861 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
1862 my ($name_2) = selectrow_query($form, $dbh, $query);
1864 if (defined($name_2)) {
1865 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
1866 my $sth = $dbh->prepare($query);
1868 do_statement($form, $sth, $query, $sortkey_1, $name_2);
1869 do_statement($form, $sth, $query, $sortkey_2, $name_1);
1876 $main::lxdebug->leave_sub();