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") {
251 my ($accno) = selectrow_query($form, $dbh, $query, $form->{accno});
254 $form->error($::locale->text('Account number not unique!'));
258 if (!$form->{id} || $form->{id} eq "") {
259 $query = qq|SELECT nextval('id')|;
260 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
261 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
262 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
270 # if charttype is heading make sure certain values are empty
271 # specifically, if charttype is changed from an existing account, empty the
272 # fields unnecessary for headings, so that e.g. heading doesn't appear in
273 # drop-down menues due to still having a valid "link" entry
275 if ( $form->{charttype} eq 'H' ) {
277 $form->{pos_bwa} = '';
278 $form->{pos_bilanz} = '';
279 $form->{pos_eur} = '';
280 $form->{new_chart_id} = '';
281 $form->{valid_from} = '';
284 $query = qq|UPDATE chart SET
300 $form->{description},
304 conv_i($form->{pos_bwa}),
305 conv_i($form->{pos_bilanz}),
306 conv_i($form->{pos_eur}),
307 conv_i($form->{new_chart_id}),
308 conv_date($form->{valid_from}),
309 ($form->{datevautomatik} eq 'T') ? 'true':'false',
316 do_query($form, $dbh, $query, @values);
322 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
326 for $tk_count (0 .. $MAX_TRIES) {
330 # Check if the account already exists, else cancel
332 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
334 last READTAXKEYS if ( $form->{'id'} == 0);
336 # check if there is a startdate
337 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
342 # Add valid taxkeys into the array
345 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
346 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
347 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
348 chart_id => conv_i($form->{"id"}),
349 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
350 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
357 for my $j (0 .. $#taxkeys){
358 if ( defined $taxkeys[$j]{'id'} ){
361 if ($taxkeys[$j]{'delete'}){
363 DELETE FROM taxkeys WHERE id = ?
366 @values = ($taxkeys[$j]{'id'});
368 do_query($form, $dbh, $query, @values);
377 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
385 $taxkeys[$j]{'tax_id'},
386 $taxkeys[$j]{'chart_id'},
387 $taxkeys[$j]{'tax_id'},
388 $taxkeys[$j]{'pos_ustva'},
389 $taxkeys[$j]{'startdate'},
392 do_query($form, $dbh, $query, @values);
398 INSERT INTO taxkeys (
405 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
408 $taxkeys[$j]{'tax_id'},
409 $taxkeys[$j]{'chart_id'},
410 $taxkeys[$j]{'tax_id'},
411 $taxkeys[$j]{'pos_ustva'},
412 $taxkeys[$j]{'startdate'},
415 do_query($form, $dbh, $query, @values);
420 # Update chart.taxkey_id to the latest from taxkeys for this chart.
426 WHERE taxkeys.chart_id = chart.id
427 ORDER BY startdate DESC
433 do_query($form, $dbh, $query, $form->{id});
436 my $rc = $dbh->commit;
439 $main::lxdebug->leave_sub();
445 $main::lxdebug->enter_sub();
447 my ($self, $myconfig, $form) = @_;
449 # connect to database, turn off AutoCommit
450 my $dbh = $form->dbconnect_noauto($myconfig);
452 my $query = qq|SELECT count(*) FROM acc_trans a
453 WHERE a.chart_id = ?|;
454 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
458 $main::lxdebug->leave_sub();
462 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
463 foreach my $type (qw(inventory income expense)) {
466 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
467 qq|WHERE ${type}_accno_id = ?|;
468 do_query($form, $dbh, $query, $form->{id});
471 foreach my $table (qw(partstax customertax vendortax tax)) {
472 $query = qq|DELETE FROM $table
474 do_query($form, $dbh, $query, $form->{id});
477 # delete chart of account record
478 $query = qq|DELETE FROM chart
480 do_query($form, $dbh, $query, $form->{id});
482 # delete account taxkeys
483 $query = qq|DELETE FROM taxkeys
485 do_query($form, $dbh, $query, $form->{id});
487 # commit and redirect
488 my $rc = $dbh->commit;
491 $main::lxdebug->leave_sub();
497 $main::lxdebug->enter_sub();
499 my ($self, $myconfig, $form) = @_;
501 # connect to database
502 my $dbh = $form->dbconnect($myconfig);
504 my $query = qq|SELECT id, lead
508 my $sth = $dbh->prepare($query);
509 $sth->execute || $form->dberror($query);
511 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
512 push @{ $form->{ALL} }, $ref;
518 $main::lxdebug->leave_sub();
522 $main::lxdebug->enter_sub();
524 my ($self, $myconfig, $form) = @_;
526 # connect to database
527 my $dbh = $form->dbconnect($myconfig);
530 qq|SELECT l.id, l.lead | .
533 my $sth = $dbh->prepare($query);
534 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
536 my $ref = $sth->fetchrow_hashref("NAME_lc");
538 map { $form->{$_} = $ref->{$_} } keys %$ref;
544 $main::lxdebug->leave_sub();
548 $main::lxdebug->enter_sub();
550 my ($self, $myconfig, $form) = @_;
553 # connect to database
554 my $dbh = $form->dbconnect($myconfig);
556 my @values = ($form->{description});
557 # id is the old record
559 $query = qq|UPDATE leads SET
562 push(@values, $form->{id});
564 $query = qq|INSERT INTO leads
568 do_query($form, $dbh, $query, @values);
572 $main::lxdebug->leave_sub();
576 $main::lxdebug->enter_sub();
578 my ($self, $myconfig, $form) = @_;
581 # connect to database
582 my $dbh = $form->dbconnect($myconfig);
584 $query = qq|DELETE FROM leads
586 do_query($form, $dbh, $query, $form->{id});
590 $main::lxdebug->leave_sub();
594 $main::lxdebug->enter_sub();
596 my ($self, $myconfig, $form, $return_list) = @_;
598 # connect to database
599 my $dbh = $form->dbconnect($myconfig);
602 "SELECT id, description, template_code, article_code, " .
603 " output_numberformat, output_dateformat, output_longdates " .
604 "FROM language ORDER BY description";
606 my $sth = $dbh->prepare($query);
607 $sth->execute || $form->dberror($query);
611 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
612 push(@{ $ary }, $ref);
618 $main::lxdebug->leave_sub();
628 $main::lxdebug->enter_sub();
630 my ($self, $myconfig, $form) = @_;
632 # connect to database
633 my $dbh = $form->dbconnect($myconfig);
636 "SELECT description, template_code, article_code, " .
637 " output_numberformat, output_dateformat, output_longdates " .
638 "FROM language WHERE id = ?";
639 my $sth = $dbh->prepare($query);
640 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
642 my $ref = $sth->fetchrow_hashref("NAME_lc");
644 map { $form->{$_} = $ref->{$_} } keys %$ref;
650 $main::lxdebug->leave_sub();
653 sub get_language_details {
654 $main::lxdebug->enter_sub();
656 my ($self, $myconfig, $form, $id) = @_;
658 # connect to database
659 my $dbh = $form->dbconnect($myconfig);
662 "SELECT template_code, " .
663 " output_numberformat, output_dateformat, output_longdates " .
664 "FROM language WHERE id = ?";
665 my @res = selectrow_query($form, $dbh, $query, $id);
668 $main::lxdebug->leave_sub();
674 $main::lxdebug->enter_sub();
676 my ($self, $myconfig, $form) = @_;
678 # connect to database
679 my $dbh = $form->dbconnect($myconfig);
680 my (@values, $query);
682 map({ push(@values, $form->{$_}); }
683 qw(description template_code article_code
684 output_numberformat output_dateformat output_longdates));
686 # id is the old record
689 "UPDATE language SET " .
690 " description = ?, template_code = ?, article_code = ?, " .
691 " output_numberformat = ?, output_dateformat = ?, " .
692 " output_longdates = ? " .
694 push(@values, $form->{id});
697 "INSERT INTO language (" .
698 " description, template_code, article_code, " .
699 " output_numberformat, output_dateformat, output_longdates" .
700 ") VALUES (?, ?, ?, ?, ?, ?)";
702 do_query($form, $dbh, $query, @values);
706 $main::lxdebug->leave_sub();
709 sub delete_language {
710 $main::lxdebug->enter_sub();
712 my ($self, $myconfig, $form) = @_;
715 # connect to database
716 my $dbh = $form->dbconnect_noauto($myconfig);
718 foreach my $table (qw(generic_translations units_language)) {
719 $query = qq|DELETE FROM $table WHERE language_id = ?|;
720 do_query($form, $dbh, $query, $form->{"id"});
723 $query = "DELETE FROM language WHERE id = ?";
724 do_query($form, $dbh, $query, $form->{"id"});
729 $main::lxdebug->leave_sub();
734 $main::lxdebug->enter_sub();
736 my ($self, $myconfig, $form) = @_;
738 # connect to database
739 my $dbh = $form->dbconnect($myconfig);
741 my $query = qq|SELECT id, description,
743 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
745 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
747 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
749 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
751 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
753 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
755 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
757 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
759 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
763 my $sth = $dbh->prepare($query);
764 $sth->execute || $form->dberror($query);
767 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
768 push @{ $form->{ALL} }, $ref;
774 $main::lxdebug->leave_sub();
777 sub get_buchungsgruppe {
778 $main::lxdebug->enter_sub();
780 my ($self, $myconfig, $form) = @_;
783 # connect to database
784 my $dbh = $form->dbconnect($myconfig);
788 qq|SELECT description, inventory_accno_id,
789 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
791 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
793 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
795 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
797 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
799 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
801 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
803 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
805 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
808 my $sth = $dbh->prepare($query);
809 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
811 my $ref = $sth->fetchrow_hashref("NAME_lc");
813 map { $form->{$_} = $ref->{$_} } keys %$ref;
818 qq|SELECT count(id) = 0 AS orphaned
820 WHERE buchungsgruppen_id = ?|;
821 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
824 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
826 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
827 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
830 $query = qq|SELECT c.accno, c.description, c.link, c.id,
831 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
832 FROM chart c, defaults d
833 WHERE c.link LIKE '%$module%'
837 my $sth = $dbh->prepare($query);
838 $sth->execute || $form->dberror($query);
839 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
840 foreach my $key (split(/:/, $ref->{link})) {
841 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
842 $form->{"std_inventory_accno_id"} = $ref->{"id"};
844 if ($key =~ /$module/) {
845 if ( ($ref->{id} eq $ref->{inventory_accno_id})
846 || ($ref->{id} eq $ref->{income_accno_id})
847 || ($ref->{id} eq $ref->{expense_accno_id})) {
848 push @{ $form->{"${module}_links"}{$key} },
849 { accno => $ref->{accno},
850 description => $ref->{description},
851 selected => "selected",
854 push @{ $form->{"${module}_links"}{$key} },
855 { accno => $ref->{accno},
856 description => $ref->{description},
868 $main::lxdebug->leave_sub();
871 sub save_buchungsgruppe {
872 $main::lxdebug->enter_sub();
874 my ($self, $myconfig, $form) = @_;
876 # connect to database
877 my $dbh = $form->dbconnect($myconfig);
879 my @values = ($form->{description}, $form->{inventory_accno_id},
880 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
881 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
882 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
883 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
887 # id is the old record
889 $query = qq|UPDATE buchungsgruppen SET
890 description = ?, inventory_accno_id = ?,
891 income_accno_id_0 = ?, expense_accno_id_0 = ?,
892 income_accno_id_1 = ?, expense_accno_id_1 = ?,
893 income_accno_id_2 = ?, expense_accno_id_2 = ?,
894 income_accno_id_3 = ?, expense_accno_id_3 = ?
896 push(@values, $form->{id});
898 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
899 my ($sortkey) = $dbh->selectrow_array($query);
900 $form->dberror($query) if ($dbh->err);
901 push(@values, $sortkey);
902 $query = qq|INSERT INTO buchungsgruppen
903 (description, inventory_accno_id,
904 income_accno_id_0, expense_accno_id_0,
905 income_accno_id_1, expense_accno_id_1,
906 income_accno_id_2, expense_accno_id_2,
907 income_accno_id_3, expense_accno_id_3,
909 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
911 do_query($form, $dbh, $query, @values);
915 $main::lxdebug->leave_sub();
918 sub delete_buchungsgruppe {
919 $main::lxdebug->enter_sub();
921 my ($self, $myconfig, $form) = @_;
923 # connect to database
924 my $dbh = $form->dbconnect($myconfig);
926 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
927 do_query($form, $dbh, $query, $form->{id});
931 $main::lxdebug->leave_sub();
935 $main::lxdebug->enter_sub();
937 my ($self, $myconfig, $form, $table) = @_;
939 # connect to database
940 my $dbh = $form->get_standard_dbh($myconfig);
944 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
945 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
946 my @values = ($form->{"id1"}, $form->{"id2"});
947 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
949 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
950 my $sth = prepare_query($form, $dbh, $query);
952 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
953 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
959 $main::lxdebug->leave_sub();
962 sub prepare_template_filename {
963 $main::lxdebug->enter_sub();
965 my ($self, $myconfig, $form) = @_;
967 my ($filename, $display_filename);
969 if ($form->{type} eq "stylesheet") {
970 $filename = "css/$myconfig->{stylesheet}";
971 $display_filename = $myconfig->{stylesheet};
974 $filename = $form->{formname};
976 if ($form->{language}) {
977 my ($id, $template_code) = split(/--/, $form->{language});
978 $filename .= "_${template_code}";
981 if ($form->{printer}) {
982 my ($id, $template_code) = split(/--/, $form->{printer});
983 $filename .= "_${template_code}";
986 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
987 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
988 $filename =~ s|.*/||;
990 $display_filename = $filename;
991 $filename = "$myconfig->{templates}/$filename";
994 $main::lxdebug->leave_sub();
996 return ($filename, $display_filename);
1001 $main::lxdebug->enter_sub();
1003 my ($self, $filename) = @_;
1005 my ($content, $lines) = ("", 0);
1009 if (open(TEMPLATE, $filename)) {
1010 while (<TEMPLATE>) {
1017 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1019 $main::lxdebug->leave_sub();
1021 return ($content, $lines);
1025 $main::lxdebug->enter_sub();
1027 my ($self, $filename, $content) = @_;
1033 if (open(TEMPLATE, ">", $filename)) {
1034 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1035 $content =~ s/\r\n/\n/g;
1036 print(TEMPLATE $content);
1042 $main::lxdebug->leave_sub();
1048 $main::lxdebug->enter_sub();
1053 my $myconfig = \%main::myconfig;
1054 my $form = $main::form;
1056 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1059 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1061 $form->{curr} =~ s/ //g;
1062 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1063 my $currency = join ':', @currencies;
1065 # these defaults are database wide
1068 qq|UPDATE defaults SET
1069 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1070 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1074 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1091 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1092 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1093 $form->{invnumber}, $form->{cnnumber},
1094 $form->{sonumber}, $form->{ponumber},
1095 $form->{sqnumber}, $form->{rfqnumber},
1096 $form->{customernumber}, $form->{vendornumber},
1097 $form->{articlenumber}, $form->{servicenumber},
1098 $form->{sdonumber}, $form->{pdonumber},
1100 $form->{businessnumber}, $form->{weightunit},
1101 conv_i($form->{language_id}));
1102 do_query($form, $dbh, $query, @values);
1106 $main::lxdebug->leave_sub();
1110 sub save_preferences {
1111 $main::lxdebug->enter_sub();
1113 my ($self, $myconfig, $form) = @_;
1115 my $dbh = $form->get_standard_dbh($myconfig);
1117 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1120 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1121 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1123 my $rc = $dbh->commit();
1125 # save first currency in myconfig
1126 $currency =~ s/:.*//;
1127 $form->{currency} = $currency;
1129 $form->{businessnumber} = $businessnumber;
1131 $myconfig = User->new(login => $form->{login});
1133 foreach my $item (keys %$form) {
1134 $myconfig->{$item} = $form->{$item};
1137 $myconfig->save_member;
1139 my $auth = $main::auth;
1141 $main::lxdebug->leave_sub();
1147 $main::lxdebug->enter_sub();
1152 my $myconfig = \%main::myconfig;
1153 my $form = $main::form;
1155 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1157 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1159 $defaults->{weightunit} ||= 'kg';
1161 $main::lxdebug->leave_sub();
1166 sub defaultaccounts {
1167 $main::lxdebug->enter_sub();
1169 my ($self, $myconfig, $form) = @_;
1171 # connect to database
1172 my $dbh = $form->dbconnect($myconfig);
1174 # get defaults from defaults table
1175 my $query = qq|SELECT * FROM defaults|;
1176 my $sth = $dbh->prepare($query);
1177 $sth->execute || $form->dberror($query);
1179 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1180 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1181 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1182 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1183 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1184 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1185 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1187 $form->{defaults}{weightunit} ||= 'kg';
1191 $query = qq|SELECT c.id, c.accno, c.description, c.link
1193 WHERE c.link LIKE '%IC%'
1195 $sth = $dbh->prepare($query);
1196 $sth->execute || $self->dberror($query);
1198 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1199 foreach my $key (split(/:/, $ref->{link})) {
1202 if ($key =~ /cogs/) {
1203 $nkey = "IC_expense";
1205 if ($key =~ /sale/) {
1206 $nkey = "IC_income";
1208 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1210 description => $ref->{description}
1217 $query = qq|SELECT c.id, c.accno, c.description
1219 WHERE c.category = 'I'
1220 AND c.charttype = 'A'
1222 $sth = $dbh->prepare($query);
1223 $sth->execute || $self->dberror($query);
1225 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1226 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1228 description => $ref->{description}
1233 $query = qq|SELECT c.id, c.accno, c.description
1235 WHERE c.category = 'E'
1236 AND c.charttype = 'A'
1238 $sth = $dbh->prepare($query);
1239 $sth->execute || $self->dberror($query);
1241 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1242 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1244 description => $ref->{description}
1249 # now get the tax rates and numbers
1250 $query = qq|SELECT c.id, c.accno, c.description,
1251 t.rate * 100 AS rate, t.taxnumber
1253 WHERE c.id = t.chart_id|;
1255 $sth = $dbh->prepare($query);
1256 $sth->execute || $form->dberror($query);
1258 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1259 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1260 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1261 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1262 if $ref->{taxnumber};
1263 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1265 # Abfrage für Standard Umlaufvermögenskonto
1267 qq|SELECT id, accno, description, link | .
1269 qq|WHERE link LIKE ? |.
1271 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1272 $sth->execute || $form->dberror($query);#
1273 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1274 foreach my $item (split(/:/, $ref->{link})) {
1275 if ($item eq "AR_paid") {
1276 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1278 description => $ref->{description}
1287 $main::lxdebug->leave_sub();
1291 $main::lxdebug->enter_sub();
1293 my ($self, $myconfig, $form) = @_;
1295 my $dbh = $form->dbconnect($myconfig);
1297 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1298 my $sth = $dbh->prepare($query);
1299 $sth->execute || $form->dberror($query);
1301 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1307 $main::lxdebug->leave_sub();
1311 $main::lxdebug->enter_sub();
1313 my ($self, $myconfig, $form) = @_;
1315 my $dbh = $form->dbconnect($myconfig);
1317 my ($query, @values);
1319 if ($form->{revtrans}) {
1320 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1322 } elsif ($form->{closedto}) {
1323 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1324 @values = (conv_date($form->{closedto}));
1327 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1330 # set close in defaults
1331 do_query($form, $dbh, $query, @values);
1335 $main::lxdebug->leave_sub();
1339 my ($self, $units, $unit_name, $factor) = @_;
1341 $factor = 1 unless ($factor);
1343 my $unit = $units->{$unit_name};
1345 if (!defined($unit) || !$unit->{"base_unit"} ||
1346 ($unit_name eq $unit->{"base_unit"})) {
1347 return ($unit_name, $factor);
1350 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1353 sub retrieve_units {
1354 $main::lxdebug->enter_sub();
1356 my ($self, $myconfig, $form, $prefix) = @_;
1359 my $dbh = $form->get_standard_dbh;
1361 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1363 my $sth = prepare_execute_query($form, $dbh, $query);
1366 while (my $ref = $sth->fetchrow_hashref()) {
1367 $units->{$ref->{"name"}} = $ref;
1371 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1372 $sth = $dbh->prepare($query_lang);
1373 $sth->execute() || $form->dberror($query_lang);
1375 while (my $ref = $sth->fetchrow_hashref()) {
1376 push(@languages, $ref);
1380 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1381 "FROM units_language ul " .
1382 "LEFT JOIN language l ON ul.language_id = l.id " .
1383 "WHERE ul.unit = ?";
1384 $sth = $dbh->prepare($query_lang);
1386 foreach my $unit (values(%{$units})) {
1387 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1389 $unit->{"LANGUAGES"} = {};
1390 foreach my $lang (@languages) {
1391 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1394 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1395 while (my $ref = $sth->fetchrow_hashref()) {
1396 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1401 $main::lxdebug->leave_sub();
1406 sub retrieve_all_units {
1407 $main::lxdebug->enter_sub();
1411 if (!$::request->{cache}{all_units}) {
1412 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1415 $main::lxdebug->leave_sub();
1417 return $::request->{cache}{all_units};
1421 sub translate_units {
1422 $main::lxdebug->enter_sub();
1424 my ($self, $form, $template_code, $unit, $amount) = @_;
1426 my $units = $self->retrieve_units(\%main::myconfig, $form);
1428 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1429 my $new_unit = $unit;
1431 if (($amount != 1) && $h->{"localized_plural"}) {
1432 $new_unit = $h->{"localized_plural"};
1433 } elsif ($h->{"localized"}) {
1434 $new_unit = $h->{"localized"};
1438 $main::lxdebug->leave_sub();
1444 $main::lxdebug->enter_sub();
1446 my ($self, $myconfig, $form, $units) = @_;
1448 my $dbh = $form->dbconnect($myconfig);
1450 map({ $_->{"in_use"} = 0; } values(%{$units}));
1452 foreach my $unit (values(%{$units})) {
1453 my $base_unit = $unit->{"original_base_unit"};
1454 while ($base_unit) {
1455 $units->{$base_unit}->{"in_use"} = 1;
1456 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1457 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1458 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1462 foreach my $unit (values(%{$units})) {
1463 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1465 foreach my $table (qw(parts invoice orderitems)) {
1466 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1468 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1469 $query .= "= " . $dbh->quote($unit->{"name"});
1471 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1472 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1475 my ($count) = $dbh->selectrow_array($query);
1476 $form->dberror($query) if ($dbh->err);
1479 $unit->{"in_use"} = 1;
1487 $main::lxdebug->leave_sub();
1490 sub convertible_units {
1491 $main::lxdebug->enter_sub();
1495 my $filter_unit = shift;
1496 my $not_smaller = shift;
1498 my $conv_units = [];
1500 $filter_unit = $units->{$filter_unit};
1502 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1503 my $unit = $units->{$name};
1505 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1506 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1507 push @{$conv_units}, $unit;
1511 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1513 $main::lxdebug->leave_sub();
1518 # if $a is translatable to $b, return the factor between them.
1521 $main::lxdebug->enter_sub(2);
1522 my ($this, $a, $b, $all_units) = @_;
1525 $all_units = $this->retrieve_all_units;
1528 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1529 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1530 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1531 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1534 sub unit_select_data {
1535 $main::lxdebug->enter_sub();
1537 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1542 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1545 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1546 if (!$convertible_into ||
1547 ($units->{$convertible_into} &&
1548 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1549 push @{$select}, { "name" => $unit,
1550 "base_unit" => $units->{$unit}->{"base_unit"},
1551 "factor" => $units->{$unit}->{"factor"},
1552 "selected" => ($unit eq $selected) ? "selected" : "" };
1556 $main::lxdebug->leave_sub();
1561 sub unit_select_html {
1562 $main::lxdebug->enter_sub();
1564 my ($self, $units, $name, $selected, $convertible_into) = @_;
1566 my $select = "<select name=${name}>";
1568 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1569 if (!$convertible_into ||
1570 ($units->{$convertible_into} &&
1571 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1572 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1575 $select .= "</select>";
1577 $main::lxdebug->leave_sub();
1583 $main::lxdebug->enter_sub();
1587 my $units = $self->retrieve_all_units();
1592 while (2 <= scalar(@_)) {
1593 my $qty = shift(@_);
1594 my $unit = $units->{shift(@_)};
1596 croak "No unit defined with name $unit" if (!defined $unit);
1599 $base_unit = $unit->{base_unit};
1600 } elsif ($base_unit ne $unit->{base_unit}) {
1601 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1604 $sum += $qty * $unit->{factor};
1607 $main::lxdebug->leave_sub();
1609 return wantarray ? ($sum, $base_unit) : $sum;
1613 $main::lxdebug->enter_sub();
1615 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1617 my $dbh = $form->dbconnect_noauto($myconfig);
1619 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1620 my ($sortkey) = selectrow_query($form, $dbh, $query);
1622 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1623 "VALUES (?, ?, ?, ?)";
1624 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1627 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1628 my $sth = $dbh->prepare($query);
1629 foreach my $lang (@{$languages}) {
1630 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1631 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1639 $main::lxdebug->leave_sub();
1643 $main::lxdebug->enter_sub();
1645 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1647 my $dbh = $form->dbconnect_noauto($myconfig);
1649 my ($base_unit, $unit, $sth, $query);
1651 $query = "DELETE FROM units_language";
1652 $dbh->do($query) || $form->dberror($query);
1654 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1655 $query = "DELETE FROM units WHERE name IN (";
1656 map({ $query .= "?," } @{$delete_units});
1657 substr($query, -1, 1) = ")";
1658 $dbh->do($query, undef, @{$delete_units}) ||
1659 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1662 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1663 $sth = $dbh->prepare($query);
1665 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1666 my $sth_lang = $dbh->prepare($query_lang);
1668 foreach $unit (values(%{$units})) {
1669 $unit->{"depth"} = 0;
1670 my $base_unit = $unit;
1671 while ($base_unit->{"base_unit"}) {
1673 $base_unit = $units->{$base_unit->{"base_unit"}};
1677 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1678 if ($unit->{"LANGUAGES"}) {
1679 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1680 next unless ($lang->{"id"} && $lang->{"localized"});
1681 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1682 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1686 next if ($unit->{"unchanged_unit"});
1688 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1689 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1693 $sth_lang->finish();
1697 $main::lxdebug->leave_sub();
1701 $main::lxdebug->enter_sub();
1703 my ($self, $myconfig, $form) = @_;
1705 # connect to database
1706 my $dbh = $form->dbconnect($myconfig);
1708 my $query = qq|SELECT
1712 round(t.rate * 100, 2) AS rate,
1713 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1714 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1718 my $sth = $dbh->prepare($query);
1719 $sth->execute || $form->dberror($query);
1722 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1723 push @{ $form->{TAX} }, $ref;
1729 $main::lxdebug->leave_sub();
1732 sub get_tax_accounts {
1733 $main::lxdebug->enter_sub();
1735 my ($self, $myconfig, $form) = @_;
1737 my $dbh = $form->dbconnect($myconfig);
1739 # get Accounts from chart
1740 my $query = qq{ SELECT
1742 accno || ' - ' || description AS taxaccount
1744 WHERE link LIKE '%_tax%'
1748 my $sth = $dbh->prepare($query);
1749 $sth->execute || $form->dberror($query);
1751 $form->{ACCOUNTS} = [];
1752 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1753 push @{ $form->{ACCOUNTS} }, $ref;
1760 $main::lxdebug->leave_sub();
1764 $main::lxdebug->enter_sub();
1766 my ($self, $myconfig, $form) = @_;
1768 # connect to database
1769 my $dbh = $form->dbconnect($myconfig);
1771 my $query = qq|SELECT
1774 round(rate * 100, 2) AS rate,
1779 my $sth = $dbh->prepare($query);
1780 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1782 my $ref = $sth->fetchrow_hashref("NAME_lc");
1784 map { $form->{$_} = $ref->{$_} } keys %$ref;
1788 # see if it is used by a taxkey
1789 $query = qq|SELECT count(*) FROM taxkeys
1790 WHERE tax_id = ? AND chart_id >0|;
1792 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1794 $form->{orphaned} = !$form->{orphaned};
1797 if (!$form->{orphaned} ) {
1798 $query = qq|SELECT DISTINCT c.id, c.accno
1800 JOIN tax t ON (t.id = tk.tax_id)
1801 JOIN chart c ON (c.id = tk.chart_id)
1802 WHERE tk.tax_id = ?|;
1804 $sth = $dbh->prepare($query);
1805 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1807 $form->{TAXINUSE} = [];
1808 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1809 push @{ $form->{TAXINUSE} }, $ref;
1817 $main::lxdebug->leave_sub();
1821 $main::lxdebug->enter_sub();
1823 my ($self, $myconfig, $form) = @_;
1826 # connect to database
1827 my $dbh = $form->get_standard_dbh($myconfig);
1829 $form->{rate} = $form->{rate} / 100;
1831 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1832 if ($form->{id} ne "") {
1833 $query = qq|UPDATE tax SET
1838 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1840 push(@values, $form->{id});
1844 $query = qq|INSERT INTO tax (
1851 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1853 do_query($form, $dbh, $query, @values);
1857 $main::lxdebug->leave_sub();
1861 $main::lxdebug->enter_sub();
1863 my ($self, $myconfig, $form) = @_;
1866 # connect to database
1867 my $dbh = $form->get_standard_dbh($myconfig);
1869 $query = qq|DELETE FROM tax
1871 do_query($form, $dbh, $query, $form->{id});
1875 $main::lxdebug->leave_sub();
1878 sub save_price_factor {
1879 $main::lxdebug->enter_sub();
1881 my ($self, $myconfig, $form) = @_;
1883 # connect to database
1884 my $dbh = $form->get_standard_dbh($myconfig);
1887 my @values = ($form->{description}, conv_i($form->{factor}));
1890 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1891 push @values, conv_i($form->{id});
1894 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1897 do_query($form, $dbh, $query, @values);
1901 $main::lxdebug->leave_sub();
1904 sub get_all_price_factors {
1905 $main::lxdebug->enter_sub();
1907 my ($self, $myconfig, $form) = @_;
1909 # connect to database
1910 my $dbh = $form->get_standard_dbh($myconfig);
1912 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1914 $main::lxdebug->leave_sub();
1917 sub get_price_factor {
1918 $main::lxdebug->enter_sub();
1920 my ($self, $myconfig, $form) = @_;
1922 # connect to database
1923 my $dbh = $form->get_standard_dbh($myconfig);
1925 my $query = qq|SELECT description, factor,
1926 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1927 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1928 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1929 FROM price_factors WHERE id = ?|;
1931 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1933 $main::lxdebug->leave_sub();
1936 sub delete_price_factor {
1937 $main::lxdebug->enter_sub();
1939 my ($self, $myconfig, $form) = @_;
1941 # connect to database
1942 my $dbh = $form->get_standard_dbh($myconfig);
1944 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1947 $main::lxdebug->leave_sub();
1950 sub save_warehouse {
1951 $main::lxdebug->enter_sub();
1953 my ($self, $myconfig, $form) = @_;
1955 # connect to database
1956 my $dbh = $form->get_standard_dbh($myconfig);
1958 my ($query, @values, $sth);
1961 $query = qq|SELECT nextval('id')|;
1962 ($form->{id}) = selectrow_query($form, $dbh, $query);
1964 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1965 do_query($form, $dbh, $query, $form->{id});
1968 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1969 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1971 if (0 < $form->{number_of_new_bins}) {
1972 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1973 $sth = prepare_query($form, $dbh, $query);
1975 foreach my $i (1..$form->{number_of_new_bins}) {
1976 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1984 $main::lxdebug->leave_sub();
1988 $main::lxdebug->enter_sub();
1990 my ($self, $myconfig, $form) = @_;
1992 # connect to database
1993 my $dbh = $form->get_standard_dbh($myconfig);
1995 my ($query, @values, $commit_necessary, $sth);
1997 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2000 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2001 do_query($form, $dbh, $query, @values);
2003 $commit_necessary = 1;
2006 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2007 $sth = prepare_query($form, $dbh, $query);
2009 foreach my $row (1..$form->{rowcount}) {
2010 next if ($form->{"delete_${row}"});
2012 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2014 $commit_necessary = 1;
2019 $dbh->commit() if ($commit_necessary);
2021 $main::lxdebug->leave_sub();
2024 sub delete_warehouse {
2025 $main::lxdebug->enter_sub();
2027 my ($self, $myconfig, $form) = @_;
2029 # connect to database
2030 my $dbh = $form->get_standard_dbh($myconfig);
2032 my $id = conv_i($form->{id});
2033 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|;
2034 my ($count) = selectrow_query($form, $dbh, $query, $id);
2037 $main::lxdebug->leave_sub();
2041 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2042 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2046 $main::lxdebug->leave_sub();
2051 sub get_all_warehouses {
2052 $main::lxdebug->enter_sub();
2054 my ($self, $myconfig, $form) = @_;
2056 # connect to database
2057 my $dbh = $form->get_standard_dbh($myconfig);
2059 my $query = qq|SELECT w.id, w.description, w.invalid,
2060 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2062 ORDER BY w.sortkey|;
2064 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2066 $main::lxdebug->leave_sub();
2070 $main::lxdebug->enter_sub();
2072 my ($self, $myconfig, $form) = @_;
2074 # connect to database
2075 my $dbh = $form->get_standard_dbh($myconfig);
2077 my $id = conv_i($form->{id});
2078 my $query = qq|SELECT w.description, w.invalid
2082 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2084 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2086 $query = qq|SELECT b.*, EXISTS
2087 (SELECT i.warehouse_id
2089 WHERE i.bin_id = b.id
2093 WHERE b.warehouse_id = ?|;
2095 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2097 $main::lxdebug->leave_sub();