X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=2ec225238a2ca63ab14fd156e73b4cb5846c15ae;hb=ee03d15d22eebae4c5224bd68a23b6b4d8a466c0;hp=62406e74c24a2a7786089bb98913555dd76ea395;hpb=d319704a66e9be64da837ccea10af6774c2b0838;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index 62406e74c..2ec225238 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -25,7 +25,8 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1335, USA. #====================================================================== # # Administration module @@ -37,1323 +38,1377 @@ package AM; +use Carp; +use Data::Dumper; +use Encode; +use List::MoreUtils qw(any); +use SL::DBUtils; +use SL::DB::AuthUser; +use SL::DB::Default; +use SL::DB::Employee; +use SL::DB::Chart; +use SL::DB::Customer; +use SL::DB::Part; +use SL::DB::Vendor; +use SL::DB; +use SL::GenericTranslations; +use SL::Helper::UserPreferences::PositionsScrollbar; +use SL::Helper::UserPreferences::PartPickerSearch; +use SL::Helper::UserPreferences::TimeRecording; +use SL::Helper::UserPreferences::UpdatePositions; + +use strict; + sub get_account { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; - - $form->{id} = "NULL" unless ($form->{id}); - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT c.accno, c.description, c.charttype, c.gifi_accno, - c.category, c.link, c.taxkey_id, c.pos_ustva, c.pos_bwa, c.pos_bilanz,c.pos_eur - FROM chart c - WHERE c.id = $form->{id}|; - - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - foreach my $key (keys %$ref) { - $form->{"$key"} = $ref->{"$key"}; - } + # fetch chart-related data and set form fields + # get_account is called by add_account in am.pl + # always sets $form->{TAXKEY} and default_accounts + # loads chart data when $form->{id} is passed - $sth->finish; + my ($self, $myconfig, $form) = @_; # get default accounts - $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id - FROM defaults|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %ref; - - $sth->finish; - - # get taxkeys and description - $query = qq|SELECT taxkey, taxdescription - FROM tax|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + map { $form->{$_} = $::instance_conf->{$_} } qw(inventory_accno_id income_accno_id expense_accno_id); + + require SL::DB::Tax; + my $taxes = SL::DB::Manager::Tax->get_all( with_objects => ['chart'] , sort_by => 'taxkey' ); + $form->{TAXKEY} = []; + foreach my $tk ( @{$taxes} ) { + push @{ $form->{TAXKEY} }, { id => $tk->id, + chart_accno => $tk->chart_id ? $tk->chart->accno : undef, + taxkey => $tk->taxkey, + tax => $tk->id . '--' . $tk->taxkey, + rate => $tk->rate + }; + }; - $ref = $sth->fetchrow_hashref(NAME_lc); + if ($form->{id}) { - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{TAXKEY} }, $ref; - } + my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart"; - $sth->finish; + my @chart_fields = qw(accno description charttype category link pos_bilanz + pos_eur pos_er new_chart_id valid_from pos_bwa datevautomatik); + foreach my $cf ( @chart_fields ) { + $form->{"$cf"} = $chart_obj->$cf; + } - # check if we have any transactions - $query = qq|SELECT a.trans_id FROM acc_trans a - WHERE a.chart_id = $form->{id}|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + my $active_taxkey = $chart_obj->get_active_taxkey; + $form->{$_} = $active_taxkey->$_ foreach qw(taxkey_id pos_ustva tax_id startdate); + $form->{tax} = $active_taxkey->tax_id . '--' . $active_taxkey->taxkey_id; + + # check if there are any transactions for this chart + $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1; + + # check if new account is active + # The old sql query was broken since at least 2006 and always returned 0 + $form->{new_chart_valid} = $chart_obj->new_chart_valid; + + # get the taxkeys of the account + $form->{ACCOUNT_TAXKEYS} = []; + foreach my $taxkey ( sort { $b->startdate <=> $a->startdate } @{ $chart_obj->taxkeys } ) { + push @{ $form->{ACCOUNT_TAXKEYS} }, { id => $taxkey->id, + chart_id => $taxkey->chart_id, + tax_id => $taxkey->tax_id, + taxkey_id => $taxkey->taxkey_id, + pos_ustva => $taxkey->pos_ustva, + startdate => $taxkey->startdate->to_kivitendo, + taxdescription => $taxkey->tax->taxdescription, + rate => $taxkey->tax->rate, + accno => defined $taxkey->tax->chart_id ? $taxkey->tax->chart->accno : undef, + }; + } - ($form->{orphaned}) = $sth->fetchrow_array; - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; + # get new accounts (Folgekonto). Find all charts with the same link + $form->{NEWACCOUNT} = $chart_obj->db->dbh->selectall_arrayref('select id, accno,description from chart where link = ? and id != ? order by accno', {Slice => {}}, $chart_obj->link, $form->{id}); - $dbh->disconnect; + } else { # set to orphaned for new charts, so chart_type can be changed (needed by $AccountIsPosted) + $form->{orphaned} = 1; + }; $main::lxdebug->leave_sub(); } sub save_account { + my ($self, $myconfig, $form) = @_; $main::lxdebug->enter_sub(); + my $rc = SL::DB->client->with_transaction(\&_save_account, $self, $myconfig, $form); + + $::lxdebug->leave_sub; + return $rc; +} + +sub _save_account { + # TODO: it should be forbidden to change an account to a heading if there + # have been bookings to this account in the past + my ($self, $myconfig, $form) = @_; - # connect to database, turn off AutoCommit - my $dbh = $form->dbconnect_noauto($myconfig); + my $dbh = SL::DB->client->dbh; - # sanity check, can't have AR with AR_... - if ($form->{AR} || $form->{AP} || $form->{IC}) { - map { delete $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 CT_tax); + for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) { + $form->{$form->{$_}} = $form->{$_} if $form->{$_}; } - $form->{link} = ""; - foreach my $item ($form->{AR}, $form->{AR_amount}, - $form->{AR_tax}, $form->{AR_paid}, - $form->{AP}, $form->{AP_amount}, - $form->{AP_tax}, $form->{AP_paid}, - $form->{IC}, $form->{IC_sale}, - $form->{IC_cogs}, $form->{IC_taxpart}, - $form->{IC_income}, $form->{IC_expense}, - $form->{IC_taxservice}, $form->{CT_tax} - ) { - $form->{link} .= "${item}:" if ($item); + # sanity check, can't have AR with AR_... + if ($form->{AR} || $form->{AP} || $form->{IC}) { + 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)) { + $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!')); + } } - chop $form->{link}; - # if we have an id then replace the old record - $form->{description} =~ s/\'/\'\'/g; + 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); + $form->{link} = join ':', grep $_, map $form->{$_}, @link_order; # strip blanks from accno map { $form->{$_} =~ s/ //g; } qw(accno); + # collapse multiple (horizontal) whitespace in chart description (Ticket 148) + map { $form->{$_} =~ s/\h+/ /g } qw(description); + my ($query, $sth); if ($form->{id} eq "NULL") { $form->{id} = ""; } - map({ $form->{$_} = "NULL" unless ($form->{$_}); } - qw(pos_ustva pos_bwa pos_bilanz pos_eur)); + $query = ' + SELECT accno + FROM chart + WHERE accno = ?'; - if ($form->{id}) { - $query = qq|UPDATE chart SET - accno = '$form->{accno}', - description = '$form->{description}', - charttype = '$form->{charttype}', - gifi_accno = '$form->{gifi_accno}', - category = '$form->{category}', - link = '$form->{link}', - taxkey_id = $form->{taxkey_id}, - pos_ustva = $form->{pos_ustva}, - pos_bwa = $form->{pos_bwa}, - pos_bilanz = $form->{pos_bilanz}, - pos_eur = $form->{pos_eur} - WHERE id = $form->{id}|; - } else { + my @values = ($form->{accno}); - $query = qq|INSERT INTO chart - (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur) - VALUES ('$form->{accno}', '$form->{description}', - '$form->{charttype}', '$form->{gifi_accno}', - '$form->{category}', '$form->{link}', $form->{taxkey_id}, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur})|; + if ( $form->{id} ) { + $query .= ' AND NOT id = ?'; + push(@values, $form->{id}); } - $dbh->do($query) || $form->dberror($query); - - if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) { - my $chart_id = $form->{id}; + my ($accno) = selectrow_query($form, $dbh, $query, @values); - unless ($form->{id}) { + if ($accno) { + $form->error($::locale->text('Account number not unique!')); + } - # get id from chart - $query = qq|SELECT c.id - FROM chart c - WHERE c.accno = '$form->{accno}'|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - ($chart_id) = $sth->fetchrow_array; - $sth->finish; - } + if (!$form->{id} || $form->{id} eq "") { + $query = qq|SELECT nextval('id')|; + ($form->{"id"}) = selectrow_query($form, $dbh, $query); + $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|; + do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, ''); + } - # add account if it doesn't exist in tax - $query = qq|SELECT t.chart_id - FROM tax t - WHERE t.chart_id = $chart_id|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + @values = (); - my ($tax_id) = $sth->fetchrow_array; - $sth->finish; - # add tax if it doesn't exist - unless ($tax_id) { - $query = qq|INSERT INTO tax (chart_id, rate) - VALUES ($chart_id, 0)|; - $dbh->do($query) || $form->dberror($query); - } - } else { + if ($form->{id}) { - # remove tax - if ($form->{id}) { - $query = qq|DELETE FROM tax - WHERE chart_id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); - } - } + # if charttype is heading make sure certain values are empty + # specifically, if charttype is changed from an existing account, empty the + # fields unnecessary for headings, so that e.g. heading doesn't appear in + # drop-down menues due to still having a valid "link" entry - # commit - my $rc = $dbh->commit; - $dbh->disconnect; + if ( $form->{charttype} eq 'H' ) { + $form->{link} = ''; + $form->{pos_bwa} = ''; + $form->{pos_bilanz} = ''; + $form->{pos_eur} = ''; + $form->{new_chart_id} = ''; + $form->{valid_from} = ''; + }; - $main::lxdebug->leave_sub(); + $query = qq|UPDATE chart SET + accno = ?, + description = ?, + charttype = ?, + category = ?, + link = ?, + pos_bwa = ?, + pos_bilanz = ?, + pos_eur = ?, + pos_er = ?, + new_chart_id = ?, + valid_from = ?, + datevautomatik = ? + WHERE id = ?|; + + @values = ( + $form->{accno}, + $form->{description}, + $form->{charttype}, + $form->{category}, + $form->{link}, + conv_i($form->{pos_bwa}), + conv_i($form->{pos_bilanz}), + conv_i($form->{pos_eur}), + conv_i($form->{pos_er}), + conv_i($form->{new_chart_id}), + conv_date($form->{valid_from}), + ($form->{datevautomatik} eq 'T') ? 'true':'false', + $form->{id}, + ); - return $rc; -} -sub delete_account { - $main::lxdebug->enter_sub(); + } - my ($self, $myconfig, $form) = @_; + do_query($form, $dbh, $query, @values); - # connect to database, turn off AutoCommit - my $dbh = $form->dbconnect_noauto($myconfig); + #Save Taxkeys - my $query = qq|SELECT count(*) FROM acc_trans a - WHERE a.chart_id = $form->{id}|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + my @taxkeys = (); - if ($sth->fetchrow_array) { - $sth->finish; - $dbh->disconnect; - $main::lxdebug->leave_sub(); - return; - } - $sth->finish; + my $MAX_TRIES = 10; # Maximum count of taxkeys in form + my $tk_count; - # delete chart of account record - $query = qq|DELETE FROM chart - WHERE id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); + READTAXKEYS: + for $tk_count (0 .. $MAX_TRIES) { - # set inventory_accno_id, income_accno_id, expense_accno_id to defaults - $query = qq|UPDATE parts - SET inventory_accno_id = - (SELECT inventory_accno_id FROM defaults) - WHERE inventory_accno_id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); + # Loop control - $query = qq|UPDATE parts - SET income_accno_id = - (SELECT income_accno_id FROM defaults) - WHERE income_accno_id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); + # Check if the account already exists, else cancel - $query = qq|UPDATE parts - SET expense_accno_id = - (SELECT expense_accno_id FROM defaults) - WHERE expense_accno_id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); + print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n"); - foreach my $table (qw(partstax customertax vendortax tax)) { - $query = qq|DELETE FROM $table - WHERE chart_id = $form->{id}|; - $dbh->do($query) || $form->dberror($query); - } + last READTAXKEYS if ( $form->{'id'} == 0); - # commit and redirect - my $rc = $dbh->commit; - $dbh->disconnect; + # check if there is a startdate + if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) { + $tk_count++; + next READTAXKEYS; + } - $main::lxdebug->leave_sub(); + # Add valid taxkeys into the array + push @taxkeys , + { + id => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}), + tax_id => conv_i($form->{"taxkey_tax_$tk_count"}), + startdate => conv_date($form->{"taxkey_startdate_$tk_count"}), + chart_id => conv_i($form->{"id"}), + pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}), + delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '', + }; + + $tk_count++; + } - return $rc; -} + TAXKEY: + for my $j (0 .. $#taxkeys){ + if ( defined $taxkeys[$j]{'id'} ){ + # delete Taxkey? -sub gifi_accounts { - $main::lxdebug->enter_sub(); + if ($taxkeys[$j]{'delete'}){ + $query = qq{ + DELETE FROM taxkeys WHERE id = ? + }; - my ($self, $myconfig, $form) = @_; + @values = ($taxkeys[$j]{'id'}); - # connect to database - my $dbh = $form->dbconnect($myconfig); + do_query($form, $dbh, $query, @values); - my $query = qq|SELECT accno, description - FROM gifi - ORDER BY accno|; + next TAXKEY; + } - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + # UPDATE Taxkey + + $query = qq{ + UPDATE taxkeys + SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?), + chart_id = ?, + tax_id = ?, + pos_ustva = ?, + startdate = ? + WHERE id = ? + }; + @values = ( + $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'chart_id'}, + $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'pos_ustva'}, + $taxkeys[$j]{'startdate'}, + $taxkeys[$j]{'id'}, + ); + do_query($form, $dbh, $query, @values); + } + else { + # INSERT Taxkey + + $query = qq{ + INSERT INTO taxkeys ( + taxkey_id, + chart_id, + tax_id, + pos_ustva, + startdate + ) + VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?) + }; + @values = ( + $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'chart_id'}, + $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'pos_ustva'}, + $taxkeys[$j]{'startdate'}, + ); + + do_query($form, $dbh, $query, @values); + } - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; } - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); + # Update chart.taxkey_id to the latest from taxkeys for this chart. + $query = <{id}); + + return 1; } -sub get_gifi { +sub delete_account { + my ($self, $myconfig, $form) = @_; $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; + my $rc = SL::DB->client->with_transaction(\&_delete_account, $self, $myconfig, $form); - # connect to database - my $dbh = $form->dbconnect($myconfig); + $::lxdebug->leave_sub; + return $rc; +} - my $query = qq|SELECT g.accno, g.description - FROM gifi g - WHERE g.accno = '$form->{accno}'|; - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); +sub _delete_account { + my ($self, $myconfig, $form) = @_; - my $ref = $sth->fetchrow_hashref(NAME_lc); + my $dbh = SL::DB->client->dbh; - map { $form->{$_} = $ref->{$_} } keys %$ref; + my $query = qq|SELECT count(*) FROM acc_trans a + WHERE a.chart_id = ?|; + my ($count) = selectrow_query($form, $dbh, $query, $form->{id}); - $sth->finish; + if ($count) { + return; + } - # check for transactions - $query = qq|SELECT count(*) FROM acc_trans a, chart c, gifi g - WHERE c.gifi_accno = g.accno - AND a.chart_id = c.id - AND g.accno = '$form->{accno}'|; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + $query = qq|DELETE FROM tax + WHERE chart_id = ?|; + do_query($form, $dbh, $query, $form->{id}); - ($form->{orphaned}) = $sth->fetchrow_array; - $sth->finish; - $form->{orphaned} = !$form->{orphaned}; + # delete account taxkeys + $query = qq|DELETE FROM taxkeys + WHERE chart_id = ?|; + do_query($form, $dbh, $query, $form->{id}); - $dbh->disconnect; + # delete chart of account record + # last step delete chart, because we have a constraint + # to taxkeys + $query = qq|DELETE FROM chart + WHERE id = ?|; + do_query($form, $dbh, $query, $form->{id}); - $main::lxdebug->leave_sub(); + return 1; } -sub save_gifi { +sub get_language_details { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($self, $myconfig, $form, $id) = @_; - $form->{description} =~ s/\'/\'\'/g; - - # id is the old account number! - if ($form->{id}) { - $query = qq|UPDATE gifi SET - accno = '$form->{accno}', - description = '$form->{description}' - WHERE accno = '$form->{id}'|; - } else { - $query = qq|INSERT INTO gifi - (accno, description) - VALUES ('$form->{accno}', '$form->{description}')|; - } - $dbh->do($query) || $form->dberror($query); + my $dbh = SL::DB->client->dbh; - $dbh->disconnect; + my $query = + "SELECT template_code, " . + " output_numberformat, output_dateformat, output_longdates " . + "FROM language WHERE id = ?"; + my @res = selectrow_query($form, $dbh, $query, $id); $main::lxdebug->leave_sub(); + + return @res; } -sub delete_gifi { +sub prepare_template_filename { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($filename, $display_filename); - # id is the old account number! - $query = qq|DELETE FROM gifi - WHERE accno = '$form->{id}'|; - $dbh->do($query) || $form->dberror($query); + $filename = $form->{formname}; - $dbh->disconnect; + if ($form->{language}) { + my ($id, $template_code) = split(/--/, $form->{language}); + $filename .= "_${template_code}"; + } + + if ($form->{printer}) { + my ($id, $template_code) = split(/--/, $form->{printer}); + $filename .= "_${template_code}"; + } + + $filename .= "." . ($form->{format} eq "html" ? "html" : "tex"); + if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) { + $filename =~ s|.*/||; + } + $display_filename = $filename; + $filename = SL::DB::Default->get->templates . "/$filename"; $main::lxdebug->leave_sub(); + + return ($filename, $display_filename); } -sub warehouses { - $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; +sub load_template { + $main::lxdebug->enter_sub(); - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($self, $filename) = @_; - my $query = qq|SELECT id, description - FROM warehouse - ORDER BY 2|; + my ($content, $lines) = ("", 0); - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + local *TEMPLATE; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; + if (open(TEMPLATE, $filename)) { + while (