id)) { dol_syslog("DeliveryInvoiceAddress: Object ist null oder hat keine ID - überspringe", LOG_DEBUG); return; } dol_syslog("DeliveryInvoiceAddress: START - Object class: ".get_class($object).", ID: ".$object->id, LOG_DEBUG); // Bestimme den Element-Typ basierend auf dem Objekt $elementType = isset($object->element) ? $object->element : ''; if (empty($elementType)) { dol_syslog("DeliveryInvoiceAddress: Kein Element-Typ gefunden - überspringe", LOG_DEBUG); return; } dol_syslog("DeliveryInvoiceAddress: Element-Typ: ".$elementType, LOG_DEBUG); // Initialisiere alle Felder $substitutionarray['billing_contact_firstname'] = ''; $substitutionarray['billing_contact_name'] = ''; $substitutionarray['billing_contact_address'] = ''; $substitutionarray['billing_contact_zip'] = ''; $substitutionarray['billing_contact_town'] = ''; $substitutionarray['billing_contact_country'] = ''; $substitutionarray['service_contact_firstname'] = ''; $substitutionarray['service_contact_name'] = ''; $substitutionarray['service_contact_address'] = ''; $substitutionarray['service_contact_zip'] = ''; $substitutionarray['service_contact_town'] = ''; $substitutionarray['service_contact_country'] = ''; $substitutionarray['delivery_contact_firstname'] = ''; $substitutionarray['delivery_contact_name'] = ''; $substitutionarray['delivery_contact_address'] = ''; $substitutionarray['delivery_contact_zip'] = ''; $substitutionarray['delivery_contact_town'] = ''; $substitutionarray['delivery_contact_country'] = ''; // Lade Kontakte direkt aus der DB - mit korrektem element-Typ! $sql = "SELECT ec.rowid, ec.fk_socpeople, tc.code"; $sql .= " FROM ".MAIN_DB_PREFIX."element_contact as ec"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_contact as tc ON ec.fk_c_type_contact = tc.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')"; dol_syslog("DeliveryInvoiceAddress: SQL: ".$sql, LOG_DEBUG); $resql = $db->query($sql); if (!$resql) { dol_syslog("DeliveryInvoiceAddress: SQL ERROR: ".$db->lasterror(), LOG_ERR); return; } $num = $db->num_rows($resql); dol_syslog("DeliveryInvoiceAddress: ".$num." Kontakte gefunden", LOG_DEBUG); if ($num == 0) { dol_syslog("DeliveryInvoiceAddress: Keine Kontakte gefunden - ENDE", LOG_DEBUG); return; } require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; while ($obj = $db->fetch_object($resql)) { dol_syslog("DeliveryInvoiceAddress: Verarbeite Kontakt - ID: ".$obj->fk_socpeople.", Code: ".$obj->code, LOG_DEBUG); $contactobj = new Contact($db); $result = $contactobj->fetch($obj->fk_socpeople); if ($result > 0) { $firstname = isset($contactobj->firstname) ? $contactobj->firstname : ''; $lastname = isset($contactobj->lastname) ? $contactobj->lastname : ''; $address = isset($contactobj->address) ? $contactobj->address : ''; $zip = isset($contactobj->zip) ? $contactobj->zip : ''; $town = isset($contactobj->town) ? $contactobj->town : ''; $country = isset($contactobj->country) ? $contactobj->country : ''; // Name-Logik: Vorname falls vorhanden, sonst Nachname $name = !empty($firstname) ? $firstname : $lastname; dol_syslog("DeliveryInvoiceAddress: Kontakt geladen - Vorname: ".$firstname.", Nachname: ".$lastname.", Code: ".$obj->code, LOG_DEBUG); // BILLING und CUSTOMER werden beide als Rechnungskontakt behandelt // BILLING hat Priorität, CUSTOMER nur wenn BILLING noch leer ist if ($obj->code == 'BILLING') { $substitutionarray['billing_contact_firstname'] = $firstname; $substitutionarray['billing_contact_name'] = $name; $substitutionarray['billing_contact_address'] = $address; $substitutionarray['billing_contact_zip'] = $zip; $substitutionarray['billing_contact_town'] = $town; $substitutionarray['billing_contact_country'] = $country; dol_syslog("DeliveryInvoiceAddress: BILLING gesetzt: ".$name." - ".$address, LOG_DEBUG); } elseif ($obj->code == 'CUSTOMER' && empty($substitutionarray['billing_contact_name'])) { // CUSTOMER als Fallback für billing_contact (bei Angeboten/Aufträgen) $substitutionarray['billing_contact_firstname'] = $firstname; $substitutionarray['billing_contact_name'] = $name; $substitutionarray['billing_contact_address'] = $address; $substitutionarray['billing_contact_zip'] = $zip; $substitutionarray['billing_contact_town'] = $town; $substitutionarray['billing_contact_country'] = $country; dol_syslog("DeliveryInvoiceAddress: CUSTOMER als BILLING gesetzt: ".$name." - ".$address, LOG_DEBUG); } elseif ($obj->code == 'SERVICE') { $substitutionarray['service_contact_firstname'] = $firstname; $substitutionarray['service_contact_name'] = $name; $substitutionarray['service_contact_address'] = $address; $substitutionarray['service_contact_zip'] = $zip; $substitutionarray['service_contact_town'] = $town; $substitutionarray['service_contact_country'] = $country; dol_syslog("DeliveryInvoiceAddress: SERVICE gesetzt: ".$name." - ".$address, LOG_DEBUG); } elseif ($obj->code == 'SHIPPING') { $substitutionarray['delivery_contact_firstname'] = $firstname; $substitutionarray['delivery_contact_name'] = $name; $substitutionarray['delivery_contact_address'] = $address; $substitutionarray['delivery_contact_zip'] = $zip; $substitutionarray['delivery_contact_town'] = $town; $substitutionarray['delivery_contact_country'] = $country; dol_syslog("DeliveryInvoiceAddress: SHIPPING gesetzt: ".$name." - ".$address, LOG_DEBUG); } } else { dol_syslog("DeliveryInvoiceAddress: FEHLER beim Laden von Kontakt ID ".$obj->fk_socpeople, LOG_ERR); } } $db->free($resql); dol_syslog("DeliveryInvoiceAddress: ENDE - Alle Substitutionen gesetzt", LOG_DEBUG); } ?>