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 #======================================================================
48 $main::lxdebug->enter_sub();
50 my ($self, $myconfig, $form) = @_;
53 my $dbh = $form->dbconnect($myconfig);
55 SELECT c.accno, c.description, c.charttype, c.category,
56 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
57 c.pos_bwa, datevautomatik,
58 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
59 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
62 ON (c.id=tk.chart_id AND tk.id =
63 (SELECT id FROM taxkeys
64 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
65 ORDER BY startdate DESC LIMIT 1))
70 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
71 my $sth = $dbh->prepare($query);
72 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
74 my $ref = $sth->fetchrow_hashref("NAME_lc");
76 foreach my $key (keys %$ref) {
77 $form->{"$key"} = $ref->{"$key"};
82 # get default accounts
83 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
85 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
86 $sth = $dbh->prepare($query);
87 $sth->execute || $form->dberror($query);
89 $ref = $sth->fetchrow_hashref("NAME_lc");
91 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
97 # get taxkeys and description
101 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
103 id||'--'||taxkey AS tax,
106 FROM tax ORDER BY taxkey
108 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
109 $sth = $dbh->prepare($query);
110 $sth->execute || $form->dberror($query);
112 $form->{TAXKEY} = [];
114 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
115 push @{ $form->{TAXKEY} }, $ref;
121 $query = qq|SELECT id, accno,description
125 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
126 $sth = $dbh->prepare($query);
127 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
129 $form->{NEWACCOUNT} = [];
130 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
131 push @{ $form->{NEWACCOUNT} }, $ref;
136 # get the taxkeys of account
150 LEFT JOIN tax t ON (t.id = tk.tax_id)
151 LEFT JOIN chart c ON (c.id = t.chart_id)
153 WHERE tk.chart_id = ?
154 ORDER BY startdate DESC
156 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
157 $sth = $dbh->prepare($query);
159 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
161 $form->{ACCOUNT_TAXKEYS} = [];
163 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
170 # check if we have any transactions
171 $query = qq|SELECT a.trans_id FROM acc_trans a
172 WHERE a.chart_id = ?|;
173 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
174 $sth = $dbh->prepare($query);
175 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
177 ($form->{orphaned}) = $sth->fetchrow_array;
178 $form->{orphaned} = !$form->{orphaned};
181 # check if new account is active
182 $form->{new_chart_valid} = 0;
183 if ($form->{new_chart_id}) {
184 $query = qq|SELECT current_date-valid_from FROM chart
186 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
187 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
189 $form->{new_chart_valid} = 1;
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 my ($self, $myconfig, $form) = @_;
204 # connect to database, turn off AutoCommit
205 my $dbh = $form->dbconnect_noauto($myconfig);
207 # sanity check, can't have AR with AR_...
208 if ($form->{AR} || $form->{AP} || $form->{IC}) {
209 map { delete $form->{$_} }
210 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);
214 foreach my $item ($form->{AR}, $form->{AR_amount},
215 $form->{AR_tax}, $form->{AR_paid},
216 $form->{AP}, $form->{AP_amount},
217 $form->{AP_tax}, $form->{AP_paid},
218 $form->{IC}, $form->{IC_sale},
219 $form->{IC_cogs}, $form->{IC_taxpart},
220 $form->{IC_income}, $form->{IC_expense},
221 $form->{IC_taxservice}, $form->{CT_tax}
223 $form->{link} .= "${item}:" if ($item);
227 # strip blanks from accno
228 map { $form->{$_} =~ s/ //g; } qw(accno);
232 if ($form->{id} eq "NULL") {
236 if (!$form->{id} || $form->{id} eq "") {
237 $query = qq|SELECT nextval('id')|;
238 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
239 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
240 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
246 $query = qq|UPDATE chart SET
262 $form->{description},
266 conv_i($form->{pos_bwa}),
267 conv_i($form->{pos_bilanz}),
268 conv_i($form->{pos_eur}),
269 conv_i($form->{new_chart_id}),
270 conv_date($form->{valid_from}),
271 ($form->{datevautomatik} eq 'T') ? 'true':'false',
277 do_query($form, $dbh, $query, @values);
283 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
287 for $tk_count (0 .. $MAX_TRIES) {
291 # Check if the account already exists, else cancel
293 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
295 last READTAXKEYS if ( $form->{'id'} == 0);
297 # check if there is a startdate
298 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
303 # Add valid taxkeys into the array
306 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
307 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
308 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
309 chart_id => conv_i($form->{"id"}),
310 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
311 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
318 for my $j (0 .. $#taxkeys){
319 if ( defined $taxkeys[$j]{'id'} ){
322 if ($taxkeys[$j]{'delete'}){
324 DELETE FROM taxkeys WHERE id = ?
327 @values = ($taxkeys[$j]{'id'});
329 do_query($form, $dbh, $query, @values);
338 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
346 $taxkeys[$j]{'tax_id'},
347 $taxkeys[$j]{'chart_id'},
348 $taxkeys[$j]{'tax_id'},
349 $taxkeys[$j]{'pos_ustva'},
350 $taxkeys[$j]{'startdate'},
353 do_query($form, $dbh, $query, @values);
359 INSERT INTO taxkeys (
366 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
369 $taxkeys[$j]{'tax_id'},
370 $taxkeys[$j]{'chart_id'},
371 $taxkeys[$j]{'tax_id'},
372 $taxkeys[$j]{'pos_ustva'},
373 $taxkeys[$j]{'startdate'},
376 do_query($form, $dbh, $query, @values);
382 my $rc = $dbh->commit;
385 $main::lxdebug->leave_sub();
391 $main::lxdebug->enter_sub();
393 my ($self, $myconfig, $form) = @_;
395 # connect to database, turn off AutoCommit
396 my $dbh = $form->dbconnect_noauto($myconfig);
398 my $query = qq|SELECT count(*) FROM acc_trans a
399 WHERE a.chart_id = ?|;
400 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
404 $main::lxdebug->leave_sub();
408 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
409 foreach my $type (qw(inventory income expense)) {
412 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
413 qq|WHERE ${type}_accno_id = ?|;
414 do_query($form, $dbh, $query, $form->{id});
417 foreach my $table (qw(partstax customertax vendortax tax)) {
418 $query = qq|DELETE FROM $table
420 do_query($form, $dbh, $query, $form->{id});
423 # delete chart of account record
424 $query = qq|DELETE FROM chart
426 do_query($form, $dbh, $query, $form->{id});
428 # delete account taxkeys
429 $query = qq|DELETE FROM taxkeys
431 do_query($form, $dbh, $query, $form->{id});
433 # commit and redirect
434 my $rc = $dbh->commit;
437 $main::lxdebug->leave_sub();
443 $main::lxdebug->enter_sub();
445 my ($self, $myconfig, $form) = @_;
447 # connect to database
448 my $dbh = $form->dbconnect($myconfig);
450 my $query = qq|SELECT d.id, d.description, d.role
454 my $sth = $dbh->prepare($query);
455 $sth->execute || $form->dberror($query);
458 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
459 push @{ $form->{ALL} }, $ref;
465 $main::lxdebug->leave_sub();
469 $main::lxdebug->enter_sub();
471 my ($self, $myconfig, $form) = @_;
473 # connect to database
474 my $dbh = $form->dbconnect($myconfig);
476 my $query = qq|SELECT d.description, d.role
479 my $sth = $dbh->prepare($query);
480 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
482 my $ref = $sth->fetchrow_hashref("NAME_lc");
484 map { $form->{$_} = $ref->{$_} } keys %$ref;
488 # see if it is in use
489 $query = qq|SELECT count(*) FROM dpt_trans d
490 WHERE d.department_id = ?|;
491 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
493 $form->{orphaned} = !$form->{orphaned};
498 $main::lxdebug->leave_sub();
501 sub save_department {
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
507 # connect to database
508 my $dbh = $form->dbconnect($myconfig);
510 my @values = ($form->{description}, $form->{role});
512 $query = qq|UPDATE department SET
513 description = ?, role = ?
515 push(@values, $form->{id});
517 $query = qq|INSERT INTO department
521 do_query($form, $dbh, $query, @values);
525 $main::lxdebug->leave_sub();
528 sub delete_department {
529 $main::lxdebug->enter_sub();
531 my ($self, $myconfig, $form) = @_;
534 # connect to database
535 my $dbh = $form->dbconnect($myconfig);
537 $query = qq|DELETE FROM department
539 do_query($form, $dbh, $query, $form->{id});
543 $main::lxdebug->leave_sub();
547 $main::lxdebug->enter_sub();
549 my ($self, $myconfig, $form) = @_;
551 # connect to database
552 my $dbh = $form->dbconnect($myconfig);
554 my $query = qq|SELECT id, lead
558 my $sth = $dbh->prepare($query);
559 $sth->execute || $form->dberror($query);
561 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
562 push @{ $form->{ALL} }, $ref;
568 $main::lxdebug->leave_sub();
572 $main::lxdebug->enter_sub();
574 my ($self, $myconfig, $form) = @_;
576 # connect to database
577 my $dbh = $form->dbconnect($myconfig);
580 qq|SELECT l.id, l.lead | .
583 my $sth = $dbh->prepare($query);
584 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
586 my $ref = $sth->fetchrow_hashref("NAME_lc");
588 map { $form->{$_} = $ref->{$_} } keys %$ref;
594 $main::lxdebug->leave_sub();
598 $main::lxdebug->enter_sub();
600 my ($self, $myconfig, $form) = @_;
603 # connect to database
604 my $dbh = $form->dbconnect($myconfig);
606 my @values = ($form->{description});
607 # id is the old record
609 $query = qq|UPDATE leads SET
612 puhs(@values, $form->{id});
614 $query = qq|INSERT INTO leads
618 do_query($form, $dbh, $query, @values);
622 $main::lxdebug->leave_sub();
626 $main::lxdebug->enter_sub();
628 my ($self, $myconfig, $form) = @_;
631 # connect to database
632 my $dbh = $form->dbconnect($myconfig);
634 $query = qq|DELETE FROM leads
636 do_query($form, $dbh, $query, $form->{id});
640 $main::lxdebug->leave_sub();
644 $main::lxdebug->enter_sub();
646 my ($self, $myconfig, $form) = @_;
648 # connect to database
649 my $dbh = $form->dbconnect($myconfig);
651 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
655 my $sth = $dbh->prepare($query);
656 $sth->execute || $form->dberror($query);
658 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
659 push @{ $form->{ALL} }, $ref;
665 $main::lxdebug->leave_sub();
669 $main::lxdebug->enter_sub();
671 my ($self, $myconfig, $form) = @_;
673 # connect to database
674 my $dbh = $form->dbconnect($myconfig);
677 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
680 my $sth = $dbh->prepare($query);
681 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
683 my $ref = $sth->fetchrow_hashref("NAME_lc");
685 map { $form->{$_} = $ref->{$_} } keys %$ref;
691 $main::lxdebug->leave_sub();
695 $main::lxdebug->enter_sub();
697 my ($self, $myconfig, $form) = @_;
700 # connect to database
701 my $dbh = $form->dbconnect($myconfig);
703 my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
704 # id is the old record
706 $query = qq|UPDATE business SET
709 customernumberinit = ?,
712 push(@values, $form->{id});
714 $query = qq|INSERT INTO business
715 (description, discount, customernumberinit, salesman)
716 VALUES (?, ?, ?, ?)|;
718 do_query($form, $dbh, $query, @values);
722 $main::lxdebug->leave_sub();
725 sub delete_business {
726 $main::lxdebug->enter_sub();
728 my ($self, $myconfig, $form) = @_;
730 # connect to database
731 my $dbh = $form->dbconnect($myconfig);
733 my $query = qq|DELETE FROM business
735 do_query($form, $dbh, $query, $form->{id});
739 $main::lxdebug->leave_sub();
744 $main::lxdebug->enter_sub();
746 my ($self, $myconfig, $form, $return_list) = @_;
748 # connect to database
749 my $dbh = $form->dbconnect($myconfig);
752 "SELECT id, description, template_code, article_code, " .
753 " output_numberformat, output_dateformat, output_longdates " .
754 "FROM language ORDER BY description";
756 my $sth = $dbh->prepare($query);
757 $sth->execute || $form->dberror($query);
761 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
762 push(@{ $ary }, $ref);
768 $main::lxdebug->leave_sub();
778 $main::lxdebug->enter_sub();
780 my ($self, $myconfig, $form) = @_;
782 # connect to database
783 my $dbh = $form->dbconnect($myconfig);
786 "SELECT description, template_code, article_code, " .
787 " output_numberformat, output_dateformat, output_longdates " .
788 "FROM language WHERE id = ?";
789 my $sth = $dbh->prepare($query);
790 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
792 my $ref = $sth->fetchrow_hashref("NAME_lc");
794 map { $form->{$_} = $ref->{$_} } keys %$ref;
800 $main::lxdebug->leave_sub();
803 sub get_language_details {
804 $main::lxdebug->enter_sub();
806 my ($self, $myconfig, $form, $id) = @_;
808 # connect to database
809 my $dbh = $form->dbconnect($myconfig);
812 "SELECT template_code, " .
813 " output_numberformat, output_dateformat, output_longdates " .
814 "FROM language WHERE id = ?";
815 my @res = selectrow_query($form, $dbh, $query, $id);
818 $main::lxdebug->leave_sub();
824 $main::lxdebug->enter_sub();
826 my ($self, $myconfig, $form) = @_;
828 # connect to database
829 my $dbh = $form->dbconnect($myconfig);
830 my (@values, $query);
832 map({ push(@values, $form->{$_}); }
833 qw(description template_code article_code
834 output_numberformat output_dateformat output_longdates));
836 # id is the old record
839 "UPDATE language SET " .
840 " description = ?, template_code = ?, article_code = ?, " .
841 " output_numberformat = ?, output_dateformat = ?, " .
842 " output_longdates = ? " .
844 push(@values, $form->{id});
847 "INSERT INTO language (" .
848 " description, template_code, article_code, " .
849 " output_numberformat, output_dateformat, output_longdates" .
850 ") VALUES (?, ?, ?, ?, ?, ?)";
852 do_query($form, $dbh, $query, @values);
856 $main::lxdebug->leave_sub();
859 sub delete_language {
860 $main::lxdebug->enter_sub();
862 my ($self, $myconfig, $form) = @_;
865 # connect to database
866 my $dbh = $form->dbconnect_noauto($myconfig);
868 foreach my $table (qw(translation_payment_terms units_language)) {
869 $query = qq|DELETE FROM $table WHERE language_id = ?|;
870 do_query($form, $dbh, $query, $form->{"id"});
873 $query = "DELETE FROM language WHERE id = ?";
874 do_query($form, $dbh, $query, $form->{"id"});
879 $main::lxdebug->leave_sub();
884 $main::lxdebug->enter_sub();
886 my ($self, $myconfig, $form) = @_;
888 # connect to database
889 my $dbh = $form->dbconnect($myconfig);
891 my $query = qq|SELECT id, description,
893 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
895 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
897 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
899 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
901 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
903 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
905 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
907 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
909 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
913 my $sth = $dbh->prepare($query);
914 $sth->execute || $form->dberror($query);
917 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
918 push @{ $form->{ALL} }, $ref;
924 $main::lxdebug->leave_sub();
927 sub get_buchungsgruppe {
928 $main::lxdebug->enter_sub();
930 my ($self, $myconfig, $form) = @_;
933 # connect to database
934 my $dbh = $form->dbconnect($myconfig);
938 qq|SELECT description, inventory_accno_id,
939 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
941 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
943 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
945 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
947 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
949 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
951 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
953 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
955 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
958 my $sth = $dbh->prepare($query);
959 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
961 my $ref = $sth->fetchrow_hashref("NAME_lc");
963 map { $form->{$_} = $ref->{$_} } keys %$ref;
968 qq|SELECT count(id) = 0 AS orphaned
970 WHERE buchungsgruppen_id = ?|;
971 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
974 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
976 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
977 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
980 $query = qq|SELECT c.accno, c.description, c.link, c.id,
981 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
982 FROM chart c, defaults d
983 WHERE c.link LIKE '%$module%'
987 my $sth = $dbh->prepare($query);
988 $sth->execute || $form->dberror($query);
989 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
990 foreach my $key (split(/:/, $ref->{link})) {
991 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
992 $form->{"std_inventory_accno_id"} = $ref->{"id"};
994 if ($key =~ /$module/) {
995 if ( ($ref->{id} eq $ref->{inventory_accno_id})
996 || ($ref->{id} eq $ref->{income_accno_id})
997 || ($ref->{id} eq $ref->{expense_accno_id})) {
998 push @{ $form->{"${module}_links"}{$key} },
999 { accno => $ref->{accno},
1000 description => $ref->{description},
1001 selected => "selected",
1004 push @{ $form->{"${module}_links"}{$key} },
1005 { accno => $ref->{accno},
1006 description => $ref->{description},
1018 $main::lxdebug->leave_sub();
1021 sub save_buchungsgruppe {
1022 $main::lxdebug->enter_sub();
1024 my ($self, $myconfig, $form) = @_;
1026 # connect to database
1027 my $dbh = $form->dbconnect($myconfig);
1029 my @values = ($form->{description}, $form->{inventory_accno_id},
1030 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1031 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1032 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1033 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1037 # id is the old record
1039 $query = qq|UPDATE buchungsgruppen SET
1040 description = ?, inventory_accno_id = ?,
1041 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1042 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1043 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1044 income_accno_id_3 = ?, expense_accno_id_3 = ?
1046 push(@values, $form->{id});
1048 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1049 my ($sortkey) = $dbh->selectrow_array($query);
1050 $form->dberror($query) if ($dbh->err);
1051 push(@values, $sortkey);
1052 $query = qq|INSERT INTO buchungsgruppen
1053 (description, inventory_accno_id,
1054 income_accno_id_0, expense_accno_id_0,
1055 income_accno_id_1, expense_accno_id_1,
1056 income_accno_id_2, expense_accno_id_2,
1057 income_accno_id_3, expense_accno_id_3,
1059 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1061 do_query($form, $dbh, $query, @values);
1065 $main::lxdebug->leave_sub();
1068 sub delete_buchungsgruppe {
1069 $main::lxdebug->enter_sub();
1071 my ($self, $myconfig, $form) = @_;
1073 # connect to database
1074 my $dbh = $form->dbconnect($myconfig);
1076 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1077 do_query($form, $dbh, $query, $form->{id});
1081 $main::lxdebug->leave_sub();
1085 $main::lxdebug->enter_sub();
1087 my ($self, $myconfig, $form, $table) = @_;
1089 # connect to database
1090 my $dbh = $form->get_standard_dbh($myconfig);
1094 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1095 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1096 my @values = ($form->{"id1"}, $form->{"id2"});
1097 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1099 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1100 my $sth = prepare_query($form, $dbh, $query);
1102 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1103 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1109 $main::lxdebug->leave_sub();
1113 $main::lxdebug->enter_sub();
1115 my ($self, $myconfig, $form) = @_;
1117 # connect to database
1118 my $dbh = $form->dbconnect($myconfig);
1120 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1122 my $sth = $dbh->prepare($query);
1123 $sth->execute || $form->dberror($query);
1126 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1127 push @{ $form->{ALL} }, $ref;
1133 $main::lxdebug->leave_sub();
1137 $main::lxdebug->enter_sub();
1139 my ($self, $myconfig, $form) = @_;
1141 # connect to database
1142 my $dbh = $form->dbconnect($myconfig);
1144 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1145 my $sth = $dbh->prepare($query);
1146 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1148 my $ref = $sth->fetchrow_hashref("NAME_lc");
1149 map { $form->{$_} = $ref->{$_} } keys %$ref;
1153 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1154 qq|FROM translation_payment_terms t | .
1155 qq|LEFT JOIN language l ON t.language_id = l.id | .
1156 qq|WHERE t.payment_terms_id = ? | .
1158 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1159 qq| l.description AS language | .
1160 qq|FROM language l|;
1161 $sth = $dbh->prepare($query);
1162 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1165 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1166 $mapping{ $ref->{"language_id"} } = $ref
1167 unless (defined($mapping{ $ref->{"language_id"} }));
1171 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1176 $main::lxdebug->leave_sub();
1180 $main::lxdebug->enter_sub();
1182 my ($self, $myconfig, $form) = @_;
1184 # connect to database
1185 my $dbh = $form->dbconnect_noauto($myconfig);
1190 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1191 qq|FROM payment_terms|;
1193 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1195 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1196 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1200 qq|DELETE FROM translation_payment_terms | .
1201 qq|WHERE payment_terms_id = ?|;
1202 do_query($form, $dbh, $query, $form->{"id"});
1205 $query = qq|UPDATE payment_terms SET
1206 description = ?, description_long = ?,
1207 terms_netto = ?, terms_skonto = ?,
1210 my @values = ($form->{description}, $form->{description_long},
1211 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1212 $form->{percent_skonto} * 1,
1214 do_query($form, $dbh, $query, @values);
1216 $query = qq|SELECT id FROM language|;
1218 my $sth = $dbh->prepare($query);
1219 $sth->execute() || $form->dberror($query);
1221 while (my ($id) = $sth->fetchrow_array()) {
1222 push(@language_ids, $id);
1227 qq|INSERT INTO translation_payment_terms | .
1228 qq|(language_id, payment_terms_id, description_long) | .
1229 qq|VALUES (?, ?, ?)|;
1230 $sth = $dbh->prepare($query);
1232 foreach my $language_id (@language_ids) {
1233 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1234 $form->{"description_long_${language_id}"});
1241 $main::lxdebug->leave_sub();
1244 sub delete_payment {
1245 $main::lxdebug->enter_sub();
1247 my ($self, $myconfig, $form) = @_;
1249 # connect to database
1250 my $dbh = $form->dbconnect_noauto($myconfig);
1253 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1254 do_query($form, $dbh, $query, $form->{"id"});
1256 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1257 do_query($form, $dbh, $query, $form->{"id"});
1262 $main::lxdebug->leave_sub();
1266 sub prepare_template_filename {
1267 $main::lxdebug->enter_sub();
1269 my ($self, $myconfig, $form) = @_;
1271 my ($filename, $display_filename);
1273 if ($form->{type} eq "stylesheet") {
1274 $filename = "css/$myconfig->{stylesheet}";
1275 $display_filename = $myconfig->{stylesheet};
1278 $filename = $form->{formname};
1280 if ($form->{language}) {
1281 my ($id, $template_code) = split(/--/, $form->{language});
1282 $filename .= "_${template_code}";
1285 if ($form->{printer}) {
1286 my ($id, $template_code) = split(/--/, $form->{printer});
1287 $filename .= "_${template_code}";
1290 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1291 $filename =~ s|.*/||;
1292 $display_filename = $filename;
1293 $filename = "$myconfig->{templates}/$filename";
1296 $main::lxdebug->leave_sub();
1298 return ($filename, $display_filename);
1303 $main::lxdebug->enter_sub();
1305 my ($self, $filename) = @_;
1307 my ($content, $lines) = ("", 0);
1311 if (open(TEMPLATE, $filename)) {
1312 while (<TEMPLATE>) {
1319 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1321 $main::lxdebug->leave_sub();
1323 return ($content, $lines);
1327 $main::lxdebug->enter_sub();
1329 my ($self, $filename, $content) = @_;
1335 if (open(TEMPLATE, ">$filename")) {
1336 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1337 $content =~ s/\r\n/\n/g;
1338 print(TEMPLATE $content);
1344 $main::lxdebug->leave_sub();
1350 $main::lxdebug->enter_sub();
1355 my $myconfig = \%main::myconfig;
1356 my $form = $main::form;
1358 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1361 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1363 $form->{curr} =~ s/ //g;
1364 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1365 my $currency = join ':', @currencies;
1367 # these defaults are database wide
1370 qq|UPDATE defaults SET
1371 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1372 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1373 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1374 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1375 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1376 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1392 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1393 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1394 $form->{invnumber}, $form->{cnnumber},
1395 $form->{sonumber}, $form->{ponumber},
1396 $form->{sqnumber}, $form->{rfqnumber},
1397 $form->{customernumber}, $form->{vendornumber},
1398 $form->{articlenumber}, $form->{servicenumber},
1399 $form->{sdonumber}, $form->{pdonumber},
1401 $form->{businessnumber}, $form->{weightunit});
1402 do_query($form, $dbh, $query, @values);
1406 $main::lxdebug->leave_sub();
1410 sub save_preferences {
1411 $main::lxdebug->enter_sub();
1413 my ($self, $myconfig, $form) = @_;
1415 my $dbh = $form->get_standard_dbh($myconfig);
1417 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1420 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1421 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1423 my $rc = $dbh->commit();
1425 # save first currency in myconfig
1426 $currency =~ s/:.*//;
1427 $form->{currency} = $currency;
1429 $form->{businessnumber} = $businessnumber;
1431 $myconfig = new User($form->{login});
1433 foreach my $item (keys %$form) {
1434 $myconfig->{$item} = $form->{$item};
1437 $myconfig->save_member;
1439 my $auth = $main::auth;
1441 $main::lxdebug->leave_sub();
1447 $main::lxdebug->enter_sub();
1452 my $myconfig = \%main::myconfig;
1453 my $form = $main::form;
1455 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1457 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1459 $defaults->{weightunit} ||= 'kg';
1461 $main::lxdebug->leave_sub();
1466 sub defaultaccounts {
1467 $main::lxdebug->enter_sub();
1469 my ($self, $myconfig, $form) = @_;
1471 # connect to database
1472 my $dbh = $form->dbconnect($myconfig);
1474 # get defaults from defaults table
1475 my $query = qq|SELECT * FROM defaults|;
1476 my $sth = $dbh->prepare($query);
1477 $sth->execute || $form->dberror($query);
1479 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1480 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1481 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1482 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1483 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1484 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1485 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1487 $form->{defaults}{weightunit} ||= 'kg';
1491 $query = qq|SELECT c.id, c.accno, c.description, c.link
1493 WHERE c.link LIKE '%IC%'
1495 $sth = $dbh->prepare($query);
1496 $sth->execute || $self->dberror($query);
1498 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1499 foreach my $key (split(/:/, $ref->{link})) {
1502 if ($key =~ /cogs/) {
1503 $nkey = "IC_expense";
1505 if ($key =~ /sale/) {
1506 $nkey = "IC_income";
1508 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1510 description => $ref->{description}
1517 $query = qq|SELECT c.id, c.accno, c.description
1519 WHERE c.category = 'I'
1520 AND c.charttype = 'A'
1522 $sth = $dbh->prepare($query);
1523 $sth->execute || $self->dberror($query);
1525 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1526 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1528 description => $ref->{description}
1533 $query = qq|SELECT c.id, c.accno, c.description
1535 WHERE c.category = 'E'
1536 AND c.charttype = 'A'
1538 $sth = $dbh->prepare($query);
1539 $sth->execute || $self->dberror($query);
1541 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1542 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1544 description => $ref->{description}
1549 # now get the tax rates and numbers
1550 $query = qq|SELECT c.id, c.accno, c.description,
1551 t.rate * 100 AS rate, t.taxnumber
1553 WHERE c.id = t.chart_id|;
1555 $sth = $dbh->prepare($query);
1556 $sth->execute || $form->dberror($query);
1558 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1559 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1560 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1561 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1562 if $ref->{taxnumber};
1563 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1565 # Abfrage für Standard Umlaufvermögenskonto
1567 qq|SELECT id, accno, description, link | .
1569 qq|WHERE link LIKE ? |.
1571 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1572 $sth->execute || $form->dberror($query);#
1573 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1574 foreach my $item (split(/:/, $ref->{link})) {
1575 if ($item eq "AR_paid") {
1576 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1578 description => $ref->{description}
1587 $main::lxdebug->leave_sub();
1591 $main::lxdebug->enter_sub();
1593 my ($self, $myconfig, $form) = @_;
1595 my $dbh = $form->dbconnect($myconfig);
1597 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1598 my $sth = $dbh->prepare($query);
1599 $sth->execute || $form->dberror($query);
1601 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1607 $main::lxdebug->leave_sub();
1611 $main::lxdebug->enter_sub();
1613 my ($self, $myconfig, $form) = @_;
1615 my $dbh = $form->dbconnect($myconfig);
1617 my ($query, @values);
1619 if ($form->{revtrans}) {
1620 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1622 } elsif ($form->{closedto}) {
1623 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1624 @values = (conv_date($form->{closedto}));
1627 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1630 # set close in defaults
1631 do_query($form, $dbh, $query, @values);
1635 $main::lxdebug->leave_sub();
1639 my ($self, $units, $unit_name, $factor) = @_;
1641 $factor = 1 unless ($factor);
1643 my $unit = $units->{$unit_name};
1645 if (!defined($unit) || !$unit->{"base_unit"} ||
1646 ($unit_name eq $unit->{"base_unit"})) {
1647 return ($unit_name, $factor);
1650 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1653 sub retrieve_units {
1654 $main::lxdebug->enter_sub();
1656 my ($self, $myconfig, $form, $prefix) = @_;
1658 my $dbh = $form->get_standard_dbh;
1660 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1662 my $sth = prepare_execute_query($form, $dbh, $query);
1665 while (my $ref = $sth->fetchrow_hashref()) {
1666 $units->{$ref->{"name"}} = $ref;
1670 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1671 $sth = $dbh->prepare($query_lang);
1672 $sth->execute() || $form->dberror($query_lang);
1674 while (my $ref = $sth->fetchrow_hashref()) {
1675 push(@languages, $ref);
1679 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1680 "FROM units_language ul " .
1681 "LEFT JOIN language l ON ul.language_id = l.id " .
1682 "WHERE ul.unit = ?";
1683 $sth = $dbh->prepare($query_lang);
1685 foreach my $unit (values(%{$units})) {
1686 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1688 $unit->{"LANGUAGES"} = {};
1689 foreach my $lang (@languages) {
1690 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1693 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1694 while (my $ref = $sth->fetchrow_hashref()) {
1695 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1700 $main::lxdebug->leave_sub();
1705 sub retrieve_all_units {
1706 $main::lxdebug->enter_sub();
1710 if (!$main::all_units) {
1711 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1714 $main::lxdebug->leave_sub();
1716 return $main::all_units;
1720 sub translate_units {
1721 $main::lxdebug->enter_sub();
1723 my ($self, $form, $template_code, $unit, $amount) = @_;
1725 my $units = $self->retrieve_units(\%main::myconfig, $form);
1727 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1728 my $new_unit = $unit;
1730 if (($amount != 1) && $h->{"localized_plural"}) {
1731 $new_unit = $h->{"localized_plural"};
1732 } elsif ($h->{"localized"}) {
1733 $new_unit = $h->{"localized"};
1737 $main::lxdebug->leave_sub();
1743 $main::lxdebug->enter_sub();
1745 my ($self, $myconfig, $form, $units) = @_;
1747 my $dbh = $form->dbconnect($myconfig);
1749 map({ $_->{"in_use"} = 0; } values(%{$units}));
1751 foreach my $unit (values(%{$units})) {
1752 my $base_unit = $unit->{"original_base_unit"};
1753 while ($base_unit) {
1754 $units->{$base_unit}->{"in_use"} = 1;
1755 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1756 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1757 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1761 foreach my $unit (values(%{$units})) {
1762 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1764 foreach my $table (qw(parts invoice orderitems)) {
1765 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1767 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1768 $query .= "= " . $dbh->quote($unit->{"name"});
1770 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1771 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1774 my ($count) = $dbh->selectrow_array($query);
1775 $form->dberror($query) if ($dbh->err);
1778 $unit->{"in_use"} = 1;
1786 $main::lxdebug->leave_sub();
1789 sub convertible_units {
1790 $main::lxdebug->enter_sub();
1794 my $filter_unit = shift;
1795 my $not_smaller = shift;
1797 my $conv_units = [];
1799 $filter_unit = $units->{$filter_unit};
1801 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1802 my $unit = $units->{$name};
1804 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1805 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1806 push @{$conv_units}, $unit;
1810 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1812 $main::lxdebug->leave_sub();
1817 # if $a is translatable to $b, return the factor between them.
1820 $main::lxdebug->enter_sub(2);
1821 my ($this, $a, $b, $all_units) = @_;
1823 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1824 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1825 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1826 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1829 sub unit_select_data {
1830 $main::lxdebug->enter_sub();
1832 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1837 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1840 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1841 if (!$convertible_into ||
1842 ($units->{$convertible_into} &&
1843 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1844 push @{$select}, { "name" => $unit,
1845 "base_unit" => $units->{$unit}->{"base_unit"},
1846 "factor" => $units->{$unit}->{"factor"},
1847 "selected" => ($unit eq $selected) ? "selected" : "" };
1851 $main::lxdebug->leave_sub();
1856 sub unit_select_html {
1857 $main::lxdebug->enter_sub();
1859 my ($self, $units, $name, $selected, $convertible_into) = @_;
1861 my $select = "<select name=${name}>";
1863 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1864 if (!$convertible_into ||
1865 ($units->{$convertible_into} &&
1866 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1867 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1870 $select .= "</select>";
1872 $main::lxdebug->leave_sub();
1878 $main::lxdebug->enter_sub();
1882 my $units = $self->retrieve_all_units();
1887 while (2 <= scalar(@_)) {
1888 my $qty = shift(@_);
1889 my $unit = $units->{shift(@_)};
1891 croak "No unit defined with name $unit" if (!defined $unit);
1894 $base_unit = $unit->{base_unit};
1895 } elsif ($base_unit ne $unit->{base_unit}) {
1896 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1899 $sum += $qty * $unit->{factor};
1902 $main::lxdebug->leave_sub();
1904 return wantarray ? ($sum, $base_unit) : $sum;
1908 $main::lxdebug->enter_sub();
1910 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1912 my $dbh = $form->dbconnect_noauto($myconfig);
1914 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1915 my ($sortkey) = selectrow_query($form, $dbh, $query);
1917 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1918 "VALUES (?, ?, ?, ?)";
1919 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1922 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1923 my $sth = $dbh->prepare($query);
1924 foreach my $lang (@{$languages}) {
1925 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1926 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1934 $main::lxdebug->leave_sub();
1938 $main::lxdebug->enter_sub();
1940 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1942 my $dbh = $form->dbconnect_noauto($myconfig);
1944 my ($base_unit, $unit, $sth, $query);
1946 $query = "DELETE FROM units_language";
1947 $dbh->do($query) || $form->dberror($query);
1949 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1950 $query = "DELETE FROM units WHERE name IN (";
1951 map({ $query .= "?," } @{$delete_units});
1952 substr($query, -1, 1) = ")";
1953 $dbh->do($query, undef, @{$delete_units}) ||
1954 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1957 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1958 $sth = $dbh->prepare($query);
1960 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1961 my $sth_lang = $dbh->prepare($query_lang);
1963 foreach $unit (values(%{$units})) {
1964 $unit->{"depth"} = 0;
1965 my $base_unit = $unit;
1966 while ($base_unit->{"base_unit"}) {
1968 $base_unit = $units->{$base_unit->{"base_unit"}};
1972 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1973 if ($unit->{"LANGUAGES"}) {
1974 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1975 next unless ($lang->{"id"} && $lang->{"localized"});
1976 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1977 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1981 next if ($unit->{"unchanged_unit"});
1983 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1984 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1988 $sth_lang->finish();
1992 $main::lxdebug->leave_sub();
1996 $main::lxdebug->enter_sub();
1998 my ($self, $myconfig, $form, $dir, $name_1) = @_;
2000 my $dbh = $form->dbconnect_noauto($myconfig);
2004 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2005 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2008 qq|SELECT sortkey FROM units | .
2009 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
2010 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2011 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
2013 if (defined($sortkey_1)) {
2014 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2015 my ($name_2) = selectrow_query($form, $dbh, $query);
2017 if (defined($name_2)) {
2018 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2019 my $sth = $dbh->prepare($query);
2021 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2022 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2029 $main::lxdebug->leave_sub();
2033 $main::lxdebug->enter_sub();
2035 my ($self, $myconfig, $form) = @_;
2037 # connect to database
2038 my $dbh = $form->dbconnect($myconfig);
2040 my $query = qq|SELECT
2044 round(t.rate * 100, 2) AS rate,
2045 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2046 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2050 my $sth = $dbh->prepare($query);
2051 $sth->execute || $form->dberror($query);
2054 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2055 push @{ $form->{TAX} }, $ref;
2061 $main::lxdebug->leave_sub();
2064 sub get_tax_accounts {
2065 $main::lxdebug->enter_sub();
2067 my ($self, $myconfig, $form) = @_;
2069 my $dbh = $form->dbconnect($myconfig);
2071 # get Accounts from chart
2072 my $query = qq{ SELECT
2074 accno || ' - ' || description AS taxaccount
2076 WHERE link LIKE '%_tax%'
2080 my $sth = $dbh->prepare($query);
2081 $sth->execute || $form->dberror($query);
2083 $form->{ACCOUNTS} = [];
2084 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2085 push @{ $form->{ACCOUNTS} }, $ref;
2092 $main::lxdebug->leave_sub();
2096 $main::lxdebug->enter_sub();
2098 my ($self, $myconfig, $form) = @_;
2100 # connect to database
2101 my $dbh = $form->dbconnect($myconfig);
2103 my $query = qq|SELECT
2106 round(rate * 100, 2) AS rate,
2111 my $sth = $dbh->prepare($query);
2112 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2114 my $ref = $sth->fetchrow_hashref("NAME_lc");
2116 map { $form->{$_} = $ref->{$_} } keys %$ref;
2120 # see if it is used by a taxkey
2121 $query = qq|SELECT count(*) FROM taxkeys
2122 WHERE tax_id = ? AND chart_id >0|;
2124 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2126 $form->{orphaned} = !$form->{orphaned};
2129 if (!$form->{orphaned} ) {
2130 $query = qq|SELECT DISTINCT c.id, c.accno
2132 JOIN tax t ON (t.id = tk.tax_id)
2133 JOIN chart c ON (c.id = tk.chart_id)
2134 WHERE tk.tax_id = ?|;
2136 $sth = $dbh->prepare($query);
2137 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2139 $form->{TAXINUSE} = [];
2140 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2141 push @{ $form->{TAXINUSE} }, $ref;
2149 $main::lxdebug->leave_sub();
2153 $main::lxdebug->enter_sub();
2155 my ($self, $myconfig, $form) = @_;
2158 # connect to database
2159 my $dbh = $form->get_standard_dbh($myconfig);
2161 $form->{rate} = $form->{rate} / 100;
2163 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2164 if ($form->{id} ne "") {
2165 $query = qq|UPDATE tax SET
2170 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2172 push(@values, $form->{id});
2176 $query = qq|INSERT INTO tax (
2183 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2185 do_query($form, $dbh, $query, @values);
2189 $main::lxdebug->leave_sub();
2193 $main::lxdebug->enter_sub();
2195 my ($self, $myconfig, $form) = @_;
2198 # connect to database
2199 my $dbh = $form->get_standard_dbh($myconfig);
2201 $query = qq|DELETE FROM tax
2203 do_query($form, $dbh, $query, $form->{id});
2207 $main::lxdebug->leave_sub();
2210 sub save_price_factor {
2211 $main::lxdebug->enter_sub();
2213 my ($self, $myconfig, $form) = @_;
2215 # connect to database
2216 my $dbh = $form->get_standard_dbh($myconfig);
2219 my @values = ($form->{description}, conv_i($form->{factor}));
2222 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2223 push @values, conv_i($form->{id});
2226 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2229 do_query($form, $dbh, $query, @values);
2233 $main::lxdebug->leave_sub();
2236 sub get_all_price_factors {
2237 $main::lxdebug->enter_sub();
2239 my ($self, $myconfig, $form) = @_;
2241 # connect to database
2242 my $dbh = $form->get_standard_dbh($myconfig);
2244 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2246 $main::lxdebug->leave_sub();
2249 sub get_price_factor {
2250 $main::lxdebug->enter_sub();
2252 my ($self, $myconfig, $form) = @_;
2254 # connect to database
2255 my $dbh = $form->get_standard_dbh($myconfig);
2257 my $query = qq|SELECT description, factor,
2258 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2259 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2260 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2261 FROM price_factors WHERE id = ?|;
2263 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2265 $main::lxdebug->leave_sub();
2268 sub delete_price_factor {
2269 $main::lxdebug->enter_sub();
2271 my ($self, $myconfig, $form) = @_;
2273 # connect to database
2274 my $dbh = $form->get_standard_dbh($myconfig);
2276 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2279 $main::lxdebug->leave_sub();
2282 sub save_warehouse {
2283 $main::lxdebug->enter_sub();
2285 my ($self, $myconfig, $form) = @_;
2287 # connect to database
2288 my $dbh = $form->get_standard_dbh($myconfig);
2290 my ($query, @values, $sth);
2293 $query = qq|SELECT nextval('id')|;
2294 ($form->{id}) = selectrow_query($form, $dbh, $query);
2296 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2297 do_query($form, $dbh, $query, $form->{id});
2300 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2301 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2303 if (0 < $form->{number_of_new_bins}) {
2304 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2305 $sth = prepare_query($form, $dbh, $query);
2307 foreach my $i (1..$form->{number_of_new_bins}) {
2308 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2316 $main::lxdebug->leave_sub();
2320 $main::lxdebug->enter_sub();
2322 my ($self, $myconfig, $form) = @_;
2324 # connect to database
2325 my $dbh = $form->get_standard_dbh($myconfig);
2327 my ($query, @values, $commit_necessary, $sth);
2329 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2332 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2333 do_query($form, $dbh, $query, @values);
2335 $commit_necessary = 1;
2338 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2339 $sth = prepare_query($form, $dbh, $query);
2341 foreach my $row (1..$form->{rowcount}) {
2342 next if ($form->{"delete_${row}"});
2344 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2346 $commit_necessary = 1;
2351 $dbh->commit() if ($commit_necessary);
2353 $main::lxdebug->leave_sub();
2356 sub delete_warehouse {
2357 $main::lxdebug->enter_sub();
2359 my ($self, $myconfig, $form) = @_;
2361 # connect to database
2362 my $dbh = $form->get_standard_dbh($myconfig);
2364 my $id = conv_i($form->{id});
2365 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|;
2366 my ($count) = selectrow_query($form, $dbh, $query, $id);
2369 $main::lxdebug->leave_sub();
2373 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2374 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2378 $main::lxdebug->leave_sub();
2383 sub get_all_warehouses {
2384 $main::lxdebug->enter_sub();
2386 my ($self, $myconfig, $form) = @_;
2388 # connect to database
2389 my $dbh = $form->get_standard_dbh($myconfig);
2391 my $query = qq|SELECT w.id, w.description, w.invalid,
2392 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2394 ORDER BY w.sortkey|;
2396 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2398 $main::lxdebug->leave_sub();
2402 $main::lxdebug->enter_sub();
2404 my ($self, $myconfig, $form) = @_;
2406 # connect to database
2407 my $dbh = $form->get_standard_dbh($myconfig);
2409 my $id = conv_i($form->{id});
2410 my $query = qq|SELECT w.description, w.invalid
2414 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2416 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2418 $query = qq|SELECT b.*, EXISTS
2419 (SELECT i.warehouse_id
2421 WHERE i.bin_id = b.id
2425 WHERE b.warehouse_id = ?|;
2427 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2429 $main::lxdebug->leave_sub();