Snippets

Levelset Engineering Nutshell Integration

Created by Hisham Younes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* This class is dedicated for Nutshell integration such as creating Leads
*/
abstract class NutshellManager
{
	static $doctypes = array('mechanics lien', 'Lien / Bond Claim', 'stop notice', 'notice of intent to lien');
	static $sources = array('mechanics lien', 'Bond Claim', 'stop notice', 'notice of intent to lien');
	static $tags = array('Collections', 'Collections', 'Collections', 'Single Document');
	static $products = array('Collect / Foreclose', 'Collect / Foreclose', 'Collect / Foreclose', 'Mechanics Lien');	
	static $nutshell;
	
	/*
	* Creates a new nutshell account for a user. If an account is already created, the existing account
	* id is returned.
	* param int $user_id The user to create the account for
	* return int The id of the nutshell account
	*/
	private static function CreateNutshellAccount(&$user)
	{
		if(!$user->nutshell_account_id)
		{
			$account = array();
			$account['name'] = $user->company_name;
			
			//for testing
			$account['name'] = $account['name'];
			
			if(!empty($user->user_phone))
			{
				$account['phone'] = array($user->user_phone);
			}
			$account['address'] = array(
				array(
					'address_1' => $user->company_address,
					'city' => $user->company_city,
					'state'=> $user->company_state,
					'postalCode' => $user->company_zip,
					'country' => 'US'															
				)
			);
			$accountObj = self::$nutshell->newAccount(array('account' => $account));		
			$user->nutshell_account_id = $accountObj->id;
			$user->Update();
		}
		else
		{
			$account = self::$nutshell->getAccount($user->nutshell_account_id);
			if(property_exists($account, 'deletedTime'))
			{
				$user->nutshell_account_id = null;
				$user->Update();
				$user->nutshell_account_id = self::CreateNutshellAccount($user);
			}
		}
		return $user->nutshell_account_id;
	}
	
	/*
	* Creates a new nutshell contact if necessary for an existing nutshell account.
	* param int $user_id The user id of the user to get a
	* return int The id of the nutshell contact.
	*/
	private static function CreateNutshellContact(&$user)
	{
		$contacts = self::$nutshell->findContacts(array('query' => array('accountId' => (int)$user->nutshell_account_id)));
		if(count($contacts) > 0)
		{
			$contactObj = $contacts[0];
		}
		else
		{
			$contact = array();
			$contact['name'] = $user->user_first_name . ' ' . $user->user_last_name;
			//for testing
			$contact['name'] = $contact['name'];
			if($user->user_phone != '')
			{
				$contact['phone'] = array($user->user_phone);
			}
			$contact['email'] = array($user->user_email);
			$contact['address'] = array(
				array(
					'address_1' => $user->company_address,
					'city' => $user->company_city,
					'state' => $user->company_state,
					'postalCode' => $user->company_zip,
					'country' => 'US'															
				)
			);
			$contact['accounts'] = array(array('id' => $user->nutshell_account_id));
			$contactObj = self::$nutshell->newContact(array('contact' => $contact));
		}
		return $contactObj->id;
	}
	
	/*
	* Creates a new lead for an incomplete order
	* param int $order_id the order Id for which to create the new lead
	* return bool Whether a lead has been created
	*/
	public static function CreateNewLeadForIncompleteOrder($incomplete_order_id)
	{
		try 
		{
			$incomplete_order = new IncompleteOrders($incomplete_order_id);
			if($incomplete_order->Load() && $incomplete_order->page_4_session)
			{
				$page_4_session = unserialize($incomplete_order->page_4_session);
				$page_4_request = unserialize($incomplete_order->page_4_request);
				
				$page_4_session = is_array($page_4_session) ? $page_4_session : array();
				$page_4_request = is_array($page_4_request) ? $page_4_request : array();
				
				$arr = array_merge($page_4_session, $page_4_request);
				if(array_key_exists('product_id', $arr) && array_key_exists('state_id', $arr))
				{
					$user_id = '';
					if($incomplete_order->user_id != '')
					{
						$user_id = $incomplete_order->user_id;
					}			
					else if(array_key_exists('user_id', $arr) == true)
					{
						$user_id = $arr['user_id'];
					}
					if($user_id != '')
					{
						$user = new User($user_id);
						if($user->Load())
						{
							if($user->non_invoiced)
							{
								return false;
							}
							$accountId = self::CreateNutshellAccount($user);
							$contactId = self::CreateNutshellContact($user);
							$docType = new DocumentType();
							$docType->document_type_name = 'Preliminary Notice';
							$docType->Load();
							$product = new Product($arr['product_id']);
							$product->Join('document_type', 'document_type_id');
							$product->Load();
							if($product->document_type_id == $docType->document_type_id)
							{
								$sourceType = 'Incomplete Order - Notice';
								$tag = 'Enterprise';
								$product->document_type_name = 'Enterprise';
							}
							else
							{
								$sourceType = 'Incomplete Order';
								$tag = 'Single Document';
							}
							list($source) = self::$nutshell->searchSources(array($sourceType));
							$prods = self::$nutshell->searchProducts(array($product->document_type_name));			
							foreach($prods as $p)
							{
								if($p->name == $product->document_type_name && !property_exists($p, 'deletedTime'))
								{
									$productObj = $p;
									break;
								}
							}
							$state = new State($arr['state_id']);
							$state->Load();
							
							$lead = array();
							$lead['primaryAccount'] = array('id' => $accountId);
							$lead['contacts'] = array(array('id' => $contactId));
							$lead['products'] = array(array('id' => $productObj->id));
							$lead['sources'] =  array(array('id' => $source->id));
							$leadObj = self::$nutshell->newLead(array('lead' => $lead));
							
							$leadwithTags = array();
							$leadwithTags['tags'] = array($tag);
							self::$nutshell->editLead($leadObj->id, $leadObj->rev, $leadwithTags);
							
							$notes=array();
							
							$notes[] = 'Automatic incomplete-order lead created for incomplete order #' . $incomplete_order_id;
							$notes[] = 'Incomplete Order Date: ' . date('l, F j, Y, g:i a', $incomplete_order->time_start);
							$notes[] = sprintf('Product: %s', $product->document_type_name);
							$notes[] = sprintf('State: %s', $state->state_name);
							$notes[] = sprintf('Project: %s', $arr['project_address']);
							$notes[] = sprintf('Work: %s', $arr['project_labor_description']);
							$notes[] = sprintf('Return URL: %s/x/wizard/return_incomplete/%d', WP_URL , $incomplete_order->incomplete_order_id);
															
							foreach($notes as $note)
							{
								self::$nutshell->addNote(array('entityType' => 'Leads', 'id' => $leadObj->id), $note);
							}
							return true;
						}
					}
				}
			}
			// else
			return false;
		}
		catch (Exception $e)
		{
			ExtendedException::Report((string)$e);
			return false;
		}
	}
	
	/*
	* Creates a new lead for a completed order.
	* param int $order_id the order ID for which to create the new lead.
	* return bool Whether a lead has been created.
	*/
	public static function CreateNewLeadForCompleteOrder($order_id)
	{
		$order = new Order($order_id);
		$order->Join('product', 'product_id');
		$order->Join('document_type', 'document_type_id', true);
		$order->Load();

		// Create leads for invoiced users only.
		if($order->project->user->non_invoiced)
		{
			return false;
		}

		foreach(self::$doctypes as $document_index => $document_name)
		{
			$doc_type = new DocumentType();
			$doc_type->document_type_name = ucwords($document_name);
			if($doc_type->Load() && $order->document_type_id == $doc_type->document_type_id)
			{
				try
				{
					list($source) = self::$nutshell->searchSources(array(self::$sources[$document_index]));
					$prods = self::$nutshell->searchProducts(array(self::$products[$document_index]));
					foreach($prods as $p)
					{
						if($p->name == self::$products[$document_index] && !property_exists($p, 'deletedTime'))
						{
							$product = $p;
							break;
						}
					}
					$accountId = self::CreateNutshellAccount($order->project->user);
					$contactId = self::CreateNutshellContact($order->project->user);

					$lead = array();
					$lead['primaryAccount'] = array('id' => $accountId);
					$lead['contacts'] = array(array('id' => $contactId));
					$lead['products'] = array(array('id' => $product->id));
					$lead['sources'] = array(array('id' => $source->id));
					$leadObj = self::$nutshell->newLead(array('lead'=>$lead));

					$leadwithTags = array();
					$leadwithTags['tags'] = array(self::$tags[$document_index]);

					self::$nutshell->editLead($leadObj->id, $leadObj->rev, $leadwithTags);
					$notes = array();
					$notes[] = 'Automatic completed-order lead created for order #' . $order_id;
					$notes[] = 'Order Number: ' . ADMIN_URL_SSL . '/orders/view_order/' . $order_id;
					if($order->order_completion_date)
					{
						$notes[] = 'Order Completion Date: ' . date('l, F j, Y, g:i a', strtotime($order->order_completion_date));
					}
					if($order->project->project_address)
					{
						$notes[]='Project Address: ' . $order->project->project_address;
					}
					foreach($notes as $note)
					{
						self::$nutshell->addNote(array('entityType' => 'Leads', 'id' => $leadObj->id), $note);
					}
				}
				catch(Exception $ex)
				{
					ExtendedException::Report('Nutshell Exception: ' . $ex->getMessage());
					return false;
				}
				return true;
			}
		}
		return false;
	}
	
	/*
	* Creates a new lead for a complete order manually
	* param int $order_id the order Id for which to create the new lead
	* return bool Whether a lead has been created.
	*/
	public static function CreateManualLeadForCompleteOrder($order_id)
	{
		try
		{
			$order = new Order($order_id);
			$order->Join('product', 'product_id');
			$order->Join('document_type', 'document_type_id', true);
			$order->Load();
			list($source) = self::$nutshell->searchSources(array('System'));
			$prods = self::$nutshell->searchProducts(array($order->document_type_name));
			$product = null;
			foreach($prods as $p)
			{
				if($p->name == $order->document_type_name && !property_exists($p, 'deletedTime'))
				{
					$product = $p;
					break;
				}
			}
			$accountId = self::CreateNutshellAccount($order->project->user);
			$contactId = self::CreateNutshellContact($order->project->user);
			$lead = array();
			$lead['primaryAccount'] = array('id' => $accountId);
			$lead['contacts'] = array(array('id' => $contactId));
			if($product)
			{
				$lead['products'] = array(array('id' => $product->id));
			}
			$lead['sources'] = array(array('id' => $source->id));
			$leadObj = self::$nutshell->newLead(array('lead' => $lead));			
			$leadwithTags = array();
			self::$nutshell->editLead($leadObj->id, $leadObj->rev, $leadwithTags);
			
			$notes = array();
			$notes[] = 'Manually exported order-based lead for order #' . $order_id;
			$notes[] = 'Order Number: ' . $order_id;
			if($order->order_completion_date)
			{
				$notes[] = 'Order Completion Date: ' . date('l, F j, Y, g:i a', strtotime($order->order_completion_date));
			}
			if($order->project->project_address)
			{
				$notes[] = 'Project Address: ' . $order->project->project_address;
			}
			

			foreach($notes as $note)
			{
				self::$nutshell->addNote(array('entityType' => 'Leads', 'id' => $leadObj->id), $note);
			}
			return true;
		}
		catch(Exception $e)
		{
			return false;
		}
		return false;
	}
	
	/*
	* Creates a new lead for a incomplete order manually
	* param int $deadline_id the deadline Id for which to create the new lead
	* return bool Whether a lead has been created.
	*/
	public static function CreateManualLeadForDeadline($project_deadline_id)
	{
		$project_deadline = new ProjectDeadline($project_deadline_id);
		$project_deadline->Join('project', 'project_id');
		$project_deadline->Join('deadline_type', 'deadline_type_id');
		if($project_deadline->Load())
		{
			try
			{
				$accountId = self::CreateNutshellAccount($project_deadline->project->user);
				$contactId = self::CreateNutshellContact($project_deadline->project->user);
				list($source) = self::$nutshell->searchSources(array('System'));
				$lead = array();
				$lead['primaryAccount'] = array('id' => $accountId);
				$lead['contacts'] = array(array('id' => $contactId));
				$lead['sources'] = array(array('id' => $source->id));
				$leadObj = self::$nutshell->newLead(array('lead' => $lead));
								
				$notes = array();
				$notes[] = 'Manually exported deadline-based lead for deadline #' . $project_deadline_id;
				if($project_deadline->project_address)
				{
					$notes[] = 'Project Address: ' . $project_deadline->project->project_address . ', ' . $project_deadline->project->project_city . ', ' . $project_deadline->project->state->state_name . ', ' . $project_deadline->project->project_zip;
				}
				
				if($project_deadline->deadline_type_name)
				{
					$notes[] = 'Deadline Name: ' . $project_deadline->deadline_type_name;
					$notes[] = 'Deadline Instructions: ' . $project_deadline->deadline_type_instructions;
				}
				foreach($notes as $note)
				{
					self::$nutshell->addNote(array('entityType'=>'Leads','id'=>$leadObj->id), $note);
				}
				return true;
			} 
			catch (Exception $e) 
			{
				return false;
			}
			return false;
		}
		else
		{
			return false;
		}
	}
	
	/*
	* Creates a new lead for a incomplete order manually
	* param int $incomplete_order_id the incomplete order Id for which to create the new lead
	* return int 1 when export is successful, 0 when order is not exportable, -1 when order export fails
	*/
	public static function CreateManualLeadForIncompleteOrder($incomplete_order_id)
	{
		try
		{
			
			$incomplete_order = new IncompleteOrders($incomplete_order_id);
			if($incomplete_order->Load() && $incomplete_order->page_4_session)
			{
				$arr = array_merge(
					unserialize($incomplete_order->page_4_session),
					unserialize($incomplete_order->page_4_request)
				);
				if(!$arr['user_id'] || !$arr['product_id'])
				{
					return 0;
				}
				$user = new User($arr['user_id']);
				$user->Load();
				$accountId = self::CreateNutshellAccount($user);
				$contactId = self::CreateNutshellContact($user);
				$product = new Product($arr['product_id']);
				$product->Join('document_type', 'document_type_id');
				$product->Load();
				list($source) = self::$nutshell->searchSources(array('System'));
				$prods = self::$nutshell->searchProducts(array($product->document_type_name));			
				foreach($prods as $p)
				{
					if($p->name == $product->document_type_name && !property_exists($p, 'deletedTime'))
					{
						$productObj = $p;
						break;
					}
				}
				
				$state = new State($arr['state_id']);
				$state->Load();
			
				$lead = array();
				$lead['primaryAccount'] = array('id' => $accountId);
				$lead['contacts'] = array(array('id' => $contactId));
				$lead['products'] = array(array('id' => $productObj->id));
				$lead['sources'] = array(array('id' => $source->id));
				$leadObj = self::$nutshell->newLead(array('lead' => $lead));

				$notes = array();
				$notes[] = 'Manually exported incomplete-order lead for incomplete order #' . $incomplete_order_id;
				$notes[] = 'Incomplete Order Date: ' . date('l, F j, Y, g:i a', $incomplete_order->time_start);
				$notes[] = sprintf('Product: %s', $product->document_type_name);
				$notes[] = sprintf('State: %s', $state->state_name);
				$notes[] = sprintf('Project: %s', $arr['project_address']);
				$notes[] = sprintf('Work: %s', $arr['project_labor_description']);
				$notes[] = sprintf('Return URL: %s/x/wizard/return_incomplete/%d', WP_URL, $incomplete_order->incomplete_order_id);
				foreach($notes as $note)
				{
					self::$nutshell->addNote(array('entityType' => 'Leads','id' => $leadObj->id), $note);
				}							
				return 1;
			}
			else
			{
				return 0;
			}
		}
		catch(Exception $e)
		{
			return -1;
		}
	}
}
NutshellManager::$nutshell = new NutshellApi(NUTSHELL_USERNAME, NUTSHELL_API_KEY);
?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.