From 41cceb1e99fa9d9dbf7977772e16e23646208608 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 11 Jan 2017 16:11:15 +0100 Subject: [PATCH] Debitorenbuchungen auf Verwendung des Chart-Pickers umgestellt --- SL/AR.pm | 45 +++----- SL/DB/Customer.pm | 23 +++++ SL/Form.pm | 8 +- SL/IS.pm | 40 -------- bin/mozilla/ar.pl | 136 ++++++------------------- templates/webpages/ar/form_footer.html | 14 +++ templates/webpages/ar/form_header.html | 8 +- 7 files changed, 94 insertions(+), 180 deletions(-) diff --git a/SL/AR.pm b/SL/AR.pm index 5f7faddf3..4fea4afe9 100644 --- a/SL/AR.pm +++ b/SL/AR.pm @@ -75,10 +75,7 @@ sub _post_transaction { $form->parse_amount($myconfig, $form->{exchangerate}) ); # get the charts selected - map { ($form->{AR_amounts}{"amount_$_"}) = split /--/, $form->{"AR_amount_$_"} } 1 .. $form->{rowcount}; - - $form->{AR_amounts}{receivables} = $form->{ARselected}; - $form->{AR}{receivables} = $form->{ARselected}; + $form->{AR_amounts}{"amount_$_"} = $form->{"AR_amount_chart_id_$_"} for (1 .. $form->{rowcount}); $form->{tax} = 0; # is this still needed? @@ -157,7 +154,7 @@ sub _post_transaction { # insert detail records in acc_trans $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) - VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT c.link FROM chart c WHERE c.accno = ?))|; + VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT c.link FROM chart c WHERE c.id = ?))|; @values = (conv_i($form->{id}), $form->{AR_amounts}{"amount_$i"}, conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, conv_i($form->{"taxkey_$i"}), conv_i($form->{"tax_id_$i"}), $form->{AR_amounts}{"amount_$i"}); do_query($form, $dbh, $query, @values); @@ -175,17 +172,15 @@ sub _post_transaction { # add recievables $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?), (SELECT tax_id FROM taxkeys - WHERE chart_id= (SELECT id - FROM chart - WHERE accno = ?) + WHERE chart_id = ? AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - (SELECT c.link FROM chart c WHERE c.accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), - $form->{AR_amounts}{receivables}, $form->{AR_amounts}{receivables}, conv_date($form->{transdate}), $form->{AR_amounts}{receivables}); + (SELECT c.link FROM chart c WHERE c.id = ?))|; + @values = (conv_i($form->{id}), $form->{AR_chart_id}, conv_i($form->{receivables}), conv_date($form->{transdate}), + $form->{AR_chart_id}, $form->{AR_chart_id}, conv_date($form->{transdate}), $form->{AR_chart_id}); do_query($form, $dbh, $query, @values); } else { @@ -225,17 +220,14 @@ sub _post_transaction { if ($amount != 0) { # add receivable $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + VALUES (?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?), (SELECT tax_id FROM taxkeys - WHERE chart_id= (SELECT id - FROM chart - WHERE accno = ?) + WHERE chart_id = ? AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - (SELECT c.link FROM chart c WHERE c.accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables}, $form->{AR}{receivables}, conv_date($form->{"datepaid_$i"}), - $form->{AR}{receivables}); + (SELECT c.link FROM chart c WHERE c.id = ?))|; + @values = (conv_i($form->{id}), $form->{AR_chart_id}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR_chart_id}, $form->{AR_chart_id}, conv_date($form->{"datepaid_$i"}), $form->{AR_chart_id}); do_query($form, $dbh, $query, @values); } @@ -421,9 +413,9 @@ sub _post_payment { $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); $form->{defaultcurrency} = $form->get_default_currency($myconfig); - # Get the AR accno (which is normally done by Form::create_links()). + # Get the AR chart ID (which is normally done by Form::create_links()). $query = - qq|SELECT c.accno + qq|SELECT c.id FROM acc_trans at LEFT JOIN chart c ON (at.chart_id = c.id) WHERE (trans_id = ?) @@ -431,7 +423,7 @@ sub _post_payment { ORDER BY at.acc_trans_id LIMIT 1|; - ($form->{ARselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); + ($form->{AR_chart_id}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); # Post the new payments. $self->post_transaction($myconfig, $form, $dbh, 1); @@ -739,15 +731,8 @@ sub setup_form { $form->{"projectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber}; $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate}; $form->{"project_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{project_id}; - } - - $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; - - if ($akey eq "AR") { - $form->{ARselected} = $form->{acc_trans}{$key}->[$i-1]->{accno}; - } elsif ($akey eq "amount") { - $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id}; + $form->{"${key}_chart_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{chart_id}; $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate}; } } diff --git a/SL/DB/Customer.pm b/SL/DB/Customer.pm index 8dd2a310d..d15f5ecd0 100644 --- a/SL/DB/Customer.pm +++ b/SL/DB/Customer.pm @@ -4,6 +4,7 @@ use strict; use Rose::DB::Object::Helpers qw(as_tree); +use SL::DBUtils (); use SL::DB::MetaSetup::Customer; use SL::DB::Manager::Customer; use SL::DB::Helper::IBANValidation; @@ -64,6 +65,28 @@ sub displayable_name { return join ' ', grep $_, $self->customernumber, $self->name; } +sub last_used_ar_chart { + my ($self) = @_; + + my $query = <db->dbh, $query, ($self->id) x 2); + + return if !$chart_id; + return SL::DB::Chart->load_cached($chart_id); +} + sub is_customer { 1 }; sub is_vendor { 0 }; sub payment_terms { goto &payment } diff --git a/SL/Form.pm b/SL/Form.pm index 7cea6a212..00880f1b2 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -2664,7 +2664,7 @@ sub create_links { # now get the account numbers $query = qq| - SELECT c.accno, c.description, c.link, c.taxkey_id, tk2.tax_id + SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk2.tax_id FROM chart c -- find newest entries in taxkeys INNER JOIN ( @@ -2694,6 +2694,7 @@ sub create_links { push @{ $self->{"${module}_links"}{$key} }, { accno => $ref->{accno}, + chart_id => $ref->{chart_id}, description => $ref->{description}, taxkey => $ref->{taxkey_id}, tax_id => $ref->{tax_id} }; @@ -2747,7 +2748,7 @@ sub create_links { } # now get the account numbers - $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id + $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk.tax_id FROM chart c LEFT JOIN taxkeys tk ON (tk.chart_id = c.id) WHERE c.link LIKE ? @@ -2769,6 +2770,7 @@ sub create_links { push @{ $self->{"${module}_links"}{$key} }, { accno => $ref->{accno}, + chart_id => $ref->{chart_id}, description => $ref->{description}, taxkey => $ref->{taxkey_id}, tax_id => $ref->{tax_id} }; @@ -2783,7 +2785,7 @@ sub create_links { $query = qq|SELECT c.accno, c.description, - a.acc_trans_id, a.source, a.amount, a.memo, a.transdate, a.gldate, a.cleared, a.project_id, a.taxkey, + a.acc_trans_id, a.source, a.amount, a.memo, a.transdate, a.gldate, a.cleared, a.project_id, a.taxkey, a.chart_id, p.projectnumber, t.rate, t.id FROM acc_trans a diff --git a/SL/IS.pm b/SL/IS.pm index 3759e5790..6d1e843cd 100644 --- a/SL/IS.pm +++ b/SL/IS.pm @@ -2203,46 +2203,6 @@ sub get_customer { } $sth->finish; - # setup last accounts used for this customer - if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) { - $query = - qq|SELECT c.id, c.accno, c.description, c.link, c.category - FROM chart c - JOIN acc_trans ac ON (ac.chart_id = c.id) - JOIN ar a ON (a.id = ac.trans_id) - WHERE a.customer_id = ? - AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%') - AND a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_id = ?)|; - $sth = prepare_execute_query($form, $dbh, $query, $cid, $cid); - - my $i = 0; - while ($ref = $sth->fetchrow_hashref('NAME_lc')) { - if ($ref->{category} eq 'I') { - $i++; - $form->{"AR_amount_$i"} = "$ref->{accno}--$ref->{description}"; - - if ($form->{initial_transdate}) { - my $tax_query = - qq|SELECT tk.tax_id, t.rate - FROM taxkeys tk - LEFT JOIN tax t ON tk.tax_id = t.id - WHERE (tk.chart_id = ?) AND (startdate <= date(?)) - ORDER BY tk.startdate DESC - LIMIT 1|; - my ($tax_id, $rate) = - selectrow_query($form, $dbh, $tax_query, $ref->{id}, - $form->{initial_transdate}); - $form->{"taxchart_$i"} = "${tax_id}--${rate}"; - } - } - if ($ref->{category} eq 'A') { - $form->{ARselected} = $form->{AR_1} = $ref->{accno}; - } - } - $sth->finish; - $form->{rowcount} = $i if ($i && !$form->{type}); - } - $main::lxdebug->leave_sub(); } diff --git a/bin/mozilla/ar.pl b/bin/mozilla/ar.pl index 7456cd5f9..9416ac703 100644 --- a/bin/mozilla/ar.pl +++ b/bin/mozilla/ar.pl @@ -38,6 +38,7 @@ use List::UtilsBy qw(sort_by); use SL::AR; use SL::FU; +use SL::GL; use SL::IS; use SL::DB::Default; use SL::DB::Invoice; @@ -101,6 +102,12 @@ sub add { $form->{initial_transdate} = $form->{transdate}; create_links(dont_save => 1); $form->{transdate} = $form->{initial_transdate}; + + if ($form->{customer_id}) { + my $last_used_ar_chart = SL::DB::Customer->load_cached($form->{customer_id})->last_used_ar_chart; + $form->{"AR_amount_chart_id_1"} = $last_used_ar_chart->id if $last_used_ar_chart; + } + &display_form; $main::lxdebug->leave_sub(); } @@ -167,6 +174,7 @@ sub create_links { $form->{$_} = $saved{$_} for keys %saved; $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}"; $form->{rowcount} = 1; + $form->{AR_chart_id} = $form->{acc_trans} && $form->{acc_trans}->{AR} ? $form->{acc_trans}->{AR}->[0]->{chart_id} : $form->{AR_links}->{AR}->[0]->{chart_id}; # currencies $form->{defaultcurrency} = $form->get_default_currency(\%myconfig); @@ -218,36 +226,14 @@ sub form_header { my ($title, $readonly, $exchangerate, $rows); my ($notes, $customer, $employee, $amount, $project); - my ($ARselected); + $form->{initial_focus} = !($form->{amount_1} * 1) ? 'customer' : 'row_' . $form->{rowcount}; $title = $form->{title}; # $locale->text('Add Accounts Receivables Transaction') # $locale->text('Edit Accounts Receivables Transaction') $form->{title} = $locale->text("$title Accounts Receivables Transaction"); - $form->{javascript} = qq||; - # show history button js - $form->{javascript} .= qq||; - #/show history button js $readonly = ($form->{id}) ? "readonly" : ""; $form->{radier} = ($::instance_conf->get_ar_changeable == 2) @@ -285,53 +271,27 @@ sub form_header { my %project_labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{"ALL_PROJECTS"} }; - my (@AR_amount_values); - my (@AR_values); - my (@AR_paid_values); - my %chart_labels; - my %charts; - my $taxchart_init; + my (@AR_paid_values, %AR_paid_labels); + my $default_ar_amount_chart_id; foreach my $item (@{ $form->{ALL_CHARTS} }) { if ($item->{link_split}{AR_amount}) { - $taxchart_init = $item->{tax_id} if ($taxchart_init eq ""); - my $key = "$item->{accno}--$item->{tax_id}"; - push(@AR_amount_values, $key); - } elsif ($item->{link_split}{AR}) { - push(@AR_values, $item->{accno}); + $default_ar_amount_chart_id //= $item->{id}; + } elsif ($item->{link_split}{AR_paid}) { push(@AR_paid_values, $item->{accno}); + $AR_paid_labels{$item->{accno}} = "$item->{accno}--$item->{description}"; } - - # weirdness for AR_amount - $chart_labels{$item->{accno}} = "$item->{accno}--$item->{description}"; - $chart_labels{"$item->{accno}--$item->{tax_id}"} = "$item->{accno}--$item->{description}"; - - $charts{$item->{accno}} = $item; - } - - my %taxchart_labels = (); - my @taxchart_values = (); - my %taxcharts = (); - foreach my $item (@{ $form->{ALL_TAXCHARTS} }) { - my $key = "$item->{id}--$item->{rate}"; - $taxchart_init = $key if ($taxchart_init eq $item->{id}); - push(@taxchart_values, $key); - $taxchart_labels{$key} = "$item->{taxdescription} " . ($item->{rate} * 100) . ' %'; - $taxcharts{$item->{id}} = $item; } my $follow_up_vc = $form->{customer}; $follow_up_vc =~ s/--.*?//; my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)"; - $form->{javascript} .= - qq|| . - qq|| . - qq||; + $::request->layout->add_javascripts("autocomplete_chart.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.GL.js"); -# $amount = $locale->text('Amount'); -# $project = $locale->text('Project'); + my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local; + my $first_taxchart; my @transactions; for my $i (1 .. $form->{rowcount}) { @@ -341,40 +301,26 @@ sub form_header { project_id => ($i==$form->{rowcount}) ? $form->{globalproject_id} : $form->{"project_id_$i"}, }; - my $selected_accno_full; - my ($accno_row) = split(/--/, $form->{"AR_amount_$i"}); - my $item = $charts{$accno_row}; - $selected_accno_full = "$item->{accno}--$item->{tax_id}"; + my (%taxchart_labels, @taxchart_values, $default_taxchart, $taxchart_to_use); + my $amount_chart_id = $form->{"AR_amount_chart_id_$i"} // $default_ar_amount_chart_id; + my $chart_has_changed = $::form->{"previous_AR_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AR_amount_chart_id_$i"}); - my $selected_taxchart = $form->{"taxchart_$i"}; - my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full); - my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AR_amount_$i"}); + foreach my $item ( GL->get_active_taxes_for_chart($amount_chart_id, $transdate) ) { + my $key = $item->id . "--" . $item->rate; + $first_taxchart //= $item; + $default_taxchart = $item if $item->{is_default}; + $taxchart_to_use = $item if $key eq $form->{"taxchart_$i"}; - if ($previous_accno && - ($previous_accno eq $selected_accno) && - ($previous_tax_id ne $selected_tax_id)) { - my $item = $taxcharts{$selected_tax_id}; - $selected_taxchart = "$item->{id}--$item->{rate}"; + push(@taxchart_values, $key); + $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %'; } - if (!$form->{"taxchart_$i"}) { - if ($form->{"AR_amount_$i"} =~ m/.--./) { - $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} }; - } else { - $selected_taxchart = $taxchart_init; - } - } + $taxchart_to_use = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use; + my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate; $transaction->{selectAR_amount} = - NTI($cgi->popup_menu('-name' => "AR_amount_$i", - '-id' => "AR_amount_$i", - '-style' => 'width:400px', - '-onChange' => "setTaxkey(this, $i)", - '-values' => \@AR_amount_values, - '-labels' => \%chart_labels, - '-default' => $selected_accno_full)) - . $cgi->hidden('-name' => "previous_AR_amount_$i", - '-default' => $selected_accno_full); + $::request->presenter->chart_picker("AR_amount_chart_id_$i", $amount_chart_id, style => "width: 400px", type => "AR_amount", class => ($form->{initial_focus} eq "row_$i" ? "initial_focus" : "")) + . $::request->presenter->hidden_tag("previous_AR_amount_chart_id_$i", $amount_chart_id); $transaction->{taxchart} = NTI($cgi->popup_menu('-name' => "taxchart_$i", @@ -389,13 +335,6 @@ sub form_header { $form->{invtotal_unformatted} = $form->{invtotal}; - $ARselected = - NTI($cgi->popup_menu('-name' => "ARselected", '-id' => "ARselected", - '-style' => 'width:400px', - '-values' => \@AR_values, '-labels' => \%chart_labels, - '-default' => $form->{ARselected})); - - $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"}); my $now = $form->current_date(\%myconfig); @@ -423,7 +362,7 @@ sub form_header { NTI($cgi->popup_menu('-name' => "AR_paid_$i", '-id' => "AR_paid_$i", '-values' => \@AR_paid_values, - '-labels' => \%chart_labels, + '-labels' => \%AR_paid_labels, '-default' => $payment->{AR_paid} || $form->{accno_arap})); @@ -457,7 +396,7 @@ sub form_header { transactions => \@transactions, project_labels => \%project_labels, rows => $rows, - ARselected => $ARselected, + AR_chart_id => $form->{AR_chart_id}, title_str => $title, follow_up_trans_info => $follow_up_trans_info, today => DateTime->today, @@ -570,17 +509,8 @@ sub update { $form->{invdate} = $form->{transdate}; - my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1 customer_id notes); - &check_name("customer"); - $form->{AR} = $saved_variables{AR}; - if ($saved_variables{AR_amount_1} =~ m/.--./) { - map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1); - } else { - delete $form->{taxchart_1}; - } - $form->{invtotal} = ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax; diff --git a/templates/webpages/ar/form_footer.html b/templates/webpages/ar/form_footer.html index df81ee58c..5ce251c39 100644 --- a/templates/webpages/ar/form_footer.html +++ b/templates/webpages/ar/form_footer.html @@ -44,3 +44,17 @@ [% END %] + + diff --git a/templates/webpages/ar/form_header.html b/templates/webpages/ar/form_header.html index bcae2be41..258a40a57 100644 --- a/templates/webpages/ar/form_header.html +++ b/templates/webpages/ar/form_header.html @@ -1,7 +1,7 @@ [%- USE HTML %] [%- USE L %] [%- USE T8 %] -[%- USE LxERP %] +[%- USE LxERP %][%- USE P -%]
@@ -43,9 +43,9 @@ [% 'Customer' | $T8 %] [%- IF selectcustomer %] - + [%- ELSE %] - + [%- END %] [% L.hidden_tag('selectcustomer', selectcustomer) %] @@ -186,7 +186,7 @@ - [% ARselected %] + [% P.chart_picker("AR_chart_id", AR_chart_id, style="width: 400px", type="AR") %] [% LxERP.format_amount(invtotal, 2) | html %] -- 2.20.1