= 400) { foreach ($ret['data']['validation_errors'] as $item) { $_SESSION['warning'][] = $item['message']; } return null; } $ret = $ret['data']; if (count($ret) > 0) { $name = strtolower($name); foreach ($ret as $item) { $cust = $item['customer']; if ($name == strtolower($cust['lastname']) || $name == strtolower($cust['firstname']) || $name == strtolower($cust['firstname'] .' '. $cust['lastname']) || $name == strtolower($cust['lastname'] .' '. $cust['firstname']) ) { return $cust; } } } return null; /* $kundenliste = json_decode(file_get_contents('../kunden.json'), TRUE); foreach ($kundenliste as $k) { foreach ($k['kontakt'] as $kon) { if ($kon['wert'] == $number) { return $k; } } } */ } function kunde_hat_email($customerno) { $ret = api_call('GET', 'customers/'.(int) $customerno); if ($ret['status_code'] >= 400) { // Fehler. Belästige den Kunden nicht damit return true; } $ret = $ret['data']; if (isset($ret['email']) && $ret['email']) { return true; } foreach ($ret['contacts'] as $c) { if (isset($c['email']) && $c['email']) { return true; } } return false; } function update_kunde($customerno, $daten) { $ret = api_call('GET', 'customers/'.(int) $customerno); if ($ret['status_code'] >= 400) { // Fehler. Erstelle Neukunde erstelle_kunde($daten); return; } $customer = $ret['data']; foreach ($daten as $field => $value) { if ($value) { $customer[$field] = $value; } } $filename = '../data/'.date('Y').'/'.time().'-kunde-'.$customerno.'.json'; @file_put_contents($filename, json_encode($customer)); $ret = api_call('PUT', 'customers/'.(int) $customerno, $customer); if ($ret['status_code'] >= 400) { foreach ($ret['data']['validation_errors'] as $item) { $_SESSION['warnings'][] = $item['key'].': '.$item['message']; } return null; } $ret = $ret['data']; if (isset($ret['id'])) { return $ret['id']; } else { return null; } } function erstelle_kunde($daten) { $json = [ "firstname" => $daten['fname'] ?: null, "lastname" => $daten['lname'], "phone" => format_number_api($daten['phone']), "email" => $daten['email'] ?: null, "street" => $daten['address'] ?: null, "zip" => $daten['zip'] ?: null, "city" => $daten['city'] ?: null, "contacts" => []]; $filename = '../data/'.date('Y').'/'.time().'-neukunde.json'; @file_put_contents($filename, json_encode($json)); $ret = api_call('POST', 'customers', $json); if ($ret['status_code'] >= 400) { foreach ($ret['data']['validation_errors'] as $item) { $_SESSION['warnings'][] = $item['key'].': '.$item['message']; } return null; } $ret = $ret['data']; if (isset($ret['id'])) { return $ret['id']; } else { return null; } } require_once('vendor/autoload.php'); function format_number_national($number) { $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $phoneNumber = $phoneNumberUtil->parse($number, 'DE'); } catch (Exception $e) { return NULL; } if ($phoneNumberUtil->isValidNumber($phoneNumber)) { return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::NATIONAL); } return NULL; } function format_number($number) { $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $phoneNumber = $phoneNumberUtil->parse($number, 'DE'); } catch (Exception $e) { return NULL; } if ($phoneNumberUtil->isValidNumber($phoneNumber)) { return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::INTERNATIONAL); } return NULL; }