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 #======================================================================
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form) = @_;
50 my $dbh = $form->dbconnect($myconfig);
52 SELECT c.accno, c.description, c.charttype, c.category,
53 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
54 c.pos_bwa, datevautomatik,
55 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
56 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
59 ON (c.id=tk.chart_id AND tk.id =
60 (SELECT id FROM taxkeys
61 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
62 ORDER BY startdate DESC LIMIT 1))
67 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
68 my $sth = $dbh->prepare($query);
69 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
71 my $ref = $sth->fetchrow_hashref(NAME_lc);
73 foreach my $key (keys %$ref) {
74 $form->{"$key"} = $ref->{"$key"};
79 # get default accounts
80 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
82 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
83 $sth = $dbh->prepare($query);
84 $sth->execute || $form->dberror($query);
86 $ref = $sth->fetchrow_hashref(NAME_lc);
88 map { $form->{$_} = $ref->{$_} } keys %ref;
94 # get taxkeys and description
98 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
100 id||'--'||taxkey AS tax,
103 FROM tax ORDER BY taxkey
105 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
106 $sth = $dbh->prepare($query);
107 $sth->execute || $form->dberror($query);
109 $form->{TAXKEY} = [];
111 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
112 push @{ $form->{TAXKEY} }, $ref;
118 $query = qq|SELECT id, accno,description
122 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
123 $sth = $dbh->prepare($query);
124 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
126 $form->{NEWACCOUNT} = [];
127 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
128 push @{ $form->{NEWACCOUNT} }, $ref;
133 # get the taxkeys of account
147 LEFT JOIN tax t ON (t.id = tk.tax_id)
148 LEFT JOIN chart c ON (c.id = t.chart_id)
150 WHERE tk.chart_id = ?
151 ORDER BY startdate DESC
153 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
154 $sth = $dbh->prepare($query);
156 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
158 $form->{ACCOUNT_TAXKEYS} = [];
160 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
161 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
167 # check if we have any transactions
168 $query = qq|SELECT a.trans_id FROM acc_trans a
169 WHERE a.chart_id = ?|;
170 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
171 $sth = $dbh->prepare($query);
172 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
174 ($form->{orphaned}) = $sth->fetchrow_array;
175 $form->{orphaned} = !$form->{orphaned};
178 # check if new account is active
179 $form->{new_chart_valid} = 0;
180 if ($form->{new_chart_id}) {
181 $query = qq|SELECT current_date-valid_from FROM chart
183 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
184 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
186 $form->{new_chart_valid} = 1;
193 $main::lxdebug->leave_sub();
197 $main::lxdebug->enter_sub();
199 my ($self, $myconfig, $form) = @_;
201 # connect to database, turn off AutoCommit
202 my $dbh = $form->dbconnect_noauto($myconfig);
204 # sanity check, can't have AR with AR_...
205 if ($form->{AR} || $form->{AP} || $form->{IC}) {
206 map { delete $form->{$_} }
207 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);
211 foreach my $item ($form->{AR}, $form->{AR_amount},
212 $form->{AR_tax}, $form->{AR_paid},
213 $form->{AP}, $form->{AP_amount},
214 $form->{AP_tax}, $form->{AP_paid},
215 $form->{IC}, $form->{IC_sale},
216 $form->{IC_cogs}, $form->{IC_taxpart},
217 $form->{IC_income}, $form->{IC_expense},
218 $form->{IC_taxservice}, $form->{CT_tax}
220 $form->{link} .= "${item}:" if ($item);
224 # strip blanks from accno
225 map { $form->{$_} =~ s/ //g; } qw(accno);
229 if ($form->{id} eq "NULL") {
236 $query = qq|UPDATE chart SET
252 $form->{description},
256 conv_i($form->{pos_bwa}),
257 conv_i($form->{pos_bilanz}),
258 conv_i($form->{pos_eur}),
259 conv_i($form->{new_chart_id}),
260 conv_date($form->{valid_from}),
261 ($form->{datevautomatik} eq 'T') ? 'true':'false',
266 elsif ($form->{id} && !$form->{new_chart_valid}) {
270 SET new_chart_id = ?,
276 conv_i($form->{new_chart_id}),
277 conv_date($form->{valid_from}),
296 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
301 $form->{description},
303 $form->{category}, $form->{link},
304 conv_i($form->{pos_bwa}),
305 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
306 conv_i($form->{new_chart_id}),
307 conv_date($form->{valid_from}),
308 ($form->{datevautomatik} eq 'T') ? 'true':'false',
313 do_query($form, $dbh, $query, @values);
319 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
323 for $tk_count (0 .. $MAX_TRIES) {
327 # Check if the account already exists, else cancel
328 last READTAXKEYS if ( $form->{'id'} == 0);
330 # check if there is a startdate
331 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
336 # Add valid taxkeys into the array
339 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
340 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
341 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
342 chart_id => conv_i($form->{"id"}),
343 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
344 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
351 for my $j (0 .. $#taxkeys){
352 if ( defined $taxkeys[$j]{'id'} ){
355 if ($taxkeys[$j]{'delete'}){
357 DELETE FROM taxkeys WHERE id = ?
360 @values = ($taxkeys[$j]{'id'});
362 do_query($form, $dbh, $query, @values);
371 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
379 $taxkeys[$j]{'tax_id'},
380 $taxkeys[$j]{'chart_id'},
381 $taxkeys[$j]{'tax_id'},
382 $taxkeys[$j]{'pos_ustva'},
383 $taxkeys[$j]{'startdate'},
386 do_query($form, $dbh, $query, @values);
392 INSERT INTO taxkeys (
399 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
402 $taxkeys[$j]{'tax_id'},
403 $taxkeys[$j]{'chart_id'},
404 $taxkeys[$j]{'tax_id'},
405 $taxkeys[$j]{'pos_ustva'},
406 $taxkeys[$j]{'startdate'},
409 do_query($form, $dbh, $query, @values);
415 my $rc = $dbh->commit;
418 $main::lxdebug->leave_sub();
424 $main::lxdebug->enter_sub();
426 my ($self, $myconfig, $form) = @_;
428 # connect to database, turn off AutoCommit
429 my $dbh = $form->dbconnect_noauto($myconfig);
431 my $query = qq|SELECT count(*) FROM acc_trans a
432 WHERE a.chart_id = ?|;
433 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
437 $main::lxdebug->leave_sub();
441 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
442 foreach my $type (qw(inventory income expense)) {
445 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
446 qq|WHERE ${type}_accno_id = ?|;
447 do_query($form, $dbh, $query, $form->{id});
450 foreach my $table (qw(partstax customertax vendortax tax)) {
451 $query = qq|DELETE FROM $table
453 do_query($form, $dbh, $query, $form->{id});
456 # delete chart of account record
457 $query = qq|DELETE FROM chart
459 do_query($form, $dbh, $query, $form->{id});
461 # delete account taxkeys
462 $query = qq|DELETE FROM taxkeys
464 do_query($form, $dbh, $query, $form->{id});
466 # commit and redirect
467 my $rc = $dbh->commit;
470 $main::lxdebug->leave_sub();
476 $main::lxdebug->enter_sub();
478 my ($self, $myconfig, $form) = @_;
480 # connect to database
481 my $dbh = $form->dbconnect($myconfig);
483 my $query = qq|SELECT d.id, d.description, d.role
487 $sth = $dbh->prepare($query);
488 $sth->execute || $form->dberror($query);
491 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
492 push @{ $form->{ALL} }, $ref;
498 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
506 # connect to database
507 my $dbh = $form->dbconnect($myconfig);
509 my $query = qq|SELECT d.description, d.role
512 my $sth = $dbh->prepare($query);
513 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
515 my $ref = $sth->fetchrow_hashref(NAME_lc);
517 map { $form->{$_} = $ref->{$_} } keys %$ref;
521 # see if it is in use
522 $query = qq|SELECT count(*) FROM dpt_trans d
523 WHERE d.department_id = ?|;
524 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
526 $form->{orphaned} = !$form->{orphaned};
531 $main::lxdebug->leave_sub();
534 sub save_department {
535 $main::lxdebug->enter_sub();
537 my ($self, $myconfig, $form) = @_;
539 # connect to database
540 my $dbh = $form->dbconnect($myconfig);
542 my @values = ($form->{description}, $form->{role});
544 $query = qq|UPDATE department SET
545 description = ?, role = ?
547 push(@values, $form->{id});
549 $query = qq|INSERT INTO department
553 do_query($form, $dbh, $query, @values);
557 $main::lxdebug->leave_sub();
560 sub delete_department {
561 $main::lxdebug->enter_sub();
563 my ($self, $myconfig, $form) = @_;
565 # connect to database
566 my $dbh = $form->dbconnect($myconfig);
568 $query = qq|DELETE FROM department
570 do_query($form, $dbh, $query, $form->{id});
574 $main::lxdebug->leave_sub();
578 $main::lxdebug->enter_sub();
580 my ($self, $myconfig, $form) = @_;
582 # connect to database
583 my $dbh = $form->dbconnect($myconfig);
585 my $query = qq|SELECT id, lead
589 $sth = $dbh->prepare($query);
590 $sth->execute || $form->dberror($query);
592 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
593 push @{ $form->{ALL} }, $ref;
599 $main::lxdebug->leave_sub();
603 $main::lxdebug->enter_sub();
605 my ($self, $myconfig, $form) = @_;
607 # connect to database
608 my $dbh = $form->dbconnect($myconfig);
611 qq|SELECT l.id, l.lead | .
614 my $sth = $dbh->prepare($query);
615 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
617 my $ref = $sth->fetchrow_hashref(NAME_lc);
619 map { $form->{$_} = $ref->{$_} } keys %$ref;
625 $main::lxdebug->leave_sub();
629 $main::lxdebug->enter_sub();
631 my ($self, $myconfig, $form) = @_;
633 # connect to database
634 my $dbh = $form->dbconnect($myconfig);
636 my @values = ($form->{description});
637 # id is the old record
639 $query = qq|UPDATE leads SET
642 puhs(@values, $form->{id});
644 $query = qq|INSERT INTO leads
648 do_query($form, $dbh, $query, @values);
652 $main::lxdebug->leave_sub();
656 $main::lxdebug->enter_sub();
658 my ($self, $myconfig, $form) = @_;
660 # connect to database
661 my $dbh = $form->dbconnect($myconfig);
663 $query = qq|DELETE FROM leads
665 do_query($form, $dbh, $query, $form->{id});
669 $main::lxdebug->leave_sub();
673 $main::lxdebug->enter_sub();
675 my ($self, $myconfig, $form) = @_;
677 # connect to database
678 my $dbh = $form->dbconnect($myconfig);
680 my $query = qq|SELECT id, description, discount, customernumberinit
684 $sth = $dbh->prepare($query);
685 $sth->execute || $form->dberror($query);
687 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
688 push @{ $form->{ALL} }, $ref;
694 $main::lxdebug->leave_sub();
698 $main::lxdebug->enter_sub();
700 my ($self, $myconfig, $form) = @_;
702 # connect to database
703 my $dbh = $form->dbconnect($myconfig);
706 qq|SELECT b.description, b.discount, b.customernumberinit
709 my $sth = $dbh->prepare($query);
710 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
712 my $ref = $sth->fetchrow_hashref(NAME_lc);
714 map { $form->{$_} = $ref->{$_} } keys %$ref;
720 $main::lxdebug->leave_sub();
724 $main::lxdebug->enter_sub();
726 my ($self, $myconfig, $form) = @_;
728 # connect to database
729 my $dbh = $form->dbconnect($myconfig);
731 my @values = ($form->{description}, $form->{discount},
732 $form->{customernumberinit});
733 # id is the old record
735 $query = qq|UPDATE business SET
738 customernumberinit = ?
740 push(@values, $form->{id});
742 $query = qq|INSERT INTO business
743 (description, discount, customernumberinit)
746 do_query($form, $dbh, $query, @values);
750 $main::lxdebug->leave_sub();
753 sub delete_business {
754 $main::lxdebug->enter_sub();
756 my ($self, $myconfig, $form) = @_;
758 # connect to database
759 my $dbh = $form->dbconnect($myconfig);
761 $query = qq|DELETE FROM business
763 do_query($form, $dbh, $query, $form->{id});
767 $main::lxdebug->leave_sub();
772 $main::lxdebug->enter_sub();
774 my ($self, $myconfig, $form, $return_list) = @_;
776 # connect to database
777 my $dbh = $form->dbconnect($myconfig);
780 "SELECT id, description, template_code, article_code, " .
781 " output_numberformat, output_dateformat, output_longdates " .
782 "FROM language ORDER BY description";
784 $sth = $dbh->prepare($query);
785 $sth->execute || $form->dberror($query);
789 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
790 push(@{ $ary }, $ref);
796 $main::lxdebug->leave_sub();
806 $main::lxdebug->enter_sub();
808 my ($self, $myconfig, $form) = @_;
810 # connect to database
811 my $dbh = $form->dbconnect($myconfig);
814 "SELECT description, template_code, article_code, " .
815 " output_numberformat, output_dateformat, output_longdates " .
816 "FROM language WHERE id = ?";
817 my $sth = $dbh->prepare($query);
818 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
820 my $ref = $sth->fetchrow_hashref(NAME_lc);
822 map { $form->{$_} = $ref->{$_} } keys %$ref;
828 $main::lxdebug->leave_sub();
831 sub get_language_details {
832 $main::lxdebug->enter_sub();
834 my ($self, $myconfig, $form, $id) = @_;
836 # connect to database
837 my $dbh = $form->dbconnect($myconfig);
840 "SELECT template_code, " .
841 " output_numberformat, output_dateformat, output_longdates " .
842 "FROM language WHERE id = ?";
843 my @res = selectrow_query($form, $dbh, $query, $id);
846 $main::lxdebug->leave_sub();
852 $main::lxdebug->enter_sub();
854 my ($self, $myconfig, $form) = @_;
856 # connect to database
857 my $dbh = $form->dbconnect($myconfig);
858 my (@values, $query);
860 map({ push(@values, $form->{$_}); }
861 qw(description template_code article_code
862 output_numberformat output_dateformat output_longdates));
864 # id is the old record
867 "UPDATE language SET " .
868 " description = ?, template_code = ?, article_code = ?, " .
869 " output_numberformat = ?, output_dateformat = ?, " .
870 " output_longdates = ? " .
872 push(@values, $form->{id});
875 "INSERT INTO language (" .
876 " description, template_code, article_code, " .
877 " output_numberformat, output_dateformat, output_longdates" .
878 ") VALUES (?, ?, ?, ?, ?, ?)";
880 do_query($form, $dbh, $query, @values);
884 $main::lxdebug->leave_sub();
887 sub delete_language {
888 $main::lxdebug->enter_sub();
890 my ($self, $myconfig, $form) = @_;
892 # connect to database
893 my $dbh = $form->dbconnect_noauto($myconfig);
895 foreach my $table (qw(translation_payment_terms units_language)) {
896 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
897 do_query($form, $dbh, $query, $form->{"id"});
900 $query = "DELETE FROM language WHERE id = ?";
901 do_query($form, $dbh, $query, $form->{"id"});
906 $main::lxdebug->leave_sub();
911 $main::lxdebug->enter_sub();
913 my ($self, $myconfig, $form) = @_;
915 # connect to database
916 my $dbh = $form->dbconnect($myconfig);
918 my $query = qq|SELECT id, description,
920 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
922 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
924 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
926 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
928 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
930 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
932 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
934 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
936 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
940 $sth = $dbh->prepare($query);
941 $sth->execute || $form->dberror($query);
944 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
945 push @{ $form->{ALL} }, $ref;
951 $main::lxdebug->leave_sub();
954 sub get_buchungsgruppe {
955 $main::lxdebug->enter_sub();
957 my ($self, $myconfig, $form) = @_;
960 # connect to database
961 my $dbh = $form->dbconnect($myconfig);
965 qq|SELECT description, inventory_accno_id,
966 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
968 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
970 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
972 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
974 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
976 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
978 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
980 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
982 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
985 my $sth = $dbh->prepare($query);
986 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
988 my $ref = $sth->fetchrow_hashref(NAME_lc);
990 map { $form->{$_} = $ref->{$_} } keys %$ref;
995 qq|SELECT count(id) = 0 AS orphaned
997 WHERE buchungsgruppen_id = ?|;
998 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1001 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1003 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1004 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1007 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1008 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1009 FROM chart c, defaults d
1010 WHERE c.link LIKE '%$module%'
1014 my $sth = $dbh->prepare($query);
1015 $sth->execute || $form->dberror($query);
1016 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1017 foreach my $key (split(/:/, $ref->{link})) {
1018 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1019 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1021 if ($key =~ /$module/) {
1022 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1023 || ($ref->{id} eq $ref->{income_accno_id})
1024 || ($ref->{id} eq $ref->{expense_accno_id})) {
1025 push @{ $form->{"${module}_links"}{$key} },
1026 { accno => $ref->{accno},
1027 description => $ref->{description},
1028 selected => "selected",
1031 push @{ $form->{"${module}_links"}{$key} },
1032 { accno => $ref->{accno},
1033 description => $ref->{description},
1045 $main::lxdebug->leave_sub();
1048 sub save_buchungsgruppe {
1049 $main::lxdebug->enter_sub();
1051 my ($self, $myconfig, $form) = @_;
1053 # connect to database
1054 my $dbh = $form->dbconnect($myconfig);
1056 my @values = ($form->{description}, $form->{inventory_accno_id},
1057 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1058 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1059 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1060 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1064 # id is the old record
1066 $query = qq|UPDATE buchungsgruppen SET
1067 description = ?, inventory_accno_id = ?,
1068 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1069 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1070 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1071 income_accno_id_3 = ?, expense_accno_id_3 = ?
1073 push(@values, $form->{id});
1075 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1076 my ($sortkey) = $dbh->selectrow_array($query);
1077 $form->dberror($query) if ($dbh->err);
1078 push(@values, $sortkey);
1079 $query = qq|INSERT INTO buchungsgruppen
1080 (description, inventory_accno_id,
1081 income_accno_id_0, expense_accno_id_0,
1082 income_accno_id_1, expense_accno_id_1,
1083 income_accno_id_2, expense_accno_id_2,
1084 income_accno_id_3, expense_accno_id_3,
1086 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1088 do_query($form, $dbh, $query, @values);
1092 $main::lxdebug->leave_sub();
1095 sub delete_buchungsgruppe {
1096 $main::lxdebug->enter_sub();
1098 my ($self, $myconfig, $form) = @_;
1100 # connect to database
1101 my $dbh = $form->dbconnect($myconfig);
1103 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1104 do_query($form, $dbh, $query, $form->{id});
1108 $main::lxdebug->leave_sub();
1112 $main::lxdebug->enter_sub();
1114 my ($self, $myconfig, $form, $table) = @_;
1116 # connect to database
1117 my $dbh = $form->get_standard_dbh($myconfig);
1121 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1122 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1123 my @values = ($form->{"id1"}, $form->{"id2"});
1124 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1126 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1127 my $sth = prepare_query($form, $dbh, $query);
1129 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1130 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1136 $main::lxdebug->leave_sub();
1140 $main::lxdebug->enter_sub();
1142 my ($self, $myconfig, $form) = @_;
1144 # connect to database
1145 my $dbh = $form->dbconnect($myconfig);
1147 my $query = qq|SELECT id, printer_description, template_code, printer_command
1151 $sth = $dbh->prepare($query);
1152 $sth->execute || $form->dberror($query);
1154 $form->{"ALL"} = [];
1155 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1156 push @{ $form->{ALL} }, $ref;
1162 $main::lxdebug->leave_sub();
1166 $main::lxdebug->enter_sub();
1168 my ($self, $myconfig, $form) = @_;
1170 # connect to database
1171 my $dbh = $form->dbconnect($myconfig);
1174 qq|SELECT p.printer_description, p.template_code, p.printer_command
1177 my $sth = $dbh->prepare($query);
1178 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1180 my $ref = $sth->fetchrow_hashref(NAME_lc);
1182 map { $form->{$_} = $ref->{$_} } keys %$ref;
1188 $main::lxdebug->leave_sub();
1192 $main::lxdebug->enter_sub();
1194 my ($self, $myconfig, $form) = @_;
1196 # connect to database
1197 my $dbh = $form->dbconnect($myconfig);
1199 my @values = ($form->{printer_description},
1200 $form->{template_code},
1201 $form->{printer_command});
1203 # id is the old record
1205 $query = qq|UPDATE printers SET
1206 printer_description = ?, template_code = ?, printer_command = ?
1208 push(@values, $form->{id});
1210 $query = qq|INSERT INTO printers
1211 (printer_description, template_code, printer_command)
1214 do_query($form, $dbh, $query, @values);
1218 $main::lxdebug->leave_sub();
1221 sub delete_printer {
1222 $main::lxdebug->enter_sub();
1224 my ($self, $myconfig, $form) = @_;
1226 # connect to database
1227 my $dbh = $form->dbconnect($myconfig);
1229 $query = qq|DELETE FROM printers
1231 do_query($form, $dbh, $query, $form->{id});
1235 $main::lxdebug->leave_sub();
1239 $main::lxdebug->enter_sub();
1241 my ($self, $myconfig, $form) = @_;
1243 # connect to database
1244 my $dbh = $form->dbconnect($myconfig);
1246 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1248 $sth = $dbh->prepare($query);
1249 $sth->execute || $form->dberror($query);
1252 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1253 push @{ $form->{ALL} }, $ref;
1259 $main::lxdebug->leave_sub();
1263 $main::lxdebug->enter_sub();
1265 my ($self, $myconfig, $form) = @_;
1267 # connect to database
1268 my $dbh = $form->dbconnect($myconfig);
1270 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1271 my $sth = $dbh->prepare($query);
1272 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1274 my $ref = $sth->fetchrow_hashref(NAME_lc);
1275 map { $form->{$_} = $ref->{$_} } keys %$ref;
1279 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1280 qq|FROM translation_payment_terms t | .
1281 qq|LEFT JOIN language l ON t.language_id = l.id | .
1282 qq|WHERE t.payment_terms_id = ? | .
1284 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1285 qq| l.description AS language | .
1286 qq|FROM language l|;
1287 $sth = $dbh->prepare($query);
1288 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1291 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1292 $mapping{ $ref->{"language_id"} } = $ref
1293 unless (defined($mapping{ $ref->{"language_id"} }));
1297 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1302 $main::lxdebug->leave_sub();
1306 $main::lxdebug->enter_sub();
1308 my ($self, $myconfig, $form) = @_;
1310 # connect to database
1311 my $dbh = $form->dbconnect_noauto($myconfig);
1316 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1317 qq|FROM payment_terms|;
1319 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1321 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1322 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1326 qq|DELETE FROM translation_payment_terms | .
1327 qq|WHERE payment_terms_id = ?|;
1328 do_query($form, $dbh, $query, $form->{"id"});
1331 $query = qq|UPDATE payment_terms SET
1332 description = ?, description_long = ?,
1333 terms_netto = ?, terms_skonto = ?,
1336 my @values = ($form->{description}, $form->{description_long},
1337 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1338 $form->{percent_skonto} * 1,
1340 do_query($form, $dbh, $query, @values);
1342 $query = qq|SELECT id FROM language|;
1344 my $sth = $dbh->prepare($query);
1345 $sth->execute() || $form->dberror($query);
1347 while (my ($id) = $sth->fetchrow_array()) {
1348 push(@language_ids, $id);
1353 qq|INSERT INTO translation_payment_terms | .
1354 qq|(language_id, payment_terms_id, description_long) | .
1355 qq|VALUES (?, ?, ?)|;
1356 $sth = $dbh->prepare($query);
1358 foreach my $language_id (@language_ids) {
1359 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1360 $form->{"description_long_${language_id}"});
1367 $main::lxdebug->leave_sub();
1370 sub delete_payment {
1371 $main::lxdebug->enter_sub();
1373 my ($self, $myconfig, $form) = @_;
1375 # connect to database
1376 my $dbh = $form->dbconnect_noauto($myconfig);
1379 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1380 do_query($form, $dbh, $query, $form->{"id"});
1382 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1383 do_query($form, $dbh, $query, $form->{"id"});
1388 $main::lxdebug->leave_sub();
1392 sub prepare_template_filename {
1393 $main::lxdebug->enter_sub();
1395 my ($self, $myconfig, $form) = @_;
1397 my ($filename, $display_filename);
1399 if ($form->{type} eq "stylesheet") {
1400 $filename = "css/$myconfig->{stylesheet}";
1401 $display_filename = $myconfig->{stylesheet};
1404 $filename = $form->{formname};
1406 if ($form->{language}) {
1407 my ($id, $template_code) = split(/--/, $form->{language});
1408 $filename .= "_${template_code}";
1411 if ($form->{printer}) {
1412 my ($id, $template_code) = split(/--/, $form->{printer});
1413 $filename .= "_${template_code}";
1416 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1417 $filename =~ s|.*/||;
1418 $display_filename = $filename;
1419 $filename = "$myconfig->{templates}/$filename";
1422 $main::lxdebug->leave_sub();
1424 return ($filename, $display_filename);
1429 $main::lxdebug->enter_sub();
1431 my ($self, $filename) = @_;
1433 my ($content, $lines) = ("", 0);
1437 if (open(TEMPLATE, $filename)) {
1438 while (<TEMPLATE>) {
1445 $main::lxdebug->leave_sub();
1447 return ($content, $lines);
1451 $main::lxdebug->enter_sub();
1453 my ($self, $filename, $content) = @_;
1459 if (open(TEMPLATE, ">$filename")) {
1460 $content =~ s/\r\n/\n/g;
1461 print(TEMPLATE $content);
1467 $main::lxdebug->leave_sub();
1473 $main::lxdebug->enter_sub();
1478 my $myconfig = \%main::myconfig;
1479 my $form = $main::form;
1481 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1484 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1486 $form->{curr} =~ s/ //g;
1487 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1488 my $currency = join ':', @currencies;
1490 # these defaults are database wide
1493 qq|UPDATE defaults SET
1494 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1495 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1496 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1497 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1498 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1511 businessnumber = ?|;
1512 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1513 $accnos{fxgain_accno}, $accnos{fxloss_accno},
1514 $form->{invnumber}, $form->{cnnumber},
1515 $form->{sonumber}, $form->{ponumber},
1516 $form->{sqnumber}, $form->{rfqnumber},
1517 $form->{customernumber}, $form->{vendornumber},
1518 $form->{articlenumber}, $form->{servicenumber},
1519 $form->{yearend}, $currency,
1520 $form->{businessnumber});
1521 do_query($form, $dbh, $query, @values);
1525 $main::lxdebug->leave_sub();
1529 sub save_preferences {
1530 $main::lxdebug->enter_sub();
1532 my ($self, $myconfig, $form, $webdav) = @_;
1534 my $dbh = $form->get_standard_dbh($myconfig);
1536 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1539 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1540 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1542 my $rc = $dbh->commit();
1544 # save first currency in myconfig
1545 $currency =~ s/:.*//;
1546 $form->{currency} = $currency;
1548 $form->{businessnumber} = $businessnumber;
1550 my $myconfig = new User($form->{login});
1552 foreach my $item (keys %$form) {
1553 $myconfig->{$item} = $form->{$item};
1556 $myconfig->save_member($memberfile);
1558 my $auth = $main::auth;
1560 if ($auth->can_change_password()
1561 && defined $form->{new_password}
1562 && ($form->{new_password} ne '********')) {
1563 $auth->change_password($form->{login}, $form->{new_password});
1565 $form->{password} = $form->{new_password};
1566 $auth->set_session_value('password', $form->{password});
1567 $auth->create_or_refresh_session();
1572 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1573 foreach $directory (@webdavdirs) {
1574 $file = "webdav/" . $directory . "/webdav-user";
1575 if ($myconfig->{$directory}) {
1576 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1577 while (<HTACCESS>) {
1578 ($login, $password) = split(/:/, $_);
1579 if ($login ne $form->{login}) {
1584 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1585 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1586 print(HTACCESS $newfile);
1589 $form->{$directory} = 0;
1590 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1591 while (<HTACCESS>) {
1592 ($login, $password) = split(/:/, $_);
1593 if ($login ne $form->{login}) {
1598 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1599 print(HTACCESS $newfile);
1605 $main::lxdebug->leave_sub();
1610 sub defaultaccounts {
1611 $main::lxdebug->enter_sub();
1613 my ($self, $myconfig, $form) = @_;
1615 # connect to database
1616 my $dbh = $form->dbconnect($myconfig);
1618 # get defaults from defaults table
1619 my $query = qq|SELECT * FROM defaults|;
1620 my $sth = $dbh->prepare($query);
1621 $sth->execute || $form->dberror($query);
1623 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1624 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1625 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1626 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1627 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1628 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1632 $query = qq|SELECT c.id, c.accno, c.description, c.link
1634 WHERE c.link LIKE '%IC%'
1636 $sth = $dbh->prepare($query);
1637 $sth->execute || $self->dberror($query);
1639 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1640 foreach my $key (split(/:/, $ref->{link})) {
1643 if ($key =~ /cogs/) {
1644 $nkey = "IC_expense";
1646 if ($key =~ /sale/) {
1647 $nkey = "IC_income";
1649 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1651 description => $ref->{description}
1658 $query = qq|SELECT c.id, c.accno, c.description
1660 WHERE c.category = 'I'
1661 AND c.charttype = 'A'
1663 $sth = $dbh->prepare($query);
1664 $sth->execute || $self->dberror($query);
1666 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1667 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1669 description => $ref->{description}
1674 $query = qq|SELECT c.id, c.accno, c.description
1676 WHERE c.category = 'E'
1677 AND c.charttype = 'A'
1679 $sth = $dbh->prepare($query);
1680 $sth->execute || $self->dberror($query);
1682 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1683 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1685 description => $ref->{description}
1690 # now get the tax rates and numbers
1691 $query = qq|SELECT c.id, c.accno, c.description,
1692 t.rate * 100 AS rate, t.taxnumber
1694 WHERE c.id = t.chart_id|;
1696 $sth = $dbh->prepare($query);
1697 $sth->execute || $form->dberror($query);
1699 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1700 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1701 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1702 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1703 if $ref->{taxnumber};
1704 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1710 $main::lxdebug->leave_sub();
1714 $main::lxdebug->enter_sub();
1716 my ($self, $myconfig, $form) = @_;
1718 my $dbh = $form->dbconnect($myconfig);
1720 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1721 my $sth = $dbh->prepare($query);
1722 $sth->execute || $form->dberror($query);
1724 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1730 $main::lxdebug->leave_sub();
1734 $main::lxdebug->enter_sub();
1736 my ($self, $myconfig, $form) = @_;
1738 my $dbh = $form->dbconnect($myconfig);
1740 my ($query, @values);
1742 if ($form->{revtrans}) {
1743 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1745 } elsif ($form->{closedto}) {
1746 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1747 @values = (conv_date($form->{closedto}));
1750 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1753 # set close in defaults
1754 do_query($form, $dbh, $query, @values);
1758 $main::lxdebug->leave_sub();
1762 my ($self, $units, $unit_name, $factor) = @_;
1764 $factor = 1 unless ($factor);
1766 my $unit = $units->{$unit_name};
1768 if (!defined($unit) || !$unit->{"base_unit"} ||
1769 ($unit_name eq $unit->{"base_unit"})) {
1770 return ($unit_name, $factor);
1773 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1776 sub retrieve_units {
1777 $main::lxdebug->enter_sub();
1779 my ($self, $myconfig, $form, $type, $prefix) = @_;
1781 my $dbh = $form->dbconnect($myconfig);
1783 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1786 $query .= " WHERE (type = ?)";
1790 my $sth = $dbh->prepare($query);
1791 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1794 while (my $ref = $sth->fetchrow_hashref()) {
1795 $units->{$ref->{"name"}} = $ref;
1799 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1800 $sth = $dbh->prepare($query_lang);
1801 $sth->execute() || $form->dberror($query_lang);
1803 while ($ref = $sth->fetchrow_hashref()) {
1804 push(@languages, $ref);
1808 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1809 "FROM units_language ul " .
1810 "LEFT JOIN language l ON ul.language_id = l.id " .
1811 "WHERE ul.unit = ?";
1812 $sth = $dbh->prepare($query_lang);
1814 foreach my $unit (values(%{$units})) {
1815 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1817 $unit->{"LANGUAGES"} = {};
1818 foreach my $lang (@languages) {
1819 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1822 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1823 while ($ref = $sth->fetchrow_hashref()) {
1824 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1831 $main::lxdebug->leave_sub();
1836 sub retrieve_all_units {
1837 $main::lxdebug->enter_sub();
1841 if (!$main::all_units) {
1842 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1845 $main::lxdebug->leave_sub();
1847 return $main::all_units;
1851 sub translate_units {
1852 $main::lxdebug->enter_sub();
1854 my ($self, $form, $template_code, $unit, $amount) = @_;
1856 my $units = $self->retrieve_units(\%main::myconfig, $form);
1858 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1859 my $new_unit = $unit;
1861 if (($amount != 1) && $h->{"localized_plural"}) {
1862 $new_unit = $h->{"localized_plural"};
1863 } elsif ($h->{"localized"}) {
1864 $new_unit = $h->{"localized"};
1868 $main::lxdebug->leave_sub();
1874 $main::lxdebug->enter_sub();
1876 my ($self, $myconfig, $form, $units) = @_;
1878 my $dbh = $form->dbconnect($myconfig);
1880 map({ $_->{"in_use"} = 0; } values(%{$units}));
1882 foreach my $unit (values(%{$units})) {
1883 my $base_unit = $unit->{"original_base_unit"};
1884 while ($base_unit) {
1885 $units->{$base_unit}->{"in_use"} = 1;
1886 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1887 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1888 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1892 foreach my $unit (values(%{$units})) {
1893 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1895 foreach my $table (qw(parts invoice orderitems)) {
1896 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1898 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1899 $query .= "= " . $dbh->quote($unit->{"name"});
1901 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1902 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1905 my ($count) = $dbh->selectrow_array($query);
1906 $form->dberror($query) if ($dbh->err);
1909 $unit->{"in_use"} = 1;
1917 $main::lxdebug->leave_sub();
1920 sub convertible_units {
1921 $main::lxdebug->enter_sub();
1925 my $filter_unit = shift;
1926 my $not_smaller = shift;
1928 my $conv_units = [];
1930 $filter_unit = $units->{$filter_unit};
1932 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1933 my $unit = $units->{$name};
1935 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1936 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1937 push @{$conv_units}, $unit;
1941 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1943 $main::lxdebug->leave_sub();
1948 # if $a is translatable to $b, return the factor between them.
1951 $main::lxdebug->enter_sub(2);
1952 ($this, $a, $b, $all_units) = @_;
1954 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1955 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1956 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1959 sub unit_select_data {
1960 $main::lxdebug->enter_sub();
1962 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1967 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1970 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1971 if (!$convertible_into ||
1972 ($units->{$convertible_into} &&
1973 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1974 push @{$select}, { "name" => $unit,
1975 "base_unit" => $units->{$unit}->{"base_unit"},
1976 "factor" => $units->{$unit}->{"factor"},
1977 "selected" => ($unit eq $selected) ? "selected" : "" };
1981 $main::lxdebug->leave_sub();
1986 sub unit_select_html {
1987 $main::lxdebug->enter_sub();
1989 my ($self, $units, $name, $selected, $convertible_into) = @_;
1991 my $select = "<select name=${name}>";
1993 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1994 if (!$convertible_into ||
1995 ($units->{$convertible_into} &&
1996 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1997 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2000 $select .= "</select>";
2002 $main::lxdebug->leave_sub();
2008 $main::lxdebug->enter_sub();
2012 my $units = $self->retrieve_all_units();
2017 while (2 <= scalar(@_)) {
2018 my $qty = shift(@_);
2019 my $unit = $units->{shift(@_)};
2021 croak "No unit defined with name $unit" if (!defined $unit);
2024 $base_unit = $unit->{base_unit};
2025 } elsif ($base_unit ne $unit->{base_unit}) {
2026 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
2029 $sum += $qty * $unit->{factor};
2032 $main::lxdebug->leave_sub();
2034 return wantarray ? ($sum, $baseunit) : $sum;
2038 $main::lxdebug->enter_sub();
2040 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
2042 my $dbh = $form->dbconnect_noauto($myconfig);
2044 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2045 my ($sortkey) = selectrow_query($form, $dbh, $query);
2047 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
2048 "VALUES (?, ?, ?, ?, ?)";
2049 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
2052 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2053 my $sth = $dbh->prepare($query);
2054 foreach my $lang (@{$languages}) {
2055 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2056 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2064 $main::lxdebug->leave_sub();
2068 $main::lxdebug->enter_sub();
2070 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2072 my $dbh = $form->dbconnect_noauto($myconfig);
2074 my ($base_unit, $unit, $sth, $query);
2076 $query = "DELETE FROM units_language";
2077 $dbh->do($query) || $form->dberror($query);
2079 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2080 $query = "DELETE FROM units WHERE name IN (";
2081 map({ $query .= "?," } @{$delete_units});
2082 substr($query, -1, 1) = ")";
2083 $dbh->do($query, undef, @{$delete_units}) ||
2084 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2087 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2088 $sth = $dbh->prepare($query);
2090 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2091 my $sth_lang = $dbh->prepare($query_lang);
2093 foreach $unit (values(%{$units})) {
2094 $unit->{"depth"} = 0;
2095 my $base_unit = $unit;
2096 while ($base_unit->{"base_unit"}) {
2098 $base_unit = $units->{$base_unit->{"base_unit"}};
2102 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2103 if ($unit->{"LANGUAGES"}) {
2104 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2105 next unless ($lang->{"id"} && $lang->{"localized"});
2106 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2107 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2111 next if ($unit->{"unchanged_unit"});
2113 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2114 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2118 $sth_lang->finish();
2122 $main::lxdebug->leave_sub();
2126 $main::lxdebug->enter_sub();
2128 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2130 my $dbh = $form->dbconnect_noauto($myconfig);
2134 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2135 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2138 qq|SELECT sortkey FROM units | .
2139 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2140 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2141 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2143 if (defined($sortkey_1)) {
2144 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2145 my ($name_2) = selectrow_query($form, $dbh, $query);
2147 if (defined($name_2)) {
2148 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2149 my $sth = $dbh->prepare($query);
2151 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2152 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2159 $main::lxdebug->leave_sub();
2163 $main::lxdebug->enter_sub();
2165 my ($self, $myconfig, $form) = @_;
2167 # connect to database
2168 my $dbh = $form->dbconnect($myconfig);
2170 my $query = qq|SELECT
2174 round(t.rate * 100, 2) AS rate,
2175 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2176 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2180 $sth = $dbh->prepare($query);
2181 $sth->execute || $form->dberror($query);
2184 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2185 push @{ $form->{TAX} }, $ref;
2191 $main::lxdebug->leave_sub();
2194 sub get_tax_accounts {
2195 $main::lxdebug->enter_sub();
2197 my ($self, $myconfig, $form) = @_;
2199 my $dbh = $form->dbconnect($myconfig);
2201 # get Accounts from chart
2202 my $query = qq{ SELECT
2204 accno || ' - ' || description AS taxaccount
2206 WHERE link LIKE '%_tax%'
2210 $sth = $dbh->prepare($query);
2211 $sth->execute || $form->dberror($query);
2213 $form->{ACCOUNTS} = [];
2214 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2215 push @{ $form->{ACCOUNTS} }, $ref;
2222 $main::lxdebug->leave_sub();
2226 $main::lxdebug->enter_sub();
2228 my ($self, $myconfig, $form) = @_;
2230 # connect to database
2231 my $dbh = $form->dbconnect($myconfig);
2233 my $query = qq|SELECT
2236 round(rate * 100, 2) AS rate,
2241 my $sth = $dbh->prepare($query);
2242 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2244 my $ref = $sth->fetchrow_hashref(NAME_lc);
2246 map { $form->{$_} = $ref->{$_} } keys %$ref;
2250 # see if it is used by a taxkey
2251 $query = qq|SELECT count(*) FROM taxkeys
2252 WHERE tax_id = ? AND chart_id >0|;
2254 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2256 $form->{orphaned} = !$form->{orphaned};
2259 if (!$form->{orphaned} ) {
2260 $query = qq|SELECT DISTINCT c.id, c.accno
2262 JOIN tax t ON (t.id = tk.tax_id)
2263 JOIN chart c ON (c.id = tk.chart_id)
2264 WHERE tk.tax_id = ?|;
2266 $sth = $dbh->prepare($query);
2267 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2269 $form->{TAXINUSE} = [];
2270 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2271 push @{ $form->{TAXINUSE} }, $ref;
2279 $main::lxdebug->leave_sub();
2283 $main::lxdebug->enter_sub();
2285 my ($self, $myconfig, $form) = @_;
2287 # connect to database
2288 my $dbh = $form->get_standard_dbh($myconfig);
2290 $form->{rate} = $form->{rate} / 100;
2292 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2293 if ($form->{id} ne "") {
2294 $query = qq|UPDATE tax SET
2299 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2301 push(@values, $form->{id});
2305 $query = qq|INSERT INTO tax (
2312 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2314 do_query($form, $dbh, $query, @values);
2318 $main::lxdebug->leave_sub();
2322 $main::lxdebug->enter_sub();
2324 my ($self, $myconfig, $form) = @_;
2326 # connect to database
2327 my $dbh = $form->get_standard_dbh($myconfig);
2329 $query = qq|DELETE FROM tax
2331 do_query($form, $dbh, $query, $form->{id});
2335 $main::lxdebug->leave_sub();
2338 sub save_price_factor {
2339 $main::lxdebug->enter_sub();
2341 my ($self, $myconfig, $form) = @_;
2343 # connect to database
2344 my $dbh = $form->get_standard_dbh($myconfig);
2347 my @values = ($form->{description}, conv_i($form->{factor}));
2350 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2351 push @values, conv_i($form->{id});
2354 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2357 do_query($form, $dbh, $query, @values);
2361 $main::lxdebug->leave_sub();
2364 sub get_all_price_factors {
2365 $main::lxdebug->enter_sub();
2367 my ($self, $myconfig, $form) = @_;
2369 # connect to database
2370 my $dbh = $form->get_standard_dbh($myconfig);
2372 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2374 $main::lxdebug->leave_sub();
2377 sub get_price_factor {
2378 $main::lxdebug->enter_sub();
2380 my ($self, $myconfig, $form) = @_;
2382 # connect to database
2383 my $dbh = $form->get_standard_dbh($myconfig);
2385 my $query = qq|SELECT description, factor,
2386 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2387 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2388 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2389 FROM price_factors WHERE id = ?|;
2391 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2393 $main::lxdebug->leave_sub();
2396 sub delete_price_factor {
2397 $main::lxdebug->enter_sub();
2399 my ($self, $myconfig, $form) = @_;
2401 # connect to database
2402 my $dbh = $form->get_standard_dbh($myconfig);
2404 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2407 $main::lxdebug->leave_sub();
2410 sub save_warehouse {
2411 $main::lxdebug->enter_sub();
2413 my ($self, $myconfig, $form) = @_;
2415 # connect to database
2416 my $dbh = $form->get_standard_dbh($myconfig);
2418 my ($query, @values, $sth);
2421 $query = qq|SELECT nextval('id')|;
2422 ($form->{id}) = selectrow_query($form, $dbh, $query);
2424 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2425 do_query($form, $dbh, $query, $form->{id});
2428 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2429 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2431 if (0 < $form->{number_of_new_bins}) {
2432 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2433 $sth = prepare_query($form, $dbh, $query);
2435 foreach my $i (1..$form->{number_of_new_bins}) {
2436 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2444 $main::lxdebug->leave_sub();
2448 $main::lxdebug->enter_sub();
2450 my ($self, $myconfig, $form) = @_;
2452 # connect to database
2453 my $dbh = $form->get_standard_dbh($myconfig);
2455 my ($query, @values, $commit_necessary, $sth);
2457 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2460 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2461 do_query($form, $dbh, $query, @values);
2463 $commit_necessary = 1;
2466 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2467 $sth = prepare_query($form, $dbh, $query);
2469 foreach my $row (1..$form->{rowcount}) {
2470 next if ($form->{"delete_${row}"});
2472 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2474 $commit_necessary = 1;
2479 $dbh->commit() if ($commit_necessary);
2481 $main::lxdebug->leave_sub();
2484 sub delete_warehouse {
2485 $main::lxdebug->enter_sub();
2487 my ($self, $myconfig, $form) = @_;
2489 # connect to database
2490 my $dbh = $form->get_standard_dbh($myconfig);
2492 my $id = conv_i($form->{id});
2493 my $query = qq|SELECT i.bin_id FROM inventory i WHERE i.bin_id IN (SELECT b.id FROM bin b WHERE b.warehouse_id = ?) LIMIT 1|;
2494 my ($count) = selectrow_query($form, $dbh, $query, $id);
2497 $main::lxdebug->leave_sub();
2501 do_query($form, $dbh, qq|DELETE FROM warehouse_access WHERE warehouse_id = ?|, conv_i($form->{id}));
2502 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2503 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2507 $main::lxdebug->leave_sub();
2512 sub get_all_warehouses {
2513 $main::lxdebug->enter_sub();
2515 my ($self, $myconfig, $form) = @_;
2517 # connect to database
2518 my $dbh = $form->get_standard_dbh($myconfig);
2520 my $query = qq|SELECT w.id, w.description, w.invalid,
2521 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2523 ORDER BY w.sortkey|;
2525 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2527 $main::lxdebug->leave_sub();
2531 $main::lxdebug->enter_sub();
2533 my ($self, $myconfig, $form) = @_;
2535 # connect to database
2536 my $dbh = $form->get_standard_dbh($myconfig);
2538 my $id = conv_i($form->{id});
2539 my $query = qq|SELECT w.description, w.invalid
2543 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2545 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2547 $query = qq|SELECT b.*, EXISTS
2548 (SELECT i.warehouse_id
2550 WHERE i.bin_id = b.id
2554 WHERE b.warehouse_id = ?|;
2556 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2558 $main::lxdebug->leave_sub();