1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Administration module
36 #======================================================================
48 $main::lxdebug->enter_sub();
50 my ($self, $myconfig, $form) = @_;
53 my $dbh = $form->dbconnect($myconfig);
55 SELECT c.accno, c.description, c.charttype, c.category,
56 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
57 c.pos_bwa, datevautomatik,
58 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
59 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
62 ON (c.id=tk.chart_id AND tk.id =
63 (SELECT id FROM taxkeys
64 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
65 ORDER BY startdate DESC LIMIT 1))
70 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
71 my $sth = $dbh->prepare($query);
72 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
74 my $ref = $sth->fetchrow_hashref("NAME_lc");
76 foreach my $key (keys %$ref) {
77 $form->{"$key"} = $ref->{"$key"};
82 # get default accounts
83 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
85 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
86 $sth = $dbh->prepare($query);
87 $sth->execute || $form->dberror($query);
89 $ref = $sth->fetchrow_hashref("NAME_lc");
91 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
97 # get taxkeys and description
101 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
103 id||'--'||taxkey AS tax,
106 FROM tax ORDER BY taxkey
108 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
109 $sth = $dbh->prepare($query);
110 $sth->execute || $form->dberror($query);
112 $form->{TAXKEY} = [];
114 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
115 push @{ $form->{TAXKEY} }, $ref;
121 $query = qq|SELECT id, accno,description
125 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
126 $sth = $dbh->prepare($query);
127 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
129 $form->{NEWACCOUNT} = [];
130 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
131 push @{ $form->{NEWACCOUNT} }, $ref;
136 # get the taxkeys of account
150 LEFT JOIN tax t ON (t.id = tk.tax_id)
151 LEFT JOIN chart c ON (c.id = t.chart_id)
153 WHERE tk.chart_id = ?
154 ORDER BY startdate DESC
156 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
157 $sth = $dbh->prepare($query);
159 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
161 $form->{ACCOUNT_TAXKEYS} = [];
163 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
170 # check if we have any transactions
171 $query = qq|SELECT a.trans_id FROM acc_trans a
172 WHERE a.chart_id = ?|;
173 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
174 $sth = $dbh->prepare($query);
175 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
177 ($form->{orphaned}) = $sth->fetchrow_array;
178 $form->{orphaned} = !$form->{orphaned};
181 # check if new account is active
182 $form->{new_chart_valid} = 0;
183 if ($form->{new_chart_id}) {
184 $query = qq|SELECT current_date-valid_from FROM chart
186 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
187 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
189 $form->{new_chart_valid} = 1;
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 # TODO: it should be forbidden to change an account to a heading if there
203 # have been bookings to this account in the past
205 my ($self, $myconfig, $form) = @_;
207 # connect to database, turn off AutoCommit
208 my $dbh = $form->dbconnect_noauto($myconfig);
210 # sanity check, can't have AR with AR_...
211 if ($form->{AR} || $form->{AP} || $form->{IC}) {
212 map { delete $form->{$_} }
213 qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice CT_tax);
217 foreach my $item ($form->{AR}, $form->{AR_amount},
218 $form->{AR_tax}, $form->{AR_paid},
219 $form->{AP}, $form->{AP_amount},
220 $form->{AP_tax}, $form->{AP_paid},
221 $form->{IC}, $form->{IC_sale},
222 $form->{IC_cogs}, $form->{IC_taxpart},
223 $form->{IC_income}, $form->{IC_expense},
224 $form->{IC_taxservice}, $form->{CT_tax}
226 $form->{link} .= "${item}:" if ($item);
230 # strip blanks from accno
231 map { $form->{$_} =~ s/ //g; } qw(accno);
235 if ($form->{id} eq "NULL") {
239 if (!$form->{id} || $form->{id} eq "") {
240 $query = qq|SELECT nextval('id')|;
241 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
242 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
243 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
251 # if charttype is heading make sure certain values are empty
252 # specifically, if charttype is changed from an existing account, empty the
253 # fields unnecessary for headings, so that e.g. heading doesn't appear in
254 # drop-down menues due to still having a valid "link" entry
256 if ( $form->{charttype} eq 'H' ) {
258 $form->{pos_bwa} = '';
259 $form->{pos_bilanz} = '';
260 $form->{pos_eur} = '';
261 $form->{new_chart_id} = '';
262 $form->{valid_from} = '';
265 $query = qq|UPDATE chart SET
281 $form->{description},
285 conv_i($form->{pos_bwa}),
286 conv_i($form->{pos_bilanz}),
287 conv_i($form->{pos_eur}),
288 conv_i($form->{new_chart_id}),
289 conv_date($form->{valid_from}),
290 ($form->{datevautomatik} eq 'T') ? 'true':'false',
297 do_query($form, $dbh, $query, @values);
303 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
307 for $tk_count (0 .. $MAX_TRIES) {
311 # Check if the account already exists, else cancel
313 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
315 last READTAXKEYS if ( $form->{'id'} == 0);
317 # check if there is a startdate
318 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
323 # Add valid taxkeys into the array
326 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
327 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
328 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
329 chart_id => conv_i($form->{"id"}),
330 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
331 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
338 for my $j (0 .. $#taxkeys){
339 if ( defined $taxkeys[$j]{'id'} ){
342 if ($taxkeys[$j]{'delete'}){
344 DELETE FROM taxkeys WHERE id = ?
347 @values = ($taxkeys[$j]{'id'});
349 do_query($form, $dbh, $query, @values);
358 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
366 $taxkeys[$j]{'tax_id'},
367 $taxkeys[$j]{'chart_id'},
368 $taxkeys[$j]{'tax_id'},
369 $taxkeys[$j]{'pos_ustva'},
370 $taxkeys[$j]{'startdate'},
373 do_query($form, $dbh, $query, @values);
379 INSERT INTO taxkeys (
386 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
389 $taxkeys[$j]{'tax_id'},
390 $taxkeys[$j]{'chart_id'},
391 $taxkeys[$j]{'tax_id'},
392 $taxkeys[$j]{'pos_ustva'},
393 $taxkeys[$j]{'startdate'},
396 do_query($form, $dbh, $query, @values);
401 # Update chart.taxkey_id to the latest from taxkeys for this chart.
407 WHERE taxkeys.chart_id = chart.id
408 ORDER BY startdate DESC
414 do_query($form, $dbh, $query, $form->{id});
417 my $rc = $dbh->commit;
420 $main::lxdebug->leave_sub();
426 $main::lxdebug->enter_sub();
428 my ($self, $myconfig, $form) = @_;
430 # connect to database, turn off AutoCommit
431 my $dbh = $form->dbconnect_noauto($myconfig);
433 my $query = qq|SELECT count(*) FROM acc_trans a
434 WHERE a.chart_id = ?|;
435 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
439 $main::lxdebug->leave_sub();
443 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
444 foreach my $type (qw(inventory income expense)) {
447 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
448 qq|WHERE ${type}_accno_id = ?|;
449 do_query($form, $dbh, $query, $form->{id});
452 foreach my $table (qw(partstax customertax vendortax tax)) {
453 $query = qq|DELETE FROM $table
455 do_query($form, $dbh, $query, $form->{id});
458 # delete chart of account record
459 $query = qq|DELETE FROM chart
461 do_query($form, $dbh, $query, $form->{id});
463 # delete account taxkeys
464 $query = qq|DELETE FROM taxkeys
466 do_query($form, $dbh, $query, $form->{id});
468 # commit and redirect
469 my $rc = $dbh->commit;
472 $main::lxdebug->leave_sub();
478 $main::lxdebug->enter_sub();
480 my ($self, $myconfig, $form) = @_;
482 # connect to database
483 my $dbh = $form->dbconnect($myconfig);
485 my $query = qq|SELECT d.id, d.description, d.role
489 my $sth = $dbh->prepare($query);
490 $sth->execute || $form->dberror($query);
493 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
494 push @{ $form->{ALL} }, $ref;
500 $main::lxdebug->leave_sub();
504 $main::lxdebug->enter_sub();
506 my ($self, $myconfig, $form) = @_;
508 # connect to database
509 my $dbh = $form->dbconnect($myconfig);
511 my $query = qq|SELECT d.description, d.role
514 my $sth = $dbh->prepare($query);
515 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
517 my $ref = $sth->fetchrow_hashref("NAME_lc");
519 map { $form->{$_} = $ref->{$_} } keys %$ref;
523 # see if it is in use
524 $query = qq|SELECT count(*) FROM dpt_trans d
525 WHERE d.department_id = ?|;
526 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
528 $form->{orphaned} = !$form->{orphaned};
533 $main::lxdebug->leave_sub();
536 sub save_department {
537 $main::lxdebug->enter_sub();
539 my ($self, $myconfig, $form) = @_;
542 # connect to database
543 my $dbh = $form->dbconnect($myconfig);
545 my @values = ($form->{description}, $form->{role});
547 $query = qq|UPDATE department SET
548 description = ?, role = ?
550 push(@values, $form->{id});
552 $query = qq|INSERT INTO department
556 do_query($form, $dbh, $query, @values);
560 $main::lxdebug->leave_sub();
563 sub delete_department {
564 $main::lxdebug->enter_sub();
566 my ($self, $myconfig, $form) = @_;
569 # connect to database
570 my $dbh = $form->dbconnect($myconfig);
572 $query = qq|DELETE FROM department
574 do_query($form, $dbh, $query, $form->{id});
578 $main::lxdebug->leave_sub();
582 $main::lxdebug->enter_sub();
584 my ($self, $myconfig, $form) = @_;
586 # connect to database
587 my $dbh = $form->dbconnect($myconfig);
589 my $query = qq|SELECT id, lead
593 my $sth = $dbh->prepare($query);
594 $sth->execute || $form->dberror($query);
596 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
597 push @{ $form->{ALL} }, $ref;
603 $main::lxdebug->leave_sub();
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form) = @_;
611 # connect to database
612 my $dbh = $form->dbconnect($myconfig);
615 qq|SELECT l.id, l.lead | .
618 my $sth = $dbh->prepare($query);
619 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
621 my $ref = $sth->fetchrow_hashref("NAME_lc");
623 map { $form->{$_} = $ref->{$_} } keys %$ref;
629 $main::lxdebug->leave_sub();
633 $main::lxdebug->enter_sub();
635 my ($self, $myconfig, $form) = @_;
638 # connect to database
639 my $dbh = $form->dbconnect($myconfig);
641 my @values = ($form->{description});
642 # id is the old record
644 $query = qq|UPDATE leads SET
647 puhs(@values, $form->{id});
649 $query = qq|INSERT INTO leads
653 do_query($form, $dbh, $query, @values);
657 $main::lxdebug->leave_sub();
661 $main::lxdebug->enter_sub();
663 my ($self, $myconfig, $form) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
669 $query = qq|DELETE FROM leads
671 do_query($form, $dbh, $query, $form->{id});
675 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ($self, $myconfig, $form) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
686 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
690 my $sth = $dbh->prepare($query);
691 $sth->execute || $form->dberror($query);
693 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
694 push @{ $form->{ALL} }, $ref;
700 $main::lxdebug->leave_sub();
704 $main::lxdebug->enter_sub();
706 my ($self, $myconfig, $form) = @_;
708 # connect to database
709 my $dbh = $form->dbconnect($myconfig);
712 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
715 my $sth = $dbh->prepare($query);
716 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
718 my $ref = $sth->fetchrow_hashref("NAME_lc");
720 map { $form->{$_} = $ref->{$_} } keys %$ref;
726 $main::lxdebug->leave_sub();
730 $main::lxdebug->enter_sub();
732 my ($self, $myconfig, $form) = @_;
735 # connect to database
736 my $dbh = $form->dbconnect($myconfig);
738 my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
739 # id is the old record
741 $query = qq|UPDATE business SET
744 customernumberinit = ?,
747 push(@values, $form->{id});
749 $query = qq|INSERT INTO business
750 (description, discount, customernumberinit, salesman)
751 VALUES (?, ?, ?, ?)|;
753 do_query($form, $dbh, $query, @values);
757 $main::lxdebug->leave_sub();
760 sub delete_business {
761 $main::lxdebug->enter_sub();
763 my ($self, $myconfig, $form) = @_;
765 # connect to database
766 my $dbh = $form->dbconnect($myconfig);
768 my $query = qq|DELETE FROM business
770 do_query($form, $dbh, $query, $form->{id});
774 $main::lxdebug->leave_sub();
779 $main::lxdebug->enter_sub();
781 my ($self, $myconfig, $form, $return_list) = @_;
783 # connect to database
784 my $dbh = $form->dbconnect($myconfig);
787 "SELECT id, description, template_code, article_code, " .
788 " output_numberformat, output_dateformat, output_longdates " .
789 "FROM language ORDER BY description";
791 my $sth = $dbh->prepare($query);
792 $sth->execute || $form->dberror($query);
796 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
797 push(@{ $ary }, $ref);
803 $main::lxdebug->leave_sub();
813 $main::lxdebug->enter_sub();
815 my ($self, $myconfig, $form) = @_;
817 # connect to database
818 my $dbh = $form->dbconnect($myconfig);
821 "SELECT description, template_code, article_code, " .
822 " output_numberformat, output_dateformat, output_longdates " .
823 "FROM language WHERE id = ?";
824 my $sth = $dbh->prepare($query);
825 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
827 my $ref = $sth->fetchrow_hashref("NAME_lc");
829 map { $form->{$_} = $ref->{$_} } keys %$ref;
835 $main::lxdebug->leave_sub();
838 sub get_language_details {
839 $main::lxdebug->enter_sub();
841 my ($self, $myconfig, $form, $id) = @_;
843 # connect to database
844 my $dbh = $form->dbconnect($myconfig);
847 "SELECT template_code, " .
848 " output_numberformat, output_dateformat, output_longdates " .
849 "FROM language WHERE id = ?";
850 my @res = selectrow_query($form, $dbh, $query, $id);
853 $main::lxdebug->leave_sub();
859 $main::lxdebug->enter_sub();
861 my ($self, $myconfig, $form) = @_;
863 # connect to database
864 my $dbh = $form->dbconnect($myconfig);
865 my (@values, $query);
867 map({ push(@values, $form->{$_}); }
868 qw(description template_code article_code
869 output_numberformat output_dateformat output_longdates));
871 # id is the old record
874 "UPDATE language SET " .
875 " description = ?, template_code = ?, article_code = ?, " .
876 " output_numberformat = ?, output_dateformat = ?, " .
877 " output_longdates = ? " .
879 push(@values, $form->{id});
882 "INSERT INTO language (" .
883 " description, template_code, article_code, " .
884 " output_numberformat, output_dateformat, output_longdates" .
885 ") VALUES (?, ?, ?, ?, ?, ?)";
887 do_query($form, $dbh, $query, @values);
891 $main::lxdebug->leave_sub();
894 sub delete_language {
895 $main::lxdebug->enter_sub();
897 my ($self, $myconfig, $form) = @_;
900 # connect to database
901 my $dbh = $form->dbconnect_noauto($myconfig);
903 foreach my $table (qw(translation_payment_terms units_language)) {
904 $query = qq|DELETE FROM $table WHERE language_id = ?|;
905 do_query($form, $dbh, $query, $form->{"id"});
908 $query = "DELETE FROM language WHERE id = ?";
909 do_query($form, $dbh, $query, $form->{"id"});
914 $main::lxdebug->leave_sub();
919 $main::lxdebug->enter_sub();
921 my ($self, $myconfig, $form) = @_;
923 # connect to database
924 my $dbh = $form->dbconnect($myconfig);
926 my $query = qq|SELECT id, description,
928 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
930 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
932 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
934 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
936 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
938 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
940 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
942 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
944 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
948 my $sth = $dbh->prepare($query);
949 $sth->execute || $form->dberror($query);
952 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
953 push @{ $form->{ALL} }, $ref;
959 $main::lxdebug->leave_sub();
962 sub get_buchungsgruppe {
963 $main::lxdebug->enter_sub();
965 my ($self, $myconfig, $form) = @_;
968 # connect to database
969 my $dbh = $form->dbconnect($myconfig);
973 qq|SELECT description, inventory_accno_id,
974 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
976 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
978 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
980 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
982 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
984 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
986 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
988 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
990 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
993 my $sth = $dbh->prepare($query);
994 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
996 my $ref = $sth->fetchrow_hashref("NAME_lc");
998 map { $form->{$_} = $ref->{$_} } keys %$ref;
1003 qq|SELECT count(id) = 0 AS orphaned
1005 WHERE buchungsgruppen_id = ?|;
1006 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1009 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1011 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1012 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
1015 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1016 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1017 FROM chart c, defaults d
1018 WHERE c.link LIKE '%$module%'
1022 my $sth = $dbh->prepare($query);
1023 $sth->execute || $form->dberror($query);
1024 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1025 foreach my $key (split(/:/, $ref->{link})) {
1026 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1027 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1029 if ($key =~ /$module/) {
1030 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1031 || ($ref->{id} eq $ref->{income_accno_id})
1032 || ($ref->{id} eq $ref->{expense_accno_id})) {
1033 push @{ $form->{"${module}_links"}{$key} },
1034 { accno => $ref->{accno},
1035 description => $ref->{description},
1036 selected => "selected",
1039 push @{ $form->{"${module}_links"}{$key} },
1040 { accno => $ref->{accno},
1041 description => $ref->{description},
1053 $main::lxdebug->leave_sub();
1056 sub save_buchungsgruppe {
1057 $main::lxdebug->enter_sub();
1059 my ($self, $myconfig, $form) = @_;
1061 # connect to database
1062 my $dbh = $form->dbconnect($myconfig);
1064 my @values = ($form->{description}, $form->{inventory_accno_id},
1065 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1066 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1067 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1068 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1072 # id is the old record
1074 $query = qq|UPDATE buchungsgruppen SET
1075 description = ?, inventory_accno_id = ?,
1076 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1077 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1078 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1079 income_accno_id_3 = ?, expense_accno_id_3 = ?
1081 push(@values, $form->{id});
1083 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1084 my ($sortkey) = $dbh->selectrow_array($query);
1085 $form->dberror($query) if ($dbh->err);
1086 push(@values, $sortkey);
1087 $query = qq|INSERT INTO buchungsgruppen
1088 (description, inventory_accno_id,
1089 income_accno_id_0, expense_accno_id_0,
1090 income_accno_id_1, expense_accno_id_1,
1091 income_accno_id_2, expense_accno_id_2,
1092 income_accno_id_3, expense_accno_id_3,
1094 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1096 do_query($form, $dbh, $query, @values);
1100 $main::lxdebug->leave_sub();
1103 sub delete_buchungsgruppe {
1104 $main::lxdebug->enter_sub();
1106 my ($self, $myconfig, $form) = @_;
1108 # connect to database
1109 my $dbh = $form->dbconnect($myconfig);
1111 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1112 do_query($form, $dbh, $query, $form->{id});
1116 $main::lxdebug->leave_sub();
1120 $main::lxdebug->enter_sub();
1122 my ($self, $myconfig, $form, $table) = @_;
1124 # connect to database
1125 my $dbh = $form->get_standard_dbh($myconfig);
1129 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1130 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1131 my @values = ($form->{"id1"}, $form->{"id2"});
1132 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1134 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1135 my $sth = prepare_query($form, $dbh, $query);
1137 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1138 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1144 $main::lxdebug->leave_sub();
1148 $main::lxdebug->enter_sub();
1150 my ($self, $myconfig, $form) = @_;
1152 # connect to database
1153 my $dbh = $form->dbconnect($myconfig);
1155 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1157 my $sth = $dbh->prepare($query);
1158 $sth->execute || $form->dberror($query);
1161 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1162 push @{ $form->{ALL} }, $ref;
1168 $main::lxdebug->leave_sub();
1172 $main::lxdebug->enter_sub();
1174 my ($self, $myconfig, $form) = @_;
1176 # connect to database
1177 my $dbh = $form->dbconnect($myconfig);
1179 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1180 my $sth = $dbh->prepare($query);
1181 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1183 my $ref = $sth->fetchrow_hashref("NAME_lc");
1184 map { $form->{$_} = $ref->{$_} } keys %$ref;
1188 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1189 qq|FROM translation_payment_terms t | .
1190 qq|LEFT JOIN language l ON t.language_id = l.id | .
1191 qq|WHERE t.payment_terms_id = ? | .
1193 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1194 qq| l.description AS language | .
1195 qq|FROM language l|;
1196 $sth = $dbh->prepare($query);
1197 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1200 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1201 $mapping{ $ref->{"language_id"} } = $ref
1202 unless (defined($mapping{ $ref->{"language_id"} }));
1206 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1211 $main::lxdebug->leave_sub();
1215 $main::lxdebug->enter_sub();
1217 my ($self, $myconfig, $form) = @_;
1219 # connect to database
1220 my $dbh = $form->dbconnect_noauto($myconfig);
1225 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1226 qq|FROM payment_terms|;
1228 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1230 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1231 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1235 qq|DELETE FROM translation_payment_terms | .
1236 qq|WHERE payment_terms_id = ?|;
1237 do_query($form, $dbh, $query, $form->{"id"});
1240 $query = qq|UPDATE payment_terms SET
1241 description = ?, description_long = ?,
1242 terms_netto = ?, terms_skonto = ?,
1245 my @values = ($form->{description}, $form->{description_long},
1246 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1247 $form->{percent_skonto} * 1,
1249 do_query($form, $dbh, $query, @values);
1251 $query = qq|SELECT id FROM language|;
1253 my $sth = $dbh->prepare($query);
1254 $sth->execute() || $form->dberror($query);
1256 while (my ($id) = $sth->fetchrow_array()) {
1257 push(@language_ids, $id);
1262 qq|INSERT INTO translation_payment_terms | .
1263 qq|(language_id, payment_terms_id, description_long) | .
1264 qq|VALUES (?, ?, ?)|;
1265 $sth = $dbh->prepare($query);
1267 foreach my $language_id (@language_ids) {
1268 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1269 $form->{"description_long_${language_id}"});
1276 $main::lxdebug->leave_sub();
1279 sub delete_payment {
1280 $main::lxdebug->enter_sub();
1282 my ($self, $myconfig, $form) = @_;
1284 # connect to database
1285 my $dbh = $form->dbconnect_noauto($myconfig);
1288 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1289 do_query($form, $dbh, $query, $form->{"id"});
1291 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1292 do_query($form, $dbh, $query, $form->{"id"});
1297 $main::lxdebug->leave_sub();
1301 sub prepare_template_filename {
1302 $main::lxdebug->enter_sub();
1304 my ($self, $myconfig, $form) = @_;
1306 my ($filename, $display_filename);
1308 if ($form->{type} eq "stylesheet") {
1309 $filename = "css/$myconfig->{stylesheet}";
1310 $display_filename = $myconfig->{stylesheet};
1313 $filename = $form->{formname};
1315 if ($form->{language}) {
1316 my ($id, $template_code) = split(/--/, $form->{language});
1317 $filename .= "_${template_code}";
1320 if ($form->{printer}) {
1321 my ($id, $template_code) = split(/--/, $form->{printer});
1322 $filename .= "_${template_code}";
1325 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1326 $filename =~ s|.*/||;
1327 $display_filename = $filename;
1328 $filename = "$myconfig->{templates}/$filename";
1331 $main::lxdebug->leave_sub();
1333 return ($filename, $display_filename);
1338 $main::lxdebug->enter_sub();
1340 my ($self, $filename) = @_;
1342 my ($content, $lines) = ("", 0);
1346 if (open(TEMPLATE, $filename)) {
1347 while (<TEMPLATE>) {
1354 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1356 $main::lxdebug->leave_sub();
1358 return ($content, $lines);
1362 $main::lxdebug->enter_sub();
1364 my ($self, $filename, $content) = @_;
1370 if (open(TEMPLATE, ">$filename")) {
1371 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1372 $content =~ s/\r\n/\n/g;
1373 print(TEMPLATE $content);
1379 $main::lxdebug->leave_sub();
1385 $main::lxdebug->enter_sub();
1390 my $myconfig = \%main::myconfig;
1391 my $form = $main::form;
1393 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1396 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1398 $form->{curr} =~ s/ //g;
1399 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1400 my $currency = join ':', @currencies;
1402 # these defaults are database wide
1405 qq|UPDATE defaults SET
1406 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1407 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1408 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1409 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1410 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1411 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1427 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1428 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1429 $form->{invnumber}, $form->{cnnumber},
1430 $form->{sonumber}, $form->{ponumber},
1431 $form->{sqnumber}, $form->{rfqnumber},
1432 $form->{customernumber}, $form->{vendornumber},
1433 $form->{articlenumber}, $form->{servicenumber},
1434 $form->{sdonumber}, $form->{pdonumber},
1436 $form->{businessnumber}, $form->{weightunit});
1437 do_query($form, $dbh, $query, @values);
1441 $main::lxdebug->leave_sub();
1445 sub save_preferences {
1446 $main::lxdebug->enter_sub();
1448 my ($self, $myconfig, $form) = @_;
1450 my $dbh = $form->get_standard_dbh($myconfig);
1452 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1455 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1456 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1458 my $rc = $dbh->commit();
1460 # save first currency in myconfig
1461 $currency =~ s/:.*//;
1462 $form->{currency} = $currency;
1464 $form->{businessnumber} = $businessnumber;
1466 $myconfig = new User($form->{login});
1468 foreach my $item (keys %$form) {
1469 $myconfig->{$item} = $form->{$item};
1472 $myconfig->save_member;
1474 my $auth = $main::auth;
1476 $main::lxdebug->leave_sub();
1482 $main::lxdebug->enter_sub();
1487 my $myconfig = \%main::myconfig;
1488 my $form = $main::form;
1490 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1492 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1494 $defaults->{weightunit} ||= 'kg';
1496 $main::lxdebug->leave_sub();
1501 sub defaultaccounts {
1502 $main::lxdebug->enter_sub();
1504 my ($self, $myconfig, $form) = @_;
1506 # connect to database
1507 my $dbh = $form->dbconnect($myconfig);
1509 # get defaults from defaults table
1510 my $query = qq|SELECT * FROM defaults|;
1511 my $sth = $dbh->prepare($query);
1512 $sth->execute || $form->dberror($query);
1514 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1515 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1516 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1517 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1518 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1519 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1520 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1522 $form->{defaults}{weightunit} ||= 'kg';
1526 $query = qq|SELECT c.id, c.accno, c.description, c.link
1528 WHERE c.link LIKE '%IC%'
1530 $sth = $dbh->prepare($query);
1531 $sth->execute || $self->dberror($query);
1533 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1534 foreach my $key (split(/:/, $ref->{link})) {
1537 if ($key =~ /cogs/) {
1538 $nkey = "IC_expense";
1540 if ($key =~ /sale/) {
1541 $nkey = "IC_income";
1543 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1545 description => $ref->{description}
1552 $query = qq|SELECT c.id, c.accno, c.description
1554 WHERE c.category = 'I'
1555 AND c.charttype = 'A'
1557 $sth = $dbh->prepare($query);
1558 $sth->execute || $self->dberror($query);
1560 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1561 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1563 description => $ref->{description}
1568 $query = qq|SELECT c.id, c.accno, c.description
1570 WHERE c.category = 'E'
1571 AND c.charttype = 'A'
1573 $sth = $dbh->prepare($query);
1574 $sth->execute || $self->dberror($query);
1576 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1577 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1579 description => $ref->{description}
1584 # now get the tax rates and numbers
1585 $query = qq|SELECT c.id, c.accno, c.description,
1586 t.rate * 100 AS rate, t.taxnumber
1588 WHERE c.id = t.chart_id|;
1590 $sth = $dbh->prepare($query);
1591 $sth->execute || $form->dberror($query);
1593 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1594 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1595 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1596 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1597 if $ref->{taxnumber};
1598 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1600 # Abfrage für Standard Umlaufvermögenskonto
1602 qq|SELECT id, accno, description, link | .
1604 qq|WHERE link LIKE ? |.
1606 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1607 $sth->execute || $form->dberror($query);#
1608 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1609 foreach my $item (split(/:/, $ref->{link})) {
1610 if ($item eq "AR_paid") {
1611 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1613 description => $ref->{description}
1622 $main::lxdebug->leave_sub();
1626 $main::lxdebug->enter_sub();
1628 my ($self, $myconfig, $form) = @_;
1630 my $dbh = $form->dbconnect($myconfig);
1632 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1633 my $sth = $dbh->prepare($query);
1634 $sth->execute || $form->dberror($query);
1636 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1642 $main::lxdebug->leave_sub();
1646 $main::lxdebug->enter_sub();
1648 my ($self, $myconfig, $form) = @_;
1650 my $dbh = $form->dbconnect($myconfig);
1652 my ($query, @values);
1654 if ($form->{revtrans}) {
1655 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1657 } elsif ($form->{closedto}) {
1658 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1659 @values = (conv_date($form->{closedto}));
1662 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1665 # set close in defaults
1666 do_query($form, $dbh, $query, @values);
1670 $main::lxdebug->leave_sub();
1674 my ($self, $units, $unit_name, $factor) = @_;
1676 $factor = 1 unless ($factor);
1678 my $unit = $units->{$unit_name};
1680 if (!defined($unit) || !$unit->{"base_unit"} ||
1681 ($unit_name eq $unit->{"base_unit"})) {
1682 return ($unit_name, $factor);
1685 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1688 sub retrieve_units {
1689 $main::lxdebug->enter_sub();
1691 my ($self, $myconfig, $form, $prefix) = @_;
1693 my $dbh = $form->get_standard_dbh;
1695 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1697 my $sth = prepare_execute_query($form, $dbh, $query);
1700 while (my $ref = $sth->fetchrow_hashref()) {
1701 $units->{$ref->{"name"}} = $ref;
1705 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1706 $sth = $dbh->prepare($query_lang);
1707 $sth->execute() || $form->dberror($query_lang);
1709 while (my $ref = $sth->fetchrow_hashref()) {
1710 push(@languages, $ref);
1714 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1715 "FROM units_language ul " .
1716 "LEFT JOIN language l ON ul.language_id = l.id " .
1717 "WHERE ul.unit = ?";
1718 $sth = $dbh->prepare($query_lang);
1720 foreach my $unit (values(%{$units})) {
1721 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1723 $unit->{"LANGUAGES"} = {};
1724 foreach my $lang (@languages) {
1725 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1728 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1729 while (my $ref = $sth->fetchrow_hashref()) {
1730 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1735 $main::lxdebug->leave_sub();
1740 sub retrieve_all_units {
1741 $main::lxdebug->enter_sub();
1745 if (!$main::all_units) {
1746 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1749 $main::lxdebug->leave_sub();
1751 return $main::all_units;
1755 sub translate_units {
1756 $main::lxdebug->enter_sub();
1758 my ($self, $form, $template_code, $unit, $amount) = @_;
1760 my $units = $self->retrieve_units(\%main::myconfig, $form);
1762 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1763 my $new_unit = $unit;
1765 if (($amount != 1) && $h->{"localized_plural"}) {
1766 $new_unit = $h->{"localized_plural"};
1767 } elsif ($h->{"localized"}) {
1768 $new_unit = $h->{"localized"};
1772 $main::lxdebug->leave_sub();
1778 $main::lxdebug->enter_sub();
1780 my ($self, $myconfig, $form, $units) = @_;
1782 my $dbh = $form->dbconnect($myconfig);
1784 map({ $_->{"in_use"} = 0; } values(%{$units}));
1786 foreach my $unit (values(%{$units})) {
1787 my $base_unit = $unit->{"original_base_unit"};
1788 while ($base_unit) {
1789 $units->{$base_unit}->{"in_use"} = 1;
1790 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1791 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1792 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1796 foreach my $unit (values(%{$units})) {
1797 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1799 foreach my $table (qw(parts invoice orderitems)) {
1800 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1802 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1803 $query .= "= " . $dbh->quote($unit->{"name"});
1805 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1806 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1809 my ($count) = $dbh->selectrow_array($query);
1810 $form->dberror($query) if ($dbh->err);
1813 $unit->{"in_use"} = 1;
1821 $main::lxdebug->leave_sub();
1824 sub convertible_units {
1825 $main::lxdebug->enter_sub();
1829 my $filter_unit = shift;
1830 my $not_smaller = shift;
1832 my $conv_units = [];
1834 $filter_unit = $units->{$filter_unit};
1836 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1837 my $unit = $units->{$name};
1839 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1840 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1841 push @{$conv_units}, $unit;
1845 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1847 $main::lxdebug->leave_sub();
1852 # if $a is translatable to $b, return the factor between them.
1855 $main::lxdebug->enter_sub(2);
1856 my ($this, $a, $b, $all_units) = @_;
1858 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1859 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1860 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1861 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1864 sub unit_select_data {
1865 $main::lxdebug->enter_sub();
1867 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1872 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1875 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1876 if (!$convertible_into ||
1877 ($units->{$convertible_into} &&
1878 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1879 push @{$select}, { "name" => $unit,
1880 "base_unit" => $units->{$unit}->{"base_unit"},
1881 "factor" => $units->{$unit}->{"factor"},
1882 "selected" => ($unit eq $selected) ? "selected" : "" };
1886 $main::lxdebug->leave_sub();
1891 sub unit_select_html {
1892 $main::lxdebug->enter_sub();
1894 my ($self, $units, $name, $selected, $convertible_into) = @_;
1896 my $select = "<select name=${name}>";
1898 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1899 if (!$convertible_into ||
1900 ($units->{$convertible_into} &&
1901 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1902 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1905 $select .= "</select>";
1907 $main::lxdebug->leave_sub();
1913 $main::lxdebug->enter_sub();
1917 my $units = $self->retrieve_all_units();
1922 while (2 <= scalar(@_)) {
1923 my $qty = shift(@_);
1924 my $unit = $units->{shift(@_)};
1926 croak "No unit defined with name $unit" if (!defined $unit);
1929 $base_unit = $unit->{base_unit};
1930 } elsif ($base_unit ne $unit->{base_unit}) {
1931 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1934 $sum += $qty * $unit->{factor};
1937 $main::lxdebug->leave_sub();
1939 return wantarray ? ($sum, $base_unit) : $sum;
1943 $main::lxdebug->enter_sub();
1945 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1947 my $dbh = $form->dbconnect_noauto($myconfig);
1949 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1950 my ($sortkey) = selectrow_query($form, $dbh, $query);
1952 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1953 "VALUES (?, ?, ?, ?)";
1954 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1957 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1958 my $sth = $dbh->prepare($query);
1959 foreach my $lang (@{$languages}) {
1960 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1961 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1969 $main::lxdebug->leave_sub();
1973 $main::lxdebug->enter_sub();
1975 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1977 my $dbh = $form->dbconnect_noauto($myconfig);
1979 my ($base_unit, $unit, $sth, $query);
1981 $query = "DELETE FROM units_language";
1982 $dbh->do($query) || $form->dberror($query);
1984 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1985 $query = "DELETE FROM units WHERE name IN (";
1986 map({ $query .= "?," } @{$delete_units});
1987 substr($query, -1, 1) = ")";
1988 $dbh->do($query, undef, @{$delete_units}) ||
1989 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1992 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1993 $sth = $dbh->prepare($query);
1995 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1996 my $sth_lang = $dbh->prepare($query_lang);
1998 foreach $unit (values(%{$units})) {
1999 $unit->{"depth"} = 0;
2000 my $base_unit = $unit;
2001 while ($base_unit->{"base_unit"}) {
2003 $base_unit = $units->{$base_unit->{"base_unit"}};
2007 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2008 if ($unit->{"LANGUAGES"}) {
2009 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2010 next unless ($lang->{"id"} && $lang->{"localized"});
2011 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2012 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2016 next if ($unit->{"unchanged_unit"});
2018 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2019 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2023 $sth_lang->finish();
2027 $main::lxdebug->leave_sub();
2031 $main::lxdebug->enter_sub();
2033 my ($self, $myconfig, $form) = @_;
2035 # connect to database
2036 my $dbh = $form->dbconnect($myconfig);
2038 my $query = qq|SELECT
2042 round(t.rate * 100, 2) AS rate,
2043 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2044 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2048 my $sth = $dbh->prepare($query);
2049 $sth->execute || $form->dberror($query);
2052 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2053 push @{ $form->{TAX} }, $ref;
2059 $main::lxdebug->leave_sub();
2062 sub get_tax_accounts {
2063 $main::lxdebug->enter_sub();
2065 my ($self, $myconfig, $form) = @_;
2067 my $dbh = $form->dbconnect($myconfig);
2069 # get Accounts from chart
2070 my $query = qq{ SELECT
2072 accno || ' - ' || description AS taxaccount
2074 WHERE link LIKE '%_tax%'
2078 my $sth = $dbh->prepare($query);
2079 $sth->execute || $form->dberror($query);
2081 $form->{ACCOUNTS} = [];
2082 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2083 push @{ $form->{ACCOUNTS} }, $ref;
2090 $main::lxdebug->leave_sub();
2094 $main::lxdebug->enter_sub();
2096 my ($self, $myconfig, $form) = @_;
2098 # connect to database
2099 my $dbh = $form->dbconnect($myconfig);
2101 my $query = qq|SELECT
2104 round(rate * 100, 2) AS rate,
2109 my $sth = $dbh->prepare($query);
2110 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2112 my $ref = $sth->fetchrow_hashref("NAME_lc");
2114 map { $form->{$_} = $ref->{$_} } keys %$ref;
2118 # see if it is used by a taxkey
2119 $query = qq|SELECT count(*) FROM taxkeys
2120 WHERE tax_id = ? AND chart_id >0|;
2122 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2124 $form->{orphaned} = !$form->{orphaned};
2127 if (!$form->{orphaned} ) {
2128 $query = qq|SELECT DISTINCT c.id, c.accno
2130 JOIN tax t ON (t.id = tk.tax_id)
2131 JOIN chart c ON (c.id = tk.chart_id)
2132 WHERE tk.tax_id = ?|;
2134 $sth = $dbh->prepare($query);
2135 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2137 $form->{TAXINUSE} = [];
2138 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2139 push @{ $form->{TAXINUSE} }, $ref;
2147 $main::lxdebug->leave_sub();
2151 $main::lxdebug->enter_sub();
2153 my ($self, $myconfig, $form) = @_;
2156 # connect to database
2157 my $dbh = $form->get_standard_dbh($myconfig);
2159 $form->{rate} = $form->{rate} / 100;
2161 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2162 if ($form->{id} ne "") {
2163 $query = qq|UPDATE tax SET
2168 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2170 push(@values, $form->{id});
2174 $query = qq|INSERT INTO tax (
2181 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2183 do_query($form, $dbh, $query, @values);
2187 $main::lxdebug->leave_sub();
2191 $main::lxdebug->enter_sub();
2193 my ($self, $myconfig, $form) = @_;
2196 # connect to database
2197 my $dbh = $form->get_standard_dbh($myconfig);
2199 $query = qq|DELETE FROM tax
2201 do_query($form, $dbh, $query, $form->{id});
2205 $main::lxdebug->leave_sub();
2208 sub save_price_factor {
2209 $main::lxdebug->enter_sub();
2211 my ($self, $myconfig, $form) = @_;
2213 # connect to database
2214 my $dbh = $form->get_standard_dbh($myconfig);
2217 my @values = ($form->{description}, conv_i($form->{factor}));
2220 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2221 push @values, conv_i($form->{id});
2224 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2227 do_query($form, $dbh, $query, @values);
2231 $main::lxdebug->leave_sub();
2234 sub get_all_price_factors {
2235 $main::lxdebug->enter_sub();
2237 my ($self, $myconfig, $form) = @_;
2239 # connect to database
2240 my $dbh = $form->get_standard_dbh($myconfig);
2242 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2244 $main::lxdebug->leave_sub();
2247 sub get_price_factor {
2248 $main::lxdebug->enter_sub();
2250 my ($self, $myconfig, $form) = @_;
2252 # connect to database
2253 my $dbh = $form->get_standard_dbh($myconfig);
2255 my $query = qq|SELECT description, factor,
2256 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2257 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2258 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2259 FROM price_factors WHERE id = ?|;
2261 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2263 $main::lxdebug->leave_sub();
2266 sub delete_price_factor {
2267 $main::lxdebug->enter_sub();
2269 my ($self, $myconfig, $form) = @_;
2271 # connect to database
2272 my $dbh = $form->get_standard_dbh($myconfig);
2274 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2277 $main::lxdebug->leave_sub();
2280 sub save_warehouse {
2281 $main::lxdebug->enter_sub();
2283 my ($self, $myconfig, $form) = @_;
2285 # connect to database
2286 my $dbh = $form->get_standard_dbh($myconfig);
2288 my ($query, @values, $sth);
2291 $query = qq|SELECT nextval('id')|;
2292 ($form->{id}) = selectrow_query($form, $dbh, $query);
2294 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2295 do_query($form, $dbh, $query, $form->{id});
2298 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2299 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2301 if (0 < $form->{number_of_new_bins}) {
2302 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2303 $sth = prepare_query($form, $dbh, $query);
2305 foreach my $i (1..$form->{number_of_new_bins}) {
2306 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2314 $main::lxdebug->leave_sub();
2318 $main::lxdebug->enter_sub();
2320 my ($self, $myconfig, $form) = @_;
2322 # connect to database
2323 my $dbh = $form->get_standard_dbh($myconfig);
2325 my ($query, @values, $commit_necessary, $sth);
2327 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2330 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2331 do_query($form, $dbh, $query, @values);
2333 $commit_necessary = 1;
2336 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2337 $sth = prepare_query($form, $dbh, $query);
2339 foreach my $row (1..$form->{rowcount}) {
2340 next if ($form->{"delete_${row}"});
2342 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2344 $commit_necessary = 1;
2349 $dbh->commit() if ($commit_necessary);
2351 $main::lxdebug->leave_sub();
2354 sub delete_warehouse {
2355 $main::lxdebug->enter_sub();
2357 my ($self, $myconfig, $form) = @_;
2359 # connect to database
2360 my $dbh = $form->get_standard_dbh($myconfig);
2362 my $id = conv_i($form->{id});
2363 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|;
2364 my ($count) = selectrow_query($form, $dbh, $query, $id);
2367 $main::lxdebug->leave_sub();
2371 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2372 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2376 $main::lxdebug->leave_sub();
2381 sub get_all_warehouses {
2382 $main::lxdebug->enter_sub();
2384 my ($self, $myconfig, $form) = @_;
2386 # connect to database
2387 my $dbh = $form->get_standard_dbh($myconfig);
2389 my $query = qq|SELECT w.id, w.description, w.invalid,
2390 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2392 ORDER BY w.sortkey|;
2394 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2396 $main::lxdebug->leave_sub();
2400 $main::lxdebug->enter_sub();
2402 my ($self, $myconfig, $form) = @_;
2404 # connect to database
2405 my $dbh = $form->get_standard_dbh($myconfig);
2407 my $id = conv_i($form->{id});
2408 my $query = qq|SELECT w.description, w.invalid
2412 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2414 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2416 $query = qq|SELECT b.*, EXISTS
2417 (SELECT i.warehouse_id
2419 WHERE i.bin_id = b.id
2423 WHERE b.warehouse_id = ?|;
2425 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2427 $main::lxdebug->leave_sub();