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 $form->{curr} =~ s/ //g;
1064 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1065 my $currency = join ':', @currencies;
1067 # these defaults are database wide
1070 qq|UPDATE defaults SET
1071 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1074 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1075 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1076 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1094 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1095 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1096 $form->{invnumber}, $form->{cnnumber},
1097 $form->{sonumber}, $form->{ponumber},
1098 $form->{sqnumber}, $form->{rfqnumber},
1099 $form->{customernumber}, $form->{vendornumber},
1100 $form->{articlenumber}, $form->{servicenumber},
1101 $form->{assemblynumber},
1102 $form->{sdonumber}, $form->{pdonumber},
1104 $form->{businessnumber}, $form->{weightunit},
1105 conv_i($form->{language_id}));
1106 do_query($form, $dbh, $query, @values);
1110 $main::lxdebug->leave_sub();
1114 sub save_preferences {
1115 $main::lxdebug->enter_sub();
1117 my ($self, $myconfig, $form) = @_;
1119 my $dbh = $form->get_standard_dbh($myconfig);
1121 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1124 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1125 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1127 my $rc = $dbh->commit();
1129 # save first currency in myconfig
1130 $currency =~ s/:.*//;
1131 $form->{currency} = $currency;
1133 $form->{businessnumber} = $businessnumber;
1135 $myconfig = User->new(login => $form->{login});
1137 foreach my $item (keys %$form) {
1138 $myconfig->{$item} = $form->{$item};
1141 $myconfig->save_member;
1143 my $auth = $main::auth;
1145 $main::lxdebug->leave_sub();
1151 $main::lxdebug->enter_sub();
1156 my $myconfig = \%main::myconfig;
1157 my $form = $main::form;
1159 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1161 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1163 $defaults->{weightunit} ||= 'kg';
1165 $main::lxdebug->leave_sub();
1170 sub defaultaccounts {
1171 $main::lxdebug->enter_sub();
1173 my ($self, $myconfig, $form) = @_;
1175 # connect to database
1176 my $dbh = $form->dbconnect($myconfig);
1178 # get defaults from defaults table
1179 my $query = qq|SELECT * FROM defaults|;
1180 my $sth = $dbh->prepare($query);
1181 $sth->execute || $form->dberror($query);
1183 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1184 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1185 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1186 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1187 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1188 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1189 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1191 $form->{defaults}{weightunit} ||= 'kg';
1195 $query = qq|SELECT c.id, c.accno, c.description, c.link
1197 WHERE c.link LIKE '%IC%'
1199 $sth = $dbh->prepare($query);
1200 $sth->execute || $self->dberror($query);
1202 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1203 foreach my $key (split(/:/, $ref->{link})) {
1206 if ($key =~ /cogs/) {
1207 $nkey = "IC_expense";
1209 if ($key =~ /sale/) {
1210 $nkey = "IC_income";
1212 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1214 description => $ref->{description}
1221 $query = qq|SELECT c.id, c.accno, c.description
1223 WHERE c.category = 'I'
1224 AND c.charttype = 'A'
1226 $sth = $dbh->prepare($query);
1227 $sth->execute || $self->dberror($query);
1229 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1230 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1232 description => $ref->{description}
1237 $query = qq|SELECT c.id, c.accno, c.description
1239 WHERE c.category = 'E'
1240 AND c.charttype = 'A'
1242 $sth = $dbh->prepare($query);
1243 $sth->execute || $self->dberror($query);
1245 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1246 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1248 description => $ref->{description}
1253 # now get the tax rates and numbers
1254 $query = qq|SELECT c.id, c.accno, c.description,
1255 t.rate * 100 AS rate, t.taxnumber
1257 WHERE c.id = t.chart_id|;
1259 $sth = $dbh->prepare($query);
1260 $sth->execute || $form->dberror($query);
1262 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1263 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1264 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1265 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1266 if $ref->{taxnumber};
1267 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1269 # Abfrage für Standard Umlaufvermögenskonto
1271 qq|SELECT id, accno, description, link | .
1273 qq|WHERE link LIKE ? |.
1275 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1276 $sth->execute || $form->dberror($query);#
1277 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1278 foreach my $item (split(/:/, $ref->{link})) {
1279 if ($item eq "AR_paid") {
1280 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1282 description => $ref->{description}
1291 $main::lxdebug->leave_sub();
1295 $main::lxdebug->enter_sub();
1297 my ($self, $myconfig, $form) = @_;
1299 my $dbh = $form->dbconnect($myconfig);
1301 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1302 my $sth = $dbh->prepare($query);
1303 $sth->execute || $form->dberror($query);
1305 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1311 $main::lxdebug->leave_sub();
1315 $main::lxdebug->enter_sub();
1317 my ($self, $myconfig, $form) = @_;
1319 my $dbh = $form->dbconnect($myconfig);
1321 my ($query, @values);
1323 if ($form->{revtrans}) {
1324 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1326 } elsif ($form->{closedto}) {
1327 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1328 @values = (conv_date($form->{closedto}));
1331 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1334 # set close in defaults
1335 do_query($form, $dbh, $query, @values);
1339 $main::lxdebug->leave_sub();
1343 my ($self, $units, $unit_name, $factor) = @_;
1345 $factor = 1 unless ($factor);
1347 my $unit = $units->{$unit_name};
1349 if (!defined($unit) || !$unit->{"base_unit"} ||
1350 ($unit_name eq $unit->{"base_unit"})) {
1351 return ($unit_name, $factor);
1354 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1357 sub retrieve_units {
1358 $main::lxdebug->enter_sub();
1360 my ($self, $myconfig, $form, $prefix) = @_;
1363 my $dbh = $form->get_standard_dbh;
1365 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1367 my $sth = prepare_execute_query($form, $dbh, $query);
1370 while (my $ref = $sth->fetchrow_hashref()) {
1371 $units->{$ref->{"name"}} = $ref;
1375 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1376 $sth = $dbh->prepare($query_lang);
1377 $sth->execute() || $form->dberror($query_lang);
1379 while (my $ref = $sth->fetchrow_hashref()) {
1380 push(@languages, $ref);
1384 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1385 "FROM units_language ul " .
1386 "LEFT JOIN language l ON ul.language_id = l.id " .
1387 "WHERE ul.unit = ?";
1388 $sth = $dbh->prepare($query_lang);
1390 foreach my $unit (values(%{$units})) {
1391 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1393 $unit->{"LANGUAGES"} = {};
1394 foreach my $lang (@languages) {
1395 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1398 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1399 while (my $ref = $sth->fetchrow_hashref()) {
1400 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1405 $main::lxdebug->leave_sub();
1410 sub retrieve_all_units {
1411 $main::lxdebug->enter_sub();
1415 if (!$::request->{cache}{all_units}) {
1416 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1419 $main::lxdebug->leave_sub();
1421 return $::request->{cache}{all_units};
1425 sub translate_units {
1426 $main::lxdebug->enter_sub();
1428 my ($self, $form, $template_code, $unit, $amount) = @_;
1430 my $units = $self->retrieve_units(\%main::myconfig, $form);
1432 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1433 my $new_unit = $unit;
1435 if (($amount != 1) && $h->{"localized_plural"}) {
1436 $new_unit = $h->{"localized_plural"};
1437 } elsif ($h->{"localized"}) {
1438 $new_unit = $h->{"localized"};
1442 $main::lxdebug->leave_sub();
1448 $main::lxdebug->enter_sub();
1450 my ($self, $myconfig, $form, $units) = @_;
1452 my $dbh = $form->dbconnect($myconfig);
1454 map({ $_->{"in_use"} = 0; } values(%{$units}));
1456 foreach my $unit (values(%{$units})) {
1457 my $base_unit = $unit->{"original_base_unit"};
1458 while ($base_unit) {
1459 $units->{$base_unit}->{"in_use"} = 1;
1460 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1461 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1462 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1466 foreach my $unit (values(%{$units})) {
1467 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1469 foreach my $table (qw(parts invoice orderitems)) {
1470 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1472 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1473 $query .= "= " . $dbh->quote($unit->{"name"});
1475 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1476 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1479 my ($count) = $dbh->selectrow_array($query);
1480 $form->dberror($query) if ($dbh->err);
1483 $unit->{"in_use"} = 1;
1491 $main::lxdebug->leave_sub();
1494 sub convertible_units {
1495 $main::lxdebug->enter_sub();
1499 my $filter_unit = shift;
1500 my $not_smaller = shift;
1502 my $conv_units = [];
1504 $filter_unit = $units->{$filter_unit};
1506 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1507 my $unit = $units->{$name};
1509 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1510 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1511 push @{$conv_units}, $unit;
1515 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1517 $main::lxdebug->leave_sub();
1522 # if $a is translatable to $b, return the factor between them.
1525 $main::lxdebug->enter_sub(2);
1526 my ($this, $a, $b, $all_units) = @_;
1529 $all_units = $this->retrieve_all_units;
1532 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1533 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1534 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1535 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1538 sub unit_select_data {
1539 $main::lxdebug->enter_sub();
1541 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1546 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1549 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1550 if (!$convertible_into ||
1551 ($units->{$convertible_into} &&
1552 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1553 push @{$select}, { "name" => $unit,
1554 "base_unit" => $units->{$unit}->{"base_unit"},
1555 "factor" => $units->{$unit}->{"factor"},
1556 "selected" => ($unit eq $selected) ? "selected" : "" };
1560 $main::lxdebug->leave_sub();
1565 sub unit_select_html {
1566 $main::lxdebug->enter_sub();
1568 my ($self, $units, $name, $selected, $convertible_into) = @_;
1570 my $select = "<select name=${name}>";
1572 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1573 if (!$convertible_into ||
1574 ($units->{$convertible_into} &&
1575 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1576 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1579 $select .= "</select>";
1581 $main::lxdebug->leave_sub();
1587 $main::lxdebug->enter_sub();
1591 my $units = $self->retrieve_all_units();
1596 while (2 <= scalar(@_)) {
1597 my $qty = shift(@_);
1598 my $unit = $units->{shift(@_)};
1600 croak "No unit defined with name $unit" if (!defined $unit);
1603 $base_unit = $unit->{base_unit};
1604 } elsif ($base_unit ne $unit->{base_unit}) {
1605 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1608 $sum += $qty * $unit->{factor};
1611 $main::lxdebug->leave_sub();
1613 return wantarray ? ($sum, $base_unit) : $sum;
1617 $main::lxdebug->enter_sub();
1619 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1621 my $dbh = $form->dbconnect_noauto($myconfig);
1623 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1624 my ($sortkey) = selectrow_query($form, $dbh, $query);
1626 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1627 "VALUES (?, ?, ?, ?)";
1628 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1631 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1632 my $sth = $dbh->prepare($query);
1633 foreach my $lang (@{$languages}) {
1634 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1635 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1643 $main::lxdebug->leave_sub();
1647 $main::lxdebug->enter_sub();
1649 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1651 my $dbh = $form->dbconnect_noauto($myconfig);
1653 my ($base_unit, $unit, $sth, $query);
1655 $query = "DELETE FROM units_language";
1656 $dbh->do($query) || $form->dberror($query);
1658 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1659 $query = "DELETE FROM units WHERE name IN (";
1660 map({ $query .= "?," } @{$delete_units});
1661 substr($query, -1, 1) = ")";
1662 $dbh->do($query, undef, @{$delete_units}) ||
1663 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1666 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1667 $sth = $dbh->prepare($query);
1669 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1670 my $sth_lang = $dbh->prepare($query_lang);
1672 foreach $unit (values(%{$units})) {
1673 $unit->{"depth"} = 0;
1674 my $base_unit = $unit;
1675 while ($base_unit->{"base_unit"}) {
1677 $base_unit = $units->{$base_unit->{"base_unit"}};
1681 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1682 if ($unit->{"LANGUAGES"}) {
1683 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1684 next unless ($lang->{"id"} && $lang->{"localized"});
1685 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1686 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1690 next if ($unit->{"unchanged_unit"});
1692 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1693 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1697 $sth_lang->finish();
1701 $main::lxdebug->leave_sub();
1705 $main::lxdebug->enter_sub();
1707 my ($self, $myconfig, $form) = @_;
1709 # connect to database
1710 my $dbh = $form->dbconnect($myconfig);
1712 my $query = qq|SELECT
1716 round(t.rate * 100, 2) AS rate,
1717 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1718 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1722 my $sth = $dbh->prepare($query);
1723 $sth->execute || $form->dberror($query);
1726 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1727 push @{ $form->{TAX} }, $ref;
1733 $main::lxdebug->leave_sub();
1736 sub get_tax_accounts {
1737 $main::lxdebug->enter_sub();
1739 my ($self, $myconfig, $form) = @_;
1741 my $dbh = $form->dbconnect($myconfig);
1743 # get Accounts from chart
1744 my $query = qq{ SELECT
1746 accno || ' - ' || description AS taxaccount
1748 WHERE link LIKE '%_tax%'
1752 my $sth = $dbh->prepare($query);
1753 $sth->execute || $form->dberror($query);
1755 $form->{ACCOUNTS} = [];
1756 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1757 push @{ $form->{ACCOUNTS} }, $ref;
1764 $main::lxdebug->leave_sub();
1768 $main::lxdebug->enter_sub();
1770 my ($self, $myconfig, $form) = @_;
1772 # connect to database
1773 my $dbh = $form->dbconnect($myconfig);
1775 my $query = qq|SELECT
1778 round(rate * 100, 2) AS rate,
1780 (id IN (SELECT tax_id
1781 FROM acc_trans)) AS tax_already_used
1785 my $sth = $dbh->prepare($query);
1786 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1788 my $ref = $sth->fetchrow_hashref("NAME_lc");
1790 map { $form->{$_} = $ref->{$_} } keys %$ref;
1794 # see if it is used by a taxkey
1795 $query = qq|SELECT count(*) FROM taxkeys
1796 WHERE tax_id = ? AND chart_id >0|;
1798 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1800 $form->{orphaned} = !$form->{orphaned};
1803 if (!$form->{orphaned} ) {
1804 $query = qq|SELECT DISTINCT c.id, c.accno
1806 JOIN tax t ON (t.id = tk.tax_id)
1807 JOIN chart c ON (c.id = tk.chart_id)
1808 WHERE tk.tax_id = ?|;
1810 $sth = $dbh->prepare($query);
1811 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1813 $form->{TAXINUSE} = [];
1814 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1815 push @{ $form->{TAXINUSE} }, $ref;
1823 $main::lxdebug->leave_sub();
1827 $main::lxdebug->enter_sub();
1829 my ($self, $myconfig, $form) = @_;
1832 # connect to database
1833 my $dbh = $form->get_standard_dbh($myconfig);
1835 $form->{rate} = $form->{rate} / 100;
1837 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1838 if ($form->{id} ne "") {
1839 $query = qq|UPDATE tax SET
1844 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1846 push(@values, $form->{id});
1850 $query = qq|INSERT INTO tax (
1857 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1859 do_query($form, $dbh, $query, @values);
1863 $main::lxdebug->leave_sub();
1867 $main::lxdebug->enter_sub();
1869 my ($self, $myconfig, $form) = @_;
1872 # connect to database
1873 my $dbh = $form->get_standard_dbh($myconfig);
1875 $query = qq|DELETE FROM tax
1877 do_query($form, $dbh, $query, $form->{id});
1881 $main::lxdebug->leave_sub();
1884 sub save_price_factor {
1885 $main::lxdebug->enter_sub();
1887 my ($self, $myconfig, $form) = @_;
1889 # connect to database
1890 my $dbh = $form->get_standard_dbh($myconfig);
1893 my @values = ($form->{description}, conv_i($form->{factor}));
1896 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1897 push @values, conv_i($form->{id});
1900 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1903 do_query($form, $dbh, $query, @values);
1907 $main::lxdebug->leave_sub();
1910 sub get_all_price_factors {
1911 $main::lxdebug->enter_sub();
1913 my ($self, $myconfig, $form) = @_;
1915 # connect to database
1916 my $dbh = $form->get_standard_dbh($myconfig);
1918 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1920 $main::lxdebug->leave_sub();
1923 sub get_price_factor {
1924 $main::lxdebug->enter_sub();
1926 my ($self, $myconfig, $form) = @_;
1928 # connect to database
1929 my $dbh = $form->get_standard_dbh($myconfig);
1931 my $query = qq|SELECT description, factor,
1932 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1933 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1934 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1935 FROM price_factors WHERE id = ?|;
1937 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1939 $main::lxdebug->leave_sub();
1942 sub delete_price_factor {
1943 $main::lxdebug->enter_sub();
1945 my ($self, $myconfig, $form) = @_;
1947 # connect to database
1948 my $dbh = $form->get_standard_dbh($myconfig);
1950 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1953 $main::lxdebug->leave_sub();
1956 sub save_warehouse {
1957 $main::lxdebug->enter_sub();
1959 my ($self, $myconfig, $form) = @_;
1961 # connect to database
1962 my $dbh = $form->get_standard_dbh($myconfig);
1964 my ($query, @values, $sth);
1967 $query = qq|SELECT nextval('id')|;
1968 ($form->{id}) = selectrow_query($form, $dbh, $query);
1970 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1971 do_query($form, $dbh, $query, $form->{id});
1974 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1975 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1977 if (0 < $form->{number_of_new_bins}) {
1978 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1979 $sth = prepare_query($form, $dbh, $query);
1981 foreach my $i (1..$form->{number_of_new_bins}) {
1982 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1990 $main::lxdebug->leave_sub();
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, $commit_necessary, $sth);
2003 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2006 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2007 do_query($form, $dbh, $query, @values);
2009 $commit_necessary = 1;
2012 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2013 $sth = prepare_query($form, $dbh, $query);
2015 foreach my $row (1..$form->{rowcount}) {
2016 next if ($form->{"delete_${row}"});
2018 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2020 $commit_necessary = 1;
2025 $dbh->commit() if ($commit_necessary);
2027 $main::lxdebug->leave_sub();
2030 sub delete_warehouse {
2031 $main::lxdebug->enter_sub();
2033 my ($self, $myconfig, $form) = @_;
2035 # connect to database
2036 my $dbh = $form->get_standard_dbh($myconfig);
2038 my $id = conv_i($form->{id});
2039 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|;
2040 my ($count) = selectrow_query($form, $dbh, $query, $id);
2043 $main::lxdebug->leave_sub();
2047 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2048 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2052 $main::lxdebug->leave_sub();
2057 sub get_all_warehouses {
2058 $main::lxdebug->enter_sub();
2060 my ($self, $myconfig, $form) = @_;
2062 # connect to database
2063 my $dbh = $form->get_standard_dbh($myconfig);
2065 my $query = qq|SELECT w.id, w.description, w.invalid,
2066 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2068 ORDER BY w.sortkey|;
2070 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2072 $main::lxdebug->leave_sub();
2076 $main::lxdebug->enter_sub();
2078 my ($self, $myconfig, $form) = @_;
2080 # connect to database
2081 my $dbh = $form->get_standard_dbh($myconfig);
2083 my $id = conv_i($form->{id});
2084 my $query = qq|SELECT w.description, w.invalid
2088 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2090 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2092 $query = qq|SELECT b.*, EXISTS
2093 (SELECT i.warehouse_id
2095 WHERE i.bin_id = b.id
2099 WHERE b.warehouse_id = ?|;
2101 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2103 $main::lxdebug->leave_sub();