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 CT_tax);
217 foreach my $item ($form->{AR}, $form->{AR_amount},
218 $form->{AR_tax}, $form->{AR_paid},
219 $form->{AP}, $form->{AP_amount},
220 $form->{AP_tax}, $form->{AP_paid},
221 $form->{IC}, $form->{IC_sale},
222 $form->{IC_cogs}, $form->{IC_taxpart},
223 $form->{IC_income}, $form->{IC_expense},
224 $form->{IC_taxservice}, $form->{CT_tax}
226 $form->{link} .= "${item}:" if ($item);
230 # strip blanks from accno
231 map { $form->{$_} =~ s/ //g; } qw(accno);
235 if ($form->{id} eq "NULL") {
239 if (!$form->{id} || $form->{id} eq "") {
240 $query = qq|SELECT nextval('id')|;
241 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
242 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
243 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
251 # if charttype is heading make sure certain values are empty
252 # specifically, if charttype is changed from an existing account, empty the
253 # fields unnecessary for headings, so that e.g. heading doesn't appear in
254 # drop-down menues due to still having a valid "link" entry
256 if ( $form->{charttype} eq 'H' ) {
258 $form->{pos_bwa} = '';
259 $form->{pos_bilanz} = '';
260 $form->{pos_eur} = '';
261 $form->{new_chart_id} = '';
262 $form->{valid_from} = '';
265 $query = qq|UPDATE chart SET
281 $form->{description},
285 conv_i($form->{pos_bwa}),
286 conv_i($form->{pos_bilanz}),
287 conv_i($form->{pos_eur}),
288 conv_i($form->{new_chart_id}),
289 conv_date($form->{valid_from}),
290 ($form->{datevautomatik} eq 'T') ? 'true':'false',
297 do_query($form, $dbh, $query, @values);
303 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
307 for $tk_count (0 .. $MAX_TRIES) {
311 # Check if the account already exists, else cancel
313 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
315 last READTAXKEYS if ( $form->{'id'} == 0);
317 # check if there is a startdate
318 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
323 # Add valid taxkeys into the array
326 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
327 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
328 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
329 chart_id => conv_i($form->{"id"}),
330 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
331 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
338 for my $j (0 .. $#taxkeys){
339 if ( defined $taxkeys[$j]{'id'} ){
342 if ($taxkeys[$j]{'delete'}){
344 DELETE FROM taxkeys WHERE id = ?
347 @values = ($taxkeys[$j]{'id'});
349 do_query($form, $dbh, $query, @values);
358 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
366 $taxkeys[$j]{'tax_id'},
367 $taxkeys[$j]{'chart_id'},
368 $taxkeys[$j]{'tax_id'},
369 $taxkeys[$j]{'pos_ustva'},
370 $taxkeys[$j]{'startdate'},
373 do_query($form, $dbh, $query, @values);
379 INSERT INTO taxkeys (
386 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
389 $taxkeys[$j]{'tax_id'},
390 $taxkeys[$j]{'chart_id'},
391 $taxkeys[$j]{'tax_id'},
392 $taxkeys[$j]{'pos_ustva'},
393 $taxkeys[$j]{'startdate'},
396 do_query($form, $dbh, $query, @values);
401 # Update chart.taxkey_id to the latest from taxkeys for this chart.
407 WHERE taxkeys.chart_id = chart.id
408 ORDER BY startdate DESC
414 do_query($form, $dbh, $query, $form->{id});
417 my $rc = $dbh->commit;
420 $main::lxdebug->leave_sub();
426 $main::lxdebug->enter_sub();
428 my ($self, $myconfig, $form) = @_;
430 # connect to database, turn off AutoCommit
431 my $dbh = $form->dbconnect_noauto($myconfig);
433 my $query = qq|SELECT count(*) FROM acc_trans a
434 WHERE a.chart_id = ?|;
435 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
439 $main::lxdebug->leave_sub();
443 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
444 foreach my $type (qw(inventory income expense)) {
447 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
448 qq|WHERE ${type}_accno_id = ?|;
449 do_query($form, $dbh, $query, $form->{id});
452 foreach my $table (qw(partstax customertax vendortax tax)) {
453 $query = qq|DELETE FROM $table
455 do_query($form, $dbh, $query, $form->{id});
458 # delete chart of account record
459 $query = qq|DELETE FROM chart
461 do_query($form, $dbh, $query, $form->{id});
463 # delete account taxkeys
464 $query = qq|DELETE FROM taxkeys
466 do_query($form, $dbh, $query, $form->{id});
468 # commit and redirect
469 my $rc = $dbh->commit;
472 $main::lxdebug->leave_sub();
478 $main::lxdebug->enter_sub();
480 my ($self, $myconfig, $form) = @_;
482 # connect to database
483 my $dbh = $form->dbconnect($myconfig);
485 my $query = qq|SELECT id, lead
489 my $sth = $dbh->prepare($query);
490 $sth->execute || $form->dberror($query);
492 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
493 push @{ $form->{ALL} }, $ref;
499 $main::lxdebug->leave_sub();
503 $main::lxdebug->enter_sub();
505 my ($self, $myconfig, $form) = @_;
507 # connect to database
508 my $dbh = $form->dbconnect($myconfig);
511 qq|SELECT l.id, l.lead | .
514 my $sth = $dbh->prepare($query);
515 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
517 my $ref = $sth->fetchrow_hashref("NAME_lc");
519 map { $form->{$_} = $ref->{$_} } keys %$ref;
525 $main::lxdebug->leave_sub();
529 $main::lxdebug->enter_sub();
531 my ($self, $myconfig, $form) = @_;
534 # connect to database
535 my $dbh = $form->dbconnect($myconfig);
537 my @values = ($form->{description});
538 # id is the old record
540 $query = qq|UPDATE leads SET
543 puhs(@values, $form->{id});
545 $query = qq|INSERT INTO leads
549 do_query($form, $dbh, $query, @values);
553 $main::lxdebug->leave_sub();
557 $main::lxdebug->enter_sub();
559 my ($self, $myconfig, $form) = @_;
562 # connect to database
563 my $dbh = $form->dbconnect($myconfig);
565 $query = qq|DELETE FROM leads
567 do_query($form, $dbh, $query, $form->{id});
571 $main::lxdebug->leave_sub();
575 $main::lxdebug->enter_sub();
577 my ($self, $myconfig, $form) = @_;
579 # connect to database
580 my $dbh = $form->dbconnect($myconfig);
582 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
586 my $sth = $dbh->prepare($query);
587 $sth->execute || $form->dberror($query);
589 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
590 push @{ $form->{ALL} }, $ref;
596 $main::lxdebug->leave_sub();
600 $main::lxdebug->enter_sub();
602 my ($self, $myconfig, $form) = @_;
604 # connect to database
605 my $dbh = $form->dbconnect($myconfig);
608 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
611 my $sth = $dbh->prepare($query);
612 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
614 my $ref = $sth->fetchrow_hashref("NAME_lc");
616 map { $form->{$_} = $ref->{$_} } keys %$ref;
622 $main::lxdebug->leave_sub();
626 $main::lxdebug->enter_sub();
628 my ($self, $myconfig, $form) = @_;
631 # connect to database
632 my $dbh = $form->dbconnect($myconfig);
634 my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
635 # id is the old record
637 $query = qq|UPDATE business SET
640 customernumberinit = ?,
643 push(@values, $form->{id});
645 $query = qq|INSERT INTO business
646 (description, discount, customernumberinit, salesman)
647 VALUES (?, ?, ?, ?)|;
649 do_query($form, $dbh, $query, @values);
653 $main::lxdebug->leave_sub();
656 sub delete_business {
657 $main::lxdebug->enter_sub();
659 my ($self, $myconfig, $form) = @_;
661 # connect to database
662 my $dbh = $form->dbconnect($myconfig);
664 my $query = qq|DELETE FROM business
666 do_query($form, $dbh, $query, $form->{id});
670 $main::lxdebug->leave_sub();
675 $main::lxdebug->enter_sub();
677 my ($self, $myconfig, $form, $return_list) = @_;
679 # connect to database
680 my $dbh = $form->dbconnect($myconfig);
683 "SELECT id, description, template_code, article_code, " .
684 " output_numberformat, output_dateformat, output_longdates " .
685 "FROM language ORDER BY description";
687 my $sth = $dbh->prepare($query);
688 $sth->execute || $form->dberror($query);
692 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
693 push(@{ $ary }, $ref);
699 $main::lxdebug->leave_sub();
709 $main::lxdebug->enter_sub();
711 my ($self, $myconfig, $form) = @_;
713 # connect to database
714 my $dbh = $form->dbconnect($myconfig);
717 "SELECT description, template_code, article_code, " .
718 " output_numberformat, output_dateformat, output_longdates " .
719 "FROM language WHERE id = ?";
720 my $sth = $dbh->prepare($query);
721 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
723 my $ref = $sth->fetchrow_hashref("NAME_lc");
725 map { $form->{$_} = $ref->{$_} } keys %$ref;
731 $main::lxdebug->leave_sub();
734 sub get_language_details {
735 $main::lxdebug->enter_sub();
737 my ($self, $myconfig, $form, $id) = @_;
739 # connect to database
740 my $dbh = $form->dbconnect($myconfig);
743 "SELECT template_code, " .
744 " output_numberformat, output_dateformat, output_longdates " .
745 "FROM language WHERE id = ?";
746 my @res = selectrow_query($form, $dbh, $query, $id);
749 $main::lxdebug->leave_sub();
755 $main::lxdebug->enter_sub();
757 my ($self, $myconfig, $form) = @_;
759 # connect to database
760 my $dbh = $form->dbconnect($myconfig);
761 my (@values, $query);
763 map({ push(@values, $form->{$_}); }
764 qw(description template_code article_code
765 output_numberformat output_dateformat output_longdates));
767 # id is the old record
770 "UPDATE language SET " .
771 " description = ?, template_code = ?, article_code = ?, " .
772 " output_numberformat = ?, output_dateformat = ?, " .
773 " output_longdates = ? " .
775 push(@values, $form->{id});
778 "INSERT INTO language (" .
779 " description, template_code, article_code, " .
780 " output_numberformat, output_dateformat, output_longdates" .
781 ") VALUES (?, ?, ?, ?, ?, ?)";
783 do_query($form, $dbh, $query, @values);
787 $main::lxdebug->leave_sub();
790 sub delete_language {
791 $main::lxdebug->enter_sub();
793 my ($self, $myconfig, $form) = @_;
796 # connect to database
797 my $dbh = $form->dbconnect_noauto($myconfig);
799 foreach my $table (qw(generic_translations units_language)) {
800 $query = qq|DELETE FROM $table WHERE language_id = ?|;
801 do_query($form, $dbh, $query, $form->{"id"});
804 $query = "DELETE FROM language WHERE id = ?";
805 do_query($form, $dbh, $query, $form->{"id"});
810 $main::lxdebug->leave_sub();
815 $main::lxdebug->enter_sub();
817 my ($self, $myconfig, $form) = @_;
819 # connect to database
820 my $dbh = $form->dbconnect($myconfig);
822 my $query = qq|SELECT id, description,
824 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
826 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
828 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
830 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
832 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
834 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
836 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
838 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
840 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
844 my $sth = $dbh->prepare($query);
845 $sth->execute || $form->dberror($query);
848 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
849 push @{ $form->{ALL} }, $ref;
855 $main::lxdebug->leave_sub();
858 sub get_buchungsgruppe {
859 $main::lxdebug->enter_sub();
861 my ($self, $myconfig, $form) = @_;
864 # connect to database
865 my $dbh = $form->dbconnect($myconfig);
869 qq|SELECT description, inventory_accno_id,
870 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
872 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
874 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
876 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
878 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
880 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
882 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
884 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
886 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
889 my $sth = $dbh->prepare($query);
890 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
892 my $ref = $sth->fetchrow_hashref("NAME_lc");
894 map { $form->{$_} = $ref->{$_} } keys %$ref;
899 qq|SELECT count(id) = 0 AS orphaned
901 WHERE buchungsgruppen_id = ?|;
902 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
905 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
907 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
908 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
911 $query = qq|SELECT c.accno, c.description, c.link, c.id,
912 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
913 FROM chart c, defaults d
914 WHERE c.link LIKE '%$module%'
918 my $sth = $dbh->prepare($query);
919 $sth->execute || $form->dberror($query);
920 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
921 foreach my $key (split(/:/, $ref->{link})) {
922 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
923 $form->{"std_inventory_accno_id"} = $ref->{"id"};
925 if ($key =~ /$module/) {
926 if ( ($ref->{id} eq $ref->{inventory_accno_id})
927 || ($ref->{id} eq $ref->{income_accno_id})
928 || ($ref->{id} eq $ref->{expense_accno_id})) {
929 push @{ $form->{"${module}_links"}{$key} },
930 { accno => $ref->{accno},
931 description => $ref->{description},
932 selected => "selected",
935 push @{ $form->{"${module}_links"}{$key} },
936 { accno => $ref->{accno},
937 description => $ref->{description},
949 $main::lxdebug->leave_sub();
952 sub save_buchungsgruppe {
953 $main::lxdebug->enter_sub();
955 my ($self, $myconfig, $form) = @_;
957 # connect to database
958 my $dbh = $form->dbconnect($myconfig);
960 my @values = ($form->{description}, $form->{inventory_accno_id},
961 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
962 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
963 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
964 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
968 # id is the old record
970 $query = qq|UPDATE buchungsgruppen SET
971 description = ?, inventory_accno_id = ?,
972 income_accno_id_0 = ?, expense_accno_id_0 = ?,
973 income_accno_id_1 = ?, expense_accno_id_1 = ?,
974 income_accno_id_2 = ?, expense_accno_id_2 = ?,
975 income_accno_id_3 = ?, expense_accno_id_3 = ?
977 push(@values, $form->{id});
979 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
980 my ($sortkey) = $dbh->selectrow_array($query);
981 $form->dberror($query) if ($dbh->err);
982 push(@values, $sortkey);
983 $query = qq|INSERT INTO buchungsgruppen
984 (description, inventory_accno_id,
985 income_accno_id_0, expense_accno_id_0,
986 income_accno_id_1, expense_accno_id_1,
987 income_accno_id_2, expense_accno_id_2,
988 income_accno_id_3, expense_accno_id_3,
990 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
992 do_query($form, $dbh, $query, @values);
996 $main::lxdebug->leave_sub();
999 sub delete_buchungsgruppe {
1000 $main::lxdebug->enter_sub();
1002 my ($self, $myconfig, $form) = @_;
1004 # connect to database
1005 my $dbh = $form->dbconnect($myconfig);
1007 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1008 do_query($form, $dbh, $query, $form->{id});
1012 $main::lxdebug->leave_sub();
1016 $main::lxdebug->enter_sub();
1018 my ($self, $myconfig, $form, $table) = @_;
1020 # connect to database
1021 my $dbh = $form->get_standard_dbh($myconfig);
1025 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1026 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1027 my @values = ($form->{"id1"}, $form->{"id2"});
1028 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1030 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1031 my $sth = prepare_query($form, $dbh, $query);
1033 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1034 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1040 $main::lxdebug->leave_sub();
1043 sub prepare_template_filename {
1044 $main::lxdebug->enter_sub();
1046 my ($self, $myconfig, $form) = @_;
1048 my ($filename, $display_filename);
1050 if ($form->{type} eq "stylesheet") {
1051 $filename = "css/$myconfig->{stylesheet}";
1052 $display_filename = $myconfig->{stylesheet};
1055 $filename = $form->{formname};
1057 if ($form->{language}) {
1058 my ($id, $template_code) = split(/--/, $form->{language});
1059 $filename .= "_${template_code}";
1062 if ($form->{printer}) {
1063 my ($id, $template_code) = split(/--/, $form->{printer});
1064 $filename .= "_${template_code}";
1067 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1068 $filename =~ s|.*/||;
1069 $display_filename = $filename;
1070 $filename = "$myconfig->{templates}/$filename";
1073 $main::lxdebug->leave_sub();
1075 return ($filename, $display_filename);
1080 $main::lxdebug->enter_sub();
1082 my ($self, $filename) = @_;
1084 my ($content, $lines) = ("", 0);
1088 if (open(TEMPLATE, $filename)) {
1089 while (<TEMPLATE>) {
1096 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1098 $main::lxdebug->leave_sub();
1100 return ($content, $lines);
1104 $main::lxdebug->enter_sub();
1106 my ($self, $filename, $content) = @_;
1112 if (open(TEMPLATE, ">$filename")) {
1113 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1114 $content =~ s/\r\n/\n/g;
1115 print(TEMPLATE $content);
1121 $main::lxdebug->leave_sub();
1127 $main::lxdebug->enter_sub();
1132 my $myconfig = \%main::myconfig;
1133 my $form = $main::form;
1135 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1138 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1140 $form->{curr} =~ s/ //g;
1141 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1142 my $currency = join ':', @currencies;
1144 # these defaults are database wide
1147 qq|UPDATE defaults SET
1148 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1149 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1150 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1151 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1152 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1153 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1169 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1170 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1171 $form->{invnumber}, $form->{cnnumber},
1172 $form->{sonumber}, $form->{ponumber},
1173 $form->{sqnumber}, $form->{rfqnumber},
1174 $form->{customernumber}, $form->{vendornumber},
1175 $form->{articlenumber}, $form->{servicenumber},
1176 $form->{sdonumber}, $form->{pdonumber},
1178 $form->{businessnumber}, $form->{weightunit});
1179 do_query($form, $dbh, $query, @values);
1183 $main::lxdebug->leave_sub();
1187 sub save_preferences {
1188 $main::lxdebug->enter_sub();
1190 my ($self, $myconfig, $form) = @_;
1192 my $dbh = $form->get_standard_dbh($myconfig);
1194 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1197 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1198 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1200 my $rc = $dbh->commit();
1202 # save first currency in myconfig
1203 $currency =~ s/:.*//;
1204 $form->{currency} = $currency;
1206 $form->{businessnumber} = $businessnumber;
1208 $myconfig = new User($form->{login});
1210 foreach my $item (keys %$form) {
1211 $myconfig->{$item} = $form->{$item};
1214 $myconfig->save_member;
1216 my $auth = $main::auth;
1218 $main::lxdebug->leave_sub();
1224 $main::lxdebug->enter_sub();
1229 my $myconfig = \%main::myconfig;
1230 my $form = $main::form;
1232 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1234 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1236 $defaults->{weightunit} ||= 'kg';
1238 $main::lxdebug->leave_sub();
1243 sub defaultaccounts {
1244 $main::lxdebug->enter_sub();
1246 my ($self, $myconfig, $form) = @_;
1248 # connect to database
1249 my $dbh = $form->dbconnect($myconfig);
1251 # get defaults from defaults table
1252 my $query = qq|SELECT * FROM defaults|;
1253 my $sth = $dbh->prepare($query);
1254 $sth->execute || $form->dberror($query);
1256 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1257 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1258 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1259 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1260 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1261 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1262 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1264 $form->{defaults}{weightunit} ||= 'kg';
1268 $query = qq|SELECT c.id, c.accno, c.description, c.link
1270 WHERE c.link LIKE '%IC%'
1272 $sth = $dbh->prepare($query);
1273 $sth->execute || $self->dberror($query);
1275 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1276 foreach my $key (split(/:/, $ref->{link})) {
1279 if ($key =~ /cogs/) {
1280 $nkey = "IC_expense";
1282 if ($key =~ /sale/) {
1283 $nkey = "IC_income";
1285 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1287 description => $ref->{description}
1294 $query = qq|SELECT c.id, c.accno, c.description
1296 WHERE c.category = 'I'
1297 AND c.charttype = 'A'
1299 $sth = $dbh->prepare($query);
1300 $sth->execute || $self->dberror($query);
1302 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1303 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1305 description => $ref->{description}
1310 $query = qq|SELECT c.id, c.accno, c.description
1312 WHERE c.category = 'E'
1313 AND c.charttype = 'A'
1315 $sth = $dbh->prepare($query);
1316 $sth->execute || $self->dberror($query);
1318 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1319 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1321 description => $ref->{description}
1326 # now get the tax rates and numbers
1327 $query = qq|SELECT c.id, c.accno, c.description,
1328 t.rate * 100 AS rate, t.taxnumber
1330 WHERE c.id = t.chart_id|;
1332 $sth = $dbh->prepare($query);
1333 $sth->execute || $form->dberror($query);
1335 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1336 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1337 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1338 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1339 if $ref->{taxnumber};
1340 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1342 # Abfrage für Standard Umlaufvermögenskonto
1344 qq|SELECT id, accno, description, link | .
1346 qq|WHERE link LIKE ? |.
1348 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1349 $sth->execute || $form->dberror($query);#
1350 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1351 foreach my $item (split(/:/, $ref->{link})) {
1352 if ($item eq "AR_paid") {
1353 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1355 description => $ref->{description}
1364 $main::lxdebug->leave_sub();
1368 $main::lxdebug->enter_sub();
1370 my ($self, $myconfig, $form) = @_;
1372 my $dbh = $form->dbconnect($myconfig);
1374 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1375 my $sth = $dbh->prepare($query);
1376 $sth->execute || $form->dberror($query);
1378 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1384 $main::lxdebug->leave_sub();
1388 $main::lxdebug->enter_sub();
1390 my ($self, $myconfig, $form) = @_;
1392 my $dbh = $form->dbconnect($myconfig);
1394 my ($query, @values);
1396 if ($form->{revtrans}) {
1397 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1399 } elsif ($form->{closedto}) {
1400 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1401 @values = (conv_date($form->{closedto}));
1404 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1407 # set close in defaults
1408 do_query($form, $dbh, $query, @values);
1412 $main::lxdebug->leave_sub();
1416 my ($self, $units, $unit_name, $factor) = @_;
1418 $factor = 1 unless ($factor);
1420 my $unit = $units->{$unit_name};
1422 if (!defined($unit) || !$unit->{"base_unit"} ||
1423 ($unit_name eq $unit->{"base_unit"})) {
1424 return ($unit_name, $factor);
1427 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1430 sub retrieve_units {
1431 $main::lxdebug->enter_sub();
1433 my ($self, $myconfig, $form, $prefix) = @_;
1435 my $dbh = $form->get_standard_dbh;
1437 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1439 my $sth = prepare_execute_query($form, $dbh, $query);
1442 while (my $ref = $sth->fetchrow_hashref()) {
1443 $units->{$ref->{"name"}} = $ref;
1447 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1448 $sth = $dbh->prepare($query_lang);
1449 $sth->execute() || $form->dberror($query_lang);
1451 while (my $ref = $sth->fetchrow_hashref()) {
1452 push(@languages, $ref);
1456 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1457 "FROM units_language ul " .
1458 "LEFT JOIN language l ON ul.language_id = l.id " .
1459 "WHERE ul.unit = ?";
1460 $sth = $dbh->prepare($query_lang);
1462 foreach my $unit (values(%{$units})) {
1463 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1465 $unit->{"LANGUAGES"} = {};
1466 foreach my $lang (@languages) {
1467 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1470 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1471 while (my $ref = $sth->fetchrow_hashref()) {
1472 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1477 $main::lxdebug->leave_sub();
1482 sub retrieve_all_units {
1483 $main::lxdebug->enter_sub();
1487 if (!$main::all_units) {
1488 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1491 $main::lxdebug->leave_sub();
1493 return $main::all_units;
1497 sub translate_units {
1498 $main::lxdebug->enter_sub();
1500 my ($self, $form, $template_code, $unit, $amount) = @_;
1502 my $units = $self->retrieve_units(\%main::myconfig, $form);
1504 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1505 my $new_unit = $unit;
1507 if (($amount != 1) && $h->{"localized_plural"}) {
1508 $new_unit = $h->{"localized_plural"};
1509 } elsif ($h->{"localized"}) {
1510 $new_unit = $h->{"localized"};
1514 $main::lxdebug->leave_sub();
1520 $main::lxdebug->enter_sub();
1522 my ($self, $myconfig, $form, $units) = @_;
1524 my $dbh = $form->dbconnect($myconfig);
1526 map({ $_->{"in_use"} = 0; } values(%{$units}));
1528 foreach my $unit (values(%{$units})) {
1529 my $base_unit = $unit->{"original_base_unit"};
1530 while ($base_unit) {
1531 $units->{$base_unit}->{"in_use"} = 1;
1532 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1533 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1534 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1538 foreach my $unit (values(%{$units})) {
1539 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1541 foreach my $table (qw(parts invoice orderitems)) {
1542 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1544 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1545 $query .= "= " . $dbh->quote($unit->{"name"});
1547 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1548 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1551 my ($count) = $dbh->selectrow_array($query);
1552 $form->dberror($query) if ($dbh->err);
1555 $unit->{"in_use"} = 1;
1563 $main::lxdebug->leave_sub();
1566 sub convertible_units {
1567 $main::lxdebug->enter_sub();
1571 my $filter_unit = shift;
1572 my $not_smaller = shift;
1574 my $conv_units = [];
1576 $filter_unit = $units->{$filter_unit};
1578 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1579 my $unit = $units->{$name};
1581 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1582 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1583 push @{$conv_units}, $unit;
1587 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1589 $main::lxdebug->leave_sub();
1594 # if $a is translatable to $b, return the factor between them.
1597 $main::lxdebug->enter_sub(2);
1598 my ($this, $a, $b, $all_units) = @_;
1600 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1601 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1602 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1603 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1606 sub unit_select_data {
1607 $main::lxdebug->enter_sub();
1609 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1614 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1617 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1618 if (!$convertible_into ||
1619 ($units->{$convertible_into} &&
1620 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1621 push @{$select}, { "name" => $unit,
1622 "base_unit" => $units->{$unit}->{"base_unit"},
1623 "factor" => $units->{$unit}->{"factor"},
1624 "selected" => ($unit eq $selected) ? "selected" : "" };
1628 $main::lxdebug->leave_sub();
1633 sub unit_select_html {
1634 $main::lxdebug->enter_sub();
1636 my ($self, $units, $name, $selected, $convertible_into) = @_;
1638 my $select = "<select name=${name}>";
1640 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1641 if (!$convertible_into ||
1642 ($units->{$convertible_into} &&
1643 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1644 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1647 $select .= "</select>";
1649 $main::lxdebug->leave_sub();
1655 $main::lxdebug->enter_sub();
1659 my $units = $self->retrieve_all_units();
1664 while (2 <= scalar(@_)) {
1665 my $qty = shift(@_);
1666 my $unit = $units->{shift(@_)};
1668 croak "No unit defined with name $unit" if (!defined $unit);
1671 $base_unit = $unit->{base_unit};
1672 } elsif ($base_unit ne $unit->{base_unit}) {
1673 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1676 $sum += $qty * $unit->{factor};
1679 $main::lxdebug->leave_sub();
1681 return wantarray ? ($sum, $base_unit) : $sum;
1685 $main::lxdebug->enter_sub();
1687 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1689 my $dbh = $form->dbconnect_noauto($myconfig);
1691 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1692 my ($sortkey) = selectrow_query($form, $dbh, $query);
1694 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1695 "VALUES (?, ?, ?, ?)";
1696 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1699 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1700 my $sth = $dbh->prepare($query);
1701 foreach my $lang (@{$languages}) {
1702 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1703 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1711 $main::lxdebug->leave_sub();
1715 $main::lxdebug->enter_sub();
1717 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1719 my $dbh = $form->dbconnect_noauto($myconfig);
1721 my ($base_unit, $unit, $sth, $query);
1723 $query = "DELETE FROM units_language";
1724 $dbh->do($query) || $form->dberror($query);
1726 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1727 $query = "DELETE FROM units WHERE name IN (";
1728 map({ $query .= "?," } @{$delete_units});
1729 substr($query, -1, 1) = ")";
1730 $dbh->do($query, undef, @{$delete_units}) ||
1731 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1734 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1735 $sth = $dbh->prepare($query);
1737 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1738 my $sth_lang = $dbh->prepare($query_lang);
1740 foreach $unit (values(%{$units})) {
1741 $unit->{"depth"} = 0;
1742 my $base_unit = $unit;
1743 while ($base_unit->{"base_unit"}) {
1745 $base_unit = $units->{$base_unit->{"base_unit"}};
1749 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1750 if ($unit->{"LANGUAGES"}) {
1751 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1752 next unless ($lang->{"id"} && $lang->{"localized"});
1753 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1754 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1758 next if ($unit->{"unchanged_unit"});
1760 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1761 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1765 $sth_lang->finish();
1769 $main::lxdebug->leave_sub();
1773 $main::lxdebug->enter_sub();
1775 my ($self, $myconfig, $form) = @_;
1777 # connect to database
1778 my $dbh = $form->dbconnect($myconfig);
1780 my $query = qq|SELECT
1784 round(t.rate * 100, 2) AS rate,
1785 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1786 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1790 my $sth = $dbh->prepare($query);
1791 $sth->execute || $form->dberror($query);
1794 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1795 push @{ $form->{TAX} }, $ref;
1801 $main::lxdebug->leave_sub();
1804 sub get_tax_accounts {
1805 $main::lxdebug->enter_sub();
1807 my ($self, $myconfig, $form) = @_;
1809 my $dbh = $form->dbconnect($myconfig);
1811 # get Accounts from chart
1812 my $query = qq{ SELECT
1814 accno || ' - ' || description AS taxaccount
1816 WHERE link LIKE '%_tax%'
1820 my $sth = $dbh->prepare($query);
1821 $sth->execute || $form->dberror($query);
1823 $form->{ACCOUNTS} = [];
1824 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1825 push @{ $form->{ACCOUNTS} }, $ref;
1832 $main::lxdebug->leave_sub();
1836 $main::lxdebug->enter_sub();
1838 my ($self, $myconfig, $form) = @_;
1840 # connect to database
1841 my $dbh = $form->dbconnect($myconfig);
1843 my $query = qq|SELECT
1846 round(rate * 100, 2) AS rate,
1851 my $sth = $dbh->prepare($query);
1852 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1854 my $ref = $sth->fetchrow_hashref("NAME_lc");
1856 map { $form->{$_} = $ref->{$_} } keys %$ref;
1860 # see if it is used by a taxkey
1861 $query = qq|SELECT count(*) FROM taxkeys
1862 WHERE tax_id = ? AND chart_id >0|;
1864 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1866 $form->{orphaned} = !$form->{orphaned};
1869 if (!$form->{orphaned} ) {
1870 $query = qq|SELECT DISTINCT c.id, c.accno
1872 JOIN tax t ON (t.id = tk.tax_id)
1873 JOIN chart c ON (c.id = tk.chart_id)
1874 WHERE tk.tax_id = ?|;
1876 $sth = $dbh->prepare($query);
1877 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1879 $form->{TAXINUSE} = [];
1880 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1881 push @{ $form->{TAXINUSE} }, $ref;
1889 $main::lxdebug->leave_sub();
1893 $main::lxdebug->enter_sub();
1895 my ($self, $myconfig, $form) = @_;
1898 # connect to database
1899 my $dbh = $form->get_standard_dbh($myconfig);
1901 $form->{rate} = $form->{rate} / 100;
1903 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1904 if ($form->{id} ne "") {
1905 $query = qq|UPDATE tax SET
1910 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1912 push(@values, $form->{id});
1916 $query = qq|INSERT INTO tax (
1923 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1925 do_query($form, $dbh, $query, @values);
1929 $main::lxdebug->leave_sub();
1933 $main::lxdebug->enter_sub();
1935 my ($self, $myconfig, $form) = @_;
1938 # connect to database
1939 my $dbh = $form->get_standard_dbh($myconfig);
1941 $query = qq|DELETE FROM tax
1943 do_query($form, $dbh, $query, $form->{id});
1947 $main::lxdebug->leave_sub();
1950 sub save_price_factor {
1951 $main::lxdebug->enter_sub();
1953 my ($self, $myconfig, $form) = @_;
1955 # connect to database
1956 my $dbh = $form->get_standard_dbh($myconfig);
1959 my @values = ($form->{description}, conv_i($form->{factor}));
1962 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1963 push @values, conv_i($form->{id});
1966 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1969 do_query($form, $dbh, $query, @values);
1973 $main::lxdebug->leave_sub();
1976 sub get_all_price_factors {
1977 $main::lxdebug->enter_sub();
1979 my ($self, $myconfig, $form) = @_;
1981 # connect to database
1982 my $dbh = $form->get_standard_dbh($myconfig);
1984 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1986 $main::lxdebug->leave_sub();
1989 sub get_price_factor {
1990 $main::lxdebug->enter_sub();
1992 my ($self, $myconfig, $form) = @_;
1994 # connect to database
1995 my $dbh = $form->get_standard_dbh($myconfig);
1997 my $query = qq|SELECT description, factor,
1998 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1999 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2000 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2001 FROM price_factors WHERE id = ?|;
2003 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2005 $main::lxdebug->leave_sub();
2008 sub delete_price_factor {
2009 $main::lxdebug->enter_sub();
2011 my ($self, $myconfig, $form) = @_;
2013 # connect to database
2014 my $dbh = $form->get_standard_dbh($myconfig);
2016 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2019 $main::lxdebug->leave_sub();
2022 sub save_warehouse {
2023 $main::lxdebug->enter_sub();
2025 my ($self, $myconfig, $form) = @_;
2027 # connect to database
2028 my $dbh = $form->get_standard_dbh($myconfig);
2030 my ($query, @values, $sth);
2033 $query = qq|SELECT nextval('id')|;
2034 ($form->{id}) = selectrow_query($form, $dbh, $query);
2036 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2037 do_query($form, $dbh, $query, $form->{id});
2040 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2041 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2043 if (0 < $form->{number_of_new_bins}) {
2044 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2045 $sth = prepare_query($form, $dbh, $query);
2047 foreach my $i (1..$form->{number_of_new_bins}) {
2048 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2056 $main::lxdebug->leave_sub();
2060 $main::lxdebug->enter_sub();
2062 my ($self, $myconfig, $form) = @_;
2064 # connect to database
2065 my $dbh = $form->get_standard_dbh($myconfig);
2067 my ($query, @values, $commit_necessary, $sth);
2069 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2072 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2073 do_query($form, $dbh, $query, @values);
2075 $commit_necessary = 1;
2078 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2079 $sth = prepare_query($form, $dbh, $query);
2081 foreach my $row (1..$form->{rowcount}) {
2082 next if ($form->{"delete_${row}"});
2084 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2086 $commit_necessary = 1;
2091 $dbh->commit() if ($commit_necessary);
2093 $main::lxdebug->leave_sub();
2096 sub delete_warehouse {
2097 $main::lxdebug->enter_sub();
2099 my ($self, $myconfig, $form) = @_;
2101 # connect to database
2102 my $dbh = $form->get_standard_dbh($myconfig);
2104 my $id = conv_i($form->{id});
2105 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|;
2106 my ($count) = selectrow_query($form, $dbh, $query, $id);
2109 $main::lxdebug->leave_sub();
2113 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2114 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2118 $main::lxdebug->leave_sub();
2123 sub get_all_warehouses {
2124 $main::lxdebug->enter_sub();
2126 my ($self, $myconfig, $form) = @_;
2128 # connect to database
2129 my $dbh = $form->get_standard_dbh($myconfig);
2131 my $query = qq|SELECT w.id, w.description, w.invalid,
2132 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2134 ORDER BY w.sortkey|;
2136 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2138 $main::lxdebug->leave_sub();
2142 $main::lxdebug->enter_sub();
2144 my ($self, $myconfig, $form) = @_;
2146 # connect to database
2147 my $dbh = $form->get_standard_dbh($myconfig);
2149 my $id = conv_i($form->{id});
2150 my $query = qq|SELECT w.description, w.invalid
2154 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2156 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2158 $query = qq|SELECT b.*, EXISTS
2159 (SELECT i.warehouse_id
2161 WHERE i.bin_id = b.id
2165 WHERE b.warehouse_id = ?|;
2167 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2169 $main::lxdebug->leave_sub();