Skip to content

Instantly share code, notes, and snippets.

@nanasess
Created June 16, 2015 05:40
Show Gist options
  • Save nanasess/7b06b757783417e27cef to your computer and use it in GitHub Desktop.
Save nanasess/7b06b757783417e27cef to your computer and use it in GitHub Desktop.
EC-CUBE セールスフォース連携ヘルパー
<?php
require_once DATA_REALDIR . 'module/force.com-toolkit/SforcePartnerClient.php';
/**
* EC-CUBE セールスフォース連携ヘルパー
*/
class SC_Helper_SForce {
private static $instance;
protected function __construct() {
// quiet.
}
public static function getInstance() {
if (!isset(self::$instance)) {
$clazz = __CLASS__;
self::$instance = new $clazz;
}
return self::$instance;
}
public function getConnection() {
$sforceConnection = new SforcePartnerClient();
$mySoapClient = $sforceConnection->createConnection(SF_PARTNER_WSDL);
$sforceConnection->login(SF_USER, SF_PASSWORD);
return $sforceConnection;
}
public function getResults($query) {
$connection = $this->getConnection();
$queryResult = $connection->query($query);
$results = $queryResult->records;
while (!$queryResult->done) {
$queryResult = $connection->queryMore($queryResult->queryLocator);
$results = array_merge($results, $queryResult->records);
}
return $results;
}
/**
* $query の結果を連想配列で取得する.
* 検索カラムに, 必ず Id を含めること.
*
* 例) SELECT Id, Name From Account WHERE Name = 'ループアズ'
*/
public function getResultsAsArray($query, $key_name = "") {
$records = $this->getResults($query);
$results = array();
foreach ($records as $record) {
$sObject = new SObject($record);
if ($key_name == "") {
$key = $sObject->Id;
} else {
$key = $sObject->fields->$key_name;
}
$results[$key] = array();
foreach ($sObject->fields as $name => $value) {
$results[$key][$name] = $value;
}
}
return $results;
}
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function isError(array $arrResults) {
foreach ($arrResults as $result) {
if (isset($result->errors)) {
return true;
}
}
return false;
}
/**
* 取引先を登録する.
*
* $sf_id が null の場合は新規登録
*/
public function registerAccount($sf_id, $arrAccount) {
if (!defined('PRODUCTION_MODE') || !PRODUCTION_MODE) {
return;
}
$masterData = new SC_DB_MasterData();
$arrPref = $masterData->getMasterData('mtb_pref');
$connection = $this->getConnection();
$sObject = new SObject();
$sObject->type = 'Account';
$sObject->fields = array('RecordTypeId' => '<RecordTypeId>',
'Name' => htmlspecialchars($arrAccount['account_name'], ENT_QUOTES),
'BillingPostalCode' => $arrAccount['zip01'] . '-' . $arrAccount['zip02'],
'BillingState' => $arrPref[$arrAccount['pref_id']],
'BillingCity' => htmlspecialchars($arrAccount['addr01'], ENT_QUOTES),
'BillingStreet' => htmlspecialchars($arrAccount['addr02'], ENT_QUOTES),
'Phone' => $arrAccount['tel01'] . '-' . $arrAccount['tel02'] . '-' . $arrAccount['tel03'],
'Fax' => $arrAccount['fax01'] . '-' . $arrAccount['fax02'] . '-' . $arrAccount['fax03']);
if (SC_Utils::isBlank($sf_id)) {
$result = $connection->create(array($sObject));
} else {
$sObject->fields['Id'] = $sf_id;
$person_contact_id = $this->getPersonContactId($sf_id);
if ($person_contact_id !== false) {
// 個人アカウントで更新できない項目は除外
unset($sObject->fields['RecordTypeId']);
unset($sObject->fields['Name']);
}
$result = $connection->update(array($sObject));
}
if ($this->isError($result)) {
$arrErr = array($arrAccount, $result);
$this->sendError(print_r($arrErr, true));
throw new Exception(print_r($result, true));
}
return $result[0]->id;
}
/**
* 取引先責任者を登録する.
*
* $sf_id が null の場合は新規登録
*/
public function registerContact($sf_id, $arrContact, $account_sf_id) {
if (!defined('PRODUCTION_MODE') || !PRODUCTION_MODE) {
return;
}
if ($arrContact['mailmaga_flg'] == 1) {
$mailmaga_flg = 1;
} else {
$mailmaga_flg = 0;
}
$connection = $this->getConnection();
$sObject = new SObject();
$sObject->type = 'Contact';
$sObject->fields = array('LastName' => htmlspecialchars($arrContact['name01'], ENT_QUOTES),
'FirstName' => htmlspecialchars($arrContact['name02'], ENT_QUOTES),
'Email' => $arrContact['email'],
'Mail_OK__c' => $mailmaga_flg,
'AccountId' => $account_sf_id);
if (SC_Utils::isBlank($sf_id)) {
$result = $connection->create(array($sObject));
$id = $result[0]->id;
} else {
$person_contact_id = $this->getPersonContactId($account_sf_id);
$sObject->fields['Id'] = $sf_id;
if ($person_contact_id !== false) {
// 個人アカウントで更新できない項目は除外
unset($sObject->fields['AccountId']);
$result = $connection->update(array($sObject));
$id = $sf_id; // 個人取引先の場合は, AccountId を返すため, Contact の Id を結果にする
} else {
$result = $connection->update(array($sObject));
$id = $result[0]->id;
}
}
if ($this->isError($result)) {
$arrErr = array($arrContact, $result);
$this->sendError(print_r($arrErr, true));
throw new Exception(print_r($result, true));
}
return $id;
}
/**
* 契約を登録する.
*
* $sf_id が null の場合は新規登録
*/
public function registerContract($sf_id, $arrContract, $account_sf_id, $contact_sf_id) {
if (!defined('PRODUCTION_MODE') || !PRODUCTION_MODE) {
return;
}
$masterData = new SC_DB_MasterData();
$arrAccountStatus = $masterData->getMasterData('mtb_account_status');
$connection = $this->getConnection();
$sObject = new SObject();
$sObject->type = 'Contract';
$sObject->fields = array('RecordTypeId' => '<RecordTypeId>',
'AccountId' => $account_sf_id,
'accountuser__c' => $contact_sf_id,
'Name' => htmlspecialchars($arrContract['contract_name'], ENT_QUOTES),
'Status' => $arrAccountStatus[$arrContract['account_status_id']],
'IP__c' => $arrContract['trial_url'],
'SITE_NAME__c' => $arrContract['site_url'],
'asp_site_id__c' => $arrContract['contract_id']);
if (!SC_Utils::isBlank($arrContract['trial_date_from'])) {
$sobject->fields['trial_start__c'] = $arrContract['trial_date_from'];
}
if (!SC_Utils::isBlank($arrContract['trial_date_to'])) {
$sObject->fields['Trial_fin__c'] = $arrContract['trial_date_to'];
}
if (!SC_Utils::isBlank($arrContract['asp_password'])) {
$sObject->fields['password__c'] = $arrContract['asp_password'];
}
if (SC_Utils::isBlank($sf_id)) {
$result = $connection->create(array($sObject));
} else {
$sObject->fields['Id'] = $sf_id;
$result = $connection->update(array($sObject));
}
if ($this->isError($result)) {
$arrErr = array($arrContract, $result);
$this->sendError(print_r($arrErr, true));
throw new Exception(print_r($result, true));
}
return $result[0]->id;
}
public function sendError($error) {
$body = "情報が反映されていない可能性があります。\n"
. "セールスフォースにログインし、確認をお願いします。\n\n"
. "エラーメッセージ: " . $error;
$objMail = new SC_SendMail();
$objMail->setItem(
"" // 宛先
, "セールスフォースと通信中にエラーが発生しました" // サブジェクト
, $body // 本文
, SUPPORT_MAIL // 配送元アドレス
, "EC-CUBE" // 配送元 名前
, SUPPORT_MAIL // reply_to
, SUPPORT_MAIL // return_path
, SUPPORT_MAIL // Errors_to
, SUPPORT_MAIL // Bcc
);
// 宛先の設定
$objMail->setTo(SUPPORT_MAIL);
$objMail->sendMail();
}
/**
* 個人アカウントの場合, 個人取引先責任者IDを取得する.
*
* 法人アカウントの場合は false を返す
*/
protected function getPersonContactId($id) {
$query = "SELECT Id, IsPersonAccount, PersonContactId FROM Account WHERE Id = '" . $id . "'";
$arrResults = $this->getResultsAsArray($query);
if (!empty($arrResults)) {
$arrKey = array_keys($arrResults);
$is_person = (bool) $arrResults[$arrKey[0]]['IsPersonAccount'];
if (!$is_person) {
return false;
}
return $arrResults[$arrKey[0]]['PersonContactId'];
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment