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 #======================================================================
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form) = @_;
50 my $dbh = $form->dbconnect($myconfig);
52 SELECT c.accno, c.description, c.charttype, c.category,
53 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
54 c.pos_bwa, datevautomatik,
55 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
56 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
59 ON (c.id=tk.chart_id AND tk.id =
60 (SELECT id FROM taxkeys
61 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
62 ORDER BY startdate DESC LIMIT 1))
67 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
68 my $sth = $dbh->prepare($query);
69 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
71 my $ref = $sth->fetchrow_hashref(NAME_lc);
73 foreach my $key (keys %$ref) {
74 $form->{"$key"} = $ref->{"$key"};
79 # get default accounts
80 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
82 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
83 $sth = $dbh->prepare($query);
84 $sth->execute || $form->dberror($query);
86 $ref = $sth->fetchrow_hashref(NAME_lc);
88 map { $form->{$_} = $ref->{$_} } keys %ref;
94 # get taxkeys and description
98 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
100 id||'--'||taxkey AS tax,
103 FROM tax ORDER BY taxkey
105 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
106 $sth = $dbh->prepare($query);
107 $sth->execute || $form->dberror($query);
109 $form->{TAXKEY} = [];
111 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
112 push @{ $form->{TAXKEY} }, $ref;
118 $query = qq|SELECT id, accno,description
122 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
123 $sth = $dbh->prepare($query);
124 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
126 $form->{NEWACCOUNT} = [];
127 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
128 push @{ $form->{NEWACCOUNT} }, $ref;
133 # get the taxkeys of account
147 LEFT JOIN tax t ON (t.id = tk.tax_id)
148 LEFT JOIN chart c ON (c.id = t.chart_id)
150 WHERE tk.chart_id = ?
151 ORDER BY startdate DESC
153 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
154 $sth = $dbh->prepare($query);
156 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
158 $form->{ACCOUNT_TAXKEYS} = [];
160 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
161 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
167 # check if we have any transactions
168 $query = qq|SELECT a.trans_id FROM acc_trans a
169 WHERE a.chart_id = ?|;
170 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
171 $sth = $dbh->prepare($query);
172 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
174 ($form->{orphaned}) = $sth->fetchrow_array;
175 $form->{orphaned} = !$form->{orphaned};
178 # check if new account is active
179 $form->{new_chart_valid} = 0;
180 if ($form->{new_chart_id}) {
181 $query = qq|SELECT current_date-valid_from FROM chart
183 $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
184 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
186 $form->{new_chart_valid} = 1;
193 $main::lxdebug->leave_sub();
197 $main::lxdebug->enter_sub();
199 my ($self, $myconfig, $form) = @_;
201 # connect to database, turn off AutoCommit
202 my $dbh = $form->dbconnect_noauto($myconfig);
204 # sanity check, can't have AR with AR_...
205 if ($form->{AR} || $form->{AP} || $form->{IC}) {
206 map { delete $form->{$_} }
207 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);
211 foreach my $item ($form->{AR}, $form->{AR_amount},
212 $form->{AR_tax}, $form->{AR_paid},
213 $form->{AP}, $form->{AP_amount},
214 $form->{AP_tax}, $form->{AP_paid},
215 $form->{IC}, $form->{IC_sale},
216 $form->{IC_cogs}, $form->{IC_taxpart},
217 $form->{IC_income}, $form->{IC_expense},
218 $form->{IC_taxservice}, $form->{CT_tax}
220 $form->{link} .= "${item}:" if ($item);
224 # strip blanks from accno
225 map { $form->{$_} =~ s/ //g; } qw(accno);
229 if ($form->{id} eq "NULL") {
233 if (!$form->{id} || $form->{id} eq "") {
234 $query = qq|SELECT nextval('id')|;
235 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
236 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
237 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
243 $query = qq|UPDATE chart SET
259 $form->{description},
263 conv_i($form->{pos_bwa}),
264 conv_i($form->{pos_bilanz}),
265 conv_i($form->{pos_eur}),
266 conv_i($form->{new_chart_id}),
267 conv_date($form->{valid_from}),
268 ($form->{datevautomatik} eq 'T') ? 'true':'false',
274 do_query($form, $dbh, $query, @values);
280 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
284 for $tk_count (0 .. $MAX_TRIES) {
288 # Check if the account already exists, else cancel
290 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
292 last READTAXKEYS if ( $form->{'id'} == 0);
294 # check if there is a startdate
295 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
300 # Add valid taxkeys into the array
303 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
304 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
305 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
306 chart_id => conv_i($form->{"id"}),
307 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
308 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
315 for my $j (0 .. $#taxkeys){
316 if ( defined $taxkeys[$j]{'id'} ){
319 if ($taxkeys[$j]{'delete'}){
321 DELETE FROM taxkeys WHERE id = ?
324 @values = ($taxkeys[$j]{'id'});
326 do_query($form, $dbh, $query, @values);
335 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
343 $taxkeys[$j]{'tax_id'},
344 $taxkeys[$j]{'chart_id'},
345 $taxkeys[$j]{'tax_id'},
346 $taxkeys[$j]{'pos_ustva'},
347 $taxkeys[$j]{'startdate'},
350 do_query($form, $dbh, $query, @values);
356 INSERT INTO taxkeys (
363 VALUES ((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 my $rc = $dbh->commit;
382 $main::lxdebug->leave_sub();
388 $main::lxdebug->enter_sub();
390 my ($self, $myconfig, $form) = @_;
392 # connect to database, turn off AutoCommit
393 my $dbh = $form->dbconnect_noauto($myconfig);
395 my $query = qq|SELECT count(*) FROM acc_trans a
396 WHERE a.chart_id = ?|;
397 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
401 $main::lxdebug->leave_sub();
405 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
406 foreach my $type (qw(inventory income expense)) {
409 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
410 qq|WHERE ${type}_accno_id = ?|;
411 do_query($form, $dbh, $query, $form->{id});
414 foreach my $table (qw(partstax customertax vendortax tax)) {
415 $query = qq|DELETE FROM $table
417 do_query($form, $dbh, $query, $form->{id});
420 # delete chart of account record
421 $query = qq|DELETE FROM chart
423 do_query($form, $dbh, $query, $form->{id});
425 # delete account taxkeys
426 $query = qq|DELETE FROM taxkeys
428 do_query($form, $dbh, $query, $form->{id});
430 # commit and redirect
431 my $rc = $dbh->commit;
434 $main::lxdebug->leave_sub();
440 $main::lxdebug->enter_sub();
442 my ($self, $myconfig, $form) = @_;
444 # connect to database
445 my $dbh = $form->dbconnect($myconfig);
447 my $query = qq|SELECT d.id, d.description, d.role
451 $sth = $dbh->prepare($query);
452 $sth->execute || $form->dberror($query);
455 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
456 push @{ $form->{ALL} }, $ref;
462 $main::lxdebug->leave_sub();
466 $main::lxdebug->enter_sub();
468 my ($self, $myconfig, $form) = @_;
470 # connect to database
471 my $dbh = $form->dbconnect($myconfig);
473 my $query = qq|SELECT d.description, d.role
476 my $sth = $dbh->prepare($query);
477 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
479 my $ref = $sth->fetchrow_hashref(NAME_lc);
481 map { $form->{$_} = $ref->{$_} } keys %$ref;
485 # see if it is in use
486 $query = qq|SELECT count(*) FROM dpt_trans d
487 WHERE d.department_id = ?|;
488 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
490 $form->{orphaned} = !$form->{orphaned};
495 $main::lxdebug->leave_sub();
498 sub save_department {
499 $main::lxdebug->enter_sub();
501 my ($self, $myconfig, $form) = @_;
503 # connect to database
504 my $dbh = $form->dbconnect($myconfig);
506 my @values = ($form->{description}, $form->{role});
508 $query = qq|UPDATE department SET
509 description = ?, role = ?
511 push(@values, $form->{id});
513 $query = qq|INSERT INTO department
517 do_query($form, $dbh, $query, @values);
521 $main::lxdebug->leave_sub();
524 sub delete_department {
525 $main::lxdebug->enter_sub();
527 my ($self, $myconfig, $form) = @_;
529 # connect to database
530 my $dbh = $form->dbconnect($myconfig);
532 $query = qq|DELETE FROM department
534 do_query($form, $dbh, $query, $form->{id});
538 $main::lxdebug->leave_sub();
542 $main::lxdebug->enter_sub();
544 my ($self, $myconfig, $form) = @_;
546 # connect to database
547 my $dbh = $form->dbconnect($myconfig);
549 my $query = qq|SELECT id, lead
553 $sth = $dbh->prepare($query);
554 $sth->execute || $form->dberror($query);
556 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
557 push @{ $form->{ALL} }, $ref;
563 $main::lxdebug->leave_sub();
567 $main::lxdebug->enter_sub();
569 my ($self, $myconfig, $form) = @_;
571 # connect to database
572 my $dbh = $form->dbconnect($myconfig);
575 qq|SELECT l.id, l.lead | .
578 my $sth = $dbh->prepare($query);
579 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
581 my $ref = $sth->fetchrow_hashref(NAME_lc);
583 map { $form->{$_} = $ref->{$_} } keys %$ref;
589 $main::lxdebug->leave_sub();
593 $main::lxdebug->enter_sub();
595 my ($self, $myconfig, $form) = @_;
597 # connect to database
598 my $dbh = $form->dbconnect($myconfig);
600 my @values = ($form->{description});
601 # id is the old record
603 $query = qq|UPDATE leads SET
606 puhs(@values, $form->{id});
608 $query = qq|INSERT INTO leads
612 do_query($form, $dbh, $query, @values);
616 $main::lxdebug->leave_sub();
620 $main::lxdebug->enter_sub();
622 my ($self, $myconfig, $form) = @_;
624 # connect to database
625 my $dbh = $form->dbconnect($myconfig);
627 $query = qq|DELETE FROM leads
629 do_query($form, $dbh, $query, $form->{id});
633 $main::lxdebug->leave_sub();
637 $main::lxdebug->enter_sub();
639 my ($self, $myconfig, $form) = @_;
641 # connect to database
642 my $dbh = $form->dbconnect($myconfig);
644 my $query = qq|SELECT id, description, discount, customernumberinit
648 $sth = $dbh->prepare($query);
649 $sth->execute || $form->dberror($query);
651 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
652 push @{ $form->{ALL} }, $ref;
658 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
670 qq|SELECT b.description, b.discount, b.customernumberinit
673 my $sth = $dbh->prepare($query);
674 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
676 my $ref = $sth->fetchrow_hashref(NAME_lc);
678 map { $form->{$_} = $ref->{$_} } keys %$ref;
684 $main::lxdebug->leave_sub();
688 $main::lxdebug->enter_sub();
690 my ($self, $myconfig, $form) = @_;
692 # connect to database
693 my $dbh = $form->dbconnect($myconfig);
695 my @values = ($form->{description}, $form->{discount},
696 $form->{customernumberinit});
697 # id is the old record
699 $query = qq|UPDATE business SET
702 customernumberinit = ?
704 push(@values, $form->{id});
706 $query = qq|INSERT INTO business
707 (description, discount, customernumberinit)
710 do_query($form, $dbh, $query, @values);
714 $main::lxdebug->leave_sub();
717 sub delete_business {
718 $main::lxdebug->enter_sub();
720 my ($self, $myconfig, $form) = @_;
722 # connect to database
723 my $dbh = $form->dbconnect($myconfig);
725 $query = qq|DELETE FROM business
727 do_query($form, $dbh, $query, $form->{id});
731 $main::lxdebug->leave_sub();
736 $main::lxdebug->enter_sub();
738 my ($self, $myconfig, $form, $return_list) = @_;
740 # connect to database
741 my $dbh = $form->dbconnect($myconfig);
744 "SELECT id, description, template_code, article_code, " .
745 " output_numberformat, output_dateformat, output_longdates " .
746 "FROM language ORDER BY description";
748 $sth = $dbh->prepare($query);
749 $sth->execute || $form->dberror($query);
753 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
754 push(@{ $ary }, $ref);
760 $main::lxdebug->leave_sub();
770 $main::lxdebug->enter_sub();
772 my ($self, $myconfig, $form) = @_;
774 # connect to database
775 my $dbh = $form->dbconnect($myconfig);
778 "SELECT description, template_code, article_code, " .
779 " output_numberformat, output_dateformat, output_longdates " .
780 "FROM language WHERE id = ?";
781 my $sth = $dbh->prepare($query);
782 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
784 my $ref = $sth->fetchrow_hashref(NAME_lc);
786 map { $form->{$_} = $ref->{$_} } keys %$ref;
792 $main::lxdebug->leave_sub();
795 sub get_language_details {
796 $main::lxdebug->enter_sub();
798 my ($self, $myconfig, $form, $id) = @_;
800 # connect to database
801 my $dbh = $form->dbconnect($myconfig);
804 "SELECT template_code, " .
805 " output_numberformat, output_dateformat, output_longdates " .
806 "FROM language WHERE id = ?";
807 my @res = selectrow_query($form, $dbh, $query, $id);
810 $main::lxdebug->leave_sub();
816 $main::lxdebug->enter_sub();
818 my ($self, $myconfig, $form) = @_;
820 # connect to database
821 my $dbh = $form->dbconnect($myconfig);
822 my (@values, $query);
824 map({ push(@values, $form->{$_}); }
825 qw(description template_code article_code
826 output_numberformat output_dateformat output_longdates));
828 # id is the old record
831 "UPDATE language SET " .
832 " description = ?, template_code = ?, article_code = ?, " .
833 " output_numberformat = ?, output_dateformat = ?, " .
834 " output_longdates = ? " .
836 push(@values, $form->{id});
839 "INSERT INTO language (" .
840 " description, template_code, article_code, " .
841 " output_numberformat, output_dateformat, output_longdates" .
842 ") VALUES (?, ?, ?, ?, ?, ?)";
844 do_query($form, $dbh, $query, @values);
848 $main::lxdebug->leave_sub();
851 sub delete_language {
852 $main::lxdebug->enter_sub();
854 my ($self, $myconfig, $form) = @_;
856 # connect to database
857 my $dbh = $form->dbconnect_noauto($myconfig);
859 foreach my $table (qw(translation_payment_terms units_language)) {
860 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
861 do_query($form, $dbh, $query, $form->{"id"});
864 $query = "DELETE FROM language WHERE id = ?";
865 do_query($form, $dbh, $query, $form->{"id"});
870 $main::lxdebug->leave_sub();
875 $main::lxdebug->enter_sub();
877 my ($self, $myconfig, $form) = @_;
879 # connect to database
880 my $dbh = $form->dbconnect($myconfig);
882 my $query = qq|SELECT id, description,
884 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
886 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
888 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
890 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
892 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
894 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
896 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
898 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
900 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
904 $sth = $dbh->prepare($query);
905 $sth->execute || $form->dberror($query);
908 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
909 push @{ $form->{ALL} }, $ref;
915 $main::lxdebug->leave_sub();
918 sub get_buchungsgruppe {
919 $main::lxdebug->enter_sub();
921 my ($self, $myconfig, $form) = @_;
924 # connect to database
925 my $dbh = $form->dbconnect($myconfig);
929 qq|SELECT description, inventory_accno_id,
930 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
932 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
934 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
936 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
938 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
940 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
942 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
944 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
946 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
949 my $sth = $dbh->prepare($query);
950 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
952 my $ref = $sth->fetchrow_hashref(NAME_lc);
954 map { $form->{$_} = $ref->{$_} } keys %$ref;
959 qq|SELECT count(id) = 0 AS orphaned
961 WHERE buchungsgruppen_id = ?|;
962 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
965 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
967 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
968 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
971 $query = qq|SELECT c.accno, c.description, c.link, c.id,
972 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
973 FROM chart c, defaults d
974 WHERE c.link LIKE '%$module%'
978 my $sth = $dbh->prepare($query);
979 $sth->execute || $form->dberror($query);
980 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
981 foreach my $key (split(/:/, $ref->{link})) {
982 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
983 $form->{"std_inventory_accno_id"} = $ref->{"id"};
985 if ($key =~ /$module/) {
986 if ( ($ref->{id} eq $ref->{inventory_accno_id})
987 || ($ref->{id} eq $ref->{income_accno_id})
988 || ($ref->{id} eq $ref->{expense_accno_id})) {
989 push @{ $form->{"${module}_links"}{$key} },
990 { accno => $ref->{accno},
991 description => $ref->{description},
992 selected => "selected",
995 push @{ $form->{"${module}_links"}{$key} },
996 { accno => $ref->{accno},
997 description => $ref->{description},
1009 $main::lxdebug->leave_sub();
1012 sub save_buchungsgruppe {
1013 $main::lxdebug->enter_sub();
1015 my ($self, $myconfig, $form) = @_;
1017 # connect to database
1018 my $dbh = $form->dbconnect($myconfig);
1020 my @values = ($form->{description}, $form->{inventory_accno_id},
1021 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1022 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1023 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1024 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1028 # id is the old record
1030 $query = qq|UPDATE buchungsgruppen SET
1031 description = ?, inventory_accno_id = ?,
1032 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1033 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1034 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1035 income_accno_id_3 = ?, expense_accno_id_3 = ?
1037 push(@values, $form->{id});
1039 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1040 my ($sortkey) = $dbh->selectrow_array($query);
1041 $form->dberror($query) if ($dbh->err);
1042 push(@values, $sortkey);
1043 $query = qq|INSERT INTO buchungsgruppen
1044 (description, inventory_accno_id,
1045 income_accno_id_0, expense_accno_id_0,
1046 income_accno_id_1, expense_accno_id_1,
1047 income_accno_id_2, expense_accno_id_2,
1048 income_accno_id_3, expense_accno_id_3,
1050 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1052 do_query($form, $dbh, $query, @values);
1056 $main::lxdebug->leave_sub();
1059 sub delete_buchungsgruppe {
1060 $main::lxdebug->enter_sub();
1062 my ($self, $myconfig, $form) = @_;
1064 # connect to database
1065 my $dbh = $form->dbconnect($myconfig);
1067 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1068 do_query($form, $dbh, $query, $form->{id});
1072 $main::lxdebug->leave_sub();
1076 $main::lxdebug->enter_sub();
1078 my ($self, $myconfig, $form, $table) = @_;
1080 # connect to database
1081 my $dbh = $form->get_standard_dbh($myconfig);
1085 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1086 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1087 my @values = ($form->{"id1"}, $form->{"id2"});
1088 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1090 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1091 my $sth = prepare_query($form, $dbh, $query);
1093 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1094 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1100 $main::lxdebug->leave_sub();
1104 $main::lxdebug->enter_sub();
1106 my ($self, $myconfig, $form) = @_;
1108 # connect to database
1109 my $dbh = $form->dbconnect($myconfig);
1111 my $query = qq|SELECT id, printer_description, template_code, printer_command
1115 $sth = $dbh->prepare($query);
1116 $sth->execute || $form->dberror($query);
1118 $form->{"ALL"} = [];
1119 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1120 push @{ $form->{ALL} }, $ref;
1126 $main::lxdebug->leave_sub();
1130 $main::lxdebug->enter_sub();
1132 my ($self, $myconfig, $form) = @_;
1134 # connect to database
1135 my $dbh = $form->dbconnect($myconfig);
1138 qq|SELECT p.printer_description, p.template_code, p.printer_command
1141 my $sth = $dbh->prepare($query);
1142 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1144 my $ref = $sth->fetchrow_hashref(NAME_lc);
1146 map { $form->{$_} = $ref->{$_} } keys %$ref;
1152 $main::lxdebug->leave_sub();
1156 $main::lxdebug->enter_sub();
1158 my ($self, $myconfig, $form) = @_;
1160 # connect to database
1161 my $dbh = $form->dbconnect($myconfig);
1163 my @values = ($form->{printer_description},
1164 $form->{template_code},
1165 $form->{printer_command});
1167 # id is the old record
1169 $query = qq|UPDATE printers SET
1170 printer_description = ?, template_code = ?, printer_command = ?
1172 push(@values, $form->{id});
1174 $query = qq|INSERT INTO printers
1175 (printer_description, template_code, printer_command)
1178 do_query($form, $dbh, $query, @values);
1182 $main::lxdebug->leave_sub();
1185 sub delete_printer {
1186 $main::lxdebug->enter_sub();
1188 my ($self, $myconfig, $form) = @_;
1190 # connect to database
1191 my $dbh = $form->dbconnect($myconfig);
1193 $query = qq|DELETE FROM printers
1195 do_query($form, $dbh, $query, $form->{id});
1199 $main::lxdebug->leave_sub();
1203 $main::lxdebug->enter_sub();
1205 my ($self, $myconfig, $form) = @_;
1207 # connect to database
1208 my $dbh = $form->dbconnect($myconfig);
1210 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1212 $sth = $dbh->prepare($query);
1213 $sth->execute || $form->dberror($query);
1216 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1217 push @{ $form->{ALL} }, $ref;
1223 $main::lxdebug->leave_sub();
1227 $main::lxdebug->enter_sub();
1229 my ($self, $myconfig, $form) = @_;
1231 # connect to database
1232 my $dbh = $form->dbconnect($myconfig);
1234 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1235 my $sth = $dbh->prepare($query);
1236 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1238 my $ref = $sth->fetchrow_hashref(NAME_lc);
1239 map { $form->{$_} = $ref->{$_} } keys %$ref;
1243 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1244 qq|FROM translation_payment_terms t | .
1245 qq|LEFT JOIN language l ON t.language_id = l.id | .
1246 qq|WHERE t.payment_terms_id = ? | .
1248 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1249 qq| l.description AS language | .
1250 qq|FROM language l|;
1251 $sth = $dbh->prepare($query);
1252 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1255 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1256 $mapping{ $ref->{"language_id"} } = $ref
1257 unless (defined($mapping{ $ref->{"language_id"} }));
1261 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1266 $main::lxdebug->leave_sub();
1270 $main::lxdebug->enter_sub();
1272 my ($self, $myconfig, $form) = @_;
1274 # connect to database
1275 my $dbh = $form->dbconnect_noauto($myconfig);
1280 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1281 qq|FROM payment_terms|;
1283 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1285 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1286 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1290 qq|DELETE FROM translation_payment_terms | .
1291 qq|WHERE payment_terms_id = ?|;
1292 do_query($form, $dbh, $query, $form->{"id"});
1295 $query = qq|UPDATE payment_terms SET
1296 description = ?, description_long = ?,
1297 terms_netto = ?, terms_skonto = ?,
1300 my @values = ($form->{description}, $form->{description_long},
1301 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1302 $form->{percent_skonto} * 1,
1304 do_query($form, $dbh, $query, @values);
1306 $query = qq|SELECT id FROM language|;
1308 my $sth = $dbh->prepare($query);
1309 $sth->execute() || $form->dberror($query);
1311 while (my ($id) = $sth->fetchrow_array()) {
1312 push(@language_ids, $id);
1317 qq|INSERT INTO translation_payment_terms | .
1318 qq|(language_id, payment_terms_id, description_long) | .
1319 qq|VALUES (?, ?, ?)|;
1320 $sth = $dbh->prepare($query);
1322 foreach my $language_id (@language_ids) {
1323 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1324 $form->{"description_long_${language_id}"});
1331 $main::lxdebug->leave_sub();
1334 sub delete_payment {
1335 $main::lxdebug->enter_sub();
1337 my ($self, $myconfig, $form) = @_;
1339 # connect to database
1340 my $dbh = $form->dbconnect_noauto($myconfig);
1343 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1344 do_query($form, $dbh, $query, $form->{"id"});
1346 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1347 do_query($form, $dbh, $query, $form->{"id"});
1352 $main::lxdebug->leave_sub();
1356 sub prepare_template_filename {
1357 $main::lxdebug->enter_sub();
1359 my ($self, $myconfig, $form) = @_;
1361 my ($filename, $display_filename);
1363 if ($form->{type} eq "stylesheet") {
1364 $filename = "css/$myconfig->{stylesheet}";
1365 $display_filename = $myconfig->{stylesheet};
1368 $filename = $form->{formname};
1370 if ($form->{language}) {
1371 my ($id, $template_code) = split(/--/, $form->{language});
1372 $filename .= "_${template_code}";
1375 if ($form->{printer}) {
1376 my ($id, $template_code) = split(/--/, $form->{printer});
1377 $filename .= "_${template_code}";
1380 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1381 $filename =~ s|.*/||;
1382 $display_filename = $filename;
1383 $filename = "$myconfig->{templates}/$filename";
1386 $main::lxdebug->leave_sub();
1388 return ($filename, $display_filename);
1393 $main::lxdebug->enter_sub();
1395 my ($self, $filename) = @_;
1397 my ($content, $lines) = ("", 0);
1401 if (open(TEMPLATE, $filename)) {
1402 while (<TEMPLATE>) {
1409 $main::lxdebug->leave_sub();
1411 return ($content, $lines);
1415 $main::lxdebug->enter_sub();
1417 my ($self, $filename, $content) = @_;
1423 if (open(TEMPLATE, ">$filename")) {
1424 $content =~ s/\r\n/\n/g;
1425 print(TEMPLATE $content);
1431 $main::lxdebug->leave_sub();
1437 $main::lxdebug->enter_sub();
1442 my $myconfig = \%main::myconfig;
1443 my $form = $main::form;
1445 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1448 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1450 $form->{curr} =~ s/ //g;
1451 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1452 my $currency = join ':', @currencies;
1454 # these defaults are database wide
1457 qq|UPDATE defaults SET
1458 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1459 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1460 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1461 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1462 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1477 businessnumber = ?|;
1478 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1479 $accnos{fxgain_accno}, $accnos{fxloss_accno},
1480 $form->{invnumber}, $form->{cnnumber},
1481 $form->{sonumber}, $form->{ponumber},
1482 $form->{sqnumber}, $form->{rfqnumber},
1483 $form->{customernumber}, $form->{vendornumber},
1484 $form->{articlenumber}, $form->{servicenumber},
1485 $form->{sdonumber}, $form->{pdonumber},
1486 $form->{yearend}, $currency,
1487 $form->{businessnumber});
1488 do_query($form, $dbh, $query, @values);
1492 $main::lxdebug->leave_sub();
1496 sub save_preferences {
1497 $main::lxdebug->enter_sub();
1499 my ($self, $myconfig, $form, $webdav) = @_;
1501 my $dbh = $form->get_standard_dbh($myconfig);
1503 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1506 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1507 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1509 my $rc = $dbh->commit();
1511 # save first currency in myconfig
1512 $currency =~ s/:.*//;
1513 $form->{currency} = $currency;
1515 $form->{businessnumber} = $businessnumber;
1517 my $myconfig = new User($form->{login});
1519 foreach my $item (keys %$form) {
1520 $myconfig->{$item} = $form->{$item};
1523 $myconfig->save_member($memberfile);
1525 my $auth = $main::auth;
1527 if ($auth->can_change_password()
1528 && defined $form->{new_password}
1529 && ($form->{new_password} ne '********')) {
1530 $auth->change_password($form->{login}, $form->{new_password});
1532 $form->{password} = $form->{new_password};
1533 $auth->set_session_value('password', $form->{password});
1534 $auth->create_or_refresh_session();
1539 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1540 foreach $directory (@webdavdirs) {
1541 $file = "webdav/" . $directory . "/webdav-user";
1542 if ($myconfig->{$directory}) {
1543 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1544 while (<HTACCESS>) {
1545 ($login, $password) = split(/:/, $_);
1546 if ($login ne $form->{login}) {
1551 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1552 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1553 print(HTACCESS $newfile);
1556 $form->{$directory} = 0;
1557 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1558 while (<HTACCESS>) {
1559 ($login, $password) = split(/:/, $_);
1560 if ($login ne $form->{login}) {
1565 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1566 print(HTACCESS $newfile);
1572 $main::lxdebug->leave_sub();
1577 sub defaultaccounts {
1578 $main::lxdebug->enter_sub();
1580 my ($self, $myconfig, $form) = @_;
1582 # connect to database
1583 my $dbh = $form->dbconnect($myconfig);
1585 # get defaults from defaults table
1586 my $query = qq|SELECT * FROM defaults|;
1587 my $sth = $dbh->prepare($query);
1588 $sth->execute || $form->dberror($query);
1590 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1591 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1592 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1593 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1594 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1595 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1599 $query = qq|SELECT c.id, c.accno, c.description, c.link
1601 WHERE c.link LIKE '%IC%'
1603 $sth = $dbh->prepare($query);
1604 $sth->execute || $self->dberror($query);
1606 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1607 foreach my $key (split(/:/, $ref->{link})) {
1610 if ($key =~ /cogs/) {
1611 $nkey = "IC_expense";
1613 if ($key =~ /sale/) {
1614 $nkey = "IC_income";
1616 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1618 description => $ref->{description}
1625 $query = qq|SELECT c.id, c.accno, c.description
1627 WHERE c.category = 'I'
1628 AND c.charttype = 'A'
1630 $sth = $dbh->prepare($query);
1631 $sth->execute || $self->dberror($query);
1633 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1634 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1636 description => $ref->{description}
1641 $query = qq|SELECT c.id, c.accno, c.description
1643 WHERE c.category = 'E'
1644 AND c.charttype = 'A'
1646 $sth = $dbh->prepare($query);
1647 $sth->execute || $self->dberror($query);
1649 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1650 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1652 description => $ref->{description}
1657 # now get the tax rates and numbers
1658 $query = qq|SELECT c.id, c.accno, c.description,
1659 t.rate * 100 AS rate, t.taxnumber
1661 WHERE c.id = t.chart_id|;
1663 $sth = $dbh->prepare($query);
1664 $sth->execute || $form->dberror($query);
1666 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1667 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1668 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1669 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1670 if $ref->{taxnumber};
1671 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1677 $main::lxdebug->leave_sub();
1681 $main::lxdebug->enter_sub();
1683 my ($self, $myconfig, $form) = @_;
1685 my $dbh = $form->dbconnect($myconfig);
1687 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1688 my $sth = $dbh->prepare($query);
1689 $sth->execute || $form->dberror($query);
1691 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1697 $main::lxdebug->leave_sub();
1701 $main::lxdebug->enter_sub();
1703 my ($self, $myconfig, $form) = @_;
1705 my $dbh = $form->dbconnect($myconfig);
1707 my ($query, @values);
1709 if ($form->{revtrans}) {
1710 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1712 } elsif ($form->{closedto}) {
1713 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1714 @values = (conv_date($form->{closedto}));
1717 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1720 # set close in defaults
1721 do_query($form, $dbh, $query, @values);
1725 $main::lxdebug->leave_sub();
1729 my ($self, $units, $unit_name, $factor) = @_;
1731 $factor = 1 unless ($factor);
1733 my $unit = $units->{$unit_name};
1735 if (!defined($unit) || !$unit->{"base_unit"} ||
1736 ($unit_name eq $unit->{"base_unit"})) {
1737 return ($unit_name, $factor);
1740 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1743 sub retrieve_units {
1744 $main::lxdebug->enter_sub();
1746 my ($self, $myconfig, $form, $prefix) = @_;
1748 my $dbh = $form->dbconnect($myconfig);
1750 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1752 my $sth = prepare_execute_query($form, $dbh, $query);
1755 while (my $ref = $sth->fetchrow_hashref()) {
1756 $units->{$ref->{"name"}} = $ref;
1760 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1761 $sth = $dbh->prepare($query_lang);
1762 $sth->execute() || $form->dberror($query_lang);
1764 while ($ref = $sth->fetchrow_hashref()) {
1765 push(@languages, $ref);
1769 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1770 "FROM units_language ul " .
1771 "LEFT JOIN language l ON ul.language_id = l.id " .
1772 "WHERE ul.unit = ?";
1773 $sth = $dbh->prepare($query_lang);
1775 foreach my $unit (values(%{$units})) {
1776 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1778 $unit->{"LANGUAGES"} = {};
1779 foreach my $lang (@languages) {
1780 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1783 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1784 while ($ref = $sth->fetchrow_hashref()) {
1785 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1792 $main::lxdebug->leave_sub();
1797 sub retrieve_all_units {
1798 $main::lxdebug->enter_sub();
1802 if (!$main::all_units) {
1803 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1806 $main::lxdebug->leave_sub();
1808 return $main::all_units;
1812 sub translate_units {
1813 $main::lxdebug->enter_sub();
1815 my ($self, $form, $template_code, $unit, $amount) = @_;
1817 my $units = $self->retrieve_units(\%main::myconfig, $form);
1819 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1820 my $new_unit = $unit;
1822 if (($amount != 1) && $h->{"localized_plural"}) {
1823 $new_unit = $h->{"localized_plural"};
1824 } elsif ($h->{"localized"}) {
1825 $new_unit = $h->{"localized"};
1829 $main::lxdebug->leave_sub();
1835 $main::lxdebug->enter_sub();
1837 my ($self, $myconfig, $form, $units) = @_;
1839 my $dbh = $form->dbconnect($myconfig);
1841 map({ $_->{"in_use"} = 0; } values(%{$units}));
1843 foreach my $unit (values(%{$units})) {
1844 my $base_unit = $unit->{"original_base_unit"};
1845 while ($base_unit) {
1846 $units->{$base_unit}->{"in_use"} = 1;
1847 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1848 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1849 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1853 foreach my $unit (values(%{$units})) {
1854 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1856 foreach my $table (qw(parts invoice orderitems)) {
1857 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1859 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1860 $query .= "= " . $dbh->quote($unit->{"name"});
1862 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1863 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1866 my ($count) = $dbh->selectrow_array($query);
1867 $form->dberror($query) if ($dbh->err);
1870 $unit->{"in_use"} = 1;
1878 $main::lxdebug->leave_sub();
1881 sub convertible_units {
1882 $main::lxdebug->enter_sub();
1886 my $filter_unit = shift;
1887 my $not_smaller = shift;
1889 my $conv_units = [];
1891 $filter_unit = $units->{$filter_unit};
1893 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1894 my $unit = $units->{$name};
1896 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1897 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1898 push @{$conv_units}, $unit;
1902 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1904 $main::lxdebug->leave_sub();
1909 # if $a is translatable to $b, return the factor between them.
1912 $main::lxdebug->enter_sub(2);
1913 ($this, $a, $b, $all_units) = @_;
1915 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1916 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1917 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1920 sub unit_select_data {
1921 $main::lxdebug->enter_sub();
1923 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1928 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1931 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1932 if (!$convertible_into ||
1933 ($units->{$convertible_into} &&
1934 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1935 push @{$select}, { "name" => $unit,
1936 "base_unit" => $units->{$unit}->{"base_unit"},
1937 "factor" => $units->{$unit}->{"factor"},
1938 "selected" => ($unit eq $selected) ? "selected" : "" };
1942 $main::lxdebug->leave_sub();
1947 sub unit_select_html {
1948 $main::lxdebug->enter_sub();
1950 my ($self, $units, $name, $selected, $convertible_into) = @_;
1952 my $select = "<select name=${name}>";
1954 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1955 if (!$convertible_into ||
1956 ($units->{$convertible_into} &&
1957 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1958 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1961 $select .= "</select>";
1963 $main::lxdebug->leave_sub();
1969 $main::lxdebug->enter_sub();
1973 my $units = $self->retrieve_all_units();
1978 while (2 <= scalar(@_)) {
1979 my $qty = shift(@_);
1980 my $unit = $units->{shift(@_)};
1982 croak "No unit defined with name $unit" if (!defined $unit);
1985 $base_unit = $unit->{base_unit};
1986 } elsif ($base_unit ne $unit->{base_unit}) {
1987 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1990 $sum += $qty * $unit->{factor};
1993 $main::lxdebug->leave_sub();
1995 return wantarray ? ($sum, $baseunit) : $sum;
1999 $main::lxdebug->enter_sub();
2001 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
2003 my $dbh = $form->dbconnect_noauto($myconfig);
2005 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2006 my ($sortkey) = selectrow_query($form, $dbh, $query);
2008 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
2009 "VALUES (?, ?, ?, ?)";
2010 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
2013 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2014 my $sth = $dbh->prepare($query);
2015 foreach my $lang (@{$languages}) {
2016 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2017 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2025 $main::lxdebug->leave_sub();
2029 $main::lxdebug->enter_sub();
2031 my ($self, $myconfig, $form, $units, $delete_units) = @_;
2033 my $dbh = $form->dbconnect_noauto($myconfig);
2035 my ($base_unit, $unit, $sth, $query);
2037 $query = "DELETE FROM units_language";
2038 $dbh->do($query) || $form->dberror($query);
2040 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2041 $query = "DELETE FROM units WHERE name IN (";
2042 map({ $query .= "?," } @{$delete_units});
2043 substr($query, -1, 1) = ")";
2044 $dbh->do($query, undef, @{$delete_units}) ||
2045 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2048 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2049 $sth = $dbh->prepare($query);
2051 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2052 my $sth_lang = $dbh->prepare($query_lang);
2054 foreach $unit (values(%{$units})) {
2055 $unit->{"depth"} = 0;
2056 my $base_unit = $unit;
2057 while ($base_unit->{"base_unit"}) {
2059 $base_unit = $units->{$base_unit->{"base_unit"}};
2063 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2064 if ($unit->{"LANGUAGES"}) {
2065 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2066 next unless ($lang->{"id"} && $lang->{"localized"});
2067 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2068 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2072 next if ($unit->{"unchanged_unit"});
2074 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2075 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2079 $sth_lang->finish();
2083 $main::lxdebug->leave_sub();
2087 $main::lxdebug->enter_sub();
2089 my ($self, $myconfig, $form, $dir, $name_1) = @_;
2091 my $dbh = $form->dbconnect_noauto($myconfig);
2095 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2096 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2099 qq|SELECT sortkey FROM units | .
2100 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
2101 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2102 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
2104 if (defined($sortkey_1)) {
2105 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2106 my ($name_2) = selectrow_query($form, $dbh, $query);
2108 if (defined($name_2)) {
2109 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2110 my $sth = $dbh->prepare($query);
2112 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2113 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2120 $main::lxdebug->leave_sub();
2124 $main::lxdebug->enter_sub();
2126 my ($self, $myconfig, $form) = @_;
2128 # connect to database
2129 my $dbh = $form->dbconnect($myconfig);
2131 my $query = qq|SELECT
2135 round(t.rate * 100, 2) AS rate,
2136 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2137 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2141 $sth = $dbh->prepare($query);
2142 $sth->execute || $form->dberror($query);
2145 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2146 push @{ $form->{TAX} }, $ref;
2152 $main::lxdebug->leave_sub();
2155 sub get_tax_accounts {
2156 $main::lxdebug->enter_sub();
2158 my ($self, $myconfig, $form) = @_;
2160 my $dbh = $form->dbconnect($myconfig);
2162 # get Accounts from chart
2163 my $query = qq{ SELECT
2165 accno || ' - ' || description AS taxaccount
2167 WHERE link LIKE '%_tax%'
2171 $sth = $dbh->prepare($query);
2172 $sth->execute || $form->dberror($query);
2174 $form->{ACCOUNTS} = [];
2175 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2176 push @{ $form->{ACCOUNTS} }, $ref;
2183 $main::lxdebug->leave_sub();
2187 $main::lxdebug->enter_sub();
2189 my ($self, $myconfig, $form) = @_;
2191 # connect to database
2192 my $dbh = $form->dbconnect($myconfig);
2194 my $query = qq|SELECT
2197 round(rate * 100, 2) AS rate,
2202 my $sth = $dbh->prepare($query);
2203 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2205 my $ref = $sth->fetchrow_hashref(NAME_lc);
2207 map { $form->{$_} = $ref->{$_} } keys %$ref;
2211 # see if it is used by a taxkey
2212 $query = qq|SELECT count(*) FROM taxkeys
2213 WHERE tax_id = ? AND chart_id >0|;
2215 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2217 $form->{orphaned} = !$form->{orphaned};
2220 if (!$form->{orphaned} ) {
2221 $query = qq|SELECT DISTINCT c.id, c.accno
2223 JOIN tax t ON (t.id = tk.tax_id)
2224 JOIN chart c ON (c.id = tk.chart_id)
2225 WHERE tk.tax_id = ?|;
2227 $sth = $dbh->prepare($query);
2228 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2230 $form->{TAXINUSE} = [];
2231 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2232 push @{ $form->{TAXINUSE} }, $ref;
2240 $main::lxdebug->leave_sub();
2244 $main::lxdebug->enter_sub();
2246 my ($self, $myconfig, $form) = @_;
2248 # connect to database
2249 my $dbh = $form->get_standard_dbh($myconfig);
2251 $form->{rate} = $form->{rate} / 100;
2253 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2254 if ($form->{id} ne "") {
2255 $query = qq|UPDATE tax SET
2260 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2262 push(@values, $form->{id});
2266 $query = qq|INSERT INTO tax (
2273 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2275 do_query($form, $dbh, $query, @values);
2279 $main::lxdebug->leave_sub();
2283 $main::lxdebug->enter_sub();
2285 my ($self, $myconfig, $form) = @_;
2287 # connect to database
2288 my $dbh = $form->get_standard_dbh($myconfig);
2290 $query = qq|DELETE FROM tax
2292 do_query($form, $dbh, $query, $form->{id});
2296 $main::lxdebug->leave_sub();
2299 sub save_price_factor {
2300 $main::lxdebug->enter_sub();
2302 my ($self, $myconfig, $form) = @_;
2304 # connect to database
2305 my $dbh = $form->get_standard_dbh($myconfig);
2308 my @values = ($form->{description}, conv_i($form->{factor}));
2311 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2312 push @values, conv_i($form->{id});
2315 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2318 do_query($form, $dbh, $query, @values);
2322 $main::lxdebug->leave_sub();
2325 sub get_all_price_factors {
2326 $main::lxdebug->enter_sub();
2328 my ($self, $myconfig, $form) = @_;
2330 # connect to database
2331 my $dbh = $form->get_standard_dbh($myconfig);
2333 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2335 $main::lxdebug->leave_sub();
2338 sub get_price_factor {
2339 $main::lxdebug->enter_sub();
2341 my ($self, $myconfig, $form) = @_;
2343 # connect to database
2344 my $dbh = $form->get_standard_dbh($myconfig);
2346 my $query = qq|SELECT description, factor,
2347 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2348 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2349 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2350 FROM price_factors WHERE id = ?|;
2352 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2354 $main::lxdebug->leave_sub();
2357 sub delete_price_factor {
2358 $main::lxdebug->enter_sub();
2360 my ($self, $myconfig, $form) = @_;
2362 # connect to database
2363 my $dbh = $form->get_standard_dbh($myconfig);
2365 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2368 $main::lxdebug->leave_sub();
2371 sub save_warehouse {
2372 $main::lxdebug->enter_sub();
2374 my ($self, $myconfig, $form) = @_;
2376 # connect to database
2377 my $dbh = $form->get_standard_dbh($myconfig);
2379 my ($query, @values, $sth);
2382 $query = qq|SELECT nextval('id')|;
2383 ($form->{id}) = selectrow_query($form, $dbh, $query);
2385 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2386 do_query($form, $dbh, $query, $form->{id});
2389 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2390 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2392 if (0 < $form->{number_of_new_bins}) {
2393 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2394 $sth = prepare_query($form, $dbh, $query);
2396 foreach my $i (1..$form->{number_of_new_bins}) {
2397 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2405 $main::lxdebug->leave_sub();
2409 $main::lxdebug->enter_sub();
2411 my ($self, $myconfig, $form) = @_;
2413 # connect to database
2414 my $dbh = $form->get_standard_dbh($myconfig);
2416 my ($query, @values, $commit_necessary, $sth);
2418 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2421 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2422 do_query($form, $dbh, $query, @values);
2424 $commit_necessary = 1;
2427 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2428 $sth = prepare_query($form, $dbh, $query);
2430 foreach my $row (1..$form->{rowcount}) {
2431 next if ($form->{"delete_${row}"});
2433 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2435 $commit_necessary = 1;
2440 $dbh->commit() if ($commit_necessary);
2442 $main::lxdebug->leave_sub();
2445 sub delete_warehouse {
2446 $main::lxdebug->enter_sub();
2448 my ($self, $myconfig, $form) = @_;
2450 # connect to database
2451 my $dbh = $form->get_standard_dbh($myconfig);
2453 my $id = conv_i($form->{id});
2454 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|;
2455 my ($count) = selectrow_query($form, $dbh, $query, $id);
2458 $main::lxdebug->leave_sub();
2462 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2463 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2467 $main::lxdebug->leave_sub();
2472 sub get_all_warehouses {
2473 $main::lxdebug->enter_sub();
2475 my ($self, $myconfig, $form) = @_;
2477 # connect to database
2478 my $dbh = $form->get_standard_dbh($myconfig);
2480 my $query = qq|SELECT w.id, w.description, w.invalid,
2481 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2483 ORDER BY w.sortkey|;
2485 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2487 $main::lxdebug->leave_sub();
2491 $main::lxdebug->enter_sub();
2493 my ($self, $myconfig, $form) = @_;
2495 # connect to database
2496 my $dbh = $form->get_standard_dbh($myconfig);
2498 my $id = conv_i($form->{id});
2499 my $query = qq|SELECT w.description, w.invalid
2503 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2505 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2507 $query = qq|SELECT b.*, EXISTS
2508 (SELECT i.warehouse_id
2510 WHERE i.bin_id = b.id
2514 WHERE b.warehouse_id = ?|;
2516 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2518 $main::lxdebug->leave_sub();