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);
598 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
599 push @{ $form->{ALL} }, $ref;
605 $main::lxdebug->leave_sub();
609 $main::lxdebug->enter_sub();
611 my ($self, $myconfig, $form) = @_;
613 # connect to database
614 my $dbh = $form->dbconnect($myconfig);
617 qq|SELECT l.id, l.lead | .
620 my $sth = $dbh->prepare($query);
621 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
623 my $ref = $sth->fetchrow_hashref(NAME_lc);
625 map { $form->{$_} = $ref->{$_} } keys %$ref;
631 $main::lxdebug->leave_sub();
635 $main::lxdebug->enter_sub();
637 my ($self, $myconfig, $form) = @_;
639 # connect to database
640 my $dbh = $form->dbconnect($myconfig);
642 my @values = ($form->{description});
643 # id is the old record
645 $query = qq|UPDATE leads SET
648 puhs(@values, $form->{id});
650 $query = qq|INSERT INTO leads
654 do_query($form, $dbh, $query, @values);
658 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
669 $query = qq|DELETE FROM leads
671 do_query($form, $dbh, $query, $form->{id});
675 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ($self, $myconfig, $form) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
686 my $query = qq|SELECT id, description, discount, customernumberinit
690 $sth = $dbh->prepare($query);
691 $sth->execute || $form->dberror($query);
694 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
695 push @{ $form->{ALL} }, $ref;
701 $main::lxdebug->leave_sub();
705 $main::lxdebug->enter_sub();
707 my ($self, $myconfig, $form) = @_;
709 # connect to database
710 my $dbh = $form->dbconnect($myconfig);
713 qq|SELECT b.description, b.discount, b.customernumberinit
716 my $sth = $dbh->prepare($query);
717 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
719 my $ref = $sth->fetchrow_hashref(NAME_lc);
721 map { $form->{$_} = $ref->{$_} } keys %$ref;
727 $main::lxdebug->leave_sub();
731 $main::lxdebug->enter_sub();
733 my ($self, $myconfig, $form) = @_;
735 # connect to database
736 my $dbh = $form->dbconnect($myconfig);
738 my @values = ($form->{description}, $form->{discount},
739 $form->{customernumberinit});
740 # id is the old record
742 $query = qq|UPDATE business SET
745 customernumberinit = ?
747 push(@values, $form->{id});
749 $query = qq|INSERT INTO business
750 (description, discount, customernumberinit)
753 do_query($form, $dbh, $query, @values);
757 $main::lxdebug->leave_sub();
760 sub delete_business {
761 $main::lxdebug->enter_sub();
763 my ($self, $myconfig, $form) = @_;
765 # connect to database
766 my $dbh = $form->dbconnect($myconfig);
768 $query = qq|DELETE FROM business
770 do_query($form, $dbh, $query, $form->{id});
774 $main::lxdebug->leave_sub();
779 $main::lxdebug->enter_sub();
781 my ($self, $myconfig, $form, $return_list) = @_;
783 # connect to database
784 my $dbh = $form->dbconnect($myconfig);
787 "SELECT id, description, template_code, article_code, " .
788 " output_numberformat, output_dateformat, output_longdates " .
789 "FROM language ORDER BY description";
791 $sth = $dbh->prepare($query);
792 $sth->execute || $form->dberror($query);
796 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
797 push(@{ $ary }, $ref);
803 $main::lxdebug->leave_sub();
813 $main::lxdebug->enter_sub();
815 my ($self, $myconfig, $form) = @_;
817 # connect to database
818 my $dbh = $form->dbconnect($myconfig);
821 "SELECT description, template_code, article_code, " .
822 " output_numberformat, output_dateformat, output_longdates " .
823 "FROM language WHERE id = ?";
824 my $sth = $dbh->prepare($query);
825 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
827 my $ref = $sth->fetchrow_hashref(NAME_lc);
829 map { $form->{$_} = $ref->{$_} } keys %$ref;
835 $main::lxdebug->leave_sub();
838 sub get_language_details {
839 $main::lxdebug->enter_sub();
841 my ($self, $myconfig, $form, $id) = @_;
843 # connect to database
844 my $dbh = $form->dbconnect($myconfig);
847 "SELECT template_code, " .
848 " output_numberformat, output_dateformat, output_longdates " .
849 "FROM language WHERE id = ?";
850 my @res = selectrow_query($form, $dbh, $query, $id);
853 $main::lxdebug->leave_sub();
859 $main::lxdebug->enter_sub();
861 my ($self, $myconfig, $form) = @_;
863 # connect to database
864 my $dbh = $form->dbconnect($myconfig);
865 my (@values, $query);
867 map({ push(@values, $form->{$_}); }
868 qw(description template_code article_code
869 output_numberformat output_dateformat output_longdates));
871 # id is the old record
874 "UPDATE language SET " .
875 " description = ?, template_code = ?, article_code = ?, " .
876 " output_numberformat = ?, output_dateformat = ?, " .
877 " output_longdates = ? " .
879 push(@values, $form->{id});
882 "INSERT INTO language (" .
883 " description, template_code, article_code, " .
884 " output_numberformat, output_dateformat, output_longdates" .
885 ") VALUES (?, ?, ?, ?, ?, ?)";
887 do_query($form, $dbh, $query, @values);
891 $main::lxdebug->leave_sub();
894 sub delete_language {
895 $main::lxdebug->enter_sub();
897 my ($self, $myconfig, $form) = @_;
899 # connect to database
900 my $dbh = $form->dbconnect_noauto($myconfig);
902 foreach my $table (qw(translation_payment_terms units_language)) {
903 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
904 do_query($form, $dbh, $query, $form->{"id"});
907 $query = "DELETE FROM language WHERE id = ?";
908 do_query($form, $dbh, $query, $form->{"id"});
913 $main::lxdebug->leave_sub();
918 $main::lxdebug->enter_sub();
920 my ($self, $myconfig, $form) = @_;
922 # connect to database
923 my $dbh = $form->dbconnect($myconfig);
925 my $query = qq|SELECT id, description,
927 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
929 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
931 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
933 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
935 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
937 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
939 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
941 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
943 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
947 $sth = $dbh->prepare($query);
948 $sth->execute || $form->dberror($query);
951 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
952 push @{ $form->{ALL} }, $ref;
958 $main::lxdebug->leave_sub();
961 sub get_buchungsgruppe {
962 $main::lxdebug->enter_sub();
964 my ($self, $myconfig, $form) = @_;
966 # connect to database
967 my $dbh = $form->dbconnect($myconfig);
971 qq|SELECT description, inventory_accno_id,
972 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
974 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
976 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
978 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
980 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
982 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
984 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
986 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
988 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
991 my $sth = $dbh->prepare($query);
992 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
994 my $ref = $sth->fetchrow_hashref(NAME_lc);
996 map { $form->{$_} = $ref->{$_} } keys %$ref;
1001 qq|SELECT count(id) = 0 AS orphaned
1003 WHERE buchungsgruppen_id = ?|;
1004 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1007 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1009 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1010 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1013 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1014 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1015 FROM chart c, defaults d
1016 WHERE c.link LIKE '%$module%'
1020 my $sth = $dbh->prepare($query);
1021 $sth->execute || $form->dberror($query);
1022 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1023 foreach my $key (split(/:/, $ref->{link})) {
1024 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1025 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1027 if ($key =~ /$module/) {
1028 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1029 || ($ref->{id} eq $ref->{income_accno_id})
1030 || ($ref->{id} eq $ref->{expense_accno_id})) {
1031 push @{ $form->{"${module}_links"}{$key} },
1032 { accno => $ref->{accno},
1033 description => $ref->{description},
1034 selected => "selected",
1037 push @{ $form->{"${module}_links"}{$key} },
1038 { accno => $ref->{accno},
1039 description => $ref->{description},
1051 $main::lxdebug->leave_sub();
1054 sub save_buchungsgruppe {
1055 $main::lxdebug->enter_sub();
1057 my ($self, $myconfig, $form) = @_;
1059 # connect to database
1060 my $dbh = $form->dbconnect($myconfig);
1062 my @values = ($form->{description}, $form->{inventory_accno_id},
1063 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1064 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1065 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1066 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1070 # id is the old record
1072 $query = qq|UPDATE buchungsgruppen SET
1073 description = ?, inventory_accno_id = ?,
1074 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1075 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1076 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1077 income_accno_id_3 = ?, expense_accno_id_3 = ?
1079 push(@values, $form->{id});
1081 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1082 my ($sortkey) = $dbh->selectrow_array($query);
1083 $form->dberror($query) if ($dbh->err);
1084 push(@values, $sortkey);
1085 $query = qq|INSERT INTO buchungsgruppen
1086 (description, inventory_accno_id,
1087 income_accno_id_0, expense_accno_id_0,
1088 income_accno_id_1, expense_accno_id_1,
1089 income_accno_id_2, expense_accno_id_2,
1090 income_accno_id_3, expense_accno_id_3,
1092 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1094 do_query($form, $dbh, $query, @values);
1098 $main::lxdebug->leave_sub();
1101 sub delete_buchungsgruppe {
1102 $main::lxdebug->enter_sub();
1104 my ($self, $myconfig, $form) = @_;
1106 # connect to database
1107 my $dbh = $form->dbconnect($myconfig);
1109 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1110 do_query($form, $dbh, $query, $form->{id});
1114 $main::lxdebug->leave_sub();
1118 $main::lxdebug->enter_sub();
1120 my ($self, $myconfig, $form, $table) = @_;
1122 # connect to database
1123 my $dbh = $form->get_standard_dbh($myconfig);
1127 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1128 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1129 my @values = ($form->{"id1"}, $form->{"id2"});
1130 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1132 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1133 my $sth = prepare_query($form, $dbh, $query);
1135 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1136 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1142 $main::lxdebug->leave_sub();
1146 $main::lxdebug->enter_sub();
1148 my ($self, $myconfig, $form) = @_;
1150 # connect to database
1151 my $dbh = $form->dbconnect($myconfig);
1153 my $query = qq|SELECT id, printer_description, template_code, printer_command
1157 $sth = $dbh->prepare($query);
1158 $sth->execute || $form->dberror($query);
1160 $form->{"ALL"} = [];
1161 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1162 push @{ $form->{ALL} }, $ref;
1168 $main::lxdebug->leave_sub();
1172 $main::lxdebug->enter_sub();
1174 my ($self, $myconfig, $form) = @_;
1176 # connect to database
1177 my $dbh = $form->dbconnect($myconfig);
1180 qq|SELECT p.printer_description, p.template_code, p.printer_command
1183 my $sth = $dbh->prepare($query);
1184 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1186 my $ref = $sth->fetchrow_hashref(NAME_lc);
1188 map { $form->{$_} = $ref->{$_} } keys %$ref;
1194 $main::lxdebug->leave_sub();
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig, $form) = @_;
1202 # connect to database
1203 my $dbh = $form->dbconnect($myconfig);
1205 my @values = ($form->{printer_description},
1206 $form->{template_code},
1207 $form->{printer_command});
1209 # id is the old record
1211 $query = qq|UPDATE printers SET
1212 printer_description = ?, template_code = ?, printer_command = ?
1214 push(@values, $form->{id});
1216 $query = qq|INSERT INTO printers
1217 (printer_description, template_code, printer_command)
1220 do_query($form, $dbh, $query, @values);
1224 $main::lxdebug->leave_sub();
1227 sub delete_printer {
1228 $main::lxdebug->enter_sub();
1230 my ($self, $myconfig, $form) = @_;
1232 # connect to database
1233 my $dbh = $form->dbconnect($myconfig);
1235 $query = qq|DELETE FROM printers
1237 do_query($form, $dbh, $query, $form->{id});
1241 $main::lxdebug->leave_sub();
1245 $main::lxdebug->enter_sub();
1247 my ($self, $myconfig, $form) = @_;
1249 # connect to database
1250 my $dbh = $form->dbconnect($myconfig);
1252 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1254 $sth = $dbh->prepare($query);
1255 $sth->execute || $form->dberror($query);
1258 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1259 push @{ $form->{ALL} }, $ref;
1265 $main::lxdebug->leave_sub();
1269 $main::lxdebug->enter_sub();
1271 my ($self, $myconfig, $form) = @_;
1273 # connect to database
1274 my $dbh = $form->dbconnect($myconfig);
1276 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1277 my $sth = $dbh->prepare($query);
1278 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1280 my $ref = $sth->fetchrow_hashref(NAME_lc);
1281 map { $form->{$_} = $ref->{$_} } keys %$ref;
1285 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1286 qq|FROM translation_payment_terms t | .
1287 qq|LEFT JOIN language l ON t.language_id = l.id | .
1288 qq|WHERE t.payment_terms_id = ? | .
1290 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1291 qq| l.description AS language | .
1292 qq|FROM language l|;
1293 $sth = $dbh->prepare($query);
1294 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1297 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1298 $mapping{ $ref->{"language_id"} } = $ref
1299 unless (defined($mapping{ $ref->{"language_id"} }));
1303 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1308 $main::lxdebug->leave_sub();
1312 $main::lxdebug->enter_sub();
1314 my ($self, $myconfig, $form) = @_;
1316 # connect to database
1317 my $dbh = $form->dbconnect_noauto($myconfig);
1322 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1323 qq|FROM payment_terms|;
1325 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1327 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1328 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1332 qq|DELETE FROM translation_payment_terms | .
1333 qq|WHERE payment_terms_id = ?|;
1334 do_query($form, $dbh, $query, $form->{"id"});
1337 $query = qq|UPDATE payment_terms SET
1338 description = ?, description_long = ?,
1339 terms_netto = ?, terms_skonto = ?,
1342 my @values = ($form->{description}, $form->{description_long},
1343 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1344 $form->{percent_skonto} * 1,
1346 do_query($form, $dbh, $query, @values);
1348 $query = qq|SELECT id FROM language|;
1350 my $sth = $dbh->prepare($query);
1351 $sth->execute() || $form->dberror($query);
1353 while (my ($id) = $sth->fetchrow_array()) {
1354 push(@language_ids, $id);
1359 qq|INSERT INTO translation_payment_terms | .
1360 qq|(language_id, payment_terms_id, description_long) | .
1361 qq|VALUES (?, ?, ?)|;
1362 $sth = $dbh->prepare($query);
1364 foreach my $language_id (@language_ids) {
1365 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1366 $form->{"description_long_${language_id}"});
1373 $main::lxdebug->leave_sub();
1376 sub delete_payment {
1377 $main::lxdebug->enter_sub();
1379 my ($self, $myconfig, $form) = @_;
1381 # connect to database
1382 my $dbh = $form->dbconnect_noauto($myconfig);
1385 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1386 do_query($form, $dbh, $query, $form->{"id"});
1388 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1389 do_query($form, $dbh, $query, $form->{"id"});
1394 $main::lxdebug->leave_sub();
1398 sub prepare_template_filename {
1399 $main::lxdebug->enter_sub();
1401 my ($self, $myconfig, $form) = @_;
1403 my ($filename, $display_filename);
1405 if ($form->{type} eq "stylesheet") {
1406 $filename = "css/$myconfig->{stylesheet}";
1407 $display_filename = $myconfig->{stylesheet};
1410 $filename = $form->{formname};
1412 if ($form->{language}) {
1413 my ($id, $template_code) = split(/--/, $form->{language});
1414 $filename .= "_${template_code}";
1417 if ($form->{printer}) {
1418 my ($id, $template_code) = split(/--/, $form->{printer});
1419 $filename .= "_${template_code}";
1422 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1423 $filename =~ s|.*/||;
1424 $display_filename = $filename;
1425 $filename = "$myconfig->{templates}/$filename";
1428 $main::lxdebug->leave_sub();
1430 return ($filename, $display_filename);
1435 $main::lxdebug->enter_sub();
1437 my ($self, $filename) = @_;
1439 my ($content, $lines) = ("", 0);
1443 if (open(TEMPLATE, $filename)) {
1444 while (<TEMPLATE>) {
1451 $main::lxdebug->leave_sub();
1453 return ($content, $lines);
1457 $main::lxdebug->enter_sub();
1459 my ($self, $filename, $content) = @_;
1465 if (open(TEMPLATE, ">$filename")) {
1466 $content =~ s/\r\n/\n/g;
1467 print(TEMPLATE $content);
1473 $main::lxdebug->leave_sub();
1478 sub save_preferences {
1479 $main::lxdebug->enter_sub();
1481 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1483 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1484 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1487 $form->{curr} =~ s/ //g;
1488 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1489 $form->{curr} = join ':', @a;
1491 # connect to database
1492 my $dbh = $form->dbconnect_noauto($myconfig);
1494 # these defaults are database wide
1495 # user specific variables are in myconfig
1498 qq|UPDATE defaults SET | .
1499 qq|inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1500 qq|income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1501 qq|expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1502 qq|fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1503 qq|fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?), | .
1504 qq|invnumber = ?, | .
1505 qq|cnnumber = ?, | .
1506 qq|sonumber = ?, | .
1507 qq|ponumber = ?, | .
1508 qq|sqnumber = ?, | .
1509 qq|rfqnumber = ?, | .
1510 qq|customernumber = ?, | .
1511 qq|vendornumber = ?, | .
1512 qq|articlenumber = ?, | .
1513 qq|servicenumber = ?, | .
1516 qq|businessnumber = ?|;
1517 my @values = ($form->{inventory_accno}, $form->{income_accno},
1518 $form->{expense_accno},
1519 $form->{fxgain_accno}, $form->{fxloss_accno},
1520 $form->{invnumber}, $form->{cnnumber},
1521 $form->{sonumber}, $form->{ponumber},
1522 $form->{sqnumber}, $form->{rfqnumber},
1523 $form->{customernumber}, $form->{vendornumber},
1524 $form->{articlenumber}, $form->{servicenumber},
1525 $form->{yearend}, $form->{curr},
1526 $form->{businessnumber});
1527 do_query($form, $dbh, $query, @values);
1530 $query = qq|UPDATE employee
1533 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1535 my $rc = $dbh->commit;
1538 # save first currency in myconfig
1539 $form->{currency} = substr($form->{curr}, 0, 3);
1541 my $myconfig = new User "$memberfile", "$form->{login}";
1543 foreach my $item (keys %$form) {
1544 $myconfig->{$item} = $form->{$item};
1547 $myconfig->save_member($memberfile, $userspath);
1551 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1552 foreach $directory (@webdavdirs) {
1553 $file = "webdav/" . $directory . "/webdav-user";
1554 if ($myconfig->{$directory}) {
1555 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1556 while (<HTACCESS>) {
1557 ($login, $password) = split(/:/, $_);
1558 if ($login ne $form->{login}) {
1563 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1564 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1565 print(HTACCESS $newfile);
1568 $form->{$directory} = 0;
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 print(HTACCESS $newfile);
1584 $main::lxdebug->leave_sub();
1589 sub defaultaccounts {
1590 $main::lxdebug->enter_sub();
1592 my ($self, $myconfig, $form) = @_;
1594 # connect to database
1595 my $dbh = $form->dbconnect($myconfig);
1597 # get defaults from defaults table
1598 my $query = qq|SELECT * FROM defaults|;
1599 my $sth = $dbh->prepare($query);
1600 $sth->execute || $form->dberror($query);
1602 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1603 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1604 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1605 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1606 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1607 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1611 $query = qq|SELECT c.id, c.accno, c.description, c.link
1613 WHERE c.link LIKE '%IC%'
1615 $sth = $dbh->prepare($query);
1616 $sth->execute || $self->dberror($query);
1618 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1619 foreach my $key (split(/:/, $ref->{link})) {
1622 if ($key =~ /cogs/) {
1623 $nkey = "IC_expense";
1625 if ($key =~ /sale/) {
1626 $nkey = "IC_income";
1628 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1630 description => $ref->{description}
1637 $query = qq|SELECT c.id, c.accno, c.description
1639 WHERE c.category = 'I'
1640 AND c.charttype = 'A'
1642 $sth = $dbh->prepare($query);
1643 $sth->execute || $self->dberror($query);
1645 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1646 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1648 description => $ref->{description}
1653 $query = qq|SELECT c.id, c.accno, c.description
1655 WHERE c.category = 'E'
1656 AND c.charttype = 'A'
1658 $sth = $dbh->prepare($query);
1659 $sth->execute || $self->dberror($query);
1661 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1662 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1664 description => $ref->{description}
1669 # now get the tax rates and numbers
1670 $query = qq|SELECT c.id, c.accno, c.description,
1671 t.rate * 100 AS rate, t.taxnumber
1673 WHERE c.id = t.chart_id|;
1675 $sth = $dbh->prepare($query);
1676 $sth->execute || $form->dberror($query);
1678 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1679 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1680 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1681 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1682 if $ref->{taxnumber};
1683 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1689 $main::lxdebug->leave_sub();
1693 $main::lxdebug->enter_sub();
1695 my ($self, $myconfig, $form) = @_;
1697 my $dbh = $form->dbconnect($myconfig);
1699 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1700 my $sth = $dbh->prepare($query);
1701 $sth->execute || $form->dberror($query);
1703 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1709 $main::lxdebug->leave_sub();
1713 $main::lxdebug->enter_sub();
1715 my ($self, $myconfig, $form) = @_;
1717 my $dbh = $form->dbconnect($myconfig);
1719 my ($query, @values);
1721 if ($form->{revtrans}) {
1722 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1724 } elsif ($form->{closedto}) {
1725 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1726 @values = (conv_date($form->{closedto}));
1729 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1732 # set close in defaults
1733 do_query($form, $dbh, $query, @values);
1737 $main::lxdebug->leave_sub();
1741 my ($self, $units, $unit_name, $factor) = @_;
1743 $factor = 1 unless ($factor);
1745 my $unit = $units->{$unit_name};
1747 if (!defined($unit) || !$unit->{"base_unit"} ||
1748 ($unit_name eq $unit->{"base_unit"})) {
1749 return ($unit_name, $factor);
1752 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1755 sub retrieve_units {
1756 $main::lxdebug->enter_sub();
1758 my ($self, $myconfig, $form, $type, $prefix) = @_;
1760 my $dbh = $form->dbconnect($myconfig);
1762 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1765 $query .= " WHERE (type = ?)";
1769 my $sth = $dbh->prepare($query);
1770 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1773 while (my $ref = $sth->fetchrow_hashref()) {
1774 $units->{$ref->{"name"}} = $ref;
1778 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1779 $sth = $dbh->prepare($query_lang);
1780 $sth->execute() || $form->dberror($query_lang);
1782 while ($ref = $sth->fetchrow_hashref()) {
1783 push(@languages, $ref);
1787 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1788 "FROM units_language ul " .
1789 "LEFT JOIN language l ON ul.language_id = l.id " .
1790 "WHERE ul.unit = ?";
1791 $sth = $dbh->prepare($query_lang);
1793 foreach my $unit (values(%{$units})) {
1794 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1796 $unit->{"LANGUAGES"} = {};
1797 foreach my $lang (@languages) {
1798 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1801 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1802 while ($ref = $sth->fetchrow_hashref()) {
1803 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1810 $main::lxdebug->leave_sub();
1815 sub translate_units {
1816 $main::lxdebug->enter_sub();
1818 my ($self, $form, $template_code, $unit, $amount) = @_;
1820 my $units = $self->retrieve_units(\%main::myconfig, $form);
1822 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1823 my $new_unit = $unit;
1825 if (($amount != 1) && $h->{"localized_plural"}) {
1826 $new_unit = $h->{"localized_plural"};
1827 } elsif ($h->{"localized"}) {
1828 $new_unit = $h->{"localized"};
1832 $main::lxdebug->leave_sub();
1838 $main::lxdebug->enter_sub();
1840 my ($self, $myconfig, $form, $units) = @_;
1842 my $dbh = $form->dbconnect($myconfig);
1844 map({ $_->{"in_use"} = 0; } values(%{$units}));
1846 foreach my $unit (values(%{$units})) {
1847 my $base_unit = $unit->{"original_base_unit"};
1848 while ($base_unit) {
1849 $units->{$base_unit}->{"in_use"} = 1;
1850 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1851 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1852 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1856 foreach my $unit (values(%{$units})) {
1857 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1859 foreach my $table (qw(parts invoice orderitems)) {
1860 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1862 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1863 $query .= "= " . $dbh->quote($unit->{"name"});
1865 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1866 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1869 my ($count) = $dbh->selectrow_array($query);
1870 $form->dberror($query) if ($dbh->err);
1873 $unit->{"in_use"} = 1;
1881 $main::lxdebug->leave_sub();
1884 sub unit_select_data {
1885 $main::lxdebug->enter_sub();
1887 my ($self, $units, $selected, $empty_entry) = @_;
1892 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1895 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1896 push(@{$select}, { "name" => $unit,
1897 "base_unit" => $units->{$unit}->{"base_unit"},
1898 "factor" => $units->{$unit}->{"factor"},
1899 "selected" => ($unit eq $selected) ? "selected" : "" });
1902 $main::lxdebug->leave_sub();
1907 sub unit_select_html {
1908 $main::lxdebug->enter_sub();
1910 my ($self, $units, $name, $selected, $convertible_into) = @_;
1912 my $select = "<select name=${name}>";
1914 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1915 if (!$convertible_into ||
1916 ($units->{$convertible_into} &&
1917 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1918 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1921 $select .= "</select>";
1923 $main::lxdebug->leave_sub();
1929 $main::lxdebug->enter_sub();
1931 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
1933 my $dbh = $form->dbconnect_noauto($myconfig);
1935 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1936 my ($sortkey) = selectrow_query($form, $dbh, $query);
1938 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
1939 "VALUES (?, ?, ?, ?, ?)";
1940 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
1943 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1944 my $sth = $dbh->prepare($query);
1945 foreach my $lang (@{$languages}) {
1946 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1947 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1955 $main::lxdebug->leave_sub();
1959 $main::lxdebug->enter_sub();
1961 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
1963 my $dbh = $form->dbconnect_noauto($myconfig);
1965 my ($base_unit, $unit, $sth, $query);
1967 $query = "DELETE FROM units_language";
1968 $dbh->do($query) || $form->dberror($query);
1970 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1971 $query = "DELETE FROM units WHERE name IN (";
1972 map({ $query .= "?," } @{$delete_units});
1973 substr($query, -1, 1) = ")";
1974 $dbh->do($query, undef, @{$delete_units}) ||
1975 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1978 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1979 $sth = $dbh->prepare($query);
1981 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1982 my $sth_lang = $dbh->prepare($query_lang);
1984 foreach $unit (values(%{$units})) {
1985 $unit->{"depth"} = 0;
1986 my $base_unit = $unit;
1987 while ($base_unit->{"base_unit"}) {
1989 $base_unit = $units->{$base_unit->{"base_unit"}};
1993 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1994 if ($unit->{"LANGUAGES"}) {
1995 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1996 next unless ($lang->{"id"} && $lang->{"localized"});
1997 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1998 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2002 next if ($unit->{"unchanged_unit"});
2004 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2005 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2009 $sth_lang->finish();
2013 $main::lxdebug->leave_sub();
2017 $main::lxdebug->enter_sub();
2019 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2021 my $dbh = $form->dbconnect_noauto($myconfig);
2025 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2026 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2029 qq|SELECT sortkey FROM units | .
2030 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2031 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2032 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2034 if (defined($sortkey_1)) {
2035 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2036 my ($name_2) = selectrow_query($form, $dbh, $query);
2038 if (defined($name_2)) {
2039 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2040 my $sth = $dbh->prepare($query);
2042 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2043 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2050 $main::lxdebug->leave_sub();
2054 $main::lxdebug->enter_sub();
2056 my ($self, $myconfig, $form) = @_;
2058 # connect to database
2059 my $dbh = $form->dbconnect($myconfig);
2061 my $query = qq|SELECT
2065 round(t.rate * 100, 2) AS rate,
2066 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2067 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2071 $sth = $dbh->prepare($query);
2072 $sth->execute || $form->dberror($query);
2075 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2076 push @{ $form->{TAX} }, $ref;
2082 $main::lxdebug->leave_sub();
2085 sub get_tax_accounts {
2086 $main::lxdebug->enter_sub();
2088 my ($self, $myconfig, $form) = @_;
2090 my $dbh = $form->dbconnect($myconfig);
2092 # get Accounts from chart
2093 my $query = qq{ SELECT
2095 accno || ' - ' || description AS taxaccount
2097 WHERE link LIKE '%_tax%'
2101 $sth = $dbh->prepare($query);
2102 $sth->execute || $form->dberror($query);
2104 $form->{ACCOUNTS} = [];
2105 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2106 push @{ $form->{ACCOUNTS} }, $ref;
2113 $main::lxdebug->leave_sub();
2117 $main::lxdebug->enter_sub();
2119 my ($self, $myconfig, $form) = @_;
2121 # connect to database
2122 my $dbh = $form->dbconnect($myconfig);
2124 my $query = qq|SELECT
2127 round(rate * 100, 2) AS rate,
2132 my $sth = $dbh->prepare($query);
2133 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2135 my $ref = $sth->fetchrow_hashref(NAME_lc);
2137 map { $form->{$_} = $ref->{$_} } keys %$ref;
2141 # see if it is used by a taxkey
2142 $query = qq|SELECT count(*) FROM taxkeys
2143 WHERE tax_id = ? AND chart_id >0|;
2145 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2147 $form->{orphaned} = !$form->{orphaned};
2150 if (!$form->{orphaned} ) {
2151 $query = qq|SELECT DISTINCT c.id, c.accno
2153 JOIN tax t ON (t.id = tk.tax_id)
2154 JOIN chart c ON (c.id = tk.chart_id)
2155 WHERE tk.tax_id = ?|;
2157 $sth = $dbh->prepare($query);
2158 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2160 $form->{TAXINUSE} = [];
2161 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2162 push @{ $form->{TAXINUSE} }, $ref;
2170 $main::lxdebug->leave_sub();
2174 $main::lxdebug->enter_sub();
2176 my ($self, $myconfig, $form) = @_;
2178 # connect to database
2179 my $dbh = $form->get_standard_dbh($myconfig);
2181 $form->{rate} = $form->{rate} / 100;
2183 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2184 if ($form->{id} ne "") {
2185 $query = qq|UPDATE tax SET
2190 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2192 push(@values, $form->{id});
2196 $query = qq|INSERT INTO tax (
2203 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2205 do_query($form, $dbh, $query, @values);
2209 $main::lxdebug->leave_sub();
2213 $main::lxdebug->enter_sub();
2215 my ($self, $myconfig, $form) = @_;
2217 # connect to database
2218 my $dbh = $form->get_standard_dbh($myconfig);
2220 $query = qq|DELETE FROM tax
2222 do_query($form, $dbh, $query, $form->{id});
2226 $main::lxdebug->leave_sub();
2229 sub save_price_factor {
2230 $main::lxdebug->enter_sub();
2232 my ($self, $myconfig, $form) = @_;
2234 # connect to database
2235 my $dbh = $form->get_standard_dbh($myconfig);
2238 my @values = ($form->{description}, conv_i($form->{factor}));
2241 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2242 push @values, conv_i($form->{id});
2245 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2248 do_query($form, $dbh, $query, @values);
2252 $main::lxdebug->leave_sub();
2255 sub get_all_price_factors {
2256 $main::lxdebug->enter_sub();
2258 my ($self, $myconfig, $form) = @_;
2260 # connect to database
2261 my $dbh = $form->get_standard_dbh($myconfig);
2263 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2265 $main::lxdebug->leave_sub();
2268 sub get_price_factor {
2269 $main::lxdebug->enter_sub();
2271 my ($self, $myconfig, $form) = @_;
2273 # connect to database
2274 my $dbh = $form->get_standard_dbh($myconfig);
2276 my $query = qq|SELECT description, factor,
2277 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2278 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2279 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2280 FROM price_factors WHERE id = ?|;
2282 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2284 $main::lxdebug->leave_sub();
2287 sub delete_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 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2298 $main::lxdebug->leave_sub();