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 # TODO: it should be forbidden to change an account to a heading if there
203 # have been bookings to this account in the past
205 my ($self, $myconfig, $form) = @_;
207 # connect to database, turn off AutoCommit
208 my $dbh = $form->dbconnect_noauto($myconfig);
210 # sanity check, can't have AR with AR_...
211 if ($form->{AR} || $form->{AP} || $form->{IC}) {
212 map { delete $form->{$_} }
213 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);
216 if ($form->{AR_include_in_dropdown}) {
217 $form->{$form->{AR_include_in_dropdown}} = $form->{AR_include_in_dropdown};
219 if ($form->{AP_include_in_dropdown}) {
220 $form->{$form->{AP_include_in_dropdown}} = $form->{AP_include_in_dropdown};
224 foreach my $item ($form->{AR}, $form->{AR_amount},
225 $form->{AR_tax}, $form->{AR_paid},
226 $form->{AP}, $form->{AP_amount},
227 $form->{AP_tax}, $form->{AP_paid},
228 $form->{IC}, $form->{IC_sale},
229 $form->{IC_cogs}, $form->{IC_taxpart},
230 $form->{IC_income}, $form->{IC_expense},
231 $form->{IC_taxservice}
233 $form->{link} .= "${item}:" if ($item);
237 # strip blanks from accno
238 map { $form->{$_} =~ s/ //g; } qw(accno);
242 if ($form->{id} eq "NULL") {
246 if (!$form->{id} || $form->{id} eq "") {
247 $query = qq|SELECT nextval('id')|;
248 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
249 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
250 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
258 # if charttype is heading make sure certain values are empty
259 # specifically, if charttype is changed from an existing account, empty the
260 # fields unnecessary for headings, so that e.g. heading doesn't appear in
261 # drop-down menues due to still having a valid "link" entry
263 if ( $form->{charttype} eq 'H' ) {
265 $form->{pos_bwa} = '';
266 $form->{pos_bilanz} = '';
267 $form->{pos_eur} = '';
268 $form->{new_chart_id} = '';
269 $form->{valid_from} = '';
272 $query = qq|UPDATE chart SET
288 $form->{description},
292 conv_i($form->{pos_bwa}),
293 conv_i($form->{pos_bilanz}),
294 conv_i($form->{pos_eur}),
295 conv_i($form->{new_chart_id}),
296 conv_date($form->{valid_from}),
297 ($form->{datevautomatik} eq 'T') ? 'true':'false',
304 do_query($form, $dbh, $query, @values);
310 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
314 for $tk_count (0 .. $MAX_TRIES) {
318 # Check if the account already exists, else cancel
320 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
322 last READTAXKEYS if ( $form->{'id'} == 0);
324 # check if there is a startdate
325 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
330 # Add valid taxkeys into the array
333 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
334 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
335 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
336 chart_id => conv_i($form->{"id"}),
337 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
338 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
345 for my $j (0 .. $#taxkeys){
346 if ( defined $taxkeys[$j]{'id'} ){
349 if ($taxkeys[$j]{'delete'}){
351 DELETE FROM taxkeys WHERE id = ?
354 @values = ($taxkeys[$j]{'id'});
356 do_query($form, $dbh, $query, @values);
365 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
373 $taxkeys[$j]{'tax_id'},
374 $taxkeys[$j]{'chart_id'},
375 $taxkeys[$j]{'tax_id'},
376 $taxkeys[$j]{'pos_ustva'},
377 $taxkeys[$j]{'startdate'},
380 do_query($form, $dbh, $query, @values);
386 INSERT INTO taxkeys (
393 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
396 $taxkeys[$j]{'tax_id'},
397 $taxkeys[$j]{'chart_id'},
398 $taxkeys[$j]{'tax_id'},
399 $taxkeys[$j]{'pos_ustva'},
400 $taxkeys[$j]{'startdate'},
403 do_query($form, $dbh, $query, @values);
408 # Update chart.taxkey_id to the latest from taxkeys for this chart.
414 WHERE taxkeys.chart_id = chart.id
415 ORDER BY startdate DESC
421 do_query($form, $dbh, $query, $form->{id});
424 my $rc = $dbh->commit;
427 $main::lxdebug->leave_sub();
433 $main::lxdebug->enter_sub();
435 my ($self, $myconfig, $form) = @_;
437 # connect to database, turn off AutoCommit
438 my $dbh = $form->dbconnect_noauto($myconfig);
440 my $query = qq|SELECT count(*) FROM acc_trans a
441 WHERE a.chart_id = ?|;
442 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
446 $main::lxdebug->leave_sub();
450 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
451 foreach my $type (qw(inventory income expense)) {
454 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
455 qq|WHERE ${type}_accno_id = ?|;
456 do_query($form, $dbh, $query, $form->{id});
459 foreach my $table (qw(partstax customertax vendortax tax)) {
460 $query = qq|DELETE FROM $table
462 do_query($form, $dbh, $query, $form->{id});
465 # delete chart of account record
466 $query = qq|DELETE FROM chart
468 do_query($form, $dbh, $query, $form->{id});
470 # delete account taxkeys
471 $query = qq|DELETE FROM taxkeys
473 do_query($form, $dbh, $query, $form->{id});
475 # commit and redirect
476 my $rc = $dbh->commit;
479 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my ($self, $myconfig, $form) = @_;
489 # connect to database
490 my $dbh = $form->dbconnect($myconfig);
492 my $query = qq|SELECT id, lead
496 my $sth = $dbh->prepare($query);
497 $sth->execute || $form->dberror($query);
499 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
500 push @{ $form->{ALL} }, $ref;
506 $main::lxdebug->leave_sub();
510 $main::lxdebug->enter_sub();
512 my ($self, $myconfig, $form) = @_;
514 # connect to database
515 my $dbh = $form->dbconnect($myconfig);
518 qq|SELECT l.id, l.lead | .
521 my $sth = $dbh->prepare($query);
522 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
524 my $ref = $sth->fetchrow_hashref("NAME_lc");
526 map { $form->{$_} = $ref->{$_} } keys %$ref;
532 $main::lxdebug->leave_sub();
536 $main::lxdebug->enter_sub();
538 my ($self, $myconfig, $form) = @_;
541 # connect to database
542 my $dbh = $form->dbconnect($myconfig);
544 my @values = ($form->{description});
545 # id is the old record
547 $query = qq|UPDATE leads SET
550 push(@values, $form->{id});
552 $query = qq|INSERT INTO leads
556 do_query($form, $dbh, $query, @values);
560 $main::lxdebug->leave_sub();
564 $main::lxdebug->enter_sub();
566 my ($self, $myconfig, $form) = @_;
569 # connect to database
570 my $dbh = $form->dbconnect($myconfig);
572 $query = qq|DELETE FROM leads
574 do_query($form, $dbh, $query, $form->{id});
578 $main::lxdebug->leave_sub();
582 $main::lxdebug->enter_sub();
584 my ($self, $myconfig, $form, $return_list) = @_;
586 # connect to database
587 my $dbh = $form->dbconnect($myconfig);
590 "SELECT id, description, template_code, article_code, " .
591 " output_numberformat, output_dateformat, output_longdates " .
592 "FROM language ORDER BY description";
594 my $sth = $dbh->prepare($query);
595 $sth->execute || $form->dberror($query);
599 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
600 push(@{ $ary }, $ref);
606 $main::lxdebug->leave_sub();
616 $main::lxdebug->enter_sub();
618 my ($self, $myconfig, $form) = @_;
620 # connect to database
621 my $dbh = $form->dbconnect($myconfig);
624 "SELECT description, template_code, article_code, " .
625 " output_numberformat, output_dateformat, output_longdates " .
626 "FROM language WHERE id = ?";
627 my $sth = $dbh->prepare($query);
628 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
630 my $ref = $sth->fetchrow_hashref("NAME_lc");
632 map { $form->{$_} = $ref->{$_} } keys %$ref;
638 $main::lxdebug->leave_sub();
641 sub get_language_details {
642 $main::lxdebug->enter_sub();
644 my ($self, $myconfig, $form, $id) = @_;
646 # connect to database
647 my $dbh = $form->dbconnect($myconfig);
650 "SELECT template_code, " .
651 " output_numberformat, output_dateformat, output_longdates " .
652 "FROM language WHERE id = ?";
653 my @res = selectrow_query($form, $dbh, $query, $id);
656 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
668 my (@values, $query);
670 map({ push(@values, $form->{$_}); }
671 qw(description template_code article_code
672 output_numberformat output_dateformat output_longdates));
674 # id is the old record
677 "UPDATE language SET " .
678 " description = ?, template_code = ?, article_code = ?, " .
679 " output_numberformat = ?, output_dateformat = ?, " .
680 " output_longdates = ? " .
682 push(@values, $form->{id});
685 "INSERT INTO language (" .
686 " description, template_code, article_code, " .
687 " output_numberformat, output_dateformat, output_longdates" .
688 ") VALUES (?, ?, ?, ?, ?, ?)";
690 do_query($form, $dbh, $query, @values);
694 $main::lxdebug->leave_sub();
697 sub delete_language {
698 $main::lxdebug->enter_sub();
700 my ($self, $myconfig, $form) = @_;
703 # connect to database
704 my $dbh = $form->dbconnect_noauto($myconfig);
706 foreach my $table (qw(generic_translations units_language)) {
707 $query = qq|DELETE FROM $table WHERE language_id = ?|;
708 do_query($form, $dbh, $query, $form->{"id"});
711 $query = "DELETE FROM language WHERE id = ?";
712 do_query($form, $dbh, $query, $form->{"id"});
717 $main::lxdebug->leave_sub();
722 $main::lxdebug->enter_sub();
724 my ($self, $myconfig, $form) = @_;
726 # connect to database
727 my $dbh = $form->dbconnect($myconfig);
729 my $query = qq|SELECT id, description,
731 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
733 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
735 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
737 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
739 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
741 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
743 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
745 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
747 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
751 my $sth = $dbh->prepare($query);
752 $sth->execute || $form->dberror($query);
755 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
756 push @{ $form->{ALL} }, $ref;
762 $main::lxdebug->leave_sub();
765 sub get_buchungsgruppe {
766 $main::lxdebug->enter_sub();
768 my ($self, $myconfig, $form) = @_;
771 # connect to database
772 my $dbh = $form->dbconnect($myconfig);
776 qq|SELECT description, inventory_accno_id,
777 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
779 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
781 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
783 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
785 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
787 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
789 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
791 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
793 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
796 my $sth = $dbh->prepare($query);
797 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
799 my $ref = $sth->fetchrow_hashref("NAME_lc");
801 map { $form->{$_} = $ref->{$_} } keys %$ref;
806 qq|SELECT count(id) = 0 AS orphaned
808 WHERE buchungsgruppen_id = ?|;
809 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
812 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
814 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
815 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
818 $query = qq|SELECT c.accno, c.description, c.link, c.id,
819 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
820 FROM chart c, defaults d
821 WHERE c.link LIKE '%$module%'
825 my $sth = $dbh->prepare($query);
826 $sth->execute || $form->dberror($query);
827 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
828 foreach my $key (split(/:/, $ref->{link})) {
829 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
830 $form->{"std_inventory_accno_id"} = $ref->{"id"};
832 if ($key =~ /$module/) {
833 if ( ($ref->{id} eq $ref->{inventory_accno_id})
834 || ($ref->{id} eq $ref->{income_accno_id})
835 || ($ref->{id} eq $ref->{expense_accno_id})) {
836 push @{ $form->{"${module}_links"}{$key} },
837 { accno => $ref->{accno},
838 description => $ref->{description},
839 selected => "selected",
842 push @{ $form->{"${module}_links"}{$key} },
843 { accno => $ref->{accno},
844 description => $ref->{description},
856 $main::lxdebug->leave_sub();
859 sub save_buchungsgruppe {
860 $main::lxdebug->enter_sub();
862 my ($self, $myconfig, $form) = @_;
864 # connect to database
865 my $dbh = $form->dbconnect($myconfig);
867 my @values = ($form->{description}, $form->{inventory_accno_id},
868 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
869 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
870 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
871 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
875 # id is the old record
877 $query = qq|UPDATE buchungsgruppen SET
878 description = ?, inventory_accno_id = ?,
879 income_accno_id_0 = ?, expense_accno_id_0 = ?,
880 income_accno_id_1 = ?, expense_accno_id_1 = ?,
881 income_accno_id_2 = ?, expense_accno_id_2 = ?,
882 income_accno_id_3 = ?, expense_accno_id_3 = ?
884 push(@values, $form->{id});
886 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
887 my ($sortkey) = $dbh->selectrow_array($query);
888 $form->dberror($query) if ($dbh->err);
889 push(@values, $sortkey);
890 $query = qq|INSERT INTO buchungsgruppen
891 (description, inventory_accno_id,
892 income_accno_id_0, expense_accno_id_0,
893 income_accno_id_1, expense_accno_id_1,
894 income_accno_id_2, expense_accno_id_2,
895 income_accno_id_3, expense_accno_id_3,
897 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
899 do_query($form, $dbh, $query, @values);
903 $main::lxdebug->leave_sub();
906 sub delete_buchungsgruppe {
907 $main::lxdebug->enter_sub();
909 my ($self, $myconfig, $form) = @_;
911 # connect to database
912 my $dbh = $form->dbconnect($myconfig);
914 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
915 do_query($form, $dbh, $query, $form->{id});
919 $main::lxdebug->leave_sub();
923 $main::lxdebug->enter_sub();
925 my ($self, $myconfig, $form, $table) = @_;
927 # connect to database
928 my $dbh = $form->get_standard_dbh($myconfig);
932 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
933 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
934 my @values = ($form->{"id1"}, $form->{"id2"});
935 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
937 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
938 my $sth = prepare_query($form, $dbh, $query);
940 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
941 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
947 $main::lxdebug->leave_sub();
950 sub prepare_template_filename {
951 $main::lxdebug->enter_sub();
953 my ($self, $myconfig, $form) = @_;
955 my ($filename, $display_filename);
957 if ($form->{type} eq "stylesheet") {
958 $filename = "css/$myconfig->{stylesheet}";
959 $display_filename = $myconfig->{stylesheet};
962 $filename = $form->{formname};
964 if ($form->{language}) {
965 my ($id, $template_code) = split(/--/, $form->{language});
966 $filename .= "_${template_code}";
969 if ($form->{printer}) {
970 my ($id, $template_code) = split(/--/, $form->{printer});
971 $filename .= "_${template_code}";
974 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
975 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
976 $filename =~ s|.*/||;
978 $display_filename = $filename;
979 $filename = "$myconfig->{templates}/$filename";
982 $main::lxdebug->leave_sub();
984 return ($filename, $display_filename);
989 $main::lxdebug->enter_sub();
991 my ($self, $filename) = @_;
993 my ($content, $lines) = ("", 0);
997 if (open(TEMPLATE, $filename)) {
1005 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1007 $main::lxdebug->leave_sub();
1009 return ($content, $lines);
1013 $main::lxdebug->enter_sub();
1015 my ($self, $filename, $content) = @_;
1021 if (open(TEMPLATE, ">", $filename)) {
1022 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1023 $content =~ s/\r\n/\n/g;
1024 print(TEMPLATE $content);
1030 $main::lxdebug->leave_sub();
1036 $main::lxdebug->enter_sub();
1041 my $myconfig = \%main::myconfig;
1042 my $form = $main::form;
1044 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1047 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1049 $form->{curr} =~ s/ //g;
1050 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1051 my $currency = join ':', @currencies;
1053 # these defaults are database wide
1056 qq|UPDATE defaults SET
1057 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1058 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1059 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1060 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1061 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1062 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1079 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1080 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1081 $form->{invnumber}, $form->{cnnumber},
1082 $form->{sonumber}, $form->{ponumber},
1083 $form->{sqnumber}, $form->{rfqnumber},
1084 $form->{customernumber}, $form->{vendornumber},
1085 $form->{articlenumber}, $form->{servicenumber},
1086 $form->{sdonumber}, $form->{pdonumber},
1088 $form->{businessnumber}, $form->{weightunit},
1089 conv_i($form->{language_id}));
1090 do_query($form, $dbh, $query, @values);
1094 $main::lxdebug->leave_sub();
1098 sub save_preferences {
1099 $main::lxdebug->enter_sub();
1101 my ($self, $myconfig, $form) = @_;
1103 my $dbh = $form->get_standard_dbh($myconfig);
1105 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1108 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1109 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1111 my $rc = $dbh->commit();
1113 # save first currency in myconfig
1114 $currency =~ s/:.*//;
1115 $form->{currency} = $currency;
1117 $form->{businessnumber} = $businessnumber;
1119 $myconfig = User->new(login => $form->{login});
1121 foreach my $item (keys %$form) {
1122 $myconfig->{$item} = $form->{$item};
1125 $myconfig->save_member;
1127 my $auth = $main::auth;
1129 $main::lxdebug->leave_sub();
1135 $main::lxdebug->enter_sub();
1140 my $myconfig = \%main::myconfig;
1141 my $form = $main::form;
1143 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1145 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1147 $defaults->{weightunit} ||= 'kg';
1149 $main::lxdebug->leave_sub();
1154 sub defaultaccounts {
1155 $main::lxdebug->enter_sub();
1157 my ($self, $myconfig, $form) = @_;
1159 # connect to database
1160 my $dbh = $form->dbconnect($myconfig);
1162 # get defaults from defaults table
1163 my $query = qq|SELECT * FROM defaults|;
1164 my $sth = $dbh->prepare($query);
1165 $sth->execute || $form->dberror($query);
1167 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1168 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1169 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1170 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1171 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1172 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1173 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1175 $form->{defaults}{weightunit} ||= 'kg';
1179 $query = qq|SELECT c.id, c.accno, c.description, c.link
1181 WHERE c.link LIKE '%IC%'
1183 $sth = $dbh->prepare($query);
1184 $sth->execute || $self->dberror($query);
1186 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1187 foreach my $key (split(/:/, $ref->{link})) {
1190 if ($key =~ /cogs/) {
1191 $nkey = "IC_expense";
1193 if ($key =~ /sale/) {
1194 $nkey = "IC_income";
1196 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1198 description => $ref->{description}
1205 $query = qq|SELECT c.id, c.accno, c.description
1207 WHERE c.category = 'I'
1208 AND c.charttype = 'A'
1210 $sth = $dbh->prepare($query);
1211 $sth->execute || $self->dberror($query);
1213 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1214 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1216 description => $ref->{description}
1221 $query = qq|SELECT c.id, c.accno, c.description
1223 WHERE c.category = 'E'
1224 AND c.charttype = 'A'
1226 $sth = $dbh->prepare($query);
1227 $sth->execute || $self->dberror($query);
1229 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1230 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1232 description => $ref->{description}
1237 # now get the tax rates and numbers
1238 $query = qq|SELECT c.id, c.accno, c.description,
1239 t.rate * 100 AS rate, t.taxnumber
1241 WHERE c.id = t.chart_id|;
1243 $sth = $dbh->prepare($query);
1244 $sth->execute || $form->dberror($query);
1246 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1247 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1248 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1249 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1250 if $ref->{taxnumber};
1251 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1253 # Abfrage für Standard Umlaufvermögenskonto
1255 qq|SELECT id, accno, description, link | .
1257 qq|WHERE link LIKE ? |.
1259 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1260 $sth->execute || $form->dberror($query);#
1261 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1262 foreach my $item (split(/:/, $ref->{link})) {
1263 if ($item eq "AR_paid") {
1264 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1266 description => $ref->{description}
1275 $main::lxdebug->leave_sub();
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig, $form) = @_;
1283 my $dbh = $form->dbconnect($myconfig);
1285 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1286 my $sth = $dbh->prepare($query);
1287 $sth->execute || $form->dberror($query);
1289 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1295 $main::lxdebug->leave_sub();
1299 $main::lxdebug->enter_sub();
1301 my ($self, $myconfig, $form) = @_;
1303 my $dbh = $form->dbconnect($myconfig);
1305 my ($query, @values);
1307 if ($form->{revtrans}) {
1308 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1310 } elsif ($form->{closedto}) {
1311 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1312 @values = (conv_date($form->{closedto}));
1315 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1318 # set close in defaults
1319 do_query($form, $dbh, $query, @values);
1323 $main::lxdebug->leave_sub();
1327 my ($self, $units, $unit_name, $factor) = @_;
1329 $factor = 1 unless ($factor);
1331 my $unit = $units->{$unit_name};
1333 if (!defined($unit) || !$unit->{"base_unit"} ||
1334 ($unit_name eq $unit->{"base_unit"})) {
1335 return ($unit_name, $factor);
1338 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1341 sub retrieve_units {
1342 $main::lxdebug->enter_sub();
1344 my ($self, $myconfig, $form, $prefix) = @_;
1347 my $dbh = $form->get_standard_dbh;
1349 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1351 my $sth = prepare_execute_query($form, $dbh, $query);
1354 while (my $ref = $sth->fetchrow_hashref()) {
1355 $units->{$ref->{"name"}} = $ref;
1359 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1360 $sth = $dbh->prepare($query_lang);
1361 $sth->execute() || $form->dberror($query_lang);
1363 while (my $ref = $sth->fetchrow_hashref()) {
1364 push(@languages, $ref);
1368 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1369 "FROM units_language ul " .
1370 "LEFT JOIN language l ON ul.language_id = l.id " .
1371 "WHERE ul.unit = ?";
1372 $sth = $dbh->prepare($query_lang);
1374 foreach my $unit (values(%{$units})) {
1375 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1377 $unit->{"LANGUAGES"} = {};
1378 foreach my $lang (@languages) {
1379 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1382 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1383 while (my $ref = $sth->fetchrow_hashref()) {
1384 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1389 $main::lxdebug->leave_sub();
1394 sub retrieve_all_units {
1395 $main::lxdebug->enter_sub();
1399 if (!$::request->{cache}{all_units}) {
1400 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1403 $main::lxdebug->leave_sub();
1405 return $::request->{cache}{all_units};
1409 sub translate_units {
1410 $main::lxdebug->enter_sub();
1412 my ($self, $form, $template_code, $unit, $amount) = @_;
1414 my $units = $self->retrieve_units(\%main::myconfig, $form);
1416 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1417 my $new_unit = $unit;
1419 if (($amount != 1) && $h->{"localized_plural"}) {
1420 $new_unit = $h->{"localized_plural"};
1421 } elsif ($h->{"localized"}) {
1422 $new_unit = $h->{"localized"};
1426 $main::lxdebug->leave_sub();
1432 $main::lxdebug->enter_sub();
1434 my ($self, $myconfig, $form, $units) = @_;
1436 my $dbh = $form->dbconnect($myconfig);
1438 map({ $_->{"in_use"} = 0; } values(%{$units}));
1440 foreach my $unit (values(%{$units})) {
1441 my $base_unit = $unit->{"original_base_unit"};
1442 while ($base_unit) {
1443 $units->{$base_unit}->{"in_use"} = 1;
1444 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1445 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1446 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1450 foreach my $unit (values(%{$units})) {
1451 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1453 foreach my $table (qw(parts invoice orderitems)) {
1454 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1456 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1457 $query .= "= " . $dbh->quote($unit->{"name"});
1459 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1460 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1463 my ($count) = $dbh->selectrow_array($query);
1464 $form->dberror($query) if ($dbh->err);
1467 $unit->{"in_use"} = 1;
1475 $main::lxdebug->leave_sub();
1478 sub convertible_units {
1479 $main::lxdebug->enter_sub();
1483 my $filter_unit = shift;
1484 my $not_smaller = shift;
1486 my $conv_units = [];
1488 $filter_unit = $units->{$filter_unit};
1490 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1491 my $unit = $units->{$name};
1493 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1494 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1495 push @{$conv_units}, $unit;
1499 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1501 $main::lxdebug->leave_sub();
1506 # if $a is translatable to $b, return the factor between them.
1509 $main::lxdebug->enter_sub(2);
1510 my ($this, $a, $b, $all_units) = @_;
1513 $all_units = $this->retrieve_all_units;
1516 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1517 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1518 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1519 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1522 sub unit_select_data {
1523 $main::lxdebug->enter_sub();
1525 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1530 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1533 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1534 if (!$convertible_into ||
1535 ($units->{$convertible_into} &&
1536 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1537 push @{$select}, { "name" => $unit,
1538 "base_unit" => $units->{$unit}->{"base_unit"},
1539 "factor" => $units->{$unit}->{"factor"},
1540 "selected" => ($unit eq $selected) ? "selected" : "" };
1544 $main::lxdebug->leave_sub();
1549 sub unit_select_html {
1550 $main::lxdebug->enter_sub();
1552 my ($self, $units, $name, $selected, $convertible_into) = @_;
1554 my $select = "<select name=${name}>";
1556 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1557 if (!$convertible_into ||
1558 ($units->{$convertible_into} &&
1559 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1560 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1563 $select .= "</select>";
1565 $main::lxdebug->leave_sub();
1571 $main::lxdebug->enter_sub();
1575 my $units = $self->retrieve_all_units();
1580 while (2 <= scalar(@_)) {
1581 my $qty = shift(@_);
1582 my $unit = $units->{shift(@_)};
1584 croak "No unit defined with name $unit" if (!defined $unit);
1587 $base_unit = $unit->{base_unit};
1588 } elsif ($base_unit ne $unit->{base_unit}) {
1589 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1592 $sum += $qty * $unit->{factor};
1595 $main::lxdebug->leave_sub();
1597 return wantarray ? ($sum, $base_unit) : $sum;
1601 $main::lxdebug->enter_sub();
1603 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1605 my $dbh = $form->dbconnect_noauto($myconfig);
1607 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1608 my ($sortkey) = selectrow_query($form, $dbh, $query);
1610 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1611 "VALUES (?, ?, ?, ?)";
1612 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1615 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1616 my $sth = $dbh->prepare($query);
1617 foreach my $lang (@{$languages}) {
1618 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1619 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1627 $main::lxdebug->leave_sub();
1631 $main::lxdebug->enter_sub();
1633 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1635 my $dbh = $form->dbconnect_noauto($myconfig);
1637 my ($base_unit, $unit, $sth, $query);
1639 $query = "DELETE FROM units_language";
1640 $dbh->do($query) || $form->dberror($query);
1642 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1643 $query = "DELETE FROM units WHERE name IN (";
1644 map({ $query .= "?," } @{$delete_units});
1645 substr($query, -1, 1) = ")";
1646 $dbh->do($query, undef, @{$delete_units}) ||
1647 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1650 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1651 $sth = $dbh->prepare($query);
1653 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1654 my $sth_lang = $dbh->prepare($query_lang);
1656 foreach $unit (values(%{$units})) {
1657 $unit->{"depth"} = 0;
1658 my $base_unit = $unit;
1659 while ($base_unit->{"base_unit"}) {
1661 $base_unit = $units->{$base_unit->{"base_unit"}};
1665 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1666 if ($unit->{"LANGUAGES"}) {
1667 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1668 next unless ($lang->{"id"} && $lang->{"localized"});
1669 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1670 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1674 next if ($unit->{"unchanged_unit"});
1676 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1677 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1681 $sth_lang->finish();
1685 $main::lxdebug->leave_sub();
1689 $main::lxdebug->enter_sub();
1691 my ($self, $myconfig, $form) = @_;
1693 # connect to database
1694 my $dbh = $form->dbconnect($myconfig);
1696 my $query = qq|SELECT
1700 round(t.rate * 100, 2) AS rate,
1701 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1702 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1706 my $sth = $dbh->prepare($query);
1707 $sth->execute || $form->dberror($query);
1710 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1711 push @{ $form->{TAX} }, $ref;
1717 $main::lxdebug->leave_sub();
1720 sub get_tax_accounts {
1721 $main::lxdebug->enter_sub();
1723 my ($self, $myconfig, $form) = @_;
1725 my $dbh = $form->dbconnect($myconfig);
1727 # get Accounts from chart
1728 my $query = qq{ SELECT
1730 accno || ' - ' || description AS taxaccount
1732 WHERE link LIKE '%_tax%'
1736 my $sth = $dbh->prepare($query);
1737 $sth->execute || $form->dberror($query);
1739 $form->{ACCOUNTS} = [];
1740 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1741 push @{ $form->{ACCOUNTS} }, $ref;
1748 $main::lxdebug->leave_sub();
1752 $main::lxdebug->enter_sub();
1754 my ($self, $myconfig, $form) = @_;
1756 # connect to database
1757 my $dbh = $form->dbconnect($myconfig);
1759 my $query = qq|SELECT
1762 round(rate * 100, 2) AS rate,
1767 my $sth = $dbh->prepare($query);
1768 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1770 my $ref = $sth->fetchrow_hashref("NAME_lc");
1772 map { $form->{$_} = $ref->{$_} } keys %$ref;
1776 # see if it is used by a taxkey
1777 $query = qq|SELECT count(*) FROM taxkeys
1778 WHERE tax_id = ? AND chart_id >0|;
1780 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1782 $form->{orphaned} = !$form->{orphaned};
1785 if (!$form->{orphaned} ) {
1786 $query = qq|SELECT DISTINCT c.id, c.accno
1788 JOIN tax t ON (t.id = tk.tax_id)
1789 JOIN chart c ON (c.id = tk.chart_id)
1790 WHERE tk.tax_id = ?|;
1792 $sth = $dbh->prepare($query);
1793 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1795 $form->{TAXINUSE} = [];
1796 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1797 push @{ $form->{TAXINUSE} }, $ref;
1805 $main::lxdebug->leave_sub();
1809 $main::lxdebug->enter_sub();
1811 my ($self, $myconfig, $form) = @_;
1814 # connect to database
1815 my $dbh = $form->get_standard_dbh($myconfig);
1817 $form->{rate} = $form->{rate} / 100;
1819 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1820 if ($form->{id} ne "") {
1821 $query = qq|UPDATE tax SET
1826 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1828 push(@values, $form->{id});
1832 $query = qq|INSERT INTO tax (
1839 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1841 do_query($form, $dbh, $query, @values);
1845 $main::lxdebug->leave_sub();
1849 $main::lxdebug->enter_sub();
1851 my ($self, $myconfig, $form) = @_;
1854 # connect to database
1855 my $dbh = $form->get_standard_dbh($myconfig);
1857 $query = qq|DELETE FROM tax
1859 do_query($form, $dbh, $query, $form->{id});
1863 $main::lxdebug->leave_sub();
1866 sub save_price_factor {
1867 $main::lxdebug->enter_sub();
1869 my ($self, $myconfig, $form) = @_;
1871 # connect to database
1872 my $dbh = $form->get_standard_dbh($myconfig);
1875 my @values = ($form->{description}, conv_i($form->{factor}));
1878 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1879 push @values, conv_i($form->{id});
1882 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1885 do_query($form, $dbh, $query, @values);
1889 $main::lxdebug->leave_sub();
1892 sub get_all_price_factors {
1893 $main::lxdebug->enter_sub();
1895 my ($self, $myconfig, $form) = @_;
1897 # connect to database
1898 my $dbh = $form->get_standard_dbh($myconfig);
1900 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1902 $main::lxdebug->leave_sub();
1905 sub get_price_factor {
1906 $main::lxdebug->enter_sub();
1908 my ($self, $myconfig, $form) = @_;
1910 # connect to database
1911 my $dbh = $form->get_standard_dbh($myconfig);
1913 my $query = qq|SELECT description, factor,
1914 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1915 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1916 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1917 FROM price_factors WHERE id = ?|;
1919 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1921 $main::lxdebug->leave_sub();
1924 sub delete_price_factor {
1925 $main::lxdebug->enter_sub();
1927 my ($self, $myconfig, $form) = @_;
1929 # connect to database
1930 my $dbh = $form->get_standard_dbh($myconfig);
1932 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1935 $main::lxdebug->leave_sub();
1938 sub save_warehouse {
1939 $main::lxdebug->enter_sub();
1941 my ($self, $myconfig, $form) = @_;
1943 # connect to database
1944 my $dbh = $form->get_standard_dbh($myconfig);
1946 my ($query, @values, $sth);
1949 $query = qq|SELECT nextval('id')|;
1950 ($form->{id}) = selectrow_query($form, $dbh, $query);
1952 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1953 do_query($form, $dbh, $query, $form->{id});
1956 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1957 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1959 if (0 < $form->{number_of_new_bins}) {
1960 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1961 $sth = prepare_query($form, $dbh, $query);
1963 foreach my $i (1..$form->{number_of_new_bins}) {
1964 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1972 $main::lxdebug->leave_sub();
1976 $main::lxdebug->enter_sub();
1978 my ($self, $myconfig, $form) = @_;
1980 # connect to database
1981 my $dbh = $form->get_standard_dbh($myconfig);
1983 my ($query, @values, $commit_necessary, $sth);
1985 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1988 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1989 do_query($form, $dbh, $query, @values);
1991 $commit_necessary = 1;
1994 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1995 $sth = prepare_query($form, $dbh, $query);
1997 foreach my $row (1..$form->{rowcount}) {
1998 next if ($form->{"delete_${row}"});
2000 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2002 $commit_necessary = 1;
2007 $dbh->commit() if ($commit_necessary);
2009 $main::lxdebug->leave_sub();
2012 sub delete_warehouse {
2013 $main::lxdebug->enter_sub();
2015 my ($self, $myconfig, $form) = @_;
2017 # connect to database
2018 my $dbh = $form->get_standard_dbh($myconfig);
2020 my $id = conv_i($form->{id});
2021 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|;
2022 my ($count) = selectrow_query($form, $dbh, $query, $id);
2025 $main::lxdebug->leave_sub();
2029 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2030 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2034 $main::lxdebug->leave_sub();
2039 sub get_all_warehouses {
2040 $main::lxdebug->enter_sub();
2042 my ($self, $myconfig, $form) = @_;
2044 # connect to database
2045 my $dbh = $form->get_standard_dbh($myconfig);
2047 my $query = qq|SELECT w.id, w.description, w.invalid,
2048 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2050 ORDER BY w.sortkey|;
2052 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2054 $main::lxdebug->leave_sub();
2058 $main::lxdebug->enter_sub();
2060 my ($self, $myconfig, $form) = @_;
2062 # connect to database
2063 my $dbh = $form->get_standard_dbh($myconfig);
2065 my $id = conv_i($form->{id});
2066 my $query = qq|SELECT w.description, w.invalid
2070 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2072 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2074 $query = qq|SELECT b.*, EXISTS
2075 (SELECT i.warehouse_id
2077 WHERE i.bin_id = b.id
2081 WHERE b.warehouse_id = ?|;
2083 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2085 $main::lxdebug->leave_sub();