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, $prefix) = @_;
1781 my $dbh = $form->dbconnect($myconfig);
1783 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1785 my $sth = prepare_execute_query($form, $dbh, $query);
1788 while (my $ref = $sth->fetchrow_hashref()) {
1789 $units->{$ref->{"name"}} = $ref;
1793 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1794 $sth = $dbh->prepare($query_lang);
1795 $sth->execute() || $form->dberror($query_lang);
1797 while ($ref = $sth->fetchrow_hashref()) {
1798 push(@languages, $ref);
1802 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1803 "FROM units_language ul " .
1804 "LEFT JOIN language l ON ul.language_id = l.id " .
1805 "WHERE ul.unit = ?";
1806 $sth = $dbh->prepare($query_lang);
1808 foreach my $unit (values(%{$units})) {
1809 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1811 $unit->{"LANGUAGES"} = {};
1812 foreach my $lang (@languages) {
1813 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1816 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1817 while ($ref = $sth->fetchrow_hashref()) {
1818 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1825 $main::lxdebug->leave_sub();
1830 sub retrieve_all_units {
1831 $main::lxdebug->enter_sub();
1835 if (!$main::all_units) {
1836 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1839 $main::lxdebug->leave_sub();
1841 return $main::all_units;
1845 sub translate_units {
1846 $main::lxdebug->enter_sub();
1848 my ($self, $form, $template_code, $unit, $amount) = @_;
1850 my $units = $self->retrieve_units(\%main::myconfig, $form);
1852 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1853 my $new_unit = $unit;
1855 if (($amount != 1) && $h->{"localized_plural"}) {
1856 $new_unit = $h->{"localized_plural"};
1857 } elsif ($h->{"localized"}) {
1858 $new_unit = $h->{"localized"};
1862 $main::lxdebug->leave_sub();
1868 $main::lxdebug->enter_sub();
1870 my ($self, $myconfig, $form, $units) = @_;
1872 my $dbh = $form->dbconnect($myconfig);
1874 map({ $_->{"in_use"} = 0; } values(%{$units}));
1876 foreach my $unit (values(%{$units})) {
1877 my $base_unit = $unit->{"original_base_unit"};
1878 while ($base_unit) {
1879 $units->{$base_unit}->{"in_use"} = 1;
1880 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1881 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1882 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1886 foreach my $unit (values(%{$units})) {
1887 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1889 foreach my $table (qw(parts invoice orderitems)) {
1890 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1892 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1893 $query .= "= " . $dbh->quote($unit->{"name"});
1895 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1896 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1899 my ($count) = $dbh->selectrow_array($query);
1900 $form->dberror($query) if ($dbh->err);
1903 $unit->{"in_use"} = 1;
1911 $main::lxdebug->leave_sub();
1914 sub convertible_units {
1915 $main::lxdebug->enter_sub();
1919 my $filter_unit = shift;
1920 my $not_smaller = shift;
1922 my $conv_units = [];
1924 $filter_unit = $units->{$filter_unit};
1926 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1927 my $unit = $units->{$name};
1929 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1930 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1931 push @{$conv_units}, $unit;
1935 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1937 $main::lxdebug->leave_sub();
1942 # if $a is translatable to $b, return the factor between them.
1945 $main::lxdebug->enter_sub(2);
1946 ($this, $a, $b, $all_units) = @_;
1948 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1949 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1950 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1953 sub unit_select_data {
1954 $main::lxdebug->enter_sub();
1956 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1961 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1964 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1965 if (!$convertible_into ||
1966 ($units->{$convertible_into} &&
1967 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1968 push @{$select}, { "name" => $unit,
1969 "base_unit" => $units->{$unit}->{"base_unit"},
1970 "factor" => $units->{$unit}->{"factor"},
1971 "selected" => ($unit eq $selected) ? "selected" : "" };
1975 $main::lxdebug->leave_sub();
1980 sub unit_select_html {
1981 $main::lxdebug->enter_sub();
1983 my ($self, $units, $name, $selected, $convertible_into) = @_;
1985 my $select = "<select name=${name}>";
1987 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1988 if (!$convertible_into ||
1989 ($units->{$convertible_into} &&
1990 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1991 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1994 $select .= "</select>";
1996 $main::lxdebug->leave_sub();
2002 $main::lxdebug->enter_sub();
2006 my $units = $self->retrieve_all_units();
2011 while (2 <= scalar(@_)) {
2012 my $qty = shift(@_);
2013 my $unit = $units->{shift(@_)};
2015 croak "No unit defined with name $unit" if (!defined $unit);
2018 $base_unit = $unit->{base_unit};
2019 } elsif ($base_unit ne $unit->{base_unit}) {
2020 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
2023 $sum += $qty * $unit->{factor};
2026 $main::lxdebug->leave_sub();
2028 return wantarray ? ($sum, $baseunit) : $sum;
2032 $main::lxdebug->enter_sub();
2034 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
2036 my $dbh = $form->dbconnect_noauto($myconfig);
2038 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2039 my ($sortkey) = selectrow_query($form, $dbh, $query);
2041 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
2042 "VALUES (?, ?, ?, ?)";
2043 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
2046 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2047 my $sth = $dbh->prepare($query);
2048 foreach my $lang (@{$languages}) {
2049 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2050 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2058 $main::lxdebug->leave_sub();
2062 $main::lxdebug->enter_sub();
2064 my ($self, $myconfig, $form, $units, $delete_units) = @_;
2066 my $dbh = $form->dbconnect_noauto($myconfig);
2068 my ($base_unit, $unit, $sth, $query);
2070 $query = "DELETE FROM units_language";
2071 $dbh->do($query) || $form->dberror($query);
2073 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2074 $query = "DELETE FROM units WHERE name IN (";
2075 map({ $query .= "?," } @{$delete_units});
2076 substr($query, -1, 1) = ")";
2077 $dbh->do($query, undef, @{$delete_units}) ||
2078 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2081 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2082 $sth = $dbh->prepare($query);
2084 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2085 my $sth_lang = $dbh->prepare($query_lang);
2087 foreach $unit (values(%{$units})) {
2088 $unit->{"depth"} = 0;
2089 my $base_unit = $unit;
2090 while ($base_unit->{"base_unit"}) {
2092 $base_unit = $units->{$base_unit->{"base_unit"}};
2096 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2097 if ($unit->{"LANGUAGES"}) {
2098 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2099 next unless ($lang->{"id"} && $lang->{"localized"});
2100 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2101 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2105 next if ($unit->{"unchanged_unit"});
2107 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2108 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2112 $sth_lang->finish();
2116 $main::lxdebug->leave_sub();
2120 $main::lxdebug->enter_sub();
2122 my ($self, $myconfig, $form, $dir, $name_1) = @_;
2124 my $dbh = $form->dbconnect_noauto($myconfig);
2128 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2129 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2132 qq|SELECT sortkey FROM units | .
2133 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
2134 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2135 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
2137 if (defined($sortkey_1)) {
2138 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2139 my ($name_2) = selectrow_query($form, $dbh, $query);
2141 if (defined($name_2)) {
2142 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2143 my $sth = $dbh->prepare($query);
2145 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2146 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2153 $main::lxdebug->leave_sub();
2157 $main::lxdebug->enter_sub();
2159 my ($self, $myconfig, $form) = @_;
2161 # connect to database
2162 my $dbh = $form->dbconnect($myconfig);
2164 my $query = qq|SELECT
2168 round(t.rate * 100, 2) AS rate,
2169 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2170 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2174 $sth = $dbh->prepare($query);
2175 $sth->execute || $form->dberror($query);
2178 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2179 push @{ $form->{TAX} }, $ref;
2185 $main::lxdebug->leave_sub();
2188 sub get_tax_accounts {
2189 $main::lxdebug->enter_sub();
2191 my ($self, $myconfig, $form) = @_;
2193 my $dbh = $form->dbconnect($myconfig);
2195 # get Accounts from chart
2196 my $query = qq{ SELECT
2198 accno || ' - ' || description AS taxaccount
2200 WHERE link LIKE '%_tax%'
2204 $sth = $dbh->prepare($query);
2205 $sth->execute || $form->dberror($query);
2207 $form->{ACCOUNTS} = [];
2208 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2209 push @{ $form->{ACCOUNTS} }, $ref;
2216 $main::lxdebug->leave_sub();
2220 $main::lxdebug->enter_sub();
2222 my ($self, $myconfig, $form) = @_;
2224 # connect to database
2225 my $dbh = $form->dbconnect($myconfig);
2227 my $query = qq|SELECT
2230 round(rate * 100, 2) AS rate,
2235 my $sth = $dbh->prepare($query);
2236 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2238 my $ref = $sth->fetchrow_hashref(NAME_lc);
2240 map { $form->{$_} = $ref->{$_} } keys %$ref;
2244 # see if it is used by a taxkey
2245 $query = qq|SELECT count(*) FROM taxkeys
2246 WHERE tax_id = ? AND chart_id >0|;
2248 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2250 $form->{orphaned} = !$form->{orphaned};
2253 if (!$form->{orphaned} ) {
2254 $query = qq|SELECT DISTINCT c.id, c.accno
2256 JOIN tax t ON (t.id = tk.tax_id)
2257 JOIN chart c ON (c.id = tk.chart_id)
2258 WHERE tk.tax_id = ?|;
2260 $sth = $dbh->prepare($query);
2261 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2263 $form->{TAXINUSE} = [];
2264 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2265 push @{ $form->{TAXINUSE} }, $ref;
2273 $main::lxdebug->leave_sub();
2277 $main::lxdebug->enter_sub();
2279 my ($self, $myconfig, $form) = @_;
2281 # connect to database
2282 my $dbh = $form->get_standard_dbh($myconfig);
2284 $form->{rate} = $form->{rate} / 100;
2286 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2287 if ($form->{id} ne "") {
2288 $query = qq|UPDATE tax SET
2293 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2295 push(@values, $form->{id});
2299 $query = qq|INSERT INTO tax (
2306 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2308 do_query($form, $dbh, $query, @values);
2312 $main::lxdebug->leave_sub();
2316 $main::lxdebug->enter_sub();
2318 my ($self, $myconfig, $form) = @_;
2320 # connect to database
2321 my $dbh = $form->get_standard_dbh($myconfig);
2323 $query = qq|DELETE FROM tax
2325 do_query($form, $dbh, $query, $form->{id});
2329 $main::lxdebug->leave_sub();
2332 sub save_price_factor {
2333 $main::lxdebug->enter_sub();
2335 my ($self, $myconfig, $form) = @_;
2337 # connect to database
2338 my $dbh = $form->get_standard_dbh($myconfig);
2341 my @values = ($form->{description}, conv_i($form->{factor}));
2344 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2345 push @values, conv_i($form->{id});
2348 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2351 do_query($form, $dbh, $query, @values);
2355 $main::lxdebug->leave_sub();
2358 sub get_all_price_factors {
2359 $main::lxdebug->enter_sub();
2361 my ($self, $myconfig, $form) = @_;
2363 # connect to database
2364 my $dbh = $form->get_standard_dbh($myconfig);
2366 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2368 $main::lxdebug->leave_sub();
2371 sub get_price_factor {
2372 $main::lxdebug->enter_sub();
2374 my ($self, $myconfig, $form) = @_;
2376 # connect to database
2377 my $dbh = $form->get_standard_dbh($myconfig);
2379 my $query = qq|SELECT description, factor,
2380 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2381 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2382 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2383 FROM price_factors WHERE id = ?|;
2385 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2387 $main::lxdebug->leave_sub();
2390 sub delete_price_factor {
2391 $main::lxdebug->enter_sub();
2393 my ($self, $myconfig, $form) = @_;
2395 # connect to database
2396 my $dbh = $form->get_standard_dbh($myconfig);
2398 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2401 $main::lxdebug->leave_sub();
2404 sub save_warehouse {
2405 $main::lxdebug->enter_sub();
2407 my ($self, $myconfig, $form) = @_;
2409 # connect to database
2410 my $dbh = $form->get_standard_dbh($myconfig);
2412 my ($query, @values, $sth);
2415 $query = qq|SELECT nextval('id')|;
2416 ($form->{id}) = selectrow_query($form, $dbh, $query);
2418 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2419 do_query($form, $dbh, $query, $form->{id});
2422 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2423 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2425 if (0 < $form->{number_of_new_bins}) {
2426 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2427 $sth = prepare_query($form, $dbh, $query);
2429 foreach my $i (1..$form->{number_of_new_bins}) {
2430 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2438 $main::lxdebug->leave_sub();
2442 $main::lxdebug->enter_sub();
2444 my ($self, $myconfig, $form) = @_;
2446 # connect to database
2447 my $dbh = $form->get_standard_dbh($myconfig);
2449 my ($query, @values, $commit_necessary, $sth);
2451 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2454 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2455 do_query($form, $dbh, $query, @values);
2457 $commit_necessary = 1;
2460 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2461 $sth = prepare_query($form, $dbh, $query);
2463 foreach my $row (1..$form->{rowcount}) {
2464 next if ($form->{"delete_${row}"});
2466 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2468 $commit_necessary = 1;
2473 $dbh->commit() if ($commit_necessary);
2475 $main::lxdebug->leave_sub();
2478 sub delete_warehouse {
2479 $main::lxdebug->enter_sub();
2481 my ($self, $myconfig, $form) = @_;
2483 # connect to database
2484 my $dbh = $form->get_standard_dbh($myconfig);
2486 my $id = conv_i($form->{id});
2487 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|;
2488 my ($count) = selectrow_query($form, $dbh, $query, $id);
2491 $main::lxdebug->leave_sub();
2495 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2496 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2500 $main::lxdebug->leave_sub();
2505 sub get_all_warehouses {
2506 $main::lxdebug->enter_sub();
2508 my ($self, $myconfig, $form) = @_;
2510 # connect to database
2511 my $dbh = $form->get_standard_dbh($myconfig);
2513 my $query = qq|SELECT w.id, w.description, w.invalid,
2514 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2516 ORDER BY w.sortkey|;
2518 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2520 $main::lxdebug->leave_sub();
2524 $main::lxdebug->enter_sub();
2526 my ($self, $myconfig, $form) = @_;
2528 # connect to database
2529 my $dbh = $form->get_standard_dbh($myconfig);
2531 my $id = conv_i($form->{id});
2532 my $query = qq|SELECT w.description, w.invalid
2536 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2538 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2540 $query = qq|SELECT b.*, EXISTS
2541 (SELECT i.warehouse_id
2543 WHERE i.bin_id = b.id
2547 WHERE b.warehouse_id = ?|;
2549 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2551 $main::lxdebug->leave_sub();