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 # Add valid taxkeys into the array
338 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
339 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
340 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
341 chart_id => conv_i($form->{"id"}),
342 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
343 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
350 for my $j (0 .. $#taxkeys){
351 if ( defined $taxkeys[$j]{'id'} ){
354 if ($taxkeys[$j]{'delete'}){
356 DELETE FROM taxkeys WHERE id = ?
359 @values = ($taxkeys[$j]{'id'});
361 do_query($form, $dbh, $query, @values);
370 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
378 $taxkeys[$j]{'tax_id'},
379 $taxkeys[$j]{'chart_id'},
380 $taxkeys[$j]{'tax_id'},
381 $taxkeys[$j]{'pos_ustva'},
382 $taxkeys[$j]{'startdate'},
385 do_query($form, $dbh, $query, @values);
391 INSERT INTO taxkeys (
398 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
401 $taxkeys[$j]{'tax_id'},
402 $taxkeys[$j]{'chart_id'},
403 $taxkeys[$j]{'tax_id'},
404 $taxkeys[$j]{'pos_ustva'},
405 $taxkeys[$j]{'startdate'},
408 do_query($form, $dbh, $query, @values);
414 my $rc = $dbh->commit;
417 $main::lxdebug->leave_sub();
423 $main::lxdebug->enter_sub();
425 my ($self, $myconfig, $form) = @_;
427 # connect to database, turn off AutoCommit
428 my $dbh = $form->dbconnect_noauto($myconfig);
430 my $query = qq|SELECT count(*) FROM acc_trans a
431 WHERE a.chart_id = ?|;
432 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
436 $main::lxdebug->leave_sub();
440 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
441 foreach my $type (qw(inventory income expense)) {
444 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
445 qq|WHERE ${type}_accno_id = ?|;
446 do_query($form, $dbh, $query, $form->{id});
449 foreach my $table (qw(partstax customertax vendortax tax)) {
450 $query = qq|DELETE FROM $table
452 do_query($form, $dbh, $query, $form->{id});
455 # delete chart of account record
456 $query = qq|DELETE FROM chart
458 do_query($form, $dbh, $query, $form->{id});
460 # delete account taxkeys
461 $query = qq|DELETE FROM taxkeys
463 do_query($form, $dbh, $query, $form->{id});
465 # commit and redirect
466 my $rc = $dbh->commit;
469 $main::lxdebug->leave_sub();
475 $main::lxdebug->enter_sub();
477 my ($self, $myconfig, $form) = @_;
479 # connect to database
480 my $dbh = $form->dbconnect($myconfig);
482 my $query = qq|SELECT d.id, d.description, d.role
486 $sth = $dbh->prepare($query);
487 $sth->execute || $form->dberror($query);
490 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
491 push @{ $form->{ALL} }, $ref;
497 $main::lxdebug->leave_sub();
501 $main::lxdebug->enter_sub();
503 my ($self, $myconfig, $form) = @_;
505 # connect to database
506 my $dbh = $form->dbconnect($myconfig);
508 my $query = qq|SELECT d.description, d.role
511 my $sth = $dbh->prepare($query);
512 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
514 my $ref = $sth->fetchrow_hashref(NAME_lc);
516 map { $form->{$_} = $ref->{$_} } keys %$ref;
520 # see if it is in use
521 $query = qq|SELECT count(*) FROM dpt_trans d
522 WHERE d.department_id = ?|;
523 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
525 $form->{orphaned} = !$form->{orphaned};
530 $main::lxdebug->leave_sub();
533 sub save_department {
534 $main::lxdebug->enter_sub();
536 my ($self, $myconfig, $form) = @_;
538 # connect to database
539 my $dbh = $form->dbconnect($myconfig);
541 my @values = ($form->{description}, $form->{role});
543 $query = qq|UPDATE department SET
544 description = ?, role = ?
546 push(@values, $form->{id});
548 $query = qq|INSERT INTO department
552 do_query($form, $dbh, $query, @values);
556 $main::lxdebug->leave_sub();
559 sub delete_department {
560 $main::lxdebug->enter_sub();
562 my ($self, $myconfig, $form) = @_;
564 # connect to database
565 my $dbh = $form->dbconnect($myconfig);
567 $query = qq|DELETE FROM department
569 do_query($form, $dbh, $query, $form->{id});
573 $main::lxdebug->leave_sub();
577 $main::lxdebug->enter_sub();
579 my ($self, $myconfig, $form) = @_;
581 # connect to database
582 my $dbh = $form->dbconnect($myconfig);
584 my $query = qq|SELECT id, lead
588 $sth = $dbh->prepare($query);
589 $sth->execute || $form->dberror($query);
591 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
592 push @{ $form->{ALL} }, $ref;
598 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my ($self, $myconfig, $form) = @_;
606 # connect to database
607 my $dbh = $form->dbconnect($myconfig);
610 qq|SELECT l.id, l.lead | .
613 my $sth = $dbh->prepare($query);
614 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
616 my $ref = $sth->fetchrow_hashref(NAME_lc);
618 map { $form->{$_} = $ref->{$_} } keys %$ref;
624 $main::lxdebug->leave_sub();
628 $main::lxdebug->enter_sub();
630 my ($self, $myconfig, $form) = @_;
632 # connect to database
633 my $dbh = $form->dbconnect($myconfig);
635 my @values = ($form->{description});
636 # id is the old record
638 $query = qq|UPDATE leads SET
641 puhs(@values, $form->{id});
643 $query = qq|INSERT INTO leads
647 do_query($form, $dbh, $query, @values);
651 $main::lxdebug->leave_sub();
655 $main::lxdebug->enter_sub();
657 my ($self, $myconfig, $form) = @_;
659 # connect to database
660 my $dbh = $form->dbconnect($myconfig);
662 $query = qq|DELETE FROM leads
664 do_query($form, $dbh, $query, $form->{id});
668 $main::lxdebug->leave_sub();
672 $main::lxdebug->enter_sub();
674 my ($self, $myconfig, $form) = @_;
676 # connect to database
677 my $dbh = $form->dbconnect($myconfig);
679 my $query = qq|SELECT id, description, discount, customernumberinit
683 $sth = $dbh->prepare($query);
684 $sth->execute || $form->dberror($query);
686 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
687 push @{ $form->{ALL} }, $ref;
693 $main::lxdebug->leave_sub();
697 $main::lxdebug->enter_sub();
699 my ($self, $myconfig, $form) = @_;
701 # connect to database
702 my $dbh = $form->dbconnect($myconfig);
705 qq|SELECT b.description, b.discount, b.customernumberinit
708 my $sth = $dbh->prepare($query);
709 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
711 my $ref = $sth->fetchrow_hashref(NAME_lc);
713 map { $form->{$_} = $ref->{$_} } keys %$ref;
719 $main::lxdebug->leave_sub();
723 $main::lxdebug->enter_sub();
725 my ($self, $myconfig, $form) = @_;
727 # connect to database
728 my $dbh = $form->dbconnect($myconfig);
730 my @values = ($form->{description}, $form->{discount},
731 $form->{customernumberinit});
732 # id is the old record
734 $query = qq|UPDATE business SET
737 customernumberinit = ?
739 push(@values, $form->{id});
741 $query = qq|INSERT INTO business
742 (description, discount, customernumberinit)
745 do_query($form, $dbh, $query, @values);
749 $main::lxdebug->leave_sub();
752 sub delete_business {
753 $main::lxdebug->enter_sub();
755 my ($self, $myconfig, $form) = @_;
757 # connect to database
758 my $dbh = $form->dbconnect($myconfig);
760 $query = qq|DELETE FROM business
762 do_query($form, $dbh, $query, $form->{id});
766 $main::lxdebug->leave_sub();
771 $main::lxdebug->enter_sub();
773 my ($self, $myconfig, $form, $return_list) = @_;
775 # connect to database
776 my $dbh = $form->dbconnect($myconfig);
779 "SELECT id, description, template_code, article_code, " .
780 " output_numberformat, output_dateformat, output_longdates " .
781 "FROM language ORDER BY description";
783 $sth = $dbh->prepare($query);
784 $sth->execute || $form->dberror($query);
788 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
789 push(@{ $ary }, $ref);
795 $main::lxdebug->leave_sub();
805 $main::lxdebug->enter_sub();
807 my ($self, $myconfig, $form) = @_;
809 # connect to database
810 my $dbh = $form->dbconnect($myconfig);
813 "SELECT description, template_code, article_code, " .
814 " output_numberformat, output_dateformat, output_longdates " .
815 "FROM language WHERE id = ?";
816 my $sth = $dbh->prepare($query);
817 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
819 my $ref = $sth->fetchrow_hashref(NAME_lc);
821 map { $form->{$_} = $ref->{$_} } keys %$ref;
827 $main::lxdebug->leave_sub();
830 sub get_language_details {
831 $main::lxdebug->enter_sub();
833 my ($self, $myconfig, $form, $id) = @_;
835 # connect to database
836 my $dbh = $form->dbconnect($myconfig);
839 "SELECT template_code, " .
840 " output_numberformat, output_dateformat, output_longdates " .
841 "FROM language WHERE id = ?";
842 my @res = selectrow_query($form, $dbh, $query, $id);
845 $main::lxdebug->leave_sub();
851 $main::lxdebug->enter_sub();
853 my ($self, $myconfig, $form) = @_;
855 # connect to database
856 my $dbh = $form->dbconnect($myconfig);
857 my (@values, $query);
859 map({ push(@values, $form->{$_}); }
860 qw(description template_code article_code
861 output_numberformat output_dateformat output_longdates));
863 # id is the old record
866 "UPDATE language SET " .
867 " description = ?, template_code = ?, article_code = ?, " .
868 " output_numberformat = ?, output_dateformat = ?, " .
869 " output_longdates = ? " .
871 push(@values, $form->{id});
874 "INSERT INTO language (" .
875 " description, template_code, article_code, " .
876 " output_numberformat, output_dateformat, output_longdates" .
877 ") VALUES (?, ?, ?, ?, ?, ?)";
879 do_query($form, $dbh, $query, @values);
883 $main::lxdebug->leave_sub();
886 sub delete_language {
887 $main::lxdebug->enter_sub();
889 my ($self, $myconfig, $form) = @_;
891 # connect to database
892 my $dbh = $form->dbconnect_noauto($myconfig);
894 foreach my $table (qw(translation_payment_terms units_language)) {
895 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
896 do_query($form, $dbh, $query, $form->{"id"});
899 $query = "DELETE FROM language WHERE id = ?";
900 do_query($form, $dbh, $query, $form->{"id"});
905 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub();
912 my ($self, $myconfig, $form) = @_;
914 # connect to database
915 my $dbh = $form->dbconnect($myconfig);
917 my $query = qq|SELECT id, description,
919 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
921 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
923 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
925 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
927 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
929 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
931 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
933 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
935 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
939 $sth = $dbh->prepare($query);
940 $sth->execute || $form->dberror($query);
943 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
944 push @{ $form->{ALL} }, $ref;
950 $main::lxdebug->leave_sub();
953 sub get_buchungsgruppe {
954 $main::lxdebug->enter_sub();
956 my ($self, $myconfig, $form) = @_;
959 # connect to database
960 my $dbh = $form->dbconnect($myconfig);
964 qq|SELECT description, inventory_accno_id,
965 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
967 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
969 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
971 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
973 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
975 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
977 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
979 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
981 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
984 my $sth = $dbh->prepare($query);
985 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
987 my $ref = $sth->fetchrow_hashref(NAME_lc);
989 map { $form->{$_} = $ref->{$_} } keys %$ref;
994 qq|SELECT count(id) = 0 AS orphaned
996 WHERE buchungsgruppen_id = ?|;
997 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1000 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1002 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1003 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1006 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1007 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1008 FROM chart c, defaults d
1009 WHERE c.link LIKE '%$module%'
1013 my $sth = $dbh->prepare($query);
1014 $sth->execute || $form->dberror($query);
1015 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1016 foreach my $key (split(/:/, $ref->{link})) {
1017 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1018 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1020 if ($key =~ /$module/) {
1021 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1022 || ($ref->{id} eq $ref->{income_accno_id})
1023 || ($ref->{id} eq $ref->{expense_accno_id})) {
1024 push @{ $form->{"${module}_links"}{$key} },
1025 { accno => $ref->{accno},
1026 description => $ref->{description},
1027 selected => "selected",
1030 push @{ $form->{"${module}_links"}{$key} },
1031 { accno => $ref->{accno},
1032 description => $ref->{description},
1044 $main::lxdebug->leave_sub();
1047 sub save_buchungsgruppe {
1048 $main::lxdebug->enter_sub();
1050 my ($self, $myconfig, $form) = @_;
1052 # connect to database
1053 my $dbh = $form->dbconnect($myconfig);
1055 my @values = ($form->{description}, $form->{inventory_accno_id},
1056 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1057 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1058 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1059 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1063 # id is the old record
1065 $query = qq|UPDATE buchungsgruppen SET
1066 description = ?, inventory_accno_id = ?,
1067 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1068 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1069 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1070 income_accno_id_3 = ?, expense_accno_id_3 = ?
1072 push(@values, $form->{id});
1074 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1075 my ($sortkey) = $dbh->selectrow_array($query);
1076 $form->dberror($query) if ($dbh->err);
1077 push(@values, $sortkey);
1078 $query = qq|INSERT INTO buchungsgruppen
1079 (description, inventory_accno_id,
1080 income_accno_id_0, expense_accno_id_0,
1081 income_accno_id_1, expense_accno_id_1,
1082 income_accno_id_2, expense_accno_id_2,
1083 income_accno_id_3, expense_accno_id_3,
1085 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1087 do_query($form, $dbh, $query, @values);
1091 $main::lxdebug->leave_sub();
1094 sub delete_buchungsgruppe {
1095 $main::lxdebug->enter_sub();
1097 my ($self, $myconfig, $form) = @_;
1099 # connect to database
1100 my $dbh = $form->dbconnect($myconfig);
1102 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1103 do_query($form, $dbh, $query, $form->{id});
1107 $main::lxdebug->leave_sub();
1111 $main::lxdebug->enter_sub();
1113 my ($self, $myconfig, $form, $table) = @_;
1115 # connect to database
1116 my $dbh = $form->get_standard_dbh($myconfig);
1120 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1121 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1122 my @values = ($form->{"id1"}, $form->{"id2"});
1123 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1125 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1126 my $sth = prepare_query($form, $dbh, $query);
1128 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1129 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1135 $main::lxdebug->leave_sub();
1139 $main::lxdebug->enter_sub();
1141 my ($self, $myconfig, $form) = @_;
1143 # connect to database
1144 my $dbh = $form->dbconnect($myconfig);
1146 my $query = qq|SELECT id, printer_description, template_code, printer_command
1150 $sth = $dbh->prepare($query);
1151 $sth->execute || $form->dberror($query);
1153 $form->{"ALL"} = [];
1154 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1155 push @{ $form->{ALL} }, $ref;
1161 $main::lxdebug->leave_sub();
1165 $main::lxdebug->enter_sub();
1167 my ($self, $myconfig, $form) = @_;
1169 # connect to database
1170 my $dbh = $form->dbconnect($myconfig);
1173 qq|SELECT p.printer_description, p.template_code, p.printer_command
1176 my $sth = $dbh->prepare($query);
1177 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1179 my $ref = $sth->fetchrow_hashref(NAME_lc);
1181 map { $form->{$_} = $ref->{$_} } keys %$ref;
1187 $main::lxdebug->leave_sub();
1191 $main::lxdebug->enter_sub();
1193 my ($self, $myconfig, $form) = @_;
1195 # connect to database
1196 my $dbh = $form->dbconnect($myconfig);
1198 my @values = ($form->{printer_description},
1199 $form->{template_code},
1200 $form->{printer_command});
1202 # id is the old record
1204 $query = qq|UPDATE printers SET
1205 printer_description = ?, template_code = ?, printer_command = ?
1207 push(@values, $form->{id});
1209 $query = qq|INSERT INTO printers
1210 (printer_description, template_code, printer_command)
1213 do_query($form, $dbh, $query, @values);
1217 $main::lxdebug->leave_sub();
1220 sub delete_printer {
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig, $form) = @_;
1225 # connect to database
1226 my $dbh = $form->dbconnect($myconfig);
1228 $query = qq|DELETE FROM printers
1230 do_query($form, $dbh, $query, $form->{id});
1234 $main::lxdebug->leave_sub();
1238 $main::lxdebug->enter_sub();
1240 my ($self, $myconfig, $form) = @_;
1242 # connect to database
1243 my $dbh = $form->dbconnect($myconfig);
1245 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1247 $sth = $dbh->prepare($query);
1248 $sth->execute || $form->dberror($query);
1251 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1252 push @{ $form->{ALL} }, $ref;
1258 $main::lxdebug->leave_sub();
1262 $main::lxdebug->enter_sub();
1264 my ($self, $myconfig, $form) = @_;
1266 # connect to database
1267 my $dbh = $form->dbconnect($myconfig);
1269 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1270 my $sth = $dbh->prepare($query);
1271 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1273 my $ref = $sth->fetchrow_hashref(NAME_lc);
1274 map { $form->{$_} = $ref->{$_} } keys %$ref;
1278 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1279 qq|FROM translation_payment_terms t | .
1280 qq|LEFT JOIN language l ON t.language_id = l.id | .
1281 qq|WHERE t.payment_terms_id = ? | .
1283 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1284 qq| l.description AS language | .
1285 qq|FROM language l|;
1286 $sth = $dbh->prepare($query);
1287 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1290 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1291 $mapping{ $ref->{"language_id"} } = $ref
1292 unless (defined($mapping{ $ref->{"language_id"} }));
1296 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1301 $main::lxdebug->leave_sub();
1305 $main::lxdebug->enter_sub();
1307 my ($self, $myconfig, $form) = @_;
1309 # connect to database
1310 my $dbh = $form->dbconnect_noauto($myconfig);
1315 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1316 qq|FROM payment_terms|;
1318 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1320 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1321 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1325 qq|DELETE FROM translation_payment_terms | .
1326 qq|WHERE payment_terms_id = ?|;
1327 do_query($form, $dbh, $query, $form->{"id"});
1330 $query = qq|UPDATE payment_terms SET
1331 description = ?, description_long = ?,
1332 terms_netto = ?, terms_skonto = ?,
1335 my @values = ($form->{description}, $form->{description_long},
1336 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1337 $form->{percent_skonto} * 1,
1339 do_query($form, $dbh, $query, @values);
1341 $query = qq|SELECT id FROM language|;
1343 my $sth = $dbh->prepare($query);
1344 $sth->execute() || $form->dberror($query);
1346 while (my ($id) = $sth->fetchrow_array()) {
1347 push(@language_ids, $id);
1352 qq|INSERT INTO translation_payment_terms | .
1353 qq|(language_id, payment_terms_id, description_long) | .
1354 qq|VALUES (?, ?, ?)|;
1355 $sth = $dbh->prepare($query);
1357 foreach my $language_id (@language_ids) {
1358 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1359 $form->{"description_long_${language_id}"});
1366 $main::lxdebug->leave_sub();
1369 sub delete_payment {
1370 $main::lxdebug->enter_sub();
1372 my ($self, $myconfig, $form) = @_;
1374 # connect to database
1375 my $dbh = $form->dbconnect_noauto($myconfig);
1378 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1379 do_query($form, $dbh, $query, $form->{"id"});
1381 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1382 do_query($form, $dbh, $query, $form->{"id"});
1387 $main::lxdebug->leave_sub();
1391 sub prepare_template_filename {
1392 $main::lxdebug->enter_sub();
1394 my ($self, $myconfig, $form) = @_;
1396 my ($filename, $display_filename);
1398 if ($form->{type} eq "stylesheet") {
1399 $filename = "css/$myconfig->{stylesheet}";
1400 $display_filename = $myconfig->{stylesheet};
1403 $filename = $form->{formname};
1405 if ($form->{language}) {
1406 my ($id, $template_code) = split(/--/, $form->{language});
1407 $filename .= "_${template_code}";
1410 if ($form->{printer}) {
1411 my ($id, $template_code) = split(/--/, $form->{printer});
1412 $filename .= "_${template_code}";
1415 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1416 $filename =~ s|.*/||;
1417 $display_filename = $filename;
1418 $filename = "$myconfig->{templates}/$filename";
1421 $main::lxdebug->leave_sub();
1423 return ($filename, $display_filename);
1428 $main::lxdebug->enter_sub();
1430 my ($self, $filename) = @_;
1432 my ($content, $lines) = ("", 0);
1436 if (open(TEMPLATE, $filename)) {
1437 while (<TEMPLATE>) {
1444 $main::lxdebug->leave_sub();
1446 return ($content, $lines);
1450 $main::lxdebug->enter_sub();
1452 my ($self, $filename, $content) = @_;
1458 if (open(TEMPLATE, ">$filename")) {
1459 $content =~ s/\r\n/\n/g;
1460 print(TEMPLATE $content);
1466 $main::lxdebug->leave_sub();
1472 $main::lxdebug->enter_sub();
1477 my $myconfig = \%main::myconfig;
1478 my $form = $main::form;
1480 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1483 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1485 $form->{curr} =~ s/ //g;
1486 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1487 my $currency = join ':', @currencies;
1489 # these defaults are database wide
1492 qq|UPDATE defaults SET
1493 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1494 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1495 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1496 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1497 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1510 businessnumber = ?|;
1511 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1512 $accnos{fxgain_accno}, $accnos{fxloss_accno},
1513 $form->{invnumber}, $form->{cnnumber},
1514 $form->{sonumber}, $form->{ponumber},
1515 $form->{sqnumber}, $form->{rfqnumber},
1516 $form->{customernumber}, $form->{vendornumber},
1517 $form->{articlenumber}, $form->{servicenumber},
1518 $form->{yearend}, $currency,
1519 $form->{businessnumber});
1520 do_query($form, $dbh, $query, @values);
1524 $main::lxdebug->leave_sub();
1528 sub save_preferences {
1529 $main::lxdebug->enter_sub();
1531 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1533 my $dbh = $form->get_standard_dbh($myconfig);
1535 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1538 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1539 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1541 my $rc = $dbh->commit();
1543 # save first currency in myconfig
1544 $currency =~ s/:.*//;
1545 $form->{currency} = $currency;
1547 $form->{businessnumber} = $businessnumber;
1549 $myconfig = new User "$memberfile", "$form->{login}";
1551 foreach my $item (keys %$form) {
1552 $myconfig->{$item} = $form->{$item};
1555 $myconfig->save_member($memberfile, $userspath);
1559 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1560 foreach $directory (@webdavdirs) {
1561 $file = "webdav/" . $directory . "/webdav-user";
1562 if ($myconfig->{$directory}) {
1563 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1564 while (<HTACCESS>) {
1565 ($login, $password) = split(/:/, $_);
1566 if ($login ne $form->{login}) {
1571 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1572 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1573 print(HTACCESS $newfile);
1576 $form->{$directory} = 0;
1577 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1578 while (<HTACCESS>) {
1579 ($login, $password) = split(/:/, $_);
1580 if ($login ne $form->{login}) {
1585 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1586 print(HTACCESS $newfile);
1592 $main::lxdebug->leave_sub();
1597 sub defaultaccounts {
1598 $main::lxdebug->enter_sub();
1600 my ($self, $myconfig, $form) = @_;
1602 # connect to database
1603 my $dbh = $form->dbconnect($myconfig);
1605 # get defaults from defaults table
1606 my $query = qq|SELECT * FROM defaults|;
1607 my $sth = $dbh->prepare($query);
1608 $sth->execute || $form->dberror($query);
1610 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1611 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1612 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1613 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1614 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1615 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1619 $query = qq|SELECT c.id, c.accno, c.description, c.link
1621 WHERE c.link LIKE '%IC%'
1623 $sth = $dbh->prepare($query);
1624 $sth->execute || $self->dberror($query);
1626 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1627 foreach my $key (split(/:/, $ref->{link})) {
1630 if ($key =~ /cogs/) {
1631 $nkey = "IC_expense";
1633 if ($key =~ /sale/) {
1634 $nkey = "IC_income";
1636 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1638 description => $ref->{description}
1645 $query = qq|SELECT c.id, c.accno, c.description
1647 WHERE c.category = 'I'
1648 AND c.charttype = 'A'
1650 $sth = $dbh->prepare($query);
1651 $sth->execute || $self->dberror($query);
1653 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1654 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1656 description => $ref->{description}
1661 $query = qq|SELECT c.id, c.accno, c.description
1663 WHERE c.category = 'E'
1664 AND c.charttype = 'A'
1666 $sth = $dbh->prepare($query);
1667 $sth->execute || $self->dberror($query);
1669 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1670 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1672 description => $ref->{description}
1677 # now get the tax rates and numbers
1678 $query = qq|SELECT c.id, c.accno, c.description,
1679 t.rate * 100 AS rate, t.taxnumber
1681 WHERE c.id = t.chart_id|;
1683 $sth = $dbh->prepare($query);
1684 $sth->execute || $form->dberror($query);
1686 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1687 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1688 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1689 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1690 if $ref->{taxnumber};
1691 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1697 $main::lxdebug->leave_sub();
1701 $main::lxdebug->enter_sub();
1703 my ($self, $myconfig, $form) = @_;
1705 my $dbh = $form->dbconnect($myconfig);
1707 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1708 my $sth = $dbh->prepare($query);
1709 $sth->execute || $form->dberror($query);
1711 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1717 $main::lxdebug->leave_sub();
1721 $main::lxdebug->enter_sub();
1723 my ($self, $myconfig, $form) = @_;
1725 my $dbh = $form->dbconnect($myconfig);
1727 my ($query, @values);
1729 if ($form->{revtrans}) {
1730 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1732 } elsif ($form->{closedto}) {
1733 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1734 @values = (conv_date($form->{closedto}));
1737 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1740 # set close in defaults
1741 do_query($form, $dbh, $query, @values);
1745 $main::lxdebug->leave_sub();
1749 my ($self, $units, $unit_name, $factor) = @_;
1751 $factor = 1 unless ($factor);
1753 my $unit = $units->{$unit_name};
1755 if (!defined($unit) || !$unit->{"base_unit"} ||
1756 ($unit_name eq $unit->{"base_unit"})) {
1757 return ($unit_name, $factor);
1760 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1763 sub retrieve_units {
1764 $main::lxdebug->enter_sub();
1766 my ($self, $myconfig, $form, $type, $prefix) = @_;
1768 my $dbh = $form->dbconnect($myconfig);
1770 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1773 $query .= " WHERE (type = ?)";
1777 my $sth = $dbh->prepare($query);
1778 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1781 while (my $ref = $sth->fetchrow_hashref()) {
1782 $units->{$ref->{"name"}} = $ref;
1786 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1787 $sth = $dbh->prepare($query_lang);
1788 $sth->execute() || $form->dberror($query_lang);
1790 while ($ref = $sth->fetchrow_hashref()) {
1791 push(@languages, $ref);
1795 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1796 "FROM units_language ul " .
1797 "LEFT JOIN language l ON ul.language_id = l.id " .
1798 "WHERE ul.unit = ?";
1799 $sth = $dbh->prepare($query_lang);
1801 foreach my $unit (values(%{$units})) {
1802 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1804 $unit->{"LANGUAGES"} = {};
1805 foreach my $lang (@languages) {
1806 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1809 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1810 while ($ref = $sth->fetchrow_hashref()) {
1811 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1818 $main::lxdebug->leave_sub();
1823 sub translate_units {
1824 $main::lxdebug->enter_sub();
1826 my ($self, $form, $template_code, $unit, $amount) = @_;
1828 my $units = $self->retrieve_units(\%main::myconfig, $form);
1830 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1831 my $new_unit = $unit;
1833 if (($amount != 1) && $h->{"localized_plural"}) {
1834 $new_unit = $h->{"localized_plural"};
1835 } elsif ($h->{"localized"}) {
1836 $new_unit = $h->{"localized"};
1840 $main::lxdebug->leave_sub();
1846 $main::lxdebug->enter_sub();
1848 my ($self, $myconfig, $form, $units) = @_;
1850 my $dbh = $form->dbconnect($myconfig);
1852 map({ $_->{"in_use"} = 0; } values(%{$units}));
1854 foreach my $unit (values(%{$units})) {
1855 my $base_unit = $unit->{"original_base_unit"};
1856 while ($base_unit) {
1857 $units->{$base_unit}->{"in_use"} = 1;
1858 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1859 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1860 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1864 foreach my $unit (values(%{$units})) {
1865 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1867 foreach my $table (qw(parts invoice orderitems)) {
1868 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1870 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1871 $query .= "= " . $dbh->quote($unit->{"name"});
1873 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1874 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1877 my ($count) = $dbh->selectrow_array($query);
1878 $form->dberror($query) if ($dbh->err);
1881 $unit->{"in_use"} = 1;
1889 $main::lxdebug->leave_sub();
1892 # if $a is translatable to $b, return the factor between them.
1895 $main::lxdebug->enter_sub(2);
1896 ($this, $a, $b, $all_units) = @_;
1898 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1899 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1900 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1903 sub unit_select_data {
1904 $main::lxdebug->enter_sub();
1906 my ($self, $units, $selected, $empty_entry) = @_;
1911 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1914 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1915 push(@{$select}, { "name" => $unit,
1916 "base_unit" => $units->{$unit}->{"base_unit"},
1917 "factor" => $units->{$unit}->{"factor"},
1918 "selected" => ($unit eq $selected) ? "selected" : "" });
1921 $main::lxdebug->leave_sub();
1926 sub unit_select_html {
1927 $main::lxdebug->enter_sub();
1929 my ($self, $units, $name, $selected, $convertible_into) = @_;
1931 my $select = "<select name=${name}>";
1933 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1934 if (!$convertible_into ||
1935 ($units->{$convertible_into} &&
1936 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1937 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1940 $select .= "</select>";
1942 $main::lxdebug->leave_sub();
1948 $main::lxdebug->enter_sub();
1950 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1952 my $dbh = $form->dbconnect_noauto($myconfig);
1954 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1955 my ($sortkey) = selectrow_query($form, $dbh, $query);
1957 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1958 "VALUES (?, ?, ?, ?, ?)";
1959 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1962 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1963 my $sth = $dbh->prepare($query);
1964 foreach my $lang (@{$languages}) {
1965 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1966 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1974 $main::lxdebug->leave_sub();
1978 $main::lxdebug->enter_sub();
1980 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1982 my $dbh = $form->dbconnect_noauto($myconfig);
1984 my ($base_unit, $unit, $sth, $query);
1986 $query = "DELETE FROM units_language";
1987 $dbh->do($query) || $form->dberror($query);
1989 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1990 $query = "DELETE FROM units WHERE name IN (";
1991 map({ $query .= "?," } @{$delete_units});
1992 substr($query, -1, 1) = ")";
1993 $dbh->do($query, undef, @{$delete_units}) ||
1994 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1997 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1998 $sth = $dbh->prepare($query);
2000 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2001 my $sth_lang = $dbh->prepare($query_lang);
2003 foreach $unit (values(%{$units})) {
2004 $unit->{"depth"} = 0;
2005 my $base_unit = $unit;
2006 while ($base_unit->{"base_unit"}) {
2008 $base_unit = $units->{$base_unit->{"base_unit"}};
2012 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2013 if ($unit->{"LANGUAGES"}) {
2014 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2015 next unless ($lang->{"id"} && $lang->{"localized"});
2016 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2017 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2021 next if ($unit->{"unchanged_unit"});
2023 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2024 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2028 $sth_lang->finish();
2032 $main::lxdebug->leave_sub();
2036 $main::lxdebug->enter_sub();
2038 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2040 my $dbh = $form->dbconnect_noauto($myconfig);
2044 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2045 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2048 qq|SELECT sortkey FROM units | .
2049 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2050 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2051 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2053 if (defined($sortkey_1)) {
2054 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2055 my ($name_2) = selectrow_query($form, $dbh, $query);
2057 if (defined($name_2)) {
2058 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2059 my $sth = $dbh->prepare($query);
2061 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2062 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2069 $main::lxdebug->leave_sub();
2073 $main::lxdebug->enter_sub();
2075 my ($self, $myconfig, $form) = @_;
2077 # connect to database
2078 my $dbh = $form->dbconnect($myconfig);
2080 my $query = qq|SELECT
2084 round(t.rate * 100, 2) AS rate,
2085 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2086 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2090 $sth = $dbh->prepare($query);
2091 $sth->execute || $form->dberror($query);
2094 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2095 push @{ $form->{TAX} }, $ref;
2101 $main::lxdebug->leave_sub();
2104 sub get_tax_accounts {
2105 $main::lxdebug->enter_sub();
2107 my ($self, $myconfig, $form) = @_;
2109 my $dbh = $form->dbconnect($myconfig);
2111 # get Accounts from chart
2112 my $query = qq{ SELECT
2114 accno || ' - ' || description AS taxaccount
2116 WHERE link LIKE '%_tax%'
2120 $sth = $dbh->prepare($query);
2121 $sth->execute || $form->dberror($query);
2123 $form->{ACCOUNTS} = [];
2124 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2125 push @{ $form->{ACCOUNTS} }, $ref;
2132 $main::lxdebug->leave_sub();
2136 $main::lxdebug->enter_sub();
2138 my ($self, $myconfig, $form) = @_;
2140 # connect to database
2141 my $dbh = $form->dbconnect($myconfig);
2143 my $query = qq|SELECT
2146 round(rate * 100, 2) AS rate,
2151 my $sth = $dbh->prepare($query);
2152 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2154 my $ref = $sth->fetchrow_hashref(NAME_lc);
2156 map { $form->{$_} = $ref->{$_} } keys %$ref;
2160 # see if it is used by a taxkey
2161 $query = qq|SELECT count(*) FROM taxkeys
2162 WHERE tax_id = ? AND chart_id >0|;
2164 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2166 $form->{orphaned} = !$form->{orphaned};
2169 if (!$form->{orphaned} ) {
2170 $query = qq|SELECT DISTINCT c.id, c.accno
2172 JOIN tax t ON (t.id = tk.tax_id)
2173 JOIN chart c ON (c.id = tk.chart_id)
2174 WHERE tk.tax_id = ?|;
2176 $sth = $dbh->prepare($query);
2177 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2179 $form->{TAXINUSE} = [];
2180 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2181 push @{ $form->{TAXINUSE} }, $ref;
2189 $main::lxdebug->leave_sub();
2193 $main::lxdebug->enter_sub();
2195 my ($self, $myconfig, $form) = @_;
2197 # connect to database
2198 my $dbh = $form->get_standard_dbh($myconfig);
2200 $form->{rate} = $form->{rate} / 100;
2202 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2203 if ($form->{id} ne "") {
2204 $query = qq|UPDATE tax SET
2209 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2211 push(@values, $form->{id});
2215 $query = qq|INSERT INTO tax (
2222 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2224 do_query($form, $dbh, $query, @values);
2228 $main::lxdebug->leave_sub();
2232 $main::lxdebug->enter_sub();
2234 my ($self, $myconfig, $form) = @_;
2236 # connect to database
2237 my $dbh = $form->get_standard_dbh($myconfig);
2239 $query = qq|DELETE FROM tax
2241 do_query($form, $dbh, $query, $form->{id});
2245 $main::lxdebug->leave_sub();
2248 sub save_price_factor {
2249 $main::lxdebug->enter_sub();
2251 my ($self, $myconfig, $form) = @_;
2253 # connect to database
2254 my $dbh = $form->get_standard_dbh($myconfig);
2257 my @values = ($form->{description}, conv_i($form->{factor}));
2260 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2261 push @values, conv_i($form->{id});
2264 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2267 do_query($form, $dbh, $query, @values);
2271 $main::lxdebug->leave_sub();
2274 sub get_all_price_factors {
2275 $main::lxdebug->enter_sub();
2277 my ($self, $myconfig, $form) = @_;
2279 # connect to database
2280 my $dbh = $form->get_standard_dbh($myconfig);
2282 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2284 $main::lxdebug->leave_sub();
2287 sub get_price_factor {
2288 $main::lxdebug->enter_sub();
2290 my ($self, $myconfig, $form) = @_;
2292 # connect to database
2293 my $dbh = $form->get_standard_dbh($myconfig);
2295 my $query = qq|SELECT description, factor,
2296 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2297 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2298 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2299 FROM price_factors WHERE id = ?|;
2301 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2303 $main::lxdebug->leave_sub();
2306 sub delete_price_factor {
2307 $main::lxdebug->enter_sub();
2309 my ($self, $myconfig, $form) = @_;
2311 # connect to database
2312 my $dbh = $form->get_standard_dbh($myconfig);
2314 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2317 $main::lxdebug->leave_sub();