diff --git a/tine20/Sales/Controller/Invoice.php b/tine20/Sales/Controller/Invoice.php index 99b1f7f4bd..04d4ac4c22 100644 --- a/tine20/Sales/Controller/Invoice.php +++ b/tine20/Sales/Controller/Invoice.php @@ -1503,6 +1503,13 @@ protected function _inspectAfterSetRelatedDataUpdate($updatedRecord, $record, $c public function createXRechnungsAttachment(Sales_Model_Invoice $invoice): void { + $remove = null; + foreach ($invoice->attachments?->filter(fn ($rec) => str_ends_with($rec->name, '-xrechnung.xml')) ?? [] as $remove) { + $invoice->attachments->removeRecord($remove); + } + if (null !== $remove) { + Tinebase_FileSystem_RecordAttachments::getInstance()->setRecordAttachments($invoice); + } try { $customer = $this->_getCustomerFromInvoiceRelations($invoice) ?? throw new Tinebase_Exception_SystemGeneric('invoice does not have a customer'); /** @var Sales_Model_Contract $contract */ @@ -1569,10 +1576,6 @@ public function createXRechnungsAttachment(Sales_Model_Invoice $invoice): void } $attachmentName = str_replace('/', '-', $customer->getTitle() . '_' . $invoice->number . '-xrechnung.xml'); - if (null !== ($remove = $invoice->attachments?->find('name', $attachmentName))) { - $invoice->attachments->removeRecord($remove); - Tinebase_FileSystem_RecordAttachments::getInstance()->setRecordAttachments($invoice); - } Tinebase_FileSystem_RecordAttachments::getInstance()->addRecordAttachment($invoice, $attachmentName, $stream); Tinebase_FileSystem_RecordAttachments::getInstance()->getRecordAttachments($invoice); } catch (Exception $e) {