$form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
}
- for my $i (1 .. $form->{rowcount}) {
- $form->{AP_amounts}{"amount_$i"} =
- (split(/--/, $form->{"AP_amount_$i"}))[0];
- }
-
- ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
- ($form->{AP_payables}) = split(/--/, $form->{APselected});
+ # get the charts selected
+ $form->{AP_amounts}{"amount_$_"} = $form->{"AP_amount_chart_id_$_"} for (1 .. $form->{rowcount});
# calculate the totals while calculating and reformatting the $amount_$i and $tax_$i
($form->{netamount},$form->{total_tax},$form->{invtotal}) = $form->calculate_arap('buy',$form->{taxincluded}, $form->{exchangerate});
$query =
qq|INSERT INTO acc_trans | .
qq| (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link)| .
- qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
- qq| ?, ?, ?, ?, ?,| .
- qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
- @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
+ qq|VALUES (?, ?, ?, ?, ?, ?, ?, (SELECT c.link FROM chart c WHERE c.id = ?))|;
+ @values = ($form->{id}, $form->{"AP_amount_chart_id_$i"},
$form->{"amount_$i"}, conv_date($form->{transdate}),
$project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
- $form->{AP_amounts}{"amount_$i"});
+ $form->{"AP_amount_chart_id_$i"});
do_query($form, $dbh, $query, @values);
if ($form->{"tax_$i"} != 0) {
# add payables
$query =
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) | .
- qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
- qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
+ qq|VALUES (?, ?, ?, ?, | .
+ qq| (SELECT taxkey_id FROM chart WHERE id = ?),| .
qq| (SELECT tax_id| .
qq| FROM taxkeys| .
- qq| WHERE chart_id= (SELECT id | .
- qq| FROM chart| .
- qq| WHERE accno = ?)| .
+ qq| WHERE chart_id = ?| .
qq| AND startdate <= ?| .
qq| ORDER BY startdate DESC LIMIT 1),| .
- qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
- @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
- conv_date($form->{transdate}), $form->{AP_amounts}{payables}, $form->{AP_amounts}{payables}, conv_date($form->{transdate}),
- $form->{AP_amounts}{payables});
+ qq| (SELECT c.link FROM chart c WHERE c.id = ?))|;
+ @values = ($form->{id}, $form->{AP_chart_id}, $form->{payables},
+ conv_date($form->{transdate}), $form->{AP_chart_id}, $form->{AP_chart_id}, conv_date($form->{transdate}),
+ $form->{AP_chart_id});
do_query($form, $dbh, $query, @values);
}
if ($form->{payables}) {
$query =
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) | .
- qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
- qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
+ qq|VALUES (?, ?, ?, ?, ?, | .
+ qq| (SELECT taxkey_id FROM chart WHERE id = ?),| .
qq| (SELECT tax_id| .
qq| FROM taxkeys| .
- qq| WHERE chart_id= (SELECT id | .
- qq| FROM chart| .
- qq| WHERE accno = ?)| .
+ qq| WHERE chart_id = ?| .
qq| AND startdate <= ?| .
qq| ORDER BY startdate DESC LIMIT 1),| .
- qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
- @values = ($form->{id}, $form->{AP_payables}, $amount,
+ qq| (SELECT c.link FROM chart c WHERE c.id = ?))|;
+ @values = ($form->{id}, $form->{AP_chart_id}, $amount,
conv_date($form->{"datepaid_$i"}), $project_id,
- $form->{AP_payables}, $form->{AP_payables}, conv_date($form->{"datepaid_$i"}),
- $form->{AP_payables});
+ $form->{AP_chart_id}, $form->{AP_chart_id}, conv_date($form->{"datepaid_$i"}),
+ $form->{AP_chart_id});
do_query($form, $dbh, $query, @values);
}
$form->{payables} = $amount;
$form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
$form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
$form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
- }
-
- $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
-
- my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
- $form->{"select${key}"} =~
- m/<option value=\"
- ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
- \">
- $form->{acc_trans}{$key}->[$i-1]->{accno}
- --
- ${q_description}
- <\/option>\n/x;
- $form->{"${key}_$k"} = $1;
-
- if ($akey eq "AP") {
- $form->{APselected} = $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};
}
}
use Rose::DB::Object::Helpers qw(as_tree);
+use SL::DBUtils ();
use SL::DB::MetaSetup::Vendor;
use SL::DB::Manager::Vendor;
use SL::DB::Helper::IBANValidation;
sub payment_terms { goto &payment }
sub number { goto &vendornumber }
+sub last_used_ap_chart {
+ my ($self) = @_;
+
+ my $query = <<EOSQL;
+ SELECT c.id
+ FROM chart c
+ JOIN acc_trans ac ON (ac.chart_id = c.id)
+ JOIN ap a ON (a.id = ac.trans_id)
+ WHERE (a.vendor_id = ?)
+ AND (c.category = 'E')
+ AND (c.link !~ '_(paid|tax)')
+ AND (a.id IN (SELECT max(a2.id) FROM ap a2 WHERE a2.vendor_id = ?))
+ ORDER BY ac.acc_trans_id ASC
+ LIMIT 1
+EOSQL
+
+ my ($chart_id) = SL::DBUtils::selectfirst_array_query($::form, $self->db->dbh, $query, ($self->id) x 2);
+
+ return if !$chart_id;
+ return SL::DB::Chart->load_cached($chart_id);
+}
+
1;
}
$sth->finish();
- if (!$params->{id} && $params->{type} !~ /_(order|quotation)/) {
- # setup last accounts used
- $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 ap a ON (a.id = ac.trans_id)
- WHERE (a.vendor_id = ?)
- AND (NOT ((c.link LIKE '%_tax%') OR (c.link LIKE '%_paid%')))
- AND (a.id IN (SELECT max(a2.id) FROM ap a2 WHERE a2.vendor_id = ?))|;
- my $refs = selectall_hashref_query($form, $dbh, $query, $vid, $vid);
-
- my $i = 0;
- for $ref (@$refs) {
- if ($ref->{category} eq 'E') {
- $i++;
- my ($tax_id, $rate);
- if ($params->{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 <= ?)
- ORDER BY tk.startdate DESC
- LIMIT 1|;
- ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id}, $params->{initial_transdate});
- $params->{"taxchart_$i"} = "${tax_id}--${rate}";
- }
-
- $params->{"AP_amount_$i"} = "$ref->{accno}--$tax_id";
- }
-
- if ($ref->{category} eq 'L') {
- $params->{APselected} = $params->{AP_1} = $ref->{accno};
- }
- }
- $params->{rowcount} = $i if ($i && !$params->{type});
- }
-
$main::lxdebug->leave_sub();
}
use SL::AP;
use SL::FU;
+use SL::GL;
use SL::IR;
use SL::IS;
use SL::ReportGenerator;
create_links(dont_save => 1);
$form->{transdate} = $form->{initial_transdate};
+ if ($form->{vendor_id}) {
+ my $last_used_ap_chart = SL::DB::Vendor->load_cached($form->{vendor_id})->last_used_ap_chart;
+ $form->{"AP_amount_chart_id_1"} = $last_used_ap_chart->id if $last_used_ap_chart;
+ }
+
&display_form;
$main::lxdebug->leave_sub();
$form->{$_} = $saved{$_} for keys %saved;
$form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
$form->{rowcount} = 1;
+ $form->{AP_chart_id} = $form->{acc_trans} && $form->{acc_trans}->{AP} ? $form->{acc_trans}->{AP}->[0]->{chart_id} : $form->{AP_links}->{AP}->[0]->{chart_id};
# build the popup menus
$form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
$::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
+ $form->{initial_focus} = !($form->{amount_1} * 1) ? 'vendor' : 'row_' . $form->{rowcount};
+
$form->{title_} = $form->{title};
$form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
}
my %charts;
- my $taxchart_init;
+ my $default_ap_amount_chart_id;
foreach my $item (@{ $form->{ALL_CHARTS} }) {
if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
- if ( $taxchart_init eq '' ) {
- $taxchart_init = $item->{tax_id};
- }
+ $default_ap_amount_chart_id //= $item->{id};
- push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
- }
- elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
- push(@{ $form->{ALL_CHARTS_AP} }, $item);
- }
- elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
+ } elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
}
my %taxcharts = ();
foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
- my $key = $item->{id} .'--'. $item->{rate};
-
- if ( $taxchart_init eq $item->{id} ) {
- $taxchart_init = $key;
- }
-
$taxcharts{$item->{id}} = $item;
}
$follow_up_vc =~ s/--.*?//;
my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
- $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
- $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
- $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
- $form->{javascript} .= qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
+ $::request->layout->add_javascripts("autocomplete_chart.js", "show_vc_details.js", "show_history.js", "follow_up.js", "kivi.Draft.js", "kivi.GL.js");
+ my $transdate = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
+ my $first_taxchart;
$form->header();
$form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
$form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
- my $selected_accno_full;
- my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
- my $item = $charts{$accno_row};
- $selected_accno_full = "$item->{accno}--$item->{tax_id}";
+ my ($default_taxchart, $taxchart_to_use);
+ my $amount_chart_id = $form->{"AP_amount_chart_id_$i"} || $default_ap_amount_chart_id;
+ my $chart_has_changed = $::form->{"previous_AP_amount_chart_id_$i"} && ($amount_chart_id != $::form->{"previous_AP_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_AP_amount_$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}";
+ 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"};
}
- $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
-
- $form->{'selected_accno_full_'. $i} = $selected_accno_full;
-
- $form->{'selected_taxchart_'. $i} = $selected_taxchart;
+ $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;
+ $form->{"selected_taxchart_$i"} = $selected_taxchart;
+ $form->{"AP_amount_chart_id_$i"} = $amount_chart_id;
}
- $form->{AP_amount_value_title_sub} = sub {
- my $item = shift;
- return [
- $item->{accno} .'--'. $item->{tax_id},
- $item->{accno} .'--'. $item->{description},
- ];
- };
-
$form->{taxchart_value_title_sub} = sub {
my $item = shift;
return [
];
};
- $form->{APselected_value_title_sub} = sub {
- my $item = shift;
- return [
- $item->{accno},
- $item->{accno} .'--'. $item->{description}
- ];
- };
-
$form->{invtotal_unformatted} = $form->{invtotal};
$form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
$form->{exchangerate} = $form->{forex} if $form->{forex};
$form->{invdate} = $form->{transdate};
- my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1 notes);
my $vendor_changed = &check_name("vendor");
- $form->{AP} = $saved_variables{AP};
- if ($saved_variables{AP_amount_1} =~ m/.--./) {
- map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
- } else {
- delete $form->{taxchart_1};
- }
-
$form->{rowcount} = $count + 1;
$form->{invtotal} =
&update;
$::dispatcher->end_request;
}
- my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
- my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
- my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
-# $form->{AP_amount_1} = $debitaccno;
- $form->{AP_payables} = $payablesaccno;
- $form->{taxkey} = $taxkey;
$form->{storno} = 0;
$form->{id} = 0 if $form->{postasnew};
[%- END %]
</form>
+
+<script type="text/javascript">
+ <!--
+$(document).ready(function() {
+ [%- SET row=0 %]
+ [%- WHILE row < rowcount %]
+ [%- SET row=row + 1 %]
+ $('#AP_amount_chart_id_[% row %]').on('set_item:ChartPicker', function(e, item) {
+ kivi.GL.update_taxes(this);
+ });
+ [%- END %]
+});
+-->
+</script>
[%- USE L %]
[%- USE HTML %]
[%- USE T8 %]
-[%- USE LxERP %]
-
-<script type="text/javascript">
-<!--
- function setTaxkey(accno, row) {
- var taxkey = accno.options[accno.selectedIndex].value;
- var reg = /--([0-9]*)/;
- var found = reg.exec(taxkey);
- var index = found[1];
- index = parseInt(index);
- var tax = 'taxchart_' + row;
- for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
- var reg2 = new RegExp("^"+ index, "");
- if (reg2.exec(document.getElementById(tax).options[i].value)) {
- document.getElementById(tax).options[i].selected = true;
- break;
- }
- }
- };
-//-->
-</script>
-
-<script type="text/javascript" src="js/show_history.js"></script>
+[%- USE LxERP %][%- USE P -%]
<form method="post" action="[% script | html %]">
<th align="right" nowrap>[% 'Vendor' | $T8 %]</th>
<td colspan="3">
[% IF ( selectvendor ) %]
- <select name="vendor" onchange="document.getElementById('update_button').click();">[% selectvendor %]</select>
+ <select name="vendor" onchange="document.getElementById('update_button').click();"[% IF initial_focus == 'vendor' %] class="initial_focus"[% END %]>[% selectvendor %]</select>
[% ELSE %]
- <input name=vendor value="[% vendor | html %]" size="35">
+ <input name=vendor value="[% vendor | html %]" size="35"[% IF initial_focus == 'vendor' %] class="initial_focus"[% END %]>
[% END %]
<input type="button" value="D" onclick="show_vc_details('vendor')">
</td>
[% FOREACH i IN [1..rowcount] %]
<tr>
<td>
- [% selected_accno_full = "selected_accno_full_"_ i %]
- [% L.select_tag('AP_amount_'_ i, ALL_CHARTS_AP_amount, value_title_sub = \AP_amount_value_title_sub, onchange = 'setTaxkey(this, '_ i _')', default = $selected_accno_full) %]
-
- <input type="hidden" name="previous_AP_amount_[% i %]" value="[% $selected_accno_full %]">
+ [% SET selected_chart_id = "AP_amount_chart_id_"_ i %]
+ [% P.chart_picker("AP_amount_chart_id_" _ i, $selected_chart_id, style="width: 400px", type="AP_amount", class=(initial_focus == 'row_' _ i ? "initial_focus" : "")) %]
+ [% L.hidden_tag("previous_AP_amount_chart_id_" _ i, $selected_chart_id) %]
<input type="hidden" name="tax_[% i %]" value="[% temp = "tax"_ i %][% $temp | html %]">
</td>
<td>
</tr>
<tr>
<td>
- [% L.select_tag('APselected', ALL_CHARTS_AP, value_title_sub = \APselected_value_title_sub, default = APselected) %]
+ [% P.chart_picker('AP_chart_id', AP_chart_id, style="width: 400px", type="AP") %]
</td>
<th align="left">[% invtotal | html %]</th>
<td colspan="4"></td>