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();
1478 $main::lxdebug->enter_sub();
1483 my $myconfig = \%main::myconfig;
1484 my $form = $main::form;
1486 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1489 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1491 $form->{curr} =~ s/ //g;
1492 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1493 my $currency = join ':', @currencies;
1495 # these defaults are database wide
1498 qq|UPDATE defaults SET
1499 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1500 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1501 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1502 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1503 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1516 businessnumber = ?|;
1517 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1518 $accnos{fxgain_accno}, $accnos{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}, $currency,
1525 $form->{businessnumber});
1526 do_query($form, $dbh, $query, @values);
1530 $main::lxdebug->leave_sub();
1534 sub save_preferences {
1535 $main::lxdebug->enter_sub();
1537 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1539 my $dbh = $form->get_standard_dbh($myconfig);
1541 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1544 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1545 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1547 my $rc = $dbh->commit();
1549 # save first currency in myconfig
1550 $currency =~ s/:.*//;
1551 $form->{currency} = $currency;
1553 $form->{businessnumber} = $businessnumber;
1555 $myconfig = new User "$memberfile", "$form->{login}";
1557 foreach my $item (keys %$form) {
1558 $myconfig->{$item} = $form->{$item};
1561 $myconfig->save_member($memberfile, $userspath);
1565 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1566 foreach $directory (@webdavdirs) {
1567 $file = "webdav/" . $directory . "/webdav-user";
1568 if ($myconfig->{$directory}) {
1569 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1570 while (<HTACCESS>) {
1571 ($login, $password) = split(/:/, $_);
1572 if ($login ne $form->{login}) {
1577 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1578 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1579 print(HTACCESS $newfile);
1582 $form->{$directory} = 0;
1583 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1584 while (<HTACCESS>) {
1585 ($login, $password) = split(/:/, $_);
1586 if ($login ne $form->{login}) {
1591 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1592 print(HTACCESS $newfile);
1598 $main::lxdebug->leave_sub();
1603 sub defaultaccounts {
1604 $main::lxdebug->enter_sub();
1606 my ($self, $myconfig, $form) = @_;
1608 # connect to database
1609 my $dbh = $form->dbconnect($myconfig);
1611 # get defaults from defaults table
1612 my $query = qq|SELECT * FROM defaults|;
1613 my $sth = $dbh->prepare($query);
1614 $sth->execute || $form->dberror($query);
1616 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1617 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1618 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1619 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1620 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1621 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1625 $query = qq|SELECT c.id, c.accno, c.description, c.link
1627 WHERE c.link LIKE '%IC%'
1629 $sth = $dbh->prepare($query);
1630 $sth->execute || $self->dberror($query);
1632 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1633 foreach my $key (split(/:/, $ref->{link})) {
1636 if ($key =~ /cogs/) {
1637 $nkey = "IC_expense";
1639 if ($key =~ /sale/) {
1640 $nkey = "IC_income";
1642 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1644 description => $ref->{description}
1651 $query = qq|SELECT c.id, c.accno, c.description
1653 WHERE c.category = 'I'
1654 AND c.charttype = 'A'
1656 $sth = $dbh->prepare($query);
1657 $sth->execute || $self->dberror($query);
1659 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1660 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1662 description => $ref->{description}
1667 $query = qq|SELECT c.id, c.accno, c.description
1669 WHERE c.category = 'E'
1670 AND c.charttype = 'A'
1672 $sth = $dbh->prepare($query);
1673 $sth->execute || $self->dberror($query);
1675 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1676 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1678 description => $ref->{description}
1683 # now get the tax rates and numbers
1684 $query = qq|SELECT c.id, c.accno, c.description,
1685 t.rate * 100 AS rate, t.taxnumber
1687 WHERE c.id = t.chart_id|;
1689 $sth = $dbh->prepare($query);
1690 $sth->execute || $form->dberror($query);
1692 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1693 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1694 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1695 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1696 if $ref->{taxnumber};
1697 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1703 $main::lxdebug->leave_sub();
1707 $main::lxdebug->enter_sub();
1709 my ($self, $myconfig, $form) = @_;
1711 my $dbh = $form->dbconnect($myconfig);
1713 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1714 my $sth = $dbh->prepare($query);
1715 $sth->execute || $form->dberror($query);
1717 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1723 $main::lxdebug->leave_sub();
1727 $main::lxdebug->enter_sub();
1729 my ($self, $myconfig, $form) = @_;
1731 my $dbh = $form->dbconnect($myconfig);
1733 my ($query, @values);
1735 if ($form->{revtrans}) {
1736 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1738 } elsif ($form->{closedto}) {
1739 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1740 @values = (conv_date($form->{closedto}));
1743 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1746 # set close in defaults
1747 do_query($form, $dbh, $query, @values);
1751 $main::lxdebug->leave_sub();
1755 my ($self, $units, $unit_name, $factor) = @_;
1757 $factor = 1 unless ($factor);
1759 my $unit = $units->{$unit_name};
1761 if (!defined($unit) || !$unit->{"base_unit"} ||
1762 ($unit_name eq $unit->{"base_unit"})) {
1763 return ($unit_name, $factor);
1766 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1769 sub retrieve_units {
1770 $main::lxdebug->enter_sub();
1772 my ($self, $myconfig, $form, $type, $prefix) = @_;
1774 my $dbh = $form->dbconnect($myconfig);
1776 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1779 $query .= " WHERE (type = ?)";
1783 my $sth = $dbh->prepare($query);
1784 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1787 while (my $ref = $sth->fetchrow_hashref()) {
1788 $units->{$ref->{"name"}} = $ref;
1792 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1793 $sth = $dbh->prepare($query_lang);
1794 $sth->execute() || $form->dberror($query_lang);
1796 while ($ref = $sth->fetchrow_hashref()) {
1797 push(@languages, $ref);
1801 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1802 "FROM units_language ul " .
1803 "LEFT JOIN language l ON ul.language_id = l.id " .
1804 "WHERE ul.unit = ?";
1805 $sth = $dbh->prepare($query_lang);
1807 foreach my $unit (values(%{$units})) {
1808 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1810 $unit->{"LANGUAGES"} = {};
1811 foreach my $lang (@languages) {
1812 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1815 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1816 while ($ref = $sth->fetchrow_hashref()) {
1817 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1824 $main::lxdebug->leave_sub();
1829 sub translate_units {
1830 $main::lxdebug->enter_sub();
1832 my ($self, $form, $template_code, $unit, $amount) = @_;
1834 my $units = $self->retrieve_units(\%main::myconfig, $form);
1836 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1837 my $new_unit = $unit;
1839 if (($amount != 1) && $h->{"localized_plural"}) {
1840 $new_unit = $h->{"localized_plural"};
1841 } elsif ($h->{"localized"}) {
1842 $new_unit = $h->{"localized"};
1846 $main::lxdebug->leave_sub();
1852 $main::lxdebug->enter_sub();
1854 my ($self, $myconfig, $form, $units) = @_;
1856 my $dbh = $form->dbconnect($myconfig);
1858 map({ $_->{"in_use"} = 0; } values(%{$units}));
1860 foreach my $unit (values(%{$units})) {
1861 my $base_unit = $unit->{"original_base_unit"};
1862 while ($base_unit) {
1863 $units->{$base_unit}->{"in_use"} = 1;
1864 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1865 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1866 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1870 foreach my $unit (values(%{$units})) {
1871 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1873 foreach my $table (qw(parts invoice orderitems)) {
1874 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1876 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1877 $query .= "= " . $dbh->quote($unit->{"name"});
1879 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1880 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1883 my ($count) = $dbh->selectrow_array($query);
1884 $form->dberror($query) if ($dbh->err);
1887 $unit->{"in_use"} = 1;
1895 $main::lxdebug->leave_sub();
1898 # if $a is translatable to $b, return the factor between them.
1901 $main::lxdebug->enter_sub(2);
1902 ($this, $a, $b, $all_units) = @_;
1904 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1905 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1906 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1909 sub unit_select_data {
1910 $main::lxdebug->enter_sub();
1912 my ($self, $units, $selected, $empty_entry) = @_;
1917 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1920 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1921 push(@{$select}, { "name" => $unit,
1922 "base_unit" => $units->{$unit}->{"base_unit"},
1923 "factor" => $units->{$unit}->{"factor"},
1924 "selected" => ($unit eq $selected) ? "selected" : "" });
1927 $main::lxdebug->leave_sub();
1932 sub unit_select_html {
1933 $main::lxdebug->enter_sub();
1935 my ($self, $units, $name, $selected, $convertible_into) = @_;
1937 my $select = "<select name=${name}>";
1939 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1940 if (!$convertible_into ||
1941 ($units->{$convertible_into} &&
1942 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1943 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1946 $select .= "</select>";
1948 $main::lxdebug->leave_sub();
1954 $main::lxdebug->enter_sub();
1956 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1958 my $dbh = $form->dbconnect_noauto($myconfig);
1960 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1961 my ($sortkey) = selectrow_query($form, $dbh, $query);
1963 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1964 "VALUES (?, ?, ?, ?, ?)";
1965 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1968 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1969 my $sth = $dbh->prepare($query);
1970 foreach my $lang (@{$languages}) {
1971 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1972 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1980 $main::lxdebug->leave_sub();
1984 $main::lxdebug->enter_sub();
1986 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1988 my $dbh = $form->dbconnect_noauto($myconfig);
1990 my ($base_unit, $unit, $sth, $query);
1992 $query = "DELETE FROM units_language";
1993 $dbh->do($query) || $form->dberror($query);
1995 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1996 $query = "DELETE FROM units WHERE name IN (";
1997 map({ $query .= "?," } @{$delete_units});
1998 substr($query, -1, 1) = ")";
1999 $dbh->do($query, undef, @{$delete_units}) ||
2000 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2003 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2004 $sth = $dbh->prepare($query);
2006 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2007 my $sth_lang = $dbh->prepare($query_lang);
2009 foreach $unit (values(%{$units})) {
2010 $unit->{"depth"} = 0;
2011 my $base_unit = $unit;
2012 while ($base_unit->{"base_unit"}) {
2014 $base_unit = $units->{$base_unit->{"base_unit"}};
2018 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2019 if ($unit->{"LANGUAGES"}) {
2020 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2021 next unless ($lang->{"id"} && $lang->{"localized"});
2022 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2023 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2027 next if ($unit->{"unchanged_unit"});
2029 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2030 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2034 $sth_lang->finish();
2038 $main::lxdebug->leave_sub();
2042 $main::lxdebug->enter_sub();
2044 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2046 my $dbh = $form->dbconnect_noauto($myconfig);
2050 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2051 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2054 qq|SELECT sortkey FROM units | .
2055 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2056 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2057 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2059 if (defined($sortkey_1)) {
2060 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2061 my ($name_2) = selectrow_query($form, $dbh, $query);
2063 if (defined($name_2)) {
2064 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2065 my $sth = $dbh->prepare($query);
2067 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2068 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2075 $main::lxdebug->leave_sub();
2079 $main::lxdebug->enter_sub();
2081 my ($self, $myconfig, $form) = @_;
2083 # connect to database
2084 my $dbh = $form->dbconnect($myconfig);
2086 my $query = qq|SELECT
2090 round(t.rate * 100, 2) AS rate,
2091 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2092 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2096 $sth = $dbh->prepare($query);
2097 $sth->execute || $form->dberror($query);
2100 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2101 push @{ $form->{TAX} }, $ref;
2107 $main::lxdebug->leave_sub();
2110 sub get_tax_accounts {
2111 $main::lxdebug->enter_sub();
2113 my ($self, $myconfig, $form) = @_;
2115 my $dbh = $form->dbconnect($myconfig);
2117 # get Accounts from chart
2118 my $query = qq{ SELECT
2120 accno || ' - ' || description AS taxaccount
2122 WHERE link LIKE '%_tax%'
2126 $sth = $dbh->prepare($query);
2127 $sth->execute || $form->dberror($query);
2129 $form->{ACCOUNTS} = [];
2130 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2131 push @{ $form->{ACCOUNTS} }, $ref;
2138 $main::lxdebug->leave_sub();
2142 $main::lxdebug->enter_sub();
2144 my ($self, $myconfig, $form) = @_;
2146 # connect to database
2147 my $dbh = $form->dbconnect($myconfig);
2149 my $query = qq|SELECT
2152 round(rate * 100, 2) AS rate,
2157 my $sth = $dbh->prepare($query);
2158 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2160 my $ref = $sth->fetchrow_hashref(NAME_lc);
2162 map { $form->{$_} = $ref->{$_} } keys %$ref;
2166 # see if it is used by a taxkey
2167 $query = qq|SELECT count(*) FROM taxkeys
2168 WHERE tax_id = ? AND chart_id >0|;
2170 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2172 $form->{orphaned} = !$form->{orphaned};
2175 if (!$form->{orphaned} ) {
2176 $query = qq|SELECT DISTINCT c.id, c.accno
2178 JOIN tax t ON (t.id = tk.tax_id)
2179 JOIN chart c ON (c.id = tk.chart_id)
2180 WHERE tk.tax_id = ?|;
2182 $sth = $dbh->prepare($query);
2183 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2185 $form->{TAXINUSE} = [];
2186 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2187 push @{ $form->{TAXINUSE} }, $ref;
2195 $main::lxdebug->leave_sub();
2199 $main::lxdebug->enter_sub();
2201 my ($self, $myconfig, $form) = @_;
2203 # connect to database
2204 my $dbh = $form->get_standard_dbh($myconfig);
2206 $form->{rate} = $form->{rate} / 100;
2208 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2209 if ($form->{id} ne "") {
2210 $query = qq|UPDATE tax SET
2215 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2217 push(@values, $form->{id});
2221 $query = qq|INSERT INTO tax (
2228 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2230 do_query($form, $dbh, $query, @values);
2234 $main::lxdebug->leave_sub();
2238 $main::lxdebug->enter_sub();
2240 my ($self, $myconfig, $form) = @_;
2242 # connect to database
2243 my $dbh = $form->get_standard_dbh($myconfig);
2245 $query = qq|DELETE FROM tax
2247 do_query($form, $dbh, $query, $form->{id});
2251 $main::lxdebug->leave_sub();
2254 sub save_price_factor {
2255 $main::lxdebug->enter_sub();
2257 my ($self, $myconfig, $form) = @_;
2259 # connect to database
2260 my $dbh = $form->get_standard_dbh($myconfig);
2263 my @values = ($form->{description}, conv_i($form->{factor}));
2266 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2267 push @values, conv_i($form->{id});
2270 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2273 do_query($form, $dbh, $query, @values);
2277 $main::lxdebug->leave_sub();
2280 sub get_all_price_factors {
2281 $main::lxdebug->enter_sub();
2283 my ($self, $myconfig, $form) = @_;
2285 # connect to database
2286 my $dbh = $form->get_standard_dbh($myconfig);
2288 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2290 $main::lxdebug->leave_sub();
2293 sub get_price_factor {
2294 $main::lxdebug->enter_sub();
2296 my ($self, $myconfig, $form) = @_;
2298 # connect to database
2299 my $dbh = $form->get_standard_dbh($myconfig);
2301 my $query = qq|SELECT description, factor,
2302 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2303 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2304 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2305 FROM price_factors WHERE id = ?|;
2307 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2309 $main::lxdebug->leave_sub();
2312 sub delete_price_factor {
2313 $main::lxdebug->enter_sub();
2315 my ($self, $myconfig, $form) = @_;
2317 # connect to database
2318 my $dbh = $form->get_standard_dbh($myconfig);
2320 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2323 $main::lxdebug->leave_sub();