1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Administration module
36 #======================================================================
48 $main::lxdebug->enter_sub();
50 my ($self, $myconfig, $form) = @_;
53 my $dbh = $form->dbconnect($myconfig);
55 SELECT c.accno, c.description, c.charttype, c.category,
56 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
57 c.pos_bwa, datevautomatik,
58 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
59 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
62 ON (c.id=tk.chart_id AND tk.id =
63 (SELECT id FROM taxkeys
64 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
65 ORDER BY startdate DESC LIMIT 1))
70 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
71 my $sth = $dbh->prepare($query);
72 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
74 my $ref = $sth->fetchrow_hashref("NAME_lc");
76 foreach my $key (keys %$ref) {
77 $form->{"$key"} = $ref->{"$key"};
82 # get default accounts
83 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
85 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
86 $sth = $dbh->prepare($query);
87 $sth->execute || $form->dberror($query);
89 $ref = $sth->fetchrow_hashref("NAME_lc");
91 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
97 # get taxkeys and description
101 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
103 id||'--'||taxkey AS tax,
106 FROM tax ORDER BY taxkey
108 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
109 $sth = $dbh->prepare($query);
110 $sth->execute || $form->dberror($query);
112 $form->{TAXKEY} = [];
114 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
115 push @{ $form->{TAXKEY} }, $ref;
121 $query = qq|SELECT id, accno,description
125 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
126 $sth = $dbh->prepare($query);
127 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
129 $form->{NEWACCOUNT} = [];
130 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
131 push @{ $form->{NEWACCOUNT} }, $ref;
136 # get the taxkeys of account
150 LEFT JOIN tax t ON (t.id = tk.tax_id)
151 LEFT JOIN chart c ON (c.id = t.chart_id)
153 WHERE tk.chart_id = ?
154 ORDER BY startdate DESC
156 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
157 $sth = $dbh->prepare($query);
159 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
161 $form->{ACCOUNT_TAXKEYS} = [];
163 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
170 # check if we have any transactions
171 $query = qq|SELECT a.trans_id FROM acc_trans a
172 WHERE a.chart_id = ?|;
173 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
174 $sth = $dbh->prepare($query);
175 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
177 ($form->{orphaned}) = $sth->fetchrow_array;
178 $form->{orphaned} = !$form->{orphaned};
181 # check if new account is active
182 $form->{new_chart_valid} = 0;
183 if ($form->{new_chart_id}) {
184 $query = qq|SELECT current_date-valid_from FROM chart
186 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
187 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
189 $form->{new_chart_valid} = 1;
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 # TODO: it should be forbidden to change an account to a heading if there
203 # have been bookings to this account in the past
205 my ($self, $myconfig, $form) = @_;
207 # connect to database, turn off AutoCommit
208 my $dbh = $form->dbconnect_noauto($myconfig);
210 # sanity check, can't have AR with AR_...
211 if ($form->{AR} || $form->{AP} || $form->{IC}) {
212 map { delete $form->{$_} }
213 qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice);
216 for (qw(AR_include_in_dropdown AP_include_in_dropdown)) {
217 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
221 foreach my $item ($form->{AR}, $form->{AR_amount},
222 $form->{AR_tax}, $form->{AR_paid},
223 $form->{AP}, $form->{AP_amount},
224 $form->{AP_tax}, $form->{AP_paid},
225 $form->{IC}, $form->{IC_sale},
226 $form->{IC_cogs}, $form->{IC_taxpart},
227 $form->{IC_income}, $form->{IC_expense},
228 $form->{IC_taxservice}
230 $form->{link} .= "${item}:" if ($item);
234 # strip blanks from accno
235 map { $form->{$_} =~ s/ //g; } qw(accno);
239 if ($form->{id} eq "NULL") {
248 my @values = ($form->{accno});
251 $query .= ' AND NOT id = ?';
252 push(@values, $form->{id});
255 my ($accno) = selectrow_query($form, $dbh, $query, @values);
258 $form->error($::locale->text('Account number not unique!'));
262 if (!$form->{id} || $form->{id} eq "") {
263 $query = qq|SELECT nextval('id')|;
264 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
265 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
266 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
274 # if charttype is heading make sure certain values are empty
275 # specifically, if charttype is changed from an existing account, empty the
276 # fields unnecessary for headings, so that e.g. heading doesn't appear in
277 # drop-down menues due to still having a valid "link" entry
279 if ( $form->{charttype} eq 'H' ) {
281 $form->{pos_bwa} = '';
282 $form->{pos_bilanz} = '';
283 $form->{pos_eur} = '';
284 $form->{new_chart_id} = '';
285 $form->{valid_from} = '';
288 $query = qq|UPDATE chart SET
304 $form->{description},
308 conv_i($form->{pos_bwa}),
309 conv_i($form->{pos_bilanz}),
310 conv_i($form->{pos_eur}),
311 conv_i($form->{new_chart_id}),
312 conv_date($form->{valid_from}),
313 ($form->{datevautomatik} eq 'T') ? 'true':'false',
320 do_query($form, $dbh, $query, @values);
326 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
330 for $tk_count (0 .. $MAX_TRIES) {
334 # Check if the account already exists, else cancel
336 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
338 last READTAXKEYS if ( $form->{'id'} == 0);
340 # check if there is a startdate
341 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
346 # Add valid taxkeys into the array
349 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
350 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
351 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
352 chart_id => conv_i($form->{"id"}),
353 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
354 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
361 for my $j (0 .. $#taxkeys){
362 if ( defined $taxkeys[$j]{'id'} ){
365 if ($taxkeys[$j]{'delete'}){
367 DELETE FROM taxkeys WHERE id = ?
370 @values = ($taxkeys[$j]{'id'});
372 do_query($form, $dbh, $query, @values);
381 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
389 $taxkeys[$j]{'tax_id'},
390 $taxkeys[$j]{'chart_id'},
391 $taxkeys[$j]{'tax_id'},
392 $taxkeys[$j]{'pos_ustva'},
393 $taxkeys[$j]{'startdate'},
396 do_query($form, $dbh, $query, @values);
402 INSERT INTO taxkeys (
409 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
412 $taxkeys[$j]{'tax_id'},
413 $taxkeys[$j]{'chart_id'},
414 $taxkeys[$j]{'tax_id'},
415 $taxkeys[$j]{'pos_ustva'},
416 $taxkeys[$j]{'startdate'},
419 do_query($form, $dbh, $query, @values);
424 # Update chart.taxkey_id to the latest from taxkeys for this chart.
430 WHERE taxkeys.chart_id = chart.id
431 ORDER BY startdate DESC
437 do_query($form, $dbh, $query, $form->{id});
440 my $rc = $dbh->commit;
443 $main::lxdebug->leave_sub();
449 $main::lxdebug->enter_sub();
451 my ($self, $myconfig, $form) = @_;
453 # connect to database, turn off AutoCommit
454 my $dbh = $form->dbconnect_noauto($myconfig);
456 my $query = qq|SELECT count(*) FROM acc_trans a
457 WHERE a.chart_id = ?|;
458 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
462 $main::lxdebug->leave_sub();
466 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
467 foreach my $type (qw(inventory income expense)) {
470 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
471 qq|WHERE ${type}_accno_id = ?|;
472 do_query($form, $dbh, $query, $form->{id});
475 $query = qq|DELETE FROM tax
477 do_query($form, $dbh, $query, $form->{id});
479 # delete chart of account record
480 $query = qq|DELETE FROM chart
482 do_query($form, $dbh, $query, $form->{id});
484 # delete account taxkeys
485 $query = qq|DELETE FROM taxkeys
487 do_query($form, $dbh, $query, $form->{id});
489 # commit and redirect
490 my $rc = $dbh->commit;
493 $main::lxdebug->leave_sub();
499 $main::lxdebug->enter_sub();
501 my ($self, $myconfig, $form) = @_;
503 # connect to database
504 my $dbh = $form->dbconnect($myconfig);
506 my $query = qq|SELECT id, lead
510 my $sth = $dbh->prepare($query);
511 $sth->execute || $form->dberror($query);
513 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
514 push @{ $form->{ALL} }, $ref;
520 $main::lxdebug->leave_sub();
524 $main::lxdebug->enter_sub();
526 my ($self, $myconfig, $form) = @_;
528 # connect to database
529 my $dbh = $form->dbconnect($myconfig);
532 qq|SELECT l.id, l.lead | .
535 my $sth = $dbh->prepare($query);
536 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
538 my $ref = $sth->fetchrow_hashref("NAME_lc");
540 map { $form->{$_} = $ref->{$_} } keys %$ref;
546 $main::lxdebug->leave_sub();
550 $main::lxdebug->enter_sub();
552 my ($self, $myconfig, $form) = @_;
555 # connect to database
556 my $dbh = $form->dbconnect($myconfig);
558 my @values = ($form->{description});
559 # id is the old record
561 $query = qq|UPDATE leads SET
564 push(@values, $form->{id});
566 $query = qq|INSERT INTO leads
570 do_query($form, $dbh, $query, @values);
574 $main::lxdebug->leave_sub();
578 $main::lxdebug->enter_sub();
580 my ($self, $myconfig, $form) = @_;
583 # connect to database
584 my $dbh = $form->dbconnect($myconfig);
586 $query = qq|DELETE FROM leads
588 do_query($form, $dbh, $query, $form->{id});
592 $main::lxdebug->leave_sub();
596 $main::lxdebug->enter_sub();
598 my ($self, $myconfig, $form, $return_list) = @_;
600 # connect to database
601 my $dbh = $form->dbconnect($myconfig);
604 "SELECT id, description, template_code, article_code, " .
605 " output_numberformat, output_dateformat, output_longdates " .
606 "FROM language ORDER BY description";
608 my $sth = $dbh->prepare($query);
609 $sth->execute || $form->dberror($query);
613 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
614 push(@{ $ary }, $ref);
620 $main::lxdebug->leave_sub();
630 $main::lxdebug->enter_sub();
632 my ($self, $myconfig, $form) = @_;
634 # connect to database
635 my $dbh = $form->dbconnect($myconfig);
638 "SELECT description, template_code, article_code, " .
639 " output_numberformat, output_dateformat, output_longdates " .
640 "FROM language WHERE id = ?";
641 my $sth = $dbh->prepare($query);
642 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
644 my $ref = $sth->fetchrow_hashref("NAME_lc");
646 map { $form->{$_} = $ref->{$_} } keys %$ref;
652 $main::lxdebug->leave_sub();
655 sub get_language_details {
656 $main::lxdebug->enter_sub();
658 my ($self, $myconfig, $form, $id) = @_;
660 # connect to database
661 my $dbh = $form->dbconnect($myconfig);
664 "SELECT template_code, " .
665 " output_numberformat, output_dateformat, output_longdates " .
666 "FROM language WHERE id = ?";
667 my @res = selectrow_query($form, $dbh, $query, $id);
670 $main::lxdebug->leave_sub();
676 $main::lxdebug->enter_sub();
678 my ($self, $myconfig, $form) = @_;
680 # connect to database
681 my $dbh = $form->dbconnect($myconfig);
682 my (@values, $query);
684 map({ push(@values, $form->{$_}); }
685 qw(description template_code article_code
686 output_numberformat output_dateformat output_longdates));
688 # id is the old record
691 "UPDATE language SET " .
692 " description = ?, template_code = ?, article_code = ?, " .
693 " output_numberformat = ?, output_dateformat = ?, " .
694 " output_longdates = ? " .
696 push(@values, $form->{id});
699 "INSERT INTO language (" .
700 " description, template_code, article_code, " .
701 " output_numberformat, output_dateformat, output_longdates" .
702 ") VALUES (?, ?, ?, ?, ?, ?)";
704 do_query($form, $dbh, $query, @values);
708 $main::lxdebug->leave_sub();
711 sub delete_language {
712 $main::lxdebug->enter_sub();
714 my ($self, $myconfig, $form) = @_;
717 # connect to database
718 my $dbh = $form->dbconnect_noauto($myconfig);
720 foreach my $table (qw(generic_translations units_language)) {
721 $query = qq|DELETE FROM $table WHERE language_id = ?|;
722 do_query($form, $dbh, $query, $form->{"id"});
725 $query = "DELETE FROM language WHERE id = ?";
726 do_query($form, $dbh, $query, $form->{"id"});
731 $main::lxdebug->leave_sub();
736 $main::lxdebug->enter_sub();
738 my ($self, $myconfig, $form) = @_;
740 # connect to database
741 my $dbh = $form->dbconnect($myconfig);
743 my $query = qq|SELECT id, description,
745 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
747 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
749 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
751 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
753 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
755 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
757 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
759 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
761 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
765 my $sth = $dbh->prepare($query);
766 $sth->execute || $form->dberror($query);
769 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
770 push @{ $form->{ALL} }, $ref;
776 $main::lxdebug->leave_sub();
779 sub get_buchungsgruppe {
780 $main::lxdebug->enter_sub();
782 my ($self, $myconfig, $form) = @_;
785 # connect to database
786 my $dbh = $form->dbconnect($myconfig);
790 qq|SELECT description, inventory_accno_id,
791 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
793 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
795 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
797 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
799 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
801 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
803 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
805 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
807 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
810 my $sth = $dbh->prepare($query);
811 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
813 my $ref = $sth->fetchrow_hashref("NAME_lc");
815 map { $form->{$_} = $ref->{$_} } keys %$ref;
820 qq|SELECT count(id) = 0 AS orphaned
822 WHERE buchungsgruppen_id = ?|;
823 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
826 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
828 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
829 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
832 $query = qq|SELECT c.accno, c.description, c.link, c.id,
833 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
834 FROM chart c, defaults d
835 WHERE c.link LIKE '%$module%'
839 my $sth = $dbh->prepare($query);
840 $sth->execute || $form->dberror($query);
841 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
842 foreach my $key (split(/:/, $ref->{link})) {
843 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
844 $form->{"std_inventory_accno_id"} = $ref->{"id"};
846 if ($key =~ /$module/) {
847 if ( ($ref->{id} eq $ref->{inventory_accno_id})
848 || ($ref->{id} eq $ref->{income_accno_id})
849 || ($ref->{id} eq $ref->{expense_accno_id})) {
850 push @{ $form->{"${module}_links"}{$key} },
851 { accno => $ref->{accno},
852 description => $ref->{description},
853 selected => "selected",
856 push @{ $form->{"${module}_links"}{$key} },
857 { accno => $ref->{accno},
858 description => $ref->{description},
870 $main::lxdebug->leave_sub();
873 sub save_buchungsgruppe {
874 $main::lxdebug->enter_sub();
876 my ($self, $myconfig, $form) = @_;
878 # connect to database
879 my $dbh = $form->dbconnect($myconfig);
881 my @values = ($form->{description}, $form->{inventory_accno_id},
882 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
883 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
884 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
885 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
889 # id is the old record
891 $query = qq|UPDATE buchungsgruppen SET
892 description = ?, inventory_accno_id = ?,
893 income_accno_id_0 = ?, expense_accno_id_0 = ?,
894 income_accno_id_1 = ?, expense_accno_id_1 = ?,
895 income_accno_id_2 = ?, expense_accno_id_2 = ?,
896 income_accno_id_3 = ?, expense_accno_id_3 = ?
898 push(@values, $form->{id});
900 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
901 my ($sortkey) = $dbh->selectrow_array($query);
902 $form->dberror($query) if ($dbh->err);
903 push(@values, $sortkey);
904 $query = qq|INSERT INTO buchungsgruppen
905 (description, inventory_accno_id,
906 income_accno_id_0, expense_accno_id_0,
907 income_accno_id_1, expense_accno_id_1,
908 income_accno_id_2, expense_accno_id_2,
909 income_accno_id_3, expense_accno_id_3,
911 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
913 do_query($form, $dbh, $query, @values);
917 $main::lxdebug->leave_sub();
920 sub delete_buchungsgruppe {
921 $main::lxdebug->enter_sub();
923 my ($self, $myconfig, $form) = @_;
925 # connect to database
926 my $dbh = $form->dbconnect($myconfig);
928 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
929 do_query($form, $dbh, $query, $form->{id});
933 $main::lxdebug->leave_sub();
937 $main::lxdebug->enter_sub();
939 my ($self, $myconfig, $form, $table) = @_;
941 # connect to database
942 my $dbh = $form->get_standard_dbh($myconfig);
946 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
947 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
948 my @values = ($form->{"id1"}, $form->{"id2"});
949 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
951 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
952 my $sth = prepare_query($form, $dbh, $query);
954 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
955 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
961 $main::lxdebug->leave_sub();
964 sub prepare_template_filename {
965 $main::lxdebug->enter_sub();
967 my ($self, $myconfig, $form) = @_;
969 my ($filename, $display_filename);
971 if ($form->{type} eq "stylesheet") {
972 $filename = "css/$myconfig->{stylesheet}";
973 $display_filename = $myconfig->{stylesheet};
976 $filename = $form->{formname};
978 if ($form->{language}) {
979 my ($id, $template_code) = split(/--/, $form->{language});
980 $filename .= "_${template_code}";
983 if ($form->{printer}) {
984 my ($id, $template_code) = split(/--/, $form->{printer});
985 $filename .= "_${template_code}";
988 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
989 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
990 $filename =~ s|.*/||;
992 $display_filename = $filename;
993 $filename = "$myconfig->{templates}/$filename";
996 $main::lxdebug->leave_sub();
998 return ($filename, $display_filename);
1003 $main::lxdebug->enter_sub();
1005 my ($self, $filename) = @_;
1007 my ($content, $lines) = ("", 0);
1011 if (open(TEMPLATE, $filename)) {
1012 while (<TEMPLATE>) {
1019 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1021 $main::lxdebug->leave_sub();
1023 return ($content, $lines);
1027 $main::lxdebug->enter_sub();
1029 my ($self, $filename, $content) = @_;
1035 if (open(TEMPLATE, ">", $filename)) {
1036 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1037 $content =~ s/\r\n/\n/g;
1038 print(TEMPLATE $content);
1044 $main::lxdebug->leave_sub();
1050 $main::lxdebug->enter_sub();
1055 my $myconfig = \%main::myconfig;
1056 my $form = $main::form;
1058 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1061 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1063 # these defaults are database wide
1066 qq|UPDATE defaults SET
1067 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1068 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1069 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1070 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1089 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1090 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1091 $form->{invnumber}, $form->{cnnumber},
1092 $form->{sonumber}, $form->{ponumber},
1093 $form->{sqnumber}, $form->{rfqnumber},
1094 $form->{customernumber}, $form->{vendornumber},
1095 $form->{articlenumber}, $form->{servicenumber},
1096 $form->{assemblynumber},
1097 $form->{sdonumber}, $form->{pdonumber},
1098 $form->{businessnumber}, $form->{weightunit},
1099 conv_i($form->{language_id}));
1100 do_query($form, $dbh, $query, @values);
1102 $main::lxdebug->message(0, "es gibt rowcount: " . $form->{rowcount});
1104 for my $i (1..$form->{rowcount}) {
1105 if ($form->{"curr_$i"} ne $form->{"old_curr_$i"}) {
1106 $query = qq|UPDATE currencies SET curr = '| . $form->{"curr_$i"} . qq|' WHERE curr = '| . $form->{"old_curr_$i"} . qq|'|;
1107 do_query($form, $dbh, $query);
1111 if (length($form->{new_curr}) > 0) {
1112 $query = qq|INSERT INTO currencies (curr) VALUES ('| . $form->{new_curr} . qq|')|;
1113 do_query($form, $dbh, $query);
1118 $main::lxdebug->leave_sub();
1122 sub save_preferences {
1123 $main::lxdebug->enter_sub();
1125 my ($self, $myconfig, $form) = @_;
1127 my $dbh = $form->get_standard_dbh($myconfig);
1129 my ($businessnumber) = selectrow_query($form, $dbh, qq|SELECT businessnumber FROM defaults|);
1132 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1133 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1135 my $rc = $dbh->commit();
1137 $form->{businessnumber} = $businessnumber;
1139 $myconfig = User->new(login => $form->{login});
1141 foreach my $item (keys %$form) {
1142 $myconfig->{$item} = $form->{$item};
1145 $myconfig->save_member;
1147 my $auth = $main::auth;
1149 $main::lxdebug->leave_sub();
1155 $main::lxdebug->enter_sub();
1160 my $myconfig = \%main::myconfig;
1161 my $form = $main::form;
1163 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1165 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1167 $defaults->{weightunit} ||= 'kg';
1169 $main::lxdebug->leave_sub();
1174 sub defaultaccounts {
1175 $main::lxdebug->enter_sub();
1177 my ($self, $myconfig, $form) = @_;
1179 # connect to database
1180 my $dbh = $form->dbconnect($myconfig);
1182 # get defaults from defaults table
1183 my $query = qq|SELECT * FROM defaults|;
1184 my $sth = $dbh->prepare($query);
1185 $sth->execute || $form->dberror($query);
1187 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1188 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1189 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1190 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1191 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1192 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1193 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1195 $form->{defaults}{weightunit} ||= 'kg';
1199 $query = qq|SELECT c.id, c.accno, c.description, c.link
1201 WHERE c.link LIKE '%IC%'
1203 $sth = $dbh->prepare($query);
1204 $sth->execute || $self->dberror($query);
1206 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1207 foreach my $key (split(/:/, $ref->{link})) {
1210 if ($key =~ /cogs/) {
1211 $nkey = "IC_expense";
1213 if ($key =~ /sale/) {
1214 $nkey = "IC_income";
1216 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1218 description => $ref->{description}
1225 $query = qq|SELECT c.id, c.accno, c.description
1227 WHERE c.category = 'I'
1228 AND c.charttype = 'A'
1230 $sth = $dbh->prepare($query);
1231 $sth->execute || $self->dberror($query);
1233 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1234 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1236 description => $ref->{description}
1241 $query = qq|SELECT c.id, c.accno, c.description
1243 WHERE c.category = 'E'
1244 AND c.charttype = 'A'
1246 $sth = $dbh->prepare($query);
1247 $sth->execute || $self->dberror($query);
1249 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1250 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1252 description => $ref->{description}
1257 # now get the tax rates and numbers
1258 $query = qq|SELECT c.id, c.accno, c.description,
1259 t.rate * 100 AS rate, t.taxnumber
1261 WHERE c.id = t.chart_id|;
1263 $sth = $dbh->prepare($query);
1264 $sth->execute || $form->dberror($query);
1266 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1267 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1268 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1269 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1270 if $ref->{taxnumber};
1271 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1273 # Abfrage für Standard Umlaufvermögenskonto
1275 qq|SELECT id, accno, description, link | .
1277 qq|WHERE link LIKE ? |.
1279 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1280 $sth->execute || $form->dberror($query);#
1281 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1282 foreach my $item (split(/:/, $ref->{link})) {
1283 if ($item eq "AR_paid") {
1284 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1286 description => $ref->{description}
1295 $query = qq|SELECT curr FROM currencies ORDER BY id|;
1297 $form->{CURRENCIES} = [];
1299 $sth = prepare_execute_query($form, $dbh, $query);
1300 $sth->execute || $form->dberror($query);
1301 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1302 push @{ $form->{ CURRENCIES } } , $ref;
1306 #Which of them is the default currency?
1307 $query = qq|SELECT curr AS defaultcurrency FROM currencies WHERE id = (SELECT curr FROM defaults LIMIT 1);|;
1308 $sth = $dbh->prepare($query);
1309 $sth->execute || $form->dberror($query);
1311 $form->{defaultcurrency} = ($sth->fetchrow_hashref("NAME_lc"))->{defaultcurrency};
1317 $main::lxdebug->leave_sub();
1321 $main::lxdebug->enter_sub();
1323 my ($self, $myconfig, $form) = @_;
1325 my $dbh = $form->dbconnect($myconfig);
1327 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1328 my $sth = $dbh->prepare($query);
1329 $sth->execute || $form->dberror($query);
1331 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1337 $main::lxdebug->leave_sub();
1341 $main::lxdebug->enter_sub();
1343 my ($self, $myconfig, $form) = @_;
1345 my $dbh = $form->dbconnect($myconfig);
1347 my ($query, @values);
1349 if ($form->{revtrans}) {
1350 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1352 } elsif ($form->{closedto}) {
1353 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1354 @values = (conv_date($form->{closedto}));
1357 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1360 # set close in defaults
1361 do_query($form, $dbh, $query, @values);
1365 $main::lxdebug->leave_sub();
1369 my ($self, $units, $unit_name, $factor) = @_;
1371 $factor = 1 unless ($factor);
1373 my $unit = $units->{$unit_name};
1375 if (!defined($unit) || !$unit->{"base_unit"} ||
1376 ($unit_name eq $unit->{"base_unit"})) {
1377 return ($unit_name, $factor);
1380 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1383 sub retrieve_units {
1384 $main::lxdebug->enter_sub();
1386 my ($self, $myconfig, $form, $prefix) = @_;
1389 my $dbh = $form->get_standard_dbh;
1391 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1393 my $sth = prepare_execute_query($form, $dbh, $query);
1396 while (my $ref = $sth->fetchrow_hashref()) {
1397 $units->{$ref->{"name"}} = $ref;
1401 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1402 $sth = $dbh->prepare($query_lang);
1403 $sth->execute() || $form->dberror($query_lang);
1405 while (my $ref = $sth->fetchrow_hashref()) {
1406 push(@languages, $ref);
1410 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1411 "FROM units_language ul " .
1412 "LEFT JOIN language l ON ul.language_id = l.id " .
1413 "WHERE ul.unit = ?";
1414 $sth = $dbh->prepare($query_lang);
1416 foreach my $unit (values(%{$units})) {
1417 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1419 $unit->{"LANGUAGES"} = {};
1420 foreach my $lang (@languages) {
1421 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1424 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1425 while (my $ref = $sth->fetchrow_hashref()) {
1426 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1431 $main::lxdebug->leave_sub();
1436 sub retrieve_all_units {
1437 $main::lxdebug->enter_sub();
1441 if (!$::request->{cache}{all_units}) {
1442 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1445 $main::lxdebug->leave_sub();
1447 return $::request->{cache}{all_units};
1451 sub translate_units {
1452 $main::lxdebug->enter_sub();
1454 my ($self, $form, $template_code, $unit, $amount) = @_;
1456 my $units = $self->retrieve_units(\%main::myconfig, $form);
1458 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1459 my $new_unit = $unit;
1461 if (($amount != 1) && $h->{"localized_plural"}) {
1462 $new_unit = $h->{"localized_plural"};
1463 } elsif ($h->{"localized"}) {
1464 $new_unit = $h->{"localized"};
1468 $main::lxdebug->leave_sub();
1474 $main::lxdebug->enter_sub();
1476 my ($self, $myconfig, $form, $units) = @_;
1478 my $dbh = $form->dbconnect($myconfig);
1480 map({ $_->{"in_use"} = 0; } values(%{$units}));
1482 foreach my $unit (values(%{$units})) {
1483 my $base_unit = $unit->{"original_base_unit"};
1484 while ($base_unit) {
1485 $units->{$base_unit}->{"in_use"} = 1;
1486 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1487 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1488 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1492 foreach my $unit (values(%{$units})) {
1493 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1495 foreach my $table (qw(parts invoice orderitems)) {
1496 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1498 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1499 $query .= "= " . $dbh->quote($unit->{"name"});
1501 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1502 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1505 my ($count) = $dbh->selectrow_array($query);
1506 $form->dberror($query) if ($dbh->err);
1509 $unit->{"in_use"} = 1;
1517 $main::lxdebug->leave_sub();
1520 sub convertible_units {
1521 $main::lxdebug->enter_sub();
1525 my $filter_unit = shift;
1526 my $not_smaller = shift;
1528 my $conv_units = [];
1530 $filter_unit = $units->{$filter_unit};
1532 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1533 my $unit = $units->{$name};
1535 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1536 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1537 push @{$conv_units}, $unit;
1541 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1543 $main::lxdebug->leave_sub();
1548 # if $a is translatable to $b, return the factor between them.
1551 $main::lxdebug->enter_sub(2);
1552 my ($this, $a, $b, $all_units) = @_;
1555 $all_units = $this->retrieve_all_units;
1558 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1559 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1560 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1561 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1564 sub unit_select_data {
1565 $main::lxdebug->enter_sub();
1567 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1572 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1575 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1576 if (!$convertible_into ||
1577 ($units->{$convertible_into} &&
1578 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1579 push @{$select}, { "name" => $unit,
1580 "base_unit" => $units->{$unit}->{"base_unit"},
1581 "factor" => $units->{$unit}->{"factor"},
1582 "selected" => ($unit eq $selected) ? "selected" : "" };
1586 $main::lxdebug->leave_sub();
1591 sub unit_select_html {
1592 $main::lxdebug->enter_sub();
1594 my ($self, $units, $name, $selected, $convertible_into) = @_;
1596 my $select = "<select name=${name}>";
1598 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1599 if (!$convertible_into ||
1600 ($units->{$convertible_into} &&
1601 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1602 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1605 $select .= "</select>";
1607 $main::lxdebug->leave_sub();
1613 $main::lxdebug->enter_sub();
1617 my $units = $self->retrieve_all_units();
1622 while (2 <= scalar(@_)) {
1623 my $qty = shift(@_);
1624 my $unit = $units->{shift(@_)};
1626 croak "No unit defined with name $unit" if (!defined $unit);
1629 $base_unit = $unit->{base_unit};
1630 } elsif ($base_unit ne $unit->{base_unit}) {
1631 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1634 $sum += $qty * $unit->{factor};
1637 $main::lxdebug->leave_sub();
1639 return wantarray ? ($sum, $base_unit) : $sum;
1643 $main::lxdebug->enter_sub();
1645 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1647 my $dbh = $form->dbconnect_noauto($myconfig);
1649 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1650 my ($sortkey) = selectrow_query($form, $dbh, $query);
1652 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1653 "VALUES (?, ?, ?, ?)";
1654 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1657 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1658 my $sth = $dbh->prepare($query);
1659 foreach my $lang (@{$languages}) {
1660 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1661 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1669 $main::lxdebug->leave_sub();
1673 $main::lxdebug->enter_sub();
1675 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1677 my $dbh = $form->dbconnect_noauto($myconfig);
1679 my ($base_unit, $unit, $sth, $query);
1681 $query = "DELETE FROM units_language";
1682 $dbh->do($query) || $form->dberror($query);
1684 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1685 $query = "DELETE FROM units WHERE name IN (";
1686 map({ $query .= "?," } @{$delete_units});
1687 substr($query, -1, 1) = ")";
1688 $dbh->do($query, undef, @{$delete_units}) ||
1689 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1692 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1693 $sth = $dbh->prepare($query);
1695 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1696 my $sth_lang = $dbh->prepare($query_lang);
1698 foreach $unit (values(%{$units})) {
1699 $unit->{"depth"} = 0;
1700 my $base_unit = $unit;
1701 while ($base_unit->{"base_unit"}) {
1703 $base_unit = $units->{$base_unit->{"base_unit"}};
1707 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1708 if ($unit->{"LANGUAGES"}) {
1709 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1710 next unless ($lang->{"id"} && $lang->{"localized"});
1711 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1712 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1716 next if ($unit->{"unchanged_unit"});
1718 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1719 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1723 $sth_lang->finish();
1727 $main::lxdebug->leave_sub();
1731 $main::lxdebug->enter_sub();
1733 my ($self, $myconfig, $form) = @_;
1735 # connect to database
1736 my $dbh = $form->dbconnect($myconfig);
1738 my $query = qq|SELECT
1742 round(t.rate * 100, 2) AS rate,
1743 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1744 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1746 ORDER BY taxkey, rate|;
1748 my $sth = $dbh->prepare($query);
1749 $sth->execute || $form->dberror($query);
1752 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1753 push @{ $form->{TAX} }, $ref;
1759 $main::lxdebug->leave_sub();
1762 sub get_tax_accounts {
1763 $main::lxdebug->enter_sub();
1765 my ($self, $myconfig, $form) = @_;
1767 my $dbh = $form->dbconnect($myconfig);
1769 # get Accounts from chart
1770 my $query = qq{ SELECT
1772 accno || ' - ' || description AS taxaccount
1774 WHERE link LIKE '%_tax%'
1778 my $sth = $dbh->prepare($query);
1779 $sth->execute || $form->dberror($query);
1781 $form->{ACCOUNTS} = [];
1782 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1783 push @{ $form->{ACCOUNTS} }, $ref;
1790 $main::lxdebug->leave_sub();
1794 $main::lxdebug->enter_sub();
1796 my ($self, $myconfig, $form) = @_;
1798 # connect to database
1799 my $dbh = $form->dbconnect($myconfig);
1801 my $query = qq|SELECT
1804 round(rate * 100, 2) AS rate,
1807 (id IN (SELECT tax_id
1808 FROM acc_trans)) AS tax_already_used
1812 my $sth = $dbh->prepare($query);
1813 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1815 my $ref = $sth->fetchrow_hashref("NAME_lc");
1817 map { $form->{$_} = $ref->{$_} } keys %$ref;
1821 # see if it is used by a taxkey
1822 $query = qq|SELECT count(*) FROM taxkeys
1823 WHERE tax_id = ? AND chart_id >0|;
1825 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1827 $form->{orphaned} = !$form->{orphaned};
1830 if (!$form->{orphaned} ) {
1831 $query = qq|SELECT DISTINCT c.id, c.accno
1833 JOIN tax t ON (t.id = tk.tax_id)
1834 JOIN chart c ON (c.id = tk.chart_id)
1835 WHERE tk.tax_id = ?|;
1837 $sth = $dbh->prepare($query);
1838 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1840 $form->{TAXINUSE} = [];
1841 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1842 push @{ $form->{TAXINUSE} }, $ref;
1850 $main::lxdebug->leave_sub();
1854 $main::lxdebug->enter_sub();
1856 my ($self, $myconfig, $form) = @_;
1859 # connect to database
1860 my $dbh = $form->get_standard_dbh($myconfig);
1862 $form->{rate} = $form->{rate} / 100;
1864 my $chart_categories = '';
1865 $chart_categories .= 'A' if $form->{asset};
1866 $chart_categories .= 'L' if $form->{liability};
1867 $chart_categories .= 'Q' if $form->{equity};
1868 $chart_categories .= 'I' if $form->{revenue};
1869 $chart_categories .= 'E' if $form->{expense};
1870 $chart_categories .= 'C' if $form->{costs};
1872 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id}, $chart_categories);
1873 if ($form->{id} ne "") {
1874 $query = qq|UPDATE tax SET
1879 taxnumber = (SELECT accno FROM chart WHERE id= ? ),
1880 chart_categories = ?
1882 push(@values, $form->{id});
1886 $query = qq|INSERT INTO tax (
1894 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ? )|;
1896 do_query($form, $dbh, $query, @values);
1900 $main::lxdebug->leave_sub();
1904 $main::lxdebug->enter_sub();
1906 my ($self, $myconfig, $form) = @_;
1909 # connect to database
1910 my $dbh = $form->get_standard_dbh($myconfig);
1912 $query = qq|DELETE FROM tax
1914 do_query($form, $dbh, $query, $form->{id});
1918 $main::lxdebug->leave_sub();
1921 sub save_price_factor {
1922 $main::lxdebug->enter_sub();
1924 my ($self, $myconfig, $form) = @_;
1926 # connect to database
1927 my $dbh = $form->get_standard_dbh($myconfig);
1930 my @values = ($form->{description}, conv_i($form->{factor}));
1933 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1934 push @values, conv_i($form->{id});
1937 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1940 do_query($form, $dbh, $query, @values);
1944 $main::lxdebug->leave_sub();
1947 sub get_all_price_factors {
1948 $main::lxdebug->enter_sub();
1950 my ($self, $myconfig, $form) = @_;
1952 # connect to database
1953 my $dbh = $form->get_standard_dbh($myconfig);
1955 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1957 $main::lxdebug->leave_sub();
1960 sub get_price_factor {
1961 $main::lxdebug->enter_sub();
1963 my ($self, $myconfig, $form) = @_;
1965 # connect to database
1966 my $dbh = $form->get_standard_dbh($myconfig);
1968 my $query = qq|SELECT description, factor,
1969 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1970 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1971 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1972 FROM price_factors WHERE id = ?|;
1974 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1976 $main::lxdebug->leave_sub();
1979 sub delete_price_factor {
1980 $main::lxdebug->enter_sub();
1982 my ($self, $myconfig, $form) = @_;
1984 # connect to database
1985 my $dbh = $form->get_standard_dbh($myconfig);
1987 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1990 $main::lxdebug->leave_sub();
1993 sub save_warehouse {
1994 $main::lxdebug->enter_sub();
1996 my ($self, $myconfig, $form) = @_;
1998 # connect to database
1999 my $dbh = $form->get_standard_dbh($myconfig);
2001 my ($query, @values, $sth);
2004 $query = qq|SELECT nextval('id')|;
2005 ($form->{id}) = selectrow_query($form, $dbh, $query);
2007 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2008 do_query($form, $dbh, $query, $form->{id});
2011 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2012 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2014 if (0 < $form->{number_of_new_bins}) {
2015 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2016 $sth = prepare_query($form, $dbh, $query);
2018 foreach my $i (1..$form->{number_of_new_bins}) {
2019 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2027 $main::lxdebug->leave_sub();
2031 $main::lxdebug->enter_sub();
2033 my ($self, $myconfig, $form) = @_;
2035 # connect to database
2036 my $dbh = $form->get_standard_dbh($myconfig);
2038 my ($query, @values, $commit_necessary, $sth);
2040 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2043 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2044 do_query($form, $dbh, $query, @values);
2046 $commit_necessary = 1;
2049 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2050 $sth = prepare_query($form, $dbh, $query);
2052 foreach my $row (1..$form->{rowcount}) {
2053 next if ($form->{"delete_${row}"});
2055 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2057 $commit_necessary = 1;
2062 $dbh->commit() if ($commit_necessary);
2064 $main::lxdebug->leave_sub();
2067 sub delete_warehouse {
2068 $main::lxdebug->enter_sub();
2070 my ($self, $myconfig, $form) = @_;
2072 # connect to database
2073 my $dbh = $form->get_standard_dbh($myconfig);
2075 my $id = conv_i($form->{id});
2076 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|;
2077 my ($count) = selectrow_query($form, $dbh, $query, $id);
2080 $main::lxdebug->leave_sub();
2084 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2085 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2089 $main::lxdebug->leave_sub();
2094 sub get_all_warehouses {
2095 $main::lxdebug->enter_sub();
2097 my ($self, $myconfig, $form) = @_;
2099 # connect to database
2100 my $dbh = $form->get_standard_dbh($myconfig);
2102 my $query = qq|SELECT w.id, w.description, w.invalid,
2103 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2105 ORDER BY w.sortkey|;
2107 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2109 $main::lxdebug->leave_sub();
2113 $main::lxdebug->enter_sub();
2115 my ($self, $myconfig, $form) = @_;
2117 # connect to database
2118 my $dbh = $form->get_standard_dbh($myconfig);
2120 my $id = conv_i($form->{id});
2121 my $query = qq|SELECT w.description, w.invalid
2125 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2127 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2129 $query = qq|SELECT b.*, EXISTS
2130 (SELECT i.warehouse_id
2132 WHERE i.bin_id = b.id
2136 WHERE b.warehouse_id = ?|;
2138 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2140 $main::lxdebug->leave_sub();