X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=9780078f514bee6876c7138ad0cce70722e31e74;hb=332e327b6613bf762b34d841442378de255d946b;hp=af52ae9a9e72cb38baf3a62bd95ff2576158bca3;hpb=bd34eaeac9e09277148172dc634c0cd71bff91a8;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index af52ae9a9..9780078f5 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,293 +38,239 @@ 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) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - my $query = qq{ - SELECT c.accno, c.description, c.charttype, c.category, - c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from, - c.pos_bwa, datevautomatik, - tk.taxkey_id, tk.pos_ustva, tk.tax_id, - tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate - FROM chart c - LEFT JOIN taxkeys tk - ON (c.id=tk.chart_id AND tk.id = - (SELECT id FROM taxkeys - WHERE taxkeys.chart_id = c.id AND startdate <= current_date - ORDER BY startdate DESC LIMIT 1)) - WHERE c.id = ? - }; - - - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - 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|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $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 - id, - (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno, - taxkey, - id||'--'||taxkey AS tax, - taxdescription, - rate - FROM tax ORDER BY taxkey - }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $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 + }; + }; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{TAXKEY} }, $ref; - } - - $sth->finish; if ($form->{id}) { - # get new accounts - $query = qq|SELECT id, accno,description - FROM chart - WHERE link = ? - ORDER BY accno|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})"); - $form->{NEWACCOUNT} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{NEWACCOUNT} }, $ref; + my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart"; + + 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; } - $sth->finish; + 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; - # get the taxkeys of account - - $query = qq{ - SELECT - tk.id, - tk.chart_id, - c.accno, - tk.tax_id, - t.taxdescription, - t.rate, - tk.taxkey_id, - tk.pos_ustva, - tk.startdate - FROM taxkeys tk - LEFT JOIN tax t ON (t.id = tk.tax_id) - LEFT JOIN chart c ON (c.id = t.chart_id) - - WHERE tk.chart_id = ? - ORDER BY startdate DESC - }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); + # check if there are any transactions for this chart + $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1; - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); + # 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} = []; - - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ACCOUNT_TAXKEYS} }, $ref; + 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, + }; } - $sth->finish; - - } - # check if we have any transactions - $query = qq|SELECT a.trans_id FROM acc_trans a - WHERE a.chart_id = ?|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - ($form->{orphaned}) = $sth->fetchrow_array; - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; - - # check if new account is active - $form->{new_chart_valid} = 0; - if ($form->{new_chart_id}) { - $query = qq|SELECT current_date-valid_from FROM chart - WHERE id = ?|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - my ($count) = selectrow_query($form, $dbh, $query, $form->{id}); - if ($count >=0) { - $form->{new_chart_valid} = 1; - } - $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; + + for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) { + $form->{$form->{$_}} = $form->{$_} if $form->{$_}; + } # 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); + 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!')); + } } - $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); - } - chop $form->{link}; + 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} = ""; } - my @values; + $query = ' + SELECT accno + FROM chart + WHERE accno = ?'; + + my @values = ($form->{accno}); + + if ( $form->{id} ) { + $query .= ' AND NOT id = ?'; + push(@values, $form->{id}); + } + + my ($accno) = selectrow_query($form, $dbh, $query, @values); + + if ($accno) { + $form->error($::locale->text('Account number not unique!')); + } + + + 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"}, ''); + } + + @values = (); + if ($form->{id}) { + + # 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 + + if ( $form->{charttype} eq 'H' ) { + $form->{link} = ''; + $form->{pos_bwa} = ''; + $form->{pos_bilanz} = ''; + $form->{pos_eur} = ''; + $form->{new_chart_id} = ''; + $form->{valid_from} = ''; + }; + $query = qq|UPDATE chart SET - accno = ?, - description = ?, + accno = ?, + description = ?, charttype = ?, - category = ?, + category = ?, link = ?, - pos_bwa = ?, + pos_bwa = ?, pos_bilanz = ?, - pos_eur = ?, - new_chart_id = ?, + pos_eur = ?, + pos_er = ?, + new_chart_id = ?, valid_from = ?, datevautomatik = ? WHERE id = ?|; - - @values = ( - $form->{accno}, - $form->{description}, + + @values = ( + $form->{accno}, + $form->{description}, $form->{charttype}, - $form->{category}, + $form->{category}, $form->{link}, conv_i($form->{pos_bwa}), - conv_i($form->{pos_bilanz}), + 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}, ); - } - elsif ($form->{id} && !$form->{new_chart_valid}) { - - $query = qq| - UPDATE chart - SET new_chart_id = ?, - valid_from = ? - WHERE id = ? - |; - - @values = ( - conv_i($form->{new_chart_id}), - conv_date($form->{valid_from}), - $form->{id} - ); - } - else { - - $query = qq| - INSERT INTO chart ( - accno, - description, - charttype, - category, - link, - pos_bwa, - pos_bilanz, - pos_eur, - new_chart_id, - valid_from, - datevautomatik ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - |; - - @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->{new_chart_id}), - conv_date($form->{valid_from}), - ($form->{datevautomatik} eq 'T') ? 'true':'false', - ); } - + do_query($form, $dbh, $query, @values); #Save Taxkeys my @taxkeys = (); - + my $MAX_TRIES = 10; # Maximum count of taxkeys in form my $tk_count; - + READTAXKEYS: for $tk_count (0 .. $MAX_TRIES) { - + # Loop control - + # Check if the account already exists, else cancel + + print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n"); + last READTAXKEYS if ( $form->{'id'} == 0); # check if there is a startdate @@ -332,14 +279,8 @@ sub save_account { next READTAXKEYS; } - # check if there is at least one relation to pos_ustva or tax_id - if ( $form->{"taxkey_pos_ustva_$tk_count"} eq '' && $form->{"taxkey_tax_$tk_count"} == 0 ) { - $tk_count++; - next READTAXKEYS; - } - # Add valid taxkeys into the array - push @taxkeys , + 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"}), @@ -348,7 +289,7 @@ sub save_account { pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}), delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '', }; - + $tk_count++; } @@ -356,7 +297,7 @@ sub save_account { for my $j (0 .. $#taxkeys){ if ( defined $taxkeys[$j]{'id'} ){ # delete Taxkey? - + if ($taxkeys[$j]{'delete'}){ $query = qq{ DELETE FROM taxkeys WHERE id = ? @@ -365,12 +306,12 @@ sub save_account { @values = ($taxkeys[$j]{'id'}); do_query($form, $dbh, $query, @values); - + next TAXKEY; } # UPDATE Taxkey - + $query = qq{ UPDATE taxkeys SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?), @@ -379,20 +320,20 @@ sub save_account { pos_ustva = ?, startdate = ? WHERE id = ? - }; + }; @values = ( $taxkeys[$j]{'tax_id'}, $taxkeys[$j]{'chart_id'}, - $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'tax_id'}, $taxkeys[$j]{'pos_ustva'}, - $taxkeys[$j]{'startdate'}, - $taxkeys[$j]{'id'}, + $taxkeys[$j]{'startdate'}, + $taxkeys[$j]{'id'}, ); do_query($form, $dbh, $query, @values); } else { # INSERT Taxkey - + $query = qq{ INSERT INTO taxkeys ( taxkey_id, @@ -402,65 +343,63 @@ sub save_account { startdate ) VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?) - }; + }; @values = ( - $taxkeys[$j]{'tax_id'}, - $taxkeys[$j]{'chart_id'}, - $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'tax_id'}, + $taxkeys[$j]{'chart_id'}, + $taxkeys[$j]{'tax_id'}, $taxkeys[$j]{'pos_ustva'}, - $taxkeys[$j]{'startdate'}, + $taxkeys[$j]{'startdate'}, ); - + do_query($form, $dbh, $query, @values); } - + } - # commit - my $rc = $dbh->commit; - $dbh->disconnect; + # Update chart.taxkey_id to the latest from taxkeys for this chart. + $query = <leave_sub(); + do_query($form, $dbh, $query, $form->{id}); - return $rc; + return 1; } sub delete_account { + my ($self, $myconfig, $form) = @_; $main::lxdebug->enter_sub(); + my $rc = SL::DB->client->with_transaction(\&_delete_account, $self, $myconfig, $form); + + $::lxdebug->leave_sub; + return $rc; +} + +sub _delete_account { my ($self, $myconfig, $form) = @_; - # connect to database, turn off AutoCommit - my $dbh = $form->dbconnect_noauto($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|SELECT count(*) FROM acc_trans a WHERE a.chart_id = ?|; my ($count) = selectrow_query($form, $dbh, $query, $form->{id}); if ($count) { - $dbh->disconnect; - $main::lxdebug->leave_sub(); return; } - # set inventory_accno_id, income_accno_id, expense_accno_id to defaults - foreach my $type (qw(inventory income expense)) { - $query = - qq|UPDATE parts | . - qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | . - qq|WHERE ${type}_accno_id = ?|; - do_query($form, $dbh, $query, $form->{id}); - } - - foreach my $table (qw(partstax customertax vendortax tax)) { - $query = qq|DELETE FROM $table - WHERE chart_id = ?|; - do_query($form, $dbh, $query, $form->{id}); - } - - # delete chart of account record - $query = qq|DELETE FROM chart - WHERE id = ?|; + $query = qq|DELETE FROM tax + WHERE chart_id = ?|; do_query($form, $dbh, $query, $form->{id}); # delete account taxkeys @@ -468,1230 +407,214 @@ sub delete_account { WHERE chart_id = ?|; do_query($form, $dbh, $query, $form->{id}); - # commit and redirect - my $rc = $dbh->commit; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); + # 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}); - return $rc; + return 1; } -sub departments { +sub get_language_details { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.id, d.description, d.role - FROM department d - ORDER BY 2|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + my ($self, $myconfig, $form, $id) = @_; - $form->{ALL} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } + my $dbh = SL::DB->client->dbh; - $sth->finish; - $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 get_department { +sub prepare_template_filename { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.description, d.role - FROM department d - WHERE d.id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - # see if it is in use - $query = qq|SELECT count(*) FROM dpt_trans d - WHERE d.department_id = ?|; - ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id}); - - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub save_department { - $main::lxdebug->enter_sub(); + my ($filename, $display_filename); - my ($self, $myconfig, $form) = @_; + $filename = $form->{formname}; - # connect to database - my $dbh = $form->dbconnect($myconfig); + if ($form->{language}) { + my ($id, $template_code) = split(/--/, $form->{language}); + $filename .= "_${template_code}"; + } - my @values = ($form->{description}, $form->{role}); - if ($form->{id}) { - $query = qq|UPDATE department SET - description = ?, role = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO department - (description, role) - VALUES (?, ?)|; + if ($form->{printer}) { + my ($id, $template_code) = split(/--/, $form->{printer}); + $filename .= "_${template_code}"; } - do_query($form, $dbh, $query, @values); - $dbh->disconnect; + $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(); -} - -sub delete_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM department - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - $main::lxdebug->leave_sub(); + return ($filename, $display_filename); } -sub lead { - $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, lead - FROM leads - ORDER BY 2|; + my ($content, $lines) = ("", 0); - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + local *TEMPLATE; - $form->{ALL}; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; + if (open(TEMPLATE, $filename)) { + while (