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 #======================================================================
43 use List::MoreUtils qw(any);
48 use SL::GenericTranslations;
53 $main::lxdebug->enter_sub();
55 my ($self, $myconfig, $form) = @_;
58 my $dbh = $form->dbconnect($myconfig);
60 SELECT c.accno, c.description, c.charttype, c.category,
61 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
62 c.pos_bwa, datevautomatik,
63 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
64 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
67 ON (c.id=tk.chart_id AND tk.id =
68 (SELECT id FROM taxkeys
69 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
70 ORDER BY startdate DESC LIMIT 1))
75 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
76 my $sth = $dbh->prepare($query);
77 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
79 my $ref = $sth->fetchrow_hashref("NAME_lc");
81 foreach my $key (keys %$ref) {
82 $form->{"$key"} = $ref->{"$key"};
87 # get default accounts
88 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
90 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
91 $sth = $dbh->prepare($query);
92 $sth->execute || $form->dberror($query);
94 $ref = $sth->fetchrow_hashref("NAME_lc");
96 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
102 # get taxkeys and description
106 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
108 id||'--'||taxkey AS tax,
111 FROM tax ORDER BY taxkey
113 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
114 $sth = $dbh->prepare($query);
115 $sth->execute || $form->dberror($query);
117 $form->{TAXKEY} = [];
119 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
120 push @{ $form->{TAXKEY} }, $ref;
126 $query = qq|SELECT id, accno,description
130 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
131 $sth = $dbh->prepare($query);
132 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
134 $form->{NEWACCOUNT} = [];
135 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
136 push @{ $form->{NEWACCOUNT} }, $ref;
141 # get the taxkeys of account
155 LEFT JOIN tax t ON (t.id = tk.tax_id)
156 LEFT JOIN chart c ON (c.id = t.chart_id)
158 WHERE tk.chart_id = ?
159 ORDER BY startdate DESC
161 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
162 $sth = $dbh->prepare($query);
164 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
166 $form->{ACCOUNT_TAXKEYS} = [];
168 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
169 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
175 # check if we have any transactions
176 $query = qq|SELECT a.trans_id FROM acc_trans a
177 WHERE a.chart_id = ?|;
178 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
179 $sth = $dbh->prepare($query);
180 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
182 ($form->{orphaned}) = $sth->fetchrow_array;
183 $form->{orphaned} = !$form->{orphaned};
186 # check if new account is active
187 $form->{new_chart_valid} = 0;
188 if ($form->{new_chart_id}) {
189 $query = qq|SELECT current_date-valid_from FROM chart
191 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
192 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
194 $form->{new_chart_valid} = 1;
201 $main::lxdebug->leave_sub();
205 $main::lxdebug->enter_sub();
207 # TODO: it should be forbidden to change an account to a heading if there
208 # have been bookings to this account in the past
210 my ($self, $myconfig, $form) = @_;
212 # connect to database, turn off AutoCommit
213 my $dbh = $form->dbconnect_noauto($myconfig);
215 for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
216 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
219 # sanity check, can't have AR with AR_...
220 if ($form->{AR} || $form->{AP} || $form->{IC}) {
221 if (any { $form->{$_} } 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)) {
222 $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
227 foreach my $item ($form->{AR}, $form->{AR_amount},
228 $form->{AR_tax}, $form->{AR_paid},
229 $form->{AP}, $form->{AP_amount},
230 $form->{AP_tax}, $form->{AP_paid},
231 $form->{IC}, $form->{IC_sale},
232 $form->{IC_cogs}, $form->{IC_taxpart},
233 $form->{IC_income}, $form->{IC_expense},
234 $form->{IC_taxservice}
236 $form->{link} .= "${item}:" if ($item);
240 # strip blanks from accno
241 map { $form->{$_} =~ s/ //g; } qw(accno);
245 if ($form->{id} eq "NULL") {
254 my @values = ($form->{accno});
257 $query .= ' AND NOT id = ?';
258 push(@values, $form->{id});
261 my ($accno) = selectrow_query($form, $dbh, $query, @values);
264 $form->error($::locale->text('Account number not unique!'));
268 if (!$form->{id} || $form->{id} eq "") {
269 $query = qq|SELECT nextval('id')|;
270 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
271 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
272 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
280 # if charttype is heading make sure certain values are empty
281 # specifically, if charttype is changed from an existing account, empty the
282 # fields unnecessary for headings, so that e.g. heading doesn't appear in
283 # drop-down menues due to still having a valid "link" entry
285 if ( $form->{charttype} eq 'H' ) {
287 $form->{pos_bwa} = '';
288 $form->{pos_bilanz} = '';
289 $form->{pos_eur} = '';
290 $form->{new_chart_id} = '';
291 $form->{valid_from} = '';
294 $query = qq|UPDATE chart SET
310 $form->{description},
314 conv_i($form->{pos_bwa}),
315 conv_i($form->{pos_bilanz}),
316 conv_i($form->{pos_eur}),
317 conv_i($form->{new_chart_id}),
318 conv_date($form->{valid_from}),
319 ($form->{datevautomatik} eq 'T') ? 'true':'false',
326 do_query($form, $dbh, $query, @values);
332 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
336 for $tk_count (0 .. $MAX_TRIES) {
340 # Check if the account already exists, else cancel
342 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
344 last READTAXKEYS if ( $form->{'id'} == 0);
346 # check if there is a startdate
347 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
352 # Add valid taxkeys into the array
355 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
356 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
357 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
358 chart_id => conv_i($form->{"id"}),
359 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
360 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
367 for my $j (0 .. $#taxkeys){
368 if ( defined $taxkeys[$j]{'id'} ){
371 if ($taxkeys[$j]{'delete'}){
373 DELETE FROM taxkeys WHERE id = ?
376 @values = ($taxkeys[$j]{'id'});
378 do_query($form, $dbh, $query, @values);
387 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
395 $taxkeys[$j]{'tax_id'},
396 $taxkeys[$j]{'chart_id'},
397 $taxkeys[$j]{'tax_id'},
398 $taxkeys[$j]{'pos_ustva'},
399 $taxkeys[$j]{'startdate'},
402 do_query($form, $dbh, $query, @values);
408 INSERT INTO taxkeys (
415 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
418 $taxkeys[$j]{'tax_id'},
419 $taxkeys[$j]{'chart_id'},
420 $taxkeys[$j]{'tax_id'},
421 $taxkeys[$j]{'pos_ustva'},
422 $taxkeys[$j]{'startdate'},
425 do_query($form, $dbh, $query, @values);
430 # Update chart.taxkey_id to the latest from taxkeys for this chart.
436 WHERE taxkeys.chart_id = chart.id
437 ORDER BY startdate DESC
443 do_query($form, $dbh, $query, $form->{id});
446 my $rc = $dbh->commit;
449 $main::lxdebug->leave_sub();
455 $main::lxdebug->enter_sub();
457 my ($self, $myconfig, $form) = @_;
459 # connect to database, turn off AutoCommit
460 my $dbh = $form->dbconnect_noauto($myconfig);
462 my $query = qq|SELECT count(*) FROM acc_trans a
463 WHERE a.chart_id = ?|;
464 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
468 $main::lxdebug->leave_sub();
472 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
473 foreach my $type (qw(inventory income expense)) {
476 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
477 qq|WHERE ${type}_accno_id = ?|;
478 do_query($form, $dbh, $query, $form->{id});
481 $query = qq|DELETE FROM tax
483 do_query($form, $dbh, $query, $form->{id});
485 # delete chart of account record
486 $query = qq|DELETE FROM chart
488 do_query($form, $dbh, $query, $form->{id});
490 # delete account taxkeys
491 $query = qq|DELETE FROM taxkeys
493 do_query($form, $dbh, $query, $form->{id});
495 # commit and redirect
496 my $rc = $dbh->commit;
499 $main::lxdebug->leave_sub();
505 $main::lxdebug->enter_sub();
507 my ($self, $myconfig, $form) = @_;
509 # connect to database
510 my $dbh = $form->dbconnect($myconfig);
512 my $query = qq|SELECT id, lead
516 my $sth = $dbh->prepare($query);
517 $sth->execute || $form->dberror($query);
519 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
520 push @{ $form->{ALL} }, $ref;
526 $main::lxdebug->leave_sub();
530 $main::lxdebug->enter_sub();
532 my ($self, $myconfig, $form) = @_;
534 # connect to database
535 my $dbh = $form->dbconnect($myconfig);
538 qq|SELECT l.id, l.lead | .
541 my $sth = $dbh->prepare($query);
542 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
544 my $ref = $sth->fetchrow_hashref("NAME_lc");
546 map { $form->{$_} = $ref->{$_} } keys %$ref;
552 $main::lxdebug->leave_sub();
556 $main::lxdebug->enter_sub();
558 my ($self, $myconfig, $form) = @_;
561 # connect to database
562 my $dbh = $form->dbconnect($myconfig);
564 my @values = ($form->{description});
565 # id is the old record
567 $query = qq|UPDATE leads SET
570 push(@values, $form->{id});
572 $query = qq|INSERT INTO leads
576 do_query($form, $dbh, $query, @values);
580 $main::lxdebug->leave_sub();
584 $main::lxdebug->enter_sub();
586 my ($self, $myconfig, $form) = @_;
589 # connect to database
590 my $dbh = $form->dbconnect($myconfig);
592 $query = qq|DELETE FROM leads
594 do_query($form, $dbh, $query, $form->{id});
598 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my ($self, $myconfig, $form, $return_list) = @_;
606 # connect to database
607 my $dbh = $form->dbconnect($myconfig);
610 "SELECT id, description, template_code, article_code, " .
611 " output_numberformat, output_dateformat, output_longdates " .
612 "FROM language ORDER BY description";
614 my $sth = $dbh->prepare($query);
615 $sth->execute || $form->dberror($query);
619 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
620 push(@{ $ary }, $ref);
626 $main::lxdebug->leave_sub();
636 $main::lxdebug->enter_sub();
638 my ($self, $myconfig, $form) = @_;
640 # connect to database
641 my $dbh = $form->dbconnect($myconfig);
644 "SELECT description, template_code, article_code, " .
645 " output_numberformat, output_dateformat, output_longdates " .
646 "FROM language WHERE id = ?";
647 my $sth = $dbh->prepare($query);
648 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
650 my $ref = $sth->fetchrow_hashref("NAME_lc");
652 map { $form->{$_} = $ref->{$_} } keys %$ref;
658 $main::lxdebug->leave_sub();
661 sub get_language_details {
662 $main::lxdebug->enter_sub();
664 my ($self, $myconfig, $form, $id) = @_;
666 # connect to database
667 my $dbh = $form->dbconnect($myconfig);
670 "SELECT template_code, " .
671 " output_numberformat, output_dateformat, output_longdates " .
672 "FROM language WHERE id = ?";
673 my @res = selectrow_query($form, $dbh, $query, $id);
676 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
684 my ($self, $myconfig, $form) = @_;
686 # connect to database
687 my $dbh = $form->dbconnect($myconfig);
688 my (@values, $query);
690 map({ push(@values, $form->{$_}); }
691 qw(description template_code article_code
692 output_numberformat output_dateformat output_longdates));
694 # id is the old record
697 "UPDATE language SET " .
698 " description = ?, template_code = ?, article_code = ?, " .
699 " output_numberformat = ?, output_dateformat = ?, " .
700 " output_longdates = ? " .
702 push(@values, $form->{id});
705 "INSERT INTO language (" .
706 " description, template_code, article_code, " .
707 " output_numberformat, output_dateformat, output_longdates" .
708 ") VALUES (?, ?, ?, ?, ?, ?)";
710 do_query($form, $dbh, $query, @values);
714 $main::lxdebug->leave_sub();
717 sub delete_language {
718 $main::lxdebug->enter_sub();
720 my ($self, $myconfig, $form) = @_;
723 # connect to database
724 my $dbh = $form->dbconnect_noauto($myconfig);
726 foreach my $table (qw(generic_translations units_language)) {
727 $query = qq|DELETE FROM $table WHERE language_id = ?|;
728 do_query($form, $dbh, $query, $form->{"id"});
731 $query = "DELETE FROM language WHERE id = ?";
732 do_query($form, $dbh, $query, $form->{"id"});
737 $main::lxdebug->leave_sub();
742 $main::lxdebug->enter_sub();
744 my ($self, $myconfig, $form) = @_;
746 # connect to database
747 my $dbh = $form->dbconnect($myconfig);
749 my $query = qq|SELECT id, description,
751 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
753 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
755 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
757 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
759 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
761 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
763 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
765 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
767 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
771 my $sth = $dbh->prepare($query);
772 $sth->execute || $form->dberror($query);
775 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
776 push @{ $form->{ALL} }, $ref;
782 $main::lxdebug->leave_sub();
785 sub get_buchungsgruppe {
786 $main::lxdebug->enter_sub();
788 my ($self, $myconfig, $form) = @_;
791 # connect to database
792 my $dbh = $form->dbconnect($myconfig);
796 qq|SELECT description, inventory_accno_id,
797 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
799 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
801 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
803 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
805 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
807 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
809 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
811 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
813 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
816 my $sth = $dbh->prepare($query);
817 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
819 my $ref = $sth->fetchrow_hashref("NAME_lc");
821 map { $form->{$_} = $ref->{$_} } keys %$ref;
826 qq|SELECT count(id) = 0 AS orphaned
828 WHERE buchungsgruppen_id = ?|;
829 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
832 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
834 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
835 $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
838 $query = qq|SELECT c.accno, c.description, c.link, c.id,
839 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
840 FROM chart c, defaults d
841 WHERE c.link LIKE '%$module%'
845 my $sth = $dbh->prepare($query);
846 $sth->execute || $form->dberror($query);
847 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
848 foreach my $key (split(/:/, $ref->{link})) {
849 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
850 $form->{"std_inventory_accno_id"} = $ref->{"id"};
852 if ($key =~ /$module/) {
853 if ( ($ref->{id} eq $ref->{inventory_accno_id})
854 || ($ref->{id} eq $ref->{income_accno_id})
855 || ($ref->{id} eq $ref->{expense_accno_id})) {
856 push @{ $form->{"${module}_links"}{$key} },
857 { accno => $ref->{accno},
858 description => $ref->{description},
859 selected => "selected",
862 push @{ $form->{"${module}_links"}{$key} },
863 { accno => $ref->{accno},
864 description => $ref->{description},
876 $main::lxdebug->leave_sub();
879 sub save_buchungsgruppe {
880 $main::lxdebug->enter_sub();
882 my ($self, $myconfig, $form) = @_;
884 # connect to database
885 my $dbh = $form->dbconnect($myconfig);
887 my @values = ($form->{description}, $form->{inventory_accno_id},
888 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
889 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
890 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
891 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
895 # id is the old record
897 $query = qq|UPDATE buchungsgruppen SET
898 description = ?, inventory_accno_id = ?,
899 income_accno_id_0 = ?, expense_accno_id_0 = ?,
900 income_accno_id_1 = ?, expense_accno_id_1 = ?,
901 income_accno_id_2 = ?, expense_accno_id_2 = ?,
902 income_accno_id_3 = ?, expense_accno_id_3 = ?
904 push(@values, $form->{id});
906 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
907 my ($sortkey) = $dbh->selectrow_array($query);
908 $form->dberror($query) if ($dbh->err);
909 push(@values, $sortkey);
910 $query = qq|INSERT INTO buchungsgruppen
911 (description, inventory_accno_id,
912 income_accno_id_0, expense_accno_id_0,
913 income_accno_id_1, expense_accno_id_1,
914 income_accno_id_2, expense_accno_id_2,
915 income_accno_id_3, expense_accno_id_3,
917 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
919 do_query($form, $dbh, $query, @values);
923 $main::lxdebug->leave_sub();
926 sub delete_buchungsgruppe {
927 $main::lxdebug->enter_sub();
929 my ($self, $myconfig, $form) = @_;
931 # connect to database
932 my $dbh = $form->dbconnect($myconfig);
934 my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
935 do_query($form, $dbh, $query, $form->{id});
939 $main::lxdebug->leave_sub();
943 $main::lxdebug->enter_sub();
945 my ($self, $myconfig, $form, $table) = @_;
947 # connect to database
948 my $dbh = $form->get_standard_dbh($myconfig);
952 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
953 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
954 my @values = ($form->{"id1"}, $form->{"id2"});
955 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
957 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
958 my $sth = prepare_query($form, $dbh, $query);
960 do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
961 do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
967 $main::lxdebug->leave_sub();
970 sub prepare_template_filename {
971 $main::lxdebug->enter_sub();
973 my ($self, $myconfig, $form) = @_;
975 my ($filename, $display_filename);
977 if ($form->{type} eq "stylesheet") {
978 $filename = "css/$myconfig->{stylesheet}";
979 $display_filename = $myconfig->{stylesheet};
982 $filename = $form->{formname};
984 if ($form->{language}) {
985 my ($id, $template_code) = split(/--/, $form->{language});
986 $filename .= "_${template_code}";
989 if ($form->{printer}) {
990 my ($id, $template_code) = split(/--/, $form->{printer});
991 $filename .= "_${template_code}";
994 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
995 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
996 $filename =~ s|.*/||;
998 $display_filename = $filename;
999 $filename = SL::DB::Default->get->templates . "/$filename";
1002 $main::lxdebug->leave_sub();
1004 return ($filename, $display_filename);
1009 $main::lxdebug->enter_sub();
1011 my ($self, $filename) = @_;
1013 my ($content, $lines) = ("", 0);
1017 if (open(TEMPLATE, $filename)) {
1018 while (<TEMPLATE>) {
1025 $content = Encode::decode('utf-8-strict', $content);
1027 $main::lxdebug->leave_sub();
1029 return ($content, $lines);
1033 $main::lxdebug->enter_sub();
1035 my ($self, $filename, $content) = @_;
1041 if (open(TEMPLATE, ">", $filename)) {
1042 $content = Encode::encode('utf-8-strict', $content);
1043 $content =~ s/\r\n/\n/g;
1044 print(TEMPLATE $content);
1050 $main::lxdebug->leave_sub();
1055 sub save_preferences {
1056 $main::lxdebug->enter_sub();
1058 my ($self, $form) = @_;
1060 my $employee = SL::DB::Manager::Employee->find_by(login => $form->{login});
1061 $employee->update_attributes(name => $form->{name});
1063 my $user = SL::DB::Manager::AuthUser->find_by(login => $form->{login});
1064 $user->update_attributes(
1066 map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
1069 $main::lxdebug->leave_sub();
1075 $main::lxdebug->enter_sub();
1080 my $myconfig = \%main::myconfig;
1081 my $form = $main::form;
1083 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1085 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1087 $defaults->{weightunit} ||= 'kg';
1089 $main::lxdebug->leave_sub();
1095 $main::lxdebug->enter_sub();
1097 my ($self, $myconfig, $form) = @_;
1099 my $dbh = $form->dbconnect($myconfig);
1101 my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
1102 my $sth = $dbh->prepare($query);
1103 $sth->execute || $form->dberror($query);
1105 ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
1111 $main::lxdebug->leave_sub();
1115 $main::lxdebug->enter_sub();
1117 my ($self, $myconfig, $form) = @_;
1119 my $dbh = $form->dbconnect($myconfig);
1121 my ($query, @values);
1123 if ($form->{revtrans}) {
1124 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1126 } elsif ($form->{closedto}) {
1127 $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
1128 @values = (conv_date($form->{closedto}), conv_date($form->{max_future_booking_interval}));
1131 $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1134 # set close in defaults
1135 do_query($form, $dbh, $query, @values);
1139 $main::lxdebug->leave_sub();
1143 my ($self, $units, $unit_name, $factor) = @_;
1145 $factor = 1 unless ($factor);
1147 my $unit = $units->{$unit_name};
1149 if (!defined($unit) || !$unit->{"base_unit"} ||
1150 ($unit_name eq $unit->{"base_unit"})) {
1151 return ($unit_name, $factor);
1154 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1157 sub retrieve_units {
1158 $main::lxdebug->enter_sub();
1160 my ($self, $myconfig, $form, $prefix) = @_;
1163 my $dbh = $form->get_standard_dbh;
1165 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1167 my $sth = prepare_execute_query($form, $dbh, $query);
1170 while (my $ref = $sth->fetchrow_hashref()) {
1171 $units->{$ref->{"name"}} = $ref;
1175 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1176 $sth = $dbh->prepare($query_lang);
1177 $sth->execute() || $form->dberror($query_lang);
1179 while (my $ref = $sth->fetchrow_hashref()) {
1180 push(@languages, $ref);
1184 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1185 "FROM units_language ul " .
1186 "LEFT JOIN language l ON ul.language_id = l.id " .
1187 "WHERE ul.unit = ?";
1188 $sth = $dbh->prepare($query_lang);
1190 foreach my $unit (values(%{$units})) {
1191 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1193 $unit->{"LANGUAGES"} = {};
1194 foreach my $lang (@languages) {
1195 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1198 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1199 while (my $ref = $sth->fetchrow_hashref()) {
1200 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1205 $main::lxdebug->leave_sub();
1210 sub retrieve_all_units {
1211 $main::lxdebug->enter_sub();
1215 if (!$::request->{cache}{all_units}) {
1216 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1219 $main::lxdebug->leave_sub();
1221 return $::request->{cache}{all_units};
1225 sub translate_units {
1226 $main::lxdebug->enter_sub();
1228 my ($self, $form, $template_code, $unit, $amount) = @_;
1230 my $units = $self->retrieve_units(\%main::myconfig, $form);
1232 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1233 my $new_unit = $unit;
1235 if (($amount != 1) && $h->{"localized_plural"}) {
1236 $new_unit = $h->{"localized_plural"};
1237 } elsif ($h->{"localized"}) {
1238 $new_unit = $h->{"localized"};
1242 $main::lxdebug->leave_sub();
1248 $main::lxdebug->enter_sub();
1250 my ($self, $myconfig, $form, $units) = @_;
1252 my $dbh = $form->dbconnect($myconfig);
1254 map({ $_->{"in_use"} = 0; } values(%{$units}));
1256 foreach my $unit (values(%{$units})) {
1257 my $base_unit = $unit->{"original_base_unit"};
1258 while ($base_unit) {
1259 $units->{$base_unit}->{"in_use"} = 1;
1260 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1261 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1262 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1266 foreach my $unit (values(%{$units})) {
1267 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1269 foreach my $table (qw(parts invoice orderitems)) {
1270 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1272 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1273 $query .= "= " . $dbh->quote($unit->{"name"});
1275 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1276 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1279 my ($count) = $dbh->selectrow_array($query);
1280 $form->dberror($query) if ($dbh->err);
1283 $unit->{"in_use"} = 1;
1291 $main::lxdebug->leave_sub();
1294 sub convertible_units {
1295 $main::lxdebug->enter_sub();
1299 my $filter_unit = shift;
1300 my $not_smaller = shift;
1302 my $conv_units = [];
1304 $filter_unit = $units->{$filter_unit};
1306 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1307 my $unit = $units->{$name};
1309 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1310 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1311 push @{$conv_units}, $unit;
1315 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1317 $main::lxdebug->leave_sub();
1322 # if $a is translatable to $b, return the factor between them.
1325 $main::lxdebug->enter_sub(2);
1326 my ($this, $a, $b, $all_units) = @_;
1329 $all_units = $this->retrieve_all_units;
1332 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1333 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1334 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1335 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1338 sub unit_select_data {
1339 $main::lxdebug->enter_sub();
1341 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1346 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1349 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1350 if (!$convertible_into ||
1351 ($units->{$convertible_into} &&
1352 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1353 push @{$select}, { "name" => $unit,
1354 "base_unit" => $units->{$unit}->{"base_unit"},
1355 "factor" => $units->{$unit}->{"factor"},
1356 "selected" => ($unit eq $selected) ? "selected" : "" };
1360 $main::lxdebug->leave_sub();
1365 sub unit_select_html {
1366 $main::lxdebug->enter_sub();
1368 my ($self, $units, $name, $selected, $convertible_into) = @_;
1370 my $select = "<select name=${name}>";
1372 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1373 if (!$convertible_into ||
1374 ($units->{$convertible_into} &&
1375 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1376 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1379 $select .= "</select>";
1381 $main::lxdebug->leave_sub();
1387 $main::lxdebug->enter_sub();
1391 my $units = $self->retrieve_all_units();
1396 while (2 <= scalar(@_)) {
1397 my $qty = shift(@_);
1398 my $unit = $units->{shift(@_)};
1400 croak "No unit defined with name $unit" if (!defined $unit);
1403 $base_unit = $unit->{base_unit};
1404 } elsif ($base_unit ne $unit->{base_unit}) {
1405 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1408 $sum += $qty * $unit->{factor};
1411 $main::lxdebug->leave_sub();
1413 return wantarray ? ($sum, $base_unit) : $sum;
1417 $main::lxdebug->enter_sub();
1419 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1421 my $dbh = $form->dbconnect_noauto($myconfig);
1423 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1424 my ($sortkey) = selectrow_query($form, $dbh, $query);
1426 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1427 "VALUES (?, ?, ?, ?)";
1428 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1431 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1432 my $sth = $dbh->prepare($query);
1433 foreach my $lang (@{$languages}) {
1434 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1435 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1443 $main::lxdebug->leave_sub();
1447 $main::lxdebug->enter_sub();
1449 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1451 my $dbh = $form->dbconnect_noauto($myconfig);
1453 my ($base_unit, $unit, $sth, $query);
1455 $query = "DELETE FROM units_language";
1456 $dbh->do($query) || $form->dberror($query);
1458 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1459 $query = "DELETE FROM units WHERE name IN (";
1460 map({ $query .= "?," } @{$delete_units});
1461 substr($query, -1, 1) = ")";
1462 $dbh->do($query, undef, @{$delete_units}) ||
1463 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1466 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1467 $sth = $dbh->prepare($query);
1469 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1470 my $sth_lang = $dbh->prepare($query_lang);
1472 foreach $unit (values(%{$units})) {
1473 $unit->{"depth"} = 0;
1474 my $base_unit = $unit;
1475 while ($base_unit->{"base_unit"}) {
1477 $base_unit = $units->{$base_unit->{"base_unit"}};
1481 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1482 if ($unit->{"LANGUAGES"}) {
1483 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1484 next unless ($lang->{"id"} && $lang->{"localized"});
1485 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1486 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1490 next if ($unit->{"unchanged_unit"});
1492 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1493 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1497 $sth_lang->finish();
1501 $main::lxdebug->leave_sub();
1505 $main::lxdebug->enter_sub();
1507 my ($self, $myconfig, $form) = @_;
1509 # connect to database
1510 my $dbh = $form->dbconnect($myconfig);
1512 my $query = qq|SELECT
1516 round(t.rate * 100, 2) AS rate,
1517 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1518 (SELECT description FROM chart WHERE id = chart_id) AS account_description
1520 ORDER BY taxkey, rate|;
1522 my $sth = $dbh->prepare($query);
1523 $sth->execute || $form->dberror($query);
1526 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1527 push @{ $form->{TAX} }, $ref;
1533 $main::lxdebug->leave_sub();
1536 sub get_tax_accounts {
1537 $main::lxdebug->enter_sub();
1539 my ($self, $myconfig, $form) = @_;
1541 my $dbh = $form->dbconnect($myconfig);
1543 # get Accounts from chart
1544 my $query = qq{ SELECT
1546 accno || ' - ' || description AS taxaccount
1548 WHERE link LIKE '%_tax%'
1552 my $sth = $dbh->prepare($query);
1553 $sth->execute || $form->dberror($query);
1555 $form->{ACCOUNTS} = [];
1556 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1557 push @{ $form->{ACCOUNTS} }, $ref;
1564 $main::lxdebug->leave_sub();
1568 $main::lxdebug->enter_sub();
1570 my ($self, $myconfig, $form) = @_;
1572 # connect to database
1573 my $dbh = $form->dbconnect($myconfig);
1575 my $query = qq|SELECT
1578 round(rate * 100, 2) AS rate,
1581 (id IN (SELECT tax_id
1582 FROM acc_trans)) AS tax_already_used
1586 my $sth = $dbh->prepare($query);
1587 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1589 my $ref = $sth->fetchrow_hashref("NAME_lc");
1591 map { $form->{$_} = $ref->{$_} } keys %$ref;
1595 # see if it is used by a taxkey
1596 $query = qq|SELECT count(*) FROM taxkeys
1597 WHERE tax_id = ? AND chart_id >0|;
1599 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1601 $form->{orphaned} = !$form->{orphaned};
1604 if (!$form->{orphaned} ) {
1605 $query = qq|SELECT DISTINCT c.id, c.accno
1607 JOIN tax t ON (t.id = tk.tax_id)
1608 JOIN chart c ON (c.id = tk.chart_id)
1609 WHERE tk.tax_id = ?|;
1611 $sth = $dbh->prepare($query);
1612 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1614 $form->{TAXINUSE} = [];
1615 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1616 push @{ $form->{TAXINUSE} }, $ref;
1624 $main::lxdebug->leave_sub();
1628 $main::lxdebug->enter_sub();
1630 my ($self, $myconfig, $form) = @_;
1633 # connect to database
1634 my $dbh = $form->get_standard_dbh($myconfig);
1636 $form->{rate} = $form->{rate} / 100;
1638 my $chart_categories = '';
1639 $chart_categories .= 'A' if $form->{asset};
1640 $chart_categories .= 'L' if $form->{liability};
1641 $chart_categories .= 'Q' if $form->{equity};
1642 $chart_categories .= 'I' if $form->{revenue};
1643 $chart_categories .= 'E' if $form->{expense};
1644 $chart_categories .= 'C' if $form->{costs};
1646 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id}, $chart_categories);
1647 if ($form->{id} ne "") {
1648 $query = qq|UPDATE tax SET
1653 taxnumber = (SELECT accno FROM chart WHERE id= ? ),
1654 chart_categories = ?
1659 ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
1660 $query = qq|INSERT INTO tax (
1669 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?)|;
1671 push(@values, $form->{id});
1672 do_query($form, $dbh, $query, @values);
1674 foreach my $language_id (keys %{ $form->{translations} }) {
1675 GenericTranslations->save('dbh' => $dbh,
1676 'translation_type' => 'SL::DB::Tax/taxdescription',
1677 'translation_id' => $form->{id},
1678 'language_id' => $language_id,
1679 'translation' => $form->{translations}->{$language_id});
1684 $main::lxdebug->leave_sub();
1688 $main::lxdebug->enter_sub();
1690 my ($self, $myconfig, $form) = @_;
1693 # connect to database
1694 my $dbh = $form->get_standard_dbh($myconfig);
1696 $query = qq|DELETE FROM tax
1698 do_query($form, $dbh, $query, $form->{id});
1702 $main::lxdebug->leave_sub();
1705 sub save_price_factor {
1706 $main::lxdebug->enter_sub();
1708 my ($self, $myconfig, $form) = @_;
1710 # connect to database
1711 my $dbh = $form->get_standard_dbh($myconfig);
1714 my @values = ($form->{description}, conv_i($form->{factor}));
1717 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1718 push @values, conv_i($form->{id});
1721 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1724 do_query($form, $dbh, $query, @values);
1728 $main::lxdebug->leave_sub();
1731 sub get_all_price_factors {
1732 $main::lxdebug->enter_sub();
1734 my ($self, $myconfig, $form) = @_;
1736 # connect to database
1737 my $dbh = $form->get_standard_dbh($myconfig);
1739 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1741 $main::lxdebug->leave_sub();
1744 sub get_price_factor {
1745 $main::lxdebug->enter_sub();
1747 my ($self, $myconfig, $form) = @_;
1749 # connect to database
1750 my $dbh = $form->get_standard_dbh($myconfig);
1752 my $query = qq|SELECT description, factor,
1753 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1754 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1755 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1756 FROM price_factors WHERE id = ?|;
1758 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1760 $main::lxdebug->leave_sub();
1763 sub delete_price_factor {
1764 $main::lxdebug->enter_sub();
1766 my ($self, $myconfig, $form) = @_;
1768 # connect to database
1769 my $dbh = $form->get_standard_dbh($myconfig);
1771 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1774 $main::lxdebug->leave_sub();
1777 sub save_warehouse {
1778 $main::lxdebug->enter_sub();
1780 my ($self, $myconfig, $form) = @_;
1782 # connect to database
1783 my $dbh = $form->get_standard_dbh($myconfig);
1785 my ($query, @values, $sth);
1788 $query = qq|SELECT nextval('id')|;
1789 ($form->{id}) = selectrow_query($form, $dbh, $query);
1791 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1792 do_query($form, $dbh, $query, $form->{id});
1795 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1796 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1798 if (0 < $form->{number_of_new_bins}) {
1799 my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1800 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1801 $sth = prepare_query($form, $dbh, $query);
1803 foreach my $i (1..$form->{number_of_new_bins}) {
1804 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1812 $main::lxdebug->leave_sub();
1816 $main::lxdebug->enter_sub();
1818 my ($self, $myconfig, $form) = @_;
1820 # connect to database
1821 my $dbh = $form->get_standard_dbh($myconfig);
1823 my ($query, @values, $commit_necessary, $sth);
1825 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1828 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1829 do_query($form, $dbh, $query, @values);
1831 $commit_necessary = 1;
1834 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1835 $sth = prepare_query($form, $dbh, $query);
1837 foreach my $row (1..$form->{rowcount}) {
1838 next if ($form->{"delete_${row}"});
1840 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1842 $commit_necessary = 1;
1847 $dbh->commit() if ($commit_necessary);
1849 $main::lxdebug->leave_sub();
1852 sub delete_warehouse {
1853 $main::lxdebug->enter_sub();
1855 my ($self, $myconfig, $form) = @_;
1857 # connect to database
1858 my $dbh = $form->get_standard_dbh($myconfig);
1860 my $id = conv_i($form->{id});
1861 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|;
1862 my ($count) = selectrow_query($form, $dbh, $query, $id);
1865 $main::lxdebug->leave_sub();
1869 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
1870 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
1874 $main::lxdebug->leave_sub();
1879 sub get_all_warehouses {
1880 $main::lxdebug->enter_sub();
1882 my ($self, $myconfig, $form) = @_;
1884 # connect to database
1885 my $dbh = $form->get_standard_dbh($myconfig);
1887 my $query = qq|SELECT w.id, w.description, w.invalid,
1888 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
1890 ORDER BY w.sortkey|;
1892 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
1894 $main::lxdebug->leave_sub();
1898 $main::lxdebug->enter_sub();
1900 my ($self, $myconfig, $form) = @_;
1902 # connect to database
1903 my $dbh = $form->get_standard_dbh($myconfig);
1905 my $id = conv_i($form->{id});
1906 my $query = qq|SELECT w.description, w.invalid
1910 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1912 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1916 ( EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
1917 OR EXISTS(SELECT p.bin_id FROM parts p WHERE p.bin_id = b.id LIMIT 1))
1920 WHERE b.warehouse_id = ?
1923 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
1925 $main::lxdebug->leave_sub();