Created
November 1, 2012 13:15
-
-
Save andrewsardone/3993575 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createNewNutshellLead( $api ) { | |
$ns_contact_id = $this->setNutshellContactAndReturnID( $api ); | |
$products = array(); | |
$quote_price_ex_vat = ( $this->price + $this->getAdditionalProductsTotalPrice() ); | |
$quote_product = array( | |
'qty'=>1, | |
'item_price'=> $quote_price_ex_vat, | |
'total_price'=>$quote_price_ex_vat, | |
'detail'=> ucwords( str_replace( '_', ' ' , $this->door_type ) ) . ( $this->layout ? ' - ' .$this->layout : '' ) | |
); | |
array_unshift( $products, $quote_product ); | |
$lead = array( | |
'confidence' => '10', | |
'market' => array ( 'id' => '5' ), | |
'isPending' => true, | |
'products' => $this->getNutshellProductsArrayForLead( $products, $api ), | |
'sources' => array( array( 'id'=> NUTSHELL_SOURCE_ID ) ), | |
'customFields' => array( 'Quote #'=>$this->id ), | |
'contacts' => array( array( 'id'=>$ns_contact_id ) ) | |
); | |
$result = $api->newLead( array( 'lead' => $lead ) ); | |
return $this->uploadQuotePdfToNutshell( $api, $result->id, $result->rev ); | |
} | |
function getNutshellProductsArrayForLead( $products_array, $api ) { | |
$api_products_array = array(); | |
foreach ( $products_array as $product ) { | |
$results = $api->searchProducts( $product['detail'] ) ; | |
foreach ( $results as $stub ) { | |
if ( $stub->name == $product['detail'] ) { | |
$api_products_array[$stub->id] = array( | |
'id'=>$stub->id, | |
'quantity'=>$product['qty'], | |
'currency_shortname'=>'GBP', | |
'price'=> (int) ( $product['item_price'] * ( 100+VAT_RATE ) / 100 ) | |
); | |
continue 2; | |
} | |
} | |
$stub = $api->newProduct( array( | |
'product' => array( | |
'name'=>$product['detail'], | |
'type'=>0, | |
'prices'=>array( | |
array( | |
'marketId'=>NUTSHELL_MARKET_ID, | |
'amount'=>(int)( $product['item_price'] * ( 100+VAT_RATE ) / 100 ) | |
) | |
) | |
) | |
) | |
); | |
$api_products_array[$stub->id] = array( | |
'id'=>$stub->id, | |
'quantity'=>$product['qty'], | |
'currency_shortname'=>'GBP', | |
'price'=> array( | |
'amount'=>(int)( $product['item_price'] * ( 100+VAT_RATE ) / 100 ), | |
'currency'=> 'USD' | |
) | |
); | |
} | |
return $api_products_array; | |
} | |
function setNutshellContactAndReturnID( $api ) { | |
$phone_numbers = array(); | |
$results = $api->searchContacts( $this->username ); | |
foreach ( $results as $stub ) { | |
$contact = $api->getContact( $stub->id ); | |
if ( | |
in_array( $this->username, (array) $contact->email ) && | |
$this->forename == $contact->name->givenName && | |
$this->surname == $contact->name->familyName | |
) { | |
foreach ( (array) $contact->phone as $object ) $phone_numbers[] = (array) $object; | |
$phone_numbers['daytime'] = $this->daytime_tel ; | |
if ( ! empty ( $this->secondary_tel ) ) $phone_numbers['secondary'] = $this->secondary_tel ; | |
$api->editContact( $stub->id, 'REV_IGNORE', array( 'phone' => array_unique( $phone_numbers ) ) ); | |
return $stub->id; | |
} | |
} | |
$phone_numbers['daytime'] = $this->daytime_tel ; | |
if ( ! empty ( $this->secondary_tel ) ) $phone_numbers['secondary'] = $this->secondary_tel ; | |
$params = array( 'contact' => array( 'name' => $this->forename . ' ' . $this->surname, 'email' => array( $this->username ), 'phone' => array_unique( $phone_numbers ) ) ); | |
$new_contact = $api->newContact( $params ); | |
return $new_contact->id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment