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);
49 use SL::GenericTranslations;
54 $main::lxdebug->enter_sub();
56 my ($self, $myconfig, $form) = @_;
59 my $dbh = $form->dbconnect($myconfig);
61 SELECT c.accno, c.description, c.charttype, c.category,
62 c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
63 c.pos_bwa, datevautomatik,
64 tk.taxkey_id, tk.pos_ustva, tk.tax_id,
65 tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
68 ON (c.id=tk.chart_id AND tk.id =
69 (SELECT id FROM taxkeys
70 WHERE taxkeys.chart_id = c.id AND startdate <= current_date
71 ORDER BY startdate DESC LIMIT 1))
76 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
77 my $sth = $dbh->prepare($query);
78 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
80 my $ref = $sth->fetchrow_hashref("NAME_lc");
82 foreach my $key (keys %$ref) {
83 $form->{"$key"} = $ref->{"$key"};
88 # get default accounts
89 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
91 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
92 $sth = $dbh->prepare($query);
93 $sth->execute || $form->dberror($query);
95 $ref = $sth->fetchrow_hashref("NAME_lc");
97 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
103 # get taxkeys and description
107 (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
109 id||'--'||taxkey AS tax,
112 FROM tax ORDER BY taxkey
114 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
115 $sth = $dbh->prepare($query);
116 $sth->execute || $form->dberror($query);
118 $form->{TAXKEY} = [];
120 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
121 push @{ $form->{TAXKEY} }, $ref;
127 $query = qq|SELECT id, accno,description
131 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
132 $sth = $dbh->prepare($query);
133 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
135 $form->{NEWACCOUNT} = [];
136 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
137 push @{ $form->{NEWACCOUNT} }, $ref;
142 # get the taxkeys of account
156 LEFT JOIN tax t ON (t.id = tk.tax_id)
157 LEFT JOIN chart c ON (c.id = t.chart_id)
159 WHERE tk.chart_id = ?
160 ORDER BY startdate DESC
162 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
163 $sth = $dbh->prepare($query);
165 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
167 $form->{ACCOUNT_TAXKEYS} = [];
169 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
170 push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
176 # check if we have any transactions
177 $query = qq|SELECT a.trans_id FROM acc_trans a
178 WHERE a.chart_id = ?|;
179 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
180 $sth = $dbh->prepare($query);
181 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
183 ($form->{orphaned}) = $sth->fetchrow_array;
184 $form->{orphaned} = !$form->{orphaned};
187 # check if new account is active
188 $form->{new_chart_valid} = 0;
189 if ($form->{new_chart_id}) {
190 $query = qq|SELECT current_date-valid_from FROM chart
192 $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
193 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
195 $form->{new_chart_valid} = 1;
202 $main::lxdebug->leave_sub();
206 $main::lxdebug->enter_sub();
208 # TODO: it should be forbidden to change an account to a heading if there
209 # have been bookings to this account in the past
211 my ($self, $myconfig, $form) = @_;
213 # connect to database, turn off AutoCommit
214 my $dbh = $form->dbconnect_noauto($myconfig);
216 for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
217 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
220 # sanity check, can't have AR with AR_...
221 if ($form->{AR} || $form->{AP} || $form->{IC}) {
222 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)) {
223 $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
228 foreach my $item ($form->{AR}, $form->{AR_amount},
229 $form->{AR_tax}, $form->{AR_paid},
230 $form->{AP}, $form->{AP_amount},
231 $form->{AP_tax}, $form->{AP_paid},
232 $form->{IC}, $form->{IC_sale},
233 $form->{IC_cogs}, $form->{IC_taxpart},
234 $form->{IC_income}, $form->{IC_expense},
235 $form->{IC_taxservice}
237 $form->{link} .= "${item}:" if ($item);
241 # strip blanks from accno
242 map { $form->{$_} =~ s/ //g; } qw(accno);
246 if ($form->{id} eq "NULL") {
255 my @values = ($form->{accno});
258 $query .= ' AND NOT id = ?';
259 push(@values, $form->{id});
262 my ($accno) = selectrow_query($form, $dbh, $query, @values);
265 $form->error($::locale->text('Account number not unique!'));
269 if (!$form->{id} || $form->{id} eq "") {
270 $query = qq|SELECT nextval('id')|;
271 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
272 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
273 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
281 # if charttype is heading make sure certain values are empty
282 # specifically, if charttype is changed from an existing account, empty the
283 # fields unnecessary for headings, so that e.g. heading doesn't appear in
284 # drop-down menues due to still having a valid "link" entry
286 if ( $form->{charttype} eq 'H' ) {
288 $form->{pos_bwa} = '';
289 $form->{pos_bilanz} = '';
290 $form->{pos_eur} = '';
291 $form->{new_chart_id} = '';
292 $form->{valid_from} = '';
295 $query = qq|UPDATE chart SET
311 $form->{description},
315 conv_i($form->{pos_bwa}),
316 conv_i($form->{pos_bilanz}),
317 conv_i($form->{pos_eur}),
318 conv_i($form->{new_chart_id}),
319 conv_date($form->{valid_from}),
320 ($form->{datevautomatik} eq 'T') ? 'true':'false',
327 do_query($form, $dbh, $query, @values);
333 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
337 for $tk_count (0 .. $MAX_TRIES) {
341 # Check if the account already exists, else cancel
343 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
345 last READTAXKEYS if ( $form->{'id'} == 0);
347 # check if there is a startdate
348 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
353 # Add valid taxkeys into the array
356 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
357 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
358 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
359 chart_id => conv_i($form->{"id"}),
360 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
361 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
368 for my $j (0 .. $#taxkeys){
369 if ( defined $taxkeys[$j]{'id'} ){
372 if ($taxkeys[$j]{'delete'}){
374 DELETE FROM taxkeys WHERE id = ?
377 @values = ($taxkeys[$j]{'id'});
379 do_query($form, $dbh, $query, @values);
388 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
396 $taxkeys[$j]{'tax_id'},
397 $taxkeys[$j]{'chart_id'},
398 $taxkeys[$j]{'tax_id'},
399 $taxkeys[$j]{'pos_ustva'},
400 $taxkeys[$j]{'startdate'},
403 do_query($form, $dbh, $query, @values);
409 INSERT INTO taxkeys (
416 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
419 $taxkeys[$j]{'tax_id'},
420 $taxkeys[$j]{'chart_id'},
421 $taxkeys[$j]{'tax_id'},
422 $taxkeys[$j]{'pos_ustva'},
423 $taxkeys[$j]{'startdate'},
426 do_query($form, $dbh, $query, @values);
431 # Update chart.taxkey_id to the latest from taxkeys for this chart.
437 WHERE taxkeys.chart_id = chart.id
438 ORDER BY startdate DESC
444 do_query($form, $dbh, $query, $form->{id});
447 my $rc = $dbh->commit;
450 $main::lxdebug->leave_sub();
456 $main::lxdebug->enter_sub();
458 my ($self, $myconfig, $form) = @_;
460 # connect to database, turn off AutoCommit
461 my $dbh = $form->dbconnect_noauto($myconfig);
463 my $query = qq|SELECT count(*) FROM acc_trans a
464 WHERE a.chart_id = ?|;
465 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
469 $main::lxdebug->leave_sub();
473 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
474 foreach my $type (qw(inventory income expense)) {
477 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
478 qq|WHERE ${type}_accno_id = ?|;
479 do_query($form, $dbh, $query, $form->{id});
482 $query = qq|DELETE FROM tax
484 do_query($form, $dbh, $query, $form->{id});
486 # delete account taxkeys
487 $query = qq|DELETE FROM taxkeys
489 do_query($form, $dbh, $query, $form->{id});
491 # delete chart of account record
492 # last step delete chart, because we have a constraint
494 $query = qq|DELETE FROM chart
496 do_query($form, $dbh, $query, $form->{id});
498 # commit and redirect
499 my $rc = $dbh->commit;
502 $main::lxdebug->leave_sub();
508 $main::lxdebug->enter_sub();
510 my ($self, $myconfig, $form) = @_;
512 # connect to database
513 my $dbh = $form->dbconnect($myconfig);
515 my $query = qq|SELECT id, lead
519 my $sth = $dbh->prepare($query);
520 $sth->execute || $form->dberror($query);
522 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
523 push @{ $form->{ALL} }, $ref;
529 $main::lxdebug->leave_sub();
533 $main::lxdebug->enter_sub();
535 my ($self, $myconfig, $form) = @_;
537 # connect to database
538 my $dbh = $form->dbconnect($myconfig);
541 qq|SELECT l.id, l.lead | .
544 my $sth = $dbh->prepare($query);
545 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
547 my $ref = $sth->fetchrow_hashref("NAME_lc");
549 map { $form->{$_} = $ref->{$_} } keys %$ref;
555 $main::lxdebug->leave_sub();
559 $main::lxdebug->enter_sub();
561 my ($self, $myconfig, $form) = @_;
564 # connect to database
565 my $dbh = $form->dbconnect($myconfig);
567 my @values = ($form->{description});
568 # id is the old record
570 $query = qq|UPDATE leads SET
573 push(@values, $form->{id});
575 $query = qq|INSERT INTO leads
579 do_query($form, $dbh, $query, @values);
583 $main::lxdebug->leave_sub();
587 $main::lxdebug->enter_sub();
589 my ($self, $myconfig, $form) = @_;
592 # connect to database
593 my $dbh = $form->dbconnect($myconfig);
595 $query = qq|DELETE FROM leads
597 do_query($form, $dbh, $query, $form->{id});
601 $main::lxdebug->leave_sub();
605 $main::lxdebug->enter_sub();
607 my ($self, $myconfig, $form, $return_list) = @_;
609 # connect to database
610 my $dbh = $form->dbconnect($myconfig);
613 "SELECT id, description, template_code, article_code, " .
614 " output_numberformat, output_dateformat, output_longdates " .
615 "FROM language ORDER BY description";
617 my $sth = $dbh->prepare($query);
618 $sth->execute || $form->dberror($query);
622 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
623 push(@{ $ary }, $ref);
629 $main::lxdebug->leave_sub();
639 $main::lxdebug->enter_sub();
641 my ($self, $myconfig, $form) = @_;
643 # connect to database
644 my $dbh = $form->dbconnect($myconfig);
647 "SELECT description, template_code, article_code, " .
648 " output_numberformat, output_dateformat, output_longdates " .
649 "FROM language WHERE id = ?";
650 my $sth = $dbh->prepare($query);
651 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
653 my $ref = $sth->fetchrow_hashref("NAME_lc");
655 map { $form->{$_} = $ref->{$_} } keys %$ref;
661 $main::lxdebug->leave_sub();
664 sub get_language_details {
665 $main::lxdebug->enter_sub();
667 my ($self, $myconfig, $form, $id) = @_;
669 # connect to database
670 my $dbh = $form->dbconnect($myconfig);
673 "SELECT template_code, " .
674 " output_numberformat, output_dateformat, output_longdates " .
675 "FROM language WHERE id = ?";
676 my @res = selectrow_query($form, $dbh, $query, $id);
679 $main::lxdebug->leave_sub();
685 $main::lxdebug->enter_sub();
687 my ($self, $myconfig, $form) = @_;
689 # connect to database
690 my $dbh = $form->dbconnect($myconfig);
691 my (@values, $query);
693 map({ push(@values, $form->{$_}); }
694 qw(description template_code article_code
695 output_numberformat output_dateformat output_longdates));
697 # id is the old record
700 "UPDATE language SET " .
701 " description = ?, template_code = ?, article_code = ?, " .
702 " output_numberformat = ?, output_dateformat = ?, " .
703 " output_longdates = ? " .
705 push(@values, $form->{id});
708 "INSERT INTO language (" .
709 " description, template_code, article_code, " .
710 " output_numberformat, output_dateformat, output_longdates" .
711 ") VALUES (?, ?, ?, ?, ?, ?)";
713 do_query($form, $dbh, $query, @values);
717 $main::lxdebug->leave_sub();
720 sub delete_language {
721 $main::lxdebug->enter_sub();
723 my ($self, $myconfig, $form) = @_;
726 # connect to database
727 my $dbh = $form->dbconnect_noauto($myconfig);
729 foreach my $table (qw(generic_translations units_language)) {
730 $query = qq|DELETE FROM $table WHERE language_id = ?|;
731 do_query($form, $dbh, $query, $form->{"id"});
734 $query = "DELETE FROM language WHERE id = ?";
735 do_query($form, $dbh, $query, $form->{"id"});
740 $main::lxdebug->leave_sub();
743 sub prepare_template_filename {
744 $main::lxdebug->enter_sub();
746 my ($self, $myconfig, $form) = @_;
748 my ($filename, $display_filename);
750 if ($form->{type} eq "stylesheet") {
751 $filename = "css/$myconfig->{stylesheet}";
752 $display_filename = $myconfig->{stylesheet};
755 $filename = $form->{formname};
757 if ($form->{language}) {
758 my ($id, $template_code) = split(/--/, $form->{language});
759 $filename .= "_${template_code}";
762 if ($form->{printer}) {
763 my ($id, $template_code) = split(/--/, $form->{printer});
764 $filename .= "_${template_code}";
767 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
768 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
769 $filename =~ s|.*/||;
771 $display_filename = $filename;
772 $filename = SL::DB::Default->get->templates . "/$filename";
775 $main::lxdebug->leave_sub();
777 return ($filename, $display_filename);
782 $main::lxdebug->enter_sub();
784 my ($self, $filename) = @_;
786 my ($content, $lines) = ("", 0);
790 if (open(TEMPLATE, $filename)) {
798 $content = Encode::decode('utf-8-strict', $content);
800 $main::lxdebug->leave_sub();
802 return ($content, $lines);
806 $main::lxdebug->enter_sub();
808 my ($self, $filename, $content) = @_;
814 if (open(TEMPLATE, ">", $filename)) {
815 $content = Encode::encode('utf-8-strict', $content);
816 $content =~ s/\r\n/\n/g;
817 print(TEMPLATE $content);
823 $main::lxdebug->leave_sub();
828 sub save_preferences {
829 $main::lxdebug->enter_sub();
831 my ($self, $form) = @_;
833 my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
834 $employee->update_attributes(name => $form->{name});
836 my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
837 $user->update_attributes(
839 %{ $user->config_values },
840 map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
843 $main::lxdebug->leave_sub();
849 $main::lxdebug->enter_sub();
854 my $myconfig = \%main::myconfig;
855 my $form = $main::form;
857 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
859 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
861 $defaults->{weightunit} ||= 'kg';
863 $main::lxdebug->leave_sub();
869 $main::lxdebug->enter_sub();
871 my ($self, $myconfig, $form) = @_;
873 my $dbh = $form->dbconnect($myconfig);
875 my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
876 my $sth = $dbh->prepare($query);
877 $sth->execute || $form->dberror($query);
879 ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
885 $main::lxdebug->leave_sub();
889 $main::lxdebug->enter_sub();
891 my ($self, $myconfig, $form) = @_;
893 my $dbh = $form->dbconnect($myconfig);
895 my ($query, @values);
897 # is currently NEVER trueish (no more hidden revtrans in $form)
898 # if ($form->{revtrans}) {
899 # $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
900 # -> therefore you can only set this to false (which is already the default)
901 # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
903 $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
904 @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
906 # set close in defaults
907 do_query($form, $dbh, $query, @values);
911 $main::lxdebug->leave_sub();
915 my ($self, $units, $unit_name, $factor) = @_;
917 $factor = 1 unless ($factor);
919 my $unit = $units->{$unit_name};
921 if (!defined($unit) || !$unit->{"base_unit"} ||
922 ($unit_name eq $unit->{"base_unit"})) {
923 return ($unit_name, $factor);
926 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
930 $main::lxdebug->enter_sub();
932 my ($self, $myconfig, $form, $prefix) = @_;
935 my $dbh = $form->get_standard_dbh;
937 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
939 my $sth = prepare_execute_query($form, $dbh, $query);
942 while (my $ref = $sth->fetchrow_hashref()) {
943 $units->{$ref->{"name"}} = $ref;
947 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
948 $sth = $dbh->prepare($query_lang);
949 $sth->execute() || $form->dberror($query_lang);
951 while (my $ref = $sth->fetchrow_hashref()) {
952 push(@languages, $ref);
956 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
957 "FROM units_language ul " .
958 "LEFT JOIN language l ON ul.language_id = l.id " .
960 $sth = $dbh->prepare($query_lang);
962 foreach my $unit (values(%{$units})) {
963 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
965 $unit->{"LANGUAGES"} = {};
966 foreach my $lang (@languages) {
967 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
970 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
971 while (my $ref = $sth->fetchrow_hashref()) {
972 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
977 $main::lxdebug->leave_sub();
982 sub retrieve_all_units {
983 $main::lxdebug->enter_sub();
987 if (!$::request->{cache}{all_units}) {
988 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
991 $main::lxdebug->leave_sub();
993 return $::request->{cache}{all_units};
997 sub translate_units {
998 $main::lxdebug->enter_sub();
1000 my ($self, $form, $template_code, $unit, $amount) = @_;
1002 my $units = $self->retrieve_units(\%main::myconfig, $form);
1004 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1005 my $new_unit = $unit;
1007 if (($amount != 1) && $h->{"localized_plural"}) {
1008 $new_unit = $h->{"localized_plural"};
1009 } elsif ($h->{"localized"}) {
1010 $new_unit = $h->{"localized"};
1014 $main::lxdebug->leave_sub();
1020 $main::lxdebug->enter_sub();
1022 my ($self, $myconfig, $form, $units) = @_;
1024 my $dbh = $form->dbconnect($myconfig);
1026 map({ $_->{"in_use"} = 0; } values(%{$units}));
1028 foreach my $unit (values(%{$units})) {
1029 my $base_unit = $unit->{"original_base_unit"};
1030 while ($base_unit) {
1031 $units->{$base_unit}->{"in_use"} = 1;
1032 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1033 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1034 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1038 foreach my $unit (values(%{$units})) {
1039 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1041 foreach my $table (qw(parts invoice orderitems)) {
1042 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1044 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1045 $query .= "= " . $dbh->quote($unit->{"name"});
1047 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1048 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1051 my ($count) = $dbh->selectrow_array($query);
1052 $form->dberror($query) if ($dbh->err);
1055 $unit->{"in_use"} = 1;
1063 $main::lxdebug->leave_sub();
1066 sub convertible_units {
1067 $main::lxdebug->enter_sub();
1071 my $filter_unit = shift;
1072 my $not_smaller = shift;
1074 my $conv_units = [];
1076 $filter_unit = $units->{$filter_unit};
1078 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1079 my $unit = $units->{$name};
1081 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1082 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1083 push @{$conv_units}, $unit;
1087 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1089 $main::lxdebug->leave_sub();
1094 # if $a is translatable to $b, return the factor between them.
1097 $main::lxdebug->enter_sub(2);
1098 my ($this, $a, $b, $all_units) = @_;
1101 $all_units = $this->retrieve_all_units;
1104 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1105 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1106 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1107 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1110 sub unit_select_data {
1111 $main::lxdebug->enter_sub();
1113 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1118 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1121 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1122 if (!$convertible_into ||
1123 ($units->{$convertible_into} &&
1124 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1125 push @{$select}, { "name" => $unit,
1126 "base_unit" => $units->{$unit}->{"base_unit"},
1127 "factor" => $units->{$unit}->{"factor"},
1128 "selected" => ($unit eq $selected) ? "selected" : "" };
1132 $main::lxdebug->leave_sub();
1137 sub unit_select_html {
1138 $main::lxdebug->enter_sub();
1140 my ($self, $units, $name, $selected, $convertible_into) = @_;
1142 my $select = "<select name=${name}>";
1144 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1145 if (!$convertible_into ||
1146 ($units->{$convertible_into} &&
1147 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1148 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1151 $select .= "</select>";
1153 $main::lxdebug->leave_sub();
1159 $main::lxdebug->enter_sub();
1163 my $units = $self->retrieve_all_units();
1168 while (2 <= scalar(@_)) {
1169 my $qty = shift(@_);
1170 my $unit = $units->{shift(@_)};
1172 croak "No unit defined with name $unit" if (!defined $unit);
1175 $base_unit = $unit->{base_unit};
1176 } elsif ($base_unit ne $unit->{base_unit}) {
1177 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1180 $sum += $qty * $unit->{factor};
1183 $main::lxdebug->leave_sub();
1189 $main::lxdebug->enter_sub();
1191 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1193 my $dbh = $form->dbconnect_noauto($myconfig);
1195 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1196 my ($sortkey) = selectrow_query($form, $dbh, $query);
1198 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1199 "VALUES (?, ?, ?, ?)";
1200 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1203 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1204 my $sth = $dbh->prepare($query);
1205 foreach my $lang (@{$languages}) {
1206 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1207 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1215 $main::lxdebug->leave_sub();
1219 $main::lxdebug->enter_sub();
1221 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1223 my $dbh = $form->dbconnect_noauto($myconfig);
1225 my ($base_unit, $unit, $sth, $query);
1227 $query = "DELETE FROM units_language";
1228 $dbh->do($query) || $form->dberror($query);
1230 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1231 $query = "DELETE FROM units WHERE name IN (";
1232 map({ $query .= "?," } @{$delete_units});
1233 substr($query, -1, 1) = ")";
1234 $dbh->do($query, undef, @{$delete_units}) ||
1235 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1238 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1239 $sth = $dbh->prepare($query);
1241 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1242 my $sth_lang = $dbh->prepare($query_lang);
1244 foreach $unit (values(%{$units})) {
1245 $unit->{"depth"} = 0;
1246 my $base_unit = $unit;
1247 while ($base_unit->{"base_unit"}) {
1249 $base_unit = $units->{$base_unit->{"base_unit"}};
1253 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1254 if ($unit->{"LANGUAGES"}) {
1255 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1256 next unless ($lang->{"id"} && $lang->{"localized"});
1257 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1258 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1262 next if ($unit->{"unchanged_unit"});
1264 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1265 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1269 $sth_lang->finish();
1273 $main::lxdebug->leave_sub();
1277 $main::lxdebug->enter_sub();
1279 my ($self, $myconfig, $form) = @_;
1281 # connect to database
1282 my $dbh = $form->dbconnect($myconfig);
1284 my $query = qq|SELECT
1288 round(t.rate * 100, 2) AS rate,
1289 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1290 (SELECT description FROM chart WHERE id = chart_id) AS account_description,
1291 (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
1292 (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
1293 (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
1294 (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
1296 ORDER BY taxkey, rate|;
1298 my $sth = $dbh->prepare($query);
1299 $sth->execute || $form->dberror($query);
1302 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1303 push @{ $form->{TAX} }, $ref;
1309 $main::lxdebug->leave_sub();
1312 sub get_tax_accounts {
1313 $main::lxdebug->enter_sub();
1315 my ($self, $myconfig, $form) = @_;
1317 my $dbh = $form->dbconnect($myconfig);
1319 # get Accounts from chart
1320 my $query = qq{ SELECT
1322 accno || ' - ' || description AS taxaccount
1324 WHERE link LIKE '%_tax%'
1328 my $sth = $dbh->prepare($query);
1329 $sth->execute || $form->dberror($query);
1331 $form->{ACCOUNTS} = [];
1332 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1333 push @{ $form->{ACCOUNTS} }, $ref;
1336 $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
1337 $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
1339 $form->{skontochart_value_title_sub} = sub {
1343 $item->{accno} .' '. $item->{description},
1351 $main::lxdebug->leave_sub();
1355 $main::lxdebug->enter_sub();
1357 my ($self, $myconfig, $form) = @_;
1359 # connect to database
1360 my $dbh = $form->dbconnect($myconfig);
1362 my $query = qq|SELECT
1365 round(rate * 100, 2) AS rate,
1368 (id IN (SELECT tax_id
1369 FROM acc_trans)) AS tax_already_used,
1370 skonto_sales_chart_id,
1371 skonto_purchase_chart_id
1375 my $sth = $dbh->prepare($query);
1376 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1378 my $ref = $sth->fetchrow_hashref("NAME_lc");
1380 map { $form->{$_} = $ref->{$_} } keys %$ref;
1384 # see if it is used by a taxkey
1385 $query = qq|SELECT count(*) FROM taxkeys
1386 WHERE tax_id = ? AND chart_id >0|;
1388 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1390 $form->{orphaned} = !$form->{orphaned};
1393 if (!$form->{orphaned} ) {
1394 $query = qq|SELECT DISTINCT c.id, c.accno
1396 JOIN tax t ON (t.id = tk.tax_id)
1397 JOIN chart c ON (c.id = tk.chart_id)
1398 WHERE tk.tax_id = ?|;
1400 $sth = $dbh->prepare($query);
1401 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1403 $form->{TAXINUSE} = [];
1404 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1405 push @{ $form->{TAXINUSE} }, $ref;
1413 $main::lxdebug->leave_sub();
1417 $main::lxdebug->enter_sub();
1419 my ($self, $myconfig, $form) = @_;
1422 # connect to database
1423 my $dbh = $form->get_standard_dbh($myconfig);
1425 $form->{rate} = $form->{rate} / 100;
1427 my $chart_categories = '';
1428 $chart_categories .= 'A' if $form->{asset};
1429 $chart_categories .= 'L' if $form->{liability};
1430 $chart_categories .= 'Q' if $form->{equity};
1431 $chart_categories .= 'I' if $form->{revenue};
1432 $chart_categories .= 'E' if $form->{expense};
1433 $chart_categories .= 'C' if $form->{costs};
1435 my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, conv_i($form->{chart_id}), conv_i($form->{chart_id}), conv_i($form->{skonto_sales_chart_id}), conv_i($form->{skonto_purchase_chart_id}), $chart_categories);
1436 if ($form->{id} ne "") {
1437 $query = qq|UPDATE tax SET
1442 taxnumber = (SELECT accno FROM chart WHERE id = ? ),
1443 skonto_sales_chart_id = ?,
1444 skonto_purchase_chart_id = ?,
1445 chart_categories = ?
1450 ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
1451 $query = qq|INSERT INTO tax (
1457 skonto_sales_chart_id,
1458 skonto_purchase_chart_id,
1462 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?, ?, ?)|;
1464 push(@values, $form->{id});
1465 do_query($form, $dbh, $query, @values);
1467 foreach my $language_id (keys %{ $form->{translations} }) {
1468 GenericTranslations->save('dbh' => $dbh,
1469 'translation_type' => 'SL::DB::Tax/taxdescription',
1470 'translation_id' => $form->{id},
1471 'language_id' => $language_id,
1472 'translation' => $form->{translations}->{$language_id});
1477 $main::lxdebug->leave_sub();
1481 $main::lxdebug->enter_sub();
1483 my ($self, $myconfig, $form) = @_;
1486 # connect to database
1487 my $dbh = $form->get_standard_dbh($myconfig);
1489 $query = qq|DELETE FROM tax
1491 do_query($form, $dbh, $query, $form->{id});
1495 $main::lxdebug->leave_sub();
1498 sub save_price_factor {
1499 $main::lxdebug->enter_sub();
1501 my ($self, $myconfig, $form) = @_;
1503 # connect to database
1504 my $dbh = $form->get_standard_dbh($myconfig);
1507 my @values = ($form->{description}, conv_i($form->{factor}));
1510 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1511 push @values, conv_i($form->{id});
1514 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1517 do_query($form, $dbh, $query, @values);
1521 $main::lxdebug->leave_sub();
1524 sub get_all_price_factors {
1525 $main::lxdebug->enter_sub();
1527 my ($self, $myconfig, $form) = @_;
1529 # connect to database
1530 my $dbh = $form->get_standard_dbh($myconfig);
1532 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1534 $main::lxdebug->leave_sub();
1537 sub get_price_factor {
1538 $main::lxdebug->enter_sub();
1540 my ($self, $myconfig, $form) = @_;
1542 # connect to database
1543 my $dbh = $form->get_standard_dbh($myconfig);
1545 my $query = qq|SELECT description, factor,
1546 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1547 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1548 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1549 FROM price_factors WHERE id = ?|;
1551 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1553 $main::lxdebug->leave_sub();
1556 sub delete_price_factor {
1557 $main::lxdebug->enter_sub();
1559 my ($self, $myconfig, $form) = @_;
1561 # connect to database
1562 my $dbh = $form->get_standard_dbh($myconfig);
1564 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1567 $main::lxdebug->leave_sub();
1570 sub save_warehouse {
1571 $main::lxdebug->enter_sub();
1573 my ($self, $myconfig, $form) = @_;
1575 # connect to database
1576 my $dbh = $form->get_standard_dbh($myconfig);
1578 my ($query, @values, $sth);
1581 $query = qq|SELECT nextval('id')|;
1582 ($form->{id}) = selectrow_query($form, $dbh, $query);
1584 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1585 do_query($form, $dbh, $query, $form->{id});
1588 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1589 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1591 if (0 < $form->{number_of_new_bins}) {
1592 my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1593 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1594 $sth = prepare_query($form, $dbh, $query);
1596 foreach my $i (1..$form->{number_of_new_bins}) {
1597 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1605 $main::lxdebug->leave_sub();
1609 $main::lxdebug->enter_sub();
1611 my ($self, $myconfig, $form) = @_;
1613 # connect to database
1614 my $dbh = $form->get_standard_dbh($myconfig);
1616 my ($query, @values, $commit_necessary, $sth);
1618 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1621 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1622 do_query($form, $dbh, $query, @values);
1624 $commit_necessary = 1;
1627 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1628 $sth = prepare_query($form, $dbh, $query);
1630 foreach my $row (1..$form->{rowcount}) {
1631 next if ($form->{"delete_${row}"});
1633 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1635 $commit_necessary = 1;
1640 $dbh->commit() if ($commit_necessary);
1642 $main::lxdebug->leave_sub();
1645 sub delete_warehouse {
1646 $main::lxdebug->enter_sub();
1648 my ($self, $myconfig, $form) = @_;
1650 # connect to database
1651 my $dbh = $form->get_standard_dbh($myconfig);
1653 my $id = conv_i($form->{id});
1654 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|;
1655 my ($count) = selectrow_query($form, $dbh, $query, $id);
1658 $main::lxdebug->leave_sub();
1662 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
1663 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
1667 $main::lxdebug->leave_sub();
1672 sub get_all_warehouses {
1673 $main::lxdebug->enter_sub();
1675 my ($self, $myconfig, $form) = @_;
1677 # connect to database
1678 my $dbh = $form->get_standard_dbh($myconfig);
1680 my $query = qq|SELECT w.id, w.description, w.invalid,
1681 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
1683 ORDER BY w.sortkey|;
1685 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
1687 $main::lxdebug->leave_sub();
1691 $main::lxdebug->enter_sub();
1693 my ($self, $myconfig, $form) = @_;
1695 # connect to database
1696 my $dbh = $form->get_standard_dbh($myconfig);
1698 my $id = conv_i($form->{id});
1699 my $query = qq|SELECT w.description, w.invalid
1703 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1705 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1709 ( EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
1710 OR EXISTS(SELECT p.bin_id FROM parts p WHERE p.bin_id = b.id LIMIT 1))
1713 WHERE b.warehouse_id = ?
1716 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
1718 $main::lxdebug->leave_sub();