· 9 years ago · Jun 30, 2016, 10:24 AM
1<?php
2
3class Polcode_Crm_Model_Proxy_Sugar extends Polcode_Crm_Model_Proxy implements Polcode_Crm_Model_Proxy_Interface {
4
5 protected $_code = 'sugar';
6 private $instanceUrl;
7 private $username;
8 private $password;
9 private $authUrl;
10 private $oauthToken;
11
12 public function __construct() {
13 $this->getConfiguration();
14 $this->getOauthToken();
15
16 parent::__construct();
17 }
18
19 private function getConfiguration() {
20 $this->instanceUrl = $this->getConfigData('api');
21 $this->username = $this->getConfigData('login');
22 $this->password = $this->getConfigData('password');
23 }
24
25 private function getOauthToken() {
26 $this->authUrl = $this->instanceUrl . "/oauth2/token";
27
28 $oauthTokenArguments = array(
29 "grant_type" => "password",
30 "client_id" => "sugar",
31 "client_secret" => "",
32 "username" => $this->username,
33 "password" => $this->password,
34 "platform" => "custom"
35 );
36
37 $authRequest = curl_init($this->authUrl);
38 curl_setopt($authRequest, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
39 curl_setopt($authRequest, CURLOPT_HEADER, false);
40 curl_setopt($authRequest, CURLOPT_SSL_VERIFYPEER, 0);
41 curl_setopt($authRequest, CURLOPT_RETURNTRANSFER, 1);
42 curl_setopt($authRequest, CURLOPT_FOLLOWLOCATION, 0);
43 curl_setopt($authRequest, CURLOPT_HTTPHEADER, array(
44 "Content-Type: application/json"
45 ));
46
47 //convert arguments to json
48 $jsonArguments = json_encode($oauthTokenArguments);
49 curl_setopt($authRequest, CURLOPT_POSTFIELDS, $jsonArguments);
50
51 //execute request
52 $oauthTokenResponse = curl_exec($authRequest);
53
54 //decode oauth2 response to get token
55 $oauthTokenResponseObj = json_decode($oauthTokenResponse);
56 $this->oauthToken = $oauthTokenResponseObj->access_token;
57 }
58
59 private function curlSetopt($request) {
60 curl_setopt($request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
61 curl_setopt($request, CURLOPT_HEADER, false);
62 curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
63 curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
64 curl_setopt($request, CURLOPT_FOLLOWLOCATION, 0);
65 curl_setopt($request, CURLOPT_HTTPHEADER, array(
66 "Content-Type: application/json",
67 "oauth-token: {$this->oauthToken}"
68 ));
69 }
70
71 public function getCustomers() {
72 $fetchUrl = $this->instanceUrl . "/Accounts?max_num=2";
73
74 $fetchRequest = curl_init($fetchUrl);
75 $this->curlSetopt($fetchRequest);
76
77 //execute request
78 $fetchResponse = curl_exec($fetchRequest);
79 $fetchResponseObj = json_decode($fetchResponse);
80
81 $customers = new Varien_Data_Collection;
82
83 foreach ($fetchResponseObj->records as $item) {
84 /* @var $customer Varien_Object */
85 $customer = new Varien_Object;
86
87 $customer->setId($item->id);
88 $personal = explode(' ', $item->name);
89 $customer->setFirstname($personal[0]);
90 $customer->setLastname($personal[1]);
91 $customer->setEmail($item->email1);
92 $customer->setPassword(md5($personal[0]));
93
94 $customers->addItem($customer);
95 }
96
97 /* return Varien Data Collection */
98 return $customers;
99 }
100
101 public function getProducts() {
102 $fetchUrl = $this->instanceUrl . "/Products?max_num=2";
103
104 $fetchRequest = curl_init($fetchUrl);
105 $this->curlSetopt($fetchRequest);
106
107 //execute request
108 $fetchResponse = curl_exec($fetchRequest);
109 $fetchResponseObj = json_decode($fetchResponse);
110
111 $products = new Varien_Data_Collection;
112
113 foreach ($fetchResponseObj->records as $item) {
114 $product = new Varien_Object;
115
116 $product->setSku($item->id)
117 ->setName($item->name)
118 ->setDescription($item->description)
119 ->setShortDescription('eeee')
120 ->setSku($item->id)
121 ->setWeight($item->weight)
122 ->setStatus(1)
123 ->setPrice($item->cost_price)
124 ->setVisibility(4)
125 ->setTaxClassId(4)
126 ->setStockData(array(
127 'use_config_manage_stock' => 0,
128 'manage_stock' => 1,
129 'min_sale_qty' => 1,
130 'max_sale_qty' => 10000,
131 'is_in_stock' => 1,
132 'qty' => $item->quantity
133 ));
134
135 $products->addItem($product);
136 }
137
138 return $products;
139 }
140
141 public function saveCustomer($customer) {
142 $response = $this->checkAccountExist($customer->getEmail());
143
144 if (empty($response->records)) {
145 $url = $this->instanceUrl . "/Accounts";
146
147 $record = array(
148 'name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
149 'email1' => $customer->getEmail(),
150 'first_name' => $customer->getFirstname(),
151 'last_name' => $customer->getLastname(),
152 );
153
154 $curl_request = curl_init($url);
155 $this->curlSetopt($curl_request);
156
157 //convert arguments to json
158 $json_arguments = json_encode($record);
159 curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
160 //execute request
161 $curl_response = curl_exec($curl_request);
162 //decode json
163 $createdRecord = json_decode($curl_response);
164
165 //display the created record
166 curl_close($curl_request);
167
168 return $createdRecord;
169 }
170 }
171
172 public function saveProduct($product) {
173 $response = $this->checkProductExist($product->getSku());
174
175 if (empty($response->recodrs)) {
176 $url = $this->instanceUrl . "/Products";
177
178 $record = array(
179 'name' => $product->getName(),
180 'cost_price' => $product->getPrice(),
181 'weight' => $product->getWeight(),
182 );
183
184 $curl_request = curl_init($url);
185 $this->curlSetopt($curl_request);
186
187 //convert arguments to json
188 $json_arguments = json_encode($record);
189
190 curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
191 //execute request
192 $curl_response = curl_exec($curl_request);
193
194 //decode json
195 $createdRecord = json_decode($curl_response);
196
197 //display the created record
198 curl_close($curl_request);
199
200 return $createdRecord;
201 }
202 }
203
204 public function checkAccountExist($param) {
205 $fetchUrl = $this->instanceUrl . "/Accounts/";
206
207 $data = array(
208 'filter' => array(
209 '$equals' => array(
210 'email1' => $param
211 )
212 )
213 );
214
215 //Add data to the URL
216 $fetchUrl = $fetchUrl . "?" . http_build_query($data);
217
218 $fetchRequest = curl_init($fetchUrl);
219 $this->curlSetopt($fetchRequest);
220
221 //execute request
222 $fetchResponse = curl_exec($fetchRequest);
223 $fetchResponseObj = json_decode($fetchResponse);
224
225 return $fetchResponseObj;
226 }
227
228 public function checkProductExist($param) {
229 $fetchUrl = $this->instanceUrl . "/Products/";
230
231 $data = array(
232 'filter' => array(
233 '$equals' => array(
234 'id' => $param
235 )
236 )
237 );
238
239 //Add data to the URL
240 $fetchUrl = $fetchUrl . "?" . http_build_query($data);
241
242 $fetchRequest = curl_init($fetchUrl);
243 $this->curlSetopt($fetchRequest);
244
245 //execute request
246 $fetchResponse = curl_exec($fetchRequest);
247 $fetchResponseObj = json_decode($fetchResponse);
248
249 return $fetchResponseObj;
250 }
251
252}