name = $name . $time; $account->email1 = 'account@' . $time . 'sugar.com'; $account->save(); $GLOBALS['db']->commit(); self::$_createdAccount = $account; } return self::$_createdAccount; } private function _createOpportunity($id, $time, $account) { global $timedate; $name = 'SugarOpportunity'; $opportunity = new Opportunity(); if (!empty($id)) { $opportunity->new_with_id = true; $opportunity->id = $id; } $opportunity->name = $name . $time; $opportunity->amount = 10000; $opportunity->account_id = $account->id; $opportunity->account_name = $account->name; $opportunity->date_closed = $timedate->to_display_date_time(gmdate("Y-m-d H:i:s")); $opportunity->save(); $GLOBALS['db']->commit(); self::$_createdOpportunities[] = $opportunity; return $opportunity; } public static function createOpportunity($id = '', Account $account = null) { $time = mt_rand(); if ($account === null) { $account = self::_createAccount($time); } $opportunity = self::_createOpportunity($id, $time, $account); return $opportunity; } public static function setCreatedOpportunity($opportunity_ids) { foreach ($opportunity_ids as $opportunity_id) { $opportunity = new Opportunity(); $opportunity->id = $opportunity_id; self::$_createdOpportunities[] = $opportunity; } } public static function removeAllCreatedOpportunities() { $opportunity_ids = self::getCreatedOpportunityIds(); if (!empty($opportunity_ids)) { $GLOBALS['db']->query('DELETE FROM opportunities WHERE id IN (\'' . implode("', '", $opportunity_ids) . '\')'); $GLOBALS['db']->query('DELETE FROM opportunities_contacts WHERE opportunity_id IN (\'' . implode("', '", $opportunity_ids) . '\')'); } if (self::$_createdAccount !== null && self::$_createdAccount->id) { $GLOBALS['db']->query('DELETE FROM accounts WHERE id = \'' . self::$_createdAccount->id . '\''); } } public static function getCreatedOpportunityIds() { $opportunity_ids = array(); foreach (self::$_createdOpportunities as $opportunity) { $opportunity_ids[] = $opportunity->id; } return $opportunity_ids; } } ?>