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 SELECT c.accno, c.description, c.charttype, c.category,
52 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
53 c.pos_bwa, datevautomatik,
54 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
55 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
58 ON (c.id=tk.chart_id AND tk.id =
59 (SELECT id FROM taxkeys
60 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
61 ORDER BY startdate DESC LIMIT 1))
66 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
67 my $sth = $dbh->prepare($query);
68 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
70 my $ref = $sth->fetchrow_hashref(NAME_lc);
72 foreach my $key (keys %$ref) {
73 $form->{"$key"} = $ref->{"$key"};
78 # get default accounts
79 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
81 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
82 $sth = $dbh->prepare($query);
83 $sth->execute || $form->dberror($query);
85 $ref = $sth->fetchrow_hashref(NAME_lc);
87 map { $form->{$_} = $ref->{$_} } keys %ref;
93 # get taxkeys and description
97 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
99 id||'--'||taxkey AS tax,
102 FROM tax ORDER BY taxkey
104 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
105 $sth = $dbh->prepare($query);
106 $sth->execute || $form->dberror($query);
108 $form->{TAXKEY} = [];
110 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
111 push @{ $form->{TAXKEY} }, $ref;
117 $query = qq|SELECT id, accno,description
121 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
122 $sth = $dbh->prepare($query);
123 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
125 $form->{NEWACCOUNT} = [];
126 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
127 push @{ $form->{NEWACCOUNT} }, $ref;
132 # get the taxkeys of account
146 LEFT JOIN tax t ON (t.id = tk.tax_id)
147 LEFT JOIN chart c ON (c.id = t.chart_id)
149 WHERE tk.chart_id = ?
150 ORDER BY startdate DESC
152 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
153 $sth = $dbh->prepare($query);
155 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
157 $form->{ACCOUNT_TAXKEYS} = [];
159 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
160 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
166 # check if we have any transactions
167 $query = qq|SELECT a.trans_id FROM acc_trans a
168 WHERE a.chart_id = ?|;
169 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
170 $sth = $dbh->prepare($query);
171 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
173 ($form->{orphaned}) = $sth->fetchrow_array;
174 $form->{orphaned} = !$form->{orphaned};
177 # check if new account is active
178 $form->{new_chart_valid} = 0;
179 if ($form->{new_chart_id}) {
180 $query = qq|SELECT current_date-valid_from FROM chart
182 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
183 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
185 $form->{new_chart_valid} = 1;
192 $main::lxdebug->leave_sub();
196 $main::lxdebug->enter_sub();
198 my ($self, $myconfig, $form) = @_;
200 # connect to database, turn off AutoCommit
201 my $dbh = $form->dbconnect_noauto($myconfig);
203 # sanity check, can't have AR with AR_...
204 if ($form->{AR} || $form->{AP} || $form->{IC}) {
205 map { delete $form->{$_} }
206 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);
210 foreach my $item ($form->{AR}, $form->{AR_amount},
211 $form->{AR_tax}, $form->{AR_paid},
212 $form->{AP}, $form->{AP_amount},
213 $form->{AP_tax}, $form->{AP_paid},
214 $form->{IC}, $form->{IC_sale},
215 $form->{IC_cogs}, $form->{IC_taxpart},
216 $form->{IC_income}, $form->{IC_expense},
217 $form->{IC_taxservice}, $form->{CT_tax}
219 $form->{link} .= "${item}:" if ($item);
223 # strip blanks from accno
224 map { $form->{$_} =~ s/ //g; } qw(accno);
228 if ($form->{id} eq "NULL") {
235 $query = qq|UPDATE chart SET
251 $form->{description},
255 conv_i($form->{pos_bwa}),
256 conv_i($form->{pos_bilanz}),
257 conv_i($form->{pos_eur}),
258 conv_i($form->{new_chart_id}),
259 conv_date($form->{valid_from}),
260 ($form->{datevautomatik} eq 'T') ? 'true':'false',
265 elsif ($form->{id} && !$form->{new_chart_valid}) {
269 SET new_chart_id = ?,
275 conv_i($form->{new_chart_id}),
276 conv_date($form->{valid_from}),
295 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
300 $form->{description},
302 $form->{category}, $form->{link},
303 conv_i($form->{pos_bwa}),
304 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
305 conv_i($form->{new_chart_id}),
306 conv_date($form->{valid_from}),
307 ($form->{datevautomatik} eq 'T') ? 'true':'false',
312 do_query($form, $dbh, $query, @values);
318 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
322 for $tk_count (0 .. $MAX_TRIES) {
326 # Check if the account already exists, else cancel
327 last READTAXKEYS if ( $form->{'id'} == 0);
329 # check if there is a startdate
330 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
335 # check if there is at least one relation to pos_ustva or tax_id
336 if ( $form->{"taxkey_pos_ustva_$tk_count"} eq '' && $form->{"taxkey_tax_$tk_count"} == 0 ) {
341 # Add valid taxkeys into the array
344 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
345 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
346 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
347 chart_id => conv_i($form->{"id"}),
348 pos_ustva => $form->{"taxkey_pos_ustva_$tk_count"},
349 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
356 for my $j (0 .. $#taxkeys){
357 if ( defined $taxkeys[$j]{'id'} ){
360 if ($taxkeys[$j]{'delete'}){
362 DELETE FROM taxkeys WHERE id = ?
365 @values = ($taxkeys[$j]{'id'});
367 do_query($form, $dbh, $query, @values);
376 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
384 $taxkeys[$j]{'tax_id'},
385 $taxkeys[$j]{'chart_id'},
386 $taxkeys[$j]{'tax_id'},
387 $taxkeys[$j]{'pos_ustva'},
388 $taxkeys[$j]{'startdate'},
391 do_query($form, $dbh, $query, @values);
397 INSERT INTO taxkeys (
404 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
407 $taxkeys[$j]{'tax_id'},
408 $taxkeys[$j]{'chart_id'},
409 $taxkeys[$j]{'tax_id'},
410 $taxkeys[$j]{'pos_ustva'},
411 $taxkeys[$j]{'startdate'},
414 do_query($form, $dbh, $query, @values);
420 my $rc = $dbh->commit;
423 $main::lxdebug->leave_sub();
429 $main::lxdebug->enter_sub();
431 my ($self, $myconfig, $form) = @_;
433 # connect to database, turn off AutoCommit
434 my $dbh = $form->dbconnect_noauto($myconfig);
436 my $query = qq|SELECT count(*) FROM acc_trans a
437 WHERE a.chart_id = ?|;
438 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
442 $main::lxdebug->leave_sub();
446 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
447 foreach my $type (qw(inventory income expense)) {
450 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
451 qq|WHERE ${type}_accno_id = ?|;
452 do_query($form, $dbh, $query, $form->{id});
455 foreach my $table (qw(partstax customertax vendortax tax)) {
456 $query = qq|DELETE FROM $table
458 do_query($form, $dbh, $query, $form->{id});
461 # delete chart of account record
462 $query = qq|DELETE FROM chart
464 do_query($form, $dbh, $query, $form->{id});
466 # delete account taxkeys
467 $query = qq|DELETE FROM taxkeys
469 do_query($form, $dbh, $query, $form->{id});
471 # commit and redirect
472 my $rc = $dbh->commit;
475 $main::lxdebug->leave_sub();
481 $main::lxdebug->enter_sub();
483 my ($self, $myconfig, $form) = @_;
485 # connect to database
486 my $dbh = $form->dbconnect($myconfig);
488 my $query = qq|SELECT d.id, d.description, d.role
492 $sth = $dbh->prepare($query);
493 $sth->execute || $form->dberror($query);
496 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
497 push @{ $form->{ALL} }, $ref;
503 $main::lxdebug->leave_sub();
507 $main::lxdebug->enter_sub();
509 my ($self, $myconfig, $form) = @_;
511 # connect to database
512 my $dbh = $form->dbconnect($myconfig);
514 my $query = qq|SELECT d.description, d.role
517 my $sth = $dbh->prepare($query);
518 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
520 my $ref = $sth->fetchrow_hashref(NAME_lc);
522 map { $form->{$_} = $ref->{$_} } keys %$ref;
526 # see if it is in use
527 $query = qq|SELECT count(*) FROM dpt_trans d
528 WHERE d.department_id = ?|;
529 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
531 $form->{orphaned} = !$form->{orphaned};
536 $main::lxdebug->leave_sub();
539 sub save_department {
540 $main::lxdebug->enter_sub();
542 my ($self, $myconfig, $form) = @_;
544 # connect to database
545 my $dbh = $form->dbconnect($myconfig);
547 my @values = ($form->{description}, $form->{role});
549 $query = qq|UPDATE department SET
550 description = ?, role = ?
552 push(@values, $form->{id});
554 $query = qq|INSERT INTO department
558 do_query($form, $dbh, $query, @values);
562 $main::lxdebug->leave_sub();
565 sub delete_department {
566 $main::lxdebug->enter_sub();
568 my ($self, $myconfig, $form) = @_;
570 # connect to database
571 my $dbh = $form->dbconnect($myconfig);
573 $query = qq|DELETE FROM department
575 do_query($form, $dbh, $query, $form->{id});
579 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my ($self, $myconfig, $form) = @_;
587 # connect to database
588 my $dbh = $form->dbconnect($myconfig);
590 my $query = qq|SELECT id, lead
594 $sth = $dbh->prepare($query);
595 $sth->execute || $form->dberror($query);
597 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
598 push @{ $form->{ALL} }, $ref;
604 $main::lxdebug->leave_sub();
608 $main::lxdebug->enter_sub();
610 my ($self, $myconfig, $form) = @_;
612 # connect to database
613 my $dbh = $form->dbconnect($myconfig);
616 qq|SELECT l.id, l.lead | .
619 my $sth = $dbh->prepare($query);
620 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
622 my $ref = $sth->fetchrow_hashref(NAME_lc);
624 map { $form->{$_} = $ref->{$_} } keys %$ref;
630 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub();
636 my ($self, $myconfig, $form) = @_;
638 # connect to database
639 my $dbh = $form->dbconnect($myconfig);
641 my @values = ($form->{description});
642 # id is the old record
644 $query = qq|UPDATE leads SET
647 puhs(@values, $form->{id});
649 $query = qq|INSERT INTO leads
653 do_query($form, $dbh, $query, @values);
657 $main::lxdebug->leave_sub();
661 $main::lxdebug->enter_sub();
663 my ($self, $myconfig, $form) = @_;
665 # connect to database
666 my $dbh = $form->dbconnect($myconfig);
668 $query = qq|DELETE FROM leads
670 do_query($form, $dbh, $query, $form->{id});
674 $main::lxdebug->leave_sub();
678 $main::lxdebug->enter_sub();
680 my ($self, $myconfig, $form) = @_;
682 # connect to database
683 my $dbh = $form->dbconnect($myconfig);
685 my $query = qq|SELECT id, description, discount, customernumberinit
689 $sth = $dbh->prepare($query);
690 $sth->execute || $form->dberror($query);
692 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
693 push @{ $form->{ALL} }, $ref;
699 $main::lxdebug->leave_sub();
703 $main::lxdebug->enter_sub();
705 my ($self, $myconfig, $form) = @_;
707 # connect to database
708 my $dbh = $form->dbconnect($myconfig);
711 qq|SELECT b.description, b.discount, b.customernumberinit
714 my $sth = $dbh->prepare($query);
715 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
717 my $ref = $sth->fetchrow_hashref(NAME_lc);
719 map { $form->{$_} = $ref->{$_} } keys %$ref;
725 $main::lxdebug->leave_sub();
729 $main::lxdebug->enter_sub();
731 my ($self, $myconfig, $form) = @_;
733 # connect to database
734 my $dbh = $form->dbconnect($myconfig);
736 my @values = ($form->{description}, $form->{discount},
737 $form->{customernumberinit});
738 # id is the old record
740 $query = qq|UPDATE business SET
743 customernumberinit = ?
745 push(@values, $form->{id});
747 $query = qq|INSERT INTO business
748 (description, discount, customernumberinit)
751 do_query($form, $dbh, $query, @values);
755 $main::lxdebug->leave_sub();
758 sub delete_business {
759 $main::lxdebug->enter_sub();
761 my ($self, $myconfig, $form) = @_;
763 # connect to database
764 my $dbh = $form->dbconnect($myconfig);
766 $query = qq|DELETE FROM business
768 do_query($form, $dbh, $query, $form->{id});
772 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 my ($self, $myconfig, $form, $return_list) = @_;
781 # connect to database
782 my $dbh = $form->dbconnect($myconfig);
785 "SELECT id, description, template_code, article_code, " .
786 " output_numberformat, output_dateformat, output_longdates " .
787 "FROM language ORDER BY description";
789 $sth = $dbh->prepare($query);
790 $sth->execute || $form->dberror($query);
794 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
795 push(@{ $ary }, $ref);
801 $main::lxdebug->leave_sub();
811 $main::lxdebug->enter_sub();
813 my ($self, $myconfig, $form) = @_;
815 # connect to database
816 my $dbh = $form->dbconnect($myconfig);
819 "SELECT description, template_code, article_code, " .
820 " output_numberformat, output_dateformat, output_longdates " .
821 "FROM language WHERE id = ?";
822 my $sth = $dbh->prepare($query);
823 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
825 my $ref = $sth->fetchrow_hashref(NAME_lc);
827 map { $form->{$_} = $ref->{$_} } keys %$ref;
833 $main::lxdebug->leave_sub();
836 sub get_language_details {
837 $main::lxdebug->enter_sub();
839 my ($self, $myconfig, $form, $id) = @_;
841 # connect to database
842 my $dbh = $form->dbconnect($myconfig);
845 "SELECT template_code, " .
846 " output_numberformat, output_dateformat, output_longdates " .
847 "FROM language WHERE id = ?";
848 my @res = selectrow_query($form, $dbh, $query, $id);
851 $main::lxdebug->leave_sub();
857 $main::lxdebug->enter_sub();
859 my ($self, $myconfig, $form) = @_;
861 # connect to database
862 my $dbh = $form->dbconnect($myconfig);
863 my (@values, $query);
865 map({ push(@values, $form->{$_}); }
866 qw(description template_code article_code
867 output_numberformat output_dateformat output_longdates));
869 # id is the old record
872 "UPDATE language SET " .
873 " description = ?, template_code = ?, article_code = ?, " .
874 " output_numberformat = ?, output_dateformat = ?, " .
875 " output_longdates = ? " .
877 push(@values, $form->{id});
880 "INSERT INTO language (" .
881 " description, template_code, article_code, " .
882 " output_numberformat, output_dateformat, output_longdates" .
883 ") VALUES (?, ?, ?, ?, ?, ?)";
885 do_query($form, $dbh, $query, @values);
889 $main::lxdebug->leave_sub();
892 sub delete_language {
893 $main::lxdebug->enter_sub();
895 my ($self, $myconfig, $form) = @_;
897 # connect to database
898 my $dbh = $form->dbconnect_noauto($myconfig);
900 foreach my $table (qw(translation_payment_terms units_language)) {
901 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
902 do_query($form, $dbh, $query, $form->{"id"});
905 $query = "DELETE FROM language WHERE id = ?";
906 do_query($form, $dbh, $query, $form->{"id"});
911 $main::lxdebug->leave_sub();
916 $main::lxdebug->enter_sub();
918 my ($self, $myconfig, $form) = @_;
920 # connect to database
921 my $dbh = $form->dbconnect($myconfig);
923 my $query = qq|SELECT id, description,
925 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
927 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
929 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
931 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
933 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
935 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
937 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
939 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
941 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
945 $sth = $dbh->prepare($query);
946 $sth->execute || $form->dberror($query);
949 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
950 push @{ $form->{ALL} }, $ref;
956 $main::lxdebug->leave_sub();
959 sub get_buchungsgruppe {
960 $main::lxdebug->enter_sub();
962 my ($self, $myconfig, $form) = @_;
965 # connect to database
966 my $dbh = $form->dbconnect($myconfig);
970 qq|SELECT description, inventory_accno_id,
971 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
973 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
975 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
977 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
979 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
981 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
983 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
985 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
987 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
990 my $sth = $dbh->prepare($query);
991 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
993 my $ref = $sth->fetchrow_hashref(NAME_lc);
995 map { $form->{$_} = $ref->{$_} } keys %$ref;
1000 qq|SELECT count(id) = 0 AS orphaned
1002 WHERE buchungsgruppen_id = ?|;
1003 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1006 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1008 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1009 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1012 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1013 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1014 FROM chart c, defaults d
1015 WHERE c.link LIKE '%$module%'
1019 my $sth = $dbh->prepare($query);
1020 $sth->execute || $form->dberror($query);
1021 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1022 foreach my $key (split(/:/, $ref->{link})) {
1023 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1024 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1026 if ($key =~ /$module/) {
1027 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1028 || ($ref->{id} eq $ref->{income_accno_id})
1029 || ($ref->{id} eq $ref->{expense_accno_id})) {
1030 push @{ $form->{"${module}_links"}{$key} },
1031 { accno => $ref->{accno},
1032 description => $ref->{description},
1033 selected => "selected",
1036 push @{ $form->{"${module}_links"}{$key} },
1037 { accno => $ref->{accno},
1038 description => $ref->{description},
1050 $main::lxdebug->leave_sub();
1053 sub save_buchungsgruppe {
1054 $main::lxdebug->enter_sub();
1056 my ($self, $myconfig, $form) = @_;
1058 # connect to database
1059 my $dbh = $form->dbconnect($myconfig);
1061 my @values = ($form->{description}, $form->{inventory_accno_id},
1062 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1063 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1064 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1065 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1069 # id is the old record
1071 $query = qq|UPDATE buchungsgruppen SET
1072 description = ?, inventory_accno_id = ?,
1073 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1074 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1075 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1076 income_accno_id_3 = ?, expense_accno_id_3 = ?
1078 push(@values, $form->{id});
1080 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1081 my ($sortkey) = $dbh->selectrow_array($query);
1082 $form->dberror($query) if ($dbh->err);
1083 push(@values, $sortkey);
1084 $query = qq|INSERT INTO buchungsgruppen
1085 (description, inventory_accno_id,
1086 income_accno_id_0, expense_accno_id_0,
1087 income_accno_id_1, expense_accno_id_1,
1088 income_accno_id_2, expense_accno_id_2,
1089 income_accno_id_3, expense_accno_id_3,
1091 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1093 do_query($form, $dbh, $query, @values);
1097 $main::lxdebug->leave_sub();
1100 sub delete_buchungsgruppe {
1101 $main::lxdebug->enter_sub();
1103 my ($self, $myconfig, $form) = @_;
1105 # connect to database
1106 my $dbh = $form->dbconnect($myconfig);
1108 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1109 do_query($form, $dbh, $query, $form->{id});
1113 $main::lxdebug->leave_sub();
1117 $main::lxdebug->enter_sub();
1119 my ($self, $myconfig, $form, $table) = @_;
1121 # connect to database
1122 my $dbh = $form->get_standard_dbh($myconfig);
1126 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1127 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1128 my @values = ($form->{"id1"}, $form->{"id2"});
1129 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1131 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1132 my $sth = prepare_query($form, $dbh, $query);
1134 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1135 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1141 $main::lxdebug->leave_sub();
1145 $main::lxdebug->enter_sub();
1147 my ($self, $myconfig, $form) = @_;
1149 # connect to database
1150 my $dbh = $form->dbconnect($myconfig);
1152 my $query = qq|SELECT id, printer_description, template_code, printer_command
1156 $sth = $dbh->prepare($query);
1157 $sth->execute || $form->dberror($query);
1159 $form->{"ALL"} = [];
1160 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1161 push @{ $form->{ALL} }, $ref;
1167 $main::lxdebug->leave_sub();
1171 $main::lxdebug->enter_sub();
1173 my ($self, $myconfig, $form) = @_;
1175 # connect to database
1176 my $dbh = $form->dbconnect($myconfig);
1179 qq|SELECT p.printer_description, p.template_code, p.printer_command
1182 my $sth = $dbh->prepare($query);
1183 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1185 my $ref = $sth->fetchrow_hashref(NAME_lc);
1187 map { $form->{$_} = $ref->{$_} } keys %$ref;
1193 $main::lxdebug->leave_sub();
1197 $main::lxdebug->enter_sub();
1199 my ($self, $myconfig, $form) = @_;
1201 # connect to database
1202 my $dbh = $form->dbconnect($myconfig);
1204 my @values = ($form->{printer_description},
1205 $form->{template_code},
1206 $form->{printer_command});
1208 # id is the old record
1210 $query = qq|UPDATE printers SET
1211 printer_description = ?, template_code = ?, printer_command = ?
1213 push(@values, $form->{id});
1215 $query = qq|INSERT INTO printers
1216 (printer_description, template_code, printer_command)
1219 do_query($form, $dbh, $query, @values);
1223 $main::lxdebug->leave_sub();
1226 sub delete_printer {
1227 $main::lxdebug->enter_sub();
1229 my ($self, $myconfig, $form) = @_;
1231 # connect to database
1232 my $dbh = $form->dbconnect($myconfig);
1234 $query = qq|DELETE FROM printers
1236 do_query($form, $dbh, $query, $form->{id});
1240 $main::lxdebug->leave_sub();
1244 $main::lxdebug->enter_sub();
1246 my ($self, $myconfig, $form) = @_;
1248 # connect to database
1249 my $dbh = $form->dbconnect($myconfig);
1251 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1253 $sth = $dbh->prepare($query);
1254 $sth->execute || $form->dberror($query);
1257 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1258 push @{ $form->{ALL} }, $ref;
1264 $main::lxdebug->leave_sub();
1268 $main::lxdebug->enter_sub();
1270 my ($self, $myconfig, $form) = @_;
1272 # connect to database
1273 my $dbh = $form->dbconnect($myconfig);
1275 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1276 my $sth = $dbh->prepare($query);
1277 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1279 my $ref = $sth->fetchrow_hashref(NAME_lc);
1280 map { $form->{$_} = $ref->{$_} } keys %$ref;
1284 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1285 qq|FROM translation_payment_terms t | .
1286 qq|LEFT JOIN language l ON t.language_id = l.id | .
1287 qq|WHERE t.payment_terms_id = ? | .
1289 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1290 qq| l.description AS language | .
1291 qq|FROM language l|;
1292 $sth = $dbh->prepare($query);
1293 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1296 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1297 $mapping{ $ref->{"language_id"} } = $ref
1298 unless (defined($mapping{ $ref->{"language_id"} }));
1302 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1307 $main::lxdebug->leave_sub();
1311 $main::lxdebug->enter_sub();
1313 my ($self, $myconfig, $form) = @_;
1315 # connect to database
1316 my $dbh = $form->dbconnect_noauto($myconfig);
1321 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1322 qq|FROM payment_terms|;
1324 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1326 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1327 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1331 qq|DELETE FROM translation_payment_terms | .
1332 qq|WHERE payment_terms_id = ?|;
1333 do_query($form, $dbh, $query, $form->{"id"});
1336 $query = qq|UPDATE payment_terms SET
1337 description = ?, description_long = ?,
1338 terms_netto = ?, terms_skonto = ?,
1341 my @values = ($form->{description}, $form->{description_long},
1342 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1343 $form->{percent_skonto} * 1,
1345 do_query($form, $dbh, $query, @values);
1347 $query = qq|SELECT id FROM language|;
1349 my $sth = $dbh->prepare($query);
1350 $sth->execute() || $form->dberror($query);
1352 while (my ($id) = $sth->fetchrow_array()) {
1353 push(@language_ids, $id);
1358 qq|INSERT INTO translation_payment_terms | .
1359 qq|(language_id, payment_terms_id, description_long) | .
1360 qq|VALUES (?, ?, ?)|;
1361 $sth = $dbh->prepare($query);
1363 foreach my $language_id (@language_ids) {
1364 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1365 $form->{"description_long_${language_id}"});
1372 $main::lxdebug->leave_sub();
1375 sub delete_payment {
1376 $main::lxdebug->enter_sub();
1378 my ($self, $myconfig, $form) = @_;
1380 # connect to database
1381 my $dbh = $form->dbconnect_noauto($myconfig);
1384 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1385 do_query($form, $dbh, $query, $form->{"id"});
1387 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1388 do_query($form, $dbh, $query, $form->{"id"});
1393 $main::lxdebug->leave_sub();
1397 sub prepare_template_filename {
1398 $main::lxdebug->enter_sub();
1400 my ($self, $myconfig, $form) = @_;
1402 my ($filename, $display_filename);
1404 if ($form->{type} eq "stylesheet") {
1405 $filename = "css/$myconfig->{stylesheet}";
1406 $display_filename = $myconfig->{stylesheet};
1409 $filename = $form->{formname};
1411 if ($form->{language}) {
1412 my ($id, $template_code) = split(/--/, $form->{language});
1413 $filename .= "_${template_code}";
1416 if ($form->{printer}) {
1417 my ($id, $template_code) = split(/--/, $form->{printer});
1418 $filename .= "_${template_code}";
1421 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1422 $filename =~ s|.*/||;
1423 $display_filename = $filename;
1424 $filename = "$myconfig->{templates}/$filename";
1427 $main::lxdebug->leave_sub();
1429 return ($filename, $display_filename);
1434 $main::lxdebug->enter_sub();
1436 my ($self, $filename) = @_;
1438 my ($content, $lines) = ("", 0);
1442 if (open(TEMPLATE, $filename)) {
1443 while (<TEMPLATE>) {
1450 $main::lxdebug->leave_sub();
1452 return ($content, $lines);
1456 $main::lxdebug->enter_sub();
1458 my ($self, $filename, $content) = @_;
1464 if (open(TEMPLATE, ">$filename")) {
1465 $content =~ s/\r\n/\n/g;
1466 print(TEMPLATE $content);
1472 $main::lxdebug->leave_sub();
1477 sub save_preferences {
1478 $main::lxdebug->enter_sub();
1480 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1482 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1483 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1486 $form->{curr} =~ s/ //g;
1487 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1488 $form->{curr} = join ':', @a;
1490 # connect to database
1491 my $dbh = $form->dbconnect_noauto($myconfig);
1493 # these defaults are database wide
1494 # user specific variables are in myconfig
1497 qq|UPDATE defaults SET | .
1498 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1499 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1500 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1501 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1502 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1503 qq|invnumber = ?, | .
1504 qq|cnnumber = ?, | .
1505 qq|sonumber = ?, | .
1506 qq|ponumber = ?, | .
1507 qq|sqnumber = ?, | .
1508 qq|rfqnumber = ?, | .
1509 qq|customernumber = ?, | .
1510 qq|vendornumber = ?, | .
1511 qq|articlenumber = ?, | .
1512 qq|servicenumber = ?, | .
1515 qq|businessnumber = ?|;
1516 my @values = ($form->{inventory_accno}, $form->{income_accno},
1517 $form->{expense_accno},
1518 $form->{fxgain_accno}, $form->{fxloss_accno},
1519 $form->{invnumber}, $form->{cnnumber},
1520 $form->{sonumber}, $form->{ponumber},
1521 $form->{sqnumber}, $form->{rfqnumber},
1522 $form->{customernumber}, $form->{vendornumber},
1523 $form->{articlenumber}, $form->{servicenumber},
1524 $form->{yearend}, $form->{curr},
1525 $form->{businessnumber});
1526 do_query($form, $dbh, $query, @values);
1529 $query = qq|UPDATE employee
1532 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1534 my $rc = $dbh->commit;
1537 # save first currency in myconfig
1538 $form->{currency} = substr($form->{curr}, 0, 3);
1540 $myconfig = new User "$memberfile", "$form->{login}";
1542 foreach my $item (keys %$form) {
1543 $myconfig->{$item} = $form->{$item};
1546 $myconfig->save_member($memberfile, $userspath);
1550 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1551 foreach $directory (@webdavdirs) {
1552 $file = "webdav/" . $directory . "/webdav-user";
1553 if ($myconfig->{$directory}) {
1554 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1555 while (<HTACCESS>) {
1556 ($login, $password) = split(/:/, $_);
1557 if ($login ne $form->{login}) {
1562 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1563 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1564 print(HTACCESS $newfile);
1567 $form->{$directory} = 0;
1568 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1569 while (<HTACCESS>) {
1570 ($login, $password) = split(/:/, $_);
1571 if ($login ne $form->{login}) {
1576 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1577 print(HTACCESS $newfile);
1583 $main::lxdebug->leave_sub();
1588 sub defaultaccounts {
1589 $main::lxdebug->enter_sub();
1591 my ($self, $myconfig, $form) = @_;
1593 # connect to database
1594 my $dbh = $form->dbconnect($myconfig);
1596 # get defaults from defaults table
1597 my $query = qq|SELECT * FROM defaults|;
1598 my $sth = $dbh->prepare($query);
1599 $sth->execute || $form->dberror($query);
1601 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1602 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1603 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1604 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1605 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1606 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1610 $query = qq|SELECT c.id, c.accno, c.description, c.link
1612 WHERE c.link LIKE '%IC%'
1614 $sth = $dbh->prepare($query);
1615 $sth->execute || $self->dberror($query);
1617 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1618 foreach my $key (split(/:/, $ref->{link})) {
1621 if ($key =~ /cogs/) {
1622 $nkey = "IC_expense";
1624 if ($key =~ /sale/) {
1625 $nkey = "IC_income";
1627 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1629 description => $ref->{description}
1636 $query = qq|SELECT c.id, c.accno, c.description
1638 WHERE c.category = 'I'
1639 AND c.charttype = 'A'
1641 $sth = $dbh->prepare($query);
1642 $sth->execute || $self->dberror($query);
1644 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1645 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1647 description => $ref->{description}
1652 $query = qq|SELECT c.id, c.accno, c.description
1654 WHERE c.category = 'E'
1655 AND c.charttype = 'A'
1657 $sth = $dbh->prepare($query);
1658 $sth->execute || $self->dberror($query);
1660 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1661 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1663 description => $ref->{description}
1668 # now get the tax rates and numbers
1669 $query = qq|SELECT c.id, c.accno, c.description,
1670 t.rate * 100 AS rate, t.taxnumber
1672 WHERE c.id = t.chart_id|;
1674 $sth = $dbh->prepare($query);
1675 $sth->execute || $form->dberror($query);
1677 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1678 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1679 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1680 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1681 if $ref->{taxnumber};
1682 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1688 $main::lxdebug->leave_sub();
1692 $main::lxdebug->enter_sub();
1694 my ($self, $myconfig, $form) = @_;
1696 my $dbh = $form->dbconnect($myconfig);
1698 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1699 my $sth = $dbh->prepare($query);
1700 $sth->execute || $form->dberror($query);
1702 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1708 $main::lxdebug->leave_sub();
1712 $main::lxdebug->enter_sub();
1714 my ($self, $myconfig, $form) = @_;
1716 my $dbh = $form->dbconnect($myconfig);
1718 my ($query, @values);
1720 if ($form->{revtrans}) {
1721 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1723 } elsif ($form->{closedto}) {
1724 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1725 @values = (conv_date($form->{closedto}));
1728 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1731 # set close in defaults
1732 do_query($form, $dbh, $query, @values);
1736 $main::lxdebug->leave_sub();
1740 my ($self, $units, $unit_name, $factor) = @_;
1742 $factor = 1 unless ($factor);
1744 my $unit = $units->{$unit_name};
1746 if (!defined($unit) || !$unit->{"base_unit"} ||
1747 ($unit_name eq $unit->{"base_unit"})) {
1748 return ($unit_name, $factor);
1751 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1754 sub retrieve_units {
1755 $main::lxdebug->enter_sub();
1757 my ($self, $myconfig, $form, $type, $prefix) = @_;
1759 my $dbh = $form->dbconnect($myconfig);
1761 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1764 $query .= " WHERE (type = ?)";
1768 my $sth = $dbh->prepare($query);
1769 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1772 while (my $ref = $sth->fetchrow_hashref()) {
1773 $units->{$ref->{"name"}} = $ref;
1777 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1778 $sth = $dbh->prepare($query_lang);
1779 $sth->execute() || $form->dberror($query_lang);
1781 while ($ref = $sth->fetchrow_hashref()) {
1782 push(@languages, $ref);
1786 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1787 "FROM units_language ul " .
1788 "LEFT JOIN language l ON ul.language_id = l.id " .
1789 "WHERE ul.unit = ?";
1790 $sth = $dbh->prepare($query_lang);
1792 foreach my $unit (values(%{$units})) {
1793 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1795 $unit->{"LANGUAGES"} = {};
1796 foreach my $lang (@languages) {
1797 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1800 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1801 while ($ref = $sth->fetchrow_hashref()) {
1802 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1809 $main::lxdebug->leave_sub();
1814 sub translate_units {
1815 $main::lxdebug->enter_sub();
1817 my ($self, $form, $template_code, $unit, $amount) = @_;
1819 my $units = $self->retrieve_units(\%main::myconfig, $form);
1821 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1822 my $new_unit = $unit;
1824 if (($amount != 1) && $h->{"localized_plural"}) {
1825 $new_unit = $h->{"localized_plural"};
1826 } elsif ($h->{"localized"}) {
1827 $new_unit = $h->{"localized"};
1831 $main::lxdebug->leave_sub();
1837 $main::lxdebug->enter_sub();
1839 my ($self, $myconfig, $form, $units) = @_;
1841 my $dbh = $form->dbconnect($myconfig);
1843 map({ $_->{"in_use"} = 0; } values(%{$units}));
1845 foreach my $unit (values(%{$units})) {
1846 my $base_unit = $unit->{"original_base_unit"};
1847 while ($base_unit) {
1848 $units->{$base_unit}->{"in_use"} = 1;
1849 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1850 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1851 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1855 foreach my $unit (values(%{$units})) {
1856 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1858 foreach my $table (qw(parts invoice orderitems)) {
1859 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1861 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1862 $query .= "= " . $dbh->quote($unit->{"name"});
1864 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1865 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1868 my ($count) = $dbh->selectrow_array($query);
1869 $form->dberror($query) if ($dbh->err);
1872 $unit->{"in_use"} = 1;
1880 $main::lxdebug->leave_sub();
1883 # if $a is translatable to $b, return the factor between them.
1886 $main::lxdebug->enter_sub(2);
1887 ($this, $a, $b, $all_units) = @_;
1889 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1890 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1891 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1894 sub unit_select_data {
1895 $main::lxdebug->enter_sub();
1897 my ($self, $units, $selected, $empty_entry) = @_;
1902 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1905 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1906 push(@{$select}, { "name" => $unit,
1907 "base_unit" => $units->{$unit}->{"base_unit"},
1908 "factor" => $units->{$unit}->{"factor"},
1909 "selected" => ($unit eq $selected) ? "selected" : "" });
1912 $main::lxdebug->leave_sub();
1917 sub unit_select_html {
1918 $main::lxdebug->enter_sub();
1920 my ($self, $units, $name, $selected, $convertible_into) = @_;
1922 my $select = "<select name=${name}>";
1924 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1925 if (!$convertible_into ||
1926 ($units->{$convertible_into} &&
1927 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1928 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1931 $select .= "</select>";
1933 $main::lxdebug->leave_sub();
1939 $main::lxdebug->enter_sub();
1941 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1943 my $dbh = $form->dbconnect_noauto($myconfig);
1945 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1946 my ($sortkey) = selectrow_query($form, $dbh, $query);
1948 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1949 "VALUES (?, ?, ?, ?, ?)";
1950 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1953 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1954 my $sth = $dbh->prepare($query);
1955 foreach my $lang (@{$languages}) {
1956 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1957 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1965 $main::lxdebug->leave_sub();
1969 $main::lxdebug->enter_sub();
1971 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1973 my $dbh = $form->dbconnect_noauto($myconfig);
1975 my ($base_unit, $unit, $sth, $query);
1977 $query = "DELETE FROM units_language";
1978 $dbh->do($query) || $form->dberror($query);
1980 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1981 $query = "DELETE FROM units WHERE name IN (";
1982 map({ $query .= "?," } @{$delete_units});
1983 substr($query, -1, 1) = ")";
1984 $dbh->do($query, undef, @{$delete_units}) ||
1985 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1988 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1989 $sth = $dbh->prepare($query);
1991 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1992 my $sth_lang = $dbh->prepare($query_lang);
1994 foreach $unit (values(%{$units})) {
1995 $unit->{"depth"} = 0;
1996 my $base_unit = $unit;
1997 while ($base_unit->{"base_unit"}) {
1999 $base_unit = $units->{$base_unit->{"base_unit"}};
2003 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2004 if ($unit->{"LANGUAGES"}) {
2005 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2006 next unless ($lang->{"id"} && $lang->{"localized"});
2007 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2008 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2012 next if ($unit->{"unchanged_unit"});
2014 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2015 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2019 $sth_lang->finish();
2023 $main::lxdebug->leave_sub();
2027 $main::lxdebug->enter_sub();
2029 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2031 my $dbh = $form->dbconnect_noauto($myconfig);
2035 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2036 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2039 qq|SELECT sortkey FROM units | .
2040 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2041 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2042 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2044 if (defined($sortkey_1)) {
2045 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2046 my ($name_2) = selectrow_query($form, $dbh, $query);
2048 if (defined($name_2)) {
2049 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2050 my $sth = $dbh->prepare($query);
2052 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2053 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2060 $main::lxdebug->leave_sub();
2064 $main::lxdebug->enter_sub();
2066 my ($self, $myconfig, $form) = @_;
2068 # connect to database
2069 my $dbh = $form->dbconnect($myconfig);
2071 my $query = qq|SELECT
2075 round(t.rate * 100, 2) AS rate,
2076 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2077 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2081 $sth = $dbh->prepare($query);
2082 $sth->execute || $form->dberror($query);
2085 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2086 push @{ $form->{TAX} }, $ref;
2092 $main::lxdebug->leave_sub();
2095 sub get_tax_accounts {
2096 $main::lxdebug->enter_sub();
2098 my ($self, $myconfig, $form) = @_;
2100 my $dbh = $form->dbconnect($myconfig);
2102 # get Accounts from chart
2103 my $query = qq{ SELECT
2105 accno || ' - ' || description AS taxaccount
2107 WHERE link LIKE '%_tax%'
2111 $sth = $dbh->prepare($query);
2112 $sth->execute || $form->dberror($query);
2114 $form->{ACCOUNTS} = [];
2115 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2116 push @{ $form->{ACCOUNTS} }, $ref;
2123 $main::lxdebug->leave_sub();
2127 $main::lxdebug->enter_sub();
2129 my ($self, $myconfig, $form) = @_;
2131 # connect to database
2132 my $dbh = $form->dbconnect($myconfig);
2134 my $query = qq|SELECT
2137 round(rate * 100, 2) AS rate,
2142 my $sth = $dbh->prepare($query);
2143 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2145 my $ref = $sth->fetchrow_hashref(NAME_lc);
2147 map { $form->{$_} = $ref->{$_} } keys %$ref;
2151 # see if it is used by a taxkey
2152 $query = qq|SELECT count(*) FROM taxkeys
2153 WHERE tax_id = ? AND chart_id >0|;
2155 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2157 $form->{orphaned} = !$form->{orphaned};
2160 if (!$form->{orphaned} ) {
2161 $query = qq|SELECT DISTINCT c.id, c.accno
2163 JOIN tax t ON (t.id = tk.tax_id)
2164 JOIN chart c ON (c.id = tk.chart_id)
2165 WHERE tk.tax_id = ?|;
2167 $sth = $dbh->prepare($query);
2168 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2170 $form->{TAXINUSE} = [];
2171 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2172 push @{ $form->{TAXINUSE} }, $ref;
2180 $main::lxdebug->leave_sub();
2184 $main::lxdebug->enter_sub();
2186 my ($self, $myconfig, $form) = @_;
2188 # connect to database
2189 my $dbh = $form->get_standard_dbh($myconfig);
2191 $form->{rate} = $form->{rate} / 100;
2193 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2194 if ($form->{id} ne "") {
2195 $query = qq|UPDATE tax SET
2200 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2202 push(@values, $form->{id});
2206 $query = qq|INSERT INTO tax (
2213 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2215 do_query($form, $dbh, $query, @values);
2219 $main::lxdebug->leave_sub();
2223 $main::lxdebug->enter_sub();
2225 my ($self, $myconfig, $form) = @_;
2227 # connect to database
2228 my $dbh = $form->get_standard_dbh($myconfig);
2230 $query = qq|DELETE FROM tax
2232 do_query($form, $dbh, $query, $form->{id});
2236 $main::lxdebug->leave_sub();
2239 sub save_price_factor {
2240 $main::lxdebug->enter_sub();
2242 my ($self, $myconfig, $form) = @_;
2244 # connect to database
2245 my $dbh = $form->get_standard_dbh($myconfig);
2248 my @values = ($form->{description}, conv_i($form->{factor}));
2251 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2252 push @values, conv_i($form->{id});
2255 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2258 do_query($form, $dbh, $query, @values);
2262 $main::lxdebug->leave_sub();
2265 sub get_all_price_factors {
2266 $main::lxdebug->enter_sub();
2268 my ($self, $myconfig, $form) = @_;
2270 # connect to database
2271 my $dbh = $form->get_standard_dbh($myconfig);
2273 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2275 $main::lxdebug->leave_sub();
2278 sub get_price_factor {
2279 $main::lxdebug->enter_sub();
2281 my ($self, $myconfig, $form) = @_;
2283 # connect to database
2284 my $dbh = $form->get_standard_dbh($myconfig);
2286 my $query = qq|SELECT description, factor,
2287 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2288 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2289 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2290 FROM price_factors WHERE id = ?|;
2292 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2294 $main::lxdebug->leave_sub();
2297 sub delete_price_factor {
2298 $main::lxdebug->enter_sub();
2300 my ($self, $myconfig, $form) = @_;
2302 # connect to database
2303 my $dbh = $form->get_standard_dbh($myconfig);
2305 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2308 $main::lxdebug->leave_sub();