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 my $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) = @_;
504 # connect to database
505 my $dbh = $form->dbconnect($myconfig);
507 my @values = ($form->{description}, $form->{role});
509 $query = qq|UPDATE department SET
510 description = ?, role = ?
512 push(@values, $form->{id});
514 $query = qq|INSERT INTO department
518 do_query($form, $dbh, $query, @values);
522 $main::lxdebug->leave_sub();
525 sub delete_department {
526 $main::lxdebug->enter_sub();
528 my ($self, $myconfig, $form) = @_;
531 # connect to database
532 my $dbh = $form->dbconnect($myconfig);
534 $query = qq|DELETE FROM department
536 do_query($form, $dbh, $query, $form->{id});
540 $main::lxdebug->leave_sub();
544 $main::lxdebug->enter_sub();
546 my ($self, $myconfig, $form) = @_;
548 # connect to database
549 my $dbh = $form->dbconnect($myconfig);
551 my $query = qq|SELECT id, lead
555 my $sth = $dbh->prepare($query);
556 $sth->execute || $form->dberror($query);
558 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
559 push @{ $form->{ALL} }, $ref;
565 $main::lxdebug->leave_sub();
569 $main::lxdebug->enter_sub();
571 my ($self, $myconfig, $form) = @_;
573 # connect to database
574 my $dbh = $form->dbconnect($myconfig);
577 qq|SELECT l.id, l.lead | .
580 my $sth = $dbh->prepare($query);
581 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
583 my $ref = $sth->fetchrow_hashref("NAME_lc");
585 map { $form->{$_} = $ref->{$_} } keys %$ref;
591 $main::lxdebug->leave_sub();
595 $main::lxdebug->enter_sub();
597 my ($self, $myconfig, $form) = @_;
600 # connect to database
601 my $dbh = $form->dbconnect($myconfig);
603 my @values = ($form->{description});
604 # id is the old record
606 $query = qq|UPDATE leads SET
609 puhs(@values, $form->{id});
611 $query = qq|INSERT INTO leads
615 do_query($form, $dbh, $query, @values);
619 $main::lxdebug->leave_sub();
623 $main::lxdebug->enter_sub();
625 my ($self, $myconfig, $form) = @_;
628 # connect to database
629 my $dbh = $form->dbconnect($myconfig);
631 $query = qq|DELETE FROM leads
633 do_query($form, $dbh, $query, $form->{id});
637 $main::lxdebug->leave_sub();
641 $main::lxdebug->enter_sub();
643 my ($self, $myconfig, $form) = @_;
645 # connect to database
646 my $dbh = $form->dbconnect($myconfig);
648 my $query = qq|SELECT id, description, discount, customernumberinit
652 my $sth = $dbh->prepare($query);
653 $sth->execute || $form->dberror($query);
655 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
656 push @{ $form->{ALL} }, $ref;
662 $main::lxdebug->leave_sub();
666 $main::lxdebug->enter_sub();
668 my ($self, $myconfig, $form) = @_;
670 # connect to database
671 my $dbh = $form->dbconnect($myconfig);
674 qq|SELECT b.description, b.discount, b.customernumberinit
677 my $sth = $dbh->prepare($query);
678 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
680 my $ref = $sth->fetchrow_hashref("NAME_lc");
682 map { $form->{$_} = $ref->{$_} } keys %$ref;
688 $main::lxdebug->leave_sub();
692 $main::lxdebug->enter_sub();
694 my ($self, $myconfig, $form) = @_;
697 # connect to database
698 my $dbh = $form->dbconnect($myconfig);
700 my @values = ($form->{description}, $form->{discount},
701 $form->{customernumberinit});
702 # id is the old record
704 $query = qq|UPDATE business SET
707 customernumberinit = ?
709 push(@values, $form->{id});
711 $query = qq|INSERT INTO business
712 (description, discount, customernumberinit)
715 do_query($form, $dbh, $query, @values);
719 $main::lxdebug->leave_sub();
722 sub delete_business {
723 $main::lxdebug->enter_sub();
725 my ($self, $myconfig, $form) = @_;
727 # connect to database
728 my $dbh = $form->dbconnect($myconfig);
730 my $query = qq|DELETE FROM business
732 do_query($form, $dbh, $query, $form->{id});
736 $main::lxdebug->leave_sub();
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $form, $return_list) = @_;
745 # connect to database
746 my $dbh = $form->dbconnect($myconfig);
749 "SELECT id, description, template_code, article_code, " .
750 " output_numberformat, output_dateformat, output_longdates " .
751 "FROM language ORDER BY description";
753 my $sth = $dbh->prepare($query);
754 $sth->execute || $form->dberror($query);
758 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
759 push(@{ $ary }, $ref);
765 $main::lxdebug->leave_sub();
775 $main::lxdebug->enter_sub();
777 my ($self, $myconfig, $form) = @_;
779 # connect to database
780 my $dbh = $form->dbconnect($myconfig);
783 "SELECT description, template_code, article_code, " .
784 " output_numberformat, output_dateformat, output_longdates " .
785 "FROM language WHERE id = ?";
786 my $sth = $dbh->prepare($query);
787 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
789 my $ref = $sth->fetchrow_hashref("NAME_lc");
791 map { $form->{$_} = $ref->{$_} } keys %$ref;
797 $main::lxdebug->leave_sub();
800 sub get_language_details {
801 $main::lxdebug->enter_sub();
803 my ($self, $myconfig, $form, $id) = @_;
805 # connect to database
806 my $dbh = $form->dbconnect($myconfig);
809 "SELECT template_code, " .
810 " output_numberformat, output_dateformat, output_longdates " .
811 "FROM language WHERE id = ?";
812 my @res = selectrow_query($form, $dbh, $query, $id);
815 $main::lxdebug->leave_sub();
821 $main::lxdebug->enter_sub();
823 my ($self, $myconfig, $form) = @_;
825 # connect to database
826 my $dbh = $form->dbconnect($myconfig);
827 my (@values, $query);
829 map({ push(@values, $form->{$_}); }
830 qw(description template_code article_code
831 output_numberformat output_dateformat output_longdates));
833 # id is the old record
836 "UPDATE language SET " .
837 " description = ?, template_code = ?, article_code = ?, " .
838 " output_numberformat = ?, output_dateformat = ?, " .
839 " output_longdates = ? " .
841 push(@values, $form->{id});
844 "INSERT INTO language (" .
845 " description, template_code, article_code, " .
846 " output_numberformat, output_dateformat, output_longdates" .
847 ") VALUES (?, ?, ?, ?, ?, ?)";
849 do_query($form, $dbh, $query, @values);
853 $main::lxdebug->leave_sub();
856 sub delete_language {
857 $main::lxdebug->enter_sub();
859 my ($self, $myconfig, $form) = @_;
862 # connect to database
863 my $dbh = $form->dbconnect_noauto($myconfig);
865 foreach my $table (qw(translation_payment_terms units_language)) {
866 $query = qq|DELETE FROM $table WHERE language_id = ?|;
867 do_query($form, $dbh, $query, $form->{"id"});
870 $query = "DELETE FROM language WHERE id = ?";
871 do_query($form, $dbh, $query, $form->{"id"});
876 $main::lxdebug->leave_sub();
881 $main::lxdebug->enter_sub();
883 my ($self, $myconfig, $form) = @_;
885 # connect to database
886 my $dbh = $form->dbconnect($myconfig);
888 my $query = qq|SELECT id, description,
890 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
892 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
894 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
896 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
898 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
900 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
902 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
904 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
906 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
910 my $sth = $dbh->prepare($query);
911 $sth->execute || $form->dberror($query);
914 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
915 push @{ $form->{ALL} }, $ref;
921 $main::lxdebug->leave_sub();
924 sub get_buchungsgruppe {
925 $main::lxdebug->enter_sub();
927 my ($self, $myconfig, $form) = @_;
930 # connect to database
931 my $dbh = $form->dbconnect($myconfig);
935 qq|SELECT description, inventory_accno_id,
936 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
938 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
940 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
942 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
944 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
946 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
948 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
950 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
952 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
955 my $sth = $dbh->prepare($query);
956 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
958 my $ref = $sth->fetchrow_hashref("NAME_lc");
960 map { $form->{$_} = $ref->{$_} } keys %$ref;
965 qq|SELECT count(id) = 0 AS orphaned
967 WHERE buchungsgruppen_id = ?|;
968 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
971 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
973 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
974 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
977 $query = qq|SELECT c.accno, c.description, c.link, c.id,
978 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
979 FROM chart c, defaults d
980 WHERE c.link LIKE '%$module%'
984 my $sth = $dbh->prepare($query);
985 $sth->execute || $form->dberror($query);
986 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
987 foreach my $key (split(/:/, $ref->{link})) {
988 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
989 $form->{"std_inventory_accno_id"} = $ref->{"id"};
991 if ($key =~ /$module/) {
992 if ( ($ref->{id} eq $ref->{inventory_accno_id})
993 || ($ref->{id} eq $ref->{income_accno_id})
994 || ($ref->{id} eq $ref->{expense_accno_id})) {
995 push @{ $form->{"${module}_links"}{$key} },
996 { accno => $ref->{accno},
997 description => $ref->{description},
998 selected => "selected",
1001 push @{ $form->{"${module}_links"}{$key} },
1002 { accno => $ref->{accno},
1003 description => $ref->{description},
1015 $main::lxdebug->leave_sub();
1018 sub save_buchungsgruppe {
1019 $main::lxdebug->enter_sub();
1021 my ($self, $myconfig, $form) = @_;
1023 # connect to database
1024 my $dbh = $form->dbconnect($myconfig);
1026 my @values = ($form->{description}, $form->{inventory_accno_id},
1027 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1028 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1029 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1030 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1034 # id is the old record
1036 $query = qq|UPDATE buchungsgruppen SET
1037 description = ?, inventory_accno_id = ?,
1038 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1039 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1040 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1041 income_accno_id_3 = ?, expense_accno_id_3 = ?
1043 push(@values, $form->{id});
1045 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1046 my ($sortkey) = $dbh->selectrow_array($query);
1047 $form->dberror($query) if ($dbh->err);
1048 push(@values, $sortkey);
1049 $query = qq|INSERT INTO buchungsgruppen
1050 (description, inventory_accno_id,
1051 income_accno_id_0, expense_accno_id_0,
1052 income_accno_id_1, expense_accno_id_1,
1053 income_accno_id_2, expense_accno_id_2,
1054 income_accno_id_3, expense_accno_id_3,
1056 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1058 do_query($form, $dbh, $query, @values);
1062 $main::lxdebug->leave_sub();
1065 sub delete_buchungsgruppe {
1066 $main::lxdebug->enter_sub();
1068 my ($self, $myconfig, $form) = @_;
1070 # connect to database
1071 my $dbh = $form->dbconnect($myconfig);
1073 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1074 do_query($form, $dbh, $query, $form->{id});
1078 $main::lxdebug->leave_sub();
1082 $main::lxdebug->enter_sub();
1084 my ($self, $myconfig, $form, $table) = @_;
1086 # connect to database
1087 my $dbh = $form->get_standard_dbh($myconfig);
1091 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1092 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1093 my @values = ($form->{"id1"}, $form->{"id2"});
1094 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1096 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1097 my $sth = prepare_query($form, $dbh, $query);
1099 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1100 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1106 $main::lxdebug->leave_sub();
1110 $main::lxdebug->enter_sub();
1112 my ($self, $myconfig, $form) = @_;
1114 # connect to database
1115 my $dbh = $form->dbconnect($myconfig);
1117 my $query = qq|SELECT id, printer_description, template_code, printer_command
1121 my $sth = $dbh->prepare($query);
1122 $sth->execute || $form->dberror($query);
1124 $form->{"ALL"} = [];
1125 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1126 push @{ $form->{ALL} }, $ref;
1132 $main::lxdebug->leave_sub();
1136 $main::lxdebug->enter_sub();
1138 my ($self, $myconfig, $form) = @_;
1140 # connect to database
1141 my $dbh = $form->dbconnect($myconfig);
1144 qq|SELECT p.printer_description, p.template_code, p.printer_command
1147 my $sth = $dbh->prepare($query);
1148 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1150 my $ref = $sth->fetchrow_hashref("NAME_lc");
1152 map { $form->{$_} = $ref->{$_} } keys %$ref;
1158 $main::lxdebug->leave_sub();
1162 $main::lxdebug->enter_sub();
1164 my ($self, $myconfig, $form) = @_;
1167 # connect to database
1168 my $dbh = $form->dbconnect($myconfig);
1170 my @values = ($form->{printer_description},
1171 $form->{template_code},
1172 $form->{printer_command});
1174 # id is the old record
1176 $query = qq|UPDATE printers SET
1177 printer_description = ?, template_code = ?, printer_command = ?
1179 push(@values, $form->{id});
1181 $query = qq|INSERT INTO printers
1182 (printer_description, template_code, printer_command)
1185 do_query($form, $dbh, $query, @values);
1189 $main::lxdebug->leave_sub();
1192 sub delete_printer {
1193 $main::lxdebug->enter_sub();
1195 my ($self, $myconfig, $form) = @_;
1197 # connect to database
1198 my $dbh = $form->dbconnect($myconfig);
1200 my $query = qq|DELETE FROM printers
1202 do_query($form, $dbh, $query, $form->{id});
1206 $main::lxdebug->leave_sub();
1210 $main::lxdebug->enter_sub();
1212 my ($self, $myconfig, $form) = @_;
1214 # connect to database
1215 my $dbh = $form->dbconnect($myconfig);
1217 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1219 my $sth = $dbh->prepare($query);
1220 $sth->execute || $form->dberror($query);
1223 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1224 push @{ $form->{ALL} }, $ref;
1230 $main::lxdebug->leave_sub();
1234 $main::lxdebug->enter_sub();
1236 my ($self, $myconfig, $form) = @_;
1238 # connect to database
1239 my $dbh = $form->dbconnect($myconfig);
1241 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1242 my $sth = $dbh->prepare($query);
1243 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1245 my $ref = $sth->fetchrow_hashref("NAME_lc");
1246 map { $form->{$_} = $ref->{$_} } keys %$ref;
1250 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1251 qq|FROM translation_payment_terms t | .
1252 qq|LEFT JOIN language l ON t.language_id = l.id | .
1253 qq|WHERE t.payment_terms_id = ? | .
1255 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1256 qq| l.description AS language | .
1257 qq|FROM language l|;
1258 $sth = $dbh->prepare($query);
1259 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1262 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1263 $mapping{ $ref->{"language_id"} } = $ref
1264 unless (defined($mapping{ $ref->{"language_id"} }));
1268 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1273 $main::lxdebug->leave_sub();
1277 $main::lxdebug->enter_sub();
1279 my ($self, $myconfig, $form) = @_;
1281 # connect to database
1282 my $dbh = $form->dbconnect_noauto($myconfig);
1287 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1288 qq|FROM payment_terms|;
1290 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1292 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1293 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1297 qq|DELETE FROM translation_payment_terms | .
1298 qq|WHERE payment_terms_id = ?|;
1299 do_query($form, $dbh, $query, $form->{"id"});
1302 $query = qq|UPDATE payment_terms SET
1303 description = ?, description_long = ?,
1304 terms_netto = ?, terms_skonto = ?,
1307 my @values = ($form->{description}, $form->{description_long},
1308 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1309 $form->{percent_skonto} * 1,
1311 do_query($form, $dbh, $query, @values);
1313 $query = qq|SELECT id FROM language|;
1315 my $sth = $dbh->prepare($query);
1316 $sth->execute() || $form->dberror($query);
1318 while (my ($id) = $sth->fetchrow_array()) {
1319 push(@language_ids, $id);
1324 qq|INSERT INTO translation_payment_terms | .
1325 qq|(language_id, payment_terms_id, description_long) | .
1326 qq|VALUES (?, ?, ?)|;
1327 $sth = $dbh->prepare($query);
1329 foreach my $language_id (@language_ids) {
1330 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1331 $form->{"description_long_${language_id}"});
1338 $main::lxdebug->leave_sub();
1341 sub delete_payment {
1342 $main::lxdebug->enter_sub();
1344 my ($self, $myconfig, $form) = @_;
1346 # connect to database
1347 my $dbh = $form->dbconnect_noauto($myconfig);
1350 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1351 do_query($form, $dbh, $query, $form->{"id"});
1353 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1354 do_query($form, $dbh, $query, $form->{"id"});
1359 $main::lxdebug->leave_sub();
1363 sub prepare_template_filename {
1364 $main::lxdebug->enter_sub();
1366 my ($self, $myconfig, $form) = @_;
1368 my ($filename, $display_filename);
1370 if ($form->{type} eq "stylesheet") {
1371 $filename = "css/$myconfig->{stylesheet}";
1372 $display_filename = $myconfig->{stylesheet};
1375 $filename = $form->{formname};
1377 if ($form->{language}) {
1378 my ($id, $template_code) = split(/--/, $form->{language});
1379 $filename .= "_${template_code}";
1382 if ($form->{printer}) {
1383 my ($id, $template_code) = split(/--/, $form->{printer});
1384 $filename .= "_${template_code}";
1387 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1388 $filename =~ s|.*/||;
1389 $display_filename = $filename;
1390 $filename = "$myconfig->{templates}/$filename";
1393 $main::lxdebug->leave_sub();
1395 return ($filename, $display_filename);
1400 $main::lxdebug->enter_sub();
1402 my ($self, $filename) = @_;
1404 my ($content, $lines) = ("", 0);
1408 if (open(TEMPLATE, $filename)) {
1409 while (<TEMPLATE>) {
1416 $main::lxdebug->leave_sub();
1418 return ($content, $lines);
1422 $main::lxdebug->enter_sub();
1424 my ($self, $filename, $content) = @_;
1430 if (open(TEMPLATE, ">$filename")) {
1431 $content =~ s/\r\n/\n/g;
1432 print(TEMPLATE $content);
1438 $main::lxdebug->leave_sub();
1444 $main::lxdebug->enter_sub();
1449 my $myconfig = \%main::myconfig;
1450 my $form = $main::form;
1452 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1455 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1457 $form->{curr} =~ s/ //g;
1458 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1459 my $currency = join ':', @currencies;
1461 # these defaults are database wide
1464 qq|UPDATE defaults SET
1465 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1466 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1467 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1468 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1469 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1486 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1487 $accnos{fxgain_accno}, $accnos{fxloss_accno},
1488 $form->{invnumber}, $form->{cnnumber},
1489 $form->{sonumber}, $form->{ponumber},
1490 $form->{sqnumber}, $form->{rfqnumber},
1491 $form->{customernumber}, $form->{vendornumber},
1492 $form->{articlenumber}, $form->{servicenumber},
1493 $form->{sdonumber}, $form->{pdonumber},
1494 $form->{yearend}, $currency,
1495 $form->{businessnumber}, $form->{weightunit});
1496 do_query($form, $dbh, $query, @values);
1500 $main::lxdebug->leave_sub();
1504 sub save_preferences {
1505 $main::lxdebug->enter_sub();
1507 my ($self, $myconfig, $form, $webdav) = @_;
1509 my $dbh = $form->get_standard_dbh($myconfig);
1511 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1514 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1515 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1517 my $rc = $dbh->commit();
1519 # save first currency in myconfig
1520 $currency =~ s/:.*//;
1521 $form->{currency} = $currency;
1523 $form->{businessnumber} = $businessnumber;
1525 $myconfig = new User($form->{login});
1527 foreach my $item (keys %$form) {
1528 $myconfig->{$item} = $form->{$item};
1531 $myconfig->save_member($main::memberfile);
1533 my $auth = $main::auth;
1535 if ($auth->can_change_password()
1536 && defined $form->{new_password}
1537 && ($form->{new_password} ne '********')) {
1538 $auth->change_password($form->{login}, $form->{new_password});
1540 $form->{password} = $form->{new_password};
1541 $auth->set_session_value('password', $form->{password});
1542 $auth->create_or_refresh_session();
1547 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1548 foreach my $directory (@webdavdirs) {
1549 my $file = "webdav/" . $directory . "/webdav-user";
1551 if ($myconfig->{$directory}) {
1552 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1553 while (<HTACCESS>) {
1554 my ($login, $password) = split(/:/, $_);
1555 if ($login ne $form->{login}) {
1560 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1561 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1562 print(HTACCESS $newfile);
1565 $form->{$directory} = 0;
1566 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1567 while (<HTACCESS>) {
1568 my ($login, $password) = split(/:/, $_);
1569 if ($login ne $form->{login}) {
1574 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1575 print(HTACCESS $newfile);
1581 $main::lxdebug->leave_sub();
1587 $main::lxdebug->enter_sub();
1592 my $myconfig = \%main::myconfig;
1593 my $form = $main::form;
1595 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1597 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1599 $defaults->{weightunit} ||= 'kg';
1601 $main::lxdebug->leave_sub();
1606 sub defaultaccounts {
1607 $main::lxdebug->enter_sub();
1609 my ($self, $myconfig, $form) = @_;
1611 # connect to database
1612 my $dbh = $form->dbconnect($myconfig);
1614 # get defaults from defaults table
1615 my $query = qq|SELECT * FROM defaults|;
1616 my $sth = $dbh->prepare($query);
1617 $sth->execute || $form->dberror($query);
1619 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1620 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1621 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1622 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1623 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1624 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1626 $form->{defaults}{weightunit} ||= 'kg';
1630 $query = qq|SELECT c.id, c.accno, c.description, c.link
1632 WHERE c.link LIKE '%IC%'
1634 $sth = $dbh->prepare($query);
1635 $sth->execute || $self->dberror($query);
1637 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1638 foreach my $key (split(/:/, $ref->{link})) {
1641 if ($key =~ /cogs/) {
1642 $nkey = "IC_expense";
1644 if ($key =~ /sale/) {
1645 $nkey = "IC_income";
1647 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1649 description => $ref->{description}
1656 $query = qq|SELECT c.id, c.accno, c.description
1658 WHERE c.category = 'I'
1659 AND c.charttype = 'A'
1661 $sth = $dbh->prepare($query);
1662 $sth->execute || $self->dberror($query);
1664 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1665 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1667 description => $ref->{description}
1672 $query = qq|SELECT c.id, c.accno, c.description
1674 WHERE c.category = 'E'
1675 AND c.charttype = 'A'
1677 $sth = $dbh->prepare($query);
1678 $sth->execute || $self->dberror($query);
1680 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1681 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1683 description => $ref->{description}
1688 # now get the tax rates and numbers
1689 $query = qq|SELECT c.id, c.accno, c.description,
1690 t.rate * 100 AS rate, t.taxnumber
1692 WHERE c.id = t.chart_id|;
1694 $sth = $dbh->prepare($query);
1695 $sth->execute || $form->dberror($query);
1697 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1698 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1699 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1700 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1701 if $ref->{taxnumber};
1702 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1708 $main::lxdebug->leave_sub();
1712 $main::lxdebug->enter_sub();
1714 my ($self, $myconfig, $form) = @_;
1716 my $dbh = $form->dbconnect($myconfig);
1718 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1719 my $sth = $dbh->prepare($query);
1720 $sth->execute || $form->dberror($query);
1722 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1728 $main::lxdebug->leave_sub();
1732 $main::lxdebug->enter_sub();
1734 my ($self, $myconfig, $form) = @_;
1736 my $dbh = $form->dbconnect($myconfig);
1738 my ($query, @values);
1740 if ($form->{revtrans}) {
1741 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1743 } elsif ($form->{closedto}) {
1744 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1745 @values = (conv_date($form->{closedto}));
1748 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1751 # set close in defaults
1752 do_query($form, $dbh, $query, @values);
1756 $main::lxdebug->leave_sub();
1760 my ($self, $units, $unit_name, $factor) = @_;
1762 $factor = 1 unless ($factor);
1764 my $unit = $units->{$unit_name};
1766 if (!defined($unit) || !$unit->{"base_unit"} ||
1767 ($unit_name eq $unit->{"base_unit"})) {
1768 return ($unit_name, $factor);
1771 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1774 sub retrieve_units {
1775 $main::lxdebug->enter_sub();
1777 my ($self, $myconfig, $form, $prefix) = @_;
1779 my $dbh = $form->dbconnect($myconfig);
1781 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1783 my $sth = prepare_execute_query($form, $dbh, $query);
1786 while (my $ref = $sth->fetchrow_hashref()) {
1787 $units->{$ref->{"name"}} = $ref;
1791 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1792 $sth = $dbh->prepare($query_lang);
1793 $sth->execute() || $form->dberror($query_lang);
1795 while (my $ref = $sth->fetchrow_hashref()) {
1796 push(@languages, $ref);
1800 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1801 "FROM units_language ul " .
1802 "LEFT JOIN language l ON ul.language_id = l.id " .
1803 "WHERE ul.unit = ?";
1804 $sth = $dbh->prepare($query_lang);
1806 foreach my $unit (values(%{$units})) {
1807 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1809 $unit->{"LANGUAGES"} = {};
1810 foreach my $lang (@languages) {
1811 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1814 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1815 while (my $ref = $sth->fetchrow_hashref()) {
1816 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1823 $main::lxdebug->leave_sub();
1828 sub retrieve_all_units {
1829 $main::lxdebug->enter_sub();
1833 if (!$main::all_units) {
1834 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1837 $main::lxdebug->leave_sub();
1839 return $main::all_units;
1843 sub translate_units {
1844 $main::lxdebug->enter_sub();
1846 my ($self, $form, $template_code, $unit, $amount) = @_;
1848 my $units = $self->retrieve_units(\%main::myconfig, $form);
1850 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1851 my $new_unit = $unit;
1853 if (($amount != 1) && $h->{"localized_plural"}) {
1854 $new_unit = $h->{"localized_plural"};
1855 } elsif ($h->{"localized"}) {
1856 $new_unit = $h->{"localized"};
1860 $main::lxdebug->leave_sub();
1866 $main::lxdebug->enter_sub();
1868 my ($self, $myconfig, $form, $units) = @_;
1870 my $dbh = $form->dbconnect($myconfig);
1872 map({ $_->{"in_use"} = 0; } values(%{$units}));
1874 foreach my $unit (values(%{$units})) {
1875 my $base_unit = $unit->{"original_base_unit"};
1876 while ($base_unit) {
1877 $units->{$base_unit}->{"in_use"} = 1;
1878 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1879 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1880 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1884 foreach my $unit (values(%{$units})) {
1885 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1887 foreach my $table (qw(parts invoice orderitems)) {
1888 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1890 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1891 $query .= "= " . $dbh->quote($unit->{"name"});
1893 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1894 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1897 my ($count) = $dbh->selectrow_array($query);
1898 $form->dberror($query) if ($dbh->err);
1901 $unit->{"in_use"} = 1;
1909 $main::lxdebug->leave_sub();
1912 sub convertible_units {
1913 $main::lxdebug->enter_sub();
1917 my $filter_unit = shift;
1918 my $not_smaller = shift;
1920 my $conv_units = [];
1922 $filter_unit = $units->{$filter_unit};
1924 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1925 my $unit = $units->{$name};
1927 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1928 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1929 push @{$conv_units}, $unit;
1933 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1935 $main::lxdebug->leave_sub();
1940 # if $a is translatable to $b, return the factor between them.
1943 $main::lxdebug->enter_sub(2);
1944 my ($this, $a, $b, $all_units) = @_;
1946 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1947 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1948 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1949 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1952 sub unit_select_data {
1953 $main::lxdebug->enter_sub();
1955 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1960 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1963 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1964 if (!$convertible_into ||
1965 ($units->{$convertible_into} &&
1966 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1967 push @{$select}, { "name" => $unit,
1968 "base_unit" => $units->{$unit}->{"base_unit"},
1969 "factor" => $units->{$unit}->{"factor"},
1970 "selected" => ($unit eq $selected) ? "selected" : "" };
1974 $main::lxdebug->leave_sub();
1979 sub unit_select_html {
1980 $main::lxdebug->enter_sub();
1982 my ($self, $units, $name, $selected, $convertible_into) = @_;
1984 my $select = "<select name=${name}>";
1986 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1987 if (!$convertible_into ||
1988 ($units->{$convertible_into} &&
1989 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1990 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1993 $select .= "</select>";
1995 $main::lxdebug->leave_sub();
2001 $main::lxdebug->enter_sub();
2005 my $units = $self->retrieve_all_units();
2010 while (2 <= scalar(@_)) {
2011 my $qty = shift(@_);
2012 my $unit = $units->{shift(@_)};
2014 croak "No unit defined with name $unit" if (!defined $unit);
2017 $base_unit = $unit->{base_unit};
2018 } elsif ($base_unit ne $unit->{base_unit}) {
2019 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
2022 $sum += $qty * $unit->{factor};
2025 $main::lxdebug->leave_sub();
2027 return wantarray ? ($sum, $base_unit) : $sum;
2031 $main::lxdebug->enter_sub();
2033 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
2035 my $dbh = $form->dbconnect_noauto($myconfig);
2037 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2038 my ($sortkey) = selectrow_query($form, $dbh, $query);
2040 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
2041 "VALUES (?, ?, ?, ?)";
2042 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
2045 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2046 my $sth = $dbh->prepare($query);
2047 foreach my $lang (@{$languages}) {
2048 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2049 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2057 $main::lxdebug->leave_sub();
2061 $main::lxdebug->enter_sub();
2063 my ($self, $myconfig, $form, $units, $delete_units) = @_;
2065 my $dbh = $form->dbconnect_noauto($myconfig);
2067 my ($base_unit, $unit, $sth, $query);
2069 $query = "DELETE FROM units_language";
2070 $dbh->do($query) || $form->dberror($query);
2072 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2073 $query = "DELETE FROM units WHERE name IN (";
2074 map({ $query .= "?," } @{$delete_units});
2075 substr($query, -1, 1) = ")";
2076 $dbh->do($query, undef, @{$delete_units}) ||
2077 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2080 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2081 $sth = $dbh->prepare($query);
2083 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2084 my $sth_lang = $dbh->prepare($query_lang);
2086 foreach $unit (values(%{$units})) {
2087 $unit->{"depth"} = 0;
2088 my $base_unit = $unit;
2089 while ($base_unit->{"base_unit"}) {
2091 $base_unit = $units->{$base_unit->{"base_unit"}};
2095 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2096 if ($unit->{"LANGUAGES"}) {
2097 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2098 next unless ($lang->{"id"} && $lang->{"localized"});
2099 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2100 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2104 next if ($unit->{"unchanged_unit"});
2106 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2107 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2111 $sth_lang->finish();
2115 $main::lxdebug->leave_sub();
2119 $main::lxdebug->enter_sub();
2121 my ($self, $myconfig, $form, $dir, $name_1) = @_;
2123 my $dbh = $form->dbconnect_noauto($myconfig);
2127 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2128 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2131 qq|SELECT sortkey FROM units | .
2132 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
2133 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2134 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
2136 if (defined($sortkey_1)) {
2137 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2138 my ($name_2) = selectrow_query($form, $dbh, $query);
2140 if (defined($name_2)) {
2141 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2142 my $sth = $dbh->prepare($query);
2144 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2145 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2152 $main::lxdebug->leave_sub();
2156 $main::lxdebug->enter_sub();
2158 my ($self, $myconfig, $form) = @_;
2160 # connect to database
2161 my $dbh = $form->dbconnect($myconfig);
2163 my $query = qq|SELECT
2167 round(t.rate * 100, 2) AS rate,
2168 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2169 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2173 my $sth = $dbh->prepare($query);
2174 $sth->execute || $form->dberror($query);
2177 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2178 push @{ $form->{TAX} }, $ref;
2184 $main::lxdebug->leave_sub();
2187 sub get_tax_accounts {
2188 $main::lxdebug->enter_sub();
2190 my ($self, $myconfig, $form) = @_;
2192 my $dbh = $form->dbconnect($myconfig);
2194 # get Accounts from chart
2195 my $query = qq{ SELECT
2197 accno || ' - ' || description AS taxaccount
2199 WHERE link LIKE '%_tax%'
2203 my $sth = $dbh->prepare($query);
2204 $sth->execute || $form->dberror($query);
2206 $form->{ACCOUNTS} = [];
2207 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2208 push @{ $form->{ACCOUNTS} }, $ref;
2215 $main::lxdebug->leave_sub();
2219 $main::lxdebug->enter_sub();
2221 my ($self, $myconfig, $form) = @_;
2223 # connect to database
2224 my $dbh = $form->dbconnect($myconfig);
2226 my $query = qq|SELECT
2229 round(rate * 100, 2) AS rate,
2234 my $sth = $dbh->prepare($query);
2235 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2237 my $ref = $sth->fetchrow_hashref("NAME_lc");
2239 map { $form->{$_} = $ref->{$_} } keys %$ref;
2243 # see if it is used by a taxkey
2244 $query = qq|SELECT count(*) FROM taxkeys
2245 WHERE tax_id = ? AND chart_id >0|;
2247 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2249 $form->{orphaned} = !$form->{orphaned};
2252 if (!$form->{orphaned} ) {
2253 $query = qq|SELECT DISTINCT c.id, c.accno
2255 JOIN tax t ON (t.id = tk.tax_id)
2256 JOIN chart c ON (c.id = tk.chart_id)
2257 WHERE tk.tax_id = ?|;
2259 $sth = $dbh->prepare($query);
2260 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2262 $form->{TAXINUSE} = [];
2263 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2264 push @{ $form->{TAXINUSE} }, $ref;
2272 $main::lxdebug->leave_sub();
2276 $main::lxdebug->enter_sub();
2278 my ($self, $myconfig, $form) = @_;
2281 # connect to database
2282 my $dbh = $form->get_standard_dbh($myconfig);
2284 $form->{rate} = $form->{rate} / 100;
2286 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2287 if ($form->{id} ne "") {
2288 $query = qq|UPDATE tax SET
2293 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2295 push(@values, $form->{id});
2299 $query = qq|INSERT INTO tax (
2306 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2308 do_query($form, $dbh, $query, @values);
2312 $main::lxdebug->leave_sub();
2316 $main::lxdebug->enter_sub();
2318 my ($self, $myconfig, $form) = @_;
2321 # connect to database
2322 my $dbh = $form->get_standard_dbh($myconfig);
2324 $query = qq|DELETE FROM tax
2326 do_query($form, $dbh, $query, $form->{id});
2330 $main::lxdebug->leave_sub();
2333 sub save_price_factor {
2334 $main::lxdebug->enter_sub();
2336 my ($self, $myconfig, $form) = @_;
2338 # connect to database
2339 my $dbh = $form->get_standard_dbh($myconfig);
2342 my @values = ($form->{description}, conv_i($form->{factor}));
2345 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2346 push @values, conv_i($form->{id});
2349 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2352 do_query($form, $dbh, $query, @values);
2356 $main::lxdebug->leave_sub();
2359 sub get_all_price_factors {
2360 $main::lxdebug->enter_sub();
2362 my ($self, $myconfig, $form) = @_;
2364 # connect to database
2365 my $dbh = $form->get_standard_dbh($myconfig);
2367 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2369 $main::lxdebug->leave_sub();
2372 sub get_price_factor {
2373 $main::lxdebug->enter_sub();
2375 my ($self, $myconfig, $form) = @_;
2377 # connect to database
2378 my $dbh = $form->get_standard_dbh($myconfig);
2380 my $query = qq|SELECT description, factor,
2381 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2382 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2383 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2384 FROM price_factors WHERE id = ?|;
2386 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2388 $main::lxdebug->leave_sub();
2391 sub delete_price_factor {
2392 $main::lxdebug->enter_sub();
2394 my ($self, $myconfig, $form) = @_;
2396 # connect to database
2397 my $dbh = $form->get_standard_dbh($myconfig);
2399 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2402 $main::lxdebug->leave_sub();
2405 sub save_warehouse {
2406 $main::lxdebug->enter_sub();
2408 my ($self, $myconfig, $form) = @_;
2410 # connect to database
2411 my $dbh = $form->get_standard_dbh($myconfig);
2413 my ($query, @values, $sth);
2416 $query = qq|SELECT nextval('id')|;
2417 ($form->{id}) = selectrow_query($form, $dbh, $query);
2419 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2420 do_query($form, $dbh, $query, $form->{id});
2423 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2424 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2426 if (0 < $form->{number_of_new_bins}) {
2427 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2428 $sth = prepare_query($form, $dbh, $query);
2430 foreach my $i (1..$form->{number_of_new_bins}) {
2431 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2439 $main::lxdebug->leave_sub();
2443 $main::lxdebug->enter_sub();
2445 my ($self, $myconfig, $form) = @_;
2447 # connect to database
2448 my $dbh = $form->get_standard_dbh($myconfig);
2450 my ($query, @values, $commit_necessary, $sth);
2452 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2455 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2456 do_query($form, $dbh, $query, @values);
2458 $commit_necessary = 1;
2461 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2462 $sth = prepare_query($form, $dbh, $query);
2464 foreach my $row (1..$form->{rowcount}) {
2465 next if ($form->{"delete_${row}"});
2467 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2469 $commit_necessary = 1;
2474 $dbh->commit() if ($commit_necessary);
2476 $main::lxdebug->leave_sub();
2479 sub delete_warehouse {
2480 $main::lxdebug->enter_sub();
2482 my ($self, $myconfig, $form) = @_;
2484 # connect to database
2485 my $dbh = $form->get_standard_dbh($myconfig);
2487 my $id = conv_i($form->{id});
2488 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|;
2489 my ($count) = selectrow_query($form, $dbh, $query, $id);
2492 $main::lxdebug->leave_sub();
2496 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2497 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2501 $main::lxdebug->leave_sub();
2506 sub get_all_warehouses {
2507 $main::lxdebug->enter_sub();
2509 my ($self, $myconfig, $form) = @_;
2511 # connect to database
2512 my $dbh = $form->get_standard_dbh($myconfig);
2514 my $query = qq|SELECT w.id, w.description, w.invalid,
2515 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2517 ORDER BY w.sortkey|;
2519 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2521 $main::lxdebug->leave_sub();
2525 $main::lxdebug->enter_sub();
2527 my ($self, $myconfig, $form) = @_;
2529 # connect to database
2530 my $dbh = $form->get_standard_dbh($myconfig);
2532 my $id = conv_i($form->{id});
2533 my $query = qq|SELECT w.description, w.invalid
2537 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2539 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2541 $query = qq|SELECT b.*, EXISTS
2542 (SELECT i.warehouse_id
2544 WHERE i.bin_id = b.id
2548 WHERE b.warehouse_id = ?|;
2550 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2552 $main::lxdebug->leave_sub();