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 my ($self, $myconfig, $form) = @_;
204 # connect to database, turn off AutoCommit
205 my $dbh = $form->dbconnect_noauto($myconfig);
207 # sanity check, can't have AR with AR_...
208 if ($form->{AR} || $form->{AP} || $form->{IC}) {
209 map { delete $form->{$_} }
210 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);
214 foreach my $item ($form->{AR}, $form->{AR_amount},
215 $form->{AR_tax}, $form->{AR_paid},
216 $form->{AP}, $form->{AP_amount},
217 $form->{AP_tax}, $form->{AP_paid},
218 $form->{IC}, $form->{IC_sale},
219 $form->{IC_cogs}, $form->{IC_taxpart},
220 $form->{IC_income}, $form->{IC_expense},
221 $form->{IC_taxservice}, $form->{CT_tax}
223 $form->{link} .= "${item}:" if ($item);
227 # strip blanks from accno
228 map { $form->{$_} =~ s/ //g; } qw(accno);
232 if ($form->{id} eq "NULL") {
236 if (!$form->{id} || $form->{id} eq "") {
237 $query = qq|SELECT nextval('id')|;
238 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
239 $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
240 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
246 $query = qq|UPDATE chart SET
262 $form->{description},
266 conv_i($form->{pos_bwa}),
267 conv_i($form->{pos_bilanz}),
268 conv_i($form->{pos_eur}),
269 conv_i($form->{new_chart_id}),
270 conv_date($form->{valid_from}),
271 ($form->{datevautomatik} eq 'T') ? 'true':'false',
277 do_query($form, $dbh, $query, @values);
283 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
287 for $tk_count (0 .. $MAX_TRIES) {
291 # Check if the account already exists, else cancel
293 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
295 last READTAXKEYS if ( $form->{'id'} == 0);
297 # check if there is a startdate
298 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
303 # Add valid taxkeys into the array
306 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
307 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
308 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
309 chart_id => conv_i($form->{"id"}),
310 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
311 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
318 for my $j (0 .. $#taxkeys){
319 if ( defined $taxkeys[$j]{'id'} ){
322 if ($taxkeys[$j]{'delete'}){
324 DELETE FROM taxkeys WHERE id = ?
327 @values = ($taxkeys[$j]{'id'});
329 do_query($form, $dbh, $query, @values);
338 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
346 $taxkeys[$j]{'tax_id'},
347 $taxkeys[$j]{'chart_id'},
348 $taxkeys[$j]{'tax_id'},
349 $taxkeys[$j]{'pos_ustva'},
350 $taxkeys[$j]{'startdate'},
353 do_query($form, $dbh, $query, @values);
359 INSERT INTO taxkeys (
366 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
369 $taxkeys[$j]{'tax_id'},
370 $taxkeys[$j]{'chart_id'},
371 $taxkeys[$j]{'tax_id'},
372 $taxkeys[$j]{'pos_ustva'},
373 $taxkeys[$j]{'startdate'},
376 do_query($form, $dbh, $query, @values);
381 # Update chart.taxkey_id to the latest from taxkeys for this chart.
387 WHERE taxkeys.chart_id = chart.id
388 ORDER BY startdate DESC
394 do_query($form, $dbh, $query, $form->{id});
397 my $rc = $dbh->commit;
400 $main::lxdebug->leave_sub();
406 $main::lxdebug->enter_sub();
408 my ($self, $myconfig, $form) = @_;
410 # connect to database, turn off AutoCommit
411 my $dbh = $form->dbconnect_noauto($myconfig);
413 my $query = qq|SELECT count(*) FROM acc_trans a
414 WHERE a.chart_id = ?|;
415 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
419 $main::lxdebug->leave_sub();
423 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
424 foreach my $type (qw(inventory income expense)) {
427 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
428 qq|WHERE ${type}_accno_id = ?|;
429 do_query($form, $dbh, $query, $form->{id});
432 foreach my $table (qw(partstax customertax vendortax tax)) {
433 $query = qq|DELETE FROM $table
435 do_query($form, $dbh, $query, $form->{id});
438 # delete chart of account record
439 $query = qq|DELETE FROM chart
441 do_query($form, $dbh, $query, $form->{id});
443 # delete account taxkeys
444 $query = qq|DELETE FROM taxkeys
446 do_query($form, $dbh, $query, $form->{id});
448 # commit and redirect
449 my $rc = $dbh->commit;
452 $main::lxdebug->leave_sub();
458 $main::lxdebug->enter_sub();
460 my ($self, $myconfig, $form) = @_;
462 # connect to database
463 my $dbh = $form->dbconnect($myconfig);
465 my $query = qq|SELECT d.id, d.description, d.role
469 my $sth = $dbh->prepare($query);
470 $sth->execute || $form->dberror($query);
473 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
474 push @{ $form->{ALL} }, $ref;
480 $main::lxdebug->leave_sub();
484 $main::lxdebug->enter_sub();
486 my ($self, $myconfig, $form) = @_;
488 # connect to database
489 my $dbh = $form->dbconnect($myconfig);
491 my $query = qq|SELECT d.description, d.role
494 my $sth = $dbh->prepare($query);
495 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
497 my $ref = $sth->fetchrow_hashref("NAME_lc");
499 map { $form->{$_} = $ref->{$_} } keys %$ref;
503 # see if it is in use
504 $query = qq|SELECT count(*) FROM dpt_trans d
505 WHERE d.department_id = ?|;
506 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
508 $form->{orphaned} = !$form->{orphaned};
513 $main::lxdebug->leave_sub();
516 sub save_department {
517 $main::lxdebug->enter_sub();
519 my ($self, $myconfig, $form) = @_;
522 # connect to database
523 my $dbh = $form->dbconnect($myconfig);
525 my @values = ($form->{description}, $form->{role});
527 $query = qq|UPDATE department SET
528 description = ?, role = ?
530 push(@values, $form->{id});
532 $query = qq|INSERT INTO department
536 do_query($form, $dbh, $query, @values);
540 $main::lxdebug->leave_sub();
543 sub delete_department {
544 $main::lxdebug->enter_sub();
546 my ($self, $myconfig, $form) = @_;
549 # connect to database
550 my $dbh = $form->dbconnect($myconfig);
552 $query = qq|DELETE FROM department
554 do_query($form, $dbh, $query, $form->{id});
558 $main::lxdebug->leave_sub();
562 $main::lxdebug->enter_sub();
564 my ($self, $myconfig, $form) = @_;
566 # connect to database
567 my $dbh = $form->dbconnect($myconfig);
569 my $query = qq|SELECT id, lead
573 my $sth = $dbh->prepare($query);
574 $sth->execute || $form->dberror($query);
576 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
577 push @{ $form->{ALL} }, $ref;
583 $main::lxdebug->leave_sub();
587 $main::lxdebug->enter_sub();
589 my ($self, $myconfig, $form) = @_;
591 # connect to database
592 my $dbh = $form->dbconnect($myconfig);
595 qq|SELECT l.id, l.lead | .
598 my $sth = $dbh->prepare($query);
599 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
601 my $ref = $sth->fetchrow_hashref("NAME_lc");
603 map { $form->{$_} = $ref->{$_} } keys %$ref;
609 $main::lxdebug->leave_sub();
613 $main::lxdebug->enter_sub();
615 my ($self, $myconfig, $form) = @_;
618 # connect to database
619 my $dbh = $form->dbconnect($myconfig);
621 my @values = ($form->{description});
622 # id is the old record
624 $query = qq|UPDATE leads SET
627 puhs(@values, $form->{id});
629 $query = qq|INSERT INTO leads
633 do_query($form, $dbh, $query, @values);
637 $main::lxdebug->leave_sub();
641 $main::lxdebug->enter_sub();
643 my ($self, $myconfig, $form) = @_;
646 # connect to database
647 my $dbh = $form->dbconnect($myconfig);
649 $query = qq|DELETE FROM leads
651 do_query($form, $dbh, $query, $form->{id});
655 $main::lxdebug->leave_sub();
659 $main::lxdebug->enter_sub();
661 my ($self, $myconfig, $form) = @_;
663 # connect to database
664 my $dbh = $form->dbconnect($myconfig);
666 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
670 my $sth = $dbh->prepare($query);
671 $sth->execute || $form->dberror($query);
673 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
674 push @{ $form->{ALL} }, $ref;
680 $main::lxdebug->leave_sub();
684 $main::lxdebug->enter_sub();
686 my ($self, $myconfig, $form) = @_;
688 # connect to database
689 my $dbh = $form->dbconnect($myconfig);
692 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
695 my $sth = $dbh->prepare($query);
696 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
698 my $ref = $sth->fetchrow_hashref("NAME_lc");
700 map { $form->{$_} = $ref->{$_} } keys %$ref;
706 $main::lxdebug->leave_sub();
710 $main::lxdebug->enter_sub();
712 my ($self, $myconfig, $form) = @_;
715 # connect to database
716 my $dbh = $form->dbconnect($myconfig);
718 my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
719 # id is the old record
721 $query = qq|UPDATE business SET
724 customernumberinit = ?,
727 push(@values, $form->{id});
729 $query = qq|INSERT INTO business
730 (description, discount, customernumberinit, salesman)
731 VALUES (?, ?, ?, ?)|;
733 do_query($form, $dbh, $query, @values);
737 $main::lxdebug->leave_sub();
740 sub delete_business {
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $form) = @_;
745 # connect to database
746 my $dbh = $form->dbconnect($myconfig);
748 my $query = qq|DELETE FROM business
750 do_query($form, $dbh, $query, $form->{id});
754 $main::lxdebug->leave_sub();
759 $main::lxdebug->enter_sub();
761 my ($self, $myconfig, $form, $return_list) = @_;
763 # connect to database
764 my $dbh = $form->dbconnect($myconfig);
767 "SELECT id, description, template_code, article_code, " .
768 " output_numberformat, output_dateformat, output_longdates " .
769 "FROM language ORDER BY description";
771 my $sth = $dbh->prepare($query);
772 $sth->execute || $form->dberror($query);
776 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
777 push(@{ $ary }, $ref);
783 $main::lxdebug->leave_sub();
793 $main::lxdebug->enter_sub();
795 my ($self, $myconfig, $form) = @_;
797 # connect to database
798 my $dbh = $form->dbconnect($myconfig);
801 "SELECT description, template_code, article_code, " .
802 " output_numberformat, output_dateformat, output_longdates " .
803 "FROM language WHERE id = ?";
804 my $sth = $dbh->prepare($query);
805 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
807 my $ref = $sth->fetchrow_hashref("NAME_lc");
809 map { $form->{$_} = $ref->{$_} } keys %$ref;
815 $main::lxdebug->leave_sub();
818 sub get_language_details {
819 $main::lxdebug->enter_sub();
821 my ($self, $myconfig, $form, $id) = @_;
823 # connect to database
824 my $dbh = $form->dbconnect($myconfig);
827 "SELECT template_code, " .
828 " output_numberformat, output_dateformat, output_longdates " .
829 "FROM language WHERE id = ?";
830 my @res = selectrow_query($form, $dbh, $query, $id);
833 $main::lxdebug->leave_sub();
839 $main::lxdebug->enter_sub();
841 my ($self, $myconfig, $form) = @_;
843 # connect to database
844 my $dbh = $form->dbconnect($myconfig);
845 my (@values, $query);
847 map({ push(@values, $form->{$_}); }
848 qw(description template_code article_code
849 output_numberformat output_dateformat output_longdates));
851 # id is the old record
854 "UPDATE language SET " .
855 " description = ?, template_code = ?, article_code = ?, " .
856 " output_numberformat = ?, output_dateformat = ?, " .
857 " output_longdates = ? " .
859 push(@values, $form->{id});
862 "INSERT INTO language (" .
863 " description, template_code, article_code, " .
864 " output_numberformat, output_dateformat, output_longdates" .
865 ") VALUES (?, ?, ?, ?, ?, ?)";
867 do_query($form, $dbh, $query, @values);
871 $main::lxdebug->leave_sub();
874 sub delete_language {
875 $main::lxdebug->enter_sub();
877 my ($self, $myconfig, $form) = @_;
880 # connect to database
881 my $dbh = $form->dbconnect_noauto($myconfig);
883 foreach my $table (qw(generic_translations units_language)) {
884 $query = qq|DELETE FROM $table WHERE language_id = ?|;
885 do_query($form, $dbh, $query, $form->{"id"});
888 $query = "DELETE FROM language WHERE id = ?";
889 do_query($form, $dbh, $query, $form->{"id"});
894 $main::lxdebug->leave_sub();
899 $main::lxdebug->enter_sub();
901 my ($self, $myconfig, $form) = @_;
903 # connect to database
904 my $dbh = $form->dbconnect($myconfig);
906 my $query = qq|SELECT id, description,
908 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
910 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
912 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
914 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
916 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
918 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
920 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
922 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
924 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
928 my $sth = $dbh->prepare($query);
929 $sth->execute || $form->dberror($query);
932 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
933 push @{ $form->{ALL} }, $ref;
939 $main::lxdebug->leave_sub();
942 sub get_buchungsgruppe {
943 $main::lxdebug->enter_sub();
945 my ($self, $myconfig, $form) = @_;
948 # connect to database
949 my $dbh = $form->dbconnect($myconfig);
953 qq|SELECT description, inventory_accno_id,
954 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
956 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
958 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
960 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
962 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
964 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
966 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
968 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
970 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
973 my $sth = $dbh->prepare($query);
974 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
976 my $ref = $sth->fetchrow_hashref("NAME_lc");
978 map { $form->{$_} = $ref->{$_} } keys %$ref;
983 qq|SELECT count(id) = 0 AS orphaned
985 WHERE buchungsgruppen_id = ?|;
986 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
989 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
991 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
992 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
995 $query = qq|SELECT c.accno, c.description, c.link, c.id,
996 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
997 FROM chart c, defaults d
998 WHERE c.link LIKE '%$module%'
1002 my $sth = $dbh->prepare($query);
1003 $sth->execute || $form->dberror($query);
1004 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1005 foreach my $key (split(/:/, $ref->{link})) {
1006 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1007 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1009 if ($key =~ /$module/) {
1010 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1011 || ($ref->{id} eq $ref->{income_accno_id})
1012 || ($ref->{id} eq $ref->{expense_accno_id})) {
1013 push @{ $form->{"${module}_links"}{$key} },
1014 { accno => $ref->{accno},
1015 description => $ref->{description},
1016 selected => "selected",
1019 push @{ $form->{"${module}_links"}{$key} },
1020 { accno => $ref->{accno},
1021 description => $ref->{description},
1033 $main::lxdebug->leave_sub();
1036 sub save_buchungsgruppe {
1037 $main::lxdebug->enter_sub();
1039 my ($self, $myconfig, $form) = @_;
1041 # connect to database
1042 my $dbh = $form->dbconnect($myconfig);
1044 my @values = ($form->{description}, $form->{inventory_accno_id},
1045 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
1046 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
1047 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
1048 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
1052 # id is the old record
1054 $query = qq|UPDATE buchungsgruppen SET
1055 description = ?, inventory_accno_id = ?,
1056 income_accno_id_0 = ?, expense_accno_id_0 = ?,
1057 income_accno_id_1 = ?, expense_accno_id_1 = ?,
1058 income_accno_id_2 = ?, expense_accno_id_2 = ?,
1059 income_accno_id_3 = ?, expense_accno_id_3 = ?
1061 push(@values, $form->{id});
1063 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
1064 my ($sortkey) = $dbh->selectrow_array($query);
1065 $form->dberror($query) if ($dbh->err);
1066 push(@values, $sortkey);
1067 $query = qq|INSERT INTO buchungsgruppen
1068 (description, inventory_accno_id,
1069 income_accno_id_0, expense_accno_id_0,
1070 income_accno_id_1, expense_accno_id_1,
1071 income_accno_id_2, expense_accno_id_2,
1072 income_accno_id_3, expense_accno_id_3,
1074 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1076 do_query($form, $dbh, $query, @values);
1080 $main::lxdebug->leave_sub();
1083 sub delete_buchungsgruppe {
1084 $main::lxdebug->enter_sub();
1086 my ($self, $myconfig, $form) = @_;
1088 # connect to database
1089 my $dbh = $form->dbconnect($myconfig);
1091 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
1092 do_query($form, $dbh, $query, $form->{id});
1096 $main::lxdebug->leave_sub();
1100 $main::lxdebug->enter_sub();
1102 my ($self, $myconfig, $form, $table) = @_;
1104 # connect to database
1105 my $dbh = $form->get_standard_dbh($myconfig);
1109 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
1110 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
1111 my @values = ($form->{"id1"}, $form->{"id2"});
1112 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
1114 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
1115 my $sth = prepare_query($form, $dbh, $query);
1117 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
1118 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
1124 $main::lxdebug->leave_sub();
1127 sub prepare_template_filename {
1128 $main::lxdebug->enter_sub();
1130 my ($self, $myconfig, $form) = @_;
1132 my ($filename, $display_filename);
1134 if ($form->{type} eq "stylesheet") {
1135 $filename = "css/$myconfig->{stylesheet}";
1136 $display_filename = $myconfig->{stylesheet};
1139 $filename = $form->{formname};
1141 if ($form->{language}) {
1142 my ($id, $template_code) = split(/--/, $form->{language});
1143 $filename .= "_${template_code}";
1146 if ($form->{printer}) {
1147 my ($id, $template_code) = split(/--/, $form->{printer});
1148 $filename .= "_${template_code}";
1151 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
1152 $filename =~ s|.*/||;
1153 $display_filename = $filename;
1154 $filename = "$myconfig->{templates}/$filename";
1157 $main::lxdebug->leave_sub();
1159 return ($filename, $display_filename);
1164 $main::lxdebug->enter_sub();
1166 my ($self, $filename) = @_;
1168 my ($content, $lines) = ("", 0);
1172 if (open(TEMPLATE, $filename)) {
1173 while (<TEMPLATE>) {
1180 $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1182 $main::lxdebug->leave_sub();
1184 return ($content, $lines);
1188 $main::lxdebug->enter_sub();
1190 my ($self, $filename, $content) = @_;
1196 if (open(TEMPLATE, ">$filename")) {
1197 $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1198 $content =~ s/\r\n/\n/g;
1199 print(TEMPLATE $content);
1205 $main::lxdebug->leave_sub();
1211 $main::lxdebug->enter_sub();
1216 my $myconfig = \%main::myconfig;
1217 my $form = $main::form;
1219 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1222 map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1224 $form->{curr} =~ s/ //g;
1225 my @currencies = grep { $_ ne '' } split m/:/, $form->{curr};
1226 my $currency = join ':', @currencies;
1228 # these defaults are database wide
1231 qq|UPDATE defaults SET
1232 inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1233 income_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1234 expense_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1235 fxgain_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1236 fxloss_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1237 ar_paid_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1253 my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1254 $accnos{fxgain_accno}, $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1255 $form->{invnumber}, $form->{cnnumber},
1256 $form->{sonumber}, $form->{ponumber},
1257 $form->{sqnumber}, $form->{rfqnumber},
1258 $form->{customernumber}, $form->{vendornumber},
1259 $form->{articlenumber}, $form->{servicenumber},
1260 $form->{sdonumber}, $form->{pdonumber},
1262 $form->{businessnumber}, $form->{weightunit});
1263 do_query($form, $dbh, $query, @values);
1267 $main::lxdebug->leave_sub();
1271 sub save_preferences {
1272 $main::lxdebug->enter_sub();
1274 my ($self, $myconfig, $form) = @_;
1276 my $dbh = $form->get_standard_dbh($myconfig);
1278 my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1281 my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1282 do_query($form, $dbh, $query, $form->{name}, $form->{login});
1284 my $rc = $dbh->commit();
1286 # save first currency in myconfig
1287 $currency =~ s/:.*//;
1288 $form->{currency} = $currency;
1290 $form->{businessnumber} = $businessnumber;
1292 $myconfig = new User($form->{login});
1294 foreach my $item (keys %$form) {
1295 $myconfig->{$item} = $form->{$item};
1298 $myconfig->save_member;
1300 my $auth = $main::auth;
1302 $main::lxdebug->leave_sub();
1308 $main::lxdebug->enter_sub();
1313 my $myconfig = \%main::myconfig;
1314 my $form = $main::form;
1316 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1318 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1320 $defaults->{weightunit} ||= 'kg';
1322 $main::lxdebug->leave_sub();
1327 sub defaultaccounts {
1328 $main::lxdebug->enter_sub();
1330 my ($self, $myconfig, $form) = @_;
1332 # connect to database
1333 my $dbh = $form->dbconnect($myconfig);
1335 # get defaults from defaults table
1336 my $query = qq|SELECT * FROM defaults|;
1337 my $sth = $dbh->prepare($query);
1338 $sth->execute || $form->dberror($query);
1340 $form->{defaults} = $sth->fetchrow_hashref("NAME_lc");
1341 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1342 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1343 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1344 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1345 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1346 $form->{defaults}{AR_paid} = $form->{defaults}{ar_paid_accno_id};
1348 $form->{defaults}{weightunit} ||= 'kg';
1352 $query = qq|SELECT c.id, c.accno, c.description, c.link
1354 WHERE c.link LIKE '%IC%'
1356 $sth = $dbh->prepare($query);
1357 $sth->execute || $self->dberror($query);
1359 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1360 foreach my $key (split(/:/, $ref->{link})) {
1363 if ($key =~ /cogs/) {
1364 $nkey = "IC_expense";
1366 if ($key =~ /sale/) {
1367 $nkey = "IC_income";
1369 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1371 description => $ref->{description}
1378 $query = qq|SELECT c.id, c.accno, c.description
1380 WHERE c.category = 'I'
1381 AND c.charttype = 'A'
1383 $sth = $dbh->prepare($query);
1384 $sth->execute || $self->dberror($query);
1386 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1387 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1389 description => $ref->{description}
1394 $query = qq|SELECT c.id, c.accno, c.description
1396 WHERE c.category = 'E'
1397 AND c.charttype = 'A'
1399 $sth = $dbh->prepare($query);
1400 $sth->execute || $self->dberror($query);
1402 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1403 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1405 description => $ref->{description}
1410 # now get the tax rates and numbers
1411 $query = qq|SELECT c.id, c.accno, c.description,
1412 t.rate * 100 AS rate, t.taxnumber
1414 WHERE c.id = t.chart_id|;
1416 $sth = $dbh->prepare($query);
1417 $sth->execute || $form->dberror($query);
1419 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1420 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1421 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1422 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1423 if $ref->{taxnumber};
1424 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1426 # Abfrage für Standard Umlaufvermögenskonto
1428 qq|SELECT id, accno, description, link | .
1430 qq|WHERE link LIKE ? |.
1432 $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1433 $sth->execute || $form->dberror($query);#
1434 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1435 foreach my $item (split(/:/, $ref->{link})) {
1436 if ($item eq "AR_paid") {
1437 %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1439 description => $ref->{description}
1448 $main::lxdebug->leave_sub();
1452 $main::lxdebug->enter_sub();
1454 my ($self, $myconfig, $form) = @_;
1456 my $dbh = $form->dbconnect($myconfig);
1458 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1459 my $sth = $dbh->prepare($query);
1460 $sth->execute || $form->dberror($query);
1462 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1468 $main::lxdebug->leave_sub();
1472 $main::lxdebug->enter_sub();
1474 my ($self, $myconfig, $form) = @_;
1476 my $dbh = $form->dbconnect($myconfig);
1478 my ($query, @values);
1480 if ($form->{revtrans}) {
1481 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1483 } elsif ($form->{closedto}) {
1484 $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1485 @values = (conv_date($form->{closedto}));
1488 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1491 # set close in defaults
1492 do_query($form, $dbh, $query, @values);
1496 $main::lxdebug->leave_sub();
1500 my ($self, $units, $unit_name, $factor) = @_;
1502 $factor = 1 unless ($factor);
1504 my $unit = $units->{$unit_name};
1506 if (!defined($unit) || !$unit->{"base_unit"} ||
1507 ($unit_name eq $unit->{"base_unit"})) {
1508 return ($unit_name, $factor);
1511 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1514 sub retrieve_units {
1515 $main::lxdebug->enter_sub();
1517 my ($self, $myconfig, $form, $prefix) = @_;
1519 my $dbh = $form->get_standard_dbh;
1521 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1523 my $sth = prepare_execute_query($form, $dbh, $query);
1526 while (my $ref = $sth->fetchrow_hashref()) {
1527 $units->{$ref->{"name"}} = $ref;
1531 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1532 $sth = $dbh->prepare($query_lang);
1533 $sth->execute() || $form->dberror($query_lang);
1535 while (my $ref = $sth->fetchrow_hashref()) {
1536 push(@languages, $ref);
1540 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1541 "FROM units_language ul " .
1542 "LEFT JOIN language l ON ul.language_id = l.id " .
1543 "WHERE ul.unit = ?";
1544 $sth = $dbh->prepare($query_lang);
1546 foreach my $unit (values(%{$units})) {
1547 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1549 $unit->{"LANGUAGES"} = {};
1550 foreach my $lang (@languages) {
1551 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1554 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1555 while (my $ref = $sth->fetchrow_hashref()) {
1556 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1561 $main::lxdebug->leave_sub();
1566 sub retrieve_all_units {
1567 $main::lxdebug->enter_sub();
1571 if (!$main::all_units) {
1572 $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
1575 $main::lxdebug->leave_sub();
1577 return $main::all_units;
1581 sub translate_units {
1582 $main::lxdebug->enter_sub();
1584 my ($self, $form, $template_code, $unit, $amount) = @_;
1586 my $units = $self->retrieve_units(\%main::myconfig, $form);
1588 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1589 my $new_unit = $unit;
1591 if (($amount != 1) && $h->{"localized_plural"}) {
1592 $new_unit = $h->{"localized_plural"};
1593 } elsif ($h->{"localized"}) {
1594 $new_unit = $h->{"localized"};
1598 $main::lxdebug->leave_sub();
1604 $main::lxdebug->enter_sub();
1606 my ($self, $myconfig, $form, $units) = @_;
1608 my $dbh = $form->dbconnect($myconfig);
1610 map({ $_->{"in_use"} = 0; } values(%{$units}));
1612 foreach my $unit (values(%{$units})) {
1613 my $base_unit = $unit->{"original_base_unit"};
1614 while ($base_unit) {
1615 $units->{$base_unit}->{"in_use"} = 1;
1616 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1617 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1618 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1622 foreach my $unit (values(%{$units})) {
1623 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1625 foreach my $table (qw(parts invoice orderitems)) {
1626 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1628 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1629 $query .= "= " . $dbh->quote($unit->{"name"});
1631 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1632 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1635 my ($count) = $dbh->selectrow_array($query);
1636 $form->dberror($query) if ($dbh->err);
1639 $unit->{"in_use"} = 1;
1647 $main::lxdebug->leave_sub();
1650 sub convertible_units {
1651 $main::lxdebug->enter_sub();
1655 my $filter_unit = shift;
1656 my $not_smaller = shift;
1658 my $conv_units = [];
1660 $filter_unit = $units->{$filter_unit};
1662 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1663 my $unit = $units->{$name};
1665 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1666 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1667 push @{$conv_units}, $unit;
1671 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1673 $main::lxdebug->leave_sub();
1678 # if $a is translatable to $b, return the factor between them.
1681 $main::lxdebug->enter_sub(2);
1682 my ($this, $a, $b, $all_units) = @_;
1684 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1685 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1686 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1687 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1690 sub unit_select_data {
1691 $main::lxdebug->enter_sub();
1693 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1698 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1701 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1702 if (!$convertible_into ||
1703 ($units->{$convertible_into} &&
1704 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1705 push @{$select}, { "name" => $unit,
1706 "base_unit" => $units->{$unit}->{"base_unit"},
1707 "factor" => $units->{$unit}->{"factor"},
1708 "selected" => ($unit eq $selected) ? "selected" : "" };
1712 $main::lxdebug->leave_sub();
1717 sub unit_select_html {
1718 $main::lxdebug->enter_sub();
1720 my ($self, $units, $name, $selected, $convertible_into) = @_;
1722 my $select = "<select name=${name}>";
1724 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1725 if (!$convertible_into ||
1726 ($units->{$convertible_into} &&
1727 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1728 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1731 $select .= "</select>";
1733 $main::lxdebug->leave_sub();
1739 $main::lxdebug->enter_sub();
1743 my $units = $self->retrieve_all_units();
1748 while (2 <= scalar(@_)) {
1749 my $qty = shift(@_);
1750 my $unit = $units->{shift(@_)};
1752 croak "No unit defined with name $unit" if (!defined $unit);
1755 $base_unit = $unit->{base_unit};
1756 } elsif ($base_unit ne $unit->{base_unit}) {
1757 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1760 $sum += $qty * $unit->{factor};
1763 $main::lxdebug->leave_sub();
1765 return wantarray ? ($sum, $base_unit) : $sum;
1769 $main::lxdebug->enter_sub();
1771 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1773 my $dbh = $form->dbconnect_noauto($myconfig);
1775 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1776 my ($sortkey) = selectrow_query($form, $dbh, $query);
1778 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1779 "VALUES (?, ?, ?, ?)";
1780 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1783 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1784 my $sth = $dbh->prepare($query);
1785 foreach my $lang (@{$languages}) {
1786 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1787 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1795 $main::lxdebug->leave_sub();
1799 $main::lxdebug->enter_sub();
1801 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1803 my $dbh = $form->dbconnect_noauto($myconfig);
1805 my ($base_unit, $unit, $sth, $query);
1807 $query = "DELETE FROM units_language";
1808 $dbh->do($query) || $form->dberror($query);
1810 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1811 $query = "DELETE FROM units WHERE name IN (";
1812 map({ $query .= "?," } @{$delete_units});
1813 substr($query, -1, 1) = ")";
1814 $dbh->do($query, undef, @{$delete_units}) ||
1815 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1818 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1819 $sth = $dbh->prepare($query);
1821 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1822 my $sth_lang = $dbh->prepare($query_lang);
1824 foreach $unit (values(%{$units})) {
1825 $unit->{"depth"} = 0;
1826 my $base_unit = $unit;
1827 while ($base_unit->{"base_unit"}) {
1829 $base_unit = $units->{$base_unit->{"base_unit"}};
1833 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1834 if ($unit->{"LANGUAGES"}) {
1835 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1836 next unless ($lang->{"id"} && $lang->{"localized"});
1837 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1838 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1842 next if ($unit->{"unchanged_unit"});
1844 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1845 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1849 $sth_lang->finish();
1853 $main::lxdebug->leave_sub();
1857 $main::lxdebug->enter_sub();
1859 my ($self, $myconfig, $form) = @_;
1861 # connect to database
1862 my $dbh = $form->dbconnect($myconfig);
1864 my $query = qq|SELECT
1868 round(t.rate * 100, 2) AS rate,
1869 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1870 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1874 my $sth = $dbh->prepare($query);
1875 $sth->execute || $form->dberror($query);
1878 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1879 push @{ $form->{TAX} }, $ref;
1885 $main::lxdebug->leave_sub();
1888 sub get_tax_accounts {
1889 $main::lxdebug->enter_sub();
1891 my ($self, $myconfig, $form) = @_;
1893 my $dbh = $form->dbconnect($myconfig);
1895 # get Accounts from chart
1896 my $query = qq{ SELECT
1898 accno || ' - ' || description AS taxaccount
1900 WHERE link LIKE '%_tax%'
1904 my $sth = $dbh->prepare($query);
1905 $sth->execute || $form->dberror($query);
1907 $form->{ACCOUNTS} = [];
1908 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1909 push @{ $form->{ACCOUNTS} }, $ref;
1916 $main::lxdebug->leave_sub();
1920 $main::lxdebug->enter_sub();
1922 my ($self, $myconfig, $form) = @_;
1924 # connect to database
1925 my $dbh = $form->dbconnect($myconfig);
1927 my $query = qq|SELECT
1930 round(rate * 100, 2) AS rate,
1935 my $sth = $dbh->prepare($query);
1936 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1938 my $ref = $sth->fetchrow_hashref("NAME_lc");
1940 map { $form->{$_} = $ref->{$_} } keys %$ref;
1944 # see if it is used by a taxkey
1945 $query = qq|SELECT count(*) FROM taxkeys
1946 WHERE tax_id = ? AND chart_id >0|;
1948 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1950 $form->{orphaned} = !$form->{orphaned};
1953 if (!$form->{orphaned} ) {
1954 $query = qq|SELECT DISTINCT c.id, c.accno
1956 JOIN tax t ON (t.id = tk.tax_id)
1957 JOIN chart c ON (c.id = tk.chart_id)
1958 WHERE tk.tax_id = ?|;
1960 $sth = $dbh->prepare($query);
1961 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1963 $form->{TAXINUSE} = [];
1964 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1965 push @{ $form->{TAXINUSE} }, $ref;
1973 $main::lxdebug->leave_sub();
1977 $main::lxdebug->enter_sub();
1979 my ($self, $myconfig, $form) = @_;
1982 # connect to database
1983 my $dbh = $form->get_standard_dbh($myconfig);
1985 $form->{rate} = $form->{rate} / 100;
1987 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1988 if ($form->{id} ne "") {
1989 $query = qq|UPDATE tax SET
1994 taxnumber = (SELECT accno FROM chart WHERE id= ? )
1996 push(@values, $form->{id});
2000 $query = qq|INSERT INTO tax (
2007 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
2009 do_query($form, $dbh, $query, @values);
2013 $main::lxdebug->leave_sub();
2017 $main::lxdebug->enter_sub();
2019 my ($self, $myconfig, $form) = @_;
2022 # connect to database
2023 my $dbh = $form->get_standard_dbh($myconfig);
2025 $query = qq|DELETE FROM tax
2027 do_query($form, $dbh, $query, $form->{id});
2031 $main::lxdebug->leave_sub();
2034 sub save_price_factor {
2035 $main::lxdebug->enter_sub();
2037 my ($self, $myconfig, $form) = @_;
2039 # connect to database
2040 my $dbh = $form->get_standard_dbh($myconfig);
2043 my @values = ($form->{description}, conv_i($form->{factor}));
2046 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
2047 push @values, conv_i($form->{id});
2050 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
2053 do_query($form, $dbh, $query, @values);
2057 $main::lxdebug->leave_sub();
2060 sub get_all_price_factors {
2061 $main::lxdebug->enter_sub();
2063 my ($self, $myconfig, $form) = @_;
2065 # connect to database
2066 my $dbh = $form->get_standard_dbh($myconfig);
2068 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
2070 $main::lxdebug->leave_sub();
2073 sub get_price_factor {
2074 $main::lxdebug->enter_sub();
2076 my ($self, $myconfig, $form) = @_;
2078 # connect to database
2079 my $dbh = $form->get_standard_dbh($myconfig);
2081 my $query = qq|SELECT description, factor,
2082 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
2083 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
2084 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
2085 FROM price_factors WHERE id = ?|;
2087 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
2089 $main::lxdebug->leave_sub();
2092 sub delete_price_factor {
2093 $main::lxdebug->enter_sub();
2095 my ($self, $myconfig, $form) = @_;
2097 # connect to database
2098 my $dbh = $form->get_standard_dbh($myconfig);
2100 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
2103 $main::lxdebug->leave_sub();
2106 sub save_warehouse {
2107 $main::lxdebug->enter_sub();
2109 my ($self, $myconfig, $form) = @_;
2111 # connect to database
2112 my $dbh = $form->get_standard_dbh($myconfig);
2114 my ($query, @values, $sth);
2117 $query = qq|SELECT nextval('id')|;
2118 ($form->{id}) = selectrow_query($form, $dbh, $query);
2120 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
2121 do_query($form, $dbh, $query, $form->{id});
2124 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
2125 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
2127 if (0 < $form->{number_of_new_bins}) {
2128 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
2129 $sth = prepare_query($form, $dbh, $query);
2131 foreach my $i (1..$form->{number_of_new_bins}) {
2132 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
2140 $main::lxdebug->leave_sub();
2144 $main::lxdebug->enter_sub();
2146 my ($self, $myconfig, $form) = @_;
2148 # connect to database
2149 my $dbh = $form->get_standard_dbh($myconfig);
2151 my ($query, @values, $commit_necessary, $sth);
2153 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
2156 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2157 do_query($form, $dbh, $query, @values);
2159 $commit_necessary = 1;
2162 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2163 $sth = prepare_query($form, $dbh, $query);
2165 foreach my $row (1..$form->{rowcount}) {
2166 next if ($form->{"delete_${row}"});
2168 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2170 $commit_necessary = 1;
2175 $dbh->commit() if ($commit_necessary);
2177 $main::lxdebug->leave_sub();
2180 sub delete_warehouse {
2181 $main::lxdebug->enter_sub();
2183 my ($self, $myconfig, $form) = @_;
2185 # connect to database
2186 my $dbh = $form->get_standard_dbh($myconfig);
2188 my $id = conv_i($form->{id});
2189 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|;
2190 my ($count) = selectrow_query($form, $dbh, $query, $id);
2193 $main::lxdebug->leave_sub();
2197 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
2198 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
2202 $main::lxdebug->leave_sub();
2207 sub get_all_warehouses {
2208 $main::lxdebug->enter_sub();
2210 my ($self, $myconfig, $form) = @_;
2212 # connect to database
2213 my $dbh = $form->get_standard_dbh($myconfig);
2215 my $query = qq|SELECT w.id, w.description, w.invalid,
2216 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2218 ORDER BY w.sortkey|;
2220 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2222 $main::lxdebug->leave_sub();
2226 $main::lxdebug->enter_sub();
2228 my ($self, $myconfig, $form) = @_;
2230 # connect to database
2231 my $dbh = $form->get_standard_dbh($myconfig);
2233 my $id = conv_i($form->{id});
2234 my $query = qq|SELECT w.description, w.invalid
2238 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
2240 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2242 $query = qq|SELECT b.*, EXISTS
2243 (SELECT i.warehouse_id
2245 WHERE i.bin_id = b.id
2249 WHERE b.warehouse_id = ?|;
2251 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2253 $main::lxdebug->leave_sub();