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.pos_er, 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
312 $form->{description},
316 conv_i($form->{pos_bwa}),
317 conv_i($form->{pos_bilanz}),
318 conv_i($form->{pos_eur}),
319 conv_i($form->{pos_er}),
320 conv_i($form->{new_chart_id}),
321 conv_date($form->{valid_from}),
322 ($form->{datevautomatik} eq 'T') ? 'true':'false',
329 do_query($form, $dbh, $query, @values);
335 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
339 for $tk_count (0 .. $MAX_TRIES) {
343 # Check if the account already exists, else cancel
345 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
347 last READTAXKEYS if ( $form->{'id'} == 0);
349 # check if there is a startdate
350 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
355 # Add valid taxkeys into the array
358 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
359 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
360 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
361 chart_id => conv_i($form->{"id"}),
362 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
363 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
370 for my $j (0 .. $#taxkeys){
371 if ( defined $taxkeys[$j]{'id'} ){
374 if ($taxkeys[$j]{'delete'}){
376 DELETE FROM taxkeys WHERE id = ?
379 @values = ($taxkeys[$j]{'id'});
381 do_query($form, $dbh, $query, @values);
390 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
398 $taxkeys[$j]{'tax_id'},
399 $taxkeys[$j]{'chart_id'},
400 $taxkeys[$j]{'tax_id'},
401 $taxkeys[$j]{'pos_ustva'},
402 $taxkeys[$j]{'startdate'},
405 do_query($form, $dbh, $query, @values);
411 INSERT INTO taxkeys (
418 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
421 $taxkeys[$j]{'tax_id'},
422 $taxkeys[$j]{'chart_id'},
423 $taxkeys[$j]{'tax_id'},
424 $taxkeys[$j]{'pos_ustva'},
425 $taxkeys[$j]{'startdate'},
428 do_query($form, $dbh, $query, @values);
433 # Update chart.taxkey_id to the latest from taxkeys for this chart.
439 WHERE taxkeys.chart_id = chart.id
440 ORDER BY startdate DESC
446 do_query($form, $dbh, $query, $form->{id});
449 my $rc = $dbh->commit;
452 $main::lxdebug->leave_sub();
458 $main::lxdebug->enter_sub();
460 my ($self, $myconfig, $form) = @_;
462 # connect to database, turn off AutoCommit
463 my $dbh = $form->dbconnect_noauto($myconfig);
465 my $query = qq|SELECT count(*) FROM acc_trans a
466 WHERE a.chart_id = ?|;
467 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
471 $main::lxdebug->leave_sub();
475 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
476 foreach my $type (qw(inventory income expense)) {
479 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
480 qq|WHERE ${type}_accno_id = ?|;
481 do_query($form, $dbh, $query, $form->{id});
484 $query = qq|DELETE FROM tax
486 do_query($form, $dbh, $query, $form->{id});
488 # delete account taxkeys
489 $query = qq|DELETE FROM taxkeys
491 do_query($form, $dbh, $query, $form->{id});
493 # delete chart of account record
494 # last step delete chart, because we have a constraint
496 $query = qq|DELETE FROM chart
498 do_query($form, $dbh, $query, $form->{id});
500 # commit and redirect
501 my $rc = $dbh->commit;
504 $main::lxdebug->leave_sub();
510 $main::lxdebug->enter_sub();
512 my ($self, $myconfig, $form) = @_;
514 # connect to database
515 my $dbh = $form->dbconnect($myconfig);
517 my $query = qq|SELECT id, lead
521 my $sth = $dbh->prepare($query);
522 $sth->execute || $form->dberror($query);
524 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
525 push @{ $form->{ALL} }, $ref;
531 $main::lxdebug->leave_sub();
535 $main::lxdebug->enter_sub();
537 my ($self, $myconfig, $form) = @_;
539 # connect to database
540 my $dbh = $form->dbconnect($myconfig);
543 qq|SELECT l.id, l.lead | .
546 my $sth = $dbh->prepare($query);
547 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
549 my $ref = $sth->fetchrow_hashref("NAME_lc");
551 map { $form->{$_} = $ref->{$_} } keys %$ref;
557 $main::lxdebug->leave_sub();
561 $main::lxdebug->enter_sub();
563 my ($self, $myconfig, $form) = @_;
566 # connect to database
567 my $dbh = $form->dbconnect($myconfig);
569 my @values = ($form->{description});
570 # id is the old record
572 $query = qq|UPDATE leads SET
575 push(@values, $form->{id});
577 $query = qq|INSERT INTO leads
581 do_query($form, $dbh, $query, @values);
585 $main::lxdebug->leave_sub();
589 $main::lxdebug->enter_sub();
591 my ($self, $myconfig, $form) = @_;
594 # connect to database
595 my $dbh = $form->dbconnect($myconfig);
597 $query = qq|DELETE FROM leads
599 do_query($form, $dbh, $query, $form->{id});
603 $main::lxdebug->leave_sub();
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form, $return_list) = @_;
611 # connect to database
612 my $dbh = $form->dbconnect($myconfig);
615 "SELECT id, description, template_code, article_code, " .
616 " output_numberformat, output_dateformat, output_longdates " .
617 "FROM language ORDER BY description";
619 my $sth = $dbh->prepare($query);
620 $sth->execute || $form->dberror($query);
624 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
625 push(@{ $ary }, $ref);
631 $main::lxdebug->leave_sub();
641 $main::lxdebug->enter_sub();
643 my ($self, $myconfig, $form) = @_;
645 # connect to database
646 my $dbh = $form->dbconnect($myconfig);
649 "SELECT description, template_code, article_code, " .
650 " output_numberformat, output_dateformat, output_longdates " .
651 "FROM language WHERE id = ?";
652 my $sth = $dbh->prepare($query);
653 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
655 my $ref = $sth->fetchrow_hashref("NAME_lc");
657 map { $form->{$_} = $ref->{$_} } keys %$ref;
663 $main::lxdebug->leave_sub();
666 sub get_language_details {
667 $main::lxdebug->enter_sub();
669 my ($self, $myconfig, $form, $id) = @_;
671 # connect to database
672 my $dbh = $form->dbconnect($myconfig);
675 "SELECT template_code, " .
676 " output_numberformat, output_dateformat, output_longdates " .
677 "FROM language WHERE id = ?";
678 my @res = selectrow_query($form, $dbh, $query, $id);
681 $main::lxdebug->leave_sub();
687 $main::lxdebug->enter_sub();
689 my ($self, $myconfig, $form) = @_;
691 # connect to database
692 my $dbh = $form->dbconnect($myconfig);
693 my (@values, $query);
695 map({ push(@values, $form->{$_}); }
696 qw(description template_code article_code
697 output_numberformat output_dateformat output_longdates));
699 # id is the old record
702 "UPDATE language SET " .
703 " description = ?, template_code = ?, article_code = ?, " .
704 " output_numberformat = ?, output_dateformat = ?, " .
705 " output_longdates = ? " .
707 push(@values, $form->{id});
710 "INSERT INTO language (" .
711 " description, template_code, article_code, " .
712 " output_numberformat, output_dateformat, output_longdates" .
713 ") VALUES (?, ?, ?, ?, ?, ?)";
715 do_query($form, $dbh, $query, @values);
719 $main::lxdebug->leave_sub();
722 sub delete_language {
723 $main::lxdebug->enter_sub();
725 my ($self, $myconfig, $form) = @_;
728 # connect to database
729 my $dbh = $form->dbconnect_noauto($myconfig);
731 foreach my $table (qw(generic_translations units_language)) {
732 $query = qq|DELETE FROM $table WHERE language_id = ?|;
733 do_query($form, $dbh, $query, $form->{"id"});
736 $query = "DELETE FROM language WHERE id = ?";
737 do_query($form, $dbh, $query, $form->{"id"});
742 $main::lxdebug->leave_sub();
745 sub prepare_template_filename {
746 $main::lxdebug->enter_sub();
748 my ($self, $myconfig, $form) = @_;
750 my ($filename, $display_filename);
752 if ($form->{type} eq "stylesheet") {
753 $filename = "css/$myconfig->{stylesheet}";
754 $display_filename = $myconfig->{stylesheet};
757 $filename = $form->{formname};
759 if ($form->{language}) {
760 my ($id, $template_code) = split(/--/, $form->{language});
761 $filename .= "_${template_code}";
764 if ($form->{printer}) {
765 my ($id, $template_code) = split(/--/, $form->{printer});
766 $filename .= "_${template_code}";
769 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
770 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
771 $filename =~ s|.*/||;
773 $display_filename = $filename;
774 $filename = SL::DB::Default->get->templates . "/$filename";
777 $main::lxdebug->leave_sub();
779 return ($filename, $display_filename);
784 $main::lxdebug->enter_sub();
786 my ($self, $filename) = @_;
788 my ($content, $lines) = ("", 0);
792 if (open(TEMPLATE, $filename)) {
800 $content = Encode::decode('utf-8-strict', $content);
802 $main::lxdebug->leave_sub();
804 return ($content, $lines);
808 $main::lxdebug->enter_sub();
810 my ($self, $filename, $content) = @_;
816 if (open(TEMPLATE, ">", $filename)) {
817 $content = Encode::encode('utf-8-strict', $content);
818 $content =~ s/\r\n/\n/g;
819 print(TEMPLATE $content);
825 $main::lxdebug->leave_sub();
830 sub save_preferences {
831 $main::lxdebug->enter_sub();
833 my ($self, $form) = @_;
835 my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
836 $employee->update_attributes(name => $form->{name});
838 my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
839 $user->update_attributes(
841 %{ $user->config_values },
842 map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
845 $main::lxdebug->leave_sub();
851 $main::lxdebug->enter_sub();
856 my $myconfig = \%main::myconfig;
857 my $form = $main::form;
859 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
861 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
863 $defaults->{weightunit} ||= 'kg';
865 $main::lxdebug->leave_sub();
871 $main::lxdebug->enter_sub();
873 my ($self, $myconfig, $form) = @_;
875 my $dbh = $form->dbconnect($myconfig);
877 my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
878 my $sth = $dbh->prepare($query);
879 $sth->execute || $form->dberror($query);
881 ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
887 $main::lxdebug->leave_sub();
891 $main::lxdebug->enter_sub();
893 my ($self, $myconfig, $form) = @_;
895 my $dbh = $form->dbconnect($myconfig);
897 my ($query, @values);
899 # is currently NEVER trueish (no more hidden revtrans in $form)
900 # if ($form->{revtrans}) {
901 # $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
902 # -> therefore you can only set this to false (which is already the default)
903 # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
905 $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
906 @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
908 # set close in defaults
909 do_query($form, $dbh, $query, @values);
913 $main::lxdebug->leave_sub();
917 my ($self, $units, $unit_name, $factor) = @_;
919 $factor = 1 unless ($factor);
921 my $unit = $units->{$unit_name};
923 if (!defined($unit) || !$unit->{"base_unit"} ||
924 ($unit_name eq $unit->{"base_unit"})) {
925 return ($unit_name, $factor);
928 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
932 $main::lxdebug->enter_sub();
934 my ($self, $myconfig, $form, $prefix) = @_;
937 my $dbh = $form->get_standard_dbh;
939 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
941 my $sth = prepare_execute_query($form, $dbh, $query);
944 while (my $ref = $sth->fetchrow_hashref()) {
945 $units->{$ref->{"name"}} = $ref;
949 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
950 $sth = $dbh->prepare($query_lang);
951 $sth->execute() || $form->dberror($query_lang);
953 while (my $ref = $sth->fetchrow_hashref()) {
954 push(@languages, $ref);
958 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
959 "FROM units_language ul " .
960 "LEFT JOIN language l ON ul.language_id = l.id " .
962 $sth = $dbh->prepare($query_lang);
964 foreach my $unit (values(%{$units})) {
965 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
967 $unit->{"LANGUAGES"} = {};
968 foreach my $lang (@languages) {
969 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
972 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
973 while (my $ref = $sth->fetchrow_hashref()) {
974 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
979 $main::lxdebug->leave_sub();
984 sub retrieve_all_units {
985 $main::lxdebug->enter_sub();
989 if (!$::request->{cache}{all_units}) {
990 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
993 $main::lxdebug->leave_sub();
995 return $::request->{cache}{all_units};
999 sub translate_units {
1000 $main::lxdebug->enter_sub();
1002 my ($self, $form, $template_code, $unit, $amount) = @_;
1004 my $units = $self->retrieve_units(\%main::myconfig, $form);
1006 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1007 my $new_unit = $unit;
1009 if (($amount != 1) && $h->{"localized_plural"}) {
1010 $new_unit = $h->{"localized_plural"};
1011 } elsif ($h->{"localized"}) {
1012 $new_unit = $h->{"localized"};
1016 $main::lxdebug->leave_sub();
1022 $main::lxdebug->enter_sub();
1024 my ($self, $myconfig, $form, $units) = @_;
1026 my $dbh = $form->dbconnect($myconfig);
1028 map({ $_->{"in_use"} = 0; } values(%{$units}));
1030 foreach my $unit (values(%{$units})) {
1031 my $base_unit = $unit->{"original_base_unit"};
1032 while ($base_unit) {
1033 $units->{$base_unit}->{"in_use"} = 1;
1034 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1035 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1036 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1040 foreach my $unit (values(%{$units})) {
1041 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1043 foreach my $table (qw(parts invoice orderitems)) {
1044 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1046 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1047 $query .= "= " . $dbh->quote($unit->{"name"});
1049 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1050 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1053 my ($count) = $dbh->selectrow_array($query);
1054 $form->dberror($query) if ($dbh->err);
1057 $unit->{"in_use"} = 1;
1065 $main::lxdebug->leave_sub();
1068 sub convertible_units {
1069 $main::lxdebug->enter_sub();
1073 my $filter_unit = shift;
1074 my $not_smaller = shift;
1076 my $conv_units = [];
1078 $filter_unit = $units->{$filter_unit};
1080 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1081 my $unit = $units->{$name};
1083 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1084 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1085 push @{$conv_units}, $unit;
1089 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1091 $main::lxdebug->leave_sub();
1096 # if $a is translatable to $b, return the factor between them.
1099 $main::lxdebug->enter_sub(2);
1100 my ($this, $a, $b, $all_units) = @_;
1103 $all_units = $this->retrieve_all_units;
1106 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1107 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1108 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1109 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1112 sub unit_select_data {
1113 $main::lxdebug->enter_sub();
1115 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1120 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1123 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1124 if (!$convertible_into ||
1125 ($units->{$convertible_into} &&
1126 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1127 push @{$select}, { "name" => $unit,
1128 "base_unit" => $units->{$unit}->{"base_unit"},
1129 "factor" => $units->{$unit}->{"factor"},
1130 "selected" => ($unit eq $selected) ? "selected" : "" };
1134 $main::lxdebug->leave_sub();
1139 sub unit_select_html {
1140 $main::lxdebug->enter_sub();
1142 my ($self, $units, $name, $selected, $convertible_into) = @_;
1144 my $select = "<select name=${name}>";
1146 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1147 if (!$convertible_into ||
1148 ($units->{$convertible_into} &&
1149 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1150 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1153 $select .= "</select>";
1155 $main::lxdebug->leave_sub();
1161 $main::lxdebug->enter_sub();
1165 my $units = $self->retrieve_all_units();
1170 while (2 <= scalar(@_)) {
1171 my $qty = shift(@_);
1172 my $unit = $units->{shift(@_)};
1174 croak "No unit defined with name $unit" if (!defined $unit);
1177 $base_unit = $unit->{base_unit};
1178 } elsif ($base_unit ne $unit->{base_unit}) {
1179 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1182 $sum += $qty * $unit->{factor};
1185 $main::lxdebug->leave_sub();
1191 $main::lxdebug->enter_sub();
1193 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1195 my $dbh = $form->dbconnect_noauto($myconfig);
1197 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1198 my ($sortkey) = selectrow_query($form, $dbh, $query);
1200 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1201 "VALUES (?, ?, ?, ?)";
1202 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1205 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1206 my $sth = $dbh->prepare($query);
1207 foreach my $lang (@{$languages}) {
1208 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1209 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1217 $main::lxdebug->leave_sub();
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig, $form, $units, $delete_units) = @_;
1225 my $dbh = $form->dbconnect_noauto($myconfig);
1227 my ($base_unit, $unit, $sth, $query);
1229 $query = "DELETE FROM units_language";
1230 $dbh->do($query) || $form->dberror($query);
1232 if ($delete_units && (0 != scalar(@{$delete_units}))) {
1233 $query = "DELETE FROM units WHERE name IN (";
1234 map({ $query .= "?," } @{$delete_units});
1235 substr($query, -1, 1) = ")";
1236 $dbh->do($query, undef, @{$delete_units}) ||
1237 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1240 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1241 $sth = $dbh->prepare($query);
1243 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1244 my $sth_lang = $dbh->prepare($query_lang);
1246 foreach $unit (values(%{$units})) {
1247 $unit->{"depth"} = 0;
1248 my $base_unit = $unit;
1249 while ($base_unit->{"base_unit"}) {
1251 $base_unit = $units->{$base_unit->{"base_unit"}};
1255 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1256 if ($unit->{"LANGUAGES"}) {
1257 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1258 next unless ($lang->{"id"} && $lang->{"localized"});
1259 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1260 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1264 next if ($unit->{"unchanged_unit"});
1266 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1267 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1271 $sth_lang->finish();
1275 $main::lxdebug->leave_sub();
1279 $main::lxdebug->enter_sub();
1281 my ($self, $myconfig, $form) = @_;
1283 # connect to database
1284 my $dbh = $form->dbconnect($myconfig);
1286 my $query = qq|SELECT
1290 round(t.rate * 100, 2) AS rate,
1291 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1292 (SELECT description FROM chart WHERE id = chart_id) AS account_description,
1293 (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
1294 (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
1295 (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
1296 (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
1298 ORDER BY taxkey, rate|;
1300 my $sth = $dbh->prepare($query);
1301 $sth->execute || $form->dberror($query);
1304 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1305 push @{ $form->{TAX} }, $ref;
1311 $main::lxdebug->leave_sub();
1314 sub get_tax_accounts {
1315 $main::lxdebug->enter_sub();
1317 my ($self, $myconfig, $form) = @_;
1319 my $dbh = $form->dbconnect($myconfig);
1321 # get Accounts from chart
1322 my $query = qq{ SELECT
1324 accno || ' - ' || description AS taxaccount
1326 WHERE link LIKE '%_tax%'
1330 my $sth = $dbh->prepare($query);
1331 $sth->execute || $form->dberror($query);
1333 $form->{ACCOUNTS} = [];
1334 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1335 push @{ $form->{ACCOUNTS} }, $ref;
1338 $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
1339 $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
1341 $form->{skontochart_value_title_sub} = sub {
1345 $item->{accno} .' '. $item->{description},
1353 $main::lxdebug->leave_sub();
1357 $main::lxdebug->enter_sub();
1359 my ($self, $myconfig, $form) = @_;
1361 # connect to database
1362 my $dbh = $form->dbconnect($myconfig);
1364 my $query = qq|SELECT
1367 round(rate * 100, 2) AS rate,
1370 (id IN (SELECT tax_id
1371 FROM acc_trans)) AS tax_already_used,
1372 skonto_sales_chart_id,
1373 skonto_purchase_chart_id
1377 my $sth = $dbh->prepare($query);
1378 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1380 my $ref = $sth->fetchrow_hashref("NAME_lc");
1382 map { $form->{$_} = $ref->{$_} } keys %$ref;
1386 # see if it is used by a taxkey
1387 $query = qq|SELECT count(*) FROM taxkeys
1388 WHERE tax_id = ? AND chart_id >0|;
1390 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1392 $form->{orphaned} = !$form->{orphaned};
1395 if (!$form->{orphaned} ) {
1396 $query = qq|SELECT DISTINCT c.id, c.accno
1398 JOIN tax t ON (t.id = tk.tax_id)
1399 JOIN chart c ON (c.id = tk.chart_id)
1400 WHERE tk.tax_id = ?|;
1402 $sth = $dbh->prepare($query);
1403 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1405 $form->{TAXINUSE} = [];
1406 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1407 push @{ $form->{TAXINUSE} }, $ref;
1415 $main::lxdebug->leave_sub();
1419 $main::lxdebug->enter_sub();
1421 my ($self, $myconfig, $form) = @_;
1424 # connect to database
1425 my $dbh = $form->get_standard_dbh($myconfig);
1427 $form->{rate} = $form->{rate} / 100;
1429 my $chart_categories = '';
1430 $chart_categories .= 'A' if $form->{asset};
1431 $chart_categories .= 'L' if $form->{liability};
1432 $chart_categories .= 'Q' if $form->{equity};
1433 $chart_categories .= 'I' if $form->{revenue};
1434 $chart_categories .= 'E' if $form->{expense};
1435 $chart_categories .= 'C' if $form->{costs};
1437 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);
1438 if ($form->{id} ne "") {
1439 $query = qq|UPDATE tax SET
1444 taxnumber = (SELECT accno FROM chart WHERE id = ? ),
1445 skonto_sales_chart_id = ?,
1446 skonto_purchase_chart_id = ?,
1447 chart_categories = ?
1452 ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
1453 $query = qq|INSERT INTO tax (
1459 skonto_sales_chart_id,
1460 skonto_purchase_chart_id,
1464 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?, ?, ?)|;
1466 push(@values, $form->{id});
1467 do_query($form, $dbh, $query, @values);
1469 foreach my $language_id (keys %{ $form->{translations} }) {
1470 GenericTranslations->save('dbh' => $dbh,
1471 'translation_type' => 'SL::DB::Tax/taxdescription',
1472 'translation_id' => $form->{id},
1473 'language_id' => $language_id,
1474 'translation' => $form->{translations}->{$language_id});
1479 $main::lxdebug->leave_sub();
1483 $main::lxdebug->enter_sub();
1485 my ($self, $myconfig, $form) = @_;
1488 # connect to database
1489 my $dbh = $form->get_standard_dbh($myconfig);
1491 $query = qq|DELETE FROM tax
1493 do_query($form, $dbh, $query, $form->{id});
1497 $main::lxdebug->leave_sub();
1500 sub save_price_factor {
1501 $main::lxdebug->enter_sub();
1503 my ($self, $myconfig, $form) = @_;
1505 # connect to database
1506 my $dbh = $form->get_standard_dbh($myconfig);
1509 my @values = ($form->{description}, conv_i($form->{factor}));
1512 $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1513 push @values, conv_i($form->{id});
1516 $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1519 do_query($form, $dbh, $query, @values);
1523 $main::lxdebug->leave_sub();
1526 sub get_all_price_factors {
1527 $main::lxdebug->enter_sub();
1529 my ($self, $myconfig, $form) = @_;
1531 # connect to database
1532 my $dbh = $form->get_standard_dbh($myconfig);
1534 $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1536 $main::lxdebug->leave_sub();
1539 sub get_price_factor {
1540 $main::lxdebug->enter_sub();
1542 my ($self, $myconfig, $form) = @_;
1544 # connect to database
1545 my $dbh = $form->get_standard_dbh($myconfig);
1547 my $query = qq|SELECT description, factor,
1548 ((SELECT COUNT(*) FROM parts WHERE price_factor_id = ?) +
1549 (SELECT COUNT(*) FROM invoice WHERE price_factor_id = ?) +
1550 (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1551 FROM price_factors WHERE id = ?|;
1553 ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1555 $main::lxdebug->leave_sub();
1558 sub delete_price_factor {
1559 $main::lxdebug->enter_sub();
1561 my ($self, $myconfig, $form) = @_;
1563 # connect to database
1564 my $dbh = $form->get_standard_dbh($myconfig);
1566 do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1569 $main::lxdebug->leave_sub();
1572 sub save_warehouse {
1573 $main::lxdebug->enter_sub();
1575 my ($self, $myconfig, $form) = @_;
1577 # connect to database
1578 my $dbh = $form->get_standard_dbh($myconfig);
1580 my ($query, @values, $sth);
1583 $query = qq|SELECT nextval('id')|;
1584 ($form->{id}) = selectrow_query($form, $dbh, $query);
1586 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1587 do_query($form, $dbh, $query, $form->{id});
1590 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1591 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1593 if (0 < $form->{number_of_new_bins}) {
1594 my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1595 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1596 $sth = prepare_query($form, $dbh, $query);
1598 foreach my $i (1..$form->{number_of_new_bins}) {
1599 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1607 $main::lxdebug->leave_sub();
1611 $main::lxdebug->enter_sub();
1613 my ($self, $myconfig, $form) = @_;
1615 # connect to database
1616 my $dbh = $form->get_standard_dbh($myconfig);
1618 my ($query, @values, $commit_necessary, $sth);
1620 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1623 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1624 do_query($form, $dbh, $query, @values);
1626 $commit_necessary = 1;
1629 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1630 $sth = prepare_query($form, $dbh, $query);
1632 foreach my $row (1..$form->{rowcount}) {
1633 next if ($form->{"delete_${row}"});
1635 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1637 $commit_necessary = 1;
1642 $dbh->commit() if ($commit_necessary);
1644 $main::lxdebug->leave_sub();
1647 sub delete_warehouse {
1648 $main::lxdebug->enter_sub();
1650 my ($self, $myconfig, $form) = @_;
1652 # connect to database
1653 my $dbh = $form->get_standard_dbh($myconfig);
1655 my $id = conv_i($form->{id});
1656 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|;
1657 my ($count) = selectrow_query($form, $dbh, $query, $id);
1660 $main::lxdebug->leave_sub();
1664 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
1665 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
1669 $main::lxdebug->leave_sub();
1674 sub get_all_warehouses {
1675 $main::lxdebug->enter_sub();
1677 my ($self, $myconfig, $form) = @_;
1679 # connect to database
1680 my $dbh = $form->get_standard_dbh($myconfig);
1682 my $query = qq|SELECT w.id, w.description, w.invalid,
1683 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
1685 ORDER BY w.sortkey|;
1687 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
1689 $main::lxdebug->leave_sub();
1693 $main::lxdebug->enter_sub();
1695 my ($self, $myconfig, $form) = @_;
1697 # connect to database
1698 my $dbh = $form->get_standard_dbh($myconfig);
1700 my $id = conv_i($form->{id});
1701 my $query = qq|SELECT w.description, w.invalid
1705 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1707 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1711 ( EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
1712 OR EXISTS(SELECT p.bin_id FROM parts p WHERE p.bin_id = b.id LIMIT 1))
1715 WHERE b.warehouse_id = ?
1718 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
1720 $main::lxdebug->leave_sub();