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 #======================================================================
43 use List::MoreUtils qw(any);
49 $main::lxdebug->enter_sub();
51 my ($self, $myconfig, $form) = @_;
54 my $dbh = $form->dbconnect($myconfig);
56 SELECT c.accno, c.description, c.charttype, c.category,
57 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
58 c.pos_bwa, datevautomatik,
59 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
60 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
63 ON (c.id=tk.chart_id AND tk.id =
64 (SELECT id FROM taxkeys
65 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
66 ORDER BY startdate DESC LIMIT 1))
71 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
72 my $sth = $dbh->prepare($query);
73 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
75 my $ref = $sth->fetchrow_hashref("NAME_lc");
77 foreach my $key (keys %$ref) {
78 $form->{"$key"} = $ref->{"$key"};
83 # get default accounts
84 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
86 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
87 $sth = $dbh->prepare($query);
88 $sth->execute || $form->dberror($query);
90 $ref = $sth->fetchrow_hashref("NAME_lc");
92 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
98 # get taxkeys and description
102 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
104 id||'--'||taxkey AS tax,
107 FROM tax ORDER BY taxkey
109 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
110 $sth = $dbh->prepare($query);
111 $sth->execute || $form->dberror($query);
113 $form->{TAXKEY} = [];
115 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
116 push @{ $form->{TAXKEY} }, $ref;
122 $query = qq|SELECT id, accno,description
126 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
127 $sth = $dbh->prepare($query);
128 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
130 $form->{NEWACCOUNT} = [];
131 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
132 push @{ $form->{NEWACCOUNT} }, $ref;
137 # get the taxkeys of account
151 LEFT JOIN tax t ON (t.id = tk.tax_id)
152 LEFT JOIN chart c ON (c.id = t.chart_id)
154 WHERE tk.chart_id = ?
155 ORDER BY startdate DESC
157 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
158 $sth = $dbh->prepare($query);
160 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
162 $form->{ACCOUNT_TAXKEYS} = [];
164 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
165 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
171 # check if we have any transactions
172 $query = qq|SELECT a.trans_id FROM acc_trans a
173 WHERE a.chart_id = ?|;
174 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
175 $sth = $dbh->prepare($query);
176 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
178 ($form->{orphaned}) = $sth->fetchrow_array;
179 $form->{orphaned} = !$form->{orphaned};
182 # check if new account is active
183 $form->{new_chart_valid} = 0;
184 if ($form->{new_chart_id}) {
185 $query = qq|SELECT current_date-valid_from FROM chart
187 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
188 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
190 $form->{new_chart_valid} = 1;
197 $main::lxdebug->leave_sub();
201 $main::lxdebug->enter_sub();
203 # TODO: it should be forbidden to change an account to a heading if there
204 # have been bookings to this account in the past
206 my ($self, $myconfig, $form) = @_;
208 # connect to database, turn off AutoCommit
209 my $dbh = $form->dbconnect_noauto($myconfig);
211 for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
212 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
215 # sanity check, can't have AR with AR_...
216 if ($form->{AR} || $form->{AP} || $form->{IC}) {
217 if (any { $form->{$_} } 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)) {
218 $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
223 foreach my $item ($form->{AR}, $form->{AR_amount},
224 $form->{AR_tax}, $form->{AR_paid},
225 $form->{AP}, $form->{AP_amount},
226 $form->{AP_tax}, $form->{AP_paid},
227 $form->{IC}, $form->{IC_sale},
228 $form->{IC_cogs}, $form->{IC_taxpart},
229 $form->{IC_income}, $form->{IC_expense},
230 $form->{IC_taxservice}
232 $form->{link} .= "${item}:" if ($item);
236 # strip blanks from accno
237 map { $form->{$_} =~ s/ //g; } qw(accno);
241 if ($form->{id} eq "NULL") {
250 my @values = ($form->{accno});
253 $query .= ' AND NOT id = ?';
254 push(@values, $form->{id});
257 my ($accno) = selectrow_query($form, $dbh, $query, @values);
260 $form->error($::locale->text('Account number not unique!'));
264 if (!$form->{id} || $form->{id} eq "") {
265 $query = qq|SELECT nextval('id')|;
266 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
267 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
268 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
276 # if charttype is heading make sure certain values are empty
277 # specifically, if charttype is changed from an existing account, empty the
278 # fields unnecessary for headings, so that e.g. heading doesn't appear in
279 # drop-down menues due to still having a valid "link" entry
281 if ( $form->{charttype} eq 'H' ) {
283 $form->{pos_bwa} = '';
284 $form->{pos_bilanz} = '';
285 $form->{pos_eur} = '';
286 $form->{new_chart_id} = '';
287 $form->{valid_from} = '';
290 $query = qq|UPDATE chart SET
306 $form->{description},
310 conv_i($form->{pos_bwa}),
311 conv_i($form->{pos_bilanz}),
312 conv_i($form->{pos_eur}),
313 conv_i($form->{new_chart_id}),
314 conv_date($form->{valid_from}),
315 ($form->{datevautomatik} eq 'T') ? 'true':'false',
322 do_query($form, $dbh, $query, @values);
328 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
332 for $tk_count (0 .. $MAX_TRIES) {
336 # Check if the account already exists, else cancel
338 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
340 last READTAXKEYS if ( $form->{'id'} == 0);
342 # check if there is a startdate
343 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
348 # Add valid taxkeys into the array
351 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
352 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
353 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
354 chart_id => conv_i($form->{"id"}),
355 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
356 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
363 for my $j (0 .. $#taxkeys){
364 if ( defined $taxkeys[$j]{'id'} ){
367 if ($taxkeys[$j]{'delete'}){
369 DELETE FROM taxkeys WHERE id = ?
372 @values = ($taxkeys[$j]{'id'});
374 do_query($form, $dbh, $query, @values);
383 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
391 $taxkeys[$j]{'tax_id'},
392 $taxkeys[$j]{'chart_id'},
393 $taxkeys[$j]{'tax_id'},
394 $taxkeys[$j]{'pos_ustva'},
395 $taxkeys[$j]{'startdate'},
398 do_query($form, $dbh, $query, @values);
404 INSERT INTO taxkeys (
411 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
414 $taxkeys[$j]{'tax_id'},
415 $taxkeys[$j]{'chart_id'},
416 $taxkeys[$j]{'tax_id'},
417 $taxkeys[$j]{'pos_ustva'},
418 $taxkeys[$j]{'startdate'},
421 do_query($form, $dbh, $query, @values);
426 # Update chart.taxkey_id to the latest from taxkeys for this chart.
432 WHERE taxkeys.chart_id = chart.id
433 ORDER BY startdate DESC
439 do_query($form, $dbh, $query, $form->{id});
442 my $rc = $dbh->commit;
445 $main::lxdebug->leave_sub();
451 $main::lxdebug->enter_sub();
453 my ($self, $myconfig, $form) = @_;
455 # connect to database, turn off AutoCommit
456 my $dbh = $form->dbconnect_noauto($myconfig);
458 my $query = qq|SELECT count(*) FROM acc_trans a
459 WHERE a.chart_id = ?|;
460 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
464 $main::lxdebug->leave_sub();
468 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
469 foreach my $type (qw(inventory income expense)) {
472 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
473 qq|WHERE ${type}_accno_id = ?|;
474 do_query($form, $dbh, $query, $form->{id});
477 $query = qq|DELETE FROM tax
479 do_query($form, $dbh, $query, $form->{id});
481 # delete chart of account record
482 $query = qq|DELETE FROM chart
484 do_query($form, $dbh, $query, $form->{id});
486 # delete account taxkeys
487 $query = qq|DELETE FROM taxkeys
489 do_query($form, $dbh, $query, $form->{id});
491 # commit and redirect
492 my $rc = $dbh->commit;
495 $main::lxdebug->leave_sub();
501 $main::lxdebug->enter_sub();
503 my ($self, $myconfig, $form) = @_;
505 # connect to database
506 my $dbh = $form->dbconnect($myconfig);
508 my $query = qq|SELECT id, lead
512 my $sth = $dbh->prepare($query);
513 $sth->execute || $form->dberror($query);
515 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
516 push @{ $form->{ALL} }, $ref;
522 $main::lxdebug->leave_sub();
526 $main::lxdebug->enter_sub();
528 my ($self, $myconfig, $form) = @_;
530 # connect to database
531 my $dbh = $form->dbconnect($myconfig);
534 qq|SELECT l.id, l.lead | .
537 my $sth = $dbh->prepare($query);
538 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
540 my $ref = $sth->fetchrow_hashref("NAME_lc");
542 map { $form->{$_} = $ref->{$_} } keys %$ref;
548 $main::lxdebug->leave_sub();
552 $main::lxdebug->enter_sub();
554 my ($self, $myconfig, $form) = @_;
557 # connect to database
558 my $dbh = $form->dbconnect($myconfig);
560 my @values = ($form->{description});
561 # id is the old record
563 $query = qq|UPDATE leads SET
566 push(@values, $form->{id});
568 $query = qq|INSERT INTO leads
572 do_query($form, $dbh, $query, @values);
576 $main::lxdebug->leave_sub();
580 $main::lxdebug->enter_sub();
582 my ($self, $myconfig, $form) = @_;
585 # connect to database
586 my $dbh = $form->dbconnect($myconfig);
588 $query = qq|DELETE FROM leads
590 do_query($form, $dbh, $query, $form->{id});
594 $main::lxdebug->leave_sub();
598 $main::lxdebug->enter_sub();
600 my ($self, $myconfig, $form, $return_list) = @_;
602 # connect to database
603 my $dbh = $form->dbconnect($myconfig);
606 "SELECT id, description, template_code, article_code, " .
607 " output_numberformat, output_dateformat, output_longdates " .
608 "FROM language ORDER BY description";
610 my $sth = $dbh->prepare($query);
611 $sth->execute || $form->dberror($query);
615 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
616 push(@{ $ary }, $ref);
622 $main::lxdebug->leave_sub();
632 $main::lxdebug->enter_sub();
634 my ($self, $myconfig, $form) = @_;
636 # connect to database
637 my $dbh = $form->dbconnect($myconfig);
640 "SELECT description, template_code, article_code, " .
641 " output_numberformat, output_dateformat, output_longdates " .
642 "FROM language WHERE id = ?";
643 my $sth = $dbh->prepare($query);
644 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
646 my $ref = $sth->fetchrow_hashref("NAME_lc");
648 map { $form->{$_} = $ref->{$_} } keys %$ref;
654 $main::lxdebug->leave_sub();
657 sub get_language_details {
658 $main::lxdebug->enter_sub();
660 my ($self, $myconfig, $form, $id) = @_;
662 # connect to database
663 my $dbh = $form->dbconnect($myconfig);
666 "SELECT template_code, " .
667 " output_numberformat, output_dateformat, output_longdates " .
668 "FROM language WHERE id = ?";
669 my @res = selectrow_query($form, $dbh, $query, $id);
672 $main::lxdebug->leave_sub();
678 $main::lxdebug->enter_sub();
680 my ($self, $myconfig, $form) = @_;
682 # connect to database
683 my $dbh = $form->dbconnect($myconfig);
684 my (@values, $query);
686 map({ push(@values, $form->{$_}); }
687 qw(description template_code article_code
688 output_numberformat output_dateformat output_longdates));
690 # id is the old record
693 "UPDATE language SET " .
694 " description = ?, template_code = ?, article_code = ?, " .
695 " output_numberformat = ?, output_dateformat = ?, " .
696 " output_longdates = ? " .
698 push(@values, $form->{id});
701 "INSERT INTO language (" .
702 " description, template_code, article_code, " .
703 " output_numberformat, output_dateformat, output_longdates" .
704 ") VALUES (?, ?, ?, ?, ?, ?)";
706 do_query($form, $dbh, $query, @values);
710 $main::lxdebug->leave_sub();
713 sub delete_language {
714 $main::lxdebug->enter_sub();
716 my ($self, $myconfig, $form) = @_;
719 # connect to database
720 my $dbh = $form->dbconnect_noauto($myconfig);
722 foreach my $table (qw(generic_translations units_language)) {
723 $query = qq|DELETE FROM $table WHERE language_id = ?|;
724 do_query($form, $dbh, $query, $form->{"id"});
727 $query = "DELETE FROM language WHERE id = ?";
728 do_query($form, $dbh, $query, $form->{"id"});
733 $main::lxdebug->leave_sub();
738 $main::lxdebug->enter_sub();
740 my ($self, $myconfig, $form) = @_;
742 # connect to database
743 my $dbh = $form->dbconnect($myconfig);
745 my $query = qq|SELECT id, description,
747 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
749 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
751 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
753 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
755 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
757 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
759 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
761 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
763 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
767 my $sth = $dbh->prepare($query);
768 $sth->execute || $form->dberror($query);
771 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
772 push @{ $form->{ALL} }, $ref;
778 $main::lxdebug->leave_sub();
781 sub get_buchungsgruppe {
782 $main::lxdebug->enter_sub();
784 my ($self, $myconfig, $form) = @_;
787 # connect to database
788 my $dbh = $form->dbconnect($myconfig);
792 qq|SELECT description, inventory_accno_id,
793 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
795 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
797 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
799 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
801 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
803 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
805 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
807 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
809 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
812 my $sth = $dbh->prepare($query);
813 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
815 my $ref = $sth->fetchrow_hashref("NAME_lc");
817 map { $form->{$_} = $ref->{$_} } keys %$ref;
822 qq|SELECT count(id) = 0 AS orphaned
824 WHERE buchungsgruppen_id = ?|;
825 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
828 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
830 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
831 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
834 $query = qq|SELECT c.accno, c.description, c.link, c.id,
835 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
836 FROM chart c, defaults d
837 WHERE c.link LIKE '%$module%'
841 my $sth = $dbh->prepare($query);
842 $sth->execute || $form->dberror($query);
843 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
844 foreach my $key (split(/:/, $ref->{link})) {
845 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
846 $form->{"std_inventory_accno_id"} = $ref->{"id"};
848 if ($key =~ /$module/) {
849 if ( ($ref->{id} eq $ref->{inventory_accno_id})
850 || ($ref->{id} eq $ref->{income_accno_id})
851 || ($ref->{id} eq $ref->{expense_accno_id})) {
852 push @{ $form->{"${module}_links"}{$key} },
853 { accno => $ref->{accno},
854 description => $ref->{description},
855 selected => "selected",
858 push @{ $form->{"${module}_links"}{$key} },
859 { accno => $ref->{accno},
860 description => $ref->{description},
872 $main::lxdebug->leave_sub();
875 sub save_buchungsgruppe {
876 $main::lxdebug->enter_sub();
878 my ($self, $myconfig, $form) = @_;
880 # connect to database
881 my $dbh = $form->dbconnect($myconfig);
883 my @values = ($form->{description}, $form->{inventory_accno_id},
884 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
885 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
886 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
887 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
891 # id is the old record
893 $query = qq|UPDATE buchungsgruppen SET
894 description = ?, inventory_accno_id = ?,
895 income_accno_id_0 = ?, expense_accno_id_0 = ?,
896 income_accno_id_1 = ?, expense_accno_id_1 = ?,
897 income_accno_id_2 = ?, expense_accno_id_2 = ?,
898 income_accno_id_3 = ?, expense_accno_id_3 = ?
900 push(@values, $form->{id});
902 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
903 my ($sortkey) = $dbh->selectrow_array($query);
904 $form->dberror($query) if ($dbh->err);
905 push(@values, $sortkey);
906 $query = qq|INSERT INTO buchungsgruppen
907 (description, inventory_accno_id,
908 income_accno_id_0, expense_accno_id_0,
909 income_accno_id_1, expense_accno_id_1,
910 income_accno_id_2, expense_accno_id_2,
911 income_accno_id_3, expense_accno_id_3,
913 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
915 do_query($form, $dbh, $query, @values);
919 $main::lxdebug->leave_sub();
922 sub delete_buchungsgruppe {
923 $main::lxdebug->enter_sub();
925 my ($self, $myconfig, $form) = @_;
927 # connect to database
928 my $dbh = $form->dbconnect($myconfig);
930 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
931 do_query($form, $dbh, $query, $form->{id});
935 $main::lxdebug->leave_sub();
939 $main::lxdebug->enter_sub();
941 my ($self, $myconfig, $form, $table) = @_;
943 # connect to database
944 my $dbh = $form->get_standard_dbh($myconfig);
948 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
949 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
950 my @values = ($form->{"id1"}, $form->{"id2"});
951 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
953 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
954 my $sth = prepare_query($form, $dbh, $query);
956 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
957 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
963 $main::lxdebug->leave_sub();
966 sub prepare_template_filename {
967 $main::lxdebug->enter_sub();
969 my ($self, $myconfig, $form) = @_;
971 my ($filename, $display_filename);
973 if ($form->{type} eq "stylesheet") {
974 $filename = "css/$myconfig->{stylesheet}";
975 $display_filename = $myconfig->{stylesheet};
978 $filename = $form->{formname};
980 if ($form->{language}) {
981 my ($id, $template_code) = split(/--/, $form->{language});
982 $filename .= "_${template_code}";
985 if ($form->{printer}) {
986 my ($id, $template_code) = split(/--/, $form->{printer});
987 $filename .= "_${template_code}";
990 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
991 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
992 $filename =~ s|.*/||;
994 $display_filename = $filename;
995 $filename = "$myconfig->{templates}/$filename";
998 $main::lxdebug->leave_sub();
1000 return ($filename, $display_filename);
1005 $main::lxdebug->enter_sub();
1007 my ($self, $filename) = @_;
1009 my ($content, $lines) = ("", 0);
1013 if (open(TEMPLATE, $filename)) {
1014 while (<TEMPLATE>) {
1021 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1023 $main::lxdebug->leave_sub();
1025 return ($content, $lines);
1029 $main::lxdebug->enter_sub();
1031 my ($self, $filename, $content) = @_;
1037 if (open(TEMPLATE, ">", $filename)) {
1038 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1039 $content =~ s/\r\n/\n/g;
1040 print(TEMPLATE $content);
1046 $main::lxdebug->leave_sub();
1052 $main::lxdebug->enter_sub();
1057 my $myconfig = \%main::myconfig;
1058 my $form = $main::form;
1060 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1063 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1065 # these defaults are database wide
1068 qq|UPDATE defaults SET
1069 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1070 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1074 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->{assemblynumber},
1099 $form->{sdonumber}, $form->{pdonumber},
1100 $form->{businessnumber}, $form->{weightunit},
1101 conv_i($form->{language_id}));
1102 do_query($form, $dbh, $query, @values);
1104 $main::lxdebug->message(0, "es gibt rowcount: " . $form->{rowcount});
1106 for my $i (1..$form->{rowcount}) {
1107 if ($form->{"curr_$i"} ne $form->{"old_curr_$i"}) {
1108 $query = qq|UPDATE currencies SET name = ? WHERE name = ?|;
1109 do_query($form, $dbh, $query, $form->{"curr_$i"}, $form->{"old_curr_$i"});
1113 if (length($form->{new_curr}) > 0) {
1114 $query = qq|INSERT INTO currencies (name) VALUES (?)|;
1115 do_query($form, $dbh, $query, $form->{new_curr});
1120 $main::lxdebug->leave_sub();
1124 sub save_preferences {
1125 $main::lxdebug->enter_sub();
1127 my ($self, $myconfig, $form) = @_;
1129 my $dbh = $form->get_standard_dbh($myconfig);
1131 my ($businessnumber) = selectrow_query($form, $dbh, qq|SELECT businessnumber FROM defaults|);
1134 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1135 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1137 my $rc = $dbh->commit();
1139 $form->{businessnumber} = $businessnumber;
1141 $myconfig = User->new(login => $form->{login});
1143 foreach my $item (keys %$form) {
1144 $myconfig->{$item} = $form->{$item};
1147 $myconfig->save_member;
1149 my $auth = $main::auth;
1151 $main::lxdebug->leave_sub();
1157 $main::lxdebug->enter_sub();
1162 my $myconfig = \%main::myconfig;
1163 my $form = $main::form;
1165 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1167 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1169 $defaults->{weightunit} ||= 'kg';
1171 $main::lxdebug->leave_sub();
1176 sub defaultaccounts {
1177 $main::lxdebug->enter_sub();
1179 my ($self, $myconfig, $form) = @_;
1181 # connect to database
1182 my $dbh = $form->dbconnect($myconfig);
1184 # get defaults from defaults table
1185 my $query = qq|SELECT * FROM defaults|;
1186 my $sth = $dbh->prepare($query);
1187 $sth->execute || $form->dberror($query);
1189 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1190 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1191 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1192 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1193 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1194 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1195 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1197 $form->{defaults}{weightunit} ||= 'kg';
1201 $query = qq|SELECT c.id, c.accno, c.description, c.link
1203 WHERE c.link LIKE '%IC%'
1205 $sth = $dbh->prepare($query);
1206 $sth->execute || $self->dberror($query);
1208 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1209 foreach my $key (split(/:/, $ref->{link})) {
1212 if ($key =~ /cogs/) {
1213 $nkey = "IC_expense";
1215 if ($key =~ /sale/) {
1216 $nkey = "IC_income";
1218 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1220 description => $ref->{description}
1227 $query = qq|SELECT c.id, c.accno, c.description
1229 WHERE c.category = 'I'
1230 AND c.charttype = 'A'
1232 $sth = $dbh->prepare($query);
1233 $sth->execute || $self->dberror($query);
1235 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1236 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1238 description => $ref->{description}
1243 $query = qq|SELECT c.id, c.accno, c.description
1245 WHERE c.category = 'E'
1246 AND c.charttype = 'A'
1248 $sth = $dbh->prepare($query);
1249 $sth->execute || $self->dberror($query);
1251 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1252 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1254 description => $ref->{description}
1259 # now get the tax rates and numbers
1260 $query = qq|SELECT c.id, c.accno, c.description,
1261 t.rate * 100 AS rate, t.taxnumber
1263 WHERE c.id = t.chart_id|;
1265 $sth = $dbh->prepare($query);
1266 $sth->execute || $form->dberror($query);
1268 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1269 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1270 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1271 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1272 if $ref->{taxnumber};
1273 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1275 # Abfrage für Standard Umlaufvermögenskonto
1277 qq|SELECT id, accno, description, link | .
1279 qq|WHERE link LIKE ? |.
1281 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1282 $sth->execute || $form->dberror($query);#
1283 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1284 foreach my $item (split(/:/, $ref->{link})) {
1285 if ($item eq "AR_paid") {
1286 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1288 description => $ref->{description}
1297 $query = qq|SELECT name AS curr FROM currencies ORDER BY id|;
1298 $form->{CURRENCIES} = selectall_hashref_query($form, $dbh, $query);
1300 #Which of them is the default currency?
1301 $query = qq|SELECT name AS defaultcurrency FROM currencies WHERE id = (SELECT currency_id FROM defaults LIMIT 1);|;
1302 ($form->{defaultcurrency}) = selectrow_query($form, $dbh, $query);
1306 $main::lxdebug->leave_sub();
1310 $main::lxdebug->enter_sub();
1312 my ($self, $myconfig, $form) = @_;
1314 my $dbh = $form->dbconnect($myconfig);
1316 my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
1317 my $sth = $dbh->prepare($query);
1318 $sth->execute || $form->dberror($query);
1320 ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
1326 $main::lxdebug->leave_sub();
1330 $main::lxdebug->enter_sub();
1332 my ($self, $myconfig, $form) = @_;
1334 my $dbh = $form->dbconnect($myconfig);
1336 my ($query, @values);
1338 if ($form->{revtrans}) {
1339 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1341 } elsif ($form->{closedto}) {
1342 $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
1343 @values = (conv_date($form->{closedto}), conv_date($form->{max_future_booking_interval}));
1346 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1349 # set close in defaults
1350 do_query($form, $dbh, $query, @values);
1354 $main::lxdebug->leave_sub();
1358 my ($self, $units, $unit_name, $factor) = @_;
1360 $factor = 1 unless ($factor);
1362 my $unit = $units->{$unit_name};
1364 if (!defined($unit) || !$unit->{"base_unit"} ||
1365 ($unit_name eq $unit->{"base_unit"})) {
1366 return ($unit_name, $factor);
1369 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1372 sub retrieve_units {
1373 $main::lxdebug->enter_sub();
1375 my ($self, $myconfig, $form, $prefix) = @_;
1378 my $dbh = $form->get_standard_dbh;
1380 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1382 my $sth = prepare_execute_query($form, $dbh, $query);
1385 while (my $ref = $sth->fetchrow_hashref()) {
1386 $units->{$ref->{"name"}} = $ref;
1390 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1391 $sth = $dbh->prepare($query_lang);
1392 $sth->execute() || $form->dberror($query_lang);
1394 while (my $ref = $sth->fetchrow_hashref()) {
1395 push(@languages, $ref);
1399 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1400 "FROM units_language ul " .
1401 "LEFT JOIN language l ON ul.language_id = l.id " .
1402 "WHERE ul.unit = ?";
1403 $sth = $dbh->prepare($query_lang);
1405 foreach my $unit (values(%{$units})) {
1406 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1408 $unit->{"LANGUAGES"} = {};
1409 foreach my $lang (@languages) {
1410 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1413 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1414 while (my $ref = $sth->fetchrow_hashref()) {
1415 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1420 $main::lxdebug->leave_sub();
1425 sub retrieve_all_units {
1426 $main::lxdebug->enter_sub();
1430 if (!$::request->{cache}{all_units}) {
1431 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1434 $main::lxdebug->leave_sub();
1436 return $::request->{cache}{all_units};
1440 sub translate_units {
1441 $main::lxdebug->enter_sub();
1443 my ($self, $form, $template_code, $unit, $amount) = @_;
1445 my $units = $self->retrieve_units(\%main::myconfig, $form);
1447 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1448 my $new_unit = $unit;
1450 if (($amount != 1) && $h->{"localized_plural"}) {
1451 $new_unit = $h->{"localized_plural"};
1452 } elsif ($h->{"localized"}) {
1453 $new_unit = $h->{"localized"};
1457 $main::lxdebug->leave_sub();
1463 $main::lxdebug->enter_sub();
1465 my ($self, $myconfig, $form, $units) = @_;
1467 my $dbh = $form->dbconnect($myconfig);
1469 map({ $_->{"in_use"} = 0; } values(%{$units}));
1471 foreach my $unit (values(%{$units})) {
1472 my $base_unit = $unit->{"original_base_unit"};
1473 while ($base_unit) {
1474 $units->{$base_unit}->{"in_use"} = 1;
1475 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1476 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1477 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1481 foreach my $unit (values(%{$units})) {
1482 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1484 foreach my $table (qw(parts invoice orderitems)) {
1485 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1487 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1488 $query .= "= " . $dbh->quote($unit->{"name"});
1490 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1491 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1494 my ($count) = $dbh->selectrow_array($query);
1495 $form->dberror($query) if ($dbh->err);
1498 $unit->{"in_use"} = 1;
1506 $main::lxdebug->leave_sub();
1509 sub convertible_units {
1510 $main::lxdebug->enter_sub();
1514 my $filter_unit = shift;
1515 my $not_smaller = shift;
1517 my $conv_units = [];
1519 $filter_unit = $units->{$filter_unit};
1521 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1522 my $unit = $units->{$name};
1524 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1525 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1526 push @{$conv_units}, $unit;
1530 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1532 $main::lxdebug->leave_sub();
1537 # if $a is translatable to $b, return the factor between them.
1540 $main::lxdebug->enter_sub(2);
1541 my ($this, $a, $b, $all_units) = @_;
1544 $all_units = $this->retrieve_all_units;
1547 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1548 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1549 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1550 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1553 sub unit_select_data {
1554 $main::lxdebug->enter_sub();
1556 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1561 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1564 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1565 if (!$convertible_into ||
1566 ($units->{$convertible_into} &&
1567 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1568 push @{$select}, { "name" => $unit,
1569 "base_unit" => $units->{$unit}->{"base_unit"},
1570 "factor" => $units->{$unit}->{"factor"},
1571 "selected" => ($unit eq $selected) ? "selected" : "" };
1575 $main::lxdebug->leave_sub();
1580 sub unit_select_html {
1581 $main::lxdebug->enter_sub();
1583 my ($self, $units, $name, $selected, $convertible_into) = @_;
1585 my $select = "<select name=${name}>";
1587 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1588 if (!$convertible_into ||
1589 ($units->{$convertible_into} &&
1590 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1591 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1594 $select .= "</select>";
1596 $main::lxdebug->leave_sub();
1602 $main::lxdebug->enter_sub();
1606 my $units = $self->retrieve_all_units();
1611 while (2 <= scalar(@_)) {
1612 my $qty = shift(@_);
1613 my $unit = $units->{shift(@_)};
1615 croak "No unit defined with name $unit" if (!defined $unit);
1618 $base_unit = $unit->{base_unit};
1619 } elsif ($base_unit ne $unit->{base_unit}) {
1620 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1623 $sum += $qty * $unit->{factor};
1626 $main::lxdebug->leave_sub();
1628 return wantarray ? ($sum, $base_unit) : $sum;
1632 $main::lxdebug->enter_sub();
1634 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1636 my $dbh = $form->dbconnect_noauto($myconfig);
1638 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1639 my ($sortkey) = selectrow_query($form, $dbh, $query);
1641 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1642 "VALUES (?, ?, ?, ?)";
1643 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1646 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1647 my $sth = $dbh->prepare($query);
1648 foreach my $lang (@{$languages}) {
1649 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1650 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1658 $main::lxdebug->leave_sub();
1662 $main::lxdebug->enter_sub();
1664 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1666 my $dbh = $form->dbconnect_noauto($myconfig);
1668 my ($base_unit, $unit, $sth, $query);
1670 $query = "DELETE FROM units_language";
1671 $dbh->do($query) || $form->dberror($query);
1673 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1674 $query = "DELETE FROM units WHERE name IN (";
1675 map({ $query .= "?," } @{$delete_units});
1676 substr($query, -1, 1) = ")";
1677 $dbh->do($query, undef, @{$delete_units}) ||
1678 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1681 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1682 $sth = $dbh->prepare($query);
1684 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1685 my $sth_lang = $dbh->prepare($query_lang);
1687 foreach $unit (values(%{$units})) {
1688 $unit->{"depth"} = 0;
1689 my $base_unit = $unit;
1690 while ($base_unit->{"base_unit"}) {
1692 $base_unit = $units->{$base_unit->{"base_unit"}};
1696 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1697 if ($unit->{"LANGUAGES"}) {
1698 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1699 next unless ($lang->{"id"} && $lang->{"localized"});
1700 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1701 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1705 next if ($unit->{"unchanged_unit"});
1707 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1708 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1712 $sth_lang->finish();
1716 $main::lxdebug->leave_sub();
1720 $main::lxdebug->enter_sub();
1722 my ($self, $myconfig, $form) = @_;
1724 # connect to database
1725 my $dbh = $form->dbconnect($myconfig);
1727 my $query = qq|SELECT
1731 round(t.rate * 100, 2) AS rate,
1732 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1733 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1735 ORDER BY taxkey, rate|;
1737 my $sth = $dbh->prepare($query);
1738 $sth->execute || $form->dberror($query);
1741 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1742 push @{ $form->{TAX} }, $ref;
1748 $main::lxdebug->leave_sub();
1751 sub get_tax_accounts {
1752 $main::lxdebug->enter_sub();
1754 my ($self, $myconfig, $form) = @_;
1756 my $dbh = $form->dbconnect($myconfig);
1758 # get Accounts from chart
1759 my $query = qq{ SELECT
1761 accno || ' - ' || description AS taxaccount
1763 WHERE link LIKE '%_tax%'
1767 my $sth = $dbh->prepare($query);
1768 $sth->execute || $form->dberror($query);
1770 $form->{ACCOUNTS} = [];
1771 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1772 push @{ $form->{ACCOUNTS} }, $ref;
1779 $main::lxdebug->leave_sub();
1783 $main::lxdebug->enter_sub();
1785 my ($self, $myconfig, $form) = @_;
1787 # connect to database
1788 my $dbh = $form->dbconnect($myconfig);
1790 my $query = qq|SELECT
1793 round(rate * 100, 2) AS rate,
1796 (id IN (SELECT tax_id
1797 FROM acc_trans)) AS tax_already_used
1801 my $sth = $dbh->prepare($query);
1802 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1804 my $ref = $sth->fetchrow_hashref("NAME_lc");
1806 map { $form->{$_} = $ref->{$_} } keys %$ref;
1810 # see if it is used by a taxkey
1811 $query = qq|SELECT count(*) FROM taxkeys
1812 WHERE tax_id = ? AND chart_id >0|;
1814 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1816 $form->{orphaned} = !$form->{orphaned};
1819 if (!$form->{orphaned} ) {
1820 $query = qq|SELECT DISTINCT c.id, c.accno
1822 JOIN tax t ON (t.id = tk.tax_id)
1823 JOIN chart c ON (c.id = tk.chart_id)
1824 WHERE tk.tax_id = ?|;
1826 $sth = $dbh->prepare($query);
1827 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1829 $form->{TAXINUSE} = [];
1830 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1831 push @{ $form->{TAXINUSE} }, $ref;
1839 $main::lxdebug->leave_sub();
1843 $main::lxdebug->enter_sub();
1845 my ($self, $myconfig, $form) = @_;
1848 # connect to database
1849 my $dbh = $form->get_standard_dbh($myconfig);
1851 $form->{rate} = $form->{rate} / 100;
1853 my $chart_categories = '';
1854 $chart_categories .= 'A' if $form->{asset};
1855 $chart_categories .= 'L' if $form->{liability};
1856 $chart_categories .= 'Q' if $form->{equity};
1857 $chart_categories .= 'I' if $form->{revenue};
1858 $chart_categories .= 'E' if $form->{expense};
1859 $chart_categories .= 'C' if $form->{costs};
1861 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id}, $chart_categories);
1862 if ($form->{id} ne "") {
1863 $query = qq|UPDATE tax SET
1868 taxnumber = (SELECT accno FROM chart WHERE id= ? ),
1869 chart_categories = ?
1871 push(@values, $form->{id});
1875 $query = qq|INSERT INTO tax (
1883 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ? )|;
1885 do_query($form, $dbh, $query, @values);
1889 $main::lxdebug->leave_sub();
1893 $main::lxdebug->enter_sub();
1895 my ($self, $myconfig, $form) = @_;
1898 # connect to database
1899 my $dbh = $form->get_standard_dbh($myconfig);
1901 $query = qq|DELETE FROM tax
1903 do_query($form, $dbh, $query, $form->{id});
1907 $main::lxdebug->leave_sub();
1910 sub save_price_factor {
1911 $main::lxdebug->enter_sub();
1913 my ($self, $myconfig, $form) = @_;
1915 # connect to database
1916 my $dbh = $form->get_standard_dbh($myconfig);
1919 my @values = ($form->{description}, conv_i($form->{factor}));
1922 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1923 push @values, conv_i($form->{id});
1926 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1929 do_query($form, $dbh, $query, @values);
1933 $main::lxdebug->leave_sub();
1936 sub get_all_price_factors {
1937 $main::lxdebug->enter_sub();
1939 my ($self, $myconfig, $form) = @_;
1941 # connect to database
1942 my $dbh = $form->get_standard_dbh($myconfig);
1944 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1946 $main::lxdebug->leave_sub();
1949 sub get_price_factor {
1950 $main::lxdebug->enter_sub();
1952 my ($self, $myconfig, $form) = @_;
1954 # connect to database
1955 my $dbh = $form->get_standard_dbh($myconfig);
1957 my $query = qq|SELECT description, factor,
1958 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1959 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1960 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1961 FROM price_factors WHERE id = ?|;
1963 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1965 $main::lxdebug->leave_sub();
1968 sub delete_price_factor {
1969 $main::lxdebug->enter_sub();
1971 my ($self, $myconfig, $form) = @_;
1973 # connect to database
1974 my $dbh = $form->get_standard_dbh($myconfig);
1976 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1979 $main::lxdebug->leave_sub();
1982 sub save_warehouse {
1983 $main::lxdebug->enter_sub();
1985 my ($self, $myconfig, $form) = @_;
1987 # connect to database
1988 my $dbh = $form->get_standard_dbh($myconfig);
1990 my ($query, @values, $sth);
1993 $query = qq|SELECT nextval('id')|;
1994 ($form->{id}) = selectrow_query($form, $dbh, $query);
1996 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1997 do_query($form, $dbh, $query, $form->{id});
2000 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2001 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2003 if (0 < $form->{number_of_new_bins}) {
2004 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2005 $sth = prepare_query($form, $dbh, $query);
2007 foreach my $i (1..$form->{number_of_new_bins}) {
2008 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2016 $main::lxdebug->leave_sub();
2020 $main::lxdebug->enter_sub();
2022 my ($self, $myconfig, $form) = @_;
2024 # connect to database
2025 my $dbh = $form->get_standard_dbh($myconfig);
2027 my ($query, @values, $commit_necessary, $sth);
2029 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2032 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2033 do_query($form, $dbh, $query, @values);
2035 $commit_necessary = 1;
2038 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2039 $sth = prepare_query($form, $dbh, $query);
2041 foreach my $row (1..$form->{rowcount}) {
2042 next if ($form->{"delete_${row}"});
2044 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2046 $commit_necessary = 1;
2051 $dbh->commit() if ($commit_necessary);
2053 $main::lxdebug->leave_sub();
2056 sub delete_warehouse {
2057 $main::lxdebug->enter_sub();
2059 my ($self, $myconfig, $form) = @_;
2061 # connect to database
2062 my $dbh = $form->get_standard_dbh($myconfig);
2064 my $id = conv_i($form->{id});
2065 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|;
2066 my ($count) = selectrow_query($form, $dbh, $query, $id);
2069 $main::lxdebug->leave_sub();
2073 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2074 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2078 $main::lxdebug->leave_sub();
2083 sub get_all_warehouses {
2084 $main::lxdebug->enter_sub();
2086 my ($self, $myconfig, $form) = @_;
2088 # connect to database
2089 my $dbh = $form->get_standard_dbh($myconfig);
2091 my $query = qq|SELECT w.id, w.description, w.invalid,
2092 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2094 ORDER BY w.sortkey|;
2096 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2098 $main::lxdebug->leave_sub();
2102 $main::lxdebug->enter_sub();
2104 my ($self, $myconfig, $form) = @_;
2106 # connect to database
2107 my $dbh = $form->get_standard_dbh($myconfig);
2109 my $id = conv_i($form->{id});
2110 my $query = qq|SELECT w.description, w.invalid
2114 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2116 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2118 $query = qq|SELECT b.*, EXISTS
2119 (SELECT i.warehouse_id, p.warehouse_id
2120 FROM inventory i, parts p
2121 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();