hesabixWCPlugin/admin/services/hesabixCustomerService.php

236 lines
12 KiB
PHP
Raw Normal View History

2025-08-05 17:24:52 +03:30
<?php
/**
* Hesabix Customer Service
*
* @package Hesabix
* @author Mohammad Rezai
* @author URI https://pirouz.xyz
* @since 1.0.0
*/
class HesabixCustomerService
{
public static $countries;
public static $states;
public static function mapCustomer($code, $id_customer, $type = 'first', $id_order = ''): array
{
self::getCountriesAndStates();
$customer = new WC_Customer($id_customer);
$order = new WC_Order($id_order);
$firstName = $customer->get_first_name() ?: $customer->get_billing_first_name();
$lastName = $customer->get_last_name() ?: $customer->get_billing_last_name();
$name = trim($firstName . ' ' . $lastName);
if (empty($name)) $name = __('Not Defined', 'hesabix');
$checkout_fields = HesabixCustomerService::getAdditionalCheckoutFileds($id_order);
$NationalCode = $checkout_fields['NationalCode'] ?? '';
$EconomicCode = $checkout_fields['EconomicCode'] ?? '';
$RegistrationNumber = $checkout_fields['RegistrationNumber'] ?? '';
$Website = $checkout_fields['Website'] ?? '';
$nodeFamily = get_option('hesabix_contact_automatic_save_node_family') === 'yes'
? __('Persons:', 'hesabix') . get_option('hesabix_contact_node_family') : null;
if ($type === 'shipping') {
$country_code = $order->get_shipping_country();
$state_code = $order->get_shipping_state();
$fullAddress = $order->get_shipping_address_1() . '-' . $order->get_shipping_address_2();
$city = $order->get_shipping_city();
$postalCode = $order->get_shipping_postcode();
} else {
$country_code = $order->get_billing_country();
$state_code = $order->get_billing_state();
$fullAddress = $order->get_billing_address_1() . '-' . $order->get_billing_address_2();
$city = $order->get_billing_city();
$postalCode = $order->get_billing_postcode();
}
if (strlen($fullAddress) < 5) {
$fullAddress = $customer->get_billing_address_1() . '-' . $customer->get_billing_address_2();
}
if (empty($postalCode)) {
$postalCode = $customer->get_billing_postcode();
}
if (preg_match('/^\d+$/', $city)) {
$func = new Hesabix_Admin_Functions();
$city = $func->convertCityCodeToName($city);
}
$country_name = self::$countries[$country_code] ?? '';
$state_name = self::$states[$country_code][$state_code] ?? '';
$hesabixCustomer = [
'nikename' => $name,
'name' => Hesabix_Validation::contactFirstNameValidation($firstName) . Hesabix_Validation::contactLastNameValidation($lastName),
'des' => __('Customer ID in OnlineStore: ', 'hesabix') . $id_customer,
'tel' => (strlen(preg_replace('/\D/', '', $customer->get_billing_phone())) === 11)
? Hesabix_Validation::contactPhoneValidation($customer->get_billing_phone())
: '',
'mobile' => '',
'mobile2' => '',
'address' => Hesabix_Validation::contactAddressValidation($fullAddress),
'company' => '',
'shenasemeli' => $NationalCode,
'codeeghtesadi'=> $EconomicCode,
'sabt' => $RegistrationNumber,
'keshvar' => Hesabix_Validation::contactCountryValidation($country_name),
'ostan' => Hesabix_Validation::contactStateValidation($state_name),
'shahr' => Hesabix_Validation::contactCityValidation($city),
'postalcode' => Hesabix_Validation::contactPostalCodeValidation($postalCode),
'email' => Hesabix_Validation::contactEmailValidation($customer->get_email()),
'website' => $Website,
'fax' => '',
'code' => (int) $code ?: 0,
'types' => [
[ 'label' => __('Customer', 'hesabix'), 'code' => 'customer', 'checked' => true ],
[ 'label' => __('Marketer', 'hesabix'), 'code' => 'marketer', 'checked' => false ],
[ 'label' => __('Employee', 'hesabix'), 'code' => 'emplyee', 'checked' => false ],
[ 'label' => __('Supplier', 'hesabix'),'code' => 'supplier', 'checked' => false ],
[ 'label' => __('Colleague', 'hesabix'), 'code' => 'colleague', 'checked' => false ],
[ 'label' => __('Salesman', 'hesabix'), 'code' => 'salesman', 'checked' => false ],
],
'accounts' => [],
'prelabel' => null,
'speedAccess' => false,
'tags' => json_encode([
'id_customer' => $id_customer
])
];
return self::correctCustomerData($hesabixCustomer);
}
public static function mapGuestCustomer($code, $id_order): array
{
$order = new WC_Order($id_order);
$name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
if (empty($order->get_billing_first_name()) && empty($order->get_billing_last_name())) {
$name = __('Guest Customer', 'hesabix');
}
$nodeFamily = get_option('hesabix_contact_automatic_save_node_family') == 'yes'? __('Persons:', 'hesabix') . get_option('hesabix_contact_node_family') :null;
$checkout_fields = hesabixCustomerService::getAdditionalCheckoutFileds($id_order);
$NationalCode = $checkout_fields['NationalCode'];
$EconomicCode = $checkout_fields['EconomicCode'];
$RegistrationNumber = $checkout_fields['RegistrationNumber'];
$Website = $checkout_fields['Website'];
WC()->countries->countries[ $order->shipping_country ];
$country_name = WC()->countries->countries[ $order->get_billing_country() ];
$states = WC()->countries->get_states( $order->get_billing_country() );
$state_name = $states[ $order->get_billing_state() ];
if(!$state_name) $state_name = WC()->countries->states[$order->billing_country][$order->billing_state];
if(!$state_name) $state_name = $order->get_billing_state();
$city = $order->get_billing_city();
if(preg_match('/^[0-9]+$/', $city)) {
$func = new Hesabix_Admin_Functions();
$city = $func->convertCityCodeToName($order->get_billing_city());
}
$fullAddress = $order->get_billing_address_1() . '-' . $order->get_billing_address_2();
$hesabixCustomer = array(
'Code' => $code,
'Name' => $name,
'FirstName' => Hesabix_Validation::contactFirstNameValidation($order->get_billing_first_name()),
'LastName' => Hesabix_Validation::contactLastNameValidation($order->get_billing_last_name()),
'ContactType' => 1,
'NationalCode' => $NationalCode,
'EconomicCode' => $EconomicCode,
'RegistrationNumber' => $RegistrationNumber,
'Website' => $Website,
'NodeFamily' => $nodeFamily,
'Address' => Hesabix_Validation::contactAddressValidation($fullAddress),
'City' => Hesabix_Validation::contactCityValidation($city),
'State' => Hesabix_Validation::contactStateValidation($state_name),
'Country' => Hesabix_Validation::contactCountryValidation($country_name),
'PostalCode' => Hesabix_Validation::contactPostalCodeValidation($order->get_billing_postcode()),
'Phone' => Hesabix_Validation::contactPhoneValidation($order->get_billing_phone()),
'Email' => Hesabix_Validation::contactEmailValidation($order->get_billing_email()),
'Tag' => json_encode(array('id_customer' => 0)),
'Note' => __('Customer registered as a GuestCustomer.', 'hesabix'),
);
return self::correctCustomerData($hesabixCustomer);
}
private static function getMobileFromPhone($phone) {
if(preg_match("/^09\d{9}$/", $phone))
return $phone;
else if(preg_match("/^9\d{9}$/", $phone))
return '0' . $phone;
else if(preg_match("/^989\d{9}$/", $phone))
return str_replace('98', '0' ,$phone);
else return '';
}
private static function correctCustomerData($hesabixCustomer) {
if($hesabixCustomer["Phone"] == '')
unset($hesabixCustomer["Phone"]);
else {
$mobile = self::getMobileFromPhone($hesabixCustomer["Phone"]);
if($mobile) $hesabixCustomer["Mobile"] = $mobile;
}
if($hesabixCustomer["Email"] == '') unset($hesabixCustomer["Email"]);
if($hesabixCustomer["Address"] == '') unset($hesabixCustomer["Address"]);
if($hesabixCustomer["PostalCode"] == '') unset($hesabixCustomer["PostalCode"]);
if($hesabixCustomer["City"] == '') unset($hesabixCustomer["City"]);
if($hesabixCustomer["State"] == '') unset($hesabixCustomer["State"]);
if($hesabixCustomer["Country"] == '') unset($hesabixCustomer["Country"]);
return $hesabixCustomer;
}
private static function getCountriesAndStates()
{
if (!isset(self::$countries)) {
$countries_obj = new WC_Countries();
self::$countries = $countries_obj->get_countries();
self::$states = $countries_obj->get_states();
}
}
private static function getAdditionalCheckoutFileds($id_order) {
$NationalCode = '_billing_hesabix_nationalcode';
$EconomicCode = '_billing_hesabix_economiccode';
$RegistrationNumber = '_billing_hesabix_registerationnumber';
$Website = '_billing_hesabix_website';
$NationalCode_isActive = get_option('hesabix_contact_NationalCode_checkbox_hesabix');
$EconomicCode_isActive = get_option('hesabix_contact_EconomicCode_checkbox_hesabix');
$RegistrationNumber_isActive = get_option('hesabix_contact_RegistrationNumber_checkbox_hesabix');
$Website_isActive = get_option('hesabix_contact_Website_checkbox_hesabix');
$add_additional_fileds = get_option('hesabix_contact_add_additional_checkout_fields_hesabix');
$fields = array();
if($add_additional_fileds == '1') {
$fields['NationalCode'] = get_post_meta( $id_order, $NationalCode, true) ?? null;
$fields['EconomicCode'] = get_post_meta( $id_order, $EconomicCode, true) ?? null;
$fields['RegistrationNumber'] = get_post_meta( $id_order, $RegistrationNumber, true) ?? null;
$fields['Website'] = get_post_meta( $id_order, $Website, true) ?? null;
} elseif($add_additional_fileds == '2') {
$NationalCode = get_option('hesabix_contact_NationalCode_text_hesabix');
$EconomicCode = get_option('hesabix_contact_EconomicCode_text_hesabix');
$RegistrationNumber = get_option('hesabix_contact_RegistrationNumber_text_hesabix');
$Website = get_option('hesabix_contact_Website_text_hesabix');
if($NationalCode_isActive == 'yes' && $NationalCode)
$fields['NationalCode'] = get_post_meta( $id_order, $NationalCode, true) ?? null;
if($EconomicCode_isActive == 'yes' && $EconomicCode)
$fields['EconomicCode'] = get_post_meta( $id_order, $EconomicCode, true) ?? null;
if($RegistrationNumber_isActive == 'yes' && $RegistrationNumber)
$fields['RegistrationNumber'] = get_post_meta( $id_order, $RegistrationNumber, true) ?? null;
if($Website_isActive == 'yes' && $Website)
$fields['Website'] = get_post_meta( $id_order, $Website, true) ?? null;
}
return $fields;
}
}