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") {
251 my @values = ($form->{accno});
254 $query .= ' AND NOT id = ?';
255 push(@values, $form->{id});
258 my ($accno) = selectrow_query($form, $dbh, $query, @values);
261 $form->error($::locale->text('Account number not unique!'));
265 if (!$form->{id} || $form->{id} eq "") {
266 $query = qq|SELECT nextval('id')|;
267 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
268 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
269 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
277 # if charttype is heading make sure certain values are empty
278 # specifically, if charttype is changed from an existing account, empty the
279 # fields unnecessary for headings, so that e.g. heading doesn't appear in
280 # drop-down menues due to still having a valid "link" entry
282 if ( $form->{charttype} eq 'H' ) {
284 $form->{pos_bwa} = '';
285 $form->{pos_bilanz} = '';
286 $form->{pos_eur} = '';
287 $form->{new_chart_id} = '';
288 $form->{valid_from} = '';
291 $query = qq|UPDATE chart SET
307 $form->{description},
311 conv_i($form->{pos_bwa}),
312 conv_i($form->{pos_bilanz}),
313 conv_i($form->{pos_eur}),
314 conv_i($form->{new_chart_id}),
315 conv_date($form->{valid_from}),
316 ($form->{datevautomatik} eq 'T') ? 'true':'false',
323 do_query($form, $dbh, $query, @values);
329 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
333 for $tk_count (0 .. $MAX_TRIES) {
337 # Check if the account already exists, else cancel
339 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
341 last READTAXKEYS if ( $form->{'id'} == 0);
343 # check if there is a startdate
344 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
349 # Add valid taxkeys into the array
352 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
353 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
354 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
355 chart_id => conv_i($form->{"id"}),
356 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
357 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
364 for my $j (0 .. $#taxkeys){
365 if ( defined $taxkeys[$j]{'id'} ){
368 if ($taxkeys[$j]{'delete'}){
370 DELETE FROM taxkeys WHERE id = ?
373 @values = ($taxkeys[$j]{'id'});
375 do_query($form, $dbh, $query, @values);
384 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
392 $taxkeys[$j]{'tax_id'},
393 $taxkeys[$j]{'chart_id'},
394 $taxkeys[$j]{'tax_id'},
395 $taxkeys[$j]{'pos_ustva'},
396 $taxkeys[$j]{'startdate'},
399 do_query($form, $dbh, $query, @values);
405 INSERT INTO taxkeys (
412 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
415 $taxkeys[$j]{'tax_id'},
416 $taxkeys[$j]{'chart_id'},
417 $taxkeys[$j]{'tax_id'},
418 $taxkeys[$j]{'pos_ustva'},
419 $taxkeys[$j]{'startdate'},
422 do_query($form, $dbh, $query, @values);
427 # Update chart.taxkey_id to the latest from taxkeys for this chart.
433 WHERE taxkeys.chart_id = chart.id
434 ORDER BY startdate DESC
440 do_query($form, $dbh, $query, $form->{id});
443 my $rc = $dbh->commit;
446 $main::lxdebug->leave_sub();
452 $main::lxdebug->enter_sub();
454 my ($self, $myconfig, $form) = @_;
456 # connect to database, turn off AutoCommit
457 my $dbh = $form->dbconnect_noauto($myconfig);
459 my $query = qq|SELECT count(*) FROM acc_trans a
460 WHERE a.chart_id = ?|;
461 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
465 $main::lxdebug->leave_sub();
469 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
470 foreach my $type (qw(inventory income expense)) {
473 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
474 qq|WHERE ${type}_accno_id = ?|;
475 do_query($form, $dbh, $query, $form->{id});
478 $query = qq|DELETE FROM tax
480 do_query($form, $dbh, $query, $form->{id});
482 # delete chart of account record
483 $query = qq|DELETE FROM chart
485 do_query($form, $dbh, $query, $form->{id});
487 # delete account taxkeys
488 $query = qq|DELETE FROM taxkeys
490 do_query($form, $dbh, $query, $form->{id});
492 # commit and redirect
493 my $rc = $dbh->commit;
496 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
506 # connect to database
507 my $dbh = $form->dbconnect($myconfig);
509 my $query = qq|SELECT id, lead
513 my $sth = $dbh->prepare($query);
514 $sth->execute || $form->dberror($query);
516 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
517 push @{ $form->{ALL} }, $ref;
523 $main::lxdebug->leave_sub();
527 $main::lxdebug->enter_sub();
529 my ($self, $myconfig, $form) = @_;
531 # connect to database
532 my $dbh = $form->dbconnect($myconfig);
535 qq|SELECT l.id, l.lead | .
538 my $sth = $dbh->prepare($query);
539 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
541 my $ref = $sth->fetchrow_hashref("NAME_lc");
543 map { $form->{$_} = $ref->{$_} } keys %$ref;
549 $main::lxdebug->leave_sub();
553 $main::lxdebug->enter_sub();
555 my ($self, $myconfig, $form) = @_;
558 # connect to database
559 my $dbh = $form->dbconnect($myconfig);
561 my @values = ($form->{description});
562 # id is the old record
564 $query = qq|UPDATE leads SET
567 push(@values, $form->{id});
569 $query = qq|INSERT INTO leads
573 do_query($form, $dbh, $query, @values);
577 $main::lxdebug->leave_sub();
581 $main::lxdebug->enter_sub();
583 my ($self, $myconfig, $form) = @_;
586 # connect to database
587 my $dbh = $form->dbconnect($myconfig);
589 $query = qq|DELETE FROM leads
591 do_query($form, $dbh, $query, $form->{id});
595 $main::lxdebug->leave_sub();
599 $main::lxdebug->enter_sub();
601 my ($self, $myconfig, $form, $return_list) = @_;
603 # connect to database
604 my $dbh = $form->dbconnect($myconfig);
607 "SELECT id, description, template_code, article_code, " .
608 " output_numberformat, output_dateformat, output_longdates " .
609 "FROM language ORDER BY description";
611 my $sth = $dbh->prepare($query);
612 $sth->execute || $form->dberror($query);
616 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
617 push(@{ $ary }, $ref);
623 $main::lxdebug->leave_sub();
633 $main::lxdebug->enter_sub();
635 my ($self, $myconfig, $form) = @_;
637 # connect to database
638 my $dbh = $form->dbconnect($myconfig);
641 "SELECT description, template_code, article_code, " .
642 " output_numberformat, output_dateformat, output_longdates " .
643 "FROM language WHERE id = ?";
644 my $sth = $dbh->prepare($query);
645 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
647 my $ref = $sth->fetchrow_hashref("NAME_lc");
649 map { $form->{$_} = $ref->{$_} } keys %$ref;
655 $main::lxdebug->leave_sub();
658 sub get_language_details {
659 $main::lxdebug->enter_sub();
661 my ($self, $myconfig, $form, $id) = @_;
663 # connect to database
664 my $dbh = $form->dbconnect($myconfig);
667 "SELECT template_code, " .
668 " output_numberformat, output_dateformat, output_longdates " .
669 "FROM language WHERE id = ?";
670 my @res = selectrow_query($form, $dbh, $query, $id);
673 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ($self, $myconfig, $form) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
685 my (@values, $query);
687 map({ push(@values, $form->{$_}); }
688 qw(description template_code article_code
689 output_numberformat output_dateformat output_longdates));
691 # id is the old record
694 "UPDATE language SET " .
695 " description = ?, template_code = ?, article_code = ?, " .
696 " output_numberformat = ?, output_dateformat = ?, " .
697 " output_longdates = ? " .
699 push(@values, $form->{id});
702 "INSERT INTO language (" .
703 " description, template_code, article_code, " .
704 " output_numberformat, output_dateformat, output_longdates" .
705 ") VALUES (?, ?, ?, ?, ?, ?)";
707 do_query($form, $dbh, $query, @values);
711 $main::lxdebug->leave_sub();
714 sub delete_language {
715 $main::lxdebug->enter_sub();
717 my ($self, $myconfig, $form) = @_;
720 # connect to database
721 my $dbh = $form->dbconnect_noauto($myconfig);
723 foreach my $table (qw(generic_translations units_language)) {
724 $query = qq|DELETE FROM $table WHERE language_id = ?|;
725 do_query($form, $dbh, $query, $form->{"id"});
728 $query = "DELETE FROM language WHERE id = ?";
729 do_query($form, $dbh, $query, $form->{"id"});
734 $main::lxdebug->leave_sub();
739 $main::lxdebug->enter_sub();
741 my ($self, $myconfig, $form) = @_;
743 # connect to database
744 my $dbh = $form->dbconnect($myconfig);
746 my $query = qq|SELECT id, description,
748 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
750 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
752 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
754 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
756 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
758 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
760 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
762 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
764 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
768 my $sth = $dbh->prepare($query);
769 $sth->execute || $form->dberror($query);
772 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
773 push @{ $form->{ALL} }, $ref;
779 $main::lxdebug->leave_sub();
782 sub get_buchungsgruppe {
783 $main::lxdebug->enter_sub();
785 my ($self, $myconfig, $form) = @_;
788 # connect to database
789 my $dbh = $form->dbconnect($myconfig);
793 qq|SELECT description, inventory_accno_id,
794 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
796 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
798 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
800 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
802 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
804 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
806 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
808 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
810 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
813 my $sth = $dbh->prepare($query);
814 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
816 my $ref = $sth->fetchrow_hashref("NAME_lc");
818 map { $form->{$_} = $ref->{$_} } keys %$ref;
823 qq|SELECT count(id) = 0 AS orphaned
825 WHERE buchungsgruppen_id = ?|;
826 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
829 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
831 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
832 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
835 $query = qq|SELECT c.accno, c.description, c.link, c.id,
836 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
837 FROM chart c, defaults d
838 WHERE c.link LIKE '%$module%'
842 my $sth = $dbh->prepare($query);
843 $sth->execute || $form->dberror($query);
844 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
845 foreach my $key (split(/:/, $ref->{link})) {
846 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
847 $form->{"std_inventory_accno_id"} = $ref->{"id"};
849 if ($key =~ /$module/) {
850 if ( ($ref->{id} eq $ref->{inventory_accno_id})
851 || ($ref->{id} eq $ref->{income_accno_id})
852 || ($ref->{id} eq $ref->{expense_accno_id})) {
853 push @{ $form->{"${module}_links"}{$key} },
854 { accno => $ref->{accno},
855 description => $ref->{description},
856 selected => "selected",
859 push @{ $form->{"${module}_links"}{$key} },
860 { accno => $ref->{accno},
861 description => $ref->{description},
873 $main::lxdebug->leave_sub();
876 sub save_buchungsgruppe {
877 $main::lxdebug->enter_sub();
879 my ($self, $myconfig, $form) = @_;
881 # connect to database
882 my $dbh = $form->dbconnect($myconfig);
884 my @values = ($form->{description}, $form->{inventory_accno_id},
885 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
886 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
887 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
888 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
892 # id is the old record
894 $query = qq|UPDATE buchungsgruppen SET
895 description = ?, inventory_accno_id = ?,
896 income_accno_id_0 = ?, expense_accno_id_0 = ?,
897 income_accno_id_1 = ?, expense_accno_id_1 = ?,
898 income_accno_id_2 = ?, expense_accno_id_2 = ?,
899 income_accno_id_3 = ?, expense_accno_id_3 = ?
901 push(@values, $form->{id});
903 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
904 my ($sortkey) = $dbh->selectrow_array($query);
905 $form->dberror($query) if ($dbh->err);
906 push(@values, $sortkey);
907 $query = qq|INSERT INTO buchungsgruppen
908 (description, inventory_accno_id,
909 income_accno_id_0, expense_accno_id_0,
910 income_accno_id_1, expense_accno_id_1,
911 income_accno_id_2, expense_accno_id_2,
912 income_accno_id_3, expense_accno_id_3,
914 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
916 do_query($form, $dbh, $query, @values);
920 $main::lxdebug->leave_sub();
923 sub delete_buchungsgruppe {
924 $main::lxdebug->enter_sub();
926 my ($self, $myconfig, $form) = @_;
928 # connect to database
929 my $dbh = $form->dbconnect($myconfig);
931 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
932 do_query($form, $dbh, $query, $form->{id});
936 $main::lxdebug->leave_sub();
940 $main::lxdebug->enter_sub();
942 my ($self, $myconfig, $form, $table) = @_;
944 # connect to database
945 my $dbh = $form->get_standard_dbh($myconfig);
949 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
950 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
951 my @values = ($form->{"id1"}, $form->{"id2"});
952 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
954 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
955 my $sth = prepare_query($form, $dbh, $query);
957 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
958 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
964 $main::lxdebug->leave_sub();
967 sub prepare_template_filename {
968 $main::lxdebug->enter_sub();
970 my ($self, $myconfig, $form) = @_;
972 my ($filename, $display_filename);
974 if ($form->{type} eq "stylesheet") {
975 $filename = "css/$myconfig->{stylesheet}";
976 $display_filename = $myconfig->{stylesheet};
979 $filename = $form->{formname};
981 if ($form->{language}) {
982 my ($id, $template_code) = split(/--/, $form->{language});
983 $filename .= "_${template_code}";
986 if ($form->{printer}) {
987 my ($id, $template_code) = split(/--/, $form->{printer});
988 $filename .= "_${template_code}";
991 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
992 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
993 $filename =~ s|.*/||;
995 $display_filename = $filename;
996 $filename = "$myconfig->{templates}/$filename";
999 $main::lxdebug->leave_sub();
1001 return ($filename, $display_filename);
1006 $main::lxdebug->enter_sub();
1008 my ($self, $filename) = @_;
1010 my ($content, $lines) = ("", 0);
1014 if (open(TEMPLATE, $filename)) {
1015 while (<TEMPLATE>) {
1022 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1024 $main::lxdebug->leave_sub();
1026 return ($content, $lines);
1030 $main::lxdebug->enter_sub();
1032 my ($self, $filename, $content) = @_;
1038 if (open(TEMPLATE, ">", $filename)) {
1039 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1040 $content =~ s/\r\n/\n/g;
1041 print(TEMPLATE $content);
1047 $main::lxdebug->leave_sub();
1053 $main::lxdebug->enter_sub();
1058 my $myconfig = \%main::myconfig;
1059 my $form = $main::form;
1061 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1064 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1066 # these defaults are database wide
1069 qq|UPDATE defaults SET
1070 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1074 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1075 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1091 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1092 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1093 $form->{invnumber}, $form->{cnnumber},
1094 $form->{sonumber}, $form->{ponumber},
1095 $form->{sqnumber}, $form->{rfqnumber},
1096 $form->{customernumber}, $form->{vendornumber},
1097 $form->{articlenumber}, $form->{servicenumber},
1098 $form->{sdonumber}, $form->{pdonumber},
1099 $form->{businessnumber}, $form->{weightunit},
1100 conv_i($form->{language_id}));
1101 do_query($form, $dbh, $query, @values);
1103 $main::lxdebug->message(0, "es gibt rowcount: " . $form->{rowcount});
1105 for my $i (1..$form->{rowcount}) {
1106 if ($form->{"curr_$i"} ne $form->{"old_curr_$i"}) {
1107 $query = qq|UPDATE currencies SET curr = '| . $form->{"curr_$i"} . qq|' WHERE curr = '| . $form->{"old_curr_$i"} . qq|'|;
1108 do_query($form, $dbh, $query);
1112 if (length($form->{new_curr}) > 0) {
1113 $query = qq|INSERT INTO currencies (curr) VALUES ('| . $form->{new_curr} . qq|')|;
1114 do_query($form, $dbh, $query);
1119 $main::lxdebug->leave_sub();
1123 sub save_preferences {
1124 $main::lxdebug->enter_sub();
1126 my ($self, $myconfig, $form) = @_;
1128 my $dbh = $form->get_standard_dbh($myconfig);
1130 my ($businessnumber) = selectrow_query($form, $dbh, qq|SELECT businessnumber FROM defaults|);
1133 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1134 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1136 my $rc = $dbh->commit();
1138 $form->{businessnumber} = $businessnumber;
1140 $myconfig = User->new(login => $form->{login});
1142 foreach my $item (keys %$form) {
1143 $myconfig->{$item} = $form->{$item};
1146 $myconfig->save_member;
1148 my $auth = $main::auth;
1150 $main::lxdebug->leave_sub();
1156 $main::lxdebug->enter_sub();
1161 my $myconfig = \%main::myconfig;
1162 my $form = $main::form;
1164 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1166 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1168 $defaults->{weightunit} ||= 'kg';
1170 $main::lxdebug->leave_sub();
1175 sub defaultaccounts {
1176 $main::lxdebug->enter_sub();
1178 my ($self, $myconfig, $form) = @_;
1180 # connect to database
1181 my $dbh = $form->dbconnect($myconfig);
1183 # get defaults from defaults table
1184 my $query = qq|SELECT * FROM defaults|;
1185 my $sth = $dbh->prepare($query);
1186 $sth->execute || $form->dberror($query);
1188 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1189 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1190 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1191 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1192 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1193 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1194 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1196 $form->{defaults}{weightunit} ||= 'kg';
1200 $query = qq|SELECT c.id, c.accno, c.description, c.link
1202 WHERE c.link LIKE '%IC%'
1204 $sth = $dbh->prepare($query);
1205 $sth->execute || $self->dberror($query);
1207 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1208 foreach my $key (split(/:/, $ref->{link})) {
1211 if ($key =~ /cogs/) {
1212 $nkey = "IC_expense";
1214 if ($key =~ /sale/) {
1215 $nkey = "IC_income";
1217 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1219 description => $ref->{description}
1226 $query = qq|SELECT c.id, c.accno, c.description
1228 WHERE c.category = 'I'
1229 AND c.charttype = 'A'
1231 $sth = $dbh->prepare($query);
1232 $sth->execute || $self->dberror($query);
1234 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1235 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1237 description => $ref->{description}
1242 $query = qq|SELECT c.id, c.accno, c.description
1244 WHERE c.category = 'E'
1245 AND c.charttype = 'A'
1247 $sth = $dbh->prepare($query);
1248 $sth->execute || $self->dberror($query);
1250 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1251 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1253 description => $ref->{description}
1258 # now get the tax rates and numbers
1259 $query = qq|SELECT c.id, c.accno, c.description,
1260 t.rate * 100 AS rate, t.taxnumber
1262 WHERE c.id = t.chart_id|;
1264 $sth = $dbh->prepare($query);
1265 $sth->execute || $form->dberror($query);
1267 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1268 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1269 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1270 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1271 if $ref->{taxnumber};
1272 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1274 # Abfrage für Standard Umlaufvermögenskonto
1276 qq|SELECT id, accno, description, link | .
1278 qq|WHERE link LIKE ? |.
1280 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1281 $sth->execute || $form->dberror($query);#
1282 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1283 foreach my $item (split(/:/, $ref->{link})) {
1284 if ($item eq "AR_paid") {
1285 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1287 description => $ref->{description}
1296 $query = qq|SELECT curr FROM currencies ORDER BY id|;
1298 $form->{CURRENCIES} = [];
1300 $sth = prepare_execute_query($form, $dbh, $query);
1301 $sth->execute || $form->dberror($query);
1302 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1303 push @{ $form->{ CURRENCIES } } , $ref;
1307 #Which of them is the default currency?
1308 $query = qq|SELECT curr AS defaultcurrency FROM currencies WHERE id = (SELECT curr FROM defaults LIMIT 1);|;
1309 $sth = $dbh->prepare($query);
1310 $sth->execute || $form->dberror($query);
1312 $form->{defaultcurrency} = ($sth->fetchrow_hashref("NAME_lc"))->{defaultcurrency};
1318 $main::lxdebug->leave_sub();
1322 $main::lxdebug->enter_sub();
1324 my ($self, $myconfig, $form) = @_;
1326 my $dbh = $form->dbconnect($myconfig);
1328 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1329 my $sth = $dbh->prepare($query);
1330 $sth->execute || $form->dberror($query);
1332 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1338 $main::lxdebug->leave_sub();
1342 $main::lxdebug->enter_sub();
1344 my ($self, $myconfig, $form) = @_;
1346 my $dbh = $form->dbconnect($myconfig);
1348 my ($query, @values);
1350 if ($form->{revtrans}) {
1351 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1353 } elsif ($form->{closedto}) {
1354 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1355 @values = (conv_date($form->{closedto}));
1358 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1361 # set close in defaults
1362 do_query($form, $dbh, $query, @values);
1366 $main::lxdebug->leave_sub();
1370 my ($self, $units, $unit_name, $factor) = @_;
1372 $factor = 1 unless ($factor);
1374 my $unit = $units->{$unit_name};
1376 if (!defined($unit) || !$unit->{"base_unit"} ||
1377 ($unit_name eq $unit->{"base_unit"})) {
1378 return ($unit_name, $factor);
1381 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1384 sub retrieve_units {
1385 $main::lxdebug->enter_sub();
1387 my ($self, $myconfig, $form, $prefix) = @_;
1390 my $dbh = $form->get_standard_dbh;
1392 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1394 my $sth = prepare_execute_query($form, $dbh, $query);
1397 while (my $ref = $sth->fetchrow_hashref()) {
1398 $units->{$ref->{"name"}} = $ref;
1402 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1403 $sth = $dbh->prepare($query_lang);
1404 $sth->execute() || $form->dberror($query_lang);
1406 while (my $ref = $sth->fetchrow_hashref()) {
1407 push(@languages, $ref);
1411 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1412 "FROM units_language ul " .
1413 "LEFT JOIN language l ON ul.language_id = l.id " .
1414 "WHERE ul.unit = ?";
1415 $sth = $dbh->prepare($query_lang);
1417 foreach my $unit (values(%{$units})) {
1418 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1420 $unit->{"LANGUAGES"} = {};
1421 foreach my $lang (@languages) {
1422 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1425 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1426 while (my $ref = $sth->fetchrow_hashref()) {
1427 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1432 $main::lxdebug->leave_sub();
1437 sub retrieve_all_units {
1438 $main::lxdebug->enter_sub();
1442 if (!$::request->{cache}{all_units}) {
1443 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1446 $main::lxdebug->leave_sub();
1448 return $::request->{cache}{all_units};
1452 sub translate_units {
1453 $main::lxdebug->enter_sub();
1455 my ($self, $form, $template_code, $unit, $amount) = @_;
1457 my $units = $self->retrieve_units(\%main::myconfig, $form);
1459 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1460 my $new_unit = $unit;
1462 if (($amount != 1) && $h->{"localized_plural"}) {
1463 $new_unit = $h->{"localized_plural"};
1464 } elsif ($h->{"localized"}) {
1465 $new_unit = $h->{"localized"};
1469 $main::lxdebug->leave_sub();
1475 $main::lxdebug->enter_sub();
1477 my ($self, $myconfig, $form, $units) = @_;
1479 my $dbh = $form->dbconnect($myconfig);
1481 map({ $_->{"in_use"} = 0; } values(%{$units}));
1483 foreach my $unit (values(%{$units})) {
1484 my $base_unit = $unit->{"original_base_unit"};
1485 while ($base_unit) {
1486 $units->{$base_unit}->{"in_use"} = 1;
1487 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1488 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1489 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1493 foreach my $unit (values(%{$units})) {
1494 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1496 foreach my $table (qw(parts invoice orderitems)) {
1497 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1499 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1500 $query .= "= " . $dbh->quote($unit->{"name"});
1502 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1503 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1506 my ($count) = $dbh->selectrow_array($query);
1507 $form->dberror($query) if ($dbh->err);
1510 $unit->{"in_use"} = 1;
1518 $main::lxdebug->leave_sub();
1521 sub convertible_units {
1522 $main::lxdebug->enter_sub();
1526 my $filter_unit = shift;
1527 my $not_smaller = shift;
1529 my $conv_units = [];
1531 $filter_unit = $units->{$filter_unit};
1533 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1534 my $unit = $units->{$name};
1536 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1537 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1538 push @{$conv_units}, $unit;
1542 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1544 $main::lxdebug->leave_sub();
1549 # if $a is translatable to $b, return the factor between them.
1552 $main::lxdebug->enter_sub(2);
1553 my ($this, $a, $b, $all_units) = @_;
1556 $all_units = $this->retrieve_all_units;
1559 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1560 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1561 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1562 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1565 sub unit_select_data {
1566 $main::lxdebug->enter_sub();
1568 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1573 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1576 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1577 if (!$convertible_into ||
1578 ($units->{$convertible_into} &&
1579 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1580 push @{$select}, { "name" => $unit,
1581 "base_unit" => $units->{$unit}->{"base_unit"},
1582 "factor" => $units->{$unit}->{"factor"},
1583 "selected" => ($unit eq $selected) ? "selected" : "" };
1587 $main::lxdebug->leave_sub();
1592 sub unit_select_html {
1593 $main::lxdebug->enter_sub();
1595 my ($self, $units, $name, $selected, $convertible_into) = @_;
1597 my $select = "<select name=${name}>";
1599 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1600 if (!$convertible_into ||
1601 ($units->{$convertible_into} &&
1602 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1603 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1606 $select .= "</select>";
1608 $main::lxdebug->leave_sub();
1614 $main::lxdebug->enter_sub();
1618 my $units = $self->retrieve_all_units();
1623 while (2 <= scalar(@_)) {
1624 my $qty = shift(@_);
1625 my $unit = $units->{shift(@_)};
1627 croak "No unit defined with name $unit" if (!defined $unit);
1630 $base_unit = $unit->{base_unit};
1631 } elsif ($base_unit ne $unit->{base_unit}) {
1632 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1635 $sum += $qty * $unit->{factor};
1638 $main::lxdebug->leave_sub();
1640 return wantarray ? ($sum, $base_unit) : $sum;
1644 $main::lxdebug->enter_sub();
1646 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1648 my $dbh = $form->dbconnect_noauto($myconfig);
1650 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1651 my ($sortkey) = selectrow_query($form, $dbh, $query);
1653 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1654 "VALUES (?, ?, ?, ?)";
1655 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1658 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1659 my $sth = $dbh->prepare($query);
1660 foreach my $lang (@{$languages}) {
1661 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1662 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1670 $main::lxdebug->leave_sub();
1674 $main::lxdebug->enter_sub();
1676 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1678 my $dbh = $form->dbconnect_noauto($myconfig);
1680 my ($base_unit, $unit, $sth, $query);
1682 $query = "DELETE FROM units_language";
1683 $dbh->do($query) || $form->dberror($query);
1685 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1686 $query = "DELETE FROM units WHERE name IN (";
1687 map({ $query .= "?," } @{$delete_units});
1688 substr($query, -1, 1) = ")";
1689 $dbh->do($query, undef, @{$delete_units}) ||
1690 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1693 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1694 $sth = $dbh->prepare($query);
1696 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1697 my $sth_lang = $dbh->prepare($query_lang);
1699 foreach $unit (values(%{$units})) {
1700 $unit->{"depth"} = 0;
1701 my $base_unit = $unit;
1702 while ($base_unit->{"base_unit"}) {
1704 $base_unit = $units->{$base_unit->{"base_unit"}};
1708 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1709 if ($unit->{"LANGUAGES"}) {
1710 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1711 next unless ($lang->{"id"} && $lang->{"localized"});
1712 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1713 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1717 next if ($unit->{"unchanged_unit"});
1719 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1720 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1724 $sth_lang->finish();
1728 $main::lxdebug->leave_sub();
1732 $main::lxdebug->enter_sub();
1734 my ($self, $myconfig, $form) = @_;
1736 # connect to database
1737 my $dbh = $form->dbconnect($myconfig);
1739 my $query = qq|SELECT
1743 round(t.rate * 100, 2) AS rate,
1744 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1745 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1749 my $sth = $dbh->prepare($query);
1750 $sth->execute || $form->dberror($query);
1753 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1754 push @{ $form->{TAX} }, $ref;
1760 $main::lxdebug->leave_sub();
1763 sub get_tax_accounts {
1764 $main::lxdebug->enter_sub();
1766 my ($self, $myconfig, $form) = @_;
1768 my $dbh = $form->dbconnect($myconfig);
1770 # get Accounts from chart
1771 my $query = qq{ SELECT
1773 accno || ' - ' || description AS taxaccount
1775 WHERE link LIKE '%_tax%'
1779 my $sth = $dbh->prepare($query);
1780 $sth->execute || $form->dberror($query);
1782 $form->{ACCOUNTS} = [];
1783 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1784 push @{ $form->{ACCOUNTS} }, $ref;
1791 $main::lxdebug->leave_sub();
1795 $main::lxdebug->enter_sub();
1797 my ($self, $myconfig, $form) = @_;
1799 # connect to database
1800 my $dbh = $form->dbconnect($myconfig);
1802 my $query = qq|SELECT
1805 round(rate * 100, 2) AS rate,
1807 (id IN (SELECT tax_id
1808 FROM acc_trans)) AS tax_already_used
1812 my $sth = $dbh->prepare($query);
1813 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1815 my $ref = $sth->fetchrow_hashref("NAME_lc");
1817 map { $form->{$_} = $ref->{$_} } keys %$ref;
1821 # see if it is used by a taxkey
1822 $query = qq|SELECT count(*) FROM taxkeys
1823 WHERE tax_id = ? AND chart_id >0|;
1825 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1827 $form->{orphaned} = !$form->{orphaned};
1830 if (!$form->{orphaned} ) {
1831 $query = qq|SELECT DISTINCT c.id, c.accno
1833 JOIN tax t ON (t.id = tk.tax_id)
1834 JOIN chart c ON (c.id = tk.chart_id)
1835 WHERE tk.tax_id = ?|;
1837 $sth = $dbh->prepare($query);
1838 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1840 $form->{TAXINUSE} = [];
1841 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1842 push @{ $form->{TAXINUSE} }, $ref;
1850 $main::lxdebug->leave_sub();
1854 $main::lxdebug->enter_sub();
1856 my ($self, $myconfig, $form) = @_;
1859 # connect to database
1860 my $dbh = $form->get_standard_dbh($myconfig);
1862 $form->{rate} = $form->{rate} / 100;
1864 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1865 if ($form->{id} ne "") {
1866 $query = qq|UPDATE tax SET
1871 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1873 push(@values, $form->{id});
1877 $query = qq|INSERT INTO tax (
1884 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1886 do_query($form, $dbh, $query, @values);
1890 $main::lxdebug->leave_sub();
1894 $main::lxdebug->enter_sub();
1896 my ($self, $myconfig, $form) = @_;
1899 # connect to database
1900 my $dbh = $form->get_standard_dbh($myconfig);
1902 $query = qq|DELETE FROM tax
1904 do_query($form, $dbh, $query, $form->{id});
1908 $main::lxdebug->leave_sub();
1911 sub save_price_factor {
1912 $main::lxdebug->enter_sub();
1914 my ($self, $myconfig, $form) = @_;
1916 # connect to database
1917 my $dbh = $form->get_standard_dbh($myconfig);
1920 my @values = ($form->{description}, conv_i($form->{factor}));
1923 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1924 push @values, conv_i($form->{id});
1927 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1930 do_query($form, $dbh, $query, @values);
1934 $main::lxdebug->leave_sub();
1937 sub get_all_price_factors {
1938 $main::lxdebug->enter_sub();
1940 my ($self, $myconfig, $form) = @_;
1942 # connect to database
1943 my $dbh = $form->get_standard_dbh($myconfig);
1945 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1947 $main::lxdebug->leave_sub();
1950 sub get_price_factor {
1951 $main::lxdebug->enter_sub();
1953 my ($self, $myconfig, $form) = @_;
1955 # connect to database
1956 my $dbh = $form->get_standard_dbh($myconfig);
1958 my $query = qq|SELECT description, factor,
1959 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1960 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1961 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1962 FROM price_factors WHERE id = ?|;
1964 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1966 $main::lxdebug->leave_sub();
1969 sub delete_price_factor {
1970 $main::lxdebug->enter_sub();
1972 my ($self, $myconfig, $form) = @_;
1974 # connect to database
1975 my $dbh = $form->get_standard_dbh($myconfig);
1977 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1980 $main::lxdebug->leave_sub();
1983 sub save_warehouse {
1984 $main::lxdebug->enter_sub();
1986 my ($self, $myconfig, $form) = @_;
1988 # connect to database
1989 my $dbh = $form->get_standard_dbh($myconfig);
1991 my ($query, @values, $sth);
1994 $query = qq|SELECT nextval('id')|;
1995 ($form->{id}) = selectrow_query($form, $dbh, $query);
1997 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1998 do_query($form, $dbh, $query, $form->{id});
2001 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2002 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2004 if (0 < $form->{number_of_new_bins}) {
2005 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2006 $sth = prepare_query($form, $dbh, $query);
2008 foreach my $i (1..$form->{number_of_new_bins}) {
2009 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2017 $main::lxdebug->leave_sub();
2021 $main::lxdebug->enter_sub();
2023 my ($self, $myconfig, $form) = @_;
2025 # connect to database
2026 my $dbh = $form->get_standard_dbh($myconfig);
2028 my ($query, @values, $commit_necessary, $sth);
2030 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2033 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2034 do_query($form, $dbh, $query, @values);
2036 $commit_necessary = 1;
2039 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2040 $sth = prepare_query($form, $dbh, $query);
2042 foreach my $row (1..$form->{rowcount}) {
2043 next if ($form->{"delete_${row}"});
2045 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2047 $commit_necessary = 1;
2052 $dbh->commit() if ($commit_necessary);
2054 $main::lxdebug->leave_sub();
2057 sub delete_warehouse {
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 i.bin_id FROM inventory i WHERE i.bin_id IN (SELECT b.id FROM bin b WHERE b.warehouse_id = ?) LIMIT 1|;
2067 my ($count) = selectrow_query($form, $dbh, $query, $id);
2070 $main::lxdebug->leave_sub();
2074 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2075 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2079 $main::lxdebug->leave_sub();
2084 sub get_all_warehouses {
2085 $main::lxdebug->enter_sub();
2087 my ($self, $myconfig, $form) = @_;
2089 # connect to database
2090 my $dbh = $form->get_standard_dbh($myconfig);
2092 my $query = qq|SELECT w.id, w.description, w.invalid,
2093 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2095 ORDER BY w.sortkey|;
2097 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2099 $main::lxdebug->leave_sub();
2103 $main::lxdebug->enter_sub();
2105 my ($self, $myconfig, $form) = @_;
2107 # connect to database
2108 my $dbh = $form->get_standard_dbh($myconfig);
2110 my $id = conv_i($form->{id});
2111 my $query = qq|SELECT w.description, w.invalid
2115 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2117 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2119 $query = qq|SELECT b.*, EXISTS
2120 (SELECT i.warehouse_id
2122 WHERE i.bin_id = b.id
2126 WHERE b.warehouse_id = ?|;
2128 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2130 $main::lxdebug->leave_sub();