1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Administration module
36 #======================================================================
48 $main::lxdebug->enter_sub();
50 my ($self, $myconfig, $form) = @_;
53 my $dbh = $form->dbconnect($myconfig);
55 SELECT c.accno, c.description, c.charttype, c.category,
56 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
57 c.pos_bwa, datevautomatik,
58 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
59 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
62 ON (c.id=tk.chart_id AND tk.id =
63 (SELECT id FROM taxkeys
64 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
65 ORDER BY startdate DESC LIMIT 1))
70 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
71 my $sth = $dbh->prepare($query);
72 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
74 my $ref = $sth->fetchrow_hashref("NAME_lc");
76 foreach my $key (keys %$ref) {
77 $form->{"$key"} = $ref->{"$key"};
82 # get default accounts
83 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
85 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
86 $sth = $dbh->prepare($query);
87 $sth->execute || $form->dberror($query);
89 $ref = $sth->fetchrow_hashref("NAME_lc");
91 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
97 # get taxkeys and description
101 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
103 id||'--'||taxkey AS tax,
106 FROM tax ORDER BY taxkey
108 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
109 $sth = $dbh->prepare($query);
110 $sth->execute || $form->dberror($query);
112 $form->{TAXKEY} = [];
114 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
115 push @{ $form->{TAXKEY} }, $ref;
121 $query = qq|SELECT id, accno,description
125 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
126 $sth = $dbh->prepare($query);
127 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
129 $form->{NEWACCOUNT} = [];
130 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
131 push @{ $form->{NEWACCOUNT} }, $ref;
136 # get the taxkeys of account
150 LEFT JOIN tax t ON (t.id = tk.tax_id)
151 LEFT JOIN chart c ON (c.id = t.chart_id)
153 WHERE tk.chart_id = ?
154 ORDER BY startdate DESC
156 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
157 $sth = $dbh->prepare($query);
159 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
161 $form->{ACCOUNT_TAXKEYS} = [];
163 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
170 # check if we have any transactions
171 $query = qq|SELECT a.trans_id FROM acc_trans a
172 WHERE a.chart_id = ?|;
173 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
174 $sth = $dbh->prepare($query);
175 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
177 ($form->{orphaned}) = $sth->fetchrow_array;
178 $form->{orphaned} = !$form->{orphaned};
181 # check if new account is active
182 $form->{new_chart_valid} = 0;
183 if ($form->{new_chart_id}) {
184 $query = qq|SELECT current_date-valid_from FROM chart
186 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
187 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
189 $form->{new_chart_valid} = 1;
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 # TODO: it should be forbidden to change an account to a heading if there
203 # have been bookings to this account in the past
205 my ($self, $myconfig, $form) = @_;
207 # connect to database, turn off AutoCommit
208 my $dbh = $form->dbconnect_noauto($myconfig);
210 # sanity check, can't have AR with AR_...
211 if ($form->{AR} || $form->{AP} || $form->{IC}) {
212 map { delete $form->{$_} }
213 qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice);
216 if ($form->{AR_include_in_dropdown}) {
217 $form->{$form->{AR_include_in_dropdown}} = $form->{AR_include_in_dropdown};
219 if ($form->{AP_include_in_dropdown}) {
220 $form->{$form->{AP_include_in_dropdown}} = $form->{AP_include_in_dropdown};
224 foreach my $item ($form->{AR}, $form->{AR_amount},
225 $form->{AR_tax}, $form->{AR_paid},
226 $form->{AP}, $form->{AP_amount},
227 $form->{AP_tax}, $form->{AP_paid},
228 $form->{IC}, $form->{IC_sale},
229 $form->{IC_cogs}, $form->{IC_taxpart},
230 $form->{IC_income}, $form->{IC_expense},
231 $form->{IC_taxservice}
233 $form->{link} .= "${item}:" if ($item);
237 # strip blanks from accno
238 map { $form->{$_} =~ s/ //g; } qw(accno);
242 if ($form->{id} eq "NULL") {
246 if (!$form->{id} || $form->{id} eq "") {
247 $query = qq|SELECT nextval('id')|;
248 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
249 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
250 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
258 # if charttype is heading make sure certain values are empty
259 # specifically, if charttype is changed from an existing account, empty the
260 # fields unnecessary for headings, so that e.g. heading doesn't appear in
261 # drop-down menues due to still having a valid "link" entry
263 if ( $form->{charttype} eq 'H' ) {
265 $form->{pos_bwa} = '';
266 $form->{pos_bilanz} = '';
267 $form->{pos_eur} = '';
268 $form->{new_chart_id} = '';
269 $form->{valid_from} = '';
277 my ($accno) = selectrow_query($form, $dbh, $query, $form->{accno});
280 $form->error($::locale->text('Account number not unique!'));
283 $query = qq|UPDATE chart SET
299 $form->{description},
303 conv_i($form->{pos_bwa}),
304 conv_i($form->{pos_bilanz}),
305 conv_i($form->{pos_eur}),
306 conv_i($form->{new_chart_id}),
307 conv_date($form->{valid_from}),
308 ($form->{datevautomatik} eq 'T') ? 'true':'false',
315 do_query($form, $dbh, $query, @values);
321 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
325 for $tk_count (0 .. $MAX_TRIES) {
329 # Check if the account already exists, else cancel
331 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
333 last READTAXKEYS if ( $form->{'id'} == 0);
335 # check if there is a startdate
336 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
341 # Add valid taxkeys into the array
344 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
345 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
346 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
347 chart_id => conv_i($form->{"id"}),
348 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
349 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
356 for my $j (0 .. $#taxkeys){
357 if ( defined $taxkeys[$j]{'id'} ){
360 if ($taxkeys[$j]{'delete'}){
362 DELETE FROM taxkeys WHERE id = ?
365 @values = ($taxkeys[$j]{'id'});
367 do_query($form, $dbh, $query, @values);
376 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
384 $taxkeys[$j]{'tax_id'},
385 $taxkeys[$j]{'chart_id'},
386 $taxkeys[$j]{'tax_id'},
387 $taxkeys[$j]{'pos_ustva'},
388 $taxkeys[$j]{'startdate'},
391 do_query($form, $dbh, $query, @values);
397 INSERT INTO taxkeys (
404 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
407 $taxkeys[$j]{'tax_id'},
408 $taxkeys[$j]{'chart_id'},
409 $taxkeys[$j]{'tax_id'},
410 $taxkeys[$j]{'pos_ustva'},
411 $taxkeys[$j]{'startdate'},
414 do_query($form, $dbh, $query, @values);
419 # Update chart.taxkey_id to the latest from taxkeys for this chart.
425 WHERE taxkeys.chart_id = chart.id
426 ORDER BY startdate DESC
432 do_query($form, $dbh, $query, $form->{id});
435 my $rc = $dbh->commit;
438 $main::lxdebug->leave_sub();
444 $main::lxdebug->enter_sub();
446 my ($self, $myconfig, $form) = @_;
448 # connect to database, turn off AutoCommit
449 my $dbh = $form->dbconnect_noauto($myconfig);
451 my $query = qq|SELECT count(*) FROM acc_trans a
452 WHERE a.chart_id = ?|;
453 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
457 $main::lxdebug->leave_sub();
461 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
462 foreach my $type (qw(inventory income expense)) {
465 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
466 qq|WHERE ${type}_accno_id = ?|;
467 do_query($form, $dbh, $query, $form->{id});
470 foreach my $table (qw(partstax customertax vendortax tax)) {
471 $query = qq|DELETE FROM $table
473 do_query($form, $dbh, $query, $form->{id});
476 # delete chart of account record
477 $query = qq|DELETE FROM chart
479 do_query($form, $dbh, $query, $form->{id});
481 # delete account taxkeys
482 $query = qq|DELETE FROM taxkeys
484 do_query($form, $dbh, $query, $form->{id});
486 # commit and redirect
487 my $rc = $dbh->commit;
490 $main::lxdebug->leave_sub();
496 $main::lxdebug->enter_sub();
498 my ($self, $myconfig, $form) = @_;
500 # connect to database
501 my $dbh = $form->dbconnect($myconfig);
503 my $query = qq|SELECT id, lead
507 my $sth = $dbh->prepare($query);
508 $sth->execute || $form->dberror($query);
510 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
511 push @{ $form->{ALL} }, $ref;
517 $main::lxdebug->leave_sub();
521 $main::lxdebug->enter_sub();
523 my ($self, $myconfig, $form) = @_;
525 # connect to database
526 my $dbh = $form->dbconnect($myconfig);
529 qq|SELECT l.id, l.lead | .
532 my $sth = $dbh->prepare($query);
533 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
535 my $ref = $sth->fetchrow_hashref("NAME_lc");
537 map { $form->{$_} = $ref->{$_} } keys %$ref;
543 $main::lxdebug->leave_sub();
547 $main::lxdebug->enter_sub();
549 my ($self, $myconfig, $form) = @_;
552 # connect to database
553 my $dbh = $form->dbconnect($myconfig);
555 my @values = ($form->{description});
556 # id is the old record
558 $query = qq|UPDATE leads SET
561 push(@values, $form->{id});
563 $query = qq|INSERT INTO leads
567 do_query($form, $dbh, $query, @values);
571 $main::lxdebug->leave_sub();
575 $main::lxdebug->enter_sub();
577 my ($self, $myconfig, $form) = @_;
580 # connect to database
581 my $dbh = $form->dbconnect($myconfig);
583 $query = qq|DELETE FROM leads
585 do_query($form, $dbh, $query, $form->{id});
589 $main::lxdebug->leave_sub();
593 $main::lxdebug->enter_sub();
595 my ($self, $myconfig, $form, $return_list) = @_;
597 # connect to database
598 my $dbh = $form->dbconnect($myconfig);
601 "SELECT id, description, template_code, article_code, " .
602 " output_numberformat, output_dateformat, output_longdates " .
603 "FROM language ORDER BY description";
605 my $sth = $dbh->prepare($query);
606 $sth->execute || $form->dberror($query);
610 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
611 push(@{ $ary }, $ref);
617 $main::lxdebug->leave_sub();
627 $main::lxdebug->enter_sub();
629 my ($self, $myconfig, $form) = @_;
631 # connect to database
632 my $dbh = $form->dbconnect($myconfig);
635 "SELECT description, template_code, article_code, " .
636 " output_numberformat, output_dateformat, output_longdates " .
637 "FROM language WHERE id = ?";
638 my $sth = $dbh->prepare($query);
639 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
641 my $ref = $sth->fetchrow_hashref("NAME_lc");
643 map { $form->{$_} = $ref->{$_} } keys %$ref;
649 $main::lxdebug->leave_sub();
652 sub get_language_details {
653 $main::lxdebug->enter_sub();
655 my ($self, $myconfig, $form, $id) = @_;
657 # connect to database
658 my $dbh = $form->dbconnect($myconfig);
661 "SELECT template_code, " .
662 " output_numberformat, output_dateformat, output_longdates " .
663 "FROM language WHERE id = ?";
664 my @res = selectrow_query($form, $dbh, $query, $id);
667 $main::lxdebug->leave_sub();
673 $main::lxdebug->enter_sub();
675 my ($self, $myconfig, $form) = @_;
677 # connect to database
678 my $dbh = $form->dbconnect($myconfig);
679 my (@values, $query);
681 map({ push(@values, $form->{$_}); }
682 qw(description template_code article_code
683 output_numberformat output_dateformat output_longdates));
685 # id is the old record
688 "UPDATE language SET " .
689 " description = ?, template_code = ?, article_code = ?, " .
690 " output_numberformat = ?, output_dateformat = ?, " .
691 " output_longdates = ? " .
693 push(@values, $form->{id});
696 "INSERT INTO language (" .
697 " description, template_code, article_code, " .
698 " output_numberformat, output_dateformat, output_longdates" .
699 ") VALUES (?, ?, ?, ?, ?, ?)";
701 do_query($form, $dbh, $query, @values);
705 $main::lxdebug->leave_sub();
708 sub delete_language {
709 $main::lxdebug->enter_sub();
711 my ($self, $myconfig, $form) = @_;
714 # connect to database
715 my $dbh = $form->dbconnect_noauto($myconfig);
717 foreach my $table (qw(generic_translations units_language)) {
718 $query = qq|DELETE FROM $table WHERE language_id = ?|;
719 do_query($form, $dbh, $query, $form->{"id"});
722 $query = "DELETE FROM language WHERE id = ?";
723 do_query($form, $dbh, $query, $form->{"id"});
728 $main::lxdebug->leave_sub();
733 $main::lxdebug->enter_sub();
735 my ($self, $myconfig, $form) = @_;
737 # connect to database
738 my $dbh = $form->dbconnect($myconfig);
740 my $query = qq|SELECT id, description,
742 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
744 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
746 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
748 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
750 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
752 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
754 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
756 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
758 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
762 my $sth = $dbh->prepare($query);
763 $sth->execute || $form->dberror($query);
766 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
767 push @{ $form->{ALL} }, $ref;
773 $main::lxdebug->leave_sub();
776 sub get_buchungsgruppe {
777 $main::lxdebug->enter_sub();
779 my ($self, $myconfig, $form) = @_;
782 # connect to database
783 my $dbh = $form->dbconnect($myconfig);
787 qq|SELECT description, inventory_accno_id,
788 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
790 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
792 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
794 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
796 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
798 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
800 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
802 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
804 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
807 my $sth = $dbh->prepare($query);
808 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
810 my $ref = $sth->fetchrow_hashref("NAME_lc");
812 map { $form->{$_} = $ref->{$_} } keys %$ref;
817 qq|SELECT count(id) = 0 AS orphaned
819 WHERE buchungsgruppen_id = ?|;
820 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
823 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
825 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
826 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
829 $query = qq|SELECT c.accno, c.description, c.link, c.id,
830 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
831 FROM chart c, defaults d
832 WHERE c.link LIKE '%$module%'
836 my $sth = $dbh->prepare($query);
837 $sth->execute || $form->dberror($query);
838 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
839 foreach my $key (split(/:/, $ref->{link})) {
840 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
841 $form->{"std_inventory_accno_id"} = $ref->{"id"};
843 if ($key =~ /$module/) {
844 if ( ($ref->{id} eq $ref->{inventory_accno_id})
845 || ($ref->{id} eq $ref->{income_accno_id})
846 || ($ref->{id} eq $ref->{expense_accno_id})) {
847 push @{ $form->{"${module}_links"}{$key} },
848 { accno => $ref->{accno},
849 description => $ref->{description},
850 selected => "selected",
853 push @{ $form->{"${module}_links"}{$key} },
854 { accno => $ref->{accno},
855 description => $ref->{description},
867 $main::lxdebug->leave_sub();
870 sub save_buchungsgruppe {
871 $main::lxdebug->enter_sub();
873 my ($self, $myconfig, $form) = @_;
875 # connect to database
876 my $dbh = $form->dbconnect($myconfig);
878 my @values = ($form->{description}, $form->{inventory_accno_id},
879 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
880 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
881 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
882 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
886 # id is the old record
888 $query = qq|UPDATE buchungsgruppen SET
889 description = ?, inventory_accno_id = ?,
890 income_accno_id_0 = ?, expense_accno_id_0 = ?,
891 income_accno_id_1 = ?, expense_accno_id_1 = ?,
892 income_accno_id_2 = ?, expense_accno_id_2 = ?,
893 income_accno_id_3 = ?, expense_accno_id_3 = ?
895 push(@values, $form->{id});
897 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
898 my ($sortkey) = $dbh->selectrow_array($query);
899 $form->dberror($query) if ($dbh->err);
900 push(@values, $sortkey);
901 $query = qq|INSERT INTO buchungsgruppen
902 (description, inventory_accno_id,
903 income_accno_id_0, expense_accno_id_0,
904 income_accno_id_1, expense_accno_id_1,
905 income_accno_id_2, expense_accno_id_2,
906 income_accno_id_3, expense_accno_id_3,
908 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
910 do_query($form, $dbh, $query, @values);
914 $main::lxdebug->leave_sub();
917 sub delete_buchungsgruppe {
918 $main::lxdebug->enter_sub();
920 my ($self, $myconfig, $form) = @_;
922 # connect to database
923 my $dbh = $form->dbconnect($myconfig);
925 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
926 do_query($form, $dbh, $query, $form->{id});
930 $main::lxdebug->leave_sub();
934 $main::lxdebug->enter_sub();
936 my ($self, $myconfig, $form, $table) = @_;
938 # connect to database
939 my $dbh = $form->get_standard_dbh($myconfig);
943 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
944 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
945 my @values = ($form->{"id1"}, $form->{"id2"});
946 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
948 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
949 my $sth = prepare_query($form, $dbh, $query);
951 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
952 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
958 $main::lxdebug->leave_sub();
961 sub prepare_template_filename {
962 $main::lxdebug->enter_sub();
964 my ($self, $myconfig, $form) = @_;
966 my ($filename, $display_filename);
968 if ($form->{type} eq "stylesheet") {
969 $filename = "css/$myconfig->{stylesheet}";
970 $display_filename = $myconfig->{stylesheet};
973 $filename = $form->{formname};
975 if ($form->{language}) {
976 my ($id, $template_code) = split(/--/, $form->{language});
977 $filename .= "_${template_code}";
980 if ($form->{printer}) {
981 my ($id, $template_code) = split(/--/, $form->{printer});
982 $filename .= "_${template_code}";
985 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
986 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
987 $filename =~ s|.*/||;
989 $display_filename = $filename;
990 $filename = "$myconfig->{templates}/$filename";
993 $main::lxdebug->leave_sub();
995 return ($filename, $display_filename);
1000 $main::lxdebug->enter_sub();
1002 my ($self, $filename) = @_;
1004 my ($content, $lines) = ("", 0);
1008 if (open(TEMPLATE, $filename)) {
1009 while (<TEMPLATE>) {
1016 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1018 $main::lxdebug->leave_sub();
1020 return ($content, $lines);
1024 $main::lxdebug->enter_sub();
1026 my ($self, $filename, $content) = @_;
1032 if (open(TEMPLATE, ">", $filename)) {
1033 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1034 $content =~ s/\r\n/\n/g;
1035 print(TEMPLATE $content);
1041 $main::lxdebug->leave_sub();
1047 $main::lxdebug->enter_sub();
1052 my $myconfig = \%main::myconfig;
1053 my $form = $main::form;
1055 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1058 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1060 $form->{curr} =~ s/ //g;
1061 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1062 my $currency = join ':', @currencies;
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->{sdonumber}, $form->{pdonumber},
1099 $form->{businessnumber}, $form->{weightunit},
1100 conv_i($form->{language_id}));
1101 do_query($form, $dbh, $query, @values);
1105 $main::lxdebug->leave_sub();
1109 sub save_preferences {
1110 $main::lxdebug->enter_sub();
1112 my ($self, $myconfig, $form) = @_;
1114 my $dbh = $form->get_standard_dbh($myconfig);
1116 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1119 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1120 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1122 my $rc = $dbh->commit();
1124 # save first currency in myconfig
1125 $currency =~ s/:.*//;
1126 $form->{currency} = $currency;
1128 $form->{businessnumber} = $businessnumber;
1130 $myconfig = User->new(login => $form->{login});
1132 foreach my $item (keys %$form) {
1133 $myconfig->{$item} = $form->{$item};
1136 $myconfig->save_member;
1138 my $auth = $main::auth;
1140 $main::lxdebug->leave_sub();
1146 $main::lxdebug->enter_sub();
1151 my $myconfig = \%main::myconfig;
1152 my $form = $main::form;
1154 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1156 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1158 $defaults->{weightunit} ||= 'kg';
1160 $main::lxdebug->leave_sub();
1165 sub defaultaccounts {
1166 $main::lxdebug->enter_sub();
1168 my ($self, $myconfig, $form) = @_;
1170 # connect to database
1171 my $dbh = $form->dbconnect($myconfig);
1173 # get defaults from defaults table
1174 my $query = qq|SELECT * FROM defaults|;
1175 my $sth = $dbh->prepare($query);
1176 $sth->execute || $form->dberror($query);
1178 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1179 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1180 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1181 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1182 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1183 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1184 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1186 $form->{defaults}{weightunit} ||= 'kg';
1190 $query = qq|SELECT c.id, c.accno, c.description, c.link
1192 WHERE c.link LIKE '%IC%'
1194 $sth = $dbh->prepare($query);
1195 $sth->execute || $self->dberror($query);
1197 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1198 foreach my $key (split(/:/, $ref->{link})) {
1201 if ($key =~ /cogs/) {
1202 $nkey = "IC_expense";
1204 if ($key =~ /sale/) {
1205 $nkey = "IC_income";
1207 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1209 description => $ref->{description}
1216 $query = qq|SELECT c.id, c.accno, c.description
1218 WHERE c.category = 'I'
1219 AND c.charttype = 'A'
1221 $sth = $dbh->prepare($query);
1222 $sth->execute || $self->dberror($query);
1224 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1225 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1227 description => $ref->{description}
1232 $query = qq|SELECT c.id, c.accno, c.description
1234 WHERE c.category = 'E'
1235 AND c.charttype = 'A'
1237 $sth = $dbh->prepare($query);
1238 $sth->execute || $self->dberror($query);
1240 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1241 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1243 description => $ref->{description}
1248 # now get the tax rates and numbers
1249 $query = qq|SELECT c.id, c.accno, c.description,
1250 t.rate * 100 AS rate, t.taxnumber
1252 WHERE c.id = t.chart_id|;
1254 $sth = $dbh->prepare($query);
1255 $sth->execute || $form->dberror($query);
1257 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1258 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1259 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1260 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1261 if $ref->{taxnumber};
1262 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1264 # Abfrage für Standard Umlaufvermögenskonto
1266 qq|SELECT id, accno, description, link | .
1268 qq|WHERE link LIKE ? |.
1270 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1271 $sth->execute || $form->dberror($query);#
1272 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1273 foreach my $item (split(/:/, $ref->{link})) {
1274 if ($item eq "AR_paid") {
1275 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1277 description => $ref->{description}
1286 $main::lxdebug->leave_sub();
1290 $main::lxdebug->enter_sub();
1292 my ($self, $myconfig, $form) = @_;
1294 my $dbh = $form->dbconnect($myconfig);
1296 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1297 my $sth = $dbh->prepare($query);
1298 $sth->execute || $form->dberror($query);
1300 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1306 $main::lxdebug->leave_sub();
1310 $main::lxdebug->enter_sub();
1312 my ($self, $myconfig, $form) = @_;
1314 my $dbh = $form->dbconnect($myconfig);
1316 my ($query, @values);
1318 if ($form->{revtrans}) {
1319 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1321 } elsif ($form->{closedto}) {
1322 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1323 @values = (conv_date($form->{closedto}));
1326 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1329 # set close in defaults
1330 do_query($form, $dbh, $query, @values);
1334 $main::lxdebug->leave_sub();
1338 my ($self, $units, $unit_name, $factor) = @_;
1340 $factor = 1 unless ($factor);
1342 my $unit = $units->{$unit_name};
1344 if (!defined($unit) || !$unit->{"base_unit"} ||
1345 ($unit_name eq $unit->{"base_unit"})) {
1346 return ($unit_name, $factor);
1349 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1352 sub retrieve_units {
1353 $main::lxdebug->enter_sub();
1355 my ($self, $myconfig, $form, $prefix) = @_;
1358 my $dbh = $form->get_standard_dbh;
1360 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1362 my $sth = prepare_execute_query($form, $dbh, $query);
1365 while (my $ref = $sth->fetchrow_hashref()) {
1366 $units->{$ref->{"name"}} = $ref;
1370 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1371 $sth = $dbh->prepare($query_lang);
1372 $sth->execute() || $form->dberror($query_lang);
1374 while (my $ref = $sth->fetchrow_hashref()) {
1375 push(@languages, $ref);
1379 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1380 "FROM units_language ul " .
1381 "LEFT JOIN language l ON ul.language_id = l.id " .
1382 "WHERE ul.unit = ?";
1383 $sth = $dbh->prepare($query_lang);
1385 foreach my $unit (values(%{$units})) {
1386 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1388 $unit->{"LANGUAGES"} = {};
1389 foreach my $lang (@languages) {
1390 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1393 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1394 while (my $ref = $sth->fetchrow_hashref()) {
1395 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1400 $main::lxdebug->leave_sub();
1405 sub retrieve_all_units {
1406 $main::lxdebug->enter_sub();
1410 if (!$::request->{cache}{all_units}) {
1411 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1414 $main::lxdebug->leave_sub();
1416 return $::request->{cache}{all_units};
1420 sub translate_units {
1421 $main::lxdebug->enter_sub();
1423 my ($self, $form, $template_code, $unit, $amount) = @_;
1425 my $units = $self->retrieve_units(\%main::myconfig, $form);
1427 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1428 my $new_unit = $unit;
1430 if (($amount != 1) && $h->{"localized_plural"}) {
1431 $new_unit = $h->{"localized_plural"};
1432 } elsif ($h->{"localized"}) {
1433 $new_unit = $h->{"localized"};
1437 $main::lxdebug->leave_sub();
1443 $main::lxdebug->enter_sub();
1445 my ($self, $myconfig, $form, $units) = @_;
1447 my $dbh = $form->dbconnect($myconfig);
1449 map({ $_->{"in_use"} = 0; } values(%{$units}));
1451 foreach my $unit (values(%{$units})) {
1452 my $base_unit = $unit->{"original_base_unit"};
1453 while ($base_unit) {
1454 $units->{$base_unit}->{"in_use"} = 1;
1455 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1456 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1457 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1461 foreach my $unit (values(%{$units})) {
1462 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1464 foreach my $table (qw(parts invoice orderitems)) {
1465 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1467 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1468 $query .= "= " . $dbh->quote($unit->{"name"});
1470 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1471 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1474 my ($count) = $dbh->selectrow_array($query);
1475 $form->dberror($query) if ($dbh->err);
1478 $unit->{"in_use"} = 1;
1486 $main::lxdebug->leave_sub();
1489 sub convertible_units {
1490 $main::lxdebug->enter_sub();
1494 my $filter_unit = shift;
1495 my $not_smaller = shift;
1497 my $conv_units = [];
1499 $filter_unit = $units->{$filter_unit};
1501 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1502 my $unit = $units->{$name};
1504 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1505 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1506 push @{$conv_units}, $unit;
1510 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1512 $main::lxdebug->leave_sub();
1517 # if $a is translatable to $b, return the factor between them.
1520 $main::lxdebug->enter_sub(2);
1521 my ($this, $a, $b, $all_units) = @_;
1524 $all_units = $this->retrieve_all_units;
1527 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1528 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1529 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1530 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1533 sub unit_select_data {
1534 $main::lxdebug->enter_sub();
1536 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1541 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1544 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1545 if (!$convertible_into ||
1546 ($units->{$convertible_into} &&
1547 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1548 push @{$select}, { "name" => $unit,
1549 "base_unit" => $units->{$unit}->{"base_unit"},
1550 "factor" => $units->{$unit}->{"factor"},
1551 "selected" => ($unit eq $selected) ? "selected" : "" };
1555 $main::lxdebug->leave_sub();
1560 sub unit_select_html {
1561 $main::lxdebug->enter_sub();
1563 my ($self, $units, $name, $selected, $convertible_into) = @_;
1565 my $select = "<select name=${name}>";
1567 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1568 if (!$convertible_into ||
1569 ($units->{$convertible_into} &&
1570 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1571 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1574 $select .= "</select>";
1576 $main::lxdebug->leave_sub();
1582 $main::lxdebug->enter_sub();
1586 my $units = $self->retrieve_all_units();
1591 while (2 <= scalar(@_)) {
1592 my $qty = shift(@_);
1593 my $unit = $units->{shift(@_)};
1595 croak "No unit defined with name $unit" if (!defined $unit);
1598 $base_unit = $unit->{base_unit};
1599 } elsif ($base_unit ne $unit->{base_unit}) {
1600 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1603 $sum += $qty * $unit->{factor};
1606 $main::lxdebug->leave_sub();
1608 return wantarray ? ($sum, $base_unit) : $sum;
1612 $main::lxdebug->enter_sub();
1614 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1616 my $dbh = $form->dbconnect_noauto($myconfig);
1618 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1619 my ($sortkey) = selectrow_query($form, $dbh, $query);
1621 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1622 "VALUES (?, ?, ?, ?)";
1623 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1626 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1627 my $sth = $dbh->prepare($query);
1628 foreach my $lang (@{$languages}) {
1629 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1630 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1638 $main::lxdebug->leave_sub();
1642 $main::lxdebug->enter_sub();
1644 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1646 my $dbh = $form->dbconnect_noauto($myconfig);
1648 my ($base_unit, $unit, $sth, $query);
1650 $query = "DELETE FROM units_language";
1651 $dbh->do($query) || $form->dberror($query);
1653 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1654 $query = "DELETE FROM units WHERE name IN (";
1655 map({ $query .= "?," } @{$delete_units});
1656 substr($query, -1, 1) = ")";
1657 $dbh->do($query, undef, @{$delete_units}) ||
1658 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1661 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1662 $sth = $dbh->prepare($query);
1664 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1665 my $sth_lang = $dbh->prepare($query_lang);
1667 foreach $unit (values(%{$units})) {
1668 $unit->{"depth"} = 0;
1669 my $base_unit = $unit;
1670 while ($base_unit->{"base_unit"}) {
1672 $base_unit = $units->{$base_unit->{"base_unit"}};
1676 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1677 if ($unit->{"LANGUAGES"}) {
1678 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1679 next unless ($lang->{"id"} && $lang->{"localized"});
1680 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1681 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1685 next if ($unit->{"unchanged_unit"});
1687 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1688 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1692 $sth_lang->finish();
1696 $main::lxdebug->leave_sub();
1700 $main::lxdebug->enter_sub();
1702 my ($self, $myconfig, $form) = @_;
1704 # connect to database
1705 my $dbh = $form->dbconnect($myconfig);
1707 my $query = qq|SELECT
1711 round(t.rate * 100, 2) AS rate,
1712 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1713 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1717 my $sth = $dbh->prepare($query);
1718 $sth->execute || $form->dberror($query);
1721 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1722 push @{ $form->{TAX} }, $ref;
1728 $main::lxdebug->leave_sub();
1731 sub get_tax_accounts {
1732 $main::lxdebug->enter_sub();
1734 my ($self, $myconfig, $form) = @_;
1736 my $dbh = $form->dbconnect($myconfig);
1738 # get Accounts from chart
1739 my $query = qq{ SELECT
1741 accno || ' - ' || description AS taxaccount
1743 WHERE link LIKE '%_tax%'
1747 my $sth = $dbh->prepare($query);
1748 $sth->execute || $form->dberror($query);
1750 $form->{ACCOUNTS} = [];
1751 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1752 push @{ $form->{ACCOUNTS} }, $ref;
1759 $main::lxdebug->leave_sub();
1763 $main::lxdebug->enter_sub();
1765 my ($self, $myconfig, $form) = @_;
1767 # connect to database
1768 my $dbh = $form->dbconnect($myconfig);
1770 my $query = qq|SELECT
1773 round(rate * 100, 2) AS rate,
1778 my $sth = $dbh->prepare($query);
1779 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1781 my $ref = $sth->fetchrow_hashref("NAME_lc");
1783 map { $form->{$_} = $ref->{$_} } keys %$ref;
1787 # see if it is used by a taxkey
1788 $query = qq|SELECT count(*) FROM taxkeys
1789 WHERE tax_id = ? AND chart_id >0|;
1791 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1793 $form->{orphaned} = !$form->{orphaned};
1796 if (!$form->{orphaned} ) {
1797 $query = qq|SELECT DISTINCT c.id, c.accno
1799 JOIN tax t ON (t.id = tk.tax_id)
1800 JOIN chart c ON (c.id = tk.chart_id)
1801 WHERE tk.tax_id = ?|;
1803 $sth = $dbh->prepare($query);
1804 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1806 $form->{TAXINUSE} = [];
1807 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1808 push @{ $form->{TAXINUSE} }, $ref;
1816 $main::lxdebug->leave_sub();
1820 $main::lxdebug->enter_sub();
1822 my ($self, $myconfig, $form) = @_;
1825 # connect to database
1826 my $dbh = $form->get_standard_dbh($myconfig);
1828 $form->{rate} = $form->{rate} / 100;
1830 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1831 if ($form->{id} ne "") {
1832 $query = qq|UPDATE tax SET
1837 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1839 push(@values, $form->{id});
1843 $query = qq|INSERT INTO tax (
1850 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1852 do_query($form, $dbh, $query, @values);
1856 $main::lxdebug->leave_sub();
1860 $main::lxdebug->enter_sub();
1862 my ($self, $myconfig, $form) = @_;
1865 # connect to database
1866 my $dbh = $form->get_standard_dbh($myconfig);
1868 $query = qq|DELETE FROM tax
1870 do_query($form, $dbh, $query, $form->{id});
1874 $main::lxdebug->leave_sub();
1877 sub save_price_factor {
1878 $main::lxdebug->enter_sub();
1880 my ($self, $myconfig, $form) = @_;
1882 # connect to database
1883 my $dbh = $form->get_standard_dbh($myconfig);
1886 my @values = ($form->{description}, conv_i($form->{factor}));
1889 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1890 push @values, conv_i($form->{id});
1893 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1896 do_query($form, $dbh, $query, @values);
1900 $main::lxdebug->leave_sub();
1903 sub get_all_price_factors {
1904 $main::lxdebug->enter_sub();
1906 my ($self, $myconfig, $form) = @_;
1908 # connect to database
1909 my $dbh = $form->get_standard_dbh($myconfig);
1911 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1913 $main::lxdebug->leave_sub();
1916 sub get_price_factor {
1917 $main::lxdebug->enter_sub();
1919 my ($self, $myconfig, $form) = @_;
1921 # connect to database
1922 my $dbh = $form->get_standard_dbh($myconfig);
1924 my $query = qq|SELECT description, factor,
1925 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1926 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1927 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1928 FROM price_factors WHERE id = ?|;
1930 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1932 $main::lxdebug->leave_sub();
1935 sub delete_price_factor {
1936 $main::lxdebug->enter_sub();
1938 my ($self, $myconfig, $form) = @_;
1940 # connect to database
1941 my $dbh = $form->get_standard_dbh($myconfig);
1943 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1946 $main::lxdebug->leave_sub();
1949 sub save_warehouse {
1950 $main::lxdebug->enter_sub();
1952 my ($self, $myconfig, $form) = @_;
1954 # connect to database
1955 my $dbh = $form->get_standard_dbh($myconfig);
1957 my ($query, @values, $sth);
1960 $query = qq|SELECT nextval('id')|;
1961 ($form->{id}) = selectrow_query($form, $dbh, $query);
1963 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1964 do_query($form, $dbh, $query, $form->{id});
1967 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1968 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1970 if (0 < $form->{number_of_new_bins}) {
1971 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1972 $sth = prepare_query($form, $dbh, $query);
1974 foreach my $i (1..$form->{number_of_new_bins}) {
1975 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1983 $main::lxdebug->leave_sub();
1987 $main::lxdebug->enter_sub();
1989 my ($self, $myconfig, $form) = @_;
1991 # connect to database
1992 my $dbh = $form->get_standard_dbh($myconfig);
1994 my ($query, @values, $commit_necessary, $sth);
1996 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1999 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2000 do_query($form, $dbh, $query, @values);
2002 $commit_necessary = 1;
2005 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2006 $sth = prepare_query($form, $dbh, $query);
2008 foreach my $row (1..$form->{rowcount}) {
2009 next if ($form->{"delete_${row}"});
2011 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2013 $commit_necessary = 1;
2018 $dbh->commit() if ($commit_necessary);
2020 $main::lxdebug->leave_sub();
2023 sub delete_warehouse {
2024 $main::lxdebug->enter_sub();
2026 my ($self, $myconfig, $form) = @_;
2028 # connect to database
2029 my $dbh = $form->get_standard_dbh($myconfig);
2031 my $id = conv_i($form->{id});
2032 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|;
2033 my ($count) = selectrow_query($form, $dbh, $query, $id);
2036 $main::lxdebug->leave_sub();
2040 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2041 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2045 $main::lxdebug->leave_sub();
2050 sub get_all_warehouses {
2051 $main::lxdebug->enter_sub();
2053 my ($self, $myconfig, $form) = @_;
2055 # connect to database
2056 my $dbh = $form->get_standard_dbh($myconfig);
2058 my $query = qq|SELECT w.id, w.description, w.invalid,
2059 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2061 ORDER BY w.sortkey|;
2063 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2065 $main::lxdebug->leave_sub();
2069 $main::lxdebug->enter_sub();
2071 my ($self, $myconfig, $form) = @_;
2073 # connect to database
2074 my $dbh = $form->get_standard_dbh($myconfig);
2076 my $id = conv_i($form->{id});
2077 my $query = qq|SELECT w.description, w.invalid
2081 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2083 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2085 $query = qq|SELECT b.*, EXISTS
2086 (SELECT i.warehouse_id
2088 WHERE i.bin_id = b.id
2092 WHERE b.warehouse_id = ?|;
2094 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2096 $main::lxdebug->leave_sub();