+ my $query = qq|DELETE FROM inventory WHERE invoice_id
+ IN (SELECT id FROM invoice WHERE trans_id = ?)|;
+
+ do_query($form, $dbh, $query, $id);
+
+ $::lxdebug->leave_sub;
+}
+
+sub _delete_payments {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $form, $dbh) = @_;
+
+ my @delete_acc_trans_ids;
+
+ # Delete old payment entries from acc_trans.
+ my $query =
+ qq|SELECT acc_trans_id
+ FROM acc_trans
+ WHERE (trans_id = ?) AND fx_transaction
+
+ UNION
+
+ SELECT at.acc_trans_id
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|;
+ push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
+
+ $query =
+ qq|SELECT at.acc_trans_id
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (trans_id = ?)
+ AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
+ ORDER BY at.acc_trans_id
+ OFFSET 1|;
+ push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
+
+ if (@delete_acc_trans_ids) {
+ $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
+ do_query($form, $dbh, $query);
+ }
+
+ $main::lxdebug->leave_sub();
+}
+
+sub post_payment {
+ my ($self, $myconfig, $form, $locale) = @_;
+ $main::lxdebug->enter_sub();
+
+ my $rc = SL::DB->client->with_transaction(\&_post_payment, $self, $myconfig, $form, $locale);
+
+ $::lxdebug->leave_sub;
+ return $rc;
+}
+
+sub _post_payment {
+ my ($self, $myconfig, $form, $locale) = @_;
+
+ my $dbh = SL::DB->client->dbh;
+
+ my (%payments, $old_form, $row, $item, $query, %keep_vars);
+
+ $old_form = save_form();
+
+ $query = <<SQL;
+ SELECT at.acc_trans_id, at.amount, at.cleared, c.accno
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (at.trans_id = ?)
+SQL
+
+ my %already_cleared = selectall_as_map($form, $dbh, $query, 'acc_trans_id', [ qw(amount cleared accno) ], $form->{id});
+
+ # Delete all entries in acc_trans from prior payments.
+ if (SL::DB::Default->get->payments_changeable != 0) {
+ $self->_delete_payments($form, $dbh);
+ }
+
+ # Save the new payments the user made before cleaning up $form.
+ map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^gldate_\d+$|^acc_trans_id_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AR_paid_\d+$|^paidaccounts$/, keys %{ $form };
+
+ # Clean up $form so that old content won't tamper the results.
+ %keep_vars = map { $_, 1 } qw(login password id);
+ map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
+
+ # Retrieve the invoice from the database.
+ $self->retrieve_invoice($myconfig, $form);
+
+ # Set up the content of $form in the way that IS::post_invoice() expects.
+ $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
+
+ for $row (1 .. scalar @{ $form->{invoice_details} }) {
+ $item = $form->{invoice_details}->[$row - 1];
+
+ map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice discount);
+
+ map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
+ }
+
+ $form->{rowcount} = scalar @{ $form->{invoice_details} };
+
+ delete @{$form}{qw(invoice_details paidaccounts storno paid)};
+
+ # Restore the payment options from the user input.
+ map { $form->{$_} = $payments{$_} } keys %payments;
+
+ # Get the AR accno (which is normally done by Form::create_links()).
+ $query =
+ qq|SELECT c.accno
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (trans_id = ?)
+ AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
+ ORDER BY at.acc_trans_id
+ LIMIT 1|;
+
+ ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
+
+ # Post the new payments.
+ $self->post_invoice($myconfig, $form, $dbh, payments_only => 1, already_cleared => \%already_cleared);
+
+ restore_form($old_form);
+
+ return 1;