Shouldn't it be something like....
// set orders as paid
if ($new_os->paid == 1) {
$invoices = $order->getInvoicesCollection();
if ($order->total_paid != 0) {
$payment_method = Module::getInstanceByName($order->module);
}
foreach ($invoices as $invoice) {
/** @var OrderInvoice $invoice */
$rest_paid = $invoice->getRestPaid();
if ($rest_paid > 0 && $order->total_paid == 0) {
// added && $order->total_paid == 0 so an allready paid order doesn't get 'paid' double or more in the stats and db entries
$payment = new OrderPayment();
$payment->order_reference = Tools::substr($order->reference, 0, 9);
$payment->id_currency = $order->id_currency;
$payment->amount = $rest_paid;
if ($order->total_paid != 0) {
$payment->payment_method = $payment_method->displayName;
} else {
$payment->payment_method = null;
}
// Update total_paid_real value for backward compatibility reasons
if ($payment->id_currency == $order->id_currency) {
$order->total_paid_real += $payment->amount;
} else {
$order->total_paid_real += Tools::ps_round(Tools::convertPrice($payment->amount, $payment->id_currency, false), 2);
}
$order->save();
$payment->conversion_rate = 1;
$payment->save();
Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'order_invoice_payment` (`id_order_invoice`, `id_order_payment`, `id_order`)
VALUES('.(int)$invoice->id.', '.(int)$payment->id.', '.(int)$order->id.')');
}
}
}