$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?
# 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);
# 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 {
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);
}
$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 = ?)
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);
$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};
}
}
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;
return join ' ', grep $_, $self->customernumber, $self->name;
}
+sub last_used_ar_chart {
+ my ($self) = @_;
+
+ my $query = <<EOSQL;
+ SELECT c.id
+ 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 (c.category = 'I')
+ AND (c.link !~ '_(paid|tax)')
+ AND (a.id IN (SELECT max(a2.id) FROM ar a2 WHERE a2.customer_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);
+}
+
sub is_customer { 1 };
sub is_vendor { 0 };
sub payment_terms { goto &payment }
# 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 (
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} };
}
# 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 ?
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} };
$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
}
$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();
}
use SL::AR;
use SL::FU;
+use SL::GL;
use SL::IS;
use SL::DB::Default;
use SL::DB::Invoice;
$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();
}
$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);
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|<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>|;
- # show history button js
- $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
- #/show history button js
$readonly = ($form->{id}) ? "readonly" : "";
$form->{radier} = ($::instance_conf->get_ar_changeable == 2)
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|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
- qq|<script type="text/javascript" src="js/follow_up.js"></script>| .
- 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");
-# $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}) {
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",
$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);
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}));
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,
$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;
[% END %]
</form>
+
+<script type="text/javascript">
+ <!--
+$(document).ready(function() {
+ [%- SET row=0 %]
+ [%- WHILE row < rowcount %]
+ [%- SET row=row + 1 %]
+ $('#AR_amount_chart_id_[% row %]').on('set_item:ChartPicker', function(e, item) {
+ kivi.GL.update_taxes(this);
+ });
+ [%- END %]
+});
+-->
+</script>
[%- USE HTML %]
[%- USE L %]
[%- USE T8 %]
-[%- USE LxERP %]
+[%- USE LxERP %][%- USE P -%]
<form method=post name="arledger" action="[% script %]">
<th align="right" nowrap>[% 'Customer' | $T8 %]</th>
<td colspan=3>
[%- IF selectcustomer %]
- <select id='customer' name="customer" onchange="document.getElementById('update_button').click();" class="initial_focus">[% selectcustomer %]</select>
+ <select id='customer' name="customer" onchange="document.getElementById('update_button').click();"[% IF initial_focus == 'customer'%] class="initial_focus"[% END %]>[% selectcustomer %]</select>
[%- ELSE %]
- <input id='customer' name=customer value="[% customer | html %]" size=35 class="initial_focus">
+ <input id='customer' name=customer value="[% customer | html %]" size=35[% IF initial_focus == 'customer'%] class="initial_focus"[% END %]>
[%- END %]
<input type="button" value="[% 'Details (one letter abbreviation)' | $T8 %]" onclick="show_vc_details('customer')"></td>
[% L.hidden_tag('selectcustomer', selectcustomer) %]
</td>
</tr>
<tr>
- <td>[% ARselected %]</td>
+ <td>[% P.chart_picker("AR_chart_id", AR_chart_id, style="width: 400px", type="AR") %]</td>
<th align=left>[% LxERP.format_amount(invtotal, 2) | html %]</th>
<input type=hidden name=oldinvtotal value='[% oldinvtotal %]'>