Created
February 25, 2014 19:19
-
-
Save chicks/9215706 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
<?php | |
class Opportunities { | |
public function __construct() { | |
# Instantiate this so we can read all of our resources into RAM ONCE | |
$this->generator = new OpportunityGenerator(); | |
} | |
public function populateForAccount(&$account) { | |
# Need our demo array | |
global $sugar_demodata; | |
# and app strings, so we can set values like Lead Source dynamically. | |
global $app_list_strings; | |
# placeholder for scope | |
$opportunitiess = false; | |
# Load Related Opps | |
if ($account->load_relationship('opportunities')) { | |
$opportunities = $account->opportunities->getBeans(); | |
} | |
# If we don't have any opportunities, we need to create some. | |
if (!$opportunities || count($opportunities) < 1) { | |
# Pick a random number of opportunitys to create | |
$total = rand(1, $sugar_demodata['variables']['max_opportunities_per_account']); | |
# Create them | |
for($i=0; $i<$total; $i++) { | |
$opportunity = $this->create_or_replace_opportunity_for($account); | |
# Create a relationship between opportunity and account | |
$account->opportunities->add($opportunity->id); | |
# Create a relationship between opportunity and a random contact | |
$this->relate_to_random_contact($account, $opportunity); | |
} | |
# Save the account | |
$account->save(); | |
} else { | |
# Replace the opportunitys, keeping relationships in tact | |
foreach($opportunities as $opportunity) { | |
$this->create_or_replace_opportunity_for($account, $opportunity); | |
} | |
} | |
} | |
# Relates an opportunity to a random contact on an account | |
private function relate_to_random_contact(&$account, &$opportunity) { | |
# Placeholders for scope | |
$contacts = false; | |
$contact = false; | |
# Load Related Contacts | |
if ($account->load_relationship('contacts')) { | |
$contacts = $account->contacts->getBeans(); | |
} | |
# Handle Contacs vs. No contacts | |
if (count($contacts) > 0) { | |
# Select a random contact | |
$contact = $contacts[array_rand($contacts)]; | |
} else { | |
# Complain if we don't have any contacts | |
die("Expected Contacts on Account: " . $account->name); | |
} | |
# Load our relationship | |
$contact->load_relationship('opportunities'); | |
# Add to the opportunities list | |
$contact->opportunities->add($opportunity); | |
} | |
# Create or replace an opportunity for a given account | |
private function create_or_replace_opportunity_for(&$account, &$opportunity=false) { | |
$opportunity_array = $this->generator->opportunityForAccount($account); | |
# Let's replace the fields on opportunitys that already exist to make it easier to change attributes | |
if ($opportunity) { | |
echo("Replacing Opportunity: " . $opportunity->name . ": " . $opportunity->id . "\n"); | |
# Remove revenue line items | |
$this->remove_revenue_line_items_for($opportunity); | |
} else { | |
echo("Creating Opportunity: " . $opportunity_array['name'] . "\n"); | |
$opportunity = BeanFactory::retrieveBean('Opportunities'); | |
} | |
# Merge fields | |
foreach($opportunity_array as $field => $value){ | |
$opportunity->$field = $value; | |
} | |
# Save! | |
$opportunity->save(); | |
echo("Oppty Bean State: \n "); | |
// stop obsessive saving | |
SugarBean::enterOperation('saving_related'); | |
BeanFactory::registerBean($opportunity); | |
$rli = $this->create_revenue_line_items_for($opportunity, $opportunity_array); | |
SugarBean::leaveOperation('saving_related'); | |
# save it again?! | |
$opportunity->save(); | |
$worksheet = BeanFactory::getBean('ForecastWorksheets'); | |
$worksheet->saveRelatedOpportunity($opportunity); | |
$worksheet->saveOpportunityProducts($opportunity); | |
return $opportunity; | |
} | |
private function create_revenue_line_items_for(&$opportunity, &$opportunity_array) { | |
# Create a revenue line item | |
$rli_array = RevenueLineItemGenerator::fromOpportunity($opportunity, $opportunity_array); | |
# Create a new RLI | |
$rli = BeanFactory::getBean('RevenueLineItems'); | |
# Merge fields | |
foreach($rli_array as $field => $value) { | |
$rli->$field = $value; | |
} | |
# Save! | |
$rli->save(); | |
return $rli; | |
} | |
private function remove_revenue_line_items_for(&$opportunity) { | |
# Find them... | |
$query = new SugarQuery(); | |
$query->from(BeanFactory::getBean('RevenueLineItems')) | |
->where() | |
->equals('opportunity_id', $opportunity->id); | |
$rli_rows = $query->execute(); | |
# ...and delete them | |
foreach($rli_rows as $rli_row) { | |
BeanFactory::deleteBean('RevenueLineItems', $rli_row['id']); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment