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 for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
211 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
214 # sanity check, can't have AR with AR_...
215 if ($form->{AR} || $form->{AP} || $form->{IC}) {
216 for (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)) {
217 $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!')) if $form->{$_};
222 foreach my $item ($form->{AR}, $form->{AR_amount},
223 $form->{AR_tax}, $form->{AR_paid},
224 $form->{AP}, $form->{AP_amount},
225 $form->{AP_tax}, $form->{AP_paid},
226 $form->{IC}, $form->{IC_sale},
227 $form->{IC_cogs}, $form->{IC_taxpart},
228 $form->{IC_income}, $form->{IC_expense},
229 $form->{IC_taxservice}
231 $form->{link} .= "${item}:" if ($item);
235 # strip blanks from accno
236 map { $form->{$_} =~ s/ //g; } qw(accno);
240 if ($form->{id} eq "NULL") {
249 my @values = ($form->{accno});
252 $query .= ' AND NOT id = ?';
253 push(@values, $form->{id});
256 my ($accno) = selectrow_query($form, $dbh, $query, @values);
259 $form->error($::locale->text('Account number not unique!'));
263 if (!$form->{id} || $form->{id} eq "") {
264 $query = qq|SELECT nextval('id')|;
265 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
266 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
267 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
275 # if charttype is heading make sure certain values are empty
276 # specifically, if charttype is changed from an existing account, empty the
277 # fields unnecessary for headings, so that e.g. heading doesn't appear in
278 # drop-down menues due to still having a valid "link" entry
280 if ( $form->{charttype} eq 'H' ) {
282 $form->{pos_bwa} = '';
283 $form->{pos_bilanz} = '';
284 $form->{pos_eur} = '';
285 $form->{new_chart_id} = '';
286 $form->{valid_from} = '';
289 $query = qq|UPDATE chart SET
305 $form->{description},
309 conv_i($form->{pos_bwa}),
310 conv_i($form->{pos_bilanz}),
311 conv_i($form->{pos_eur}),
312 conv_i($form->{new_chart_id}),
313 conv_date($form->{valid_from}),
314 ($form->{datevautomatik} eq 'T') ? 'true':'false',
321 do_query($form, $dbh, $query, @values);
327 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
331 for $tk_count (0 .. $MAX_TRIES) {
335 # Check if the account already exists, else cancel
337 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
339 last READTAXKEYS if ( $form->{'id'} == 0);
341 # check if there is a startdate
342 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
347 # Add valid taxkeys into the array
350 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
351 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
352 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
353 chart_id => conv_i($form->{"id"}),
354 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
355 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
362 for my $j (0 .. $#taxkeys){
363 if ( defined $taxkeys[$j]{'id'} ){
366 if ($taxkeys[$j]{'delete'}){
368 DELETE FROM taxkeys WHERE id = ?
371 @values = ($taxkeys[$j]{'id'});
373 do_query($form, $dbh, $query, @values);
382 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
390 $taxkeys[$j]{'tax_id'},
391 $taxkeys[$j]{'chart_id'},
392 $taxkeys[$j]{'tax_id'},
393 $taxkeys[$j]{'pos_ustva'},
394 $taxkeys[$j]{'startdate'},
397 do_query($form, $dbh, $query, @values);
403 INSERT INTO taxkeys (
410 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
413 $taxkeys[$j]{'tax_id'},
414 $taxkeys[$j]{'chart_id'},
415 $taxkeys[$j]{'tax_id'},
416 $taxkeys[$j]{'pos_ustva'},
417 $taxkeys[$j]{'startdate'},
420 do_query($form, $dbh, $query, @values);
425 # Update chart.taxkey_id to the latest from taxkeys for this chart.
431 WHERE taxkeys.chart_id = chart.id
432 ORDER BY startdate DESC
438 do_query($form, $dbh, $query, $form->{id});
441 my $rc = $dbh->commit;
444 $main::lxdebug->leave_sub();
450 $main::lxdebug->enter_sub();
452 my ($self, $myconfig, $form) = @_;
454 # connect to database, turn off AutoCommit
455 my $dbh = $form->dbconnect_noauto($myconfig);
457 my $query = qq|SELECT count(*) FROM acc_trans a
458 WHERE a.chart_id = ?|;
459 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
463 $main::lxdebug->leave_sub();
467 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
468 foreach my $type (qw(inventory income expense)) {
471 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
472 qq|WHERE ${type}_accno_id = ?|;
473 do_query($form, $dbh, $query, $form->{id});
476 $query = qq|DELETE FROM tax
478 do_query($form, $dbh, $query, $form->{id});
480 # delete chart of account record
481 $query = qq|DELETE FROM chart
483 do_query($form, $dbh, $query, $form->{id});
485 # delete account taxkeys
486 $query = qq|DELETE FROM taxkeys
488 do_query($form, $dbh, $query, $form->{id});
490 # commit and redirect
491 my $rc = $dbh->commit;
494 $main::lxdebug->leave_sub();
500 $main::lxdebug->enter_sub();
502 my ($self, $myconfig, $form) = @_;
504 # connect to database
505 my $dbh = $form->dbconnect($myconfig);
507 my $query = qq|SELECT id, lead
511 my $sth = $dbh->prepare($query);
512 $sth->execute || $form->dberror($query);
514 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
515 push @{ $form->{ALL} }, $ref;
521 $main::lxdebug->leave_sub();
525 $main::lxdebug->enter_sub();
527 my ($self, $myconfig, $form) = @_;
529 # connect to database
530 my $dbh = $form->dbconnect($myconfig);
533 qq|SELECT l.id, l.lead | .
536 my $sth = $dbh->prepare($query);
537 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
539 my $ref = $sth->fetchrow_hashref("NAME_lc");
541 map { $form->{$_} = $ref->{$_} } keys %$ref;
547 $main::lxdebug->leave_sub();
551 $main::lxdebug->enter_sub();
553 my ($self, $myconfig, $form) = @_;
556 # connect to database
557 my $dbh = $form->dbconnect($myconfig);
559 my @values = ($form->{description});
560 # id is the old record
562 $query = qq|UPDATE leads SET
565 push(@values, $form->{id});
567 $query = qq|INSERT INTO leads
571 do_query($form, $dbh, $query, @values);
575 $main::lxdebug->leave_sub();
579 $main::lxdebug->enter_sub();
581 my ($self, $myconfig, $form) = @_;
584 # connect to database
585 my $dbh = $form->dbconnect($myconfig);
587 $query = qq|DELETE FROM leads
589 do_query($form, $dbh, $query, $form->{id});
593 $main::lxdebug->leave_sub();
597 $main::lxdebug->enter_sub();
599 my ($self, $myconfig, $form, $return_list) = @_;
601 # connect to database
602 my $dbh = $form->dbconnect($myconfig);
605 "SELECT id, description, template_code, article_code, " .
606 " output_numberformat, output_dateformat, output_longdates " .
607 "FROM language ORDER BY description";
609 my $sth = $dbh->prepare($query);
610 $sth->execute || $form->dberror($query);
614 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
615 push(@{ $ary }, $ref);
621 $main::lxdebug->leave_sub();
631 $main::lxdebug->enter_sub();
633 my ($self, $myconfig, $form) = @_;
635 # connect to database
636 my $dbh = $form->dbconnect($myconfig);
639 "SELECT description, template_code, article_code, " .
640 " output_numberformat, output_dateformat, output_longdates " .
641 "FROM language WHERE id = ?";
642 my $sth = $dbh->prepare($query);
643 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
645 my $ref = $sth->fetchrow_hashref("NAME_lc");
647 map { $form->{$_} = $ref->{$_} } keys %$ref;
653 $main::lxdebug->leave_sub();
656 sub get_language_details {
657 $main::lxdebug->enter_sub();
659 my ($self, $myconfig, $form, $id) = @_;
661 # connect to database
662 my $dbh = $form->dbconnect($myconfig);
665 "SELECT template_code, " .
666 " output_numberformat, output_dateformat, output_longdates " .
667 "FROM language WHERE id = ?";
668 my @res = selectrow_query($form, $dbh, $query, $id);
671 $main::lxdebug->leave_sub();
677 $main::lxdebug->enter_sub();
679 my ($self, $myconfig, $form) = @_;
681 # connect to database
682 my $dbh = $form->dbconnect($myconfig);
683 my (@values, $query);
685 map({ push(@values, $form->{$_}); }
686 qw(description template_code article_code
687 output_numberformat output_dateformat output_longdates));
689 # id is the old record
692 "UPDATE language SET " .
693 " description = ?, template_code = ?, article_code = ?, " .
694 " output_numberformat = ?, output_dateformat = ?, " .
695 " output_longdates = ? " .
697 push(@values, $form->{id});
700 "INSERT INTO language (" .
701 " description, template_code, article_code, " .
702 " output_numberformat, output_dateformat, output_longdates" .
703 ") VALUES (?, ?, ?, ?, ?, ?)";
705 do_query($form, $dbh, $query, @values);
709 $main::lxdebug->leave_sub();
712 sub delete_language {
713 $main::lxdebug->enter_sub();
715 my ($self, $myconfig, $form) = @_;
718 # connect to database
719 my $dbh = $form->dbconnect_noauto($myconfig);
721 foreach my $table (qw(generic_translations units_language)) {
722 $query = qq|DELETE FROM $table WHERE language_id = ?|;
723 do_query($form, $dbh, $query, $form->{"id"});
726 $query = "DELETE FROM language WHERE id = ?";
727 do_query($form, $dbh, $query, $form->{"id"});
732 $main::lxdebug->leave_sub();
737 $main::lxdebug->enter_sub();
739 my ($self, $myconfig, $form) = @_;
741 # connect to database
742 my $dbh = $form->dbconnect($myconfig);
744 my $query = qq|SELECT id, description,
746 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
748 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
750 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
752 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
754 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
756 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
758 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
760 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
762 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
766 my $sth = $dbh->prepare($query);
767 $sth->execute || $form->dberror($query);
770 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
771 push @{ $form->{ALL} }, $ref;
777 $main::lxdebug->leave_sub();
780 sub get_buchungsgruppe {
781 $main::lxdebug->enter_sub();
783 my ($self, $myconfig, $form) = @_;
786 # connect to database
787 my $dbh = $form->dbconnect($myconfig);
791 qq|SELECT description, inventory_accno_id,
792 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
794 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
796 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
798 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
800 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
802 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
804 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
806 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
808 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
811 my $sth = $dbh->prepare($query);
812 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
814 my $ref = $sth->fetchrow_hashref("NAME_lc");
816 map { $form->{$_} = $ref->{$_} } keys %$ref;
821 qq|SELECT count(id) = 0 AS orphaned
823 WHERE buchungsgruppen_id = ?|;
824 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
827 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
829 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
830 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
833 $query = qq|SELECT c.accno, c.description, c.link, c.id,
834 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
835 FROM chart c, defaults d
836 WHERE c.link LIKE '%$module%'
840 my $sth = $dbh->prepare($query);
841 $sth->execute || $form->dberror($query);
842 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
843 foreach my $key (split(/:/, $ref->{link})) {
844 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
845 $form->{"std_inventory_accno_id"} = $ref->{"id"};
847 if ($key =~ /$module/) {
848 if ( ($ref->{id} eq $ref->{inventory_accno_id})
849 || ($ref->{id} eq $ref->{income_accno_id})
850 || ($ref->{id} eq $ref->{expense_accno_id})) {
851 push @{ $form->{"${module}_links"}{$key} },
852 { accno => $ref->{accno},
853 description => $ref->{description},
854 selected => "selected",
857 push @{ $form->{"${module}_links"}{$key} },
858 { accno => $ref->{accno},
859 description => $ref->{description},
871 $main::lxdebug->leave_sub();
874 sub save_buchungsgruppe {
875 $main::lxdebug->enter_sub();
877 my ($self, $myconfig, $form) = @_;
879 # connect to database
880 my $dbh = $form->dbconnect($myconfig);
882 my @values = ($form->{description}, $form->{inventory_accno_id},
883 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
884 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
885 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
886 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
890 # id is the old record
892 $query = qq|UPDATE buchungsgruppen SET
893 description = ?, inventory_accno_id = ?,
894 income_accno_id_0 = ?, expense_accno_id_0 = ?,
895 income_accno_id_1 = ?, expense_accno_id_1 = ?,
896 income_accno_id_2 = ?, expense_accno_id_2 = ?,
897 income_accno_id_3 = ?, expense_accno_id_3 = ?
899 push(@values, $form->{id});
901 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
902 my ($sortkey) = $dbh->selectrow_array($query);
903 $form->dberror($query) if ($dbh->err);
904 push(@values, $sortkey);
905 $query = qq|INSERT INTO buchungsgruppen
906 (description, inventory_accno_id,
907 income_accno_id_0, expense_accno_id_0,
908 income_accno_id_1, expense_accno_id_1,
909 income_accno_id_2, expense_accno_id_2,
910 income_accno_id_3, expense_accno_id_3,
912 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
914 do_query($form, $dbh, $query, @values);
918 $main::lxdebug->leave_sub();
921 sub delete_buchungsgruppe {
922 $main::lxdebug->enter_sub();
924 my ($self, $myconfig, $form) = @_;
926 # connect to database
927 my $dbh = $form->dbconnect($myconfig);
929 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
930 do_query($form, $dbh, $query, $form->{id});
934 $main::lxdebug->leave_sub();
938 $main::lxdebug->enter_sub();
940 my ($self, $myconfig, $form, $table) = @_;
942 # connect to database
943 my $dbh = $form->get_standard_dbh($myconfig);
947 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
948 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
949 my @values = ($form->{"id1"}, $form->{"id2"});
950 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
952 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
953 my $sth = prepare_query($form, $dbh, $query);
955 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
956 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
962 $main::lxdebug->leave_sub();
965 sub prepare_template_filename {
966 $main::lxdebug->enter_sub();
968 my ($self, $myconfig, $form) = @_;
970 my ($filename, $display_filename);
972 if ($form->{type} eq "stylesheet") {
973 $filename = "css/$myconfig->{stylesheet}";
974 $display_filename = $myconfig->{stylesheet};
977 $filename = $form->{formname};
979 if ($form->{language}) {
980 my ($id, $template_code) = split(/--/, $form->{language});
981 $filename .= "_${template_code}";
984 if ($form->{printer}) {
985 my ($id, $template_code) = split(/--/, $form->{printer});
986 $filename .= "_${template_code}";
989 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
990 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
991 $filename =~ s|.*/||;
993 $display_filename = $filename;
994 $filename = "$myconfig->{templates}/$filename";
997 $main::lxdebug->leave_sub();
999 return ($filename, $display_filename);
1004 $main::lxdebug->enter_sub();
1006 my ($self, $filename) = @_;
1008 my ($content, $lines) = ("", 0);
1012 if (open(TEMPLATE, $filename)) {
1013 while (<TEMPLATE>) {
1020 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1022 $main::lxdebug->leave_sub();
1024 return ($content, $lines);
1028 $main::lxdebug->enter_sub();
1030 my ($self, $filename, $content) = @_;
1036 if (open(TEMPLATE, ">", $filename)) {
1037 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1038 $content =~ s/\r\n/\n/g;
1039 print(TEMPLATE $content);
1045 $main::lxdebug->leave_sub();
1051 $main::lxdebug->enter_sub();
1056 my $myconfig = \%main::myconfig;
1057 my $form = $main::form;
1059 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1062 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1064 # these defaults are database wide
1067 qq|UPDATE defaults SET
1068 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1069 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1070 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1090 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1091 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1092 $form->{invnumber}, $form->{cnnumber},
1093 $form->{sonumber}, $form->{ponumber},
1094 $form->{sqnumber}, $form->{rfqnumber},
1095 $form->{customernumber}, $form->{vendornumber},
1096 $form->{articlenumber}, $form->{servicenumber},
1097 $form->{assemblynumber},
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 name = ? WHERE name = ?|;
1108 do_query($form, $dbh, $query, $form->{"curr_$i"}, $form->{"old_curr_$i"});
1112 if (length($form->{new_curr}) > 0) {
1113 $query = qq|INSERT INTO currencies (name) VALUES (?)|;
1114 do_query($form, $dbh, $query, $form->{new_curr});
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 name AS curr FROM currencies ORDER BY id|;
1297 $form->{CURRENCIES} = selectall_hashref_query($form, $dbh, $query);
1299 #Which of them is the default currency?
1300 $query = qq|SELECT name AS defaultcurrency FROM currencies WHERE id = (SELECT currency_id FROM defaults LIMIT 1);|;
1301 ($form->{defaultcurrency}) = selectrow_query($form, $dbh, $query);
1305 $main::lxdebug->leave_sub();
1309 $main::lxdebug->enter_sub();
1311 my ($self, $myconfig, $form) = @_;
1313 my $dbh = $form->dbconnect($myconfig);
1315 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1316 my $sth = $dbh->prepare($query);
1317 $sth->execute || $form->dberror($query);
1319 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1325 $main::lxdebug->leave_sub();
1329 $main::lxdebug->enter_sub();
1331 my ($self, $myconfig, $form) = @_;
1333 my $dbh = $form->dbconnect($myconfig);
1335 my ($query, @values);
1337 if ($form->{revtrans}) {
1338 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1340 } elsif ($form->{closedto}) {
1341 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1342 @values = (conv_date($form->{closedto}));
1345 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1348 # set close in defaults
1349 do_query($form, $dbh, $query, @values);
1353 $main::lxdebug->leave_sub();
1357 my ($self, $units, $unit_name, $factor) = @_;
1359 $factor = 1 unless ($factor);
1361 my $unit = $units->{$unit_name};
1363 if (!defined($unit) || !$unit->{"base_unit"} ||
1364 ($unit_name eq $unit->{"base_unit"})) {
1365 return ($unit_name, $factor);
1368 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1371 sub retrieve_units {
1372 $main::lxdebug->enter_sub();
1374 my ($self, $myconfig, $form, $prefix) = @_;
1377 my $dbh = $form->get_standard_dbh;
1379 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1381 my $sth = prepare_execute_query($form, $dbh, $query);
1384 while (my $ref = $sth->fetchrow_hashref()) {
1385 $units->{$ref->{"name"}} = $ref;
1389 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1390 $sth = $dbh->prepare($query_lang);
1391 $sth->execute() || $form->dberror($query_lang);
1393 while (my $ref = $sth->fetchrow_hashref()) {
1394 push(@languages, $ref);
1398 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1399 "FROM units_language ul " .
1400 "LEFT JOIN language l ON ul.language_id = l.id " .
1401 "WHERE ul.unit = ?";
1402 $sth = $dbh->prepare($query_lang);
1404 foreach my $unit (values(%{$units})) {
1405 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1407 $unit->{"LANGUAGES"} = {};
1408 foreach my $lang (@languages) {
1409 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1412 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1413 while (my $ref = $sth->fetchrow_hashref()) {
1414 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1419 $main::lxdebug->leave_sub();
1424 sub retrieve_all_units {
1425 $main::lxdebug->enter_sub();
1429 if (!$::request->{cache}{all_units}) {
1430 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1433 $main::lxdebug->leave_sub();
1435 return $::request->{cache}{all_units};
1439 sub translate_units {
1440 $main::lxdebug->enter_sub();
1442 my ($self, $form, $template_code, $unit, $amount) = @_;
1444 my $units = $self->retrieve_units(\%main::myconfig, $form);
1446 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1447 my $new_unit = $unit;
1449 if (($amount != 1) && $h->{"localized_plural"}) {
1450 $new_unit = $h->{"localized_plural"};
1451 } elsif ($h->{"localized"}) {
1452 $new_unit = $h->{"localized"};
1456 $main::lxdebug->leave_sub();
1462 $main::lxdebug->enter_sub();
1464 my ($self, $myconfig, $form, $units) = @_;
1466 my $dbh = $form->dbconnect($myconfig);
1468 map({ $_->{"in_use"} = 0; } values(%{$units}));
1470 foreach my $unit (values(%{$units})) {
1471 my $base_unit = $unit->{"original_base_unit"};
1472 while ($base_unit) {
1473 $units->{$base_unit}->{"in_use"} = 1;
1474 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1475 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1476 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1480 foreach my $unit (values(%{$units})) {
1481 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1483 foreach my $table (qw(parts invoice orderitems)) {
1484 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1486 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1487 $query .= "= " . $dbh->quote($unit->{"name"});
1489 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1490 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1493 my ($count) = $dbh->selectrow_array($query);
1494 $form->dberror($query) if ($dbh->err);
1497 $unit->{"in_use"} = 1;
1505 $main::lxdebug->leave_sub();
1508 sub convertible_units {
1509 $main::lxdebug->enter_sub();
1513 my $filter_unit = shift;
1514 my $not_smaller = shift;
1516 my $conv_units = [];
1518 $filter_unit = $units->{$filter_unit};
1520 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1521 my $unit = $units->{$name};
1523 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1524 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1525 push @{$conv_units}, $unit;
1529 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1531 $main::lxdebug->leave_sub();
1536 # if $a is translatable to $b, return the factor between them.
1539 $main::lxdebug->enter_sub(2);
1540 my ($this, $a, $b, $all_units) = @_;
1543 $all_units = $this->retrieve_all_units;
1546 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1547 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1548 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1549 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1552 sub unit_select_data {
1553 $main::lxdebug->enter_sub();
1555 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1560 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1563 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1564 if (!$convertible_into ||
1565 ($units->{$convertible_into} &&
1566 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1567 push @{$select}, { "name" => $unit,
1568 "base_unit" => $units->{$unit}->{"base_unit"},
1569 "factor" => $units->{$unit}->{"factor"},
1570 "selected" => ($unit eq $selected) ? "selected" : "" };
1574 $main::lxdebug->leave_sub();
1579 sub unit_select_html {
1580 $main::lxdebug->enter_sub();
1582 my ($self, $units, $name, $selected, $convertible_into) = @_;
1584 my $select = "<select name=${name}>";
1586 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1587 if (!$convertible_into ||
1588 ($units->{$convertible_into} &&
1589 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1590 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1593 $select .= "</select>";
1595 $main::lxdebug->leave_sub();
1601 $main::lxdebug->enter_sub();
1605 my $units = $self->retrieve_all_units();
1610 while (2 <= scalar(@_)) {
1611 my $qty = shift(@_);
1612 my $unit = $units->{shift(@_)};
1614 croak "No unit defined with name $unit" if (!defined $unit);
1617 $base_unit = $unit->{base_unit};
1618 } elsif ($base_unit ne $unit->{base_unit}) {
1619 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1622 $sum += $qty * $unit->{factor};
1625 $main::lxdebug->leave_sub();
1627 return wantarray ? ($sum, $base_unit) : $sum;
1631 $main::lxdebug->enter_sub();
1633 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1635 my $dbh = $form->dbconnect_noauto($myconfig);
1637 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1638 my ($sortkey) = selectrow_query($form, $dbh, $query);
1640 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1641 "VALUES (?, ?, ?, ?)";
1642 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1645 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1646 my $sth = $dbh->prepare($query);
1647 foreach my $lang (@{$languages}) {
1648 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1649 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1657 $main::lxdebug->leave_sub();
1661 $main::lxdebug->enter_sub();
1663 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1665 my $dbh = $form->dbconnect_noauto($myconfig);
1667 my ($base_unit, $unit, $sth, $query);
1669 $query = "DELETE FROM units_language";
1670 $dbh->do($query) || $form->dberror($query);
1672 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1673 $query = "DELETE FROM units WHERE name IN (";
1674 map({ $query .= "?," } @{$delete_units});
1675 substr($query, -1, 1) = ")";
1676 $dbh->do($query, undef, @{$delete_units}) ||
1677 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1680 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1681 $sth = $dbh->prepare($query);
1683 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1684 my $sth_lang = $dbh->prepare($query_lang);
1686 foreach $unit (values(%{$units})) {
1687 $unit->{"depth"} = 0;
1688 my $base_unit = $unit;
1689 while ($base_unit->{"base_unit"}) {
1691 $base_unit = $units->{$base_unit->{"base_unit"}};
1695 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1696 if ($unit->{"LANGUAGES"}) {
1697 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1698 next unless ($lang->{"id"} && $lang->{"localized"});
1699 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1700 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1704 next if ($unit->{"unchanged_unit"});
1706 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1707 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1711 $sth_lang->finish();
1715 $main::lxdebug->leave_sub();
1719 $main::lxdebug->enter_sub();
1721 my ($self, $myconfig, $form) = @_;
1723 # connect to database
1724 my $dbh = $form->dbconnect($myconfig);
1726 my $query = qq|SELECT
1730 round(t.rate * 100, 2) AS rate,
1731 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1732 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1734 ORDER BY taxkey, rate|;
1736 my $sth = $dbh->prepare($query);
1737 $sth->execute || $form->dberror($query);
1740 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1741 push @{ $form->{TAX} }, $ref;
1747 $main::lxdebug->leave_sub();
1750 sub get_tax_accounts {
1751 $main::lxdebug->enter_sub();
1753 my ($self, $myconfig, $form) = @_;
1755 my $dbh = $form->dbconnect($myconfig);
1757 # get Accounts from chart
1758 my $query = qq{ SELECT
1760 accno || ' - ' || description AS taxaccount
1762 WHERE link LIKE '%_tax%'
1766 my $sth = $dbh->prepare($query);
1767 $sth->execute || $form->dberror($query);
1769 $form->{ACCOUNTS} = [];
1770 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1771 push @{ $form->{ACCOUNTS} }, $ref;
1778 $main::lxdebug->leave_sub();
1782 $main::lxdebug->enter_sub();
1784 my ($self, $myconfig, $form) = @_;
1786 # connect to database
1787 my $dbh = $form->dbconnect($myconfig);
1789 my $query = qq|SELECT
1792 round(rate * 100, 2) AS rate,
1795 (id IN (SELECT tax_id
1796 FROM acc_trans)) AS tax_already_used
1800 my $sth = $dbh->prepare($query);
1801 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1803 my $ref = $sth->fetchrow_hashref("NAME_lc");
1805 map { $form->{$_} = $ref->{$_} } keys %$ref;
1809 # see if it is used by a taxkey
1810 $query = qq|SELECT count(*) FROM taxkeys
1811 WHERE tax_id = ? AND chart_id >0|;
1813 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1815 $form->{orphaned} = !$form->{orphaned};
1818 if (!$form->{orphaned} ) {
1819 $query = qq|SELECT DISTINCT c.id, c.accno
1821 JOIN tax t ON (t.id = tk.tax_id)
1822 JOIN chart c ON (c.id = tk.chart_id)
1823 WHERE tk.tax_id = ?|;
1825 $sth = $dbh->prepare($query);
1826 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1828 $form->{TAXINUSE} = [];
1829 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1830 push @{ $form->{TAXINUSE} }, $ref;
1838 $main::lxdebug->leave_sub();
1842 $main::lxdebug->enter_sub();
1844 my ($self, $myconfig, $form) = @_;
1847 # connect to database
1848 my $dbh = $form->get_standard_dbh($myconfig);
1850 $form->{rate} = $form->{rate} / 100;
1852 my $chart_categories = '';
1853 $chart_categories .= 'A' if $form->{asset};
1854 $chart_categories .= 'L' if $form->{liability};
1855 $chart_categories .= 'Q' if $form->{equity};
1856 $chart_categories .= 'I' if $form->{revenue};
1857 $chart_categories .= 'E' if $form->{expense};
1858 $chart_categories .= 'C' if $form->{costs};
1860 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id}, $chart_categories);
1861 if ($form->{id} ne "") {
1862 $query = qq|UPDATE tax SET
1867 taxnumber = (SELECT accno FROM chart WHERE id= ? ),
1868 chart_categories = ?
1870 push(@values, $form->{id});
1874 $query = qq|INSERT INTO tax (
1882 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ? )|;
1884 do_query($form, $dbh, $query, @values);
1888 $main::lxdebug->leave_sub();
1892 $main::lxdebug->enter_sub();
1894 my ($self, $myconfig, $form) = @_;
1897 # connect to database
1898 my $dbh = $form->get_standard_dbh($myconfig);
1900 $query = qq|DELETE FROM tax
1902 do_query($form, $dbh, $query, $form->{id});
1906 $main::lxdebug->leave_sub();
1909 sub save_price_factor {
1910 $main::lxdebug->enter_sub();
1912 my ($self, $myconfig, $form) = @_;
1914 # connect to database
1915 my $dbh = $form->get_standard_dbh($myconfig);
1918 my @values = ($form->{description}, conv_i($form->{factor}));
1921 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1922 push @values, conv_i($form->{id});
1925 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1928 do_query($form, $dbh, $query, @values);
1932 $main::lxdebug->leave_sub();
1935 sub get_all_price_factors {
1936 $main::lxdebug->enter_sub();
1938 my ($self, $myconfig, $form) = @_;
1940 # connect to database
1941 my $dbh = $form->get_standard_dbh($myconfig);
1943 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1945 $main::lxdebug->leave_sub();
1948 sub get_price_factor {
1949 $main::lxdebug->enter_sub();
1951 my ($self, $myconfig, $form) = @_;
1953 # connect to database
1954 my $dbh = $form->get_standard_dbh($myconfig);
1956 my $query = qq|SELECT description, factor,
1957 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1958 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1959 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1960 FROM price_factors WHERE id = ?|;
1962 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1964 $main::lxdebug->leave_sub();
1967 sub delete_price_factor {
1968 $main::lxdebug->enter_sub();
1970 my ($self, $myconfig, $form) = @_;
1972 # connect to database
1973 my $dbh = $form->get_standard_dbh($myconfig);
1975 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1978 $main::lxdebug->leave_sub();
1981 sub save_warehouse {
1982 $main::lxdebug->enter_sub();
1984 my ($self, $myconfig, $form) = @_;
1986 # connect to database
1987 my $dbh = $form->get_standard_dbh($myconfig);
1989 my ($query, @values, $sth);
1992 $query = qq|SELECT nextval('id')|;
1993 ($form->{id}) = selectrow_query($form, $dbh, $query);
1995 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1996 do_query($form, $dbh, $query, $form->{id});
1999 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2000 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2002 if (0 < $form->{number_of_new_bins}) {
2003 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2004 $sth = prepare_query($form, $dbh, $query);
2006 foreach my $i (1..$form->{number_of_new_bins}) {
2007 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2015 $main::lxdebug->leave_sub();
2019 $main::lxdebug->enter_sub();
2021 my ($self, $myconfig, $form) = @_;
2023 # connect to database
2024 my $dbh = $form->get_standard_dbh($myconfig);
2026 my ($query, @values, $commit_necessary, $sth);
2028 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2031 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2032 do_query($form, $dbh, $query, @values);
2034 $commit_necessary = 1;
2037 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2038 $sth = prepare_query($form, $dbh, $query);
2040 foreach my $row (1..$form->{rowcount}) {
2041 next if ($form->{"delete_${row}"});
2043 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2045 $commit_necessary = 1;
2050 $dbh->commit() if ($commit_necessary);
2052 $main::lxdebug->leave_sub();
2055 sub delete_warehouse {
2056 $main::lxdebug->enter_sub();
2058 my ($self, $myconfig, $form) = @_;
2060 # connect to database
2061 my $dbh = $form->get_standard_dbh($myconfig);
2063 my $id = conv_i($form->{id});
2064 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|;
2065 my ($count) = selectrow_query($form, $dbh, $query, $id);
2068 $main::lxdebug->leave_sub();
2072 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2073 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2077 $main::lxdebug->leave_sub();
2082 sub get_all_warehouses {
2083 $main::lxdebug->enter_sub();
2085 my ($self, $myconfig, $form) = @_;
2087 # connect to database
2088 my $dbh = $form->get_standard_dbh($myconfig);
2090 my $query = qq|SELECT w.id, w.description, w.invalid,
2091 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2093 ORDER BY w.sortkey|;
2095 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2097 $main::lxdebug->leave_sub();
2101 $main::lxdebug->enter_sub();
2103 my ($self, $myconfig, $form) = @_;
2105 # connect to database
2106 my $dbh = $form->get_standard_dbh($myconfig);
2108 my $id = conv_i($form->{id});
2109 my $query = qq|SELECT w.description, w.invalid
2113 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2115 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2117 $query = qq|SELECT b.*, EXISTS
2118 (SELECT i.warehouse_id
2120 WHERE i.bin_id = b.id
2124 WHERE b.warehouse_id = ?|;
2126 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2128 $main::lxdebug->leave_sub();