id)) { return; } // Element-Typ bestimmen (facture, propal, commande) $elementType = $object->element ?? ''; if (empty($elementType)) { return; } // Platzhalter-Prefixe je Kontakttyp $codeToPrefix = array( 'BILLING' => 'billing_contact', 'CUSTOMER' => 'billing_contact', 'SERVICE' => 'service_contact', 'SHIPPING' => 'delivery_contact', ); // Felder pro Kontakt $fields = array('firstname', 'name', 'fullname', 'address', 'zip', 'town', 'country'); // Alle Platzhalter leer initialisieren (ODT + E-Mail-Format) foreach (array('billing_contact', 'service_contact', 'delivery_contact') as $prefix) { foreach ($fields as $field) { $substitutionarray[$prefix.'_'.$field] = ''; $substitutionarray['__'.strtoupper($prefix.'_'.$field).'__'] = ''; } } $substitutionarray['billing_or_thirdparty'] = ''; $substitutionarray['__BILLING_OR_THIRDPARTY__'] = ''; $substitutionarray['billing_or_thirdparty_firstname'] = ''; $substitutionarray['__BILLING_OR_THIRDPARTY_FIRSTNAME__'] = ''; $substitutionarray['billing_or_thirdparty_name'] = ''; $substitutionarray['__BILLING_OR_THIRDPARTY_NAME__'] = ''; // Kontaktdaten per JOIN direkt laden $sql = "SELECT tc.code, sp.rowid, sp.firstname, sp.lastname, sp.address, sp.zip, sp.town,"; $sql .= " co.label as country"; $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec"; $sql .= " JOIN ".MAIN_DB_PREFIX."c_type_contact as tc ON ec.fk_c_type_contact = tc.rowid"; $sql .= " JOIN ".MAIN_DB_PREFIX."socpeople as sp ON ec.fk_socpeople = sp.rowid"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON sp.fk_pays = co.rowid"; $sql .= " WHERE ec.element_id = ".((int) $object->id); $sql .= " AND tc.element = '".$db->escape($elementType)."'"; $sql .= " AND tc.code IN ('BILLING', 'SERVICE', 'SHIPPING', 'CUSTOMER')"; $resql = $db->query($sql); if (!$resql) { dol_syslog("DeliveryInvoiceAddress: SQL FEHLER: ".$db->lasterror(), LOG_ERR); return; } while ($obj = $db->fetch_object($resql)) { $prefix = $codeToPrefix[$obj->code] ?? null; if (!$prefix) { continue; } // CUSTOMER nur als Fallback wenn BILLING noch leer if ($obj->code == 'CUSTOMER' && !empty($substitutionarray['billing_contact_name'])) { continue; } $firstname = $obj->firstname ?? ''; $lastname = $obj->lastname ?? ''; // Fullname: Vorname + Nachname, Reihenfolge je nach Dolibarr-Einstellung $fullname = trim($firstname.' '.$lastname); if (getDolGlobalString('MAIN_FIRSTNAME_NAME_POSITION')) { $fullname = trim($lastname.' '.$firstname); } $data = array( 'firstname' => $firstname, 'name' => $lastname, 'fullname' => $fullname, 'address' => $obj->address ?? '', 'zip' => $obj->zip ?? '', 'town' => $obj->town ?? '', 'country' => $obj->country ?? '', ); // ODT-Format + E-Mail-Format setzen foreach ($data as $field => $value) { $substitutionarray[$prefix.'_'.$field] = $value; $substitutionarray['__'.strtoupper($prefix.'_'.$field).'__'] = $value; } } $db->free($resql); // Fallback-Platzhalter: Rechnungskontakt oder Kundenname $hasContact = !empty($substitutionarray['billing_contact_name']); if ($hasContact) { $substitutionarray['billing_or_thirdparty'] = $substitutionarray['billing_contact_fullname']; $substitutionarray['billing_or_thirdparty_firstname'] = $substitutionarray['billing_contact_firstname']; $substitutionarray['billing_or_thirdparty_name'] = $substitutionarray['billing_contact_name']; } else { // Fallback auf Kundenname (thirdparty) $thirdpartyName = ''; if (!empty($object->thirdparty) && is_object($object->thirdparty)) { $thirdpartyName = $object->thirdparty->name ?? ''; } elseif (!empty($object->socid)) { $sqltp = "SELECT nom as name FROM ".MAIN_DB_PREFIX."societe WHERE rowid = ".((int) $object->socid); $restp = $db->query($sqltp); if ($restp && $db->num_rows($restp) > 0) { $objtp = $db->fetch_object($restp); $thirdpartyName = $objtp->name ?? ''; } } $substitutionarray['billing_or_thirdparty'] = $thirdpartyName; $substitutionarray['billing_or_thirdparty_firstname'] = ''; $substitutionarray['billing_or_thirdparty_name'] = $thirdpartyName; } // E-Mail-Format (__KEY__) spiegeln $substitutionarray['__BILLING_OR_THIRDPARTY__'] = $substitutionarray['billing_or_thirdparty']; $substitutionarray['__BILLING_OR_THIRDPARTY_FIRSTNAME__'] = $substitutionarray['billing_or_thirdparty_firstname']; $substitutionarray['__BILLING_OR_THIRDPARTY_NAME__'] = $substitutionarray['billing_or_thirdparty_name']; }