}
+sub retrieve_taxaccounts {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $parts_id, $index, $copy_accnos) = @_;
+
+ my ($query, $sth, $dbh);
+
+ return $main::lxdebug->leave_sub() if (!defined($form->{"taxzone_id"}));
+
+ $dbh = $form->dbconnect($myconfig);
+
+ my $transdate = "";
+ if ($form->{type} eq "invoice") {
+ if (($form->{vc} eq "vendor") || !$form->{deliverydate}) {
+ $transdate = $form->{invdate};
+ } else {
+ $transdate = $form->{deliverydate};
+ }
+ } else {
+ $transdate = $form->{transdate};
+ }
+
+ if ($transdate eq "") {
+ $transdate = "current_date";
+ } else {
+ $transdate = $dbh->quote($transdate);
+ }
+
+ my $inc_exp = $form->{vc} eq "customer" ? "income" : "expense";
+
+ my $accno_str = "${inc_exp}_accno_id_$form->{taxzone_id}";
+
+ $query =
+ "SELECT bg.$accno_str AS accno_id, c.accno " .
+ "FROM buchungsgruppen bg " .
+ "LEFT JOIN chart c ON bg.$accno_str = c.id " .
+ "WHERE bg.id = (SELECT p.buchungsgruppen_id FROM parts p WHERE p.id = ?)";
+ $sth = $dbh->prepare($query);
+ $sth->execute($parts_id) || $form->dberror($query . " ($parts_id)");
+ my $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+
+ if (!$ref) {
+ $dbh->disconnect();
+ return $lxdebug->leave_sub();
+ }
+
+ my ($accno_id, $accno) = ($ref->{"accno_id"}, $ref->{"accno"});
+ my ($old_accno_id, $old_accno) = ($ref->{"accno_id"}, $ref->{"accno"});
+
+ my @visited_accno_ids = ($accno_id);
+
+ $query =
+ "SELECT c.new_chart_id, date($transdate) >= c.valid_from AS is_valid, " .
+ " cnew.accno " .
+ "FROM chart c " .
+ "LEFT JOIN chart cnew ON c.new_chart_id = cnew.id " .
+ "WHERE (c.id = ?) AND NOT c.new_chart_id ISNULL AND (c.new_chart_id > 0)";
+ $sth = $dbh->prepare($query);
+
+ while (1) {
+ $sth->execute($accno_id) || $form->dberror($query . " ($accno_id)");
+ $ref = $sth->fetchrow_hashref();
+ last unless ($ref && $ref->{"is_valid"} &&
+ !grep({ $_ == $ref->{"new_chart_id"} } @visited_accno_ids));
+ $accno_id = $ref->{"new_chart_id"};
+ $accno = $ref->{"accno"};
+ push(@visited_accno_ids, $accno_id);
+ }
+
+# $main::lxdebug->message(0, "found final accno_id $accno_id accno $accno for old accno_id $old_accno_id accno $old_accno");
+
+ $query =
+ "SELECT c.accno, t.taxdescription AS description, t.rate, t.taxnumber " .
+ "FROM tax t " .
+ "LEFT JOIN chart c ON c.id = t.chart_id " .
+ "WHERE t.id IN " .
+ " (SELECT tk.tax_id " .
+ " FROM taxkeys tk " .
+ " WHERE tk.chart_id = $accno_id AND startdate <= $transdate " .
+ " ORDER BY startdate DESC LIMIT 1) ";
+ $sth = $dbh->prepare($query);
+ $sth->execute() || $form->dberror($query);
+ $ref = $sth->fetchrow_hashref();
+ $sth->finish();
+ $dbh->disconnect();
+
+ return $lxdebug->leave_sub() unless ($ref);
+
+ $form->{"taxaccounts_$index"} = $ref->{"accno"};
+ if ($form->{"taxaccounts"} !~ /$ref->{accno}/) {
+ $form->{"taxaccounts"} .= "$ref->{accno} ";
+ }
+ map({ $form->{"$ref->{accno}_${_}"} = $ref->{$_}; }
+ qw(rate description taxnumber));
+
+# $main::lxdebug->message(0, "formvars: rate " . $form->{"$ref->{accno}_rate"} .
+# " description " . $form->{"$ref->{accno}_description"} .
+# " taxnumber " . $form->{"$ref->{accno}_taxnumber"} .
+# " || taxaccounts_$index " . $form->{"taxaccounts_$index"} .
+# " || taxaccounts " . $form->{"taxaccounts"});
+
+ $sth->finish();
+
+ $main::lxdebug->leave_sub();
+}
1;
#
#######################################################################
+use SL::IC;
+
# any custom scripts for this one
if (-f "$form->{path}/custom_io.pl") {
eval { require "$form->{path}/custom_io.pl"; };
sub display_form {
$lxdebug->enter_sub();
+
+ relink_accounts();
+
$form->language_payment(\%myconfig);
# if we have a display_form
$lxdebug->leave_sub();
}
+sub relink_accounts {
+ $lxdebug->enter_sub();
+
+ $form->{"taxaccounts"} =~ s/\s*$//;
+ $form->{"taxaccounts"} =~ s/^\s*//;
+ foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
+ map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
+ }
+ $form->{"taxaccounts"} = "";
+
+ for ($i = 1; $i <= $form->{"rowcount"}; $i++) {
+ if ($form->{"id_$i"}) {
+ IC->retrieve_taxaccounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
+ }
+ }
+
+ $lxdebug->leave_sub();
+}
$form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber");
}
+ relink_accounts();
$form->redirect( $locale->text('Invoice')
. " $form->{invnumber} "
. $locale->text('posted!'))
($form->{AR}) = split /--/, $form->{AR};
($form->{AR_paid}) = split /--/, $form->{AR_paid};
+ relink_accounts();
$form->redirect($locale->text(' Payment posted!'))
if (IS->post_payment(\%myconfig, \%$form));
$form->error($locale->text('Cannot post payment!'));
$form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber");
}
}
+ relink_accounts();
if ($print_post) {
if (!(IS->post_invoice(\%myconfig, \%$form))) {
$form->error($locale->text('Cannot post invoice!'));
$form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
}
+ relink_accounts();
+
$form->redirect(
$form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
if (OE->save(\%myconfig, \%$form));
$form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
unless $form->{$ordnumber};
+ relink_accounts();
OE->save(\%myconfig, \%$form);
$form->{simple_save} = 1;
# if not it's most likely a collective order, which can't be saved back
# so they just have to be closed
if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
+ relink_accounts();
OE->save(\%myconfig, \%$form);
} else {
OE->close_orders(\%myconfig, \%$form);
} qw(sellprice discount);
}
+ relink_accounts();
+
OE->save(\%myconfig, \%$form);
# rebuild rows for invoice
if ( $form->{type} eq 'sales_quotation'
|| $form->{type} eq 'request_quotation') {
$form->{closed} = 1;
+ relink_accounts();
OE->save(\%myconfig, \%$form);
}
if ( $form->{type} eq 'sales_quotation'
|| $form->{type} eq 'request_quotation') {
$form->{closed} = 1;
+ relink_accounts();
OE->save(\%myconfig, \%$form);
}
'print_options' => 'print_options',
'project_selected' => 'project_selected',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'sales_invoice' => 'sales_invoice',
'section_menu' => 'section_menu',
'select_item' => 'select_item',
'print_options' => 'print_options',
'project_selected' => 'project_selected',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'sales_invoice' => 'sales_invoice',
'save' => 'save',
'save_dunning' => 'save_dunning',
'print_form' => 'print_form',
'print_options' => 'print_options',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'restock_assemblies' => 'restock_assemblies',
'save' => 'save',
'save_as_new' => 'save_as_new',
'print_form' => 'print_form',
'print_options' => 'print_options',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'select_item' => 'select_item',
'send_email' => 'send_email',
'set_pricegroup' => 'set_pricegroup',
'print_options' => 'print_options',
'project_selected' => 'project_selected',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'sales_invoice' => 'sales_invoice',
'section_menu' => 'section_menu',
'select_item' => 'select_item',
'print_options' => 'print_options',
'project_selected' => 'project_selected',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'sales_invoice' => 'sales_invoice',
'section_menu' => 'section_menu',
'select_item' => 'select_item',
'project_selected' => 'project_selected',
'purchase_order' => 'purchase_order',
'quotation' => 'quotation',
+ 'relink_accounts' => 'relink_accounts',
'sales_invoice' => 'sales_invoice',
'sales_order' => 'sales_order',
'save' => 'save',