- # Retrieve the invoice's AR/AP chart ID.
- do_statement($form, @{ $handles{get_arap} }, $orig_item->{"${arap}_id"});
- my ($arap_chart_id) = $handles{get_arap}->[0]->fetchrow_array();
-
- # Record the payment in acc_trans offsetting AR/AP.
- do_statement($form, @{ $handles{add_acc_trans} }, $orig_item->{"${arap}_id"}, $arap_chart_id, -1 * $mult * $orig_item->{amount}, $item->{execution_date}, '', $arap_chart_id);
- do_statement($form, @{ $handles{add_acc_trans} }, $orig_item->{"${arap}_id"}, $orig_item->{chart_id}, $mult * $orig_item->{amount}, $item->{execution_date}, $orig_item->{reference},
- $orig_item->{chart_id});
-
- # Update the invoice to reflect the new paid amount.
- do_statement($form, @{ $handles{update_arap} }, $orig_item->{amount}, $orig_item->{"${arap}_id"});
-
- # Update datepaid of invoice. set_datepaid (which has some extra logic)
- # finds the date from acc_trans, where the payment has already been
- # recorded above, so we don't need to explicitly pass
- # $item->{execution_date}
- IO->set_datepaid(table => "$arap", id => $orig_item->{"${arap}_id"}, dbh => $dbh);
+ # fetch item_id via Rose (same id as orig_item)
+ my $sepa_export_item = SL::DB::Manager::SepaExportItem->find_by( id => $item_id);
+
+ my $invoice;
+
+ if ( $sepa_export_item->ar_id ) {
+ $invoice = SL::DB::Manager::Invoice->find_by( id => $sepa_export_item->ar_id);
+ } elsif ( $sepa_export_item->ap_id ) {
+ $invoice = SL::DB::Manager::PurchaseInvoice->find_by( id => $sepa_export_item->ap_id);
+ } else {
+ die "sepa_export_item needs either ar_id or ap_id\n";
+ };
+
+ $invoice->pay_invoice(amount => $sepa_export_item->amount,
+ payment_type => $sepa_export_item->payment_type,
+ chart_id => $sepa_export_item->chart_id,
+ source => $sepa_export_item->reference,
+ transdate => $item->{execution_date}, # value from user form
+ );