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 #======================================================================
47 $main::lxdebug->enter_sub();
49 my ($self, $myconfig, $form) = @_;
52 my $dbh = $form->dbconnect($myconfig);
54 SELECT c.accno, c.description, c.charttype, c.category,
55 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
56 c.pos_bwa, datevautomatik,
57 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
58 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
61 ON (c.id=tk.chart_id AND tk.id =
62 (SELECT id FROM taxkeys
63 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
64 ORDER BY startdate DESC LIMIT 1))
69 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
70 my $sth = $dbh->prepare($query);
71 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
73 my $ref = $sth->fetchrow_hashref("NAME_lc");
75 foreach my $key (keys %$ref) {
76 $form->{"$key"} = $ref->{"$key"};
81 # get default accounts
82 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
84 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
85 $sth = $dbh->prepare($query);
86 $sth->execute || $form->dberror($query);
88 $ref = $sth->fetchrow_hashref("NAME_lc");
90 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
96 # get taxkeys and description
100 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
102 id||'--'||taxkey AS tax,
105 FROM tax ORDER BY taxkey
107 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
108 $sth = $dbh->prepare($query);
109 $sth->execute || $form->dberror($query);
111 $form->{TAXKEY} = [];
113 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
114 push @{ $form->{TAXKEY} }, $ref;
120 $query = qq|SELECT id, accno,description
124 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
125 $sth = $dbh->prepare($query);
126 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
128 $form->{NEWACCOUNT} = [];
129 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
130 push @{ $form->{NEWACCOUNT} }, $ref;
135 # get the taxkeys of account
149 LEFT JOIN tax t ON (t.id = tk.tax_id)
150 LEFT JOIN chart c ON (c.id = t.chart_id)
152 WHERE tk.chart_id = ?
153 ORDER BY startdate DESC
155 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
156 $sth = $dbh->prepare($query);
158 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
160 $form->{ACCOUNT_TAXKEYS} = [];
162 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
163 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
169 # check if we have any transactions
170 $query = qq|SELECT a.trans_id FROM acc_trans a
171 WHERE a.chart_id = ?|;
172 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
173 $sth = $dbh->prepare($query);
174 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
176 ($form->{orphaned}) = $sth->fetchrow_array;
177 $form->{orphaned} = !$form->{orphaned};
180 # check if new account is active
181 $form->{new_chart_valid} = 0;
182 if ($form->{new_chart_id}) {
183 $query = qq|SELECT current_date-valid_from FROM chart
185 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
186 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
188 $form->{new_chart_valid} = 1;
195 $main::lxdebug->leave_sub();
199 $main::lxdebug->enter_sub();
201 my ($self, $myconfig, $form) = @_;
203 # connect to database, turn off AutoCommit
204 my $dbh = $form->dbconnect_noauto($myconfig);
206 # sanity check, can't have AR with AR_...
207 if ($form->{AR} || $form->{AP} || $form->{IC}) {
208 map { delete $form->{$_} }
209 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);
213 foreach my $item ($form->{AR}, $form->{AR_amount},
214 $form->{AR_tax}, $form->{AR_paid},
215 $form->{AP}, $form->{AP_amount},
216 $form->{AP_tax}, $form->{AP_paid},
217 $form->{IC}, $form->{IC_sale},
218 $form->{IC_cogs}, $form->{IC_taxpart},
219 $form->{IC_income}, $form->{IC_expense},
220 $form->{IC_taxservice}, $form->{CT_tax}
222 $form->{link} .= "${item}:" if ($item);
226 # strip blanks from accno
227 map { $form->{$_} =~ s/ //g; } qw(accno);
231 if ($form->{id} eq "NULL") {
235 if (!$form->{id} || $form->{id} eq "") {
236 $query = qq|SELECT nextval('id')|;
237 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
238 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
239 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
245 $query = qq|UPDATE chart SET
261 $form->{description},
265 conv_i($form->{pos_bwa}),
266 conv_i($form->{pos_bilanz}),
267 conv_i($form->{pos_eur}),
268 conv_i($form->{new_chart_id}),
269 conv_date($form->{valid_from}),
270 ($form->{datevautomatik} eq 'T') ? 'true':'false',
276 do_query($form, $dbh, $query, @values);
282 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
286 for $tk_count (0 .. $MAX_TRIES) {
290 # Check if the account already exists, else cancel
292 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
294 last READTAXKEYS if ( $form->{'id'} == 0);
296 # check if there is a startdate
297 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
302 # Add valid taxkeys into the array
305 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
306 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
307 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
308 chart_id => conv_i($form->{"id"}),
309 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
310 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
317 for my $j (0 .. $#taxkeys){
318 if ( defined $taxkeys[$j]{'id'} ){
321 if ($taxkeys[$j]{'delete'}){
323 DELETE FROM taxkeys WHERE id = ?
326 @values = ($taxkeys[$j]{'id'});
328 do_query($form, $dbh, $query, @values);
337 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
345 $taxkeys[$j]{'tax_id'},
346 $taxkeys[$j]{'chart_id'},
347 $taxkeys[$j]{'tax_id'},
348 $taxkeys[$j]{'pos_ustva'},
349 $taxkeys[$j]{'startdate'},
352 do_query($form, $dbh, $query, @values);
358 INSERT INTO taxkeys (
365 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
368 $taxkeys[$j]{'tax_id'},
369 $taxkeys[$j]{'chart_id'},
370 $taxkeys[$j]{'tax_id'},
371 $taxkeys[$j]{'pos_ustva'},
372 $taxkeys[$j]{'startdate'},
375 do_query($form, $dbh, $query, @values);
381 my $rc = $dbh->commit;
384 $main::lxdebug->leave_sub();
390 $main::lxdebug->enter_sub();
392 my ($self, $myconfig, $form) = @_;
394 # connect to database, turn off AutoCommit
395 my $dbh = $form->dbconnect_noauto($myconfig);
397 my $query = qq|SELECT count(*) FROM acc_trans a
398 WHERE a.chart_id = ?|;
399 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
403 $main::lxdebug->leave_sub();
407 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
408 foreach my $type (qw(inventory income expense)) {
411 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
412 qq|WHERE ${type}_accno_id = ?|;
413 do_query($form, $dbh, $query, $form->{id});
416 foreach my $table (qw(partstax customertax vendortax tax)) {
417 $query = qq|DELETE FROM $table
419 do_query($form, $dbh, $query, $form->{id});
422 # delete chart of account record
423 $query = qq|DELETE FROM chart
425 do_query($form, $dbh, $query, $form->{id});
427 # delete account taxkeys
428 $query = qq|DELETE FROM taxkeys
430 do_query($form, $dbh, $query, $form->{id});
432 # commit and redirect
433 my $rc = $dbh->commit;
436 $main::lxdebug->leave_sub();
442 $main::lxdebug->enter_sub();
444 my ($self, $myconfig, $form) = @_;
446 # connect to database
447 my $dbh = $form->dbconnect($myconfig);
449 my $query = qq|SELECT d.id, d.description, d.role
453 my $sth = $dbh->prepare($query);
454 $sth->execute || $form->dberror($query);
457 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
458 push @{ $form->{ALL} }, $ref;
464 $main::lxdebug->leave_sub();
468 $main::lxdebug->enter_sub();
470 my ($self, $myconfig, $form) = @_;
472 # connect to database
473 my $dbh = $form->dbconnect($myconfig);
475 my $query = qq|SELECT d.description, d.role
478 my $sth = $dbh->prepare($query);
479 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
481 my $ref = $sth->fetchrow_hashref("NAME_lc");
483 map { $form->{$_} = $ref->{$_} } keys %$ref;
487 # see if it is in use
488 $query = qq|SELECT count(*) FROM dpt_trans d
489 WHERE d.department_id = ?|;
490 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
492 $form->{orphaned} = !$form->{orphaned};
497 $main::lxdebug->leave_sub();
500 sub save_department {
501 $main::lxdebug->enter_sub();
503 my ($self, $myconfig, $form) = @_;
506 # connect to database
507 my $dbh = $form->dbconnect($myconfig);
509 my @values = ($form->{description}, $form->{role});
511 $query = qq|UPDATE department SET
512 description = ?, role = ?
514 push(@values, $form->{id});
516 $query = qq|INSERT INTO department
520 do_query($form, $dbh, $query, @values);
524 $main::lxdebug->leave_sub();
527 sub delete_department {
528 $main::lxdebug->enter_sub();
530 my ($self, $myconfig, $form) = @_;
533 # connect to database
534 my $dbh = $form->dbconnect($myconfig);
536 $query = qq|DELETE FROM department
538 do_query($form, $dbh, $query, $form->{id});
542 $main::lxdebug->leave_sub();
546 $main::lxdebug->enter_sub();
548 my ($self, $myconfig, $form) = @_;
550 # connect to database
551 my $dbh = $form->dbconnect($myconfig);
553 my $query = qq|SELECT id, lead
557 my $sth = $dbh->prepare($query);
558 $sth->execute || $form->dberror($query);
560 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
561 push @{ $form->{ALL} }, $ref;
567 $main::lxdebug->leave_sub();
571 $main::lxdebug->enter_sub();
573 my ($self, $myconfig, $form) = @_;
575 # connect to database
576 my $dbh = $form->dbconnect($myconfig);
579 qq|SELECT l.id, l.lead | .
582 my $sth = $dbh->prepare($query);
583 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
585 my $ref = $sth->fetchrow_hashref("NAME_lc");
587 map { $form->{$_} = $ref->{$_} } keys %$ref;
593 $main::lxdebug->leave_sub();
597 $main::lxdebug->enter_sub();
599 my ($self, $myconfig, $form) = @_;
602 # connect to database
603 my $dbh = $form->dbconnect($myconfig);
605 my @values = ($form->{description});
606 # id is the old record
608 $query = qq|UPDATE leads SET
611 puhs(@values, $form->{id});
613 $query = qq|INSERT INTO leads
617 do_query($form, $dbh, $query, @values);
621 $main::lxdebug->leave_sub();
625 $main::lxdebug->enter_sub();
627 my ($self, $myconfig, $form) = @_;
630 # connect to database
631 my $dbh = $form->dbconnect($myconfig);
633 $query = qq|DELETE FROM leads
635 do_query($form, $dbh, $query, $form->{id});
639 $main::lxdebug->leave_sub();
643 $main::lxdebug->enter_sub();
645 my ($self, $myconfig, $form) = @_;
647 # connect to database
648 my $dbh = $form->dbconnect($myconfig);
650 my $query = qq|SELECT id, description, discount, customernumberinit
654 my $sth = $dbh->prepare($query);
655 $sth->execute || $form->dberror($query);
657 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
658 push @{ $form->{ALL} }, $ref;
664 $main::lxdebug->leave_sub();
668 $main::lxdebug->enter_sub();
670 my ($self, $myconfig, $form) = @_;
672 # connect to database
673 my $dbh = $form->dbconnect($myconfig);
676 qq|SELECT b.description, b.discount, b.customernumberinit
679 my $sth = $dbh->prepare($query);
680 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
682 my $ref = $sth->fetchrow_hashref("NAME_lc");
684 map { $form->{$_} = $ref->{$_} } keys %$ref;
690 $main::lxdebug->leave_sub();
694 $main::lxdebug->enter_sub();
696 my ($self, $myconfig, $form) = @_;
699 # connect to database
700 my $dbh = $form->dbconnect($myconfig);
702 my @values = ($form->{description}, $form->{discount},
703 $form->{customernumberinit});
704 # id is the old record
706 $query = qq|UPDATE business SET
709 customernumberinit = ?
711 push(@values, $form->{id});
713 $query = qq|INSERT INTO business
714 (description, discount, customernumberinit)
717 do_query($form, $dbh, $query, @values);
721 $main::lxdebug->leave_sub();
724 sub delete_business {
725 $main::lxdebug->enter_sub();
727 my ($self, $myconfig, $form) = @_;
729 # connect to database
730 my $dbh = $form->dbconnect($myconfig);
732 my $query = qq|DELETE FROM business
734 do_query($form, $dbh, $query, $form->{id});
738 $main::lxdebug->leave_sub();
743 $main::lxdebug->enter_sub();
745 my ($self, $myconfig, $form, $return_list) = @_;
747 # connect to database
748 my $dbh = $form->dbconnect($myconfig);
751 "SELECT id, description, template_code, article_code, " .
752 " output_numberformat, output_dateformat, output_longdates " .
753 "FROM language ORDER BY description";
755 my $sth = $dbh->prepare($query);
756 $sth->execute || $form->dberror($query);
760 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
761 push(@{ $ary }, $ref);
767 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 my ($self, $myconfig, $form) = @_;
781 # connect to database
782 my $dbh = $form->dbconnect($myconfig);
785 "SELECT description, template_code, article_code, " .
786 " output_numberformat, output_dateformat, output_longdates " .
787 "FROM language WHERE id = ?";
788 my $sth = $dbh->prepare($query);
789 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
791 my $ref = $sth->fetchrow_hashref("NAME_lc");
793 map { $form->{$_} = $ref->{$_} } keys %$ref;
799 $main::lxdebug->leave_sub();
802 sub get_language_details {
803 $main::lxdebug->enter_sub();
805 my ($self, $myconfig, $form, $id) = @_;
807 # connect to database
808 my $dbh = $form->dbconnect($myconfig);
811 "SELECT template_code, " .
812 " output_numberformat, output_dateformat, output_longdates " .
813 "FROM language WHERE id = ?";
814 my @res = selectrow_query($form, $dbh, $query, $id);
817 $main::lxdebug->leave_sub();
823 $main::lxdebug->enter_sub();
825 my ($self, $myconfig, $form) = @_;
827 # connect to database
828 my $dbh = $form->dbconnect($myconfig);
829 my (@values, $query);
831 map({ push(@values, $form->{$_}); }
832 qw(description template_code article_code
833 output_numberformat output_dateformat output_longdates));
835 # id is the old record
838 "UPDATE language SET " .
839 " description = ?, template_code = ?, article_code = ?, " .
840 " output_numberformat = ?, output_dateformat = ?, " .
841 " output_longdates = ? " .
843 push(@values, $form->{id});
846 "INSERT INTO language (" .
847 " description, template_code, article_code, " .
848 " output_numberformat, output_dateformat, output_longdates" .
849 ") VALUES (?, ?, ?, ?, ?, ?)";
851 do_query($form, $dbh, $query, @values);
855 $main::lxdebug->leave_sub();
858 sub delete_language {
859 $main::lxdebug->enter_sub();
861 my ($self, $myconfig, $form) = @_;
864 # connect to database
865 my $dbh = $form->dbconnect_noauto($myconfig);
867 foreach my $table (qw(translation_payment_terms units_language)) {
868 $query = qq|DELETE FROM $table WHERE language_id = ?|;
869 do_query($form, $dbh, $query, $form->{"id"});
872 $query = "DELETE FROM language WHERE id = ?";
873 do_query($form, $dbh, $query, $form->{"id"});
878 $main::lxdebug->leave_sub();
883 $main::lxdebug->enter_sub();
885 my ($self, $myconfig, $form) = @_;
887 # connect to database
888 my $dbh = $form->dbconnect($myconfig);
890 my $query = qq|SELECT id, description,
892 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
894 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
896 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
898 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
900 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
902 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
904 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
906 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
908 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
912 my $sth = $dbh->prepare($query);
913 $sth->execute || $form->dberror($query);
916 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
917 push @{ $form->{ALL} }, $ref;
923 $main::lxdebug->leave_sub();
926 sub get_buchungsgruppe {
927 $main::lxdebug->enter_sub();
929 my ($self, $myconfig, $form) = @_;
932 # connect to database
933 my $dbh = $form->dbconnect($myconfig);
937 qq|SELECT description, inventory_accno_id,
938 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
940 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
942 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
944 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
946 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
948 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
950 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
952 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
954 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
957 my $sth = $dbh->prepare($query);
958 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
960 my $ref = $sth->fetchrow_hashref("NAME_lc");
962 map { $form->{$_} = $ref->{$_} } keys %$ref;
967 qq|SELECT count(id) = 0 AS orphaned
969 WHERE buchungsgruppen_id = ?|;
970 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
973 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
975 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
976 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
979 $query = qq|SELECT c.accno, c.description, c.link, c.id,
980 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
981 FROM chart c, defaults d
982 WHERE c.link LIKE '%$module%'
986 my $sth = $dbh->prepare($query);
987 $sth->execute || $form->dberror($query);
988 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
989 foreach my $key (split(/:/, $ref->{link})) {
990 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
991 $form->{"std_inventory_accno_id"} = $ref->{"id"};
993 if ($key =~ /$module/) {
994 if ( ($ref->{id} eq $ref->{inventory_accno_id})
995 || ($ref->{id} eq $ref->{income_accno_id})
996 || ($ref->{id} eq $ref->{expense_accno_id})) {
997 push @{ $form->{"${module}_links"}{$key} },
998 { accno => $ref->{accno},
999 description => $ref->{description},
1000 selected => "selected",
1003 push @{ $form->{"${module}_links"}{$key} },
1004 { accno => $ref->{accno},
1005 description => $ref->{description},
1017 $main::lxdebug->leave_sub();
1020 sub save_buchungsgruppe {
1021 $main::lxdebug->enter_sub();
1023 my ($self, $myconfig, $form) = @_;
1025 # connect to database
1026 my $dbh = $form->dbconnect($myconfig);
1028 my @values = ($form->{description}, $form->{inventory_accno_id},
1029 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1030 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1031 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1032 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1036 # id is the old record
1038 $query = qq|UPDATE buchungsgruppen SET
1039 description = ?, inventory_accno_id = ?,
1040 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1041 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1042 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1043 income_accno_id_3 = ?, expense_accno_id_3 = ?
1045 push(@values, $form->{id});
1047 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1048 my ($sortkey) = $dbh->selectrow_array($query);
1049 $form->dberror($query) if ($dbh->err);
1050 push(@values, $sortkey);
1051 $query = qq|INSERT INTO buchungsgruppen
1052 (description, inventory_accno_id,
1053 income_accno_id_0, expense_accno_id_0,
1054 income_accno_id_1, expense_accno_id_1,
1055 income_accno_id_2, expense_accno_id_2,
1056 income_accno_id_3, expense_accno_id_3,
1058 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1060 do_query($form, $dbh, $query, @values);
1064 $main::lxdebug->leave_sub();
1067 sub delete_buchungsgruppe {
1068 $main::lxdebug->enter_sub();
1070 my ($self, $myconfig, $form) = @_;
1072 # connect to database
1073 my $dbh = $form->dbconnect($myconfig);
1075 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1076 do_query($form, $dbh, $query, $form->{id});
1080 $main::lxdebug->leave_sub();
1084 $main::lxdebug->enter_sub();
1086 my ($self, $myconfig, $form, $table) = @_;
1088 # connect to database
1089 my $dbh = $form->get_standard_dbh($myconfig);
1093 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1094 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1095 my @values = ($form->{"id1"}, $form->{"id2"});
1096 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1098 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1099 my $sth = prepare_query($form, $dbh, $query);
1101 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1102 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1108 $main::lxdebug->leave_sub();
1112 $main::lxdebug->enter_sub();
1114 my ($self, $myconfig, $form) = @_;
1116 # connect to database
1117 my $dbh = $form->dbconnect($myconfig);
1119 my $query = qq|SELECT id, printer_description, template_code, printer_command
1123 my $sth = $dbh->prepare($query);
1124 $sth->execute || $form->dberror($query);
1126 $form->{"ALL"} = [];
1127 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1128 push @{ $form->{ALL} }, $ref;
1134 $main::lxdebug->leave_sub();
1138 $main::lxdebug->enter_sub();
1140 my ($self, $myconfig, $form) = @_;
1142 # connect to database
1143 my $dbh = $form->dbconnect($myconfig);
1146 qq|SELECT p.printer_description, p.template_code, p.printer_command
1149 my $sth = $dbh->prepare($query);
1150 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1152 my $ref = $sth->fetchrow_hashref("NAME_lc");
1154 map { $form->{$_} = $ref->{$_} } keys %$ref;
1160 $main::lxdebug->leave_sub();
1164 $main::lxdebug->enter_sub();
1166 my ($self, $myconfig, $form) = @_;
1169 # connect to database
1170 my $dbh = $form->dbconnect($myconfig);
1172 my @values = ($form->{printer_description},
1173 $form->{template_code},
1174 $form->{printer_command});
1176 # id is the old record
1178 $query = qq|UPDATE printers SET
1179 printer_description = ?, template_code = ?, printer_command = ?
1181 push(@values, $form->{id});
1183 $query = qq|INSERT INTO printers
1184 (printer_description, template_code, printer_command)
1187 do_query($form, $dbh, $query, @values);
1191 $main::lxdebug->leave_sub();
1194 sub delete_printer {
1195 $main::lxdebug->enter_sub();
1197 my ($self, $myconfig, $form) = @_;
1199 # connect to database
1200 my $dbh = $form->dbconnect($myconfig);
1202 my $query = qq|DELETE FROM printers
1204 do_query($form, $dbh, $query, $form->{id});
1208 $main::lxdebug->leave_sub();
1212 $main::lxdebug->enter_sub();
1214 my ($self, $myconfig, $form) = @_;
1216 # connect to database
1217 my $dbh = $form->dbconnect($myconfig);
1219 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1221 my $sth = $dbh->prepare($query);
1222 $sth->execute || $form->dberror($query);
1225 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1226 push @{ $form->{ALL} }, $ref;
1232 $main::lxdebug->leave_sub();
1236 $main::lxdebug->enter_sub();
1238 my ($self, $myconfig, $form) = @_;
1240 # connect to database
1241 my $dbh = $form->dbconnect($myconfig);
1243 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1244 my $sth = $dbh->prepare($query);
1245 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1247 my $ref = $sth->fetchrow_hashref("NAME_lc");
1248 map { $form->{$_} = $ref->{$_} } keys %$ref;
1252 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1253 qq|FROM translation_payment_terms t | .
1254 qq|LEFT JOIN language l ON t.language_id = l.id | .
1255 qq|WHERE t.payment_terms_id = ? | .
1257 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1258 qq| l.description AS language | .
1259 qq|FROM language l|;
1260 $sth = $dbh->prepare($query);
1261 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1264 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1265 $mapping{ $ref->{"language_id"} } = $ref
1266 unless (defined($mapping{ $ref->{"language_id"} }));
1270 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1275 $main::lxdebug->leave_sub();
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig, $form) = @_;
1283 # connect to database
1284 my $dbh = $form->dbconnect_noauto($myconfig);
1289 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1290 qq|FROM payment_terms|;
1292 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1294 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1295 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1299 qq|DELETE FROM translation_payment_terms | .
1300 qq|WHERE payment_terms_id = ?|;
1301 do_query($form, $dbh, $query, $form->{"id"});
1304 $query = qq|UPDATE payment_terms SET
1305 description = ?, description_long = ?,
1306 terms_netto = ?, terms_skonto = ?,
1309 my @values = ($form->{description}, $form->{description_long},
1310 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1311 $form->{percent_skonto} * 1,
1313 do_query($form, $dbh, $query, @values);
1315 $query = qq|SELECT id FROM language|;
1317 my $sth = $dbh->prepare($query);
1318 $sth->execute() || $form->dberror($query);
1320 while (my ($id) = $sth->fetchrow_array()) {
1321 push(@language_ids, $id);
1326 qq|INSERT INTO translation_payment_terms | .
1327 qq|(language_id, payment_terms_id, description_long) | .
1328 qq|VALUES (?, ?, ?)|;
1329 $sth = $dbh->prepare($query);
1331 foreach my $language_id (@language_ids) {
1332 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1333 $form->{"description_long_${language_id}"});
1340 $main::lxdebug->leave_sub();
1343 sub delete_payment {
1344 $main::lxdebug->enter_sub();
1346 my ($self, $myconfig, $form) = @_;
1348 # connect to database
1349 my $dbh = $form->dbconnect_noauto($myconfig);
1352 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1353 do_query($form, $dbh, $query, $form->{"id"});
1355 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1356 do_query($form, $dbh, $query, $form->{"id"});
1361 $main::lxdebug->leave_sub();
1365 sub prepare_template_filename {
1366 $main::lxdebug->enter_sub();
1368 my ($self, $myconfig, $form) = @_;
1370 my ($filename, $display_filename);
1372 if ($form->{type} eq "stylesheet") {
1373 $filename = "css/$myconfig->{stylesheet}";
1374 $display_filename = $myconfig->{stylesheet};
1377 $filename = $form->{formname};
1379 if ($form->{language}) {
1380 my ($id, $template_code) = split(/--/, $form->{language});
1381 $filename .= "_${template_code}";
1384 if ($form->{printer}) {
1385 my ($id, $template_code) = split(/--/, $form->{printer});
1386 $filename .= "_${template_code}";
1389 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1390 $filename =~ s|.*/||;
1391 $display_filename = $filename;
1392 $filename = "$myconfig->{templates}/$filename";
1395 $main::lxdebug->leave_sub();
1397 return ($filename, $display_filename);
1402 $main::lxdebug->enter_sub();
1404 my ($self, $filename) = @_;
1406 my ($content, $lines) = ("", 0);
1410 if (open(TEMPLATE, $filename)) {
1411 while (<TEMPLATE>) {
1418 $main::lxdebug->leave_sub();
1420 return ($content, $lines);
1424 $main::lxdebug->enter_sub();
1426 my ($self, $filename, $content) = @_;
1432 if (open(TEMPLATE, ">$filename")) {
1433 $content =~ s/\r\n/\n/g;
1434 print(TEMPLATE $content);
1440 $main::lxdebug->leave_sub();
1446 $main::lxdebug->enter_sub();
1451 my $myconfig = \%main::myconfig;
1452 my $form = $main::form;
1454 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1457 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1459 $form->{curr} =~ s/ //g;
1460 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1461 my $currency = join ':', @currencies;
1463 # these defaults are database wide
1466 qq|UPDATE defaults SET
1467 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1468 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1469 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1470 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1471 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1488 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1489 $accnos{fxgain_accno}, $accnos{fxloss_accno},
1490 $form->{invnumber}, $form->{cnnumber},
1491 $form->{sonumber}, $form->{ponumber},
1492 $form->{sqnumber}, $form->{rfqnumber},
1493 $form->{customernumber}, $form->{vendornumber},
1494 $form->{articlenumber}, $form->{servicenumber},
1495 $form->{sdonumber}, $form->{pdonumber},
1496 $form->{yearend}, $currency,
1497 $form->{businessnumber}, $form->{weightunit});
1498 do_query($form, $dbh, $query, @values);
1502 $main::lxdebug->leave_sub();
1506 sub save_preferences {
1507 $main::lxdebug->enter_sub();
1509 my ($self, $myconfig, $form, $webdav) = @_;
1511 my $dbh = $form->get_standard_dbh($myconfig);
1513 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1516 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1517 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1519 my $rc = $dbh->commit();
1521 # save first currency in myconfig
1522 $currency =~ s/:.*//;
1523 $form->{currency} = $currency;
1525 $form->{businessnumber} = $businessnumber;
1527 $myconfig = new User($form->{login});
1529 foreach my $item (keys %$form) {
1530 $myconfig->{$item} = $form->{$item};
1533 $myconfig->save_member($main::memberfile);
1535 my $auth = $main::auth;
1537 if ($auth->can_change_password()
1538 && defined $form->{new_password}
1539 && ($form->{new_password} ne '********')) {
1540 $auth->change_password($form->{login}, $form->{new_password});
1542 $form->{password} = $form->{new_password};
1543 $auth->set_session_value('password', $form->{password});
1544 $auth->create_or_refresh_session();
1549 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1550 foreach my $directory (@webdavdirs) {
1551 my $file = "webdav/" . $directory . "/webdav-user";
1553 if ($myconfig->{$directory}) {
1554 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1555 while (<HTACCESS>) {
1556 my ($login, $password) = split(/:/, $_);
1557 if ($login ne $form->{login}) {
1562 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1563 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1564 print(HTACCESS $newfile);
1567 $form->{$directory} = 0;
1568 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1569 while (<HTACCESS>) {
1570 my ($login, $password) = split(/:/, $_);
1571 if ($login ne $form->{login}) {
1576 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1577 print(HTACCESS $newfile);
1583 $main::lxdebug->leave_sub();
1589 $main::lxdebug->enter_sub();
1594 my $myconfig = \%main::myconfig;
1595 my $form = $main::form;
1597 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1599 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1601 $defaults->{weightunit} ||= 'kg';
1603 $main::lxdebug->leave_sub();
1608 sub defaultaccounts {
1609 $main::lxdebug->enter_sub();
1611 my ($self, $myconfig, $form) = @_;
1613 # connect to database
1614 my $dbh = $form->dbconnect($myconfig);
1616 # get defaults from defaults table
1617 my $query = qq|SELECT * FROM defaults|;
1618 my $sth = $dbh->prepare($query);
1619 $sth->execute || $form->dberror($query);
1621 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1622 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1623 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1624 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1625 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1626 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1628 $form->{defaults}{weightunit} ||= 'kg';
1632 $query = qq|SELECT c.id, c.accno, c.description, c.link
1634 WHERE c.link LIKE '%IC%'
1636 $sth = $dbh->prepare($query);
1637 $sth->execute || $self->dberror($query);
1639 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1640 foreach my $key (split(/:/, $ref->{link})) {
1643 if ($key =~ /cogs/) {
1644 $nkey = "IC_expense";
1646 if ($key =~ /sale/) {
1647 $nkey = "IC_income";
1649 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1651 description => $ref->{description}
1658 $query = qq|SELECT c.id, c.accno, c.description
1660 WHERE c.category = 'I'
1661 AND c.charttype = 'A'
1663 $sth = $dbh->prepare($query);
1664 $sth->execute || $self->dberror($query);
1666 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1667 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1669 description => $ref->{description}
1674 $query = qq|SELECT c.id, c.accno, c.description
1676 WHERE c.category = 'E'
1677 AND c.charttype = 'A'
1679 $sth = $dbh->prepare($query);
1680 $sth->execute || $self->dberror($query);
1682 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1683 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1685 description => $ref->{description}
1690 # now get the tax rates and numbers
1691 $query = qq|SELECT c.id, c.accno, c.description,
1692 t.rate * 100 AS rate, t.taxnumber
1694 WHERE c.id = t.chart_id|;
1696 $sth = $dbh->prepare($query);
1697 $sth->execute || $form->dberror($query);
1699 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1700 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1701 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1702 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1703 if $ref->{taxnumber};
1704 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1710 $main::lxdebug->leave_sub();
1714 $main::lxdebug->enter_sub();
1716 my ($self, $myconfig, $form) = @_;
1718 my $dbh = $form->dbconnect($myconfig);
1720 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1721 my $sth = $dbh->prepare($query);
1722 $sth->execute || $form->dberror($query);
1724 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1730 $main::lxdebug->leave_sub();
1734 $main::lxdebug->enter_sub();
1736 my ($self, $myconfig, $form) = @_;
1738 my $dbh = $form->dbconnect($myconfig);
1740 my ($query, @values);
1742 if ($form->{revtrans}) {
1743 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1745 } elsif ($form->{closedto}) {
1746 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1747 @values = (conv_date($form->{closedto}));
1750 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1753 # set close in defaults
1754 do_query($form, $dbh, $query, @values);
1758 $main::lxdebug->leave_sub();
1762 my ($self, $units, $unit_name, $factor) = @_;
1764 $factor = 1 unless ($factor);
1766 my $unit = $units->{$unit_name};
1768 if (!defined($unit) || !$unit->{"base_unit"} ||
1769 ($unit_name eq $unit->{"base_unit"})) {
1770 return ($unit_name, $factor);
1773 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1776 sub retrieve_units {
1777 $main::lxdebug->enter_sub();
1779 my ($self, $myconfig, $form, $prefix) = @_;
1781 my $dbh = $form->dbconnect($myconfig);
1783 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1785 my $sth = prepare_execute_query($form, $dbh, $query);
1788 while (my $ref = $sth->fetchrow_hashref()) {
1789 $units->{$ref->{"name"}} = $ref;
1793 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1794 $sth = $dbh->prepare($query_lang);
1795 $sth->execute() || $form->dberror($query_lang);
1797 while (my $ref = $sth->fetchrow_hashref()) {
1798 push(@languages, $ref);
1802 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1803 "FROM units_language ul " .
1804 "LEFT JOIN language l ON ul.language_id = l.id " .
1805 "WHERE ul.unit = ?";
1806 $sth = $dbh->prepare($query_lang);
1808 foreach my $unit (values(%{$units})) {
1809 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1811 $unit->{"LANGUAGES"} = {};
1812 foreach my $lang (@languages) {
1813 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1816 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1817 while (my $ref = $sth->fetchrow_hashref()) {
1818 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1825 $main::lxdebug->leave_sub();
1830 sub retrieve_all_units {
1831 $main::lxdebug->enter_sub();
1835 if (!$main::all_units) {
1836 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1839 $main::lxdebug->leave_sub();
1841 return $main::all_units;
1845 sub translate_units {
1846 $main::lxdebug->enter_sub();
1848 my ($self, $form, $template_code, $unit, $amount) = @_;
1850 my $units = $self->retrieve_units(\%main::myconfig, $form);
1852 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1853 my $new_unit = $unit;
1855 if (($amount != 1) && $h->{"localized_plural"}) {
1856 $new_unit = $h->{"localized_plural"};
1857 } elsif ($h->{"localized"}) {
1858 $new_unit = $h->{"localized"};
1862 $main::lxdebug->leave_sub();
1868 $main::lxdebug->enter_sub();
1870 my ($self, $myconfig, $form, $units) = @_;
1872 my $dbh = $form->dbconnect($myconfig);
1874 map({ $_->{"in_use"} = 0; } values(%{$units}));
1876 foreach my $unit (values(%{$units})) {
1877 my $base_unit = $unit->{"original_base_unit"};
1878 while ($base_unit) {
1879 $units->{$base_unit}->{"in_use"} = 1;
1880 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1881 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1882 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1886 foreach my $unit (values(%{$units})) {
1887 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1889 foreach my $table (qw(parts invoice orderitems)) {
1890 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1892 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1893 $query .= "= " . $dbh->quote($unit->{"name"});
1895 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1896 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1899 my ($count) = $dbh->selectrow_array($query);
1900 $form->dberror($query) if ($dbh->err);
1903 $unit->{"in_use"} = 1;
1911 $main::lxdebug->leave_sub();
1914 sub convertible_units {
1915 $main::lxdebug->enter_sub();
1919 my $filter_unit = shift;
1920 my $not_smaller = shift;
1922 my $conv_units = [];
1924 $filter_unit = $units->{$filter_unit};
1926 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1927 my $unit = $units->{$name};
1929 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1930 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1931 push @{$conv_units}, $unit;
1935 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1937 $main::lxdebug->leave_sub();
1942 # if $a is translatable to $b, return the factor between them.
1945 $main::lxdebug->enter_sub(2);
1946 my ($this, $a, $b, $all_units) = @_;
1948 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1949 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1950 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1951 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1954 sub unit_select_data {
1955 $main::lxdebug->enter_sub();
1957 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1962 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1965 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1966 if (!$convertible_into ||
1967 ($units->{$convertible_into} &&
1968 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1969 push @{$select}, { "name" => $unit,
1970 "base_unit" => $units->{$unit}->{"base_unit"},
1971 "factor" => $units->{$unit}->{"factor"},
1972 "selected" => ($unit eq $selected) ? "selected" : "" };
1976 $main::lxdebug->leave_sub();
1981 sub unit_select_html {
1982 $main::lxdebug->enter_sub();
1984 my ($self, $units, $name, $selected, $convertible_into) = @_;
1986 my $select = "<select name=${name}>";
1988 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1989 if (!$convertible_into ||
1990 ($units->{$convertible_into} &&
1991 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1992 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1995 $select .= "</select>";
1997 $main::lxdebug->leave_sub();
2003 $main::lxdebug->enter_sub();
2007 my $units = $self->retrieve_all_units();
2012 while (2 <= scalar(@_)) {
2013 my $qty = shift(@_);
2014 my $unit = $units->{shift(@_)};
2016 croak "No unit defined with name $unit" if (!defined $unit);
2019 $base_unit = $unit->{base_unit};
2020 } elsif ($base_unit ne $unit->{base_unit}) {
2021 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
2024 $sum += $qty * $unit->{factor};
2027 $main::lxdebug->leave_sub();
2029 return wantarray ? ($sum, $base_unit) : $sum;
2033 $main::lxdebug->enter_sub();
2035 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
2037 my $dbh = $form->dbconnect_noauto($myconfig);
2039 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2040 my ($sortkey) = selectrow_query($form, $dbh, $query);
2042 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
2043 "VALUES (?, ?, ?, ?)";
2044 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
2047 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2048 my $sth = $dbh->prepare($query);
2049 foreach my $lang (@{$languages}) {
2050 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2051 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2059 $main::lxdebug->leave_sub();
2063 $main::lxdebug->enter_sub();
2065 my ($self, $myconfig, $form, $units, $delete_units) = @_;
2067 my $dbh = $form->dbconnect_noauto($myconfig);
2069 my ($base_unit, $unit, $sth, $query);
2071 $query = "DELETE FROM units_language";
2072 $dbh->do($query) || $form->dberror($query);
2074 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2075 $query = "DELETE FROM units WHERE name IN (";
2076 map({ $query .= "?," } @{$delete_units});
2077 substr($query, -1, 1) = ")";
2078 $dbh->do($query, undef, @{$delete_units}) ||
2079 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2082 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2083 $sth = $dbh->prepare($query);
2085 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2086 my $sth_lang = $dbh->prepare($query_lang);
2088 foreach $unit (values(%{$units})) {
2089 $unit->{"depth"} = 0;
2090 my $base_unit = $unit;
2091 while ($base_unit->{"base_unit"}) {
2093 $base_unit = $units->{$base_unit->{"base_unit"}};
2097 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2098 if ($unit->{"LANGUAGES"}) {
2099 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2100 next unless ($lang->{"id"} && $lang->{"localized"});
2101 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2102 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2106 next if ($unit->{"unchanged_unit"});
2108 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2109 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2113 $sth_lang->finish();
2117 $main::lxdebug->leave_sub();
2121 $main::lxdebug->enter_sub();
2123 my ($self, $myconfig, $form, $dir, $name_1) = @_;
2125 my $dbh = $form->dbconnect_noauto($myconfig);
2129 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2130 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2133 qq|SELECT sortkey FROM units | .
2134 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
2135 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2136 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
2138 if (defined($sortkey_1)) {
2139 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2140 my ($name_2) = selectrow_query($form, $dbh, $query);
2142 if (defined($name_2)) {
2143 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2144 my $sth = $dbh->prepare($query);
2146 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2147 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2154 $main::lxdebug->leave_sub();
2158 $main::lxdebug->enter_sub();
2160 my ($self, $myconfig, $form) = @_;
2162 # connect to database
2163 my $dbh = $form->dbconnect($myconfig);
2165 my $query = qq|SELECT
2169 round(t.rate * 100, 2) AS rate,
2170 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
2171 (SELECT description FROM chart WHERE id = chart_id) AS account_description
2175 my $sth = $dbh->prepare($query);
2176 $sth->execute || $form->dberror($query);
2179 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2180 push @{ $form->{TAX} }, $ref;
2186 $main::lxdebug->leave_sub();
2189 sub get_tax_accounts {
2190 $main::lxdebug->enter_sub();
2192 my ($self, $myconfig, $form) = @_;
2194 my $dbh = $form->dbconnect($myconfig);
2196 # get Accounts from chart
2197 my $query = qq{ SELECT
2199 accno || ' - ' || description AS taxaccount
2201 WHERE link LIKE '%_tax%'
2205 my $sth = $dbh->prepare($query);
2206 $sth->execute || $form->dberror($query);
2208 $form->{ACCOUNTS} = [];
2209 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2210 push @{ $form->{ACCOUNTS} }, $ref;
2217 $main::lxdebug->leave_sub();
2221 $main::lxdebug->enter_sub();
2223 my ($self, $myconfig, $form) = @_;
2225 # connect to database
2226 my $dbh = $form->dbconnect($myconfig);
2228 my $query = qq|SELECT
2231 round(rate * 100, 2) AS rate,
2236 my $sth = $dbh->prepare($query);
2237 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2239 my $ref = $sth->fetchrow_hashref("NAME_lc");
2241 map { $form->{$_} = $ref->{$_} } keys %$ref;
2245 # see if it is used by a taxkey
2246 $query = qq|SELECT count(*) FROM taxkeys
2247 WHERE tax_id = ? AND chart_id >0|;
2249 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
2251 $form->{orphaned} = !$form->{orphaned};
2254 if (!$form->{orphaned} ) {
2255 $query = qq|SELECT DISTINCT c.id, c.accno
2257 JOIN tax t ON (t.id = tk.tax_id)
2258 JOIN chart c ON (c.id = tk.chart_id)
2259 WHERE tk.tax_id = ?|;
2261 $sth = $dbh->prepare($query);
2262 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
2264 $form->{TAXINUSE} = [];
2265 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2266 push @{ $form->{TAXINUSE} }, $ref;
2274 $main::lxdebug->leave_sub();
2278 $main::lxdebug->enter_sub();
2280 my ($self, $myconfig, $form) = @_;
2283 # connect to database
2284 my $dbh = $form->get_standard_dbh($myconfig);
2286 $form->{rate} = $form->{rate} / 100;
2288 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
2289 if ($form->{id} ne "") {
2290 $query = qq|UPDATE tax SET
2295 taxnumber = (SELECT accno FROM chart WHERE id= ? )
2297 push(@values, $form->{id});
2301 $query = qq|INSERT INTO tax (
2308 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2310 do_query($form, $dbh, $query, @values);
2314 $main::lxdebug->leave_sub();
2318 $main::lxdebug->enter_sub();
2320 my ($self, $myconfig, $form) = @_;
2323 # connect to database
2324 my $dbh = $form->get_standard_dbh($myconfig);
2326 $query = qq|DELETE FROM tax
2328 do_query($form, $dbh, $query, $form->{id});
2332 $main::lxdebug->leave_sub();
2335 sub save_price_factor {
2336 $main::lxdebug->enter_sub();
2338 my ($self, $myconfig, $form) = @_;
2340 # connect to database
2341 my $dbh = $form->get_standard_dbh($myconfig);
2344 my @values = ($form->{description}, conv_i($form->{factor}));
2347 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2348 push @values, conv_i($form->{id});
2351 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2354 do_query($form, $dbh, $query, @values);
2358 $main::lxdebug->leave_sub();
2361 sub get_all_price_factors {
2362 $main::lxdebug->enter_sub();
2364 my ($self, $myconfig, $form) = @_;
2366 # connect to database
2367 my $dbh = $form->get_standard_dbh($myconfig);
2369 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2371 $main::lxdebug->leave_sub();
2374 sub get_price_factor {
2375 $main::lxdebug->enter_sub();
2377 my ($self, $myconfig, $form) = @_;
2379 # connect to database
2380 my $dbh = $form->get_standard_dbh($myconfig);
2382 my $query = qq|SELECT description, factor,
2383 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2384 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2385 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2386 FROM price_factors WHERE id = ?|;
2388 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2390 $main::lxdebug->leave_sub();
2393 sub delete_price_factor {
2394 $main::lxdebug->enter_sub();
2396 my ($self, $myconfig, $form) = @_;
2398 # connect to database
2399 my $dbh = $form->get_standard_dbh($myconfig);
2401 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2404 $main::lxdebug->leave_sub();
2407 sub save_warehouse {
2408 $main::lxdebug->enter_sub();
2410 my ($self, $myconfig, $form) = @_;
2412 # connect to database
2413 my $dbh = $form->get_standard_dbh($myconfig);
2415 my ($query, @values, $sth);
2418 $query = qq|SELECT nextval('id')|;
2419 ($form->{id}) = selectrow_query($form, $dbh, $query);
2421 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2422 do_query($form, $dbh, $query, $form->{id});
2425 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2426 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2428 if (0 < $form->{number_of_new_bins}) {
2429 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2430 $sth = prepare_query($form, $dbh, $query);
2432 foreach my $i (1..$form->{number_of_new_bins}) {
2433 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2441 $main::lxdebug->leave_sub();
2445 $main::lxdebug->enter_sub();
2447 my ($self, $myconfig, $form) = @_;
2449 # connect to database
2450 my $dbh = $form->get_standard_dbh($myconfig);
2452 my ($query, @values, $commit_necessary, $sth);
2454 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2457 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2458 do_query($form, $dbh, $query, @values);
2460 $commit_necessary = 1;
2463 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2464 $sth = prepare_query($form, $dbh, $query);
2466 foreach my $row (1..$form->{rowcount}) {
2467 next if ($form->{"delete_${row}"});
2469 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2471 $commit_necessary = 1;
2476 $dbh->commit() if ($commit_necessary);
2478 $main::lxdebug->leave_sub();
2481 sub delete_warehouse {
2482 $main::lxdebug->enter_sub();
2484 my ($self, $myconfig, $form) = @_;
2486 # connect to database
2487 my $dbh = $form->get_standard_dbh($myconfig);
2489 my $id = conv_i($form->{id});
2490 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|;
2491 my ($count) = selectrow_query($form, $dbh, $query, $id);
2494 $main::lxdebug->leave_sub();
2498 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2499 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2503 $main::lxdebug->leave_sub();
2508 sub get_all_warehouses {
2509 $main::lxdebug->enter_sub();
2511 my ($self, $myconfig, $form) = @_;
2513 # connect to database
2514 my $dbh = $form->get_standard_dbh($myconfig);
2516 my $query = qq|SELECT w.id, w.description, w.invalid,
2517 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2519 ORDER BY w.sortkey|;
2521 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2523 $main::lxdebug->leave_sub();
2527 $main::lxdebug->enter_sub();
2529 my ($self, $myconfig, $form) = @_;
2531 # connect to database
2532 my $dbh = $form->get_standard_dbh($myconfig);
2534 my $id = conv_i($form->{id});
2535 my $query = qq|SELECT w.description, w.invalid
2539 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2541 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2543 $query = qq|SELECT b.*, EXISTS
2544 (SELECT i.warehouse_id
2546 WHERE i.bin_id = b.id
2550 WHERE b.warehouse_id = ?|;
2552 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2554 $main::lxdebug->leave_sub();