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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # Administration module
37 #======================================================================
44 use List::MoreUtils qw(any);
51 use SL::GenericTranslations;
56 $main::lxdebug->enter_sub();
58 # fetch chart-related data and set form fields
59 # get_account is called by add_account in am.pl
60 # always sets $form->{TAXKEY} and default_accounts
61 # loads chart data when $form->{id} is passed
63 my ($self, $myconfig, $form) = @_;
65 # get default accounts
66 map { $form->{$_} = $::instance_conf->{$_} } qw(inventory_accno_id income_accno_id expense_accno_id);
69 my $taxes = SL::DB::Manager::Tax->get_all( with_objects => ['chart'] , sort_by => 'taxkey' );
71 foreach my $tk ( @{$taxes} ) {
72 push @{ $form->{TAXKEY} }, { id => $tk->id,
73 chart_accno => $tk->chart_id ? $tk->chart->accno : undef,
74 taxkey => $tk->taxkey,
75 tax => $tk->id . '--' . $tk->taxkey,
82 my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart";
84 my @chart_fields = qw(accno description charttype category link pos_bilanz
85 pos_eur pos_er new_chart_id valid_from pos_bwa datevautomatik);
86 foreach my $cf ( @chart_fields ) {
87 $form->{"$cf"} = $chart_obj->$cf;
90 my $active_taxkey = $chart_obj->get_active_taxkey;
91 $form->{$_} = $active_taxkey->$_ foreach qw(taxkey_id pos_ustva tax_id startdate);
92 $form->{tax} = $active_taxkey->tax_id . '--' . $active_taxkey->taxkey_id;
94 # check if there are any transactions for this chart
95 $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1;
97 # check if new account is active
98 # The old sql query was broken since at least 2006 and always returned 0
99 $form->{new_chart_valid} = $chart_obj->new_chart_valid;
101 # get the taxkeys of the account
102 $form->{ACCOUNT_TAXKEYS} = [];
103 foreach my $taxkey ( @{ $chart_obj->taxkeys } ) {
104 push @{ $form->{ACCOUNT_TAXKEYS} }, { id => $taxkey->id,
105 chart_id => $taxkey->chart_id,
106 tax_id => $taxkey->tax_id,
107 taxkey_id => $taxkey->taxkey_id,
108 pos_ustva => $taxkey->pos_ustva,
109 startdate => $taxkey->startdate->to_kivitendo,
110 taxdescription => $taxkey->tax->taxdescription,
111 rate => $taxkey->tax->rate,
112 accno => defined $taxkey->tax->chart_id ? $taxkey->tax->chart->accno : undef,
116 # get new accounts (Folgekonto). Find all charts with the same link
117 $form->{NEWACCOUNT} = $chart_obj->db->dbh->selectall_arrayref('select id, accno,description from chart where link = ? order by accno', {Slice => {}}, $chart_obj->link);
119 } else { # set to orphaned for new charts, so chart_type can be changed (needed by $AccountIsPosted)
120 $form->{orphaned} = 1;
123 $main::lxdebug->leave_sub();
127 my ($self, $myconfig, $form) = @_;
128 $main::lxdebug->enter_sub();
130 my $rc = SL::DB->client->with_transaction(\&_save_account, $self, $myconfig, $form);
132 $::lxdebug->leave_sub;
137 # TODO: it should be forbidden to change an account to a heading if there
138 # have been bookings to this account in the past
140 my ($self, $myconfig, $form) = @_;
142 my $dbh = SL::DB->client->dbh;
144 for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
145 $form->{$form->{$_}} = $form->{$_} if $form->{$_};
148 # sanity check, can't have AR with AR_...
149 if ($form->{AR} || $form->{AP} || $form->{IC}) {
150 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)) {
151 $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
155 my @link_order = qw(AR AR_amount AR_tax AR_paid AP AP_amount AP_tax AP_paid IC IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice);
156 $form->{link} = join ':', grep $_, map $form->{$_}, @link_order;
158 # strip blanks from accno
159 map { $form->{$_} =~ s/ //g; } qw(accno);
161 # collapse multiple (horizontal) whitespace in chart description (Ticket 148)
162 map { $form->{$_} =~ s/\h+/ /g } qw(description);
166 if ($form->{id} eq "NULL") {
175 my @values = ($form->{accno});
178 $query .= ' AND NOT id = ?';
179 push(@values, $form->{id});
182 my ($accno) = selectrow_query($form, $dbh, $query, @values);
185 $form->error($::locale->text('Account number not unique!'));
189 if (!$form->{id} || $form->{id} eq "") {
190 $query = qq|SELECT nextval('id')|;
191 ($form->{"id"}) = selectrow_query($form, $dbh, $query);
192 $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
193 do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
201 # if charttype is heading make sure certain values are empty
202 # specifically, if charttype is changed from an existing account, empty the
203 # fields unnecessary for headings, so that e.g. heading doesn't appear in
204 # drop-down menues due to still having a valid "link" entry
206 if ( $form->{charttype} eq 'H' ) {
208 $form->{pos_bwa} = '';
209 $form->{pos_bilanz} = '';
210 $form->{pos_eur} = '';
211 $form->{new_chart_id} = '';
212 $form->{valid_from} = '';
215 $query = qq|UPDATE chart SET
232 $form->{description},
236 conv_i($form->{pos_bwa}),
237 conv_i($form->{pos_bilanz}),
238 conv_i($form->{pos_eur}),
239 conv_i($form->{pos_er}),
240 conv_i($form->{new_chart_id}),
241 conv_date($form->{valid_from}),
242 ($form->{datevautomatik} eq 'T') ? 'true':'false',
249 do_query($form, $dbh, $query, @values);
255 my $MAX_TRIES = 10; # Maximum count of taxkeys in form
259 for $tk_count (0 .. $MAX_TRIES) {
263 # Check if the account already exists, else cancel
265 print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
267 last READTAXKEYS if ( $form->{'id'} == 0);
269 # check if there is a startdate
270 if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
275 # Add valid taxkeys into the array
278 id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
279 tax_id => conv_i($form->{"taxkey_tax_$tk_count"}),
280 startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
281 chart_id => conv_i($form->{"id"}),
282 pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
283 delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
290 for my $j (0 .. $#taxkeys){
291 if ( defined $taxkeys[$j]{'id'} ){
294 if ($taxkeys[$j]{'delete'}){
296 DELETE FROM taxkeys WHERE id = ?
299 @values = ($taxkeys[$j]{'id'});
301 do_query($form, $dbh, $query, @values);
310 SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
318 $taxkeys[$j]{'tax_id'},
319 $taxkeys[$j]{'chart_id'},
320 $taxkeys[$j]{'tax_id'},
321 $taxkeys[$j]{'pos_ustva'},
322 $taxkeys[$j]{'startdate'},
325 do_query($form, $dbh, $query, @values);
331 INSERT INTO taxkeys (
338 VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
341 $taxkeys[$j]{'tax_id'},
342 $taxkeys[$j]{'chart_id'},
343 $taxkeys[$j]{'tax_id'},
344 $taxkeys[$j]{'pos_ustva'},
345 $taxkeys[$j]{'startdate'},
348 do_query($form, $dbh, $query, @values);
353 # Update chart.taxkey_id to the latest from taxkeys for this chart.
359 WHERE taxkeys.chart_id = chart.id
360 ORDER BY startdate DESC
366 do_query($form, $dbh, $query, $form->{id});
372 my ($self, $myconfig, $form) = @_;
373 $main::lxdebug->enter_sub();
375 my $rc = SL::DB->client->with_transaction(\&_delete_account, $self, $myconfig, $form);
377 $::lxdebug->leave_sub;
381 sub _delete_account {
382 my ($self, $myconfig, $form) = @_;
384 my $dbh = SL::DB->client->dbh;
386 my $query = qq|SELECT count(*) FROM acc_trans a
387 WHERE a.chart_id = ?|;
388 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
394 $query = qq|DELETE FROM tax
396 do_query($form, $dbh, $query, $form->{id});
398 # delete account taxkeys
399 $query = qq|DELETE FROM taxkeys
401 do_query($form, $dbh, $query, $form->{id});
403 # delete chart of account record
404 # last step delete chart, because we have a constraint
406 $query = qq|DELETE FROM chart
408 do_query($form, $dbh, $query, $form->{id});
413 sub get_language_details {
414 $main::lxdebug->enter_sub();
416 my ($self, $myconfig, $form, $id) = @_;
418 my $dbh = SL::DB->client->dbh;
421 "SELECT template_code, " .
422 " output_numberformat, output_dateformat, output_longdates " .
423 "FROM language WHERE id = ?";
424 my @res = selectrow_query($form, $dbh, $query, $id);
426 $main::lxdebug->leave_sub();
431 sub prepare_template_filename {
432 $main::lxdebug->enter_sub();
434 my ($self, $myconfig, $form) = @_;
436 my ($filename, $display_filename);
438 if ($form->{type} eq "stylesheet") {
439 $filename = "css/$myconfig->{stylesheet}";
440 $display_filename = $myconfig->{stylesheet};
443 $filename = $form->{formname};
445 if ($form->{language}) {
446 my ($id, $template_code) = split(/--/, $form->{language});
447 $filename .= "_${template_code}";
450 if ($form->{printer}) {
451 my ($id, $template_code) = split(/--/, $form->{printer});
452 $filename .= "_${template_code}";
455 $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
456 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
457 $filename =~ s|.*/||;
459 $display_filename = $filename;
460 $filename = SL::DB::Default->get->templates . "/$filename";
463 $main::lxdebug->leave_sub();
465 return ($filename, $display_filename);
470 $main::lxdebug->enter_sub();
472 my ($self, $filename) = @_;
474 my ($content, $lines) = ("", 0);
478 if (open(TEMPLATE, $filename)) {
486 $content = Encode::decode('utf-8-strict', $content);
488 $main::lxdebug->leave_sub();
490 return ($content, $lines);
494 $main::lxdebug->enter_sub();
496 my ($self, $filename, $content) = @_;
502 if (open(TEMPLATE, ">", $filename)) {
503 $content = Encode::encode('utf-8-strict', $content);
504 $content =~ s/\r\n/\n/g;
505 print(TEMPLATE $content);
511 $main::lxdebug->leave_sub();
516 sub save_preferences {
517 $main::lxdebug->enter_sub();
519 my ($self, $form) = @_;
521 my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
522 $employee->update_attributes(name => $form->{name});
524 my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
525 $user->update_attributes(
527 %{ $user->config_values },
528 map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
531 $main::lxdebug->leave_sub();
537 $main::lxdebug->enter_sub();
542 my $myconfig = \%main::myconfig;
543 my $form = $main::form;
545 my $dbh = $params{dbh} || SL::DB->client->dbh;
547 my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
549 $defaults->{weightunit} ||= 'kg';
551 $main::lxdebug->leave_sub();
557 $main::lxdebug->enter_sub();
559 my ($self, $myconfig, $form) = @_;
561 my $dbh = SL::DB->client->dbh;
563 my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
564 my $sth = $dbh->prepare($query);
565 $sth->execute || $form->dberror($query);
567 ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
571 $main::lxdebug->leave_sub();
575 $main::lxdebug->enter_sub();
577 my ($self, $myconfig, $form) = @_;
579 SL::DB->client->with_transaction(sub {
580 my $dbh = SL::DB->client->dbh;
582 my ($query, @values);
584 # is currently NEVER trueish (no more hidden revtrans in $form)
585 # if ($form->{revtrans}) {
586 # $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
587 # -> therefore you can only set this to false (which is already the default)
588 # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
590 $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
591 @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
593 # set close in defaults
594 do_query($form, $dbh, $query, @values);
596 }) or do { die SL::DB->client->error };
598 $main::lxdebug->leave_sub();
602 my ($self, $units, $unit_name, $factor) = @_;
604 $factor = 1 unless ($factor);
606 my $unit = $units->{$unit_name};
608 if (!defined($unit) || !$unit->{"base_unit"} ||
609 ($unit_name eq $unit->{"base_unit"})) {
610 return ($unit_name, $factor);
613 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
617 $main::lxdebug->enter_sub();
619 my ($self, $myconfig, $form, $prefix) = @_;
622 my $dbh = SL::DB->client->dbh;
624 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
626 my $sth = prepare_execute_query($form, $dbh, $query);
629 while (my $ref = $sth->fetchrow_hashref()) {
630 $units->{$ref->{"name"}} = $ref;
634 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
635 $sth = $dbh->prepare($query_lang);
636 $sth->execute() || $form->dberror($query_lang);
638 while (my $ref = $sth->fetchrow_hashref()) {
639 push(@languages, $ref);
643 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
644 "FROM units_language ul " .
645 "LEFT JOIN language l ON ul.language_id = l.id " .
647 $sth = $dbh->prepare($query_lang);
649 foreach my $unit (values(%{$units})) {
650 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
652 $unit->{"LANGUAGES"} = {};
653 foreach my $lang (@languages) {
654 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
657 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
658 while (my $ref = $sth->fetchrow_hashref()) {
659 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
664 $main::lxdebug->leave_sub();
669 sub retrieve_all_units {
670 $main::lxdebug->enter_sub();
674 if (!$::request->{cache}{all_units}) {
675 $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
678 $main::lxdebug->leave_sub();
680 return $::request->{cache}{all_units};
684 sub translate_units {
685 $main::lxdebug->enter_sub();
687 my ($self, $form, $template_code, $unit, $amount) = @_;
689 my $units = $self->retrieve_units(\%main::myconfig, $form);
691 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
692 my $new_unit = $unit;
694 if (($amount != 1) && $h->{"localized_plural"}) {
695 $new_unit = $h->{"localized_plural"};
696 } elsif ($h->{"localized"}) {
697 $new_unit = $h->{"localized"};
701 $main::lxdebug->leave_sub();
707 $main::lxdebug->enter_sub();
709 my ($self, $myconfig, $form, $units) = @_;
711 my $dbh = SL::DB->client->dbh;
713 map({ $_->{"in_use"} = 0; } values(%{$units}));
715 foreach my $unit (values(%{$units})) {
716 my $base_unit = $unit->{"original_base_unit"};
718 $units->{$base_unit}->{"in_use"} = 1;
719 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
720 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
721 $base_unit = $units->{$base_unit}->{"original_base_unit"};
725 foreach my $unit (values(%{$units})) {
726 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
728 foreach my $table (qw(parts invoice orderitems)) {
729 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
731 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
732 $query .= "= " . $dbh->quote($unit->{"name"});
734 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
735 join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
738 my ($count) = $dbh->selectrow_array($query);
739 $form->dberror($query) if ($dbh->err);
742 $unit->{"in_use"} = 1;
748 $main::lxdebug->leave_sub();
751 sub convertible_units {
752 $main::lxdebug->enter_sub();
756 my $filter_unit = shift;
757 my $not_smaller = shift;
761 $filter_unit = $units->{$filter_unit};
763 foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
764 my $unit = $units->{$name};
766 if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
767 (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
768 push @{$conv_units}, $unit;
772 my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
774 $main::lxdebug->leave_sub();
779 # if $a is translatable to $b, return the factor between them.
782 $main::lxdebug->enter_sub(2);
783 my ($this, $a, $b, $all_units) = @_;
786 $all_units = $this->retrieve_all_units;
789 $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
790 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
791 $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
792 $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
795 sub unit_select_data {
796 $main::lxdebug->enter_sub();
798 my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
803 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
806 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
807 if (!$convertible_into ||
808 ($units->{$convertible_into} &&
809 ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
810 push @{$select}, { "name" => $unit,
811 "base_unit" => $units->{$unit}->{"base_unit"},
812 "factor" => $units->{$unit}->{"factor"},
813 "selected" => ($unit eq $selected) ? "selected" : "" };
817 $main::lxdebug->leave_sub();
822 sub unit_select_html {
823 $main::lxdebug->enter_sub();
825 my ($self, $units, $name, $selected, $convertible_into) = @_;
827 my $select = "<select name=${name}>";
829 foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
830 if (!$convertible_into ||
831 ($units->{$convertible_into} &&
832 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
833 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
836 $select .= "</select>";
838 $main::lxdebug->leave_sub();
844 $main::lxdebug->enter_sub();
848 my $units = $self->retrieve_all_units();
853 while (2 <= scalar(@_)) {
855 my $unit = $units->{shift(@_)};
857 croak "No unit defined with name $unit" if (!defined $unit);
860 $base_unit = $unit->{base_unit};
861 } elsif ($base_unit ne $unit->{base_unit}) {
862 croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
865 $sum += $qty * $unit->{factor};
868 $main::lxdebug->leave_sub();
874 $main::lxdebug->enter_sub();
876 my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
878 SL::DB->client->with_transaction(sub {
879 my $dbh = SL::DB->client->dbh;
881 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
882 my ($sortkey) = selectrow_query($form, $dbh, $query);
884 $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
885 "VALUES (?, ?, ?, ?)";
886 do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
889 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
890 my $sth = $dbh->prepare($query);
891 foreach my $lang (@{$languages}) {
892 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
893 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
898 }) or do { die SL::DB->client->error };
900 $main::lxdebug->leave_sub();
904 my ($self, $myconfig, $form, $units, $delete_units) = @_;
905 $main::lxdebug->enter_sub();
907 my $rc = SL::DB->client->with_transaction(\&_save_units, $self, $myconfig, $form, $units, $delete_units);
909 $::lxdebug->leave_sub;
914 my ($self, $myconfig, $form, $units, $delete_units) = @_;
916 my $dbh = SL::DB->client->dbh;
918 my ($base_unit, $unit, $sth, $query);
920 $query = "DELETE FROM units_language";
921 $dbh->do($query) || $form->dberror($query);
923 if ($delete_units && (0 != scalar(@{$delete_units}))) {
924 $query = "DELETE FROM units WHERE name IN (";
925 map({ $query .= "?," } @{$delete_units});
926 substr($query, -1, 1) = ")";
927 $dbh->do($query, undef, @{$delete_units}) ||
928 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
931 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
932 $sth = $dbh->prepare($query);
934 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
935 my $sth_lang = $dbh->prepare($query_lang);
937 foreach $unit (values(%{$units})) {
938 $unit->{"depth"} = 0;
939 my $base_unit = $unit;
940 while ($base_unit->{"base_unit"}) {
942 $base_unit = $units->{$base_unit->{"base_unit"}};
946 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
947 if ($unit->{"LANGUAGES"}) {
948 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
949 next unless ($lang->{"id"} && $lang->{"localized"});
950 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
951 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
955 next if ($unit->{"unchanged_unit"});
957 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
958 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
968 $main::lxdebug->enter_sub();
970 my ($self, $myconfig, $form) = @_;
972 my $dbh = SL::DB->client->dbh;
974 my $query = qq|SELECT
978 round(t.rate * 100, 2) AS rate,
979 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
980 (SELECT description FROM chart WHERE id = chart_id) AS account_description,
981 (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
982 (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
983 (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
984 (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
986 ORDER BY taxkey, rate|;
988 my $sth = $dbh->prepare($query);
989 $sth->execute || $form->dberror($query);
992 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
993 push @{ $form->{TAX} }, $ref;
998 $main::lxdebug->leave_sub();
1001 sub get_tax_accounts {
1002 $main::lxdebug->enter_sub();
1004 my ($self, $myconfig, $form) = @_;
1006 my $dbh = SL::DB->client->dbh;
1008 # get Accounts from chart
1009 my $query = qq{ SELECT
1011 accno || ' - ' || description AS taxaccount
1013 WHERE link LIKE '%_tax%'
1017 my $sth = $dbh->prepare($query);
1018 $sth->execute || $form->dberror($query);
1020 $form->{ACCOUNTS} = [];
1021 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1022 push @{ $form->{ACCOUNTS} }, $ref;
1025 $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
1026 $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
1028 $form->{skontochart_value_title_sub} = sub {
1032 $item->{accno} .' '. $item->{description},
1038 $main::lxdebug->leave_sub();
1042 $main::lxdebug->enter_sub();
1044 my ($self, $myconfig, $form) = @_;
1046 my $dbh = SL::DB->client->dbh;
1048 my $query = qq|SELECT
1051 round(rate * 100, 2) AS rate,
1054 (id IN (SELECT tax_id
1055 FROM acc_trans)) AS tax_already_used,
1056 skonto_sales_chart_id,
1057 skonto_purchase_chart_id
1061 my $sth = $dbh->prepare($query);
1062 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1064 my $ref = $sth->fetchrow_hashref("NAME_lc");
1066 map { $form->{$_} = $ref->{$_} } keys %$ref;
1070 # see if it is used by a taxkey
1071 $query = qq|SELECT count(*) FROM taxkeys
1072 WHERE tax_id = ? AND chart_id >0|;
1074 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1076 $form->{orphaned} = !$form->{orphaned};
1079 if (!$form->{orphaned} ) {
1080 $query = qq|SELECT DISTINCT c.id, c.accno
1082 JOIN tax t ON (t.id = tk.tax_id)
1083 JOIN chart c ON (c.id = tk.chart_id)
1084 WHERE tk.tax_id = ?|;
1086 $sth = $dbh->prepare($query);
1087 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1089 $form->{TAXINUSE} = [];
1090 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1091 push @{ $form->{TAXINUSE} }, $ref;
1097 $main::lxdebug->leave_sub();
1101 my ($self, $myconfig, $form) = @_;
1102 $main::lxdebug->enter_sub();
1104 my $rc = SL::DB->client->with_transaction(\&_save_tax, $self, $myconfig, $form);
1106 $::lxdebug->leave_sub;
1111 my ($self, $myconfig, $form) = @_;
1114 my $dbh = SL::DB->client->dbh;
1116 $form->{rate} = $form->{rate} / 100;
1118 my $chart_categories = '';
1119 $chart_categories .= 'A' if $form->{asset};
1120 $chart_categories .= 'L' if $form->{liability};
1121 $chart_categories .= 'Q' if $form->{equity};
1122 $chart_categories .= 'I' if $form->{revenue};
1123 $chart_categories .= 'E' if $form->{expense};
1124 $chart_categories .= 'C' if $form->{costs};
1126 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);
1127 if ($form->{id} ne "") {
1128 $query = qq|UPDATE tax SET
1133 taxnumber = (SELECT accno FROM chart WHERE id = ? ),
1134 skonto_sales_chart_id = ?,
1135 skonto_purchase_chart_id = ?,
1136 chart_categories = ?
1141 ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
1142 $query = qq|INSERT INTO tax (
1148 skonto_sales_chart_id,
1149 skonto_purchase_chart_id,
1153 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?, ?, ?)|;
1155 push(@values, $form->{id});
1156 do_query($form, $dbh, $query, @values);
1158 foreach my $language_id (keys %{ $form->{translations} }) {
1159 GenericTranslations->save('dbh' => $dbh,
1160 'translation_type' => 'SL::DB::Tax/taxdescription',
1161 'translation_id' => $form->{id},
1162 'language_id' => $language_id,
1163 'translation' => $form->{translations}->{$language_id});
1168 $main::lxdebug->enter_sub();
1170 my ($self, $myconfig, $form) = @_;
1173 SL::DB->client->with_transaction(sub {
1174 $query = qq|DELETE FROM tax WHERE id = ?|;
1175 do_query($form, SL::DB->client->dbh, $query, $form->{id});
1177 }) or do { die SL::DB->client->error };
1179 $main::lxdebug->leave_sub();
1182 sub save_warehouse {
1183 $main::lxdebug->enter_sub();
1185 my ($self, $myconfig, $form) = @_;
1187 SL::DB->client->with_transaction(sub {
1188 my $dbh = SL::DB->client->dbh;
1190 my ($query, @values, $sth);
1193 $query = qq|SELECT nextval('id')|;
1194 ($form->{id}) = selectrow_query($form, $dbh, $query);
1196 $query = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1197 do_query($form, $dbh, $query, $form->{id});
1200 do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1201 $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1203 if (0 < $form->{number_of_new_bins}) {
1204 my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1205 $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1206 $sth = prepare_query($form, $dbh, $query);
1208 foreach my $i (1..$form->{number_of_new_bins}) {
1209 do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1215 }) or do { die SL::DB->client->error };
1217 $main::lxdebug->leave_sub();
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig, $form) = @_;
1225 SL::DB->client->with_transaction(sub {
1226 my $dbh = SL::DB->client->dbh;
1228 my ($query, @values, $sth);
1230 @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1233 $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1234 do_query($form, $dbh, $query, @values);
1237 $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1238 $sth = prepare_query($form, $dbh, $query);
1240 foreach my $row (1..$form->{rowcount}) {
1241 next if ($form->{"delete_${row}"});
1243 do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1248 }) or do { die SL::DB->client->error };
1250 $main::lxdebug->leave_sub();
1253 sub delete_warehouse {
1254 $main::lxdebug->enter_sub();
1256 my ($self, $myconfig, $form) = @_;
1258 my $rc = SL::DB->client->with_transaction(sub {
1259 my $dbh = SL::DB->client->dbh;
1261 my $id = conv_i($form->{id});
1262 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|;
1263 my ($count) = selectrow_query($form, $dbh, $query, $id);
1269 do_query($form, $dbh, qq|DELETE FROM bin WHERE warehouse_id = ?|, conv_i($form->{id}));
1270 do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id = ?|, conv_i($form->{id}));
1275 $main::lxdebug->leave_sub();
1280 sub get_all_warehouses {
1281 $main::lxdebug->enter_sub();
1283 my ($self, $myconfig, $form) = @_;
1285 my $dbh = SL::DB->client->dbh;
1287 my $query = qq|SELECT w.id, w.description, w.invalid,
1288 (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
1290 ORDER BY w.sortkey|;
1292 $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
1294 $main::lxdebug->leave_sub();
1298 $main::lxdebug->enter_sub();
1300 my ($self, $myconfig, $form) = @_;
1302 my $dbh = SL::DB->client->dbh;
1304 my $id = conv_i($form->{id});
1305 my $query = qq|SELECT w.description, w.invalid
1309 my $ref = selectfirst_hashref_query($form, $dbh, $query, $id);
1311 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
1315 ( EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
1316 OR EXISTS(SELECT p.bin_id FROM parts p WHERE p.bin_id = b.id LIMIT 1))
1319 WHERE b.warehouse_id = ?
1322 $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
1324 $main::lxdebug->leave_sub();