Customer code check/generation

Discussion in 'Developers Forum' started by websnail, Jan 1, 2011.

  1. websnail New Member

    EDIT: Turns out this code is redundant as you can submit a customer record via API and it will auto-generate the Code for you.
    QED: customer "Code" is not a required variable for inserting a new customer


    Not sure if someone has already created something like this but I've just hacked together a function that generates a customer code, checks if it's already in use and then keeps trying to generate something usable before creating a random string.


    NB: Uses the creokashflow class to handle the XML interaction with the API
    PHP:

    /*
     * Returns a four letter + numeric postfix code that is not already in use
     * Will generate a random 4 letter prefix if the firstname/lastname code is already too common 
     *  to reduce load from repeated queries.
     * Usage $code = get_customer_code('lastfoo', 'firstfoo');
     */
        
    function get_customer_code($last$first) {
            
            
    $initialCode substr(strtoupper($first.$last), 4).'01';
            
    $code $initialCode;
            
    $post 1;
            
            while(
    $testCustomerCode $kf->GetCustomer($code) AND count($testCustomerCode) >= 1) {
                
                
    // Avoid getting into a complete loop over a first name
                
    if($post <= 10) {
                    
    $pre substr(strtoupper($first.$last), 4);
                }
                
    // First name too common so we use first initial + lastname 
                
    elseif($post <= 20) {
                    
    $pre substr(strtoupper($first), 1) . substr(strtoupper($last), 3);
                }
                
    // Still too common, use a random string instead
                
    else {
                    
    $pre strtoupper(generate_random_string(4));
                }

                
    $post++;
                
    $strPost = ($post <= 9) ? '0'.$post $post;
                
    $code $pre.$strPost;
            }
            
            
    // Should drop out of the loop when it finds a free code
            
    return $code;
        }

    function 
    generate_random_string($count=4) {
        
    //To Pull X Unique Random Values Out Of AlphaNumeric

        //removed number 0, capital o, number 1 and small L
        //Total: keys = 32, elements = 33
        
    $characters = array(
            
    "A","B","C","D","E","F","G","H","J","K","L","M",
            
    "N","P","Q","R","S","T","U","V","W","X","Y","Z",
            
    "1","2","3","4","5","6","7","8","9");

        
    //make an "empty container" or array for our keys
        
    $keys = array();

        
    //first count of $keys is empty so "1", remaining count is 1-6 = total 7 times
        
    while(count($keys) < $count) {
            
    //"0" because we use this to FIND ARRAY KEYS which has a 0 value
            //"-1" because were only concerned of number of keys which is 32 not 33
            //count($characters) = 33
            
    $x mt_rand(0count($characters)-1);
            if(!
    in_array($x$keys)) {
                
    $keys[] = $x;
            }
        }

        foreach(
    $keys as $key){
               
    $random_chars .= $characters[$key];
        }
        return 
    $random_chars;
    }
  2. DuaneJackson Administrator

    Doesn't KashFlow already do this for you if you insertCustomer with a blank code?
  3. websnail New Member

    No idea... Documentation doesn't specify required fields or those auto generated... From what I'd gleaned elsewhere it seemed you needed to generate and insert it yourself.
  4. websnail New Member

    Just on this topic of Documentation... the API info could really use a bit more intel.

    There's been more than one request for the documentation on required variables to be updated and yet it's never been heeded much less answered. It does sort of make the various questions about whether the forums are worth keeping somewhat moot if there are (some) threads on development issues that aren't answered or acted on.

    Intended as constructive crit' I hasten to add, but I suspect developers are less inclined to share hard won intel like this (on here or elsewhere) because they had to work for it, and as a developer you're unlikely to want to give that away to potential competitors.

Share This Page