function cw_user_update($userinfo, $customer_id, $by_customer_id)
{
    global $tables, $addons;
    foreach (array('email', 'status', 'membership_id', 'language', 'change_password') as $fld) {
        if (isset($userinfo[$fld])) {
            $customer[$fld] = $userinfo[$fld];
        }
    }
    if (AREA_TYPE == 'A' && $userinfo['usertype']) {
        $customer['usertype'] = $userinfo['usertype'];
    }
    if (AREA_TYPE == 'A' && $userinfo['customer_id']) {
        $customer['customer_id'] = $userinfo['customer_id'];
    }
    $current = cw_user_get_info($customer_id, 0);
    //allow password update only by the profile owner or by admin
    if (isset($userinfo['password'])) {
        if (!empty($userinfo['password']) && ($customer_id == $by_customer_id || AREA_TYPE == 'A')) {
            $customer['password'] = cw_call('cw_user_get_hashed_password', array($userinfo['password']));
        }
    }
    cw_event('on_user_update', array($customer_id, $by_customer_id, $customer, $userinfo));
    cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
    $additional_info = $userinfo['additional_info'];
    $customer_info_fields = array('ssn', 'tax_number', 'birthday', 'birthday_place', 'sex', 'married', 'nationality', 'company', 'employees', 'foundation', 'foundation_place', 'company_type', 'company_id', 'can_change_company_id', 'contact_date', 'tax_id', 'payment_id', 'payment_note', 'tax_exempt', 'separate_invoices', 'shipping_operated', 'shipment_paid', 'shipping_company_to_carrier_id', 'shipping_company_from_carrier_id', 'cod_delivery_type_id', 'leaving_type', 'department_id', 'division_id', 'doc_prefix', 'order_entering_format', 'status_note');
    if (!cw_query_first_cell("select count(*) from {$tables['customers_customer_info']} where customer_id='{$customer_id}'")) {
        cw_array2insert('customers_customer_info', array('customer_id' => $customer_id));
    }
    cw_array2update('customers_customer_info', $additional_info, "customer_id='{$customer_id}'", $customer_info_fields);
    /*
          db_query("update $tables[customers_addresses] set main=0, current=0 where customer_id = '$customer_id'");
        foreach(array('main_address', 'current_address') as $addr)  {
            if (!$userinfo[$addr]) continue;
    
            $userinfo[$addr]['main'] = $addr == 'main_address';
            $userinfo[$addr]['current'] = $addr == 'current_address';
    
            $address_id = $userinfo[$addr]['address_id'];
            if (!$address_id) $address_id = cw_array2insert('customers_addresses', array('customer_id' => $customer_id, 'main' => $userinfo[$addr]['main'], 'current' => $userinfo[$addr]['current']));
    
            cw_user_update_address($customer_id, $address_id, $userinfo[$addr]);
        }
    */
    foreach (cw_user_address_array($userinfo['addresses']) as $address_id => $address) {
        cw_user_update_address($customer_id, $address['address_id'], $address);
    }
    cw_user_check_addresses($customer_id);
    $relations = $userinfo['relations'];
    $relations_info = array('salesman_customer_id' => $relations['salesman_customer_id'], 'employee_customer_id' => $relations['employee_customer_id'], 'warehouse_customer_id' => $relations['warehouse_customer_id']);
    if (!cw_query_first_cell("select count(*) from {$tables['customers_relations']} where customer_id='{$customer_id}'")) {
        db_query("insert into {$tables['customers_relations']}(customer_id) values('{$customer_id}')");
    }
    cw_array2update('customers_relations', $relations_info, "customer_id='{$customer_id}'");
    if ($userinfo['chamber_certificate_uploaded']['file_path']) {
        db_query("delete from {$tables['customers_chamber_certificates']} where customer_id='{$customer_id}'");
        cw_file_area_save('customers_chamber_certificates', $customer_id, $userinfo['chamber_certificate_uploaded']);
    }
    if (is_array($userinfo['custom_fields'])) {
        foreach ($userinfo['custom_fields'] as $field_id => $value) {
            db_query("delete from {$tables['register_fields_values']} where customer_id='{$customer_id}' AND field_id='{$field_id}'");
            cw_array2insert('register_fields_values', array('field_id' => $field_id, 'value' => $value, 'customer_id' => $customer_id));
        }
    }
    $customer_system_info = cw_query_first("select * from {$tables['customers_system_info']} where customer_id='{$customer_id}'");
    $customer_system_info['customer_id'] = $customer_id;
    if (!$customer_system_info['creation_customer_id']) {
        $customer_system_info['creation_customer_id'] = $by_customer_id;
        $customer_system_info['creation_date'] = cw_core_get_time();
    }
    $customer_system_info['modification_customer_id'] = $by_customer_id;
    $customer_system_info['modification_date'] = cw_core_get_time();
    cw_array2insert('customers_system_info', $customer_system_info, true);
    $salesman_info = cw_query_first("select * from {$tables['customers_salesman_info']} where customer_id = '{$customer_id}'");
    $salesman_info['parent_customer_id'] = $userinfo['salesman_info']['parent_customer_id'];
    cw_array2insert('customers_salesman_info', $salesman_info, true);
}
if ($action == 'load') {
    if ($fill_error) {
        $address = $prefilled_address;
    } elseif (empty($address)) {
        $address = cw_user_get_address($user, $address_id);
    }
    $smarty->assign('address', $address);
    $smarty->assign('address_id', $address_id);
    //    $smarty->assign('is_main', $is_main);
    $smarty->assign('name_prefix', 'update_fields[address][' . (in_array($address_type, array('main', 'current'), true) ? $address_type : $address_id) . ']');
    cw_add_ajax_block(array('id' => in_array($address_type, array('main', 'current'), true) ? $address_type . '_address' : 'address', 'action' => 'update', 'template' => 'main/users/sections/address_modify.tpl'), $address_type . '_address');
}
if ($action == 'set_main' || $action == 'set_current') {
    $field = $action == 'set_main' ? 'main' : 'current';
    Customer\Address\setAddressType($user, $field, $address_id);
    cw_user_check_addresses($user);
    // For correct shipping cost calculate after address changing
    cw_load('user');
    $user_address =& cw_session_register('user_address');
    $user_address = array();
    global $userinfo;
    $userinfo['current_address'] = cw_user_get_address_by_type('current');
    cw_add_top_message('Address has been updated');
}
$smarty->assign('address_type', $address_type);
$smarty->assign('user', $user);
if (defined('IS_AJAX')) {
    if ($user) {
        $addresses = cw_user_get_addresses(intval($user));
    }
    $smarty->assign('addresses', $addresses);