From a7cbdafe74c81bec3398356d369b032b3d4e5c2f Mon Sep 17 00:00:00 2001 From: Paul Mehrer Date: Fri, 10 Jan 2025 11:48:43 +0100 Subject: [PATCH] tweak(Sales) legacy invoice xrechnung use contract billing address --- tests/tine20/Sales/CustomersTest.php | 4 +++- tine20/Sales/Controller/Invoice.php | 19 ++++++++++++++----- tine20/Sales/Model/Contract.php | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/tine20/Sales/CustomersTest.php b/tests/tine20/Sales/CustomersTest.php index 215b0001f1..b07402077f 100644 --- a/tests/tine20/Sales/CustomersTest.php +++ b/tests/tine20/Sales/CustomersTest.php @@ -4,7 +4,7 @@ * * @package Sales * @license http://www.gnu.org/licenses/agpl.html - * @copyright Copyright (c) 2013-2024 Metaways Infosystems GmbH (http://www.metaways.de) + * @copyright Copyright (c) 2013-2025 Metaways Infosystems GmbH (http://www.metaways.de) * @author Alexander Stintzing * */ @@ -321,6 +321,8 @@ public function testDeleteUsedBillingAddress() 'billing_address_id' => $customer['debitors'][0]['billing'][0]['id'], ))); + $this->assertInstanceOf(Sales_Model_Address::class, $contract->billing_address_id); + // if the property is set to null, no handling of this dependent records must be done $customer['debitors'][0]['billing'] = NULL; $customer = $this->_json->saveCustomer($customer); diff --git a/tine20/Sales/Controller/Invoice.php b/tine20/Sales/Controller/Invoice.php index 5a7e53007e..d117dc73a6 100644 --- a/tine20/Sales/Controller/Invoice.php +++ b/tine20/Sales/Controller/Invoice.php @@ -6,7 +6,7 @@ * @subpackage Controller * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3 * @author Alexander Stintzing - * @copyright Copyright (c) 2013-2018 Metaways Infosystems GmbH (http://www.metaways.de) + * @copyright Copyright (c) 2013-2025 Metaways Infosystems GmbH (http://www.metaways.de) * */ @@ -1512,10 +1512,18 @@ public function createXRechnungsAttachment(Sales_Model_Invoice $invoice): void } try { $customer = $this->_getCustomerFromInvoiceRelations($invoice) ?? throw new Tinebase_Exception_SystemGeneric('invoice does not have a customer'); - /** @var Sales_Model_Contract $contract */ - $contract = $invoice->relations->find('type', 'CONTRACT')?->related_record; Tinebase_Record_Expander::expandRecord($customer); - $debitor = $customer->{Sales_Model_Customer::FLD_DEBITORS}->getFirstRecord(); + if ($contract = $invoice->relations->find('type', 'CONTRACT')?->related_record) { + Tinebase_Record_Expander::expandRecord($contract); + } + $debitor = null; + /** @var Sales_Model_Contract $contract */ + if ($contract?->billing_address_id) { + $debitor = $customer->{Sales_Model_Customer::FLD_DEBITORS}->find(fn($deb) => $deb->{Sales_Model_Debitor::FLD_BILLING}->getById($contract->getIdFromProperty('billing_address_id')), null); + } + if (null === $debitor) { + $debitor = $customer->{Sales_Model_Customer::FLD_DEBITORS}->getFirstRecord(); + } // type (invoice_type) => REVERSAL => storno gibt verknüpfung @@ -1530,7 +1538,8 @@ public function createXRechnungsAttachment(Sales_Model_Invoice $invoice): void Sales_Model_Document_Invoice::FLD_CUSTOMER_ID => $customer, Sales_Model_Document_Invoice::FLD_CONTRACT_ID => $contract, Sales_Model_Document_Invoice::FLD_RECIPIENT_ID => new Sales_Model_Document_Address( - $debitor->{Sales_Model_Debitor::FLD_BILLING}->getFirstRecord()->toArray(), true + ($contract?->billing_address_id ?: + $debitor->{Sales_Model_Debitor::FLD_BILLING}->getFirstRecord())->toArray(), true ), // TODO FIXME do we have a FLD_CONTACT_ID? Sales_Model_Document_Invoice::FLD_DOCUMENT_LANGUAGE => 'de', diff --git a/tine20/Sales/Model/Contract.php b/tine20/Sales/Model/Contract.php index 03c88f1a28..c688092ca5 100644 --- a/tine20/Sales/Model/Contract.php +++ b/tine20/Sales/Model/Contract.php @@ -5,7 +5,7 @@ * @package Sales * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3 * @author Philipp Schüle - * @copyright Copyright (c) 2007-2024 Metaways Infosystems GmbH (http://www.metaways.de) + * @copyright Copyright (c) 2007-2025 Metaways Infosystems GmbH (http://www.metaways.de) */ /** @@ -99,6 +99,7 @@ class Sales_Model_Contract extends Tinebase_Record_Abstract self::JSON_EXPANDER => [ Tinebase_Record_Expander::EXPANDER_PROPERTIES => [ + 'billing_address_id' => [], 'products' => [ Tinebase_Record_Expander::EXPANDER_PROPERTIES => [ 'product_id' => [