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 $query = qq|DELETE FROM tax
480 do_query($form, $dbh, $query, $form->{id});
482 # delete chart of account record
483 $query = qq|DELETE FROM chart
485 do_query($form, $dbh, $query, $form->{id});
487 # delete account taxkeys
488 $query = qq|DELETE FROM taxkeys
490 do_query($form, $dbh, $query, $form->{id});
492 # commit and redirect
493 my $rc = $dbh->commit;
496 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
506 # connect to database
507 my $dbh = $form->dbconnect($myconfig);
509 my $query = qq|SELECT id, lead
513 my $sth = $dbh->prepare($query);
514 $sth->execute || $form->dberror($query);
516 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
517 push @{ $form->{ALL} }, $ref;
523 $main::lxdebug->leave_sub();
527 $main::lxdebug->enter_sub();
529 my ($self, $myconfig, $form) = @_;
531 # connect to database
532 my $dbh = $form->dbconnect($myconfig);
535 qq|SELECT l.id, l.lead | .
538 my $sth = $dbh->prepare($query);
539 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
541 my $ref = $sth->fetchrow_hashref("NAME_lc");
543 map { $form->{$_} = $ref->{$_} } keys %$ref;
549 $main::lxdebug->leave_sub();
553 $main::lxdebug->enter_sub();
555 my ($self, $myconfig, $form) = @_;
558 # connect to database
559 my $dbh = $form->dbconnect($myconfig);
561 my @values = ($form->{description});
562 # id is the old record
564 $query = qq|UPDATE leads SET
567 push(@values, $form->{id});
569 $query = qq|INSERT INTO leads
573 do_query($form, $dbh, $query, @values);
577 $main::lxdebug->leave_sub();
581 $main::lxdebug->enter_sub();
583 my ($self, $myconfig, $form) = @_;
586 # connect to database
587 my $dbh = $form->dbconnect($myconfig);
589 $query = qq|DELETE FROM leads
591 do_query($form, $dbh, $query, $form->{id});
595 $main::lxdebug->leave_sub();
599 $main::lxdebug->enter_sub();
601 my ($self, $myconfig, $form, $return_list) = @_;
603 # connect to database
604 my $dbh = $form->dbconnect($myconfig);
607 "SELECT id, description, template_code, article_code, " .
608 " output_numberformat, output_dateformat, output_longdates " .
609 "FROM language ORDER BY description";
611 my $sth = $dbh->prepare($query);
612 $sth->execute || $form->dberror($query);
616 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
617 push(@{ $ary }, $ref);
623 $main::lxdebug->leave_sub();
633 $main::lxdebug->enter_sub();
635 my ($self, $myconfig, $form) = @_;
637 # connect to database
638 my $dbh = $form->dbconnect($myconfig);
641 "SELECT description, template_code, article_code, " .
642 " output_numberformat, output_dateformat, output_longdates " .
643 "FROM language WHERE id = ?";
644 my $sth = $dbh->prepare($query);
645 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
647 my $ref = $sth->fetchrow_hashref("NAME_lc");
649 map { $form->{$_} = $ref->{$_} } keys %$ref;
655 $main::lxdebug->leave_sub();
658 sub get_language_details {
659 $main::lxdebug->enter_sub();
661 my ($self, $myconfig, $form, $id) = @_;
663 # connect to database
664 my $dbh = $form->dbconnect($myconfig);
667 "SELECT template_code, " .
668 " output_numberformat, output_dateformat, output_longdates " .
669 "FROM language WHERE id = ?";
670 my @res = selectrow_query($form, $dbh, $query, $id);
673 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ($self, $myconfig, $form) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
685 my (@values, $query);
687 map({ push(@values, $form->{$_}); }
688 qw(description template_code article_code
689 output_numberformat output_dateformat output_longdates));
691 # id is the old record
694 "UPDATE language SET " .
695 " description = ?, template_code = ?, article_code = ?, " .
696 " output_numberformat = ?, output_dateformat = ?, " .
697 " output_longdates = ? " .
699 push(@values, $form->{id});
702 "INSERT INTO language (" .
703 " description, template_code, article_code, " .
704 " output_numberformat, output_dateformat, output_longdates" .
705 ") VALUES (?, ?, ?, ?, ?, ?)";
707 do_query($form, $dbh, $query, @values);
711 $main::lxdebug->leave_sub();
714 sub delete_language {
715 $main::lxdebug->enter_sub();
717 my ($self, $myconfig, $form) = @_;
720 # connect to database
721 my $dbh = $form->dbconnect_noauto($myconfig);
723 foreach my $table (qw(generic_translations units_language)) {
724 $query = qq|DELETE FROM $table WHERE language_id = ?|;
725 do_query($form, $dbh, $query, $form->{"id"});
728 $query = "DELETE FROM language WHERE id = ?";
729 do_query($form, $dbh, $query, $form->{"id"});
734 $main::lxdebug->leave_sub();
739 $main::lxdebug->enter_sub();
741 my ($self, $myconfig, $form) = @_;
743 # connect to database
744 my $dbh = $form->dbconnect($myconfig);
746 my $query = qq|SELECT id, description,
748 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
750 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
752 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
754 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
756 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
758 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
760 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
762 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
764 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
768 my $sth = $dbh->prepare($query);
769 $sth->execute || $form->dberror($query);
772 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
773 push @{ $form->{ALL} }, $ref;
779 $main::lxdebug->leave_sub();
782 sub get_buchungsgruppe {
783 $main::lxdebug->enter_sub();
785 my ($self, $myconfig, $form) = @_;
788 # connect to database
789 my $dbh = $form->dbconnect($myconfig);
793 qq|SELECT description, inventory_accno_id,
794 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
796 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
798 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
800 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
802 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
804 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
806 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
808 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
810 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
813 my $sth = $dbh->prepare($query);
814 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
816 my $ref = $sth->fetchrow_hashref("NAME_lc");
818 map { $form->{$_} = $ref->{$_} } keys %$ref;
823 qq|SELECT count(id) = 0 AS orphaned
825 WHERE buchungsgruppen_id = ?|;
826 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
829 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
831 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
832 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
835 $query = qq|SELECT c.accno, c.description, c.link, c.id,
836 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
837 FROM chart c, defaults d
838 WHERE c.link LIKE '%$module%'
842 my $sth = $dbh->prepare($query);
843 $sth->execute || $form->dberror($query);
844 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
845 foreach my $key (split(/:/, $ref->{link})) {
846 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
847 $form->{"std_inventory_accno_id"} = $ref->{"id"};
849 if ($key =~ /$module/) {
850 if ( ($ref->{id} eq $ref->{inventory_accno_id})
851 || ($ref->{id} eq $ref->{income_accno_id})
852 || ($ref->{id} eq $ref->{expense_accno_id})) {
853 push @{ $form->{"${module}_links"}{$key} },
854 { accno => $ref->{accno},
855 description => $ref->{description},
856 selected => "selected",
859 push @{ $form->{"${module}_links"}{$key} },
860 { accno => $ref->{accno},
861 description => $ref->{description},
873 $main::lxdebug->leave_sub();
876 sub save_buchungsgruppe {
877 $main::lxdebug->enter_sub();
879 my ($self, $myconfig, $form) = @_;
881 # connect to database
882 my $dbh = $form->dbconnect($myconfig);
884 my @values = ($form->{description}, $form->{inventory_accno_id},
885 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
886 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
887 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
888 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
892 # id is the old record
894 $query = qq|UPDATE buchungsgruppen SET
895 description = ?, inventory_accno_id = ?,
896 income_accno_id_0 = ?, expense_accno_id_0 = ?,
897 income_accno_id_1 = ?, expense_accno_id_1 = ?,
898 income_accno_id_2 = ?, expense_accno_id_2 = ?,
899 income_accno_id_3 = ?, expense_accno_id_3 = ?
901 push(@values, $form->{id});
903 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
904 my ($sortkey) = $dbh->selectrow_array($query);
905 $form->dberror($query) if ($dbh->err);
906 push(@values, $sortkey);
907 $query = qq|INSERT INTO buchungsgruppen
908 (description, inventory_accno_id,
909 income_accno_id_0, expense_accno_id_0,
910 income_accno_id_1, expense_accno_id_1,
911 income_accno_id_2, expense_accno_id_2,
912 income_accno_id_3, expense_accno_id_3,
914 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
916 do_query($form, $dbh, $query, @values);
920 $main::lxdebug->leave_sub();
923 sub delete_buchungsgruppe {
924 $main::lxdebug->enter_sub();
926 my ($self, $myconfig, $form) = @_;
928 # connect to database
929 my $dbh = $form->dbconnect($myconfig);
931 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
932 do_query($form, $dbh, $query, $form->{id});
936 $main::lxdebug->leave_sub();
940 $main::lxdebug->enter_sub();
942 my ($self, $myconfig, $form, $table) = @_;
944 # connect to database
945 my $dbh = $form->get_standard_dbh($myconfig);
949 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
950 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
951 my @values = ($form->{"id1"}, $form->{"id2"});
952 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
954 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
955 my $sth = prepare_query($form, $dbh, $query);
957 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
958 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
964 $main::lxdebug->leave_sub();
967 sub prepare_template_filename {
968 $main::lxdebug->enter_sub();
970 my ($self, $myconfig, $form) = @_;
972 my ($filename, $display_filename);
974 if ($form->{type} eq "stylesheet") {
975 $filename = "css/$myconfig->{stylesheet}";
976 $display_filename = $myconfig->{stylesheet};
979 $filename = $form->{formname};
981 if ($form->{language}) {
982 my ($id, $template_code) = split(/--/, $form->{language});
983 $filename .= "_${template_code}";
986 if ($form->{printer}) {
987 my ($id, $template_code) = split(/--/, $form->{printer});
988 $filename .= "_${template_code}";
991 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
992 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
993 $filename =~ s|.*/||;
995 $display_filename = $filename;
996 $filename = "$myconfig->{templates}/$filename";
999 $main::lxdebug->leave_sub();
1001 return ($filename, $display_filename);
1006 $main::lxdebug->enter_sub();
1008 my ($self, $filename) = @_;
1010 my ($content, $lines) = ("", 0);
1014 if (open(TEMPLATE, $filename)) {
1015 while (<TEMPLATE>) {
1022 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1024 $main::lxdebug->leave_sub();
1026 return ($content, $lines);
1030 $main::lxdebug->enter_sub();
1032 my ($self, $filename, $content) = @_;
1038 if (open(TEMPLATE, ">", $filename)) {
1039 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1040 $content =~ s/\r\n/\n/g;
1041 print(TEMPLATE $content);
1047 $main::lxdebug->leave_sub();
1053 $main::lxdebug->enter_sub();
1058 my $myconfig = \%main::myconfig;
1059 my $form = $main::form;
1061 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1064 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1066 $form->{curr} =~ s/ //g;
1067 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1068 my $currency = join ':', @currencies;
1070 # these defaults are database wide
1073 qq|UPDATE defaults SET
1074 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1075 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1076 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1077 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1078 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1079 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1096 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1097 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1098 $form->{invnumber}, $form->{cnnumber},
1099 $form->{sonumber}, $form->{ponumber},
1100 $form->{sqnumber}, $form->{rfqnumber},
1101 $form->{customernumber}, $form->{vendornumber},
1102 $form->{articlenumber}, $form->{servicenumber},
1103 $form->{sdonumber}, $form->{pdonumber},
1105 $form->{businessnumber}, $form->{weightunit},
1106 conv_i($form->{language_id}));
1107 do_query($form, $dbh, $query, @values);
1111 $main::lxdebug->leave_sub();
1115 sub save_preferences {
1116 $main::lxdebug->enter_sub();
1118 my ($self, $myconfig, $form) = @_;
1120 my $dbh = $form->get_standard_dbh($myconfig);
1122 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1125 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1126 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1128 my $rc = $dbh->commit();
1130 # save first currency in myconfig
1131 $currency =~ s/:.*//;
1132 $form->{currency} = $currency;
1134 $form->{businessnumber} = $businessnumber;
1136 $myconfig = User->new(login => $form->{login});
1138 foreach my $item (keys %$form) {
1139 $myconfig->{$item} = $form->{$item};
1142 $myconfig->save_member;
1144 my $auth = $main::auth;
1146 $main::lxdebug->leave_sub();
1152 $main::lxdebug->enter_sub();
1157 my $myconfig = \%main::myconfig;
1158 my $form = $main::form;
1160 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1162 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1164 $defaults->{weightunit} ||= 'kg';
1166 $main::lxdebug->leave_sub();
1171 sub defaultaccounts {
1172 $main::lxdebug->enter_sub();
1174 my ($self, $myconfig, $form) = @_;
1176 # connect to database
1177 my $dbh = $form->dbconnect($myconfig);
1179 # get defaults from defaults table
1180 my $query = qq|SELECT * FROM defaults|;
1181 my $sth = $dbh->prepare($query);
1182 $sth->execute || $form->dberror($query);
1184 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1185 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1186 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1187 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1188 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1189 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1190 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1192 $form->{defaults}{weightunit} ||= 'kg';
1196 $query = qq|SELECT c.id, c.accno, c.description, c.link
1198 WHERE c.link LIKE '%IC%'
1200 $sth = $dbh->prepare($query);
1201 $sth->execute || $self->dberror($query);
1203 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1204 foreach my $key (split(/:/, $ref->{link})) {
1207 if ($key =~ /cogs/) {
1208 $nkey = "IC_expense";
1210 if ($key =~ /sale/) {
1211 $nkey = "IC_income";
1213 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1215 description => $ref->{description}
1222 $query = qq|SELECT c.id, c.accno, c.description
1224 WHERE c.category = 'I'
1225 AND c.charttype = 'A'
1227 $sth = $dbh->prepare($query);
1228 $sth->execute || $self->dberror($query);
1230 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1231 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1233 description => $ref->{description}
1238 $query = qq|SELECT c.id, c.accno, c.description
1240 WHERE c.category = 'E'
1241 AND c.charttype = 'A'
1243 $sth = $dbh->prepare($query);
1244 $sth->execute || $self->dberror($query);
1246 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1247 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1249 description => $ref->{description}
1254 # now get the tax rates and numbers
1255 $query = qq|SELECT c.id, c.accno, c.description,
1256 t.rate * 100 AS rate, t.taxnumber
1258 WHERE c.id = t.chart_id|;
1260 $sth = $dbh->prepare($query);
1261 $sth->execute || $form->dberror($query);
1263 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1264 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1265 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1266 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1267 if $ref->{taxnumber};
1268 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1270 # Abfrage für Standard Umlaufvermögenskonto
1272 qq|SELECT id, accno, description, link | .
1274 qq|WHERE link LIKE ? |.
1276 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1277 $sth->execute || $form->dberror($query);#
1278 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1279 foreach my $item (split(/:/, $ref->{link})) {
1280 if ($item eq "AR_paid") {
1281 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1283 description => $ref->{description}
1292 $main::lxdebug->leave_sub();
1296 $main::lxdebug->enter_sub();
1298 my ($self, $myconfig, $form) = @_;
1300 my $dbh = $form->dbconnect($myconfig);
1302 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1303 my $sth = $dbh->prepare($query);
1304 $sth->execute || $form->dberror($query);
1306 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1312 $main::lxdebug->leave_sub();
1316 $main::lxdebug->enter_sub();
1318 my ($self, $myconfig, $form) = @_;
1320 my $dbh = $form->dbconnect($myconfig);
1322 my ($query, @values);
1324 if ($form->{revtrans}) {
1325 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1327 } elsif ($form->{closedto}) {
1328 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1329 @values = (conv_date($form->{closedto}));
1332 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1335 # set close in defaults
1336 do_query($form, $dbh, $query, @values);
1340 $main::lxdebug->leave_sub();
1344 my ($self, $units, $unit_name, $factor) = @_;
1346 $factor = 1 unless ($factor);
1348 my $unit = $units->{$unit_name};
1350 if (!defined($unit) || !$unit->{"base_unit"} ||
1351 ($unit_name eq $unit->{"base_unit"})) {
1352 return ($unit_name, $factor);
1355 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1358 sub retrieve_units {
1359 $main::lxdebug->enter_sub();
1361 my ($self, $myconfig, $form, $prefix) = @_;
1364 my $dbh = $form->get_standard_dbh;
1366 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1368 my $sth = prepare_execute_query($form, $dbh, $query);
1371 while (my $ref = $sth->fetchrow_hashref()) {
1372 $units->{$ref->{"name"}} = $ref;
1376 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1377 $sth = $dbh->prepare($query_lang);
1378 $sth->execute() || $form->dberror($query_lang);
1380 while (my $ref = $sth->fetchrow_hashref()) {
1381 push(@languages, $ref);
1385 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1386 "FROM units_language ul " .
1387 "LEFT JOIN language l ON ul.language_id = l.id " .
1388 "WHERE ul.unit = ?";
1389 $sth = $dbh->prepare($query_lang);
1391 foreach my $unit (values(%{$units})) {
1392 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1394 $unit->{"LANGUAGES"} = {};
1395 foreach my $lang (@languages) {
1396 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1399 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1400 while (my $ref = $sth->fetchrow_hashref()) {
1401 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1406 $main::lxdebug->leave_sub();
1411 sub retrieve_all_units {
1412 $main::lxdebug->enter_sub();
1416 if (!$::request->{cache}{all_units}) {
1417 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1420 $main::lxdebug->leave_sub();
1422 return $::request->{cache}{all_units};
1426 sub translate_units {
1427 $main::lxdebug->enter_sub();
1429 my ($self, $form, $template_code, $unit, $amount) = @_;
1431 my $units = $self->retrieve_units(\%main::myconfig, $form);
1433 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1434 my $new_unit = $unit;
1436 if (($amount != 1) && $h->{"localized_plural"}) {
1437 $new_unit = $h->{"localized_plural"};
1438 } elsif ($h->{"localized"}) {
1439 $new_unit = $h->{"localized"};
1443 $main::lxdebug->leave_sub();
1449 $main::lxdebug->enter_sub();
1451 my ($self, $myconfig, $form, $units) = @_;
1453 my $dbh = $form->dbconnect($myconfig);
1455 map({ $_->{"in_use"} = 0; } values(%{$units}));
1457 foreach my $unit (values(%{$units})) {
1458 my $base_unit = $unit->{"original_base_unit"};
1459 while ($base_unit) {
1460 $units->{$base_unit}->{"in_use"} = 1;
1461 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1462 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1463 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1467 foreach my $unit (values(%{$units})) {
1468 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1470 foreach my $table (qw(parts invoice orderitems)) {
1471 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1473 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1474 $query .= "= " . $dbh->quote($unit->{"name"});
1476 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1477 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1480 my ($count) = $dbh->selectrow_array($query);
1481 $form->dberror($query) if ($dbh->err);
1484 $unit->{"in_use"} = 1;
1492 $main::lxdebug->leave_sub();
1495 sub convertible_units {
1496 $main::lxdebug->enter_sub();
1500 my $filter_unit = shift;
1501 my $not_smaller = shift;
1503 my $conv_units = [];
1505 $filter_unit = $units->{$filter_unit};
1507 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1508 my $unit = $units->{$name};
1510 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1511 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1512 push @{$conv_units}, $unit;
1516 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1518 $main::lxdebug->leave_sub();
1523 # if $a is translatable to $b, return the factor between them.
1526 $main::lxdebug->enter_sub(2);
1527 my ($this, $a, $b, $all_units) = @_;
1530 $all_units = $this->retrieve_all_units;
1533 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1534 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1535 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1536 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1539 sub unit_select_data {
1540 $main::lxdebug->enter_sub();
1542 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1547 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1550 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1551 if (!$convertible_into ||
1552 ($units->{$convertible_into} &&
1553 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1554 push @{$select}, { "name" => $unit,
1555 "base_unit" => $units->{$unit}->{"base_unit"},
1556 "factor" => $units->{$unit}->{"factor"},
1557 "selected" => ($unit eq $selected) ? "selected" : "" };
1561 $main::lxdebug->leave_sub();
1566 sub unit_select_html {
1567 $main::lxdebug->enter_sub();
1569 my ($self, $units, $name, $selected, $convertible_into) = @_;
1571 my $select = "<select name=${name}>";
1573 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1574 if (!$convertible_into ||
1575 ($units->{$convertible_into} &&
1576 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1577 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1580 $select .= "</select>";
1582 $main::lxdebug->leave_sub();
1588 $main::lxdebug->enter_sub();
1592 my $units = $self->retrieve_all_units();
1597 while (2 <= scalar(@_)) {
1598 my $qty = shift(@_);
1599 my $unit = $units->{shift(@_)};
1601 croak "No unit defined with name $unit" if (!defined $unit);
1604 $base_unit = $unit->{base_unit};
1605 } elsif ($base_unit ne $unit->{base_unit}) {
1606 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1609 $sum += $qty * $unit->{factor};
1612 $main::lxdebug->leave_sub();
1614 return wantarray ? ($sum, $base_unit) : $sum;
1618 $main::lxdebug->enter_sub();
1620 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1622 my $dbh = $form->dbconnect_noauto($myconfig);
1624 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1625 my ($sortkey) = selectrow_query($form, $dbh, $query);
1627 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1628 "VALUES (?, ?, ?, ?)";
1629 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1632 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1633 my $sth = $dbh->prepare($query);
1634 foreach my $lang (@{$languages}) {
1635 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1636 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1644 $main::lxdebug->leave_sub();
1648 $main::lxdebug->enter_sub();
1650 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1652 my $dbh = $form->dbconnect_noauto($myconfig);
1654 my ($base_unit, $unit, $sth, $query);
1656 $query = "DELETE FROM units_language";
1657 $dbh->do($query) || $form->dberror($query);
1659 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1660 $query = "DELETE FROM units WHERE name IN (";
1661 map({ $query .= "?," } @{$delete_units});
1662 substr($query, -1, 1) = ")";
1663 $dbh->do($query, undef, @{$delete_units}) ||
1664 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1667 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1668 $sth = $dbh->prepare($query);
1670 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1671 my $sth_lang = $dbh->prepare($query_lang);
1673 foreach $unit (values(%{$units})) {
1674 $unit->{"depth"} = 0;
1675 my $base_unit = $unit;
1676 while ($base_unit->{"base_unit"}) {
1678 $base_unit = $units->{$base_unit->{"base_unit"}};
1682 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1683 if ($unit->{"LANGUAGES"}) {
1684 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1685 next unless ($lang->{"id"} && $lang->{"localized"});
1686 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1687 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1691 next if ($unit->{"unchanged_unit"});
1693 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1694 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1698 $sth_lang->finish();
1702 $main::lxdebug->leave_sub();
1706 $main::lxdebug->enter_sub();
1708 my ($self, $myconfig, $form) = @_;
1710 # connect to database
1711 my $dbh = $form->dbconnect($myconfig);
1713 my $query = qq|SELECT
1717 round(t.rate * 100, 2) AS rate,
1718 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1719 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1723 my $sth = $dbh->prepare($query);
1724 $sth->execute || $form->dberror($query);
1727 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1728 push @{ $form->{TAX} }, $ref;
1734 $main::lxdebug->leave_sub();
1737 sub get_tax_accounts {
1738 $main::lxdebug->enter_sub();
1740 my ($self, $myconfig, $form) = @_;
1742 my $dbh = $form->dbconnect($myconfig);
1744 # get Accounts from chart
1745 my $query = qq{ SELECT
1747 accno || ' - ' || description AS taxaccount
1749 WHERE link LIKE '%_tax%'
1753 my $sth = $dbh->prepare($query);
1754 $sth->execute || $form->dberror($query);
1756 $form->{ACCOUNTS} = [];
1757 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1758 push @{ $form->{ACCOUNTS} }, $ref;
1765 $main::lxdebug->leave_sub();
1769 $main::lxdebug->enter_sub();
1771 my ($self, $myconfig, $form) = @_;
1773 # connect to database
1774 my $dbh = $form->dbconnect($myconfig);
1776 my $query = qq|SELECT
1779 round(rate * 100, 2) AS rate,
1781 (id IN (SELECT tax_id
1782 FROM acc_trans)) AS tax_already_used
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();