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 @values = ($form->{accno});
254 $query .= ' AND NOT id = ?';
255 push(@values, $form->{id});
258 my ($accno) = selectrow_query($form, $dbh, $query, @values);
261 $form->error($::locale->text('Account number not unique!'));
265 if (!$form->{id} || $form->{id} eq "") {
266 $query = qq|SELECT nextval('id')|;
267 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
268 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
269 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
277 # if charttype is heading make sure certain values are empty
278 # specifically, if charttype is changed from an existing account, empty the
279 # fields unnecessary for headings, so that e.g. heading doesn't appear in
280 # drop-down menues due to still having a valid "link" entry
282 if ( $form->{charttype} eq 'H' ) {
284 $form->{pos_bwa} = '';
285 $form->{pos_bilanz} = '';
286 $form->{pos_eur} = '';
287 $form->{new_chart_id} = '';
288 $form->{valid_from} = '';
291 $query = qq|UPDATE chart SET
307 $form->{description},
311 conv_i($form->{pos_bwa}),
312 conv_i($form->{pos_bilanz}),
313 conv_i($form->{pos_eur}),
314 conv_i($form->{new_chart_id}),
315 conv_date($form->{valid_from}),
316 ($form->{datevautomatik} eq 'T') ? 'true':'false',
323 do_query($form, $dbh, $query, @values);
329 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
333 for $tk_count (0 .. $MAX_TRIES) {
337 # Check if the account already exists, else cancel
339 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
341 last READTAXKEYS if ( $form->{'id'} == 0);
343 # check if there is a startdate
344 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
349 # Add valid taxkeys into the array
352 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
353 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
354 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
355 chart_id => conv_i($form->{"id"}),
356 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
357 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
364 for my $j (0 .. $#taxkeys){
365 if ( defined $taxkeys[$j]{'id'} ){
368 if ($taxkeys[$j]{'delete'}){
370 DELETE FROM taxkeys WHERE id = ?
373 @values = ($taxkeys[$j]{'id'});
375 do_query($form, $dbh, $query, @values);
384 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
392 $taxkeys[$j]{'tax_id'},
393 $taxkeys[$j]{'chart_id'},
394 $taxkeys[$j]{'tax_id'},
395 $taxkeys[$j]{'pos_ustva'},
396 $taxkeys[$j]{'startdate'},
399 do_query($form, $dbh, $query, @values);
405 INSERT INTO taxkeys (
412 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
415 $taxkeys[$j]{'tax_id'},
416 $taxkeys[$j]{'chart_id'},
417 $taxkeys[$j]{'tax_id'},
418 $taxkeys[$j]{'pos_ustva'},
419 $taxkeys[$j]{'startdate'},
422 do_query($form, $dbh, $query, @values);
427 # Update chart.taxkey_id to the latest from taxkeys for this chart.
433 WHERE taxkeys.chart_id = chart.id
434 ORDER BY startdate DESC
440 do_query($form, $dbh, $query, $form->{id});
443 my $rc = $dbh->commit;
446 $main::lxdebug->leave_sub();
452 $main::lxdebug->enter_sub();
454 my ($self, $myconfig, $form) = @_;
456 # connect to database, turn off AutoCommit
457 my $dbh = $form->dbconnect_noauto($myconfig);
459 my $query = qq|SELECT count(*) FROM acc_trans a
460 WHERE a.chart_id = ?|;
461 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
465 $main::lxdebug->leave_sub();
469 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
470 foreach my $type (qw(inventory income expense)) {
473 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
474 qq|WHERE ${type}_accno_id = ?|;
475 do_query($form, $dbh, $query, $form->{id});
478 foreach my $table (qw(partstax customertax vendortax tax)) {
479 $query = qq|DELETE FROM $table
481 do_query($form, $dbh, $query, $form->{id});
484 # delete chart of account record
485 $query = qq|DELETE FROM chart
487 do_query($form, $dbh, $query, $form->{id});
489 # delete account taxkeys
490 $query = qq|DELETE FROM taxkeys
492 do_query($form, $dbh, $query, $form->{id});
494 # commit and redirect
495 my $rc = $dbh->commit;
498 $main::lxdebug->leave_sub();
504 $main::lxdebug->enter_sub();
506 my ($self, $myconfig, $form) = @_;
508 # connect to database
509 my $dbh = $form->dbconnect($myconfig);
511 my $query = qq|SELECT id, lead
515 my $sth = $dbh->prepare($query);
516 $sth->execute || $form->dberror($query);
518 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
519 push @{ $form->{ALL} }, $ref;
525 $main::lxdebug->leave_sub();
529 $main::lxdebug->enter_sub();
531 my ($self, $myconfig, $form) = @_;
533 # connect to database
534 my $dbh = $form->dbconnect($myconfig);
537 qq|SELECT l.id, l.lead | .
540 my $sth = $dbh->prepare($query);
541 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
543 my $ref = $sth->fetchrow_hashref("NAME_lc");
545 map { $form->{$_} = $ref->{$_} } keys %$ref;
551 $main::lxdebug->leave_sub();
555 $main::lxdebug->enter_sub();
557 my ($self, $myconfig, $form) = @_;
560 # connect to database
561 my $dbh = $form->dbconnect($myconfig);
563 my @values = ($form->{description});
564 # id is the old record
566 $query = qq|UPDATE leads SET
569 push(@values, $form->{id});
571 $query = qq|INSERT INTO leads
575 do_query($form, $dbh, $query, @values);
579 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my ($self, $myconfig, $form) = @_;
588 # connect to database
589 my $dbh = $form->dbconnect($myconfig);
591 $query = qq|DELETE FROM leads
593 do_query($form, $dbh, $query, $form->{id});
597 $main::lxdebug->leave_sub();
601 $main::lxdebug->enter_sub();
603 my ($self, $myconfig, $form, $return_list) = @_;
605 # connect to database
606 my $dbh = $form->dbconnect($myconfig);
609 "SELECT id, description, template_code, article_code, " .
610 " output_numberformat, output_dateformat, output_longdates " .
611 "FROM language ORDER BY description";
613 my $sth = $dbh->prepare($query);
614 $sth->execute || $form->dberror($query);
618 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
619 push(@{ $ary }, $ref);
625 $main::lxdebug->leave_sub();
635 $main::lxdebug->enter_sub();
637 my ($self, $myconfig, $form) = @_;
639 # connect to database
640 my $dbh = $form->dbconnect($myconfig);
643 "SELECT description, template_code, article_code, " .
644 " output_numberformat, output_dateformat, output_longdates " .
645 "FROM language WHERE id = ?";
646 my $sth = $dbh->prepare($query);
647 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
649 my $ref = $sth->fetchrow_hashref("NAME_lc");
651 map { $form->{$_} = $ref->{$_} } keys %$ref;
657 $main::lxdebug->leave_sub();
660 sub get_language_details {
661 $main::lxdebug->enter_sub();
663 my ($self, $myconfig, $form, $id) = @_;
665 # connect to database
666 my $dbh = $form->dbconnect($myconfig);
669 "SELECT template_code, " .
670 " output_numberformat, output_dateformat, output_longdates " .
671 "FROM language WHERE id = ?";
672 my @res = selectrow_query($form, $dbh, $query, $id);
675 $main::lxdebug->leave_sub();
681 $main::lxdebug->enter_sub();
683 my ($self, $myconfig, $form) = @_;
685 # connect to database
686 my $dbh = $form->dbconnect($myconfig);
687 my (@values, $query);
689 map({ push(@values, $form->{$_}); }
690 qw(description template_code article_code
691 output_numberformat output_dateformat output_longdates));
693 # id is the old record
696 "UPDATE language SET " .
697 " description = ?, template_code = ?, article_code = ?, " .
698 " output_numberformat = ?, output_dateformat = ?, " .
699 " output_longdates = ? " .
701 push(@values, $form->{id});
704 "INSERT INTO language (" .
705 " description, template_code, article_code, " .
706 " output_numberformat, output_dateformat, output_longdates" .
707 ") VALUES (?, ?, ?, ?, ?, ?)";
709 do_query($form, $dbh, $query, @values);
713 $main::lxdebug->leave_sub();
716 sub delete_language {
717 $main::lxdebug->enter_sub();
719 my ($self, $myconfig, $form) = @_;
722 # connect to database
723 my $dbh = $form->dbconnect_noauto($myconfig);
725 foreach my $table (qw(generic_translations units_language)) {
726 $query = qq|DELETE FROM $table WHERE language_id = ?|;
727 do_query($form, $dbh, $query, $form->{"id"});
730 $query = "DELETE FROM language WHERE id = ?";
731 do_query($form, $dbh, $query, $form->{"id"});
736 $main::lxdebug->leave_sub();
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $form) = @_;
745 # connect to database
746 my $dbh = $form->dbconnect($myconfig);
748 my $query = qq|SELECT id, description,
750 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
752 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
754 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
756 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
758 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
760 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
762 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
764 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
766 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
770 my $sth = $dbh->prepare($query);
771 $sth->execute || $form->dberror($query);
774 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
775 push @{ $form->{ALL} }, $ref;
781 $main::lxdebug->leave_sub();
784 sub get_buchungsgruppe {
785 $main::lxdebug->enter_sub();
787 my ($self, $myconfig, $form) = @_;
790 # connect to database
791 my $dbh = $form->dbconnect($myconfig);
795 qq|SELECT description, inventory_accno_id,
796 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
798 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
800 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
802 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
804 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
806 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
808 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
810 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
812 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
815 my $sth = $dbh->prepare($query);
816 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
818 my $ref = $sth->fetchrow_hashref("NAME_lc");
820 map { $form->{$_} = $ref->{$_} } keys %$ref;
825 qq|SELECT count(id) = 0 AS orphaned
827 WHERE buchungsgruppen_id = ?|;
828 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
831 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
833 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
834 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
837 $query = qq|SELECT c.accno, c.description, c.link, c.id,
838 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
839 FROM chart c, defaults d
840 WHERE c.link LIKE '%$module%'
844 my $sth = $dbh->prepare($query);
845 $sth->execute || $form->dberror($query);
846 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
847 foreach my $key (split(/:/, $ref->{link})) {
848 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
849 $form->{"std_inventory_accno_id"} = $ref->{"id"};
851 if ($key =~ /$module/) {
852 if ( ($ref->{id} eq $ref->{inventory_accno_id})
853 || ($ref->{id} eq $ref->{income_accno_id})
854 || ($ref->{id} eq $ref->{expense_accno_id})) {
855 push @{ $form->{"${module}_links"}{$key} },
856 { accno => $ref->{accno},
857 description => $ref->{description},
858 selected => "selected",
861 push @{ $form->{"${module}_links"}{$key} },
862 { accno => $ref->{accno},
863 description => $ref->{description},
875 $main::lxdebug->leave_sub();
878 sub save_buchungsgruppe {
879 $main::lxdebug->enter_sub();
881 my ($self, $myconfig, $form) = @_;
883 # connect to database
884 my $dbh = $form->dbconnect($myconfig);
886 my @values = ($form->{description}, $form->{inventory_accno_id},
887 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
888 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
889 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
890 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
894 # id is the old record
896 $query = qq|UPDATE buchungsgruppen SET
897 description = ?, inventory_accno_id = ?,
898 income_accno_id_0 = ?, expense_accno_id_0 = ?,
899 income_accno_id_1 = ?, expense_accno_id_1 = ?,
900 income_accno_id_2 = ?, expense_accno_id_2 = ?,
901 income_accno_id_3 = ?, expense_accno_id_3 = ?
903 push(@values, $form->{id});
905 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
906 my ($sortkey) = $dbh->selectrow_array($query);
907 $form->dberror($query) if ($dbh->err);
908 push(@values, $sortkey);
909 $query = qq|INSERT INTO buchungsgruppen
910 (description, inventory_accno_id,
911 income_accno_id_0, expense_accno_id_0,
912 income_accno_id_1, expense_accno_id_1,
913 income_accno_id_2, expense_accno_id_2,
914 income_accno_id_3, expense_accno_id_3,
916 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
918 do_query($form, $dbh, $query, @values);
922 $main::lxdebug->leave_sub();
925 sub delete_buchungsgruppe {
926 $main::lxdebug->enter_sub();
928 my ($self, $myconfig, $form) = @_;
930 # connect to database
931 my $dbh = $form->dbconnect($myconfig);
933 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
934 do_query($form, $dbh, $query, $form->{id});
938 $main::lxdebug->leave_sub();
942 $main::lxdebug->enter_sub();
944 my ($self, $myconfig, $form, $table) = @_;
946 # connect to database
947 my $dbh = $form->get_standard_dbh($myconfig);
951 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
952 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
953 my @values = ($form->{"id1"}, $form->{"id2"});
954 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
956 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
957 my $sth = prepare_query($form, $dbh, $query);
959 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
960 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
966 $main::lxdebug->leave_sub();
969 sub prepare_template_filename {
970 $main::lxdebug->enter_sub();
972 my ($self, $myconfig, $form) = @_;
974 my ($filename, $display_filename);
976 if ($form->{type} eq "stylesheet") {
977 $filename = "css/$myconfig->{stylesheet}";
978 $display_filename = $myconfig->{stylesheet};
981 $filename = $form->{formname};
983 if ($form->{language}) {
984 my ($id, $template_code) = split(/--/, $form->{language});
985 $filename .= "_${template_code}";
988 if ($form->{printer}) {
989 my ($id, $template_code) = split(/--/, $form->{printer});
990 $filename .= "_${template_code}";
993 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
994 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
995 $filename =~ s|.*/||;
997 $display_filename = $filename;
998 $filename = "$myconfig->{templates}/$filename";
1001 $main::lxdebug->leave_sub();
1003 return ($filename, $display_filename);
1008 $main::lxdebug->enter_sub();
1010 my ($self, $filename) = @_;
1012 my ($content, $lines) = ("", 0);
1016 if (open(TEMPLATE, $filename)) {
1017 while (<TEMPLATE>) {
1024 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1026 $main::lxdebug->leave_sub();
1028 return ($content, $lines);
1032 $main::lxdebug->enter_sub();
1034 my ($self, $filename, $content) = @_;
1040 if (open(TEMPLATE, ">", $filename)) {
1041 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1042 $content =~ s/\r\n/\n/g;
1043 print(TEMPLATE $content);
1049 $main::lxdebug->leave_sub();
1055 $main::lxdebug->enter_sub();
1060 my $myconfig = \%main::myconfig;
1061 my $form = $main::form;
1063 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1066 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1068 $form->{curr} =~ s/ //g;
1069 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1070 my $currency = join ':', @currencies;
1072 # these defaults are database wide
1075 qq|UPDATE defaults SET
1076 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1077 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1078 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1079 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1080 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1081 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1098 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1099 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1100 $form->{invnumber}, $form->{cnnumber},
1101 $form->{sonumber}, $form->{ponumber},
1102 $form->{sqnumber}, $form->{rfqnumber},
1103 $form->{customernumber}, $form->{vendornumber},
1104 $form->{articlenumber}, $form->{servicenumber},
1105 $form->{sdonumber}, $form->{pdonumber},
1107 $form->{businessnumber}, $form->{weightunit},
1108 conv_i($form->{language_id}));
1109 do_query($form, $dbh, $query, @values);
1113 $main::lxdebug->leave_sub();
1117 sub save_preferences {
1118 $main::lxdebug->enter_sub();
1120 my ($self, $myconfig, $form) = @_;
1122 my $dbh = $form->get_standard_dbh($myconfig);
1124 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1127 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1128 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1130 my $rc = $dbh->commit();
1132 # save first currency in myconfig
1133 $currency =~ s/:.*//;
1134 $form->{currency} = $currency;
1136 $form->{businessnumber} = $businessnumber;
1138 $myconfig = User->new(login => $form->{login});
1140 foreach my $item (keys %$form) {
1141 $myconfig->{$item} = $form->{$item};
1144 $myconfig->save_member;
1146 my $auth = $main::auth;
1148 $main::lxdebug->leave_sub();
1154 $main::lxdebug->enter_sub();
1159 my $myconfig = \%main::myconfig;
1160 my $form = $main::form;
1162 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1164 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1166 $defaults->{weightunit} ||= 'kg';
1168 $main::lxdebug->leave_sub();
1173 sub defaultaccounts {
1174 $main::lxdebug->enter_sub();
1176 my ($self, $myconfig, $form) = @_;
1178 # connect to database
1179 my $dbh = $form->dbconnect($myconfig);
1181 # get defaults from defaults table
1182 my $query = qq|SELECT * FROM defaults|;
1183 my $sth = $dbh->prepare($query);
1184 $sth->execute || $form->dberror($query);
1186 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1187 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1188 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1189 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1190 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1191 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1192 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1194 $form->{defaults}{weightunit} ||= 'kg';
1198 $query = qq|SELECT c.id, c.accno, c.description, c.link
1200 WHERE c.link LIKE '%IC%'
1202 $sth = $dbh->prepare($query);
1203 $sth->execute || $self->dberror($query);
1205 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1206 foreach my $key (split(/:/, $ref->{link})) {
1209 if ($key =~ /cogs/) {
1210 $nkey = "IC_expense";
1212 if ($key =~ /sale/) {
1213 $nkey = "IC_income";
1215 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1217 description => $ref->{description}
1224 $query = qq|SELECT c.id, c.accno, c.description
1226 WHERE c.category = 'I'
1227 AND c.charttype = 'A'
1229 $sth = $dbh->prepare($query);
1230 $sth->execute || $self->dberror($query);
1232 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1233 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1235 description => $ref->{description}
1240 $query = qq|SELECT c.id, c.accno, c.description
1242 WHERE c.category = 'E'
1243 AND c.charttype = 'A'
1245 $sth = $dbh->prepare($query);
1246 $sth->execute || $self->dberror($query);
1248 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1249 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1251 description => $ref->{description}
1256 # now get the tax rates and numbers
1257 $query = qq|SELECT c.id, c.accno, c.description,
1258 t.rate * 100 AS rate, t.taxnumber
1260 WHERE c.id = t.chart_id|;
1262 $sth = $dbh->prepare($query);
1263 $sth->execute || $form->dberror($query);
1265 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1266 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1267 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1268 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1269 if $ref->{taxnumber};
1270 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1272 # Abfrage für Standard Umlaufvermögenskonto
1274 qq|SELECT id, accno, description, link | .
1276 qq|WHERE link LIKE ? |.
1278 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1279 $sth->execute || $form->dberror($query);#
1280 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1281 foreach my $item (split(/:/, $ref->{link})) {
1282 if ($item eq "AR_paid") {
1283 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1285 description => $ref->{description}
1294 $main::lxdebug->leave_sub();
1298 $main::lxdebug->enter_sub();
1300 my ($self, $myconfig, $form) = @_;
1302 my $dbh = $form->dbconnect($myconfig);
1304 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1305 my $sth = $dbh->prepare($query);
1306 $sth->execute || $form->dberror($query);
1308 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1314 $main::lxdebug->leave_sub();
1318 $main::lxdebug->enter_sub();
1320 my ($self, $myconfig, $form) = @_;
1322 my $dbh = $form->dbconnect($myconfig);
1324 my ($query, @values);
1326 if ($form->{revtrans}) {
1327 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1329 } elsif ($form->{closedto}) {
1330 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1331 @values = (conv_date($form->{closedto}));
1334 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1337 # set close in defaults
1338 do_query($form, $dbh, $query, @values);
1342 $main::lxdebug->leave_sub();
1346 my ($self, $units, $unit_name, $factor) = @_;
1348 $factor = 1 unless ($factor);
1350 my $unit = $units->{$unit_name};
1352 if (!defined($unit) || !$unit->{"base_unit"} ||
1353 ($unit_name eq $unit->{"base_unit"})) {
1354 return ($unit_name, $factor);
1357 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1360 sub retrieve_units {
1361 $main::lxdebug->enter_sub();
1363 my ($self, $myconfig, $form, $prefix) = @_;
1366 my $dbh = $form->get_standard_dbh;
1368 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1370 my $sth = prepare_execute_query($form, $dbh, $query);
1373 while (my $ref = $sth->fetchrow_hashref()) {
1374 $units->{$ref->{"name"}} = $ref;
1378 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1379 $sth = $dbh->prepare($query_lang);
1380 $sth->execute() || $form->dberror($query_lang);
1382 while (my $ref = $sth->fetchrow_hashref()) {
1383 push(@languages, $ref);
1387 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1388 "FROM units_language ul " .
1389 "LEFT JOIN language l ON ul.language_id = l.id " .
1390 "WHERE ul.unit = ?";
1391 $sth = $dbh->prepare($query_lang);
1393 foreach my $unit (values(%{$units})) {
1394 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1396 $unit->{"LANGUAGES"} = {};
1397 foreach my $lang (@languages) {
1398 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1401 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1402 while (my $ref = $sth->fetchrow_hashref()) {
1403 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1408 $main::lxdebug->leave_sub();
1413 sub retrieve_all_units {
1414 $main::lxdebug->enter_sub();
1418 if (!$::request->{cache}{all_units}) {
1419 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1422 $main::lxdebug->leave_sub();
1424 return $::request->{cache}{all_units};
1428 sub translate_units {
1429 $main::lxdebug->enter_sub();
1431 my ($self, $form, $template_code, $unit, $amount) = @_;
1433 my $units = $self->retrieve_units(\%main::myconfig, $form);
1435 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1436 my $new_unit = $unit;
1438 if (($amount != 1) && $h->{"localized_plural"}) {
1439 $new_unit = $h->{"localized_plural"};
1440 } elsif ($h->{"localized"}) {
1441 $new_unit = $h->{"localized"};
1445 $main::lxdebug->leave_sub();
1451 $main::lxdebug->enter_sub();
1453 my ($self, $myconfig, $form, $units) = @_;
1455 my $dbh = $form->dbconnect($myconfig);
1457 map({ $_->{"in_use"} = 0; } values(%{$units}));
1459 foreach my $unit (values(%{$units})) {
1460 my $base_unit = $unit->{"original_base_unit"};
1461 while ($base_unit) {
1462 $units->{$base_unit}->{"in_use"} = 1;
1463 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1464 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1465 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1469 foreach my $unit (values(%{$units})) {
1470 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1472 foreach my $table (qw(parts invoice orderitems)) {
1473 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1475 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1476 $query .= "= " . $dbh->quote($unit->{"name"});
1478 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1479 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1482 my ($count) = $dbh->selectrow_array($query);
1483 $form->dberror($query) if ($dbh->err);
1486 $unit->{"in_use"} = 1;
1494 $main::lxdebug->leave_sub();
1497 sub convertible_units {
1498 $main::lxdebug->enter_sub();
1502 my $filter_unit = shift;
1503 my $not_smaller = shift;
1505 my $conv_units = [];
1507 $filter_unit = $units->{$filter_unit};
1509 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1510 my $unit = $units->{$name};
1512 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1513 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1514 push @{$conv_units}, $unit;
1518 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1520 $main::lxdebug->leave_sub();
1525 # if $a is translatable to $b, return the factor between them.
1528 $main::lxdebug->enter_sub(2);
1529 my ($this, $a, $b, $all_units) = @_;
1532 $all_units = $this->retrieve_all_units;
1535 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1536 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1537 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1538 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1541 sub unit_select_data {
1542 $main::lxdebug->enter_sub();
1544 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1549 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1552 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1553 if (!$convertible_into ||
1554 ($units->{$convertible_into} &&
1555 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1556 push @{$select}, { "name" => $unit,
1557 "base_unit" => $units->{$unit}->{"base_unit"},
1558 "factor" => $units->{$unit}->{"factor"},
1559 "selected" => ($unit eq $selected) ? "selected" : "" };
1563 $main::lxdebug->leave_sub();
1568 sub unit_select_html {
1569 $main::lxdebug->enter_sub();
1571 my ($self, $units, $name, $selected, $convertible_into) = @_;
1573 my $select = "<select name=${name}>";
1575 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1576 if (!$convertible_into ||
1577 ($units->{$convertible_into} &&
1578 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1579 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1582 $select .= "</select>";
1584 $main::lxdebug->leave_sub();
1590 $main::lxdebug->enter_sub();
1594 my $units = $self->retrieve_all_units();
1599 while (2 <= scalar(@_)) {
1600 my $qty = shift(@_);
1601 my $unit = $units->{shift(@_)};
1603 croak "No unit defined with name $unit" if (!defined $unit);
1606 $base_unit = $unit->{base_unit};
1607 } elsif ($base_unit ne $unit->{base_unit}) {
1608 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1611 $sum += $qty * $unit->{factor};
1614 $main::lxdebug->leave_sub();
1616 return wantarray ? ($sum, $base_unit) : $sum;
1620 $main::lxdebug->enter_sub();
1622 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1624 my $dbh = $form->dbconnect_noauto($myconfig);
1626 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1627 my ($sortkey) = selectrow_query($form, $dbh, $query);
1629 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1630 "VALUES (?, ?, ?, ?)";
1631 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1634 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1635 my $sth = $dbh->prepare($query);
1636 foreach my $lang (@{$languages}) {
1637 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1638 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1646 $main::lxdebug->leave_sub();
1650 $main::lxdebug->enter_sub();
1652 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1654 my $dbh = $form->dbconnect_noauto($myconfig);
1656 my ($base_unit, $unit, $sth, $query);
1658 $query = "DELETE FROM units_language";
1659 $dbh->do($query) || $form->dberror($query);
1661 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1662 $query = "DELETE FROM units WHERE name IN (";
1663 map({ $query .= "?," } @{$delete_units});
1664 substr($query, -1, 1) = ")";
1665 $dbh->do($query, undef, @{$delete_units}) ||
1666 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1669 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1670 $sth = $dbh->prepare($query);
1672 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1673 my $sth_lang = $dbh->prepare($query_lang);
1675 foreach $unit (values(%{$units})) {
1676 $unit->{"depth"} = 0;
1677 my $base_unit = $unit;
1678 while ($base_unit->{"base_unit"}) {
1680 $base_unit = $units->{$base_unit->{"base_unit"}};
1684 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1685 if ($unit->{"LANGUAGES"}) {
1686 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1687 next unless ($lang->{"id"} && $lang->{"localized"});
1688 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1689 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1693 next if ($unit->{"unchanged_unit"});
1695 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1696 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1700 $sth_lang->finish();
1704 $main::lxdebug->leave_sub();
1708 $main::lxdebug->enter_sub();
1710 my ($self, $myconfig, $form) = @_;
1712 # connect to database
1713 my $dbh = $form->dbconnect($myconfig);
1715 my $query = qq|SELECT
1719 round(t.rate * 100, 2) AS rate,
1720 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1721 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1725 my $sth = $dbh->prepare($query);
1726 $sth->execute || $form->dberror($query);
1729 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1730 push @{ $form->{TAX} }, $ref;
1736 $main::lxdebug->leave_sub();
1739 sub get_tax_accounts {
1740 $main::lxdebug->enter_sub();
1742 my ($self, $myconfig, $form) = @_;
1744 my $dbh = $form->dbconnect($myconfig);
1746 # get Accounts from chart
1747 my $query = qq{ SELECT
1749 accno || ' - ' || description AS taxaccount
1751 WHERE link LIKE '%_tax%'
1755 my $sth = $dbh->prepare($query);
1756 $sth->execute || $form->dberror($query);
1758 $form->{ACCOUNTS} = [];
1759 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1760 push @{ $form->{ACCOUNTS} }, $ref;
1767 $main::lxdebug->leave_sub();
1771 $main::lxdebug->enter_sub();
1773 my ($self, $myconfig, $form) = @_;
1775 # connect to database
1776 my $dbh = $form->dbconnect($myconfig);
1778 my $query = qq|SELECT
1781 round(rate * 100, 2) AS rate,
1786 my $sth = $dbh->prepare($query);
1787 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1789 my $ref = $sth->fetchrow_hashref("NAME_lc");
1791 map { $form->{$_} = $ref->{$_} } keys %$ref;
1795 # see if it is used by a taxkey
1796 $query = qq|SELECT count(*) FROM taxkeys
1797 WHERE tax_id = ? AND chart_id >0|;
1799 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1801 $form->{orphaned} = !$form->{orphaned};
1804 if (!$form->{orphaned} ) {
1805 $query = qq|SELECT DISTINCT c.id, c.accno
1807 JOIN tax t ON (t.id = tk.tax_id)
1808 JOIN chart c ON (c.id = tk.chart_id)
1809 WHERE tk.tax_id = ?|;
1811 $sth = $dbh->prepare($query);
1812 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1814 $form->{TAXINUSE} = [];
1815 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1816 push @{ $form->{TAXINUSE} }, $ref;
1824 $main::lxdebug->leave_sub();
1828 $main::lxdebug->enter_sub();
1830 my ($self, $myconfig, $form) = @_;
1833 # connect to database
1834 my $dbh = $form->get_standard_dbh($myconfig);
1836 $form->{rate} = $form->{rate} / 100;
1838 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1839 if ($form->{id} ne "") {
1840 $query = qq|UPDATE tax SET
1845 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1847 push(@values, $form->{id});
1851 $query = qq|INSERT INTO tax (
1858 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1860 do_query($form, $dbh, $query, @values);
1864 $main::lxdebug->leave_sub();
1868 $main::lxdebug->enter_sub();
1870 my ($self, $myconfig, $form) = @_;
1873 # connect to database
1874 my $dbh = $form->get_standard_dbh($myconfig);
1876 $query = qq|DELETE FROM tax
1878 do_query($form, $dbh, $query, $form->{id});
1882 $main::lxdebug->leave_sub();
1885 sub save_price_factor {
1886 $main::lxdebug->enter_sub();
1888 my ($self, $myconfig, $form) = @_;
1890 # connect to database
1891 my $dbh = $form->get_standard_dbh($myconfig);
1894 my @values = ($form->{description}, conv_i($form->{factor}));
1897 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1898 push @values, conv_i($form->{id});
1901 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1904 do_query($form, $dbh, $query, @values);
1908 $main::lxdebug->leave_sub();
1911 sub get_all_price_factors {
1912 $main::lxdebug->enter_sub();
1914 my ($self, $myconfig, $form) = @_;
1916 # connect to database
1917 my $dbh = $form->get_standard_dbh($myconfig);
1919 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1921 $main::lxdebug->leave_sub();
1924 sub get_price_factor {
1925 $main::lxdebug->enter_sub();
1927 my ($self, $myconfig, $form) = @_;
1929 # connect to database
1930 my $dbh = $form->get_standard_dbh($myconfig);
1932 my $query = qq|SELECT description, factor,
1933 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1934 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1935 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1936 FROM price_factors WHERE id = ?|;
1938 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1940 $main::lxdebug->leave_sub();
1943 sub delete_price_factor {
1944 $main::lxdebug->enter_sub();
1946 my ($self, $myconfig, $form) = @_;
1948 # connect to database
1949 my $dbh = $form->get_standard_dbh($myconfig);
1951 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1954 $main::lxdebug->leave_sub();
1957 sub save_warehouse {
1958 $main::lxdebug->enter_sub();
1960 my ($self, $myconfig, $form) = @_;
1962 # connect to database
1963 my $dbh = $form->get_standard_dbh($myconfig);
1965 my ($query, @values, $sth);
1968 $query = qq|SELECT nextval('id')|;
1969 ($form->{id}) = selectrow_query($form, $dbh, $query);
1971 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1972 do_query($form, $dbh, $query, $form->{id});
1975 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1976 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1978 if (0 < $form->{number_of_new_bins}) {
1979 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1980 $sth = prepare_query($form, $dbh, $query);
1982 foreach my $i (1..$form->{number_of_new_bins}) {
1983 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1991 $main::lxdebug->leave_sub();
1995 $main::lxdebug->enter_sub();
1997 my ($self, $myconfig, $form) = @_;
1999 # connect to database
2000 my $dbh = $form->get_standard_dbh($myconfig);
2002 my ($query, @values, $commit_necessary, $sth);
2004 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2007 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2008 do_query($form, $dbh, $query, @values);
2010 $commit_necessary = 1;
2013 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2014 $sth = prepare_query($form, $dbh, $query);
2016 foreach my $row (1..$form->{rowcount}) {
2017 next if ($form->{"delete_${row}"});
2019 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2021 $commit_necessary = 1;
2026 $dbh->commit() if ($commit_necessary);
2028 $main::lxdebug->leave_sub();
2031 sub delete_warehouse {
2032 $main::lxdebug->enter_sub();
2034 my ($self, $myconfig, $form) = @_;
2036 # connect to database
2037 my $dbh = $form->get_standard_dbh($myconfig);
2039 my $id = conv_i($form->{id});
2040 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|;
2041 my ($count) = selectrow_query($form, $dbh, $query, $id);
2044 $main::lxdebug->leave_sub();
2048 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2049 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2053 $main::lxdebug->leave_sub();
2058 sub get_all_warehouses {
2059 $main::lxdebug->enter_sub();
2061 my ($self, $myconfig, $form) = @_;
2063 # connect to database
2064 my $dbh = $form->get_standard_dbh($myconfig);
2066 my $query = qq|SELECT w.id, w.description, w.invalid,
2067 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2069 ORDER BY w.sortkey|;
2071 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2073 $main::lxdebug->leave_sub();
2077 $main::lxdebug->enter_sub();
2079 my ($self, $myconfig, $form) = @_;
2081 # connect to database
2082 my $dbh = $form->get_standard_dbh($myconfig);
2084 my $id = conv_i($form->{id});
2085 my $query = qq|SELECT w.description, w.invalid
2089 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2091 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2093 $query = qq|SELECT b.*, EXISTS
2094 (SELECT i.warehouse_id
2096 WHERE i.bin_id = b.id
2100 WHERE b.warehouse_id = ?|;
2102 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2104 $main::lxdebug->leave_sub();